~ubuntu-branches/ubuntu/utopic/castle-game-engine/utopic

« back to all changes in this revision

Viewing changes to src/base/castlevectors_operators_matrix_implementation.inc

  • Committer: Package Import Robot
  • Author(s): Abou Al Montacir
  • Date: 2013-04-27 18:06:40 UTC
  • Revision ID: package-import@ubuntu.com-20130427180640-eink4nmwzuivez1c
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{ Implement matrix operators.
 
2
  Copied and adjusted from FPC's rtl/inc/mmatimp.inc }
 
3
 
 
4
{*****************************************************************************
 
5
                           Matrix to matrix operations
 
6
*****************************************************************************}
 
7
 
 
8
operator + (const m1,m2:objectname) : objectname;
 
9
 
 
10
{Add the elements of a matrix to each other.}
 
11
 
 
12
begin
 
13
    result[0,0]:=m1[0,0]+m2[0,0];
 
14
    result[0,1]:=m1[0,1]+m2[0,1];
 
15
{$if matsize>=3}
 
16
    result[0,2]:=m1[0,2]+m2[0,2];
 
17
{$endif}
 
18
{$if matsize>=4}
 
19
    result[0,3]:=m1[0,3]+m2[0,3];
 
20
{$endif}
 
21
    result[1,0]:=m1[1,0]+m2[1,0];
 
22
    result[1,1]:=m1[1,1]+m2[1,1];
 
23
{$if matsize>=3}
 
24
    result[1,2]:=m1[1,2]+m2[1,2];
 
25
{$endif}
 
26
{$if matsize>=4}
 
27
    result[1,3]:=m1[1,3]+m2[1,3];
 
28
{$endif}
 
29
{$if matsize>=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];
 
33
{$endif}
 
34
{$if matsize>=4}
 
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];
 
40
{$endif}
 
41
end;
 
42
 
 
43
operator - (const m1,m2:objectname) : objectname;
 
44
 
 
45
{Subtract the elements of two matrixes from each other.}
 
46
 
 
47
begin
 
48
    result[0,0]:=m1[0,0]-m2[0,0];
 
49
    result[0,1]:=m1[0,1]-m2[0,1];
 
50
{$if matsize>=3}
 
51
    result[0,2]:=m1[0,2]-m2[0,2];
 
52
{$endif}
 
53
{$if matsize>=4}
 
54
    result[0,3]:=m1[0,3]-m2[0,3];
 
55
{$endif}
 
56
    result[1,0]:=m1[1,0]-m2[1,0];
 
57
    result[1,1]:=m1[1,1]-m2[1,1];
 
58
{$if matsize>=3}
 
59
    result[1,2]:=m1[1,2]-m2[1,2];
 
60
{$endif}
 
61
{$if matsize>=4}
 
62
    result[1,3]:=m1[1,3]-m2[1,3];
 
63
{$endif}
 
64
{$if matsize>=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];
 
68
{$endif}
 
69
{$if matsize>=4}
 
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];
 
75
{$endif}
 
76
end;
 
77
 
 
78
operator - (const m1:objectname) : objectname;
 
79
 
 
80
{Negate the elements of a matrix.}
 
81
 
 
82
begin
 
83
    result[0,0]:=-m1[0,0];
 
84
    result[0,1]:=-m1[0,1];
 
85
{$if matsize>=3}
 
86
    result[0,2]:=-m1[0,2];
 
87
{$endif}
 
88
{$if matsize>=4}
 
89
    result[0,3]:=-m1[0,3];
 
90
{$endif}
 
91
    result[1,0]:=-m1[1,0];
 
92
    result[1,1]:=-m1[1,1];
 
93
{$if matsize>=3}
 
94
    result[1,2]:=-m1[1,2];
 
95
{$endif}
 
96
{$if matsize>=4}
 
97
    result[1,3]:=-m1[1,3];
 
98
{$endif}
 
99
{$if matsize>=3}
 
100
    result[2,0]:=-m1[2,0];
 
101
    result[2,1]:=-m1[2,1];
 
102
    result[2,2]:=-m1[2,2];
 
103
{$endif}
 
104
{$if matsize>=4}
 
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];
 
110
{$endif}
 
111
end;
 
112
 
 
113
(* TODO: Fix, FPC matrix was ordered by rows, we should make transpose inside.
 
114
 
 
115
Doing transpose here would be a waste of time...
 
116
  function MatrixTranspose(const M: TMatrix4Single): TMatrix4Single;
 
117
  begin
 
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];
 
122
 
 
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];
 
127
 
 
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];
 
132
 
 
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];
 
137
  end;
 
138
 
 
139
 
 
140
operator * (const m1,m2:objectname) : objectname;
 
141
 
 
142
{Multiply two matrixes.}
 
143
 
 
144
var r:array[0..matsize-1] of datatype;
 
145
    i:byte;
 
146
 
 
147
begin
 
148
    for i:=0 to matsize-1 do
 
149
        begin
 
150
            r:=m1[i];
 
151
            result[i,0]:=r[0]*m2[0,0]
 
152
                             +r[1]*m2[1,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]
 
