Skip to content

Commit

Permalink
[wpimath] Replace constexpr coeff() and coeffRef() with operator() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Nov 16, 2024
1 parent ca51197 commit aa7dd25
Show file tree
Hide file tree
Showing 74 changed files with 1,852 additions and 1,228 deletions.
3 changes: 2 additions & 1 deletion upstream_utils/eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def copy_upstream_src(wpilib_root):
def main():
name = "eigen"
url = "https://gitlab.com/libeigen/eigen.git"
tag = "d14b0a4e531760b6aeccf20b666eaec8bd0b8461"
# master on 2024-11-14
tag = "0fb2ed140d4fc0108553ecfb25f2d7fc1a9319a1"

eigen = Lib(name, url, tag, copy_upstream_src)
eigen.main()
Expand Down
4 changes: 2 additions & 2 deletions upstream_utils/eigen_patches/0001-Disable-warnings.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Subject: [PATCH 1/2] Disable warnings
1 file changed, 6 insertions(+)

diff --git a/Eigen/src/Core/util/DisableStupidWarnings.h b/Eigen/src/Core/util/DisableStupidWarnings.h
index 32a427d852355a51dc4263d81498554ff4c3cbba..1182198231ab784346f8d80838363e4a0abba2ba 100644
index ab0c542d0e24c6ecb77abfc535c8232774cba6d5..7ecd7bf8cc927d07a28c9da4ebbe1ea4d4d2b97b 100644
--- a/Eigen/src/Core/util/DisableStupidWarnings.h
+++ b/Eigen/src/Core/util/DisableStupidWarnings.h
@@ -81,6 +81,12 @@
Expand All @@ -23,4 +23,4 @@ index 32a427d852355a51dc4263d81498554ff4c3cbba..1182198231ab784346f8d80838363e4a
+#endif
#endif

#if defined __NVCC__
#if defined __NVCC__ && defined __CUDACC__
25 changes: 12 additions & 13 deletions wpimath/src/main/native/include/frc/StateSpaceUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ constexpr Matrixd<sizeof...(Ts), sizeof...(Ts)> MakeCostMatrix(
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
if (row != col) {
result.coeffRef(row, col) = 0.0;
result(row, col) = 0.0;
}
}
}

wpi::for_each(
[&](int i, double tolerance) {
if (tolerance == std::numeric_limits<double>::infinity()) {
result.coeffRef(i, i) = 0.0;
result(i, i) = 0.0;
} else {
result.coeffRef(i, i) = 1.0 / (tolerance * tolerance);
result(i, i) = 1.0 / (tolerance * tolerance);
}
},
tolerances...);
Expand All @@ -78,14 +78,13 @@ constexpr Matrixd<sizeof...(Ts), sizeof...(Ts)> MakeCovMatrix(Ts... stdDevs) {
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
if (row != col) {
result.coeffRef(row, col) = 0.0;
result(row, col) = 0.0;
}
}
}

wpi::for_each(
[&](int i, double stdDev) { result.coeffRef(i, i) = stdDev * stdDev; },
stdDevs...);
wpi::for_each([&](int i, double stdDev) { result(i, i) = stdDev * stdDev; },
stdDevs...);

