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: b64dc86a1ee0a73f6aa76e70dc8d372e78a9f1fd (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
/*
 * 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"

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);
  }

  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 */