tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/******************************************************************************
 
 @File         PVRTVector.cpp
 
 @Title        PVRTVector
 
 @Version      
 
 @Copyright    Copyright (c) Imagination Technologies Limited.
 
 @Platform     ANSI compatible
 
 @Description  Vector and matrix mathematics library
 
******************************************************************************/
 
#include "PVRTVector.h"
 
#include <math.h>
 
/*!***************************************************************************
** PVRTVec2 2 component vector
****************************************************************************/
 
/*!***************************************************************************
 @Function            PVRTVec2
 @Input                v3Vec a Vec3
 @Description        Constructor from a Vec3
 *****************************************************************************/
   PVRTVec2::PVRTVec2(const PVRTVec3& vec3)
   {
       x = vec3.x; y = vec3.y;
   }
 
/*!***************************************************************************
** PVRTVec3 3 component vector
****************************************************************************/
 
/*!***************************************************************************
 @Function            PVRTVec3
 @Input                v4Vec a PVRTVec4
 @Description        Constructor from a PVRTVec4
*****************************************************************************/
   PVRTVec3::PVRTVec3(const PVRTVec4& vec4)
   {
       x = vec4.x; y = vec4.y; z = vec4.z;
   }
 
/*!***************************************************************************
 @Function            *
 @Input                rhs a PVRTMat3
 @Returns            result of multiplication
 @Description        matrix multiplication operator PVRTVec3 and PVRTMat3
****************************************************************************/
   PVRTVec3 PVRTVec3::operator*(const PVRTMat3& rhs) const
   {
       PVRTVec3 out;
 
       out.x = VERTTYPEMUL(x,rhs.f[0])+VERTTYPEMUL(y,rhs.f[1])+VERTTYPEMUL(z,rhs.f[2]);
       out.y = VERTTYPEMUL(x,rhs.f[3])+VERTTYPEMUL(y,rhs.f[4])+VERTTYPEMUL(z,rhs.f[5]);
       out.z = VERTTYPEMUL(x,rhs.f[6])+VERTTYPEMUL(y,rhs.f[7])+VERTTYPEMUL(z,rhs.f[8]);
 
       return out;
   }
 
/*!***************************************************************************
 @Function            *=
 @Input                rhs a PVRTMat3
 @Returns            result of multiplication and assignment
 @Description        matrix multiplication and assignment operator for PVRTVec3 and PVRTMat3
****************************************************************************/
   PVRTVec3& PVRTVec3::operator*=(const PVRTMat3& rhs)
   {
       VERTTYPE tx = VERTTYPEMUL(x,rhs.f[0])+VERTTYPEMUL(y,rhs.f[1])+VERTTYPEMUL(z,rhs.f[2]);
       VERTTYPE ty = VERTTYPEMUL(x,rhs.f[3])+VERTTYPEMUL(y,rhs.f[4])+VERTTYPEMUL(z,rhs.f[5]);
       z = VERTTYPEMUL(x,rhs.f[6])+VERTTYPEMUL(y,rhs.f[7])+VERTTYPEMUL(z,rhs.f[8]);
       x = tx;
       y = ty;
 
       return *this;
   }
 
/*!***************************************************************************
** PVRTVec4 4 component vector
****************************************************************************/
 
/*!***************************************************************************
 @Function            *
 @Input                rhs a PVRTMat4
 @Returns            result of multiplication
 @Description        matrix multiplication operator PVRTVec4 and PVRTMat4
****************************************************************************/
   PVRTVec4 PVRTVec4::operator*(const PVRTMat4& rhs) const
   {
       PVRTVec4 out;
       out.x = VERTTYPEMUL(x,rhs.f[0])+VERTTYPEMUL(y,rhs.f[1])+VERTTYPEMUL(z,rhs.f[2])+VERTTYPEMUL(w,rhs.f[3]);
       out.y = VERTTYPEMUL(x,rhs.f[4])+VERTTYPEMUL(y,rhs.f[5])+VERTTYPEMUL(z,rhs.f[6])+VERTTYPEMUL(w,rhs.f[7]);
       out.z = VERTTYPEMUL(x,rhs.f[8])+VERTTYPEMUL(y,rhs.f[9])+VERTTYPEMUL(z,rhs.f[10])+VERTTYPEMUL(w,rhs.f[11]);
       out.w = VERTTYPEMUL(x,rhs.f[12])+VERTTYPEMUL(y,rhs.f[13])+VERTTYPEMUL(z,rhs.f[14])+VERTTYPEMUL(w,rhs.f[15]);
       return out;
   }
 
