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