Blender  V2.93
eigen_utils.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) Blender Foundation
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #if defined(__GNUC__) && !defined(__clang__)
27 # pragma GCC diagnostic push
28 /* XXX suppress verbose warnings in eigen */
29 # pragma GCC diagnostic ignored "-Wlogical-op"
30 #endif
31 
32 #include <Eigen/Sparse>
33 #include <Eigen/src/Core/util/DisableStupidWarnings.h>
34 
35 #ifdef __GNUC__
36 # pragma GCC diagnostic pop
37 #endif
38 
39 #include "BLI_utildefines.h"
40 #include "implicit.h"
41 
42 typedef float Scalar;
43 
44 /* slightly extended Eigen vector class
45  * with conversion to/from plain C float array
46  */
47 class Vector3 : public Eigen::Vector3f {
48  public:
49  typedef float *ctype;
50 
52  {
53  }
54 
55  Vector3(const ctype &v)
56  {
57  for (int k = 0; k < 3; k++) {
58  coeffRef(k) = v[k];
59  }
60  }
61 
63  {
64  for (int k = 0; k < 3; k++) {
65  coeffRef(k) = v[k];
66  }
67  return *this;
68  }
69 
70  operator ctype()
71  {
72  return data();
73  }
74 };
75 
76 /* slightly extended Eigen matrix class
77  * with conversion to/from plain C float array
78  */
79 class Matrix3 : public Eigen::Matrix3f {
80  public:
81  typedef float (*ctype)[3];
82 
84  {
85  }
86 
87  Matrix3(const ctype &v)
88  {
89  for (int k = 0; k < 3; k++) {
90  for (int l = 0; l < 3; l++) {
91  coeffRef(l, k) = v[k][l];
92  }
93  }
94  }
95 
97  {
98  for (int k = 0; k < 3; k++) {
99  for (int l = 0; l < 3; l++) {
100  coeffRef(l, k) = v[k][l];
101  }
102  }
103  return *this;
104  }
105 
106  operator ctype()
107  {
108  return (ctype)data();
109  }
110 };
111 
112 typedef Eigen::VectorXf lVector;
113 
114 /* Extension of dense Eigen vectors,
115  * providing 3-float block access for blenlib math functions
116  */
117 class lVector3f : public Eigen::VectorXf {
118  public:
119  typedef Eigen::VectorXf base_t;
120 
122  {
123  }
124 
125  template<typename T> lVector3f &operator=(T rhs)
126  {
127  base_t::operator=(rhs);
128  return *this;
129  }
130 
131  float *v3(int vertex)
132  {
133  return &coeffRef(3 * vertex);
134  }
135 
136  const float *v3(int vertex) const
137  {
138  return &coeffRef(3 * vertex);
139  }
140 };
141 
142 typedef Eigen::Triplet<Scalar> Triplet;
143 typedef std::vector<Triplet> TripletList;
144 
145 typedef Eigen::SparseMatrix<Scalar> lMatrix;
146 
147 /* Constructor type that provides more convenient handling of Eigen triplets
148  * for efficient construction of sparse 3x3 block matrices.
149  * This should be used for building lMatrix instead of writing to such lMatrix directly (which is
150  * very inefficient). After all elements have been defined using the set() method, the actual
151  * matrix can be filled using construct().
152  */
155  {
156  }
157 
158  void reset()
159  {
160  m_trips.clear();
161  }
162 
163  void reserve(int numverts)
164  {
165  /* reserve for diagonal entries */
166  m_trips.reserve(numverts * 9);
167  }
168 
169  void add(int i, int j, const Matrix3 &m)
170  {
171  i *= 3;
172  j *= 3;
173  for (int k = 0; k < 3; k++) {
174  for (int l = 0; l < 3; l++) {
175  m_trips.push_back(Triplet(i + k, j + l, m.coeff(l, k)));
176  }
177  }
178  }
179 
180  void sub(int i, int j, const Matrix3 &m)
181  {
182  i *= 3;
183  j *= 3;
184  for (int k = 0; k < 3; k++) {
185  for (int l = 0; l < 3; l++) {
186  m_trips.push_back(Triplet(i + k, j + l, -m.coeff(l, k)));
187  }
188  }
189  }
190 
191  inline void construct(lMatrix &m)
192  {
193  m.setFromTriplets(m_trips.begin(), m_trips.end());
194  m_trips.clear();
195  }
196 
197  private:
198  TripletList m_trips;
199 };
200 
201 typedef Eigen::ConjugateGradient<lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner<Scalar>>
203 
204 using Eigen::ComputationInfo;
205 
207 {
208  for (int i = 0; i < v.rows(); i++) {
209  if (i > 0 && i % 3 == 0) {
210  printf("\n");
211  }
212 
213  printf("%f,\n", v[i]);
214  }
215 }
216 
218 {
219  for (int j = 0; j < m.rows(); j++) {
220  if (j > 0 && j % 3 == 0) {
221  printf("\n");
222  }
223 
224  for (int i = 0; i < m.cols(); i++) {
225  if (i > 0 && i % 3 == 0) {
226  printf(" ");
227  }
228 
229  implicit_print_matrix_elem(m.coeff(j, i));
230  }
231  printf("\n");
232  }
233 }
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:81
Matrix3(const ctype &v)
Definition: eigen_utils.h:87
Matrix3 & operator=(const ctype &v)
Definition: eigen_utils.h:96
Vector3 & operator=(const ctype &v)
Definition: eigen_utils.h:62
float * ctype
Definition: eigen_utils.h:49
Vector3(const ctype &v)
Definition: eigen_utils.h:55
lVector3f & operator=(T rhs)
Definition: eigen_utils.h:125
float * v3(int vertex)
Definition: eigen_utils.h:131
const float * v3(int vertex) const
Definition: eigen_utils.h:136
Eigen::VectorXf base_t
Definition: eigen_utils.h:119
Eigen::ConjugateGradient< lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner< Scalar > > ConjugateGradient
Definition: eigen_utils.h:202
std::vector< Triplet > TripletList
Definition: eigen_utils.h:143
Eigen::VectorXf lVector
Definition: eigen_utils.h:112
BLI_INLINE void print_lmatrix(const lMatrix &m)
Definition: eigen_utils.h:217
Eigen::SparseMatrix< Scalar > lMatrix
Definition: eigen_utils.h:145
BLI_INLINE void print_lvector(const lVector3f &v)
Definition: eigen_utils.h:206
float Scalar
Definition: eigen_utils.h:42
Eigen::Triplet< Scalar > Triplet
Definition: eigen_utils.h:142
BLI_INLINE void implicit_print_matrix_elem(float v)
Definition: implicit.h:62
#define T
void construct(lMatrix &m)
Definition: eigen_utils.h:191
void add(int i, int j, const Matrix3 &m)
Definition: eigen_utils.h:169
void sub(int i, int j, const Matrix3 &m)
Definition: eigen_utils.h:180
void reserve(int numverts)
Definition: eigen_utils.h:163