/*!***************************************************************************
 @Function            *=
 @Input                rhs a PVRTMat4
 @Returns            result of multiplication and assignment
 @Description        matrix multiplication and assignment operator for PVRTVec4 and PVRTMat4
****************************************************************************/
   PVRTVec4& PVRTVec4::operator*=(const PVRTMat4& rhs)
   {
       VERTTYPE tx = VERTTYPEMUL(x,rhs.f[0])+VERTTYPEMUL(y,rhs.f[1])+VERTTYPEMUL(z,rhs.f[2])+VERTTYPEMUL(w,rhs.f[3]);
       VERTTYPE ty = VERTTYPEMUL(x,rhs.f[4])+VERTTYPEMUL(y,rhs.f[5])+VERTTYPEMUL(z,rhs.f[6])+VERTTYPEMUL(w,rhs.f[7]);
       VERTTYPE tz = VERTTYPEMUL(x,rhs.f[8])+VERTTYPEMUL(y,rhs.f[9])+VERTTYPEMUL(z,rhs.f[10])+VERTTYPEMUL(w,rhs.f[11]);
       w = VERTTYPEMUL(x,rhs.f[12])+VERTTYPEMUL(y,rhs.f[13])+VERTTYPEMUL(z,rhs.f[14])+VERTTYPEMUL(w,rhs.f[15]);
       x = tx;
       y = ty;
       z = tz;
       return *this;
   }
 
/*!***************************************************************************
** PVRTMat3 3x3 matrix
****************************************************************************/
/*!***************************************************************************
 @Function            PVRTMat3
 @Input                mat a PVRTMat4
 @Description        constructor to form a PVRTMat3 from a PVRTMat4
****************************************************************************/
   PVRTMat3::PVRTMat3(const PVRTMat4& mat)
   {
       VERTTYPE *dest = (VERTTYPE*)f, *src = (VERTTYPE*)mat.f;
       for(int i=0;i<3;i++)
       {
           for(int j=0;j<3;j++)
           {
               (*dest++) = (*src++);
           }
           src++;
       }
   }
 
/*!***************************************************************************
 @Function            RotationX
 @Input                angle the angle of rotation
 @Returns            rotation matrix
 @Description        generates a 3x3 rotation matrix about the X axis
****************************************************************************/
   PVRTMat3 PVRTMat3::RotationX(VERTTYPE angle)
   {
       PVRTMat4 out;
       PVRTMatrixRotationX(out,angle);
       return PVRTMat3(out);
   }
/*!***************************************************************************
 @Function            RotationY
 @Input                angle the angle of rotation
 @Returns            rotation matrix
 @Description        generates a 3x3 rotation matrix about the Y axis
****************************************************************************/
   PVRTMat3 PVRTMat3::RotationY(VERTTYPE angle)
   {
       PVRTMat4 out;
       PVRTMatrixRotationY(out,angle);
       return PVRTMat3(out);
   }
/*!***************************************************************************
 @Function            RotationZ
 @Input                angle the angle of rotation
 @Returns            rotation matrix
 @Description        generates a 3x3 rotation matrix about the Z axis
****************************************************************************/
   PVRTMat3 PVRTMat3::RotationZ(VERTTYPE angle)
   {
       PVRTMat4 out;
       PVRTMatrixRotationZ(out,angle);
       return PVRTMat3(out);
   }
 
 
/*!***************************************************************************
** PVRTMat4 4x4 matrix
****************************************************************************/
/*!***************************************************************************
 @Function            RotationX
 @Input                angle the angle of rotation
 @Returns            rotation matrix
 @Description        generates a 4x4 rotation matrix about the X axis
****************************************************************************/
   PVRTMat4 PVRTMat4::RotationX(VERTTYPE angle)
   {
       PVRTMat4 out;
       PVRTMatrixRotationX(out,angle);
       return out;
   }
/*!***************************************************************************
 @Function            RotationY
 @Input                angle the angle of rotation
 @Returns            rotation matrix
 @Description        generates a 4x4 rotation matrix about the Y axis
****************************************************************************/
   PVRTMat4 PVRTMat4::RotationY(VERTTYPE angle)
   {
       PVRTMat4 out;
       PVRTMatrixRotationY(out,angle);
       return out;
   }
