Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autoware_utils_math): split package #33

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions autoware_utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ The geometry module provides classes and functions for handling 2D and 3D points
- Intersection checks for convex polygons using GJK.
- Conversion between different coordinate systems.

#### Math Module

The math module offers a variety of mathematical utilities:

- **`accumulator.hpp`**: A class for accumulating statistical data, supporting min, max, and mean calculations.
- **`constants.hpp`**: Defines commonly used mathematical constants like π and gravity.
- **`normalization.hpp`**: Functions for normalizing angles and degrees.
- **`range.hpp`**: Functions for generating sequences of numbers (arange, linspace).
- **`trigonometry.hpp`**: Optimized trigonometric functions for faster computation.
- **`unit_conversion.hpp`**: Functions for converting between different units (e.g., degrees to radians, km/h to m/s).

#### ROS Module

The ROS module provides utilities for working with ROS messages and nodes:
Expand Down Expand Up @@ -110,27 +99,6 @@ int main() {
}
```

#### Using Accumulator from accumulator.hpp

```cpp
#include "autoware_utils/math/accumulator.hpp"

int main() {
autoware_utils::Accumulator<double> acc;

acc.add(1.0);
acc.add(2.0);
acc.add(3.0);

std::cout << "Mean: " << acc.mean() << "\n";
std::cout << "Min: " << acc.min() << "\n";
std::cout << "Max: " << acc.max() << "\n";
std::cout << "Count: " << acc.count() << "\n";

return 0;
}
```

### Detailed Usage Examples

#### Update Parameters Dynamically with update_param.hpp
Expand Down
74 changes: 4 additions & 70 deletions autoware_utils/include/autoware_utils/math/accumulator.hpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are keeping these header files in autoware_utils for backward compatibility. Could you add comment somewhere in the files stating that this file will be deprecated in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added deprecated messages fba6738

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Tier IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,81 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>
#include <limits>

#ifndef AUTOWARE_UTILS__MATH__ACCUMULATOR_HPP_
#define AUTOWARE_UTILS__MATH__ACCUMULATOR_HPP_

#include <autoware_utils_math/accumulator.hpp>

namespace autoware_utils
{
/**
* @brief class to accumulate statistical data, supporting min, max and mean.
* @typedef T type of the values (default to double)
*/
template <typename T = double>
class Accumulator
{
public:
/**
* @brief add a value
* @param value value to add
*/
void add(const T & value)
{
if (value < min_) {
min_ = value;
}
if (value > max_) {
max_ = value;
}
++count_;
mean_ = mean_ + (value - mean_) / count_;
}

/**
* @brief get the mean value
*/
long double mean() const { return mean_; }

/**
* @brief get the minimum value
*/
T min() const { return min_; }

/**
* @brief get the maximum value
*/
T max() const { return max_; }

/**
* @brief get the number of values used to build this statistic
*/
unsigned int count() const { return count_; }

template <typename U>
friend std::ostream & operator<<(std::ostream & os, const Accumulator<U> & accumulator);

private:
T min_ = std::numeric_limits<T>::max();
T max_ = std::numeric_limits<T>::lowest();
long double mean_ = 0.0;
unsigned int count_ = 0;
};

/**
* @brief overload << operator for easy print to output stream
*/
template <typename T>
std::ostream & operator<<(std::ostream & os, const Accumulator<T> & accumulator)
{
if (accumulator.count() == 0) {
os << "None None None";
} else {
os << accumulator.min() << " " << accumulator.max() << " " << accumulator.mean();
}
return os;
}
using namespace autoware_utils_math; // NOLINT(build/namespaces)

} // namespace autoware_utils

Expand Down
9 changes: 6 additions & 3 deletions autoware_utils/include/autoware_utils/math/constants.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 TIER IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,13 @@
#ifndef AUTOWARE_UTILS__MATH__CONSTANTS_HPP_
#define AUTOWARE_UTILS__MATH__CONSTANTS_HPP_

#include <autoware_utils_math/constants.hpp>

namespace autoware_utils
{
constexpr double pi = 3.14159265358979323846; // To be replaced by std::numbers::pi in C++20
constexpr double gravity = 9.80665;

using namespace autoware_utils_math; // NOLINT(build/namespaces)

} // namespace autoware_utils

#endif // AUTOWARE_UTILS__MATH__CONSTANTS_HPP_
29 changes: 3 additions & 26 deletions autoware_utils/include/autoware_utils/math/normalization.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 TIER IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,35 +15,12 @@
#ifndef AUTOWARE_UTILS__MATH__NORMALIZATION_HPP_
#define AUTOWARE_UTILS__MATH__NORMALIZATION_HPP_

#include "autoware_utils/math/constants.hpp"

#include <cmath>
#include <autoware_utils_math/normalization.hpp>

namespace autoware_utils
{
inline double normalize_degree(const double deg, const double min_deg = -180)
{
const auto max_deg = min_deg + 360.0;

const auto value = std::fmod(deg, 360.0);
if (min_deg <= value && value < max_deg) {
return value;
}

return value - std::copysign(360.0, value);
}

inline double normalize_radian(const double rad, const double min_rad = -pi)
{
const auto max_rad = min_rad + 2 * pi;

const auto value = std::fmod(rad, 2 * pi);
if (min_rad <= value && value < max_rad) {
return value;
}

return value - std::copysign(2 * pi, value);
}
using namespace autoware_utils_math; // NOLINT(build/namespaces)

} // namespace autoware_utils

Expand Down
57 changes: 3 additions & 54 deletions autoware_utils/include/autoware_utils/math/range.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 TIER IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,63 +15,12 @@
#ifndef AUTOWARE_UTILS__MATH__RANGE_HPP_
#define AUTOWARE_UTILS__MATH__RANGE_HPP_

#include <cmath>
#include <limits>
#include <stdexcept>
#include <vector>
#include <autoware_utils_math/range.hpp>

namespace autoware_utils
{
template <class T>
std::vector<T> arange(const T start, const T stop, const T step = 1)
{
if (step == 0) {
throw std::invalid_argument("step must be non-zero value.");
}

if (step > 0 && stop < start) {
throw std::invalid_argument("must be stop >= start for positive step.");
}

if (step < 0 && stop > start) {
throw std::invalid_argument("must be stop <= start for negative step.");
}

const double max_i_double = std::ceil(static_cast<double>(stop - start) / step);
const auto max_i = static_cast<size_t>(max_i_double);

std::vector<T> out;
out.reserve(max_i);
for (size_t i = 0; i < max_i; ++i) {
out.push_back(start + i * step);
}

return out;
}

template <class T>
std::vector<double> linspace(const T start, const T stop, const size_t num)
{
const auto start_double = static_cast<double>(start);
const auto stop_double = static_cast<double>(stop);

if (num == 0) {
return {};
}

if (num == 1) {
return {start_double};
}

std::vector<double> out;
out.reserve(num);
const double step = (stop_double - start_double) / static_cast<double>(num - 1);
for (size_t i = 0; i < num; i++) {
out.push_back(start_double + static_cast<double>(i) * step);
}

return out;
}
using namespace autoware_utils_math; // NOLINT(build/namespaces)

} // namespace autoware_utils

Expand Down
9 changes: 3 additions & 6 deletions autoware_utils/include/autoware_utils/math/sin_table.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,15 +15,12 @@
#ifndef AUTOWARE_UTILS__MATH__SIN_TABLE_HPP_
#define AUTOWARE_UTILS__MATH__SIN_TABLE_HPP_

#include <cstddef>
#include <autoware_utils_math/sin_table.hpp>

namespace autoware_utils
{

constexpr size_t sin_table_size = 32769;
constexpr size_t discrete_arcs_num_90 = 32768;
constexpr size_t discrete_arcs_num_360 = 131072;
extern const float g_sin_table[sin_table_size];
using namespace autoware_utils_math; // NOLINT(build/namespaces)

} // namespace autoware_utils

Expand Down
12 changes: 3 additions & 9 deletions autoware_utils/include/autoware_utils/math/trigonometry.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,18 +15,12 @@
#ifndef AUTOWARE_UTILS__MATH__TRIGONOMETRY_HPP_
#define AUTOWARE_UTILS__MATH__TRIGONOMETRY_HPP_

#include <utility>
#include <autoware_utils_math/trigonometry.hpp>

namespace autoware_utils
{

float sin(float radian);

float cos(float radian);

std::pair<float, float> sin_and_cos(float radian);

float opencv_fast_atan2(float dy, float dx);
using namespace autoware_utils_math; // NOLINT(build/namespaces)

} // namespace autoware_utils

Expand Down
22 changes: 3 additions & 19 deletions autoware_utils/include/autoware_utils/math/unit_conversion.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 TIER IV, Inc.
// Copyright 2025 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,29 +15,13 @@
#ifndef AUTOWARE_UTILS__MATH__UNIT_CONVERSION_HPP_
#define AUTOWARE_UTILS__MATH__UNIT_CONVERSION_HPP_

#include "autoware_utils/math/constants.hpp"
#include <autoware_utils_math/unit_conversion.hpp>

namespace autoware_utils
{
constexpr double deg2rad(const double deg)
{
return deg * pi / 180.0;
}

constexpr double rad2deg(const double rad)
{
return rad * 180.0 / pi;
}
using namespace autoware_utils_math; // NOLINT(build/namespaces)

constexpr double kmph2mps(const double kmph)
{
return kmph * 1000.0 / 3600.0;
}

constexpr double mps2kmph(const double mps)
{
return mps * 3600.0 / 1000.0;
}
} // namespace autoware_utils

#endif // AUTOWARE_UTILS__MATH__UNIT_CONVERSION_HPP_
1 change: 1 addition & 0 deletions autoware_utils/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<depend>autoware_perception_msgs</depend>
<depend>autoware_planning_msgs</depend>
<depend>autoware_utils_geometry</depend>
<depend>autoware_utils_math</depend>
<depend>autoware_utils_pcl</depend>
<depend>autoware_vehicle_msgs</depend>
<depend>builtin_interfaces</depend>
Expand Down
17 changes: 17 additions & 0 deletions autoware_utils_math/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.14)
project(autoware_utils_math)

find_package(autoware_cmake REQUIRED)
autoware_package()

ament_auto_add_library(${PROJECT_NAME} SHARED
"src/sin_table.cpp"
"src/trigonometry.cpp"
)

if(BUILD_TESTING)
file(GLOB_RECURSE test_files test/**/*.cpp)
ament_auto_add_gtest(test_${PROJECT_NAME} ${test_files})
endif()

ament_auto_package()
Loading