forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(distortion_corrector_node): performance tuning (autowarefoundati…
…on#2913) * Avoid unnecessary object instantiation Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix * Fix: Avoid unnecessary object instantiation Signed-off-by: Takahiro Ishikawa <[email protected]> * Minimize object instantiation Signed-off-by: Takahiro Ishikawa <[email protected]> * Avoid transform computation if possible Signed-off-by: Takahiro Ishikawa <[email protected]> * Pre-compute sin/cos Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix * Place sincos precompute under util Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix * Use hardcoded sin values for approcimation Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix * Use new approximated sin/cos functions Signed-off-by: Takahiro Ishikawa <[email protected]> * Improve accuracy of approximated sin/cos Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix * Add test for trigonometry Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix * Fix Signed-off-by: Takahiro Ishikawa <[email protected]> * Fix Signed-off-by: Takahiro Ishikawa <[email protected]> * Conform to clang-tidy Signed-off-by: Takahiro Ishikawa <[email protected]> * style(pre-commit): autofix --------- Signed-off-by: Takahiro Ishikawa <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d01c5cf
commit b72ca85
Showing
8 changed files
with
8,413 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
common/tier4_autoware_utils/include/tier4_autoware_utils/math/sin_table.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2023 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef TIER4_AUTOWARE_UTILS__MATH__SIN_TABLE_HPP_ | ||
#define TIER4_AUTOWARE_UTILS__MATH__SIN_TABLE_HPP_ | ||
|
||
#include <cstddef> | ||
|
||
namespace tier4_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]; | ||
|
||
} // namespace tier4_autoware_utils | ||
|
||
#endif // TIER4_AUTOWARE_UTILS__MATH__SIN_TABLE_HPP_ |
27 changes: 27 additions & 0 deletions
27
common/tier4_autoware_utils/include/tier4_autoware_utils/math/trigonometry.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef TIER4_AUTOWARE_UTILS__MATH__TRIGONOMETRY_HPP_ | ||
#define TIER4_AUTOWARE_UTILS__MATH__TRIGONOMETRY_HPP_ | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
|
||
float sin(float radian); | ||
|
||
float cos(float radian); | ||
|
||
} // namespace tier4_autoware_utils | ||
|
||
#endif // TIER4_AUTOWARE_UTILS__MATH__TRIGONOMETRY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8,215 changes: 8,215 additions & 0 deletions
8,215
common/tier4_autoware_utils/src/math/sin_table.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2023 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "tier4_autoware_utils/math/trigonometry.hpp" | ||
|
||
#include "tier4_autoware_utils/math/constants.hpp" | ||
#include "tier4_autoware_utils/math/sin_table.hpp" | ||
|
||
#include <cmath> | ||
|
||
namespace tier4_autoware_utils | ||
{ | ||
|
||
float sin(float radian) | ||
{ | ||
float degree = radian * (180.f / static_cast<float>(tier4_autoware_utils::pi)) * | ||
(discrete_arcs_num_360 / 360.f); | ||
size_t idx = | ||
(static_cast<int>(std::round(degree)) % discrete_arcs_num_360 + discrete_arcs_num_360) % | ||
discrete_arcs_num_360; | ||
|
||
float mul = 1.f; | ||
if (discrete_arcs_num_90 <= idx && idx < 2 * discrete_arcs_num_90) { | ||
idx = 2 * discrete_arcs_num_90 - idx; | ||
} else if (2 * discrete_arcs_num_90 <= idx && idx < 3 * discrete_arcs_num_90) { | ||
mul = -1.f; | ||
idx = idx - 2 * discrete_arcs_num_90; | ||
} else if (3 * discrete_arcs_num_90 <= idx && idx < 4 * discrete_arcs_num_90) { | ||
mul = -1.f; | ||
idx = 4 * discrete_arcs_num_90 - idx; | ||
} | ||
|
||
return mul * g_sin_table[idx]; | ||
} | ||
|
||
float cos(float radian) { return sin(radian + static_cast<float>(tier4_autoware_utils::pi) / 2.f); } | ||
|
||
} // namespace tier4_autoware_utils |
42 changes: 42 additions & 0 deletions
42
common/tier4_autoware_utils/test/src/math/test_trigonometry.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2023 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "tier4_autoware_utils/math/constants.hpp" | ||
#include "tier4_autoware_utils/math/trigonometry.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <cmath> | ||
|
||
TEST(trigonometry, sin) | ||
{ | ||
float x = 4.f * tier4_autoware_utils::pi / 128.f; | ||
for (int i = 0; i < 128; i++) { | ||
EXPECT_TRUE( | ||
std::abs( | ||
std::sin(x * static_cast<float>(i)) - | ||
tier4_autoware_utils::sin(x * static_cast<float>(i))) < 10e-5); | ||
} | ||
} | ||
|
||
TEST(trigonometry, cos) | ||
{ | ||
float x = 4.f * tier4_autoware_utils::pi / 128.f; | ||
for (int i = 0; i < 128; i++) { | ||
EXPECT_TRUE( | ||
std::abs( | ||
std::cos(x * static_cast<float>(i)) - | ||
tier4_autoware_utils::cos(x * static_cast<float>(i))) < 10e-5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters