Blender V5.3
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
26namespace blender {
27
28using Scalar = float;
29
30/* slightly extended Eigen vector class
31 * with conversion to/from plain C float array
32 */
33class Vector3 : public Eigen::Vector3f {
34 public:
35 using ctype = float *;
36
37 Vector3() = default;
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 */
63class Matrix3 : public Eigen::Matrix3f {
64 public:
65 using ctype = float (*)[3];
66
67 Matrix3() = default;
68
69 Matrix3(const ctype &v)
70 {
71 for (int k = 0; k < 3; k++) {
72 for (int l = 0; l < 3; l++) {
73 coeffRef(l, k) = v[k][l];
74 }
75 }
76 }
77
79 {
80 for (int k = 0; k < 3; k++) {
81 for (int l = 0; l < 3; l++) {
82 coeffRef(l, k) = v[k][l];
83 }
84 }
85 return *this;
86 }
87
88 operator ctype()
89 {
90 return reinterpret_cast<ctype>(data());
91 }
92};
93
94using lVector = Eigen::VectorXf;
95
96/* Extension of dense Eigen vectors,
97 * providing 3-float block access for `blenlib` math functions
98 */
99class lVector3f : public Eigen::VectorXf {
100 public:
101 using base_t = Eigen::VectorXf;
102
103 lVector3f() = default;
104
105 template<typename T> lVector3f &operator=(T rhs)
106 {
107 base_t::operator=(rhs);
108 return *this;
109 }
110
111 float *v3(int vertex)
112 {
113 return &coeffRef(3 * vertex);
114 }
115
116 const float *v3(int vertex) const
117 {
118 return &coeffRef(3 * vertex);
119 }
120};
121
122using Triplet = Eigen::Triplet<Scalar>;
123using TripletList = std::vector<Triplet>;
124
125using lMatrix = Eigen::SparseMatrix<Scalar>;
126
127/* Constructor type that provides more convenient handling of Eigen triplets
128 * for efficient construction of sparse 3x3 block matrices.
129 * This should be used for building lMatrix instead of writing to such lMatrix directly (which is
130 * very inefficient). After all elements have been defined using the set() method, the actual
131 * matrix can be filled using construct().
132 */
134 lMatrix3fCtor() = default;
135
136 void reset()
137 {
138 m_trips.clear();
139 }
140
141 void reserve(int numverts)
142 {
143 /* reserve for diagonal entries */
144 m_trips.reserve(numverts * 9);
145 }
146
147 void add(int i, int j, const Matrix3 &m)
148 {
149 i *= 3;
150 j *= 3;
151 for (int k = 0; k < 3; k++) {
152 for (int l = 0; l < 3; l++) {
153 m_trips.emplace_back(i + k, j + l, m.coeff(l, k));
154 }
155 }
156 }
157
158 void sub(int i, int j, const Matrix3 &m)
159 {
160 i *= 3;
161 j *= 3;
162 for (int k = 0; k < 3; k++) {
163 for (int l = 0; l < 3; l++) {
164 m_trips.emplace_back(i + k, j + l, -m.coeff(l, k));
165 }
166 }
167 }
168
170 {
171 m.setFromTriplets(m_trips.begin(), m_trips.end());
172 m_trips.clear();
173 }
174
175 private:
176 TripletList m_trips;
177};
178
180 Eigen::ConjugateGradient<lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner<Scalar>>;
181
182using Eigen::ComputationInfo;
183
185{
186 for (int i = 0; i < v.rows(); i++) {
187 if (i > 0 && i % 3 == 0) {
188 printf("\n");
189 }
190
191 printf("%f,\n", v[i]);
192 }
193}
194
196{
197 for (int j = 0; j < m.rows(); j++) {
198 if (j > 0 && j % 3 == 0) {
199 printf("\n");
200 }
201
202 for (int i = 0; i < m.cols(); i++) {
203 if (i > 0 && i % 3 == 0) {
204 printf(" ");
205 }
206
207 implicit_print_matrix_elem(m.coeff(j, i));
208 }
209 printf("\n");
210 }
211}
212
213} // namespace blender
#define BLI_INLINE
float(*)[3] ctype
Definition eigen_utils.h:65
Matrix3 & operator=(const ctype &v)
Definition eigen_utils.h:78
Matrix3(const ctype &v)
Definition eigen_utils.h:69
Matrix3()=default
Vector3 & operator=(const ctype &v)
Definition eigen_utils.h:46
Vector3(const ctype &v)
Definition eigen_utils.h:39
Vector3()=default
Eigen::VectorXf base_t
float * v3(int vertex)
lVector3f & operator=(T rhs)
const float * v3(int vertex) const
#define vertex
#define T
BMesh Iterators.
BLI_INLINE void print_lvector(const lVector3f &v)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
BLI_INLINE void implicit_print_matrix_elem(float v)
Definition implicit.h:43
Eigen::VectorXf lVector
Definition eigen_utils.h:94
Eigen::SparseMatrix< Scalar > lMatrix
ATTR_WARN_UNUSED_RESULT const BMVert * v
std::vector< Triplet > TripletList
BMesh const char void * data
Eigen::ConjugateGradient< lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner< Scalar > > ConjugateGradient
Eigen::Triplet< Scalar > Triplet
BLI_INLINE void print_lmatrix(const lMatrix &m)
float Scalar
Definition eigen_utils.h:28
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void reserve(int numverts)
void construct(lMatrix &m)
void add(int i, int j, const Matrix3 &m)
void sub(int i, int j, const Matrix3 &m)