File: matrix_3x3.h

package info (click to toggle)
retroarch 1.20.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,736 kB
  • sloc: ansic: 1,207,646; cpp: 104,166; objc: 8,567; asm: 6,624; python: 3,776; makefile: 2,838; sh: 2,786; xml: 1,408; perl: 393; javascript: 10
file content (253 lines) | stat: -rw-r--r-- 9,728 bytes parent folder | download | duplicates (5)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Copyright  (C) 2010-2020 The RetroArch team
 *
 * ---------------------------------------------------------------------------------------
 * The following license statement only applies to this file (matrix_3x3.h).
 * ---------------------------------------------------------------------------------------
 *
 * Permission is hereby granted, free of charge,
 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef __LIBRETRO_SDK_GFX_MATH_MATRIX_3X3_H__
#define __LIBRETRO_SDK_GFX_MATH_MATRIX_3X3_H__

#include <boolean.h>
#include <math.h>
#include <string.h>

#include <retro_common_api.h>
#include <retro_inline.h>

RETRO_BEGIN_DECLS

typedef struct math_matrix_3x3
{
   float data[9];
} math_matrix_3x3;

#define MAT_ELEM_3X3(mat, r, c) ((mat).data[3 * (r) + (c)])

#define matrix_3x3_init(mat, n11, n12, n13, n21, n22, n23, n31, n32, n33) \
   MAT_ELEM_3X3(mat, 0, 0) = n11; \
   MAT_ELEM_3X3(mat, 0, 1) = n12; \
   MAT_ELEM_3X3(mat, 0, 2) = n13; \
   MAT_ELEM_3X3(mat, 1, 0) = n21; \
   MAT_ELEM_3X3(mat, 1, 1) = n22; \
   MAT_ELEM_3X3(mat, 1, 2) = n23; \
   MAT_ELEM_3X3(mat, 2, 0) = n31; \
   MAT_ELEM_3X3(mat, 2, 1) = n32; \
   MAT_ELEM_3X3(mat, 2, 2) = n33

#define matrix_3x3_identity(mat) \
   MAT_ELEM_3X3(mat, 0, 0) = 1.0f; \
   MAT_ELEM_3X3(mat, 0, 1) = 0; \
   MAT_ELEM_3X3(mat, 0, 2) = 0; \
   MAT_ELEM_3X3(mat, 1, 0) = 0; \
   MAT_ELEM_3X3(mat, 1, 1) = 1.0f; \
   MAT_ELEM_3X3(mat, 1, 2) = 0; \
   MAT_ELEM_3X3(mat, 2, 0) = 0; \
   MAT_ELEM_3X3(mat, 2, 1) = 0; \
   MAT_ELEM_3X3(mat, 2, 2) = 1.0f

#define matrix_3x3_divide_scalar(mat, s) \
   MAT_ELEM_3X3(mat, 0, 0) /= s; \
   MAT_ELEM_3X3(mat, 0, 1) /= s; \
   MAT_ELEM_3X3(mat, 0, 2) /= s; \
   MAT_ELEM_3X3(mat, 1, 0) /= s; \
   MAT_ELEM_3X3(mat, 1, 1) /= s; \
   MAT_ELEM_3X3(mat, 1, 2) /= s; \
   MAT_ELEM_3X3(mat, 2, 0) /= s; \
   MAT_ELEM_3X3(mat, 2, 1) /= s; \
   MAT_ELEM_3X3(mat, 2, 2) /= s

