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

BLI_float2x2.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d4f9ae522a1502fd12d6f0321ddf0396c8a35c4 (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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#pragma once

#include "BLI_assert.h"
#include "BLI_float2.hh"
#include "BLI_math_matrix.h"

#include <cmath>
#include <tuple>

namespace blender {

/**
 * A 2x2 column major matrix.
 *
 * float2x2::values[i] is the ith column of the matrix.
 *
 * |m00 m10|
 * |m01 m11|
 *
 */
struct float2x2 {
  float values[2][2];

  float2x2() = default;

  float2x2(const float *matrix)
  {
    memcpy(values, matrix, sizeof(float) * 4);
  }

  float2x2(const float matrix[2][2]) : float2x2(static_cast<const float *>(matrix[0]))
  {
  }

  static float2x2 identity()
  {
    float2x2 mat;
    unit_m2(mat.values);
    return mat;
  }

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

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

  using c_style_float2x2 = float[2][2];
  c_style_float2x2 &ptr()
  {
    return values;
  }

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

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

  friend float2 operator*(const float2x2 &m, const float2 &v)
  {
    float2 result;
    mul_v2_m2v2(result, m.values, v);
    return result;
  }

  friend float2 operator*(const float2x2 &m, const float (*v)[2])
  {
    return m * float2(v);
  }

  /**
   * Multiplies all the elements of `m` with `val`
   */
  friend float2x2 operator*(const float2x2 &m, const float val)
  {
    float2x2 res;

    res.ptr()[0][0] = m.ptr()[0][0] * val;
    res.ptr()[0][1] = m.ptr()[0][1] * val;
    res.ptr()[1][0] = m.ptr()[1][0] * val;
    res.ptr()[1][1] = m.ptr()[1][1] * val;

    return res;
  }

  friend float2x2 operator+(const float2x2 &m1, const float2x2 &m2)
  {
    float2x2 res;

    res.ptr()[0][0] = m1.ptr()[0][0] + m2.ptr()[0][0];
    res.ptr()[0][1] = m1.ptr()[0][1] + m2.ptr()[0][1];
    res.ptr()[1][0] = m1.ptr()[1][0] + m2.ptr()[1][0];
    res.ptr()[1][1] = m1.ptr()[1][1] + m2.ptr()[1][1];

    return float2x2(res);
  }

  float2x2 linear_blend(const float2x2 &other, float factor) const
  {
    BLI_assert(factor >= 0.0 && factor <= 1.0);
    const float inv_factor = 1.0 - factor;
    float2x2 res;

    res.ptr()[0][0] = this->ptr()[0][0] * factor + other.ptr()[0][0] * inv_factor;
    res.ptr()[0][1] = this->ptr()[0][1] * factor + other.ptr()[0][1] * inv_factor;
    res.ptr()[1][0] = this->ptr()[1][0] * factor + other.ptr()[1][0] * inv_factor;
    res.ptr()[1][1] = this->ptr()[1][1] * factor + other.ptr()[1][1] * inv_factor;

    return float2x2(res);
  }

  /**
   * Computes the eigen decomposition of the 2x2 matrix and returns Q
   * and Lambda
   *
   * A = Q * Lambda * Q.inverse()
   *
   * A is this matrix.
   *
   * Q is a 2x2 matrix whose ith column is the eigen vector q_i of the
   * matrix A.
   *
   * Lambda is the diagonal matrix whose diagonal elements are the
   * corresponding eigenvalues.
   *
   * Reference https://en.wikipedia.org/wiki/Eigenvalue_algorithm
   * http://www.math.harvard.edu/archive/21b_fall_04/exhibits/2dmatrices/index.html
   */
  std::tuple<float2x2, float2> eigen_decomposition() const
  {
    const auto a = this->ptr()[0][0];
    const auto b = this->ptr()[1][0];
    const auto c = this->ptr()[0][1];
    const auto d = this->ptr()[1][1];

    const auto trace = a + d;
    const auto det = a * d - b * c;

    const auto l1 = (trace + std::sqrt(trace * trace - 4.0 * det)) / 2.0;
    const auto l2 = (trace - std::sqrt(trace * trace - 4.0 * det)) / 2.0;
    const auto lambda = float2(l1, l2);

    if (c != 0.0) {
      float2x2 q;
      q.ptr()[0][0] = l1 - d;
      q.ptr()[1][0] = l2 - d;
      q.ptr()[0][1] = c;
      q.ptr()[1][1] = c;
      return {q, lambda};
    }
    if (b != 0.0) {
      float2x2 q;
      q.ptr()[0][0] = b;
      q.ptr()[1][0] = b;
      q.ptr()[0][1] = l1 - a;
      q.ptr()[1][1] = l2 - a;
      return {q, lambda};
    }

    float2x2 q;
    q.ptr()[0][0] = 1;
    q.ptr()[1][0] = 0;
    q.ptr()[0][1] = 0;
    q.ptr()[1][1] = 1;
    return {q, lambda};
  }

  uint64_t hash() const
  {
    uint64_t h = 435109;
    for (int i = 0; i < 4; i++) {
      float value = (static_cast<const float *>(values[0]))[i];
      h = h * 33 + *reinterpret_cast<const uint32_t *>(&value);
    }
    return h;
  }
};

} /* namespace blender */