Skip to content

Commit

Permalink
[MAINT] Updated SO3 to perform normalization if required
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroFornasier committed Sep 7, 2024
1 parent 278e989 commit c5306fa
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions include/groups/SO3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,33 @@ class SO3
/**
* @brief Construct an identity SO3 object
*/
SO3() : R_(MatrixType::Identity()), q_(QuaternionType::Identity()) { checkq(); }
SO3() : R_(MatrixType::Identity()), q_(QuaternionType::Identity()) {}

/**
* @brief Construct a SO3 object from a given normalized quaternion.
*
* @param q Quaternion
*/
SO3(const QuaternionType& q) : R_(q.toRotationMatrix()), q_(q) { checkq(); }
SO3(const QuaternionType& q) : R_(q.toRotationMatrix()), q_(q)
{
if (!isNormalized())
{
normalize();
}
}

/**
* @brief Construct a SO3 object from a given rotation matrix
*
* @param R Rotation matrix
*/
SO3(const MatrixType& R) : R_(R), q_(R) { checkq(); }
SO3(const MatrixType& R) : R_(R), q_(R)
{
if (!isNormalized())
{
normalize();
}
}

/**
* @brief Construct a SO3 object from two vector such that Ru = v
Expand Down Expand Up @@ -90,8 +102,6 @@ class SO3
R_ = MatrixType::Identity() + wedge(ax) + ((1.0 - c) / (s * s)) * (wedge(ax) * wedge(ax));
q_ = QuaternionType(R_);
}

checkq();
}

/**
Expand Down Expand Up @@ -390,7 +400,10 @@ class SO3
q_ = q;
R_ = q.toRotationMatrix();

checkq();
if (!isNormalized())
{
normalize();
}
}

/**
Expand All @@ -403,7 +416,10 @@ class SO3
q_ = R;
R_ = R;

checkq();
if (!isNormalized())
{
normalize();
}
}

/**
Expand All @@ -423,13 +439,18 @@ class SO3
protected:
/**
* @brief Check that q_ is a normalized quaternion
*
* @return true if q_ is normalized
*/
void checkq()
[[nodiscard]] bool isNormalized() { return std::abs(q_.norm() - 1.0) < eps_; }

/**
* @brief Normalize the quaternion and update the rotation matrix
*/
void normalize()
{
if (std::abs(q_.norm() - 1.0) > eps_)
{
throw std::invalid_argument("SO3: QuaternionType has to be normalized!");
}
q_.normalize();
R_ = q_.toRotationMatrix();
}

MatrixType R_; //!< Rotation matrix
Expand Down

0 comments on commit c5306fa

Please sign in to comment.