3 #include <Caribou/config.h>
4 #include <Caribou/macros.h>
5 #include <Caribou/traits.h>
6 #include <sofa/defaulttype/BaseMatrix.h>
10 namespace SofaCaribou::Algebra {
21 template <
typename Derived,
typename Enable =
void>
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>."
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."
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;
47 explicit EigenVector(std::remove_reference_t<Derived> & eigen_vector) : p_eigen_vector(eigen_vector) {}
57 Index
size() const final {
return static_cast<int>(p_eigen_vector.size());}
61 SReal
element(Index index)
const final {
return static_cast<SReal
>(p_eigen_vector[index]);}
64 void resize(Index n)
final {p_eigen_vector.resize(n);p_eigen_vector.setZero();}
67 void clear() final {p_eigen_vector.setZero();}
70 void set(Index index, SReal value)
final {p_eigen_vector[index] =
static_cast<Real
>(value);}
73 void add(Index index, SReal value)
final {p_eigen_vector[index] +=
static_cast<Real
>(value);}
77 return (std::is_integral_v<Real> ? Base::ELEMENT_INT : Base::ELEMENT_FLOAT);
86 const EigenType &
vector()
const {
return p_eigen_vector;}
89 EigenType &
vector() {
return p_eigen_vector;}
93 Derived p_eigen_vector;
This class can be use to represent any Eigen vector within SOFA.
Definition: EigenVector.h:23
Base::ElementType getElementType() const final
Type of elements stored in this matrix.
Definition: EigenVector.h:76
EigenVector(Eigen::Index n)
Construct a new Eigen vector of type Derived have n elements.
Definition: EigenVector.h:53
void set(Index index, SReal value) final
Write the value of element index.
Definition: EigenVector.h:70
Index size() const final
Number of elements in the vector.
Definition: EigenVector.h:57
EigenType & vector()
Get a reference to the underlying Eigen vector
Definition: EigenVector.h:89
void resize(Index n) final
Resize the vector.
Definition: EigenVector.h:64
void clear() final
Reset all values to 0.
Definition: EigenVector.h:67
void add(Index index, SReal value) final
Add v to the existing value of element i.
Definition: EigenVector.h:73
EigenVector(std::remove_reference_t< Derived > &eigen_vector)
Construct the class using another Eigen vector.
Definition: EigenVector.h:47
const EigenType & vector() const
Get a const reference to the underlying Eigen vector
Definition: EigenVector.h:86
std::size_t getElementSize() const final
Size of one element stored in this matrix.
Definition: EigenVector.h:81
SReal element(Index index) const final
Read the value of element i.
Definition: EigenVector.h:61