/*!***************************************************************************
 @Function            RotationZ
 @Input                angle the angle of rotation
 @Returns            rotation matrix
 @Description        generates a 4x4 rotation matrix about the Z axis
****************************************************************************/
   PVRTMat4 PVRTMat4::RotationZ(VERTTYPE angle)
   {
       PVRTMat4 out;
       PVRTMatrixRotationZ(out,angle);
       return out;
   }
 
/*!***************************************************************************
 @Function            *
 @Input                rhs another PVRTMat4
 @Returns            result of multiplication
 @Description        Matrix multiplication of two 4x4 matrices.
*****************************************************************************/
   PVRTMat4 PVRTMat4::operator*(const PVRTMat4& rhs) const
   {
       PVRTMat4 out;
       // col 1
       out.f[0] =    VERTTYPEMUL(f[0],rhs.f[0])+VERTTYPEMUL(f[4],rhs.f[1])+VERTTYPEMUL(f[8],rhs.f[2])+VERTTYPEMUL(f[12],rhs.f[3]);
       out.f[1] =    VERTTYPEMUL(f[1],rhs.f[0])+VERTTYPEMUL(f[5],rhs.f[1])+VERTTYPEMUL(f[9],rhs.f[2])+VERTTYPEMUL(f[13],rhs.f[3]);
       out.f[2] =    VERTTYPEMUL(f[2],rhs.f[0])+VERTTYPEMUL(f[6],rhs.f[1])+VERTTYPEMUL(f[10],rhs.f[2])+VERTTYPEMUL(f[14],rhs.f[3]);
       out.f[3] =    VERTTYPEMUL(f[3],rhs.f[0])+VERTTYPEMUL(f[7],rhs.f[1])+VERTTYPEMUL(f[11],rhs.f[2])+VERTTYPEMUL(f[15],rhs.f[3]);
 
       // col 2
       out.f[4] =    VERTTYPEMUL(f[0],rhs.f[4])+VERTTYPEMUL(f[4],rhs.f[5])+VERTTYPEMUL(f[8],rhs.f[6])+VERTTYPEMUL(f[12],rhs.f[7]);
       out.f[5] =    VERTTYPEMUL(f[1],rhs.f[4])+VERTTYPEMUL(f[5],rhs.f[5])+VERTTYPEMUL(f[9],rhs.f[6])+VERTTYPEMUL(f[13],rhs.f[7]);
       out.f[6] =    VERTTYPEMUL(f[2],rhs.f[4])+VERTTYPEMUL(f[6],rhs.f[5])+VERTTYPEMUL(f[10],rhs.f[6])+VERTTYPEMUL(f[14],rhs.f[7]);
       out.f[7] =    VERTTYPEMUL(f[3],rhs.f[4])+VERTTYPEMUL(f[7],rhs.f[5])+VERTTYPEMUL(f[11],rhs.f[6])+VERTTYPEMUL(f[15],rhs.f[7]);
 
       // col3
       out.f[8] =    VERTTYPEMUL(f[0],rhs.f[8])+VERTTYPEMUL(f[4],rhs.f[9])+VERTTYPEMUL(f[8],rhs.f[10])+VERTTYPEMUL(f[12],rhs.f[11]);
       out.f[9] =    VERTTYPEMUL(f[1],rhs.f[8])+VERTTYPEMUL(f[5],rhs.f[9])+VERTTYPEMUL(f[9],rhs.f[10])+VERTTYPEMUL(f[13],rhs.f[11]);
       out.f[10] =    VERTTYPEMUL(f[2],rhs.f[8])+VERTTYPEMUL(f[6],rhs.f[9])+VERTTYPEMUL(f[10],rhs.f[10])+VERTTYPEMUL(f[14],rhs.f[11]);
       out.f[11] =    VERTTYPEMUL(f[3],rhs.f[8])+VERTTYPEMUL(f[7],rhs.f[9])+VERTTYPEMUL(f[11],rhs.f[10])+VERTTYPEMUL(f[15],rhs.f[11]);
 
       // col3
       out.f[12] =    VERTTYPEMUL(f[0],rhs.f[12])+VERTTYPEMUL(f[4],rhs.f[13])+VERTTYPEMUL(f[8],rhs.f[14])+VERTTYPEMUL(f[12],rhs.f[15]);
       out.f[13] =    VERTTYPEMUL(f[1],rhs.f[12])+VERTTYPEMUL(f[5],rhs.f[13])+VERTTYPEMUL(f[9],rhs.f[14])+VERTTYPEMUL(f[13],rhs.f[15]);
       out.f[14] =    VERTTYPEMUL(f[2],rhs.f[12])+VERTTYPEMUL(f[6],rhs.f[13])+VERTTYPEMUL(f[10],rhs.f[14])+VERTTYPEMUL(f[14],rhs.f[15]);
       out.f[15] =    VERTTYPEMUL(f[3],rhs.f[12])+VERTTYPEMUL(f[7],rhs.f[13])+VERTTYPEMUL(f[11],rhs.f[14])+VERTTYPEMUL(f[15],rhs.f[15]);
       return out;
   }
 
 
