PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Eigen.h
1#ifndef PBAT_MATH_LINALG_MINI_EIGEN_H
2#define PBAT_MATH_LINALG_MINI_EIGEN_H
3
4#include "Api.h"
5#include "Concepts.h"
6
7#include <Eigen/Core>
8#include <type_traits>
9#include <utility>
10
11namespace pbat {
12namespace math {
13namespace linalg {
14namespace mini {
15
16template <class TMatrix>
17concept CEigenConvertible = CMatrix<TMatrix> && requires(TMatrix M)
18{
19 {
20 M.Data()
21 } -> std::convertible_to<typename TMatrix::ScalarType*>;
22};
23
24template <class TMatrix>
25Eigen::Map<Eigen::Matrix<
26 typename std::remove_cvref_t<TMatrix>::ScalarType,
27 std::remove_cvref_t<TMatrix>::kRows,
28 std::remove_cvref_t<TMatrix>::kCols,
29 (std::remove_cvref_t<TMatrix>::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor>>
30ToEigen(TMatrix& A)
31{
32 using MatrixType = std::remove_cvref_t<TMatrix>;
33 static_assert(CEigenConvertible<MatrixType>, "A must satisfy CEigenConvertible");
34 using EigenMatrixType = Eigen::Matrix<
35 typename MatrixType::ScalarType,
36 MatrixType::kRows,
37 MatrixType::kCols,
38 (MatrixType::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor>;
39 return Eigen::Map<EigenMatrixType>(A.Data());
40}
41
42template <class TMatrix>
43Eigen::Map<Eigen::Matrix<
44 typename std::remove_cvref_t<TMatrix>::ScalarType,
45 std::remove_cvref_t<TMatrix>::kRows,
46 std::remove_cvref_t<TMatrix>::kCols,
47 (std::remove_cvref_t<TMatrix>::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor> const>
48ToEigen(TMatrix const& A)
49{
50 using MatrixType = std::remove_cvref_t<TMatrix>;
51 static_assert(CEigenConvertible<MatrixType>, "A must satisfy CEigenConvertible");
52 using EigenMatrixType = Eigen::Matrix<
53 typename MatrixType::ScalarType,
54 MatrixType::kRows,
55 MatrixType::kCols,
56 (MatrixType::bRowMajor) ? Eigen::RowMajor : Eigen::ColMajor>;
57 return Eigen::Map<EigenMatrixType const>(A.Data());
58}
59
60template <class TDerived>
61class ConstEigenMatrixWrapper
62{
63 public:
64 using ScalarType = typename TDerived::Scalar;
65 using SelfType = ConstEigenMatrixWrapper<TDerived>;
66
67 static int constexpr kRows = TDerived::RowsAtCompileTime;
68 static int constexpr kCols = TDerived::ColsAtCompileTime;
69 static bool constexpr bRowMajor = TDerived::IsRowMajor;
70
71 PBAT_HOST_DEVICE ConstEigenMatrixWrapper(TDerived const& A) : mA(A)
72 {
73 static_assert(TDerived::RowsAtCompileTime > 0, "A must have compile-time row dimensions");
74 static_assert(
75 TDerived::ColsAtCompileTime > 0,
76 "A must have compile-time column dimensions");
77 }
78
79 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const { return mA(i, j); }
80
81 // Vector(ized) access
82 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return mA.reshaped()(i); }
83 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
84
85 PBAT_MINI_READ_API(SelfType)
86
87 private:
88 TDerived const& mA;
89};
90
91template <class TDerived>
92class EigenMatrixWrapper
93{
94 public:
95 using ScalarType = typename TDerived::Scalar;
96 using SelfType = EigenMatrixWrapper<TDerived>;
97
98 static int constexpr kRows = TDerived::RowsAtCompileTime;
99 static int constexpr kCols = TDerived::ColsAtCompileTime;
100 static bool constexpr bRowMajor = TDerived::IsRowMajor;
101
102 PBAT_HOST_DEVICE EigenMatrixWrapper(TDerived& A) : mA(A)
103 {
104 static_assert(TDerived::RowsAtCompileTime > 0, "A must have compile-time row dimensions");
105 static_assert(
106 TDerived::ColsAtCompileTime > 0,
107 "A must have compile-time column dimensions");
108 }
109
110 PBAT_HOST_DEVICE void SetConstant(ScalarType k) { Assign(*this, k); }
111
112 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const { return mA(i, j); }
113 PBAT_HOST_DEVICE ScalarType& operator()(auto i, auto j) { return mA(i, j); }
114
115 // Vector(ized) access
116 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return mA.reshaped()(i); }
117 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
118 PBAT_HOST_DEVICE ScalarType& operator()(auto i) { return mA.reshaped()(i); }
119 PBAT_HOST_DEVICE ScalarType& operator[](auto i) { return (*this)(i); }
120
121 PBAT_MINI_READ_WRITE_API(SelfType)
122
123 private:
124 TDerived& mA;
125};
126
127template <class TDerived>
128auto FromEigen(TDerived& A)
129{
131}
132
133template <class TDerived>
134auto FromEigen(TDerived const& A)
135{
136 return ConstEigenMatrixWrapper<TDerived>(A);
137}
138
139} // namespace mini
140} // namespace linalg
141} // namespace math
142} // namespace pbat
143
144#endif // PBAT_MATH_LINALG_MINI_EIGEN_H
Definition Concepts.h:14
Mini linear algebra related functionality.
Definition Assign.h:12
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
Math related functionality.
Definition Concepts.h:19
The main namespace of the library.
Definition Aliases.h:15