Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
Make Rotation2 normalize inputs if required (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Jun 25, 2024
1 parent f95414a commit a565b82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 11 additions & 3 deletions include/trajopt/geometry/Rotation2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#pragma once

#include <cassert>
#include <cmath>
#include <concepts>
#include <numbers>
Expand Down Expand Up @@ -36,13 +35,22 @@ class Rotation2 {

/**
* Constructs a rotation with the given x and y (cosine and sine) components.
* x and y must be normalized.
* The x and y don't have to be normalized.
*
* @param x The x component or cosine of the rotation.
* @param y The y component or sine of the rotation.
*/
constexpr Rotation2(T x, T y) : m_cos{std::move(x)}, m_sin{std::move(y)} {
assert(abs(x * x + y * y - 1.0) < 1e-9); // NOLINT
auto magnitude = hypot(m_cos, m_sin); // NOLINT
if (magnitude > 1e-6) {
if (abs(m_cos * m_cos + m_sin * m_sin - 1.0) > 1e-9) { // NOLINT
m_cos /= magnitude;
m_sin /= magnitude;
}
} else {
m_cos = 1.0;
m_sin = 0.0;
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/src/geometry/Translation2dTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ TEST_CASE("Translation2d - Angle", "[Translation2d]") {
const trajopt::Translation2d two{2.0, 5.0};

CHECK(one.Angle().Radians() == std::atan2(3.0, 1.0));
CHECK(two.Angle().Radians() == std::atan2(5.0, 2.0));
CHECK(two.Angle().Radians() ==
Catch::Approx(std::atan2(5.0, 2.0)).margin(1e-9));
}

TEST_CASE("Translation2d - Dot", "[Translation2d]") {
Expand Down

0 comments on commit a565b82

Please sign in to comment.