/*!***************************************************************************
 @Function            inverse
 @Returns            inverse mat4
 @Description        Calculates multiplicative inverse of this matrix
                   The matrix must be of the form :
                   A 0
                   C 1
                   Where A is a 3x3 matrix and C is a 1x3 matrix.
*****************************************************************************/
   PVRTMat4 PVRTMat4::inverse() const
   {
       PVRTMat4 out;
       VERTTYPE    det_1;
       VERTTYPE    pos, neg, temp;
 
       /* Calculate the determinant of submatrix A and determine if the
          the matrix is singular as limited by the double precision
          floating-point data representation. */
       pos = neg = f2vt(0.0);
       temp =  VERTTYPEMUL(VERTTYPEMUL(f[ 0], f[ 5]), f[10]);
       if (temp >= 0) pos += temp; else neg += temp;
       temp =  VERTTYPEMUL(VERTTYPEMUL(f[ 4], f[ 9]), f[ 2]);
       if (temp >= 0) pos += temp; else neg += temp;
       temp =  VERTTYPEMUL(VERTTYPEMUL(f[ 8], f[ 1]), f[ 6]);
       if (temp >= 0) pos += temp; else neg += temp;
       temp =  VERTTYPEMUL(VERTTYPEMUL(-f[ 8], f[ 5]), f[ 2]);
       if (temp >= 0) pos += temp; else neg += temp;
       temp =  VERTTYPEMUL(VERTTYPEMUL(-f[ 4], f[ 1]), f[10]);
       if (temp >= 0) pos += temp; else neg += temp;
       temp =  VERTTYPEMUL(VERTTYPEMUL(-f[ 0], f[ 9]), f[ 6]);
       if (temp >= 0) pos += temp; else neg += temp;
       det_1 = pos + neg;
 
       /* Is the submatrix A singular? */
       if (det_1 == f2vt(0.0)) //|| (VERTTYPEABS(det_1 / (pos - neg)) < 1.0e-15)
       {
           /* Matrix M has no inverse */
           _RPT0(_CRT_WARN, "Matrix has no inverse : singular matrix\n");
       }
       else
       {
           /* Calculate inverse(A) = adj(A) / det(A) */
           //det_1 = 1.0 / det_1;
           det_1 = VERTTYPEDIV(f2vt(1.0f), det_1);
           out.f[ 0] =   VERTTYPEMUL(( VERTTYPEMUL(f[ 5], f[10]) - VERTTYPEMUL(f[ 9], f[ 6]) ), det_1);
           out.f[ 1] = - VERTTYPEMUL(( VERTTYPEMUL(f[ 1], f[10]) - VERTTYPEMUL(f[ 9], f[ 2]) ), det_1);
           out.f[ 2] =   VERTTYPEMUL(( VERTTYPEMUL(f[ 1], f[ 6]) - VERTTYPEMUL(f[ 5], f[ 2]) ), det_1);
           out.f[ 4] = - VERTTYPEMUL(( VERTTYPEMUL(f[ 4], f[10]) - VERTTYPEMUL(f[ 8], f[ 6]) ), det_1);
           out.f[ 5] =   VERTTYPEMUL(( VERTTYPEMUL(f[ 0], f[10]) - VERTTYPEMUL(f[ 8], f[ 2]) ), det_1);
           out.f[ 6] = - VERTTYPEMUL(( VERTTYPEMUL(f[ 0], f[ 6]) - VERTTYPEMUL(f[ 4], f[ 2]) ), det_1);
           out.f[ 8] =   VERTTYPEMUL(( VERTTYPEMUL(f[ 4], f[ 9]) - VERTTYPEMUL(f[ 8], f[ 5]) ), det_1);
           out.f[ 9] = - VERTTYPEMUL(( VERTTYPEMUL(f[ 0], f[ 9]) - VERTTYPEMUL(f[ 8], f[ 1]) ), det_1);
           out.f[10] =   VERTTYPEMUL(( VERTTYPEMUL(f[ 0], f[ 5]) - VERTTYPEMUL(f[ 4], f[ 1]) ), det_1);
 
           /* Calculate -C * inverse(A) */
           out.f[12] = - ( VERTTYPEMUL(f[12], out.f[ 0]) + VERTTYPEMUL(f[13], out.f[ 4]) + VERTTYPEMUL(f[14], out.f[ 8]) );
           out.f[13] = - ( VERTTYPEMUL(f[12], out.f[ 1]) + VERTTYPEMUL(f[13], out.f[ 5]) + VERTTYPEMUL(f[14], out.f[ 9]) );
           out.f[14] = - ( VERTTYPEMUL(f[12], out.f[ 2]) + VERTTYPEMUL(f[13], out.f[ 6]) + VERTTYPEMUL(f[14], out.f[10]) );
 
           /* Fill in last row */
           out.f[ 3] = f2vt(0.0f);
           out.f[ 7] = f2vt(0.0f);
           out.f[11] = f2vt(0.0f);
           out.f[15] = f2vt(1.0f);
       }
 
       return out;
   }
 
