1
{ Implement matrix operators.
2
Copied and adjusted from FPC's rtl/inc/mmatimp.inc }
4
{*****************************************************************************
5
Matrix to matrix operations
6
*****************************************************************************}
8
operator + (const m1,m2:objectname) : objectname;
10
{Add the elements of a matrix to each other.}
13
result[0,0]:=m1[0,0]+m2[0,0];
14
result[0,1]:=m1[0,1]+m2[0,1];
16
result[0,2]:=m1[0,2]+m2[0,2];
19
result[0,3]:=m1[0,3]+m2[0,3];
21
result[1,0]:=m1[1,0]+m2[1,0];
22
result[1,1]:=m1[1,1]+m2[1,1];
24
result[1,2]:=m1[1,2]+m2[1,2];
27
result[1,3]:=m1[1,3]+m2[1,3];
30
result[2,0]:=m1[2,0]+m2[2,0];
31
result[2,1]:=m1[2,1]+m2[2,1];
32
result[2,2]:=m1[2,2]+m2[2,2];
35
result[2,3]:=m1[2,3]+m2[2,3];
36
result[3,0]:=m1[3,0]+m2[3,0];
37
result[3,1]:=m1[3,1]+m2[3,1];
38
result[3,2]:=m1[3,2]+m2[3,2];
39
result[3,3]:=m1[3,3]+m2[3,3];
43
operator - (const m1,m2:objectname) : objectname;
45
{Subtract the elements of two matrixes from each other.}
48
result[0,0]:=m1[0,0]-m2[0,0];
49
result[0,1]:=m1[0,1]-m2[0,1];
51
result[0,2]:=m1[0,2]-m2[0,2];
54
result[0,3]:=m1[0,3]-m2[0,3];
56
result[1,0]:=m1[1,0]-m2[1,0];
57
result[1,1]:=m1[1,1]-m2[1,1];
59
result[1,2]:=m1[1,2]-m2[1,2];
62
result[1,3]:=m1[1,3]-m2[1,3];
65
result[2,0]:=m1[2,0]-m2[2,0];
66
result[2,1]:=m1[2,1]-m2[2,1];
67
result[2,2]:=m1[2,2]-m2[2,2];
70
result[2,3]:=m1[2,3]-m2[2,3];
71
result[3,0]:=m1[3,0]-m2[3,0];
72
result[3,1]:=m1[3,1]-m2[3,1];
73
result[3,2]:=m1[3,2]-m2[3,2];
74
result[3,3]:=m1[3,3]-m2[3,3];
78
operator - (const m1:objectname) : objectname;
80
{Negate the elements of a matrix.}
83
result[0,0]:=-m1[0,0];
84
result[0,1]:=-m1[0,1];
86
result[0,2]:=-m1[0,2];
89
result[0,3]:=-m1[0,3];
91
result[1,0]:=-m1[1,0];
92
result[1,1]:=-m1[1,1];
94
result[1,2]:=-m1[1,2];
97
result[1,3]:=-m1[1,3];
100
result[2,0]:=-m1[2,0];
101
result[2,1]:=-m1[2,1];
102
result[2,2]:=-m1[2,2];
105
result[2,3]:=-m1[2,3];
106
result[3,0]:=-m1[3,0];
107
result[3,1]:=-m1[3,1];
108
result[3,2]:=-m1[3,2];
109
result[3,3]:=-m1[3,3];
113
(* TODO: Fix, FPC matrix was ordered by rows, we should make transpose inside.
115
Doing transpose here would be a waste of time...
116
function MatrixTranspose(const M: TMatrix4Single): TMatrix4Single;
118
Result[0, 0] := M[0, 0];
119
Result[0, 1] := M[1, 0];
120
Result[0, 2] := M[2, 0];
121
Result[0, 3] := M[3, 0];
123
Result[1, 0] := M[0, 1];
124
Result[1, 1] := M[1, 1];
125
Result[1, 2] := M[2, 1];
126
Result[1, 3] := M[3, 1];
128
Result[2, 0] := M[0, 2];
129
Result[2, 1] := M[1, 2];
130
Result[2, 2] := M[2, 2];
131
Result[2, 3] := M[3, 2];
133
Result[3, 0] := M[0, 3];
134
Result[3, 1] := M[1, 3];
135
Result[3, 2] := M[2, 3];
136
Result[3, 3] := M[3, 3];
140
operator * (const m1,m2:objectname) : objectname;
142
{Multiply two matrixes.}
144
var r:array[0..matsize-1] of datatype;
148
for i:=0 to matsize-1 do
151
result[i,0]:=r[0]*m2[0,0]
153
{$if matsize>=3}+r[2]*m2[2,0]{$endif}
154
{$if matsize>=4}+r[3]*m2[3,0]{$endif};
155
result[i,1]:=r[0]*m2[0,1]
157
{$if matsize>=3}+r[2]*m2[2,1]{$endif}
158
{$if matsize>=4}+r[3]*m2[3,1]{$endif};
160
result[i,2]:=r[0]*m2[0,2]
163
{$if matsize>=4}+r[3]*m2[3,2]{$endif};
166
result[i,3]:=r[0]*m2[0,3]
175
{*****************************************************************************
176
Vector/matrix operations
177
*****************************************************************************}
179
(* TODO: Fix, FPC matrix was ordered by rows, we should make transpose inside.
181
operator * (const m:objectname;const v:vectorcompanion) : vectorcompanion;
183
{Multiplies a matrix with a vector.}
186
result[0]:=m[0,0]*v[0]
188
{$if matsize>=3}+m[0,2]*v[2]{$endif}
189
{$if matsize>=4}+m[0,3]*v[3]{$endif};
190
result[1]:=m[1,0]*v[0]
192
{$if matsize>=3}+m[1,2]*v[2]{$endif}
193
{$if matsize>=4}+m[1,3]*v[3]{$endif};
195
result[2]:=m[2,0]*v[0]
198
{$if matsize>=4}+m[2,3]*v[3]{$endif};
201
result[3]:=m[3,0]*v[0]
211
{ For now, we just define 4x4 multiplications. Copied from MatrixMult. }
212
operator * (const m1,m2:objectname) : objectname;
214
result[0, 0] := m1[0, 0] * m2[0, 0] + m1[1, 0] * m2[0, 1] + m1[2, 0] * m2[0, 2] + m1[3, 0] * m2[0, 3];
215
result[1, 0] := m1[0, 0] * m2[1, 0] + m1[1, 0] * m2[1, 1] + m1[2, 0] * m2[1, 2] + m1[3, 0] * m2[1, 3];
216
result[2, 0] := m1[0, 0] * m2[2, 0] + m1[1, 0] * m2[2, 1] + m1[2, 0] * m2[2, 2] + m1[3, 0] * m2[2, 3];
217
result[3, 0] := m1[0, 0] * m2[3, 0] + m1[1, 0] * m2[3, 1] + m1[2, 0] * m2[3, 2] + m1[3, 0] * m2[3, 3];
218
result[0, 1] := m1[0, 1] * m2[0, 0] + m1[1, 1] * m2[0, 1] + m1[2, 1] * m2[0, 2] + m1[3, 1] * m2[0, 3];
219
result[1, 1] := m1[0, 1] * m2[1, 0] + m1[1, 1] * m2[1, 1] + m1[2, 1] * m2[1, 2] + m1[3, 1] * m2[1, 3];
220
result[2, 1] := m1[0, 1] * m2[2, 0] + m1[1, 1] * m2[2, 1] + m1[2, 1] * m2[2, 2] + m1[3, 1] * m2[2, 3];
221
result[3, 1] := m1[0, 1] * m2[3, 0] + m1[1, 1] * m2[3, 1] + m1[2, 1] * m2[3, 2] + m1[3, 1] * m2[3, 3];
222
result[0, 2] := m1[0, 2] * m2[0, 0] + m1[1, 2] * m2[0, 1] + m1[2, 2] * m2[0, 2] + m1[3, 2] * m2[0, 3];
223
result[1, 2] := m1[0, 2] * m2[1, 0] + m1[1, 2] * m2[1, 1] + m1[2, 2] * m2[1, 2] + m1[3, 2] * m2[1, 3];
224
result[2, 2] := m1[0, 2] * m2[2, 0] + m1[1, 2] * m2[2, 1] + m1[2, 2] * m2[2, 2] + m1[3, 2] * m2[2, 3];
225
result[3, 2] := m1[0, 2] * m2[3, 0] + m1[1, 2] * m2[3, 1] + m1[2, 2] * m2[3, 2] + m1[3, 2] * m2[3, 3];
226
result[0, 3] := m1[0, 3] * m2[0, 0] + m1[1, 3] * m2[0, 1] + m1[2, 3] * m2[0, 2] + m1[3, 3] * m2[0, 3];
227
result[1, 3] := m1[0, 3] * m2[1, 0] + m1[1, 3] * m2[1, 1] + m1[2, 3] * m2[1, 2] + m1[3, 3] * m2[1, 3];
228
result[2, 3] := m1[0, 3] * m2[2, 0] + m1[1, 3] * m2[2, 1] + m1[2, 3] * m2[2, 2] + m1[3, 3] * m2[2, 3];
229
result[3, 3] := m1[0, 3] * m2[3, 0] + m1[1, 3] * m2[3, 1] + m1[2, 3] * m2[3, 2] + m1[3, 3] * m2[3, 3];
233
{*****************************************************************************
234
Matrix/scalar operations
235
*****************************************************************************}
237
operator * (const m:objectname;const x:datatype) : objectname;
239
{Multiplies the elements of a matrix.}
242
result[0,0]:=m[0,0]*x;
243
result[0,1]:=m[0,1]*x;
245
result[0,2]:=m[0,2]*x;
248
result[0,3]:=m[0,3]*x;
250
result[1,0]:=m[1,0]*x;
251
result[1,1]:=m[1,1]*x;
253
result[1,2]:=m[1,2]*x;
256
result[1,3]:=m[1,3]*x;
259
result[2,0]:=m[2,0]*x;
260
result[2,1]:=m[2,1]*x;
261
result[2,2]:=m[2,2]*x;
264
result[2,3]:=m[2,3]*x;
265
result[3,0]:=m[3,0]*x;
266
result[3,1]:=m[3,1]*x;
267
result[3,2]:=m[3,2]*x;
268
result[3,3]:=m[3,3]*x;
272
operator / (const m:objectname;const x:datatype) : objectname;
274
{Divides the elements of a matrix.
276
In most cases, you will want to avoid this and multiply by the inverse.
277
In case you need to preserve accuracy, dividing might be better though.}
280
result[0,0]:=m[0,0]/x;
281
result[0,1]:=m[0,1]/x;
283
result[0,2]:=m[0,2]/x;
286
result[0,3]:=m[0,3]/x;
288
result[1,0]:=m[1,0]/x;
289
result[1,1]:=m[1,1]/x;
291
result[1,2]:=m[1,2]/x;
294
result[1,3]:=m[1,3]/x;
297
result[2,0]:=m[2,0]/x;
298
result[2,1]:=m[2,1]/x;
299
result[2,2]:=m[2,2]/x;
302
result[2,3]:=m[2,3]/x;
303
result[3,0]:=m[3,0]/x;
304
result[3,1]:=m[3,1]/x;
305
result[3,2]:=m[3,2]/x;
306
result[3,3]:=m[3,3]/x;