#define matrix_3x3_transpose(mat, in) \
   MAT_ELEM_3X3(mat, 0, 0) = MAT_ELEM_3X3(in, 0, 0); \
   MAT_ELEM_3X3(mat, 1, 0) = MAT_ELEM_3X3(in, 0, 1); \
   MAT_ELEM_3X3(mat, 2, 0) = MAT_ELEM_3X3(in, 0, 2); \
   MAT_ELEM_3X3(mat, 0, 1) = MAT_ELEM_3X3(in, 1, 0); \
   MAT_ELEM_3X3(mat, 1, 1) = MAT_ELEM_3X3(in, 1, 1); \
   MAT_ELEM_3X3(mat, 2, 1) = MAT_ELEM_3X3(in, 1, 2); \
   MAT_ELEM_3X3(mat, 0, 2) = MAT_ELEM_3X3(in, 2, 0); \
   MAT_ELEM_3X3(mat, 1, 2) = MAT_ELEM_3X3(in, 2, 1); \
   MAT_ELEM_3X3(mat, 2, 2) = MAT_ELEM_3X3(in, 2, 2)

#define matrix_3x3_multiply(out, a, b) \
   MAT_ELEM_3X3(out, 0, 0) =  \
      MAT_ELEM_3X3(a, 0, 0) * MAT_ELEM_3X3(b, 0, 0) + \
      MAT_ELEM_3X3(a, 0, 1) * MAT_ELEM_3X3(b, 1, 0) + \
      MAT_ELEM_3X3(a, 0, 2) * MAT_ELEM_3X3(b, 2, 0); \
   MAT_ELEM_3X3(out, 0, 1) = \
      MAT_ELEM_3X3(a, 0, 0) * MAT_ELEM_3X3(b, 0, 1) + \
      MAT_ELEM_3X3(a, 0, 1) * MAT_ELEM_3X3(b, 1, 1) + \
      MAT_ELEM_3X3(a, 0, 2) * MAT_ELEM_3X3(b, 2, 1); \
   MAT_ELEM_3X3(out, 0, 2) = \
      MAT_ELEM_3X3(a, 0, 0) * MAT_ELEM_3X3(b, 0, 2) + \
      MAT_ELEM_3X3(a, 0, 1) * MAT_ELEM_3X3(b, 1, 2) + \
      MAT_ELEM_3X3(a, 0, 2) * MAT_ELEM_3X3(b, 2, 2); \
   MAT_ELEM_3X3(out, 1, 0) = \
      MAT_ELEM_3X3(a, 1, 0) * MAT_ELEM_3X3(b, 0, 0) + \
      MAT_ELEM_3X3(a, 1, 1) * MAT_ELEM_3X3(b, 1, 0) + \
      MAT_ELEM_3X3(a, 1, 2) * MAT_ELEM_3X3(b, 2, 0); \
   MAT_ELEM_3X3(out, 1, 1) =  \
      MAT_ELEM_3X3(a, 1, 0) * MAT_ELEM_3X3(b, 0, 1) + \
      MAT_ELEM_3X3(a, 1, 1) * MAT_ELEM_3X3(b, 1, 1) + \
      MAT_ELEM_3X3(a, 1, 2) * MAT_ELEM_3X3(b, 2, 1); \
   MAT_ELEM_3X3(out, 1, 2) = \
      MAT_ELEM_3X3(a, 1, 0) * MAT_ELEM_3X3(b, 0, 2) + \
      MAT_ELEM_3X3(a, 1, 1) * MAT_ELEM_3X3(b, 1, 2) + \
      MAT_ELEM_3X3(a, 1, 2) * MAT_ELEM_3X3(b, 2, 2); \
   MAT_ELEM_3X3(out, 2, 0) =  \
      MAT_ELEM_3X3(a, 2, 0) * MAT_ELEM_3X3(b, 0, 0) + \
      MAT_ELEM_3X3(a, 2, 1) * MAT_ELEM_3X3(b, 1, 0) + \
      MAT_ELEM_3X3(a, 2, 2) * MAT_ELEM_3X3(b, 2, 0); \
   MAT_ELEM_3X3(out, 2, 1) = \
      MAT_ELEM_3X3(a, 2, 0) * MAT_ELEM_3X3(b, 0, 1) + \
      MAT_ELEM_3X3(a, 2, 1) * MAT_ELEM_3X3(b, 1, 1) + \
      MAT_ELEM_3X3(a, 2, 2) * MAT_ELEM_3X3(b, 2, 1); \
   MAT_ELEM_3X3(out, 2, 2) =  \
      MAT_ELEM_3X3(a, 2, 0) * MAT_ELEM_3X3(b, 0, 2) + \
      MAT_ELEM_3X3(a, 2, 1) * MAT_ELEM_3X3(b, 1, 2) + \
      MAT_ELEM_3X3(a, 2, 2) * MAT_ELEM_3X3(b, 2, 2)

