Blender  V3.3
BLI_float3x3.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
5 #include <cmath>
6 #include <cstdint>
7 
8 #include "BLI_assert.h"
9 #include "BLI_math_base.h"
10 #include "BLI_math_matrix.h"
11 #include "BLI_math_vec_types.hh"
12 #include "BLI_math_vector.h"
13 
14 namespace blender {
15 
16 struct float3x3 {
17  /* A 3x3 matrix in column major order. */
18  float values[3][3];
19 
20  float3x3() = default;
21 
22  float3x3(const float *matrix)
23  {
24  memcpy(values, matrix, sizeof(float) * 3 * 3);
25  }
26 
27  float3x3(const float matrix[3][3]) : float3x3(static_cast<const float *>(matrix[0]))
28  {
29  }
30 
31  static float3x3 zero()
32  {
34  zero_m3(result.values);
35  return result;
36  }
37 
38  static float3x3 identity()
39  {
41  unit_m3(result.values);
42  return result;
43  }
44 
45  static float3x3 from_translation(const float2 translation)
46  {
48  result.values[2][0] = translation.x;
49  result.values[2][1] = translation.y;
50  return result;
51  }
52 
53  static float3x3 from_rotation(float rotation)
54  {
55  float3x3 result = zero();
56  const float cosine = std::cos(rotation);
57  const float sine = std::sin(rotation);
58  result.values[0][0] = cosine;
59  result.values[0][1] = sine;
60  result.values[1][0] = -sine;
61  result.values[1][1] = cosine;
62  result.values[2][2] = 1.0f;
63  return result;
64  }
65 
67  float rotation,
68  const float2 scale)
69  {
71  const float cosine = std::cos(rotation);
72  const float sine = std::sin(rotation);
73  result.values[0][0] = scale.x * cosine;
74  result.values[0][1] = scale.x * sine;
75  result.values[0][2] = 0.0f;
76  result.values[1][0] = scale.y * -sine;
77  result.values[1][1] = scale.y * cosine;
78  result.values[1][2] = 0.0f;
79  result.values[2][0] = translation.x;
80  result.values[2][1] = translation.y;
81  result.values[2][2] = 1.0f;
82  return result;
83  }
84 
85  static float3x3 from_normalized_axes(const float2 translation,
86  const float2 horizontal,
87  const float2 vertical)
88  {
89  BLI_ASSERT_UNIT_V2(horizontal);
90  BLI_ASSERT_UNIT_V2(vertical);
91 
93  result.values[0][0] = horizontal.x;
94  result.values[0][1] = horizontal.y;
95  result.values[0][2] = 0.0f;
96  result.values[1][0] = vertical.x;
97  result.values[1][1] = vertical.y;
98  result.values[1][2] = 0.0f;
99  result.values[2][0] = translation.x;
100  result.values[2][1] = translation.y;
101  result.values[2][2] = 1.0f;
102  return result;
103  }
104 
105  /* Construct a transformation that is pivoted around the given origin point. So for instance,
106  * from_origin_transformation(from_rotation(M_PI_2), float2(0.0f, 2.0f))
107  * will construct a transformation representing a 90 degree rotation around the point (0, 2). */
108  static float3x3 from_origin_transformation(const float3x3 &transformation, const float2 origin)
109  {
110  return from_translation(origin) * transformation * from_translation(-origin);
111  }
112 
113  operator float *()
114  {
115  return &values[0][0];
116  }
117 
118  operator const float *() const
119  {
120  return &values[0][0];
121  }
122 
123  float *operator[](const int64_t index)
124  {
125  BLI_assert(index >= 0);
126  BLI_assert(index < 3);
127  return &values[index][0];
128  }
129 
130  const float *operator[](const int64_t index) const
131  {
132  BLI_assert(index >= 0);
133  BLI_assert(index < 3);
134  return &values[index][0];
135  }
136 
137  using c_style_float3x3 = float[3][3];
139  {
140  return values;
141  }
142 
143  const c_style_float3x3 &ptr() const
144  {
145  return values;
146  }
147 
148  friend float3x3 operator*(const float3x3 &a, const float3x3 &b)
149  {
151  mul_m3_m3m3(result.values, a.values, b.values);
152  return result;
153  }
154 
155  friend float3 operator*(const float3x3 &a, const float3 &b)
156  {
157  float3 result;
158  mul_v3_m3v3(result, a.values, b);
159  return result;
160  }
161 
162  void operator*=(const float3x3 &other)
163  {
164  mul_m3_m3_post(values, other.values);
165  }
166 
167  friend float2 operator*(const float3x3 &transformation, const float2 &vector)
168  {
169  float2 result;
170  mul_v2_m3v2(result, transformation.values, vector);
171  return result;
172  }
173 
174  friend float2 operator*(const float3x3 &transformation, const float (*vector)[2])
175  {
176  return transformation * float2(vector);
177  }
178 
180  {
182  transpose_m3_m3(result.values, values);
183  return result;
184  }
185 
187  {
189  invert_m3_m3(result.values, values);
190  return result;
191  }
192 
193  friend bool operator==(const float3x3 &a, const float3x3 &b)
194  {
195  return equals_m3m3(a.values, b.values);
196  }
197 };
198 
199 } // namespace blender
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_ASSERT_UNIT_V2(v)
void unit_m3(float m[3][3])
Definition: math_matrix.c:40
void mul_m3_m3_post(float R[3][3], const float B[3][3])
Definition: math_matrix.c:409
void zero_m3(float m[3][3])
Definition: math_matrix.c:23
bool equals_m3m3(const float mat1[3][3], const float mat2[3][3])
Definition: math_matrix.c:2525
bool invert_m3_m3(float R[3][3], const float A[3][3])
Definition: math_matrix.c:1180
void mul_v2_m3v2(float r[2], const float m[3][3], const float v[2])
Definition: math_matrix.c:711
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
Definition: math_matrix.c:897
void transpose_m3_m3(float R[3][3], const float M[3][3])
Definition: math_matrix.c:1347
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
Definition: math_matrix.c:388
static unsigned a[3]
Definition: RandGen.cpp:78
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
vec_base< float, 2 > float2
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
__int64 int64_t
Definition: stdint.h:89
const float * operator[](const int64_t index) const
float[3][3] c_style_float3x3
static float3x3 from_translation(const float2 translation)
Definition: BLI_float3x3.hh:45
const c_style_float3x3 & ptr() const
float3x3 transposed() const
static float3x3 from_rotation(float rotation)
Definition: BLI_float3x3.hh:53
float3x3()=default
friend float3x3 operator*(const float3x3 &a, const float3x3 &b)
static float3x3 identity()
Definition: BLI_float3x3.hh:38
static float3x3 zero()
Definition: BLI_float3x3.hh:31
float3x3(const float *matrix)
Definition: BLI_float3x3.hh:22
void operator*=(const float3x3 &other)
static float3x3 from_normalized_axes(const float2 translation, const float2 horizontal, const float2 vertical)
Definition: BLI_float3x3.hh:85
static float3x3 from_origin_transformation(const float3x3 &transformation, const float2 origin)
float3x3 inverted() const
friend float3 operator*(const float3x3 &a, const float3 &b)
static float3x3 from_translation_rotation_scale(const float2 translation, float rotation, const float2 scale)
Definition: BLI_float3x3.hh:66
friend float2 operator*(const float3x3 &transformation, const float(*vector)[2])
friend bool operator==(const float3x3 &a, const float3x3 &b)
friend float2 operator*(const float3x3 &transformation, const float2 &vector)
c_style_float3x3 & ptr()
float3x3(const float matrix[3][3])
Definition: BLI_float3x3.hh:27
float values[3][3]
Definition: BLI_float3x3.hh:18
float * operator[](const int64_t index)