/*!***************************************************************************
 @Function            PVRTLinearEqSolve
 @Input                pSrc    2D array of floats. 4 Eq linear problem is 5x4
                           matrix, constants in first column
 @Input                nCnt    Number of equations to solve
 @Output            pRes    Result
 @Description        Solves 'nCnt' simultaneous equations of 'nCnt' variables.
                   pRes should be an array large enough to contain the
                   results: the values of the 'nCnt' variables.
                   This fn recursively uses Gaussian Elimination.
*****************************************************************************/
void PVRTLinearEqSolve(VERTTYPE * const pRes, VERTTYPE ** const pSrc, const int nCnt)
{
   int            i, j, k;
   VERTTYPE    f;
 
   if (nCnt == 1)
   {
       _ASSERT(pSrc[0][1] != 0);
       pRes[0] = VERTTYPEDIV(pSrc[0][0], pSrc[0][1]);
       return;
   }
 
   // Loop backwards in an attempt avoid the need to swap rows
   i = nCnt;
   while(i)
   {
       --i;
 
       if(pSrc[i][nCnt] != f2vt(0.0f))
       {
           // Row i can be used to zero the other rows; let's move it to the bottom
           if(i != (nCnt-1))
           {
               for(j = 0; j <= nCnt; ++j)
               {
                   // Swap the two values
                   f = pSrc[nCnt-1][j];
                   pSrc[nCnt-1][j] = pSrc[i][j];
                   pSrc[i][j] = f;
               }
           }
 
           // Now zero the last columns of the top rows
           for(j = 0; j < (nCnt-1); ++j)
           {
               _ASSERT(pSrc[nCnt-1][nCnt] != f2vt(0.0f));
               f = VERTTYPEDIV(pSrc[j][nCnt], pSrc[nCnt-1][nCnt]);
 
               // No need to actually calculate a zero for the final column
               for(k = 0; k < nCnt; ++k)
               {
                   pSrc[j][k] -= VERTTYPEMUL(f, pSrc[nCnt-1][k]);
               }
           }
 
           break;
       }
   }
 
   // Solve the top-left sub matrix
   PVRTLinearEqSolve(pRes, pSrc, nCnt - 1);
 
   // Now calc the solution for the bottom row
   f = pSrc[nCnt-1][0];
   for(k = 1; k < nCnt; ++k)
   {
       f -= VERTTYPEMUL(pSrc[nCnt-1][k], pRes[k-1]);
   }
   _ASSERT(pSrc[nCnt-1][nCnt] != f2vt(0));
   f = VERTTYPEDIV(f, pSrc[nCnt-1][nCnt]);
   pRes[nCnt-1] = f;
}
 
/*****************************************************************************
End of file (PVRTVector.cpp)
*****************************************************************************/