Blender  V3.3
eigen_utils.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright Blender Foundation. All rights reserved. */
3 
4 #pragma once
5 
10 #if defined(__GNUC__) && !defined(__clang__)
11 # pragma GCC diagnostic push
12 /* XXX suppress verbose warnings in eigen */
13 # pragma GCC diagnostic ignored "-Wlogical-op"
14 #endif
15 
16 #include <Eigen/Sparse>
17 #include <Eigen/src/Core/util/DisableStupidWarnings.h>
18 
19 #ifdef __GNUC__
20 # pragma GCC diagnostic pop
21 #endif
22 
23 #include "BLI_utildefines.h"
24 #include "implicit.h"
25 
26 typedef float Scalar;
27 
28 /* slightly extended Eigen vector class
29  * with conversion to/from plain C float array
30  */
31 class Vector3 : public Eigen::Vector3f {
32  public:
33  typedef float *ctype;
34 
36  {
37  }
38 
39  Vector3(const ctype &v)
40  {
41  for (int k = 0; k < 3; k++) {
42  coeffRef(k) = v[k];
43  }
44  }
45 
47  {
48  for (int k = 0; k < 3; k++) {
49  coeffRef(k) = v[k];
50  }
51  return *this;
52  }
53 
54  operator ctype()
55  {
56  return data();
57  }
58 };
59 
60 /* slightly extended Eigen matrix class
61  * with conversion to/from plain C float array
62  */
63 class Matrix3 : public Eigen::Matrix3f {
64  public:
65  typedef float (*ctype)[3];
66 
68  {
69  }
70 
71  Matrix3(const ctype &v)
72  {
73  for (int k = 0; k < 3; k++) {
74  for (int l = 0; l < 3; l++) {
75  coeffRef(l, k) = v[k][l];
76  }
77  }
78  }
79 
81  {
82  for (int k = 0; k < 3; k++) {
83  for (int l = 0; l < 3; l++) {
84  coeffRef(l, k) = v[k][l];
85  }
86  }
87  return *this;
88  }
89 
90  operator ctype()
91  {
92  return (ctype)data();
93  }
94 };
95 
96 typedef Eigen::VectorXf lVector;
97 
98 /* Extension of dense Eigen vectors,
99  * providing 3-float block access for blenlib math functions
100  */
101 class lVector3f : public Eigen::VectorXf {
102  public:
103  typedef Eigen::VectorXf base_t;
104 
106  {
107  }
108 
109  template<typename T> lVector3f &operator=(T rhs)
110  {
111  base_t::operator=(rhs);
112  return *this;
113  }
114 
115  float *v3(int vertex)
116  {
117  return &coeffRef(3 * vertex);
118  }
119 
120  const float *v3(int vertex) const
121  {
122  return &coeffRef(3 * vertex);
123  }
124 };
125 
126 typedef Eigen::Triplet<Scalar> Triplet;
127 typedef std::vector<Triplet> TripletList;
128 
129 typedef Eigen::SparseMatrix<Scalar> lMatrix;
130 
131 /* Constructor type that provides more convenient handling of Eigen triplets
132  * for efficient construction of sparse 3x3 block matrices.
133  * This should be used for building lMatrix instead of writing to such lMatrix directly (which is
134  * very inefficient). After all elements have been defined using the set() method, the actual
135  * matrix can be filled using construct().
136  */
139  {
140  }
141 
142  void reset()
143  {
144  m_trips.clear();
145  }
146 
147  void reserve(int numverts)
148  {
149  /* reserve for diagonal entries */
150  m_trips.reserve(numverts * 9);
151  }
152 
153  void add(int i, int j, const Matrix3 &m)
154  {
155  i *= 3;
156  j *= 3;
157  for (int k = 0; k < 3; k++) {
158  for (int l = 0; l < 3; l++) {
159  m_trips.push_back(Triplet(i + k, j + l, m.coeff(l, k)));
160  }
161  }
162  }
163 
164  void sub(int i, int j, const Matrix3 &m)
165  {
166  i *= 3;
167  j *= 3;
168  for (int k = 0; k < 3; k++) {
169  for (int l = 0; l < 3; l++) {
170  m_trips.push_back(Triplet(i + k, j + l, -m.coeff(l, k)));
171  }
172  }
173  }
174 
175  inline void construct(lMatrix &m)
176  {
177  m.setFromTriplets(m_trips.begin(), m_trips.end());
178  m_trips.clear();
179  }
180 
181  private:
182  TripletList m_trips;
183 };
184 
185 typedef Eigen::ConjugateGradient<lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner<Scalar>>
187 
188 using Eigen::ComputationInfo;
189 
191 {
192  for (int i = 0; i < v.rows(); i++) {
193  if (i > 0 && i % 3 == 0) {
194  printf("\n");
195  }
196 
197  printf("%f,\n", v[i]);
198  }
199 }
200 
202 {
203  for (int j = 0; j < m.rows(); j++) {
204  if (j > 0 && j % 3 == 0) {
205  printf("\n");
206  }
207 
208  for (int i = 0; i < m.cols(); i++) {
209  if (i > 0 && i % 3 == 0) {
210  printf(" ");
211  }
212 
213  implicit_print_matrix_elem(m.coeff(j, i));
214  }
215  printf("\n");
216  }
217 }
typedef float(TangentPoint)[2]
#define BLI_INLINE
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
float(* ctype)[3]
Definition: eigen_utils.h:65
Matrix3(const ctype &v)
Definition: eigen_utils.h:71
Matrix3 & operator=(const ctype &v)
Definition: eigen_utils.h:80
Vector3 & operator=(const ctype &v)
Definition: eigen_utils.h:46
float * ctype
Definition: eigen_utils.h:33
Vector3(const ctype &v)
Definition: eigen_utils.h:39
lVector3f & operator=(T rhs)
Definition: eigen_utils.h:109
float * v3(int vertex)
Definition: eigen_utils.h:115
const float * v3(int vertex) const
Definition: eigen_utils.h:120
Eigen::VectorXf base_t
Definition: eigen_utils.h:103
Eigen::ConjugateGradient< lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner< Scalar > > ConjugateGradient
Definition: eigen_utils.h:186
std::vector< Triplet > TripletList
Definition: eigen_utils.h:127
Eigen::VectorXf lVector
Definition: eigen_utils.h:96
BLI_INLINE void print_lmatrix(const lMatrix &m)
Definition: eigen_utils.h:201
Eigen::SparseMatrix< Scalar > lMatrix
Definition: eigen_utils.h:129
BLI_INLINE void print_lvector(const lVector3f &v)
Definition: eigen_utils.h:190
float Scalar
Definition: eigen_utils.h:26
Eigen::Triplet< Scalar > Triplet
Definition: eigen_utils.h:126
BLI_INLINE void implicit_print_matrix_elem(float v)
Definition: implicit.h:46
#define T
void construct(lMatrix &m)
Definition: eigen_utils.h:175
void add(int i, int j, const Matrix3 &m)
Definition: eigen_utils.h:153
void sub(int i, int j, const Matrix3 &m)
Definition: eigen_utils.h:164
void reserve(int numverts)
Definition: eigen_utils.h:147