Skip to content

Commit

Permalink
[wpimath] Use ct_matrix instead of <Eigen/LU> for determinant in headers
Browse files Browse the repository at this point in the history
This caught a bug in ct_matrix's 3x3 determinant.
  • Loading branch information
calcmogul committed Dec 28, 2024
1 parent 46d4015 commit 5f2c4a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions wpimath/src/main/native/include/frc/ct_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class ct_matrix {
requires(Rows == 3 && Cols == 3)
{
// |a b c|
// |d e f| = aei + bfg + cgh - ceg - bdi - afh
// |d e f| = aei + bfg + cdh - ceg - bdi - afh
// |g h i|
Scalar a = (*this)(0, 0);
Scalar b = (*this)(0, 1);
Expand All @@ -345,7 +345,7 @@ class ct_matrix {
Scalar g = (*this)(2, 0);
Scalar h = (*this)(2, 1);
Scalar i = (*this)(2, 2);
return a * e * i + b * f * g + c * g * h - c * e * g - b * d * i -
return a * e * i + b * f * g + c * d * h - c * e * g - b * d * i -
a * f * h;
}

Expand Down
7 changes: 5 additions & 2 deletions wpimath/src/main/native/include/frc/geometry/Rotation2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <utility>

#include <Eigen/Core>
#include <Eigen/LU>
#include <gcem.hpp>
#include <wpi/StackTrace.h>
#include <wpi/SymbolExports.h>
Expand Down Expand Up @@ -84,7 +83,11 @@ class WPILIB_DLLEXPORT Rotation2d {
if ((R * R.transpose() - Matrix2d::Identity()).norm() > 1e-9) {
throw std::domain_error("Rotation matrix isn't orthogonal");
}
if (gcem::abs(R.determinant() - 1.0) > 1e-9) {
// HACK: Uses ct_matrix instead of <Eigen/LU> for determinant because
// including <Eigen/LU> doubles compilation times on MSVC, even if
// this constructor is unused. MSVC's frontend inefficiently parses
// large headers; GCC and Clang are largely unaffected.
if (gcem::abs(ct_matrix{R}.determinant() - 1.0) > 1e-9) {
throw std::domain_error(
"Rotation matrix is orthogonal but not special orthogonal");
}
Expand Down
7 changes: 5 additions & 2 deletions wpimath/src/main/native/include/frc/geometry/Rotation3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <type_traits>

#include <Eigen/Core>
#include <Eigen/LU>
#include <fmt/format.h>
#include <gcem.hpp>
#include <wpi/SymbolExports.h>
Expand Down Expand Up @@ -114,7 +113,11 @@ class WPILIB_DLLEXPORT Rotation3d {
if ((R * R.transpose() - Matrix3d::Identity()).norm() > 1e-9) {
throw std::domain_error("Rotation matrix isn't orthogonal");
}
if (gcem::abs(R.determinant() - 1.0) > 1e-9) {
// HACK: Uses ct_matrix instead of <Eigen/LU> for determinant because
// including <Eigen/LU> doubles compilation times on MSVC, even if
// this constructor is unused. MSVC's frontend inefficiently parses
// large headers; GCC and Clang are largely unaffected.
if (gcem::abs(ct_matrix{R}.determinant() - 1.0) > 1e-9) {
throw std::domain_error(
"Rotation matrix is orthogonal but not special orthogonal");
}
Expand Down

0 comments on commit 5f2c4a4

Please sign in to comment.