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