156
                             +r[1]*m2[1,1]
 
157
             {$if matsize>=3}+r[2]*m2[2,1]{$endif}
 
158
             {$if matsize>=4}+r[3]*m2[3,1]{$endif};
 
159
        {$if matsize>=3}
 
160
            result[i,2]:=r[0]*m2[0,2]
 
161
                             +r[1]*m2[1,2]
 
162
                             +r[2]*m2[2,2]
 
163
             {$if matsize>=4}+r[3]*m2[3,2]{$endif};
 
164
        {$endif}
 
165
        {$if matsize>=4}
 
166
            result[i,3]:=r[0]*m2[0,3]
 
167
                             +r[1]*m2[1,3]
 
168
                             +r[2]*m2[2,3]
 
169
                             +r[3]*m2[3,3];
 
170
        {$endif}
 
171
        end;
 
172
end;
 
173
*)
 
174
 
 
175
{*****************************************************************************
 
176
                           Vector/matrix operations
 
177
*****************************************************************************}
 
178
 
 
179
(* TODO: Fix, FPC matrix was ordered by rows, we should make transpose inside.
 
180
 
 
181
operator * (const m:objectname;const v:vectorcompanion) : vectorcompanion;
 
182
 
 
183
{Multiplies a matrix with a vector.}
 
184
 
 
185
begin
 
186
    result[0]:=m[0,0]*v[0]
 
187
                   +m[0,1]*v[1]
 
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]
 
191
                   +m[1,1]*v[1]
 
192
   {$if matsize>=3}+m[1,2]*v[2]{$endif}
 
193
   {$if matsize>=4}+m[1,3]*v[3]{$endif};
 
194
{$if matsize>=3}
 
195
    result[2]:=m[2,0]*v[0]
 
196
                   +m[2,1]*v[1]
 
197
                   +m[2,2]*v[2]
 
198
   {$if matsize>=4}+m[2,3]*v[3]{$endif};
 
199
{$endif}
 
200
{$if matsize>=4}
 
201
    result[3]:=m[3,0]*v[0]
 
202
                   +m[3,1]*v[1]
 
203
                   +m[3,2]*v[2]
 
204
                   +m[3,3]*v[3];
 
205
{$endif}
 
206
end;
 
207
 
 
208
*)
 
209
 
 
210
{$if matsize>=4}
 
211
{ For now, we just define 4x4 multiplications. Copied from MatrixMult. }
 
212
operator * (const m1,m2:objectname) : objectname;
 
213
begin
 
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];
 
230
end;
 
231
{$endif}
 
232
 
 
233
{*****************************************************************************
 
234
                           Matrix/scalar operations
 
235
*****************************************************************************}
 
236
 
 
237
operator * (const m:objectname;const x:datatype) : objectname;
 
238
 
 
239
{Multiplies the elements of a matrix.}
 
240
 
 
241
begin
 
242
  result[0,0]:=m[0,0]*x;
 
243
  result[0,1]:=m[0,1]*x;
 
244
{$if matsize>=3}
 
245
  result[0,2]:=m[0,2]*x;
 
246
{$endif}
 
247
{$if matsize>=4}
 
248
  result[0,3]:=m[0,3]*x;
 
249
{$endif}
 
250
  result[1,0]:=m[1,0]*x;
 
251
  result[1,1]:=m[1,1]*x;
 
252
{$if matsize>=3}
 
253
  result[1,2]:=m[1,2]*x;
 
254
{$endif}
 
255
{$if matsize>=4}
 
256
  result[1,3]:=m[1,3]*x;
 
257
{$endif}
 
258
{$if matsize>=3}
 
259
  result[2,0]:=m[2,0]*x;
 
260
  result[2,1]:=m[2,1]*x;
 
261
  result[2,2]:=m[2,2]*x;
 
262
{$endif}
 
263
{$if matsize>=4}
 
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;
 
269
{$endif}
 
270
end;
 
271
 
 
272
operator / (const m:objectname;const x:datatype) : objectname;
 
273
 
 
274
{Divides the elements of a matrix.
 
275
 
 
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.}
 
278
 
 
279
begin
 
280
  result[0,0]:=m[0,0]/x;
 
281
  result[0,1]:=m[0,1]/x;
 
282
{$if matsize>=3}
 
283
  result[0,2]:=m[0,2]/x;
 
284
{$endif}
 
285
{$if matsize>=4}
 
286
  result[0,3]:=m[0,3]/x;
 
287
{$endif}
 
288
  result[1,0]:=m[1,0]/x;
 
289
  result[1,1]:=m[1,1]/x;
 
290
{$if matsize>=3}
 
291
  result[1,2]:=m[1,2]/x;
 
292
{$endif}
 
293
{$if matsize>=4}
 
294
  result[1,3]:=m[1,3]/x;
 
295
{$endif}
 
296
{$if matsize>=3}
 
297
  result[2,0]:=m[2,0]/x;
 
298
  result[2,1]:=m[2,1]/x;
 
299
  result[2,2]:=m[2,2]/x;
 
300
{$endif}
 
301
{$if matsize>=4}
 
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;
 
307
{$endif}
 
308
end;
 
309