Caribou
EigenVector.h
1 #pragma once
2 
3 #include <Caribou/config.h>
4 #include <Caribou/macros.h>
5 #include <Caribou/traits.h>
6 #include <sofa/defaulttype/BaseMatrix.h>
7 
8 #include <Eigen/Dense>
9 
10 namespace SofaCaribou::Algebra {
11 
21 template <typename Derived, typename Enable = void>
22 class EigenVector : public sofa::defaulttype::BaseVector
23 {
24  static_assert(
25  std::is_base_of_v<Eigen::EigenBase<std::decay_t<Derived> >, std::decay_t<Derived> >,
26  "The class template argument 'Derived' must be derived from Eigen::EigenBase<Derived>."
27  );
28 
29  static_assert(
30  std::remove_reference_t<Derived>::IsVectorAtCompileTime,
31  "The class template argument 'Derived' must be an Eigen vector (either 1 or dynamic column"
32  " at compile time, or 1 or dynamic row at compile time."
33  );
34 
35 public:
36  using EigenType = std::remove_cv_t<std::remove_reference_t<Derived>>;
37  using Base = sofa::defaulttype::BaseVector;
38  using Index = Base::Index;
39  using Real = typename EigenType::Scalar;
40 
47  explicit EigenVector(std::remove_reference_t<Derived> & eigen_vector) : p_eigen_vector(eigen_vector) {}
48 
53  EigenVector(Eigen::Index n) : p_eigen_vector(n) {}
54 
56  [[nodiscard]]
57  Index size() const final {return static_cast<int>(p_eigen_vector.size());}
58 
60  [[nodiscard]]
61  SReal element(Index index) const final {return static_cast<SReal>(p_eigen_vector[index]);}
62 
64  void resize(Index n) final {p_eigen_vector.resize(n);p_eigen_vector.setZero();}
65 
67  void clear() final {p_eigen_vector.setZero();}
68 
70  void set(Index index, SReal value) final {p_eigen_vector[index] = static_cast<Real>(value);}
71 
73  void add(Index index, SReal value) final {p_eigen_vector[index] += static_cast<Real>(value);}
74 
76  Base::ElementType getElementType() const final {
77  return (std::is_integral_v<Real> ? Base::ELEMENT_INT : Base::ELEMENT_FLOAT);
78  }
79 
81  std::size_t getElementSize() const final {
82  return sizeof(Real);
83  }
84 
86  const EigenType & vector() const {return p_eigen_vector;}
87 
89  EigenType & vector() {return p_eigen_vector;}
90 
91 private:
93  Derived p_eigen_vector;
94 };
95 
96 } // namespace SofaCaribou::Algebra
SofaCaribou::Algebra::EigenVector
This class can be use to represent any Eigen vector within SOFA.
Definition: EigenVector.h:23
SofaCaribou::Algebra::EigenVector::getElementType
Base::ElementType getElementType() const final
Type of elements stored in this matrix.
Definition: EigenVector.h:76
SofaCaribou::Algebra::EigenVector::EigenVector
EigenVector(Eigen::Index n)
Construct a new Eigen vector of type Derived have n elements.
Definition: EigenVector.h:53
SofaCaribou::Algebra::EigenVector::set
void set(Index index, SReal value) final
Write the value of element index.
Definition: EigenVector.h:70
SofaCaribou::Algebra::EigenVector::size
Index size() const final
Number of elements in the vector.
Definition: EigenVector.h:57
SofaCaribou::Algebra::EigenVector::vector
EigenType & vector()
Get a reference to the underlying Eigen vector
Definition: EigenVector.h:89
SofaCaribou::Algebra::EigenVector::resize
void resize(Index n) final
Resize the vector.
Definition: EigenVector.h:64
SofaCaribou::Algebra::EigenVector::clear
void clear() final
Reset all values to 0.
Definition: EigenVector.h:67
SofaCaribou::Algebra::EigenVector::add
void add(Index index, SReal value) final
Add v to the existing value of element i.
Definition: EigenVector.h:73
SofaCaribou::Algebra::EigenVector::EigenVector
EigenVector(std::remove_reference_t< Derived > &eigen_vector)
Construct the class using another Eigen vector.
Definition: EigenVector.h:47
SofaCaribou::Algebra::EigenVector::vector
const EigenType & vector() const
Get a const reference to the underlying Eigen vector
Definition: EigenVector.h:86
SofaCaribou::Algebra::EigenVector::getElementSize
std::size_t getElementSize() const final
Size of one element stored in this matrix.
Definition: EigenVector.h:81
SofaCaribou::Algebra::EigenVector::element
SReal element(Index index) const final
Read the value of element i.
Definition: EigenVector.h:61