Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_float3x3.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 178973c155dc9e3cbb5e2f0f88d7ba9c0502c2ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

#include <cmath>
#include <cstdint>

#include "BLI_assert.h"
#include "BLI_math_base.h"
#include "BLI_math_matrix.h"
#include "BLI_math_vec_types.hh"
#include "BLI_math_vector.h"

namespace blender {

struct float3x3 {
  /* A 3x3 matrix in column major order. */
  float values[3][3];

  float3x3() = default;

  float3x3(const float *matrix)
  {
    memcpy(values, matrix, sizeof(float) * 3 * 3);
  }

  float3x3(const float matrix[3][3]) : float3x3(static_cast<const float *>(matrix[0]))
  {
  }

  static float3x3 zero()
  {
    float3x3 result;
    zero_m3(result.values);
    return result;
  }

  static float3x3 identity()
  {
    float3x3 result;
    unit_m3(result.values);
    return result;
  }

  static float3x3 from_translation(const float2 translation)
  {
    float3x3 result = identity();
    result.values[2][0] = translation.x;
    result.values[2][1] = translation.y;
    return result;
  }

  static float3x3 from_rotation(float rotation)
  {
    float3x3 result = zero();
    const float cosine = std::cos(rotation);
    const float sine = std::sin(rotation);
    result.values[0][0] = cosine;
    result.values[0][1] = sine;
    result.values[1][0] = -sine;
    result.values[1][1] = cosine;
    result.values[2][2] = 1.0f;
    return result;
  }

  static float3x3 from_scale(const float2 scale)
  {
    float3x3 result = zero();
    result.values[0][0] = scale.x;
    result.values[1][1] = scale.y;
    result.values[2][2] = 1.0f;
    return result;
  }

  static float3x3 from_translation_rotation_scale(const float2 translation,
                                                  float rotation,
                                                  const float2 scale)
  {
    float3x3 result;
    const float cosine = std::cos(rotation);
    const float sine = std::sin(rotation);
    result.values[0][0] = scale.x * cosine;
    result.values[0][1] = scale.x * sine;
    result.values[0][2] = 0.0f;
    result.values[1][0] = scale.y * -sine;
    result.values[1][1] = scale.y * cosine;
    result.values[1][2] = 0.0f;
    result.values[2][0] = translation.x;
    result.values[2][1] = translation.y;
    result.values[2][2] = 1.0f;
    return result;
  }

  static float3x3 from_normalized_axes(const float2 translation,
                                       const float2 horizontal,
                                       const float2 vertical)
  {
    BLI_ASSERT_UNIT_V2(horizontal);
    BLI_ASSERT_UNIT_V2(vertical);

    float3x3 result;
    result.values[0][0] = horizontal.x;
    result.values[0][1] = horizontal.y;
    result.values[0][2] = 0.0f;
    result.values[1][0] = vertical.x;
    result.values[1][1] = vertical.y;
    result.values[1][2] = 0.0f;
    result.values[2][0] = translation.x;
    result.values[2][1] = translation.y;
    result.values[2][2] = 1.0f;
    return result;
  }

  /* Construct a transformation that is pivoted around the given origin point. So for instance,
   * from_origin_transformation(from_rotation(M_PI_2), float2(0.0f, 2.0f))
   * will construct a transformation representing a 90 degree rotation around the point (0, 2). */
  static float3x3 from_origin_transformation(const float3x3 &transformation, const float2 origin)
  {
    return from_translation(origin) * transformation * from_translation(-origin);
  }

  operator float *()
  {
    return &values[0][0];
  }

  operator const float *() const
  {
    return &values[0][0];
  }

  float *operator[](const int64_t index)
  {
    BLI_assert(index >= 0);
    BLI_assert(index < 3);
    return &values[index][0];
  }

  const float *operator[](const int64_t index) const
  {
    BLI_assert(index >= 0);
    BLI_assert(index < 3);
    return &values[index][0];
  }

  using c_style_float3x3 = float[3][3];
  c_style_float3x3 &ptr()
  {
    return values;
  }

  const c_style_float3x3 &ptr() const
  {
    return values;
  }

  friend float3x3 operator*(const float3x3 &a, const float3x3 &b)
  {
    float3x3 result;
    mul_m3_m3m3(result.values, a.values, b.values);
    return result;
  }

  friend float3 operator*(const float3x3 &a, const float3 &b)
  {
    float3 result;
    mul_v3_m3v3(result, a.values, b);
    return result;
  }

  void operator*=(const float3x3 &other)
  {
    mul_m3_m3_post(values, other.values);
  }

  friend float2 operator*(const float3x3 &transformation, const float2 &vector)
  {
    float2 result;
    mul_v2_m3v2(result, transformation.values, vector);
    return result;
  }

  friend float2 operator*(const float3x3 &transformation, const float (*vector)[2])
  {
    return transformation * float2(vector);
  }

  float3x3 transposed() const
  {
    float3x3 result;
    transpose_m3_m3(result.values, values);
    return result;
  }

  float3x3 inverted() const
  {
    float3x3 result;
    invert_m3_m3(result.values, values);
    return result;
  }

  float2 scale_2d() const
  {
    float2 scale;
    mat3_to_size_2d(scale, values);
    return scale;
  }

  friend bool operator==(const float3x3 &a, const float3x3 &b)
  {
    return equals_m3m3(a.values, b.values);
  }
};

}  // namespace blender