#define matrix_3x3_determinant(mat) (MAT_ELEM_3X3(mat, 0, 0) * (MAT_ELEM_3X3(mat, 1, 1) * MAT_ELEM_3X3(mat, 2, 2) - MAT_ELEM_3X3(mat, 1, 2) * MAT_ELEM_3X3(mat, 2, 1)) - MAT_ELEM_3X3(mat, 0, 1) * (MAT_ELEM_3X3(mat, 1, 0) * MAT_ELEM_3X3(mat, 2, 2) - MAT_ELEM_3X3(mat, 1, 2) * MAT_ELEM_3X3(mat, 2, 0)) + MAT_ELEM_3X3(mat, 0, 2) * (MAT_ELEM_3X3(mat, 1, 0) * MAT_ELEM_3X3(mat, 2, 1) - MAT_ELEM_3X3(mat, 1, 1) * MAT_ELEM_3X3(mat, 2, 0)))

#define matrix_3x3_adjoint(mat) \
   MAT_ELEM_3X3(mat, 0, 0) =  (MAT_ELEM_3X3(mat, 1, 1) * MAT_ELEM_3X3(mat, 2, 2) - MAT_ELEM_3X3(mat, 1, 2) * MAT_ELEM_3X3(mat, 2, 1)); \
   MAT_ELEM_3X3(mat, 0, 1) = -(MAT_ELEM_3X3(mat, 0, 1) * MAT_ELEM_3X3(mat, 2, 2) - MAT_ELEM_3X3(mat, 0, 2) * MAT_ELEM_3X3(mat, 2, 1)); \
   MAT_ELEM_3X3(mat, 0, 2) =  (MAT_ELEM_3X3(mat, 0, 1) * MAT_ELEM_3X3(mat, 1, 1) - MAT_ELEM_3X3(mat, 0, 2) * MAT_ELEM_3X3(mat, 1, 1)); \
   MAT_ELEM_3X3(mat, 1, 0) = -(MAT_ELEM_3X3(mat, 1, 0) * MAT_ELEM_3X3(mat, 2, 2) - MAT_ELEM_3X3(mat, 1, 2) * MAT_ELEM_3X3(mat, 2, 0)); \
   MAT_ELEM_3X3(mat, 1, 1) =  (MAT_ELEM_3X3(mat, 0, 0) * MAT_ELEM_3X3(mat, 2, 2) - MAT_ELEM_3X3(mat, 0, 2) * MAT_ELEM_3X3(mat, 2, 0)); \
   MAT_ELEM_3X3(mat, 1, 2) = -(MAT_ELEM_3X3(mat, 0, 0) * MAT_ELEM_3X3(mat, 1, 2) - MAT_ELEM_3X3(mat, 0, 2) * MAT_ELEM_3X3(mat, 1, 0)); \
   MAT_ELEM_3X3(mat, 2, 0) =  (MAT_ELEM_3X3(mat, 1, 0) * MAT_ELEM_3X3(mat, 2, 1) - MAT_ELEM_3X3(mat, 1, 1) * MAT_ELEM_3X3(mat, 2, 0)); \
   MAT_ELEM_3X3(mat, 2, 1) = -(MAT_ELEM_3X3(mat, 0, 0) * MAT_ELEM_3X3(mat, 2, 1) - MAT_ELEM_3X3(mat, 0, 1) * MAT_ELEM_3X3(mat, 2, 0)); \
   MAT_ELEM_3X3(mat, 2, 2) =  (MAT_ELEM_3X3(mat, 0, 0) * MAT_ELEM_3X3(mat, 1, 1) - MAT_ELEM_3X3(mat, 0, 1) * MAT_ELEM_3X3(mat, 1, 0))

