/
vpmat2x1x32.go
332 lines (263 loc) · 8.37 KB
/
vpmat2x1x32.go
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Vapor is a toolkit designed to support Liquid War 7.
// Copyright (C) 2015, 2016 Christian Mauduit <ufoot@ufoot.org>
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// Vapor homepage: https://github.com/ufoot/vapor
// Contact author: ufoot@ufoot.org
package vpmat2x1
import (
"encoding/json"
"github.com/ufoot/vapor/go/vperror"
"github.com/ufoot/vapor/go/vpnumber"
"github.com/ufoot/vapor/go/vpvec2"
)
// X32 is a matrix containing 2x1 fixed point 32 bit values.
// Can hold the values of a point in a plane.
type X32 [Size]vpnumber.X32
// X32New creates a new matrix containing 2x1 fixed point 32 bit values.
// The column-major (OpenGL notation) mode is used,
// first elements fill first column.
func X32New(x1, x2 vpnumber.X32) *X32 {
return &X32{x1, x2}
}
// X32Identity creates a new identity matrix.
func X32Identity() *X32 {
return &X32{vpnumber.X32Const1, vpnumber.X32Const0}
}
// X32Translation creates a new translation matrix.
func X32Translation(x vpnumber.X32) *X32 {
return &X32{vpnumber.X32Const1, x}
}
// X32Scale creates a new scale matrix.
func X32Scale(x vpnumber.X32) *X32 {
return &X32{x, vpnumber.X32Const0}
}
// X32RebaseOX creates a matrix that translates from the default
// O=(0), X=(1) basis to the given
// basis. It assumes f(a+b) equals f(a)+f(b).
func X32RebaseOX(Origin, PosX vpnumber.X32) *X32 {
return &X32{PosX - Origin, Origin}
}
// ToX64 converts the matrix to a fixed point number matrix on 64 bits.
func (mat *X32) ToX64() *X64 {
var ret X64
for i, v := range mat {
ret[i] = vpnumber.X32ToX64(v)
}
return &ret
}
// ToF32 converts the matrix to a float32 matrix.
func (mat *X32) ToF32() *F32 {
var ret F32
for i, v := range mat {
ret[i] = vpnumber.X32ToF32(v)
}
return &ret
}
// ToF64 converts the matrix to a float64 matrix.
func (mat *X32) ToF64() *F64 {
var ret F64
for i, v := range mat {
ret[i] = vpnumber.X32ToF64(v)
}
return &ret
}
// Set sets the value of the matrix for a given column and row.
func (mat *X32) Set(col, row int, val vpnumber.X32) {
mat[col+row] = val
}
// Get gets the value of the matrix for a given column and row.
func (mat *X32) Get(col, row int) vpnumber.X32 {
return mat[col+row]
}
// SetCol sets a column to the values contained in a vector.
func (mat *X32) SetCol(col int, vec vpnumber.X32) {
mat[col] = vec
}
// GetCol gets a column and returns it in a vector.
func (mat *X32) GetCol(col int) vpnumber.X32 {
return mat[col]
}
// SetRow sets a row to the values contained in a vector.
func (mat *X32) SetRow(vec *vpvec2.X32) {
*mat = X32(*vec)
}
// GetRow gets a row and returns it in a vector.
func (mat *X32) GetRow() *vpvec2.X32 {
ret := vpvec2.X32(*mat)
return &ret
}
// MarshalJSON implements the json.Marshaler interface.
func (mat *X32) MarshalJSON() ([]byte, error) {
var tmpArray [Width][Height]int32
for col := range tmpArray {
for row := range tmpArray[col] {
tmpArray[col][row] = int32(mat[col+row])
}
}
ret, err := json.Marshal(tmpArray)
if err != nil {
return nil, vperror.Chain(err, "unable to marshal X32")
}
return ret, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (mat *X32) UnmarshalJSON(data []byte) error {
var tmpArray [Width][Height]int32
err := json.Unmarshal(data, &tmpArray)
if err != nil {
return vperror.Chain(err, "unable to unmarshal X32")
}
for col := range tmpArray {
for row := range tmpArray[col] {
mat[col+row] = vpnumber.X32(tmpArray[col][row])
}
}
return nil
}
// String returns a readable form of the matrix.
func (mat *X32) String() string {
buf, err := mat.ToF32().MarshalJSON()
if err != nil {
// Catching & ignoring error
return ""
}
return string(buf)
}
// Add adds operand to the matrix.
// It modifies the matrix, and returns a pointer on it.
func (mat *X32) Add(op *X32) *X32 {
for i, v := range op {
mat[i] += v
}
return mat
}
// Sub substracts operand from the matrix.
// It modifies the matrix, and returns a pointer on it.
func (mat *X32) Sub(op *X32) *X32 {
for i, v := range op {
mat[i] -= v
}
return mat
}
// MulScale multiplies all values of the matrix by factor.
// It modifies the matrix, and returns a pointer on it.
func (mat *X32) MulScale(factor vpnumber.X32) *X32 {
for i, v := range mat {
mat[i] = vpnumber.X32Mul(v, factor)
}
return mat
}
// DivScale divides all values of the matrix by factor.
// It modifies the matrix, and returns a pointer on it.
func (mat *X32) DivScale(factor vpnumber.X32) *X32 {
for i, v := range mat {
mat[i] = vpnumber.X32Div(v, factor)
}
return mat
}
// IsSimilar returns true if matrices are approximatively the same.
// This is a workarround to ignore rounding errors.
func (mat *X32) IsSimilar(op *X32) bool {
ret := true
for i, v := range mat {
ret = ret && vpnumber.X32IsSimilar(v, op[i])
}
return ret
}
// MulComp multiplies the matrix by another matrix (composition).
// It modifies the matrix, and returns a pointer on it.
func (mat *X32) MulComp(op *X32) *X32 {
*mat = *X32MulComp(mat, op)
return mat
}
// Det returns the matrix determinant.
func (mat *X32) Det() vpnumber.X32 {
return mat[Col0Row0]
}
// Inv inverts the matrix.
// Never fails (no division by zero error, never) but if the
// matrix can't be inverted, result does not make sense.
// It modifies the matrix, and returns a pointer on it.
func (mat *X32) Inv() *X32 {
*mat = *X32Inv(mat)
return mat
}
// MulVecPos performs a multiplication of a vector by a 2x1 matrix,
// considering the vector is a column vector (matrix left, vector right).
// The last member of the vector is assumed to be 1, so in practice a
// position vector of length 1 (here, a scalar) is passed. This allow geometric
// transformations such as rotations and translations to be accumulated
// within the matrix and then performed at once.
func (mat *X32) MulVecPos(vec vpnumber.X32) vpnumber.X32 {
return vpnumber.X32Mul(mat[Col0Row0], vec) + mat[Col1Row0]
}
// MulVecDir performs a multiplication of a vector by a 2x1 matrix,
// considering the vector is a column vector (matrix left, vector right).
// The last member of the vector is assumed to be 0, so in practice a
// direction vector of length 1 (here, a scalar) is passed. This allow geometric
// transformations such as rotations to be accumulated
// within the matrix and then performed at once.
func (mat *X32) MulVecDir(vec vpnumber.X32) vpnumber.X32 {
return vpnumber.X32Mul(mat[Col0Row0], vec)
}
// X32Add adds two matrices.
// Args are left untouched, a pointer on a new object is returned.
func X32Add(mata, matb *X32) *X32 {
var ret = *mata
_ = ret.Add(matb)
return &ret
}
// X32Sub substracts matrix b from matrix a.
// Args are left untouched, a pointer on a new object is returned.
func X32Sub(mata, matb *X32) *X32 {
var ret = *mata
_ = ret.Sub(matb)
return &ret
}
// X32MulScale multiplies all values of a matrix by a scalar.
// Args are left untouched, a pointer on a new object is returned.
func X32MulScale(mat *X32, factor vpnumber.X32) *X32 {
var ret = *mat
_ = ret.MulScale(factor)
return &ret
}
// X32DivScale divides all values of a matrix by a scalar.
// Args are left untouched, a pointer on a new object is returned.
func X32DivScale(mat *X32, factor vpnumber.X32) *X32 {
var ret = *mat
_ = ret.DivScale(factor)
return &ret
}
// X32MulComp multiplies two matrices (composition).
// Args are left untouched, a pointer on a new object is returned.
func X32MulComp(a, b *X32) *X32 {
ret := X32{vpnumber.X32Mul(a[Col0Row0], b[Col0Row0]),
vpnumber.X32Mul(a[Col0Row0], b[Col1Row0]) + a[Col1Row0]}
return &ret
}
// X32Inv inverts a matrix.
// Never fails (no division by zero error, never) but if the
// matrix can't be inverted, result does not make sense.
// Args is left untouched, a pointer on a new object is returned.
func X32Inv(mat *X32) *X32 {
ret := X32{
vpnumber.X32Const1,
-mat[Col1Row0],
}
det := mat.Det()
ret.DivScale(det)
return &ret
}