return result;
}
Expand All @@ -111,12 +110,12 @@ constexpr Matrixd<N, N> MakeCostMatrix(const std::array<double, N>& costs) {
for (int col = 0; col < result.cols(); ++col) {
if (row == col) {
if (costs[row] == std::numeric_limits<double>::infinity()) {
result.coeffRef(row, col) = 0.0;
result(row, col) = 0.0;
} else {
result.coeffRef(row, col) = 1.0 / (costs[row] * costs[row]);
result(row, col) = 1.0 / (costs[row] * costs[row]);
}
} else {
result.coeffRef(row, col) = 0.0;
result(row, col) = 0.0;
}
}
}
Expand All @@ -143,9 +142,9 @@ constexpr Matrixd<N, N> MakeCovMatrix(const std::array<double, N>& stdDevs) {
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.cols(); ++col) {
if (row == col) {
result.coeffRef(row, col) = stdDevs[row] * stdDevs[row];
result(row, col) = stdDevs[row] * stdDevs[row];
} else {
result.coeffRef(row, col) = 0.0;
result(row, col) = 0.0;
}
}
}
Expand Down Expand Up @@ -334,7 +333,7 @@ constexpr Vectord<Inputs> ClampInputMaxMagnitude(const Vectord<Inputs>& u,
const Vectord<Inputs>& umax) {
Vectord<Inputs> result;
for (int i = 0; i < Inputs; ++i) {
result.coeffRef(i) = std::clamp(u.coeff(i), umin.coeff(i), umax.coeff(i));
result(i) = std::clamp(u(i), umin(i), umax(i));
}
return result;
}
Expand Down
10 changes: 4 additions & 6 deletions wpimath/src/main/native/include/frc/ct_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ct_matrix {
* @param col Column index.
*/
constexpr const Scalar& operator()(int row, int col) const {
return m_storage.coeff(row, col);
return m_storage(row, col);
}

/**
Expand All @@ -71,9 +71,7 @@ class ct_matrix {
* @param row Row index.
* @param col Column index.
*/
constexpr Scalar& operator()(int row, int col) {
return m_storage.coeffRef(row, col);
}
constexpr Scalar& operator()(int row, int col) { return m_storage(row, col); }

/**
* Returns reference to matrix element.
Expand All @@ -83,7 +81,7 @@ class ct_matrix {
constexpr const Scalar& operator()(int index) const
requires(Rows == 1 || Cols == 1)
{
return m_storage.coeff(index);
return m_storage(index);
}

/**
Expand All @@ -94,7 +92,7 @@ class ct_matrix {
constexpr Scalar& operator()(int index)
requires(Rows == 1 || Cols == 1)
{
return m_storage.coeffRef(index);
return m_storage(index);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions wpimath/src/main/native/include/frc/geometry/CoordinateSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ class WPILIB_DLLEXPORT CoordinateSystem {
// the NWU coordinate system. Each column vector in the change of basis
// matrix is one of the old basis vectors mapped to its representation in
// the new basis.
Eigen::Matrix3d R{{positiveX.m_axis.coeff(0), positiveY.m_axis.coeff(0),
positiveZ.m_axis.coeff(0)},
{positiveX.m_axis.coeff(1), positiveY.m_axis.coeff(1),
positiveZ.m_axis.coeff(1)},
{positiveX.m_axis.coeff(2), positiveY.m_axis.coeff(2),
positiveZ.m_axis.coeff(2)}};
Eigen::Matrix3d R{
{positiveX.m_axis(0), positiveY.m_axis(0), positiveZ.m_axis(0)},
{positiveX.m_axis(1), positiveY.m_axis(1), positiveZ.m_axis(1)},
{positiveX.m_axis(2), positiveY.m_axis(2), positiveZ.m_axis(2)}};

// The change of basis matrix should be a pure rotation. The Rotation3d
// constructor will verify this by checking for special orthogonality.
Expand Down
6 changes: 3 additions & 3 deletions wpimath/src/main/native/include/frc/geometry/Pose3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ constexpr Eigen::Matrix3d RotationVectorToMatrix(
// [ 0 -c b]
// Omega = [ c 0 -a]
// [-b a 0]
return Eigen::Matrix3d{{0.0, -rotation.coeff(2), rotation.coeff(1)},
{rotation.coeff(2), 0.0, -rotation.coeff(0)},
{-rotation.coeff(1), rotation.coeff(0), 0.0}};
return Eigen::Matrix3d{{0.0, -rotation(2), rotation(1)},
{rotation(2), 0.0, -rotation(0)},
{-rotation(1), rotation(0), 0.0}};
}

} // namespace detail
Expand Down
7 changes: 3 additions & 4 deletions wpimath/src/main/native/include/frc/geometry/Quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class WPILIB_DLLEXPORT Quaternion {
// 𝑞 = std::cos(θ/2) + std::sin(θ/2) * v̂
// 𝑞 = std::cos(θ/2) + std::sin(θ/2) / θ * 𝑣⃗

double theta = gcem::hypot(rvec.coeff(0), rvec.coeff(1), rvec.coeff(2));
double theta = gcem::hypot(rvec(0), rvec(1), rvec(2));
double cos = gcem::cos(theta / 2);

double axial_scalar;
Expand All @@ -312,9 +312,8 @@ class WPILIB_DLLEXPORT Quaternion {
axial_scalar = gcem::sin(theta / 2) / theta;
}

return Quaternion{cos, axial_scalar * rvec.coeff(0),
axial_scalar * rvec.coeff(1),
axial_scalar * rvec.coeff(2)};
return Quaternion{cos, axial_scalar * rvec(0), axial_scalar * rvec(1),
axial_scalar * rvec(2)};
}

private:
Expand Down
15 changes: 7 additions & 8 deletions wpimath/src/main/native/include/frc/geometry/Rotation3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ class WPILIB_DLLEXPORT Rotation3d {
}

// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Definition
Eigen::Vector3d v{{axis.coeff(0) / norm * units::math::sin(angle / 2.0),
axis.coeff(1) / norm * units::math::sin(angle / 2.0),
axis.coeff(2) / norm * units::math::sin(angle / 2.0)}};
m_q = Quaternion{units::math::cos(angle / 2.0), v.coeff(0), v.coeff(1),
v.coeff(2)};
Eigen::Vector3d v{{axis(0) / norm * units::math::sin(angle / 2.0),
axis(1) / norm * units::math::sin(angle / 2.0),
axis(2) / norm * units::math::sin(angle / 2.0)}};
m_q = Quaternion{units::math::cos(angle / 2.0), v(0), v(1), v(2)};
}

/**
Expand Down Expand Up @@ -191,9 +190,9 @@ class WPILIB_DLLEXPORT Rotation3d {
// rotation is required. Any other vector can be used to generate an
// orthogonal one.

double x = gcem::abs(initial.coeff(0));
double y = gcem::abs(initial.coeff(1));
double z = gcem::abs(initial.coeff(2));
double x = gcem::abs(initial(0));
double y = gcem::abs(initial(1));
double z = gcem::abs(initial(2));

// Find vector that is most orthogonal to initial vector
Eigen::Vector3d other;
Expand Down
3 changes: 1 addition & 2 deletions wpimath/src/main/native/include/frc/geometry/Translation2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class WPILIB_DLLEXPORT Translation2d {
* @param vector The translation vector to represent.
*/
constexpr explicit Translation2d(const Eigen::Vector2d& vector)
: m_x{units::meter_t{vector.coeff(0)}},
m_y{units::meter_t{vector.coeff(1)}} {}
: m_x{units::meter_t{vector(0)}}, m_y{units::meter_t{vector(1)}} {}

/**
* Calculates the distance between two translations in 2D space.
Expand Down
2 changes: 1 addition & 1 deletion wpimath/src/main/native/include/frc/system/LinearSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LinearSystem {
if (std::is_constant_evaluated()) {
for (int row = 0; row < mat.rows(); ++row) {
for (int col = 0; col < mat.cols(); ++col) {
if (!gcem::internal::is_finite(mat.coeff(row, col))) {
if (!gcem::internal::is_finite(mat(row, col))) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ BasedOnStyle: Google
ColumnLimit: 120
StatementMacros:
- EIGEN_STATIC_ASSERT
- EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
- EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
SortIncludes: false
AttributeMacros:
- EIGEN_STRONG_INLINE
Expand Down
8 changes: 7 additions & 1 deletion wpimath/src/main/native/thirdparty/eigen/include/Eigen/Core
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
// for std::is_nothrow_move_assignable
#include <type_traits>

// for std::this_thread::yield().
#if !defined(EIGEN_USE_BLAS) && (defined(EIGEN_HAS_OPENMP) || defined(EIGEN_GEMM_THREADPOOL))
#include <thread>
#endif

// for outputting debug info
#ifdef EIGEN_DEBUG_ASSIGN
#include <iostream>
Expand All @@ -117,8 +122,8 @@
#include <CL/sycl.hpp>
#include <map>
#include <memory>
#include <utility>
#include <thread>
#include <utility>
#ifndef EIGEN_SYCL_LOCAL_THREAD_DIM0
#define EIGEN_SYCL_LOCAL_THREAD_DIM0 16
#endif
Expand Down Expand Up @@ -319,6 +324,7 @@ using std::ptrdiff_t;
#include "src/Core/CwiseNullaryOp.h"
#include "src/Core/CwiseUnaryView.h"
#include "src/Core/SelfCwiseBinaryOp.h"
#include "src/Core/InnerProduct.h"
#include "src/Core/Dot.h"
#include "src/Core/StableNorm.h"
#include "src/Core/Stride.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,12 @@ class Array : public PlainObjectBase<Array<Scalar_, Rows_, Cols_, Options_, MaxR
*
* \sa resize(Index,Index)
*/
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array() : Base() { EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }

#ifndef EIGEN_PARSED_BY_DOXYGEN
// FIXME is it still needed ??
/** \internal */
EIGEN_DEVICE_FUNC Array(internal::constructor_without_unaligned_array_assert)
: Base(internal::constructor_without_unaligned_array_assert()){EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED}
#ifdef EIGEN_INITIALIZE_COEFFS
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array() : Base() { EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
#else
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array() = default;
#endif

EIGEN_DEVICE_FUNC Array(Array && other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
: Base(std::move(other)) {
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array(Array&&) = default;
EIGEN_DEVICE_FUNC Array& operator=(Array&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value) {
Base::operator=(std::move(other));
return *this;
Expand Down Expand Up @@ -232,7 +226,7 @@ class Array : public PlainObjectBase<Array<Scalar_, Rows_, Cols_, Options_, MaxR
}

/** Copy constructor */
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Array(const Array& other) : Base(other) {}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Array(const Array&) = default;

private:
struct PrivateType {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class ArrayWrapper : public ArrayBase<ArrayWrapper<ExpressionType> > {
return m_expression.innerStride();
}

EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_expression.data(); }
EIGEN_DEVICE_FUNC constexpr ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
EIGEN_DEVICE_FUNC constexpr const Scalar* data() const { return m_expression.data(); }

EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index rowId, Index colId) const {
return m_expression.coeffRef(rowId, colId);
Expand Down Expand Up @@ -144,8 +144,8 @@ class MatrixWrapper : public MatrixBase<MatrixWrapper<ExpressionType> > {
return m_expression.innerStride();
}

EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_expression.data(); }
EIGEN_DEVICE_FUNC constexpr ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
EIGEN_DEVICE_FUNC constexpr const Scalar* data() const { return m_expression.data(); }

EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index rowId, Index colId) const {
return m_expression.derived().coeffRef(rowId, colId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class BlockImpl_dense : public internal::dense_xpr_base<Block<XprType, BlockRows

#ifdef EIGEN_PARSED_BY_DOXYGEN
/** \sa MapBase::data() */
EIGEN_DEVICE_FUNC inline const Scalar* data() const;
EIGEN_DEVICE_FUNC constexpr const Scalar* data() const;
EIGEN_DEVICE_FUNC inline Index innerStride() const;
EIGEN_DEVICE_FUNC inline Index outerStride() const;
#endif
Expand Down
Loading

0 comments on commit aa7dd25

Please sign in to comment.