#define FLOATS_ARE_EQUAL(x, y)  (fabs(x - y) <= 0.00001f * ((x) > (y) ? (y) : (x)))
#define FLOAT_IS_ZERO(x)        (FLOATS_ARE_EQUAL((x) + 1, 1))

static INLINE bool matrix_3x3_invert(math_matrix_3x3 *mat)
{
   float det = matrix_3x3_determinant(*mat);

   if (FLOAT_IS_ZERO(det))
      return false;

   matrix_3x3_adjoint(*mat);
   matrix_3x3_divide_scalar(*mat, det);

   return true;
}

static INLINE bool matrix_3x3_square_to_quad(
      const float dx0, const float dy0,
      const float dx1, const float dy1,
      const float dx3, const float dy3,
      const float dx2, const float dy2,
      math_matrix_3x3 *mat)
{
   float a, b, d, e;
   float ax  = dx0 - dx1 + dx2 - dx3;
   float ay  = dy0 - dy1 + dy2 - dy3;
   float c   = dx0;
   float f   = dy0;
   float g   = 0;
   float h   = 0;

   if (FLOAT_IS_ZERO(ax) && FLOAT_IS_ZERO(ay))
   {
      /* affine case */
      a = dx1 - dx0;
      b = dx2 - dx1;
      d = dy1 - dy0;
      e = dy2 - dy1;
   }
   else
   {
      float ax1 = dx1 - dx2;
      float ax2 = dx3 - dx2;
      float ay1 = dy1 - dy2;
      float ay2 = dy3 - dy2;

      /* determinants */
      float gtop    =  ax  * ay2 - ax2 * ay;
      float htop    =  ax1 * ay  - ax  * ay1;
      float bottom  =  ax1 * ay2 - ax2 * ay1;

      if (!bottom)
         return false;

      g = gtop / bottom;
      h = htop / bottom;

      a = dx1 - dx0 + g * dx1;
      b = dx3 - dx0 + h * dx3;
      d = dy1 - dy0 + g * dy1;
      e = dy3 - dy0 + h * dy3;
   }

   matrix_3x3_init(*mat,
         a, d, g,
         b, e, h,
         c, f, 1.f);

   return true;
}

static INLINE bool matrix_3x3_quad_to_square(
      const float sx0, const float sy0,
      const float sx1, const float sy1,
      const float sx2, const float sy2,
      const float sx3, const float sy3,
      math_matrix_3x3 *mat)
{
   return matrix_3x3_square_to_quad(sx0, sy0, sx1, sy1,
         sx2, sy2, sx3, sy3,
         mat) ? matrix_3x3_invert(mat) : false;
}

static INLINE bool matrix_3x3_quad_to_quad(
      const float dx0, const float dy0,
      const float dx1, const float dy1,
      const float dx2, const float dy2,
      const float dx3, const float dy3,
      const float sx0, const float sy0,
      const float sx1, const float sy1,
      const float sx2, const float sy2,
      const float sx3, const float sy3,
      math_matrix_3x3 *mat)
{
   math_matrix_3x3 square_to_quad;

   if (matrix_3x3_square_to_quad(dx0, dy0, dx1, dy1,
            dx2, dy2, dx3, dy3,
            &square_to_quad))
   {
      math_matrix_3x3 quad_to_square;
      if (matrix_3x3_quad_to_square(sx0, sy0, sx1, sy1,
               sx2, sy2, sx3, sy3,
               &quad_to_square))
      {
         matrix_3x3_multiply(*mat, quad_to_square, square_to_quad);

         return true;
      }
   }

   return false;
}

RETRO_END_DECLS

#endif