~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Source/OpenEXR/Imath/ImathMatrix.h

  • Committer: Stefano Rivera
  • Date: 2010-07-24 15:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: stefanor@ubuntu.com-20100724153551-6s3fth1653huk31a
Tags: upstream-3.13.1
ImportĀ upstreamĀ versionĀ 3.31.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///////////////////////////////////////////////////////////////////////////
2
 
//
3
 
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4
 
// Digital Ltd. LLC
5
 
// 
6
 
// All rights reserved.
7
 
// 
8
 
// Redistribution and use in source and binary forms, with or without
9
 
// modification, are permitted provided that the following conditions are
10
 
// met:
11
 
// *       Redistributions of source code must retain the above copyright
12
 
// notice, this list of conditions and the following disclaimer.
13
 
// *       Redistributions in binary form must reproduce the above
14
 
// copyright notice, this list of conditions and the following disclaimer
15
 
// in the documentation and/or other materials provided with the
16
 
// distribution.
17
 
// *       Neither the name of Industrial Light & Magic nor the names of
18
 
// its contributors may be used to endorse or promote products derived
19
 
// from this software without specific prior written permission. 
20
 
// 
21
 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 
//
33
 
///////////////////////////////////////////////////////////////////////////
34
 
 
35
 
 
36
 
 
37
 
#ifndef INCLUDED_IMATHMATRIX_H
38
 
#define INCLUDED_IMATHMATRIX_H
39
 
 
40
 
//----------------------------------------------------------------
41
 
//
42
 
//      2D (3x3) and 3D (4x4) transformation matrix templates.
43
 
//
44
 
//----------------------------------------------------------------
45
 
 
46
 
#include "ImathPlatform.h"
47
 
#include "ImathFun.h"
48
 
#include "ImathExc.h"
49
 
#include "ImathVec.h"
50
 
#include "ImathShear.h"
51
 
 
52
 
#include <iostream>
53
 
#include <iomanip>
54
 
 
55
 
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
56
 
// suppress exception specification warnings
57
 
#pragma warning(disable:4290)
58
 
#endif
59
 
 
60
 
 
61
 
namespace Imath {
62
 
 
63
 
 
64
 
template <class T> class Matrix33
65
 
{
66
 
  public:
67
 
 
68
 
    //-------------------
69
 
    // Access to elements
70
 
    //-------------------
71
 
 
72
 
    T           x[3][3];
73
 
 
74
 
    T *         operator [] (int i);
75
 
    const T *   operator [] (int i) const;
76
 
 
77
 
 
78
 
    //-------------
79
 
    // Constructors
80
 
    //-------------
81
 
 
82
 
    Matrix33 ();
83
 
                                // 1 0 0
84
 
                                // 0 1 0
85
 
                                // 0 0 1
86
 
 
87
 
    Matrix33 (T a);
88
 
                                // a a a
89
 
                                // a a a
90
 
                                // a a a
91
 
 
92
 
    Matrix33 (const T a[3][3]);
93
 
                                // a[0][0] a[0][1] a[0][2]
94
 
                                // a[1][0] a[1][1] a[1][2]
95
 
                                // a[2][0] a[2][1] a[2][2]
96
 
 
97
 
    Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i);
98
 
 
99
 
                                // a b c
100
 
                                // d e f
101
 
                                // g h i
102
 
 
103
 
 
104
 
    //--------------------------------
105
 
    // Copy constructor and assignment
106
 
    //--------------------------------
107
 
 
108
 
    Matrix33 (const Matrix33 &v);
109
 
 
110
 
    const Matrix33 &    operator = (const Matrix33 &v);
111
 
    const Matrix33 &    operator = (T a);
112
 
 
113
 
 
114
 
    //----------------------
115
 
    // Compatibility with Sb
116
 
    //----------------------
117
 
    
118
 
    T *                 getValue ();
119
 
    const T *           getValue () const;
120
 
 
121
 
    template <class S>
122
 
    void                getValue (Matrix33<S> &v) const;
123
 
    template <class S>
124
 
    Matrix33 &          setValue (const Matrix33<S> &v);
125
 
 
126
 
    template <class S>
127
 
    Matrix33 &          setTheMatrix (const Matrix33<S> &v);
128
 
 
129
 
 
130
 
    //---------
131
 
    // Identity
132
 
    //---------
133
 
 
134
 
    void                makeIdentity();
135
 
 
136
 
 
137
 
    //---------
138
 
    // Equality
139
 
    //---------
140
 
 
141
 
    bool                operator == (const Matrix33 &v) const;
142
 
    bool                operator != (const Matrix33 &v) const;
143
 
 
144
 
    //-----------------------------------------------------------------------
145
 
    // Compare two matrices and test if they are "approximately equal":
146
 
    //
147
 
    // equalWithAbsError (m, e)
148
 
    //
149
 
    //      Returns true if the coefficients of this and m are the same with
150
 
    //      an absolute error of no more than e, i.e., for all i, j
151
 
    //
152
 
    //      abs (this[i][j] - m[i][j]) <= e
153
 
    //
154
 
    // equalWithRelError (m, e)
155
 
    //
156
 
    //      Returns true if the coefficients of this and m are the same with
157
 
    //      a relative error of no more than e, i.e., for all i, j
158
 
    //
159
 
    //      abs (this[i] - v[i][j]) <= e * abs (this[i][j])
160
 
    //-----------------------------------------------------------------------
161
 
 
162
 
    bool                equalWithAbsError (const Matrix33<T> &v, T e) const;
163
 
    bool                equalWithRelError (const Matrix33<T> &v, T e) const;
164
 
 
165
 
 
166
 
    //------------------------
167
 
    // Component-wise addition
168
 
    //------------------------
169
 
 
170
 
    const Matrix33 &    operator += (const Matrix33 &v);
171
 
    const Matrix33 &    operator += (T a);
172
 
    Matrix33            operator + (const Matrix33 &v) const;
173
 
 
174
 
 
175
 
    //---------------------------
176
 
    // Component-wise subtraction
177
 
    //---------------------------
178
 
 
179
 
    const Matrix33 &    operator -= (const Matrix33 &v);
180
 
    const Matrix33 &    operator -= (T a);
181
 
    Matrix33            operator - (const Matrix33 &v) const;
182
 
 
183
 
 
184
 
    //------------------------------------
185
 
    // Component-wise multiplication by -1
186
 
    //------------------------------------
187
 
 
188
 
    Matrix33            operator - () const;
189
 
    const Matrix33 &    negate ();
190
 
 
191
 
 
192
 
    //------------------------------
193
 
    // Component-wise multiplication
194
 
    //------------------------------
195
 
 
196
 
    const Matrix33 &    operator *= (T a);
197
 
    Matrix33            operator * (T a) const;
198
 
 
199
 
 
200
 
    //-----------------------------------
201
 
    // Matrix-times-matrix multiplication
202
 
    //-----------------------------------
203
 
 
204
 
    const Matrix33 &    operator *= (const Matrix33 &v);
205
 
    Matrix33            operator * (const Matrix33 &v) const;
206
 
 
207
 
 
208
 
    //---------------------------------------------
209
 
    // Vector-times-matrix multiplication; see also
210
 
    // the "operator *" functions defined below.
211
 
    //---------------------------------------------
212
 
 
213
 
    template <class S>
214
 
    void                multVecMatrix(const Vec2<S> &src, Vec2<S> &dst) const;
215
 
 
216
 
    template <class S>
217
 
    void                multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const;
218
 
 
219
 
 
220
 
    //------------------------
221
 
    // Component-wise division
222
 
    //------------------------
223
 
 
224
 
    const Matrix33 &    operator /= (T a);
225
 
    Matrix33            operator / (T a) const;
226
 
 
227
 
 
228
 
    //------------------
229
 
    // Transposed matrix
230
 
    //------------------
231
 
 
232
 
    const Matrix33 &    transpose ();
233
 
    Matrix33            transposed () const;
234
 
 
235
 
 
236
 
    //------------------------------------------------------------
237
 
    // Inverse matrix: If singExc is false, inverting a singular
238
 
    // matrix produces an identity matrix.  If singExc is true,
239
 
    // inverting a singular matrix throws a SingMatrixExc.
240
 
    //
241
 
    // inverse() and invert() invert matrices using determinants;
242
 
    // gjInverse() and gjInvert() use the Gauss-Jordan method.
243
 
    //
244
 
    // inverse() and invert() are significantly faster than
245
 
    // gjInverse() and gjInvert(), but the results may be slightly
246
 
    // less accurate.
247
 
    // 
248
 
    //------------------------------------------------------------
249
 
 
250
 
    const Matrix33 &    invert (bool singExc = false)
251
 
                        throw (Iex::MathExc);
252
 
 
253
 
    Matrix33<T>         inverse (bool singExc = false) const
254
 
                        throw (Iex::MathExc);
255
 
 
256
 
    const Matrix33 &    gjInvert (bool singExc = false)
257
 
                        throw (Iex::MathExc);
258
 
 
259
 
    Matrix33<T>         gjInverse (bool singExc = false) const
260
 
                        throw (Iex::MathExc);
261
 
 
262
 
 
263
 
    //-----------------------------------------
264
 
    // Set matrix to rotation by r (in radians)
265
 
    //-----------------------------------------
266
 
 
267
 
    template <class S>
268
 
    const Matrix33 &    setRotation (S r);
269
 
 
270
 
 
271
 
    //-----------------------------
272
 
    // Rotate the given matrix by r
273
 
    //-----------------------------
274
 
 
275
 
    template <class S>
276
 
    const Matrix33 &    rotate (S r);
277
 
 
278
 
 
279
 
    //--------------------------------------------
280
 
    // Set matrix to scale by given uniform factor
281
 
    //--------------------------------------------
282
 
 
283
 
    const Matrix33 &    setScale (T s);
284
 
 
285
 
 
286
 
    //------------------------------------
287
 
    // Set matrix to scale by given vector
288
 
    //------------------------------------
289
 
 
290
 
    template <class S>
291
 
    const Matrix33 &    setScale (const Vec2<S> &s);
292
 
 
293
 
 
294
 
    //----------------------
295
 
    // Scale the matrix by s
296
 
    //----------------------
297
 
 
298
 
    template <class S>
299
 
    const Matrix33 &    scale (const Vec2<S> &s);
300
 
 
301
 
 
302
 
    //------------------------------------------
303
 
    // Set matrix to translation by given vector
304
 
    //------------------------------------------
305
 
 
306
 
    template <class S>
307
 
    const Matrix33 &    setTranslation (const Vec2<S> &t);
308
 
 
309
 
 
310
 
    //-----------------------------
311
 
    // Return translation component
312
 
    //-----------------------------
313
 
 
314
 
    Vec2<T>             translation () const;
315
 
 
316
 
 
317
 
    //--------------------------
318
 
    // Translate the matrix by t
319
 
    //--------------------------
320
 
 
321
 
    template <class S>
322
 
    const Matrix33 &    translate (const Vec2<S> &t);
323
 
 
324
 
 
325
 
    //-----------------------------------------------------------
326
 
    // Set matrix to shear x for each y coord. by given factor xy
327
 
    //-----------------------------------------------------------
328
 
 
329
 
    template <class S>
330
 
    const Matrix33 &    setShear (const S &h);
331
 
 
332
 
 
333
 
    //-------------------------------------------------------------
334
 
    // Set matrix to shear x for each y coord. by given factor h[0]
335
 
    // and to shear y for each x coord. by given factor h[1]
336
 
    //-------------------------------------------------------------
337
 
 
338
 
    template <class S>
339
 
    const Matrix33 &    setShear (const Vec2<S> &h);
340
 
 
341
 
 
342
 
    //-----------------------------------------------------------
343
 
    // Shear the matrix in x for each y coord. by given factor xy
344
 
    //-----------------------------------------------------------
345
 
 
346
 
    template <class S>
347
 
    const Matrix33 &    shear (const S &xy);
348
 
 
349
 
 
350
 
    //-----------------------------------------------------------
351
 
    // Shear the matrix in x for each y coord. by given factor xy
352
 
    // and shear y for each x coord. by given factor yx
353
 
    //-----------------------------------------------------------
354
 
 
355
 
    template <class S>
356
 
    const Matrix33 &    shear (const Vec2<S> &h);
357
 
 
358
 
 
359
 
    //-------------------------------------------------
360
 
    // Limitations of type T (see also class limits<T>)
361
 
    //-------------------------------------------------
362
 
 
363
 
    static T            baseTypeMin()           {return limits<T>::min();}
364
 
    static T            baseTypeMax()           {return limits<T>::max();}
365
 
    static T            baseTypeSmallest()      {return limits<T>::smallest();}
366
 
    static T            baseTypeEpsilon()       {return limits<T>::epsilon();}
367
 
};
368
 
 
369
 
 
370
 
template <class T> class Matrix44
371
 
{
372
 
  public:
373
 
 
374
 
    //-------------------
375
 
    // Access to elements
376
 
    //-------------------
377
 
 
378
 
    T           x[4][4];
379
 
 
380
 
    T *         operator [] (int i);
381
 
    const T *   operator [] (int i) const;
382
 
 
383
 
 
384
 
    //-------------
385
 
    // Constructors
386
 
    //-------------
387
 
 
388
 
    Matrix44 ();
389
 
                                // 1 0 0 0
390
 
                                // 0 1 0 0
391
 
                                // 0 0 1 0
392
 
                                // 0 0 0 1
393
 
 
394
 
    Matrix44 (T a);
395
 
                                // a a a a
396
 
                                // a a a a
397
 
                                // a a a a
398
 
                                // a a a a
399
 
 
400
 
    Matrix44 (const T a[4][4]) ;
401
 
                                // a[0][0] a[0][1] a[0][2] a[0][3]
402
 
                                // a[1][0] a[1][1] a[1][2] a[1][3]
403
 
                                // a[2][0] a[2][1] a[2][2] a[2][3]
404
 
                                // a[3][0] a[3][1] a[3][2] a[3][3]
405
 
 
406
 
    Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h,
407
 
              T i, T j, T k, T l, T m, T n, T o, T p);
408
 
 
409
 
                                // a b c d
410
 
                                // e f g h
411
 
                                // i j k l
412
 
                                // m n o p
413
 
 
414
 
    Matrix44 (Matrix33<T> r, Vec3<T> t);
415
 
                                // r r r 0
416
 
                                // r r r 0
417
 
                                // r r r 0
418
 
                                // t t t 1
419
 
 
420
 
 
421
 
    //--------------------------------
422
 
    // Copy constructor and assignment
423
 
    //--------------------------------
424
 
 
425
 
    Matrix44 (const Matrix44 &v);
426
 
 
427
 
    const Matrix44 &    operator = (const Matrix44 &v);
428
 
    const Matrix44 &    operator = (T a);
429
 
 
430
 
 
431
 
    //----------------------
432
 
    // Compatibility with Sb
433
 
    //----------------------
434
 
    
435
 
    T *                 getValue ();
436
 
    const T *           getValue () const;
437
 
 
438
 
    template <class S>
439
 
    void                getValue (Matrix44<S> &v) const;
440
 
    template <class S>
441
 
    Matrix44 &          setValue (const Matrix44<S> &v);
442
 
 
443
 
    template <class S>
444
 
    Matrix44 &          setTheMatrix (const Matrix44<S> &v);
445
 
 
446
 
    //---------
447
 
    // Identity
448
 
    //---------
449
 
 
450
 
    void                makeIdentity();
451
 
 
452
 
 
453
 
    //---------
454
 
    // Equality
455
 
    //---------
456
 
 
457
 
    bool                operator == (const Matrix44 &v) const;
458
 
    bool                operator != (const Matrix44 &v) const;
459
 
 
460
 
    //-----------------------------------------------------------------------
461
 
    // Compare two matrices and test if they are "approximately equal":
462
 
    //
463
 
    // equalWithAbsError (m, e)
464
 
    //
465
 
    //      Returns true if the coefficients of this and m are the same with
466
 
    //      an absolute error of no more than e, i.e., for all i, j
467
 
    //
468
 
    //      abs (this[i][j] - m[i][j]) <= e
469
 
    //
470
 
    // equalWithRelError (m, e)
471
 
    //
472
 
    //      Returns true if the coefficients of this and m are the same with
473
 
    //      a relative error of no more than e, i.e., for all i, j
474
 
    //
475
 
    //      abs (this[i] - v[i][j]) <= e * abs (this[i][j])
476
 
    //-----------------------------------------------------------------------
477
 
 
478
 
    bool                equalWithAbsError (const Matrix44<T> &v, T e) const;
479
 
    bool                equalWithRelError (const Matrix44<T> &v, T e) const;
480
 
 
481
 
 
482
 
    //------------------------
483
 
    // Component-wise addition
484
 
    //------------------------
485
 
 
486
 
    const Matrix44 &    operator += (const Matrix44 &v);
487
 
    const Matrix44 &    operator += (T a);
488
 
    Matrix44            operator + (const Matrix44 &v) const;
489
 
 
490
 
 
491
 
    //---------------------------
492
 
    // Component-wise subtraction
493
 
    //---------------------------
494
 
 
495
 
    const Matrix44 &    operator -= (const Matrix44 &v);
496
 
    const Matrix44 &    operator -= (T a);
497
 
    Matrix44            operator - (const Matrix44 &v) const;
498
 
 
499
 
 
500
 
    //------------------------------------
501
 
    // Component-wise multiplication by -1
502
 
    //------------------------------------
503
 
 
504
 
    Matrix44            operator - () const;
505
 
    const Matrix44 &    negate ();
506
 
 
507
 
 
508
 
    //------------------------------
509
 
    // Component-wise multiplication
510
 
    //------------------------------
511
 
 
512
 
    const Matrix44 &    operator *= (T a);
513
 
    Matrix44            operator * (T a) const;
514
 
 
515
 
 
516
 
    //-----------------------------------
517
 
    // Matrix-times-matrix multiplication
518
 
    //-----------------------------------
519
 
 
520
 
    const Matrix44 &    operator *= (const Matrix44 &v);
521
 
    Matrix44            operator * (const Matrix44 &v) const;
522
 
 
523
 
    static void         multiply (const Matrix44 &a,    // assumes that
524
 
                                  const Matrix44 &b,    // &a != &c and
525
 
                                  Matrix44 &c);         // &b != &c.
526
 
 
527
 
 
528
 
    //---------------------------------------------
529
 
    // Vector-times-matrix multiplication; see also
530
 
    // the "operator *" functions defined below.
531
 
    //---------------------------------------------
532
 
 
533
 
    template <class S>
534
 
    void                multVecMatrix(const Vec3<S> &src, Vec3<S> &dst) const;
535
 
 
536
 
    template <class S>
537
 
    void                multDirMatrix(const Vec3<S> &src, Vec3<S> &dst) const;
538
 
 
539
 
 
540
 
    //------------------------
541
 
    // Component-wise division
542
 
    //------------------------
543
 
 
544
 
    const Matrix44 &    operator /= (T a);
545
 
    Matrix44            operator / (T a) const;
546
 
 
547
 
 
548
 
    //------------------
549
 
    // Transposed matrix
550
 
    //------------------
551
 
 
552
 
    const Matrix44 &    transpose ();
553
 
    Matrix44            transposed () const;
554
 
 
555
 
 
556
 
    //------------------------------------------------------------
557
 
    // Inverse matrix: If singExc is false, inverting a singular
558
 
    // matrix produces an identity matrix.  If singExc is true,
559
 
    // inverting a singular matrix throws a SingMatrixExc.
560
 
    //
561
 
    // inverse() and invert() invert matrices using determinants;
562
 
    // gjInverse() and gjInvert() use the Gauss-Jordan method.
563
 
    //
564
 
    // inverse() and invert() are significantly faster than
565
 
    // gjInverse() and gjInvert(), but the results may be slightly
566
 
    // less accurate.
567
 
    // 
568
 
    //------------------------------------------------------------
569
 
 
570
 
    const Matrix44 &    invert (bool singExc = false)
571
 
                        throw (Iex::MathExc);
572
 
 
573
 
    Matrix44<T>         inverse (bool singExc = false) const
574
 
                        throw (Iex::MathExc);
575
 
 
576
 
    const Matrix44 &    gjInvert (bool singExc = false)
577
 
                        throw (Iex::MathExc);
578
 
 
579
 
    Matrix44<T>         gjInverse (bool singExc = false) const
580
 
                        throw (Iex::MathExc);
581
 
 
582
 
 
583
 
    //--------------------------------------------------------
584
 
    // Set matrix to rotation by XYZ euler angles (in radians)
585
 
    //--------------------------------------------------------
586
 
 
587
 
    template <class S>
588
 
    const Matrix44 &    setEulerAngles (const Vec3<S>& r);
589
 
 
590
 
 
591
 
    //--------------------------------------------------------
592
 
    // Set matrix to rotation around given axis by given angle
593
 
    //--------------------------------------------------------
594
 
 
595
 
    template <class S>
596
 
    const Matrix44 &    setAxisAngle (const Vec3<S>& ax, S ang);
597
 
 
598
 
 
599
 
    //-------------------------------------------
600
 
    // Rotate the matrix by XYZ euler angles in r
601
 
    //-------------------------------------------
602
 
 
603
 
    template <class S>
604
 
    const Matrix44 &    rotate (const Vec3<S> &r);
605
 
 
606
 
 
607
 
    //--------------------------------------------
608
 
    // Set matrix to scale by given uniform factor
609
 
    //--------------------------------------------
610
 
 
611
 
    const Matrix44 &    setScale (T s);
612
 
 
613
 
 
614
 
    //------------------------------------
615
 
    // Set matrix to scale by given vector
616
 
    //------------------------------------
617
 
 
618
 
    template <class S>
619
 
    const Matrix44 &    setScale (const Vec3<S> &s);
620
 
 
621
 
 
622
 
    //----------------------
623
 
    // Scale the matrix by s
624
 
    //----------------------
625
 
 
626
 
    template <class S>
627
 
    const Matrix44 &    scale (const Vec3<S> &s);
628
 
 
629
 
 
630
 
    //------------------------------------------
631
 
    // Set matrix to translation by given vector
632
 
    //------------------------------------------
633
 
 
634
 
    template <class S>
635
 
    const Matrix44 &    setTranslation (const Vec3<S> &t);
636
 
 
637
 
 
638
 
    //-----------------------------
639
 
    // Return translation component
640
 
    //-----------------------------
641
 
 
642
 
    const Vec3<T>       translation () const;
643
 
 
644
 
 
645
 
    //--------------------------
646
 
    // Translate the matrix by t
647
 
    //--------------------------
648
 
 
649
 
    template <class S>
650
 
    const Matrix44 &    translate (const Vec3<S> &t);
651
 
 
652
 
 
653
 
    //-------------------------------------------------------------
654
 
    // Set matrix to shear by given vector h.  The resulting matrix
655
 
    //    will shear x for each y coord. by a factor of h[0] ;
656
 
    //    will shear x for each z coord. by a factor of h[1] ;
657
 
    //    will shear y for each z coord. by a factor of h[2] .
658
 
    //-------------------------------------------------------------
659
 
 
660
 
    template <class S>
661
 
    const Matrix44 &    setShear (const Vec3<S> &h);
662
 
 
663
 
 
664
 
    //------------------------------------------------------------
665
 
    // Set matrix to shear by given factors.  The resulting matrix
666
 
    //    will shear x for each y coord. by a factor of h.xy ;
667
 
    //    will shear x for each z coord. by a factor of h.xz ;
668
 
    //    will shear y for each z coord. by a factor of h.yz ; 
669
 
    //    will shear y for each x coord. by a factor of h.yx ;
670
 
    //    will shear z for each x coord. by a factor of h.zx ;
671
 
    //    will shear z for each y coord. by a factor of h.zy .
672
 
    //------------------------------------------------------------
673
 
 
674
 
    template <class S>
675
 
    const Matrix44 &    setShear (const Shear6<S> &h);
676
 
 
677
 
 
678
 
    //--------------------------------------------------------
679
 
    // Shear the matrix by given vector.  The composed matrix 
680
 
    // will be <shear> * <this>, where the shear matrix ...
681
 
    //    will shear x for each y coord. by a factor of h[0] ;
682
 
    //    will shear x for each z coord. by a factor of h[1] ;
683
 
    //    will shear y for each z coord. by a factor of h[2] .
684
 
    //--------------------------------------------------------
685
 
 
686
 
    template <class S>
687
 
    const Matrix44 &    shear (const Vec3<S> &h);
688
 
 
689
 
 
690
 
    //------------------------------------------------------------
691
 
    // Shear the matrix by the given factors.  The composed matrix 
692
 
    // will be <shear> * <this>, where the shear matrix ...
693
 
    //    will shear x for each y coord. by a factor of h.xy ;
694
 
    //    will shear x for each z coord. by a factor of h.xz ;
695
 
    //    will shear y for each z coord. by a factor of h.yz ;
696
 
    //    will shear y for each x coord. by a factor of h.yx ;
697
 
    //    will shear z for each x coord. by a factor of h.zx ;
698
 
    //    will shear z for each y coord. by a factor of h.zy .
699
 
    //------------------------------------------------------------
700
 
 
701
 
    template <class S>
702
 
    const Matrix44 &    shear (const Shear6<S> &h);
703
 
 
704
 
 
705
 
    //-------------------------------------------------
706
 
    // Limitations of type T (see also class limits<T>)
707
 
    //-------------------------------------------------
708
 
 
709
 
    static T            baseTypeMin()           {return limits<T>::min();}
710
 
    static T            baseTypeMax()           {return limits<T>::max();}
711
 
    static T            baseTypeSmallest()      {return limits<T>::smallest();}
712
 
    static T            baseTypeEpsilon()       {return limits<T>::epsilon();}
713
 
};
714
 
 
715
 
 
716
 
//--------------
717
 
// Stream output
718
 
//--------------
719
 
 
720
 
template <class T>
721
 
std::ostream &  operator << (std::ostream & s, const Matrix33<T> &m); 
722
 
 
723
 
template <class T>
724
 
std::ostream &  operator << (std::ostream & s, const Matrix44<T> &m); 
725
 
 
726
 
 
727
 
//---------------------------------------------
728
 
// Vector-times-matrix multiplication operators
729
 
//---------------------------------------------
730
 
 
731
 
template <class S, class T>
732
 
const Vec2<S> &            operator *= (Vec2<S> &v, const Matrix33<T> &m);
733
 
 
734
 
template <class S, class T>
735
 
Vec2<S>                    operator * (const Vec2<S> &v, const Matrix33<T> &m);
736
 
 
737
 
template <class S, class T>
738
 
const Vec3<S> &            operator *= (Vec3<S> &v, const Matrix33<T> &m);
739
 
 
740
 
template <class S, class T>
741
 
Vec3<S>                    operator * (const Vec3<S> &v, const Matrix33<T> &m);
742
 
 
743
 
template <class S, class T>
744
 
const Vec3<S> &            operator *= (Vec3<S> &v, const Matrix44<T> &m);
745
 
 
746
 
template <class S, class T>
747
 
Vec3<S>                    operator * (const Vec3<S> &v, const Matrix44<T> &m);
748
 
 
749
 
 
750
 
//-------------------------
751
 
// Typedefs for convenience
752
 
//-------------------------
753
 
 
754
 
typedef Matrix33 <float>  M33f;
755
 
typedef Matrix33 <double> M33d;
756
 
typedef Matrix44 <float>  M44f;
757
 
typedef Matrix44 <double> M44d;
758
 
 
759
 
 
760
 
//---------------------------
761
 
// Implementation of Matrix33
762
 
//---------------------------
763
 
 
764
 
template <class T>
765
 
inline T *
766
 
Matrix33<T>::operator [] (int i)
767
 
{
768
 
    return x[i];
769
 
}
770
 
 
771
 
template <class T>
772
 
inline const T *
773
 
Matrix33<T>::operator [] (int i) const
774
 
{
775
 
    return x[i];
776
 
}
777
 
 
778
 
template <class T>
779
 
inline
780
 
Matrix33<T>::Matrix33 ()
781
 
{
782
 
    x[0][0] = 1;
783
 
    x[0][1] = 0;
784
 
    x[0][2] = 0;
785
 
    x[1][0] = 0;
786
 
    x[1][1] = 1;
787
 
    x[1][2] = 0;
788
 
    x[2][0] = 0;
789
 
    x[2][1] = 0;
790
 
    x[2][2] = 1;
791
 
}
792
 
 
793
 
template <class T>
794
 
inline
795
 
Matrix33<T>::Matrix33 (T a)
796
 
{
797
 
    x[0][0] = a;
798
 
    x[0][1] = a;
799
 
    x[0][2] = a;
800
 
    x[1][0] = a;
801
 
    x[1][1] = a;
802
 
    x[1][2] = a;
803
 
    x[2][0] = a;
804
 
    x[2][1] = a;
805
 
    x[2][2] = a;
806
 
}
807
 
 
808
 
template <class T>
809
 
inline
810
 
Matrix33<T>::Matrix33 (const T a[3][3]) 
811
 
{
812
 
    x[0][0] = a[0][0];
813
 
    x[0][1] = a[0][1];
814
 
    x[0][2] = a[0][2];
815
 
    x[1][0] = a[1][0];
816
 
    x[1][1] = a[1][1];
817
 
    x[1][2] = a[1][2];
818
 
    x[2][0] = a[2][0];
819
 
    x[2][1] = a[2][1];
820
 
    x[2][2] = a[2][2];
821
 
}
822
 
 
823
 
template <class T>
824
 
inline
825
 
Matrix33<T>::Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i)
826
 
{
827
 
    x[0][0] = a;
828
 
    x[0][1] = b;
829
 
    x[0][2] = c;
830
 
    x[1][0] = d;
831
 
    x[1][1] = e;
832
 
    x[1][2] = f;
833
 
    x[2][0] = g;
834
 
    x[2][1] = h;
835
 
    x[2][2] = i;
836
 
}
837
 
 
838
 
template <class T>
839
 
inline
840
 
Matrix33<T>::Matrix33 (const Matrix33 &v)
841
 
{
842
 
    x[0][0] = v.x[0][0];
843
 
    x[0][1] = v.x[0][1];
844
 
    x[0][2] = v.x[0][2];
845
 
    x[1][0] = v.x[1][0];
846
 
    x[1][1] = v.x[1][1];
847
 
    x[1][2] = v.x[1][2];
848
 
    x[2][0] = v.x[2][0];
849
 
    x[2][1] = v.x[2][1];
850
 
    x[2][2] = v.x[2][2];
851
 
}
852
 
 
853
 
template <class T>
854
 
inline const Matrix33<T> &
855
 
Matrix33<T>::operator = (const Matrix33 &v)
856
 
{
857
 
    x[0][0] = v.x[0][0];
858
 
    x[0][1] = v.x[0][1];
859
 
    x[0][2] = v.x[0][2];
860
 
    x[1][0] = v.x[1][0];
861
 
    x[1][1] = v.x[1][1];
862
 
    x[1][2] = v.x[1][2];
863
 
    x[2][0] = v.x[2][0];
864
 
    x[2][1] = v.x[2][1];
865
 
    x[2][2] = v.x[2][2];
866
 
    return *this;
867
 
}
868
 
 
869
 
template <class T>
870
 
inline const Matrix33<T> &
871
 
Matrix33<T>::operator = (T a)
872
 
{
873
 
    x[0][0] = a;
874
 
    x[0][1] = a;
875
 
    x[0][2] = a;
876
 
    x[1][0] = a;
877
 
    x[1][1] = a;
878
 
    x[1][2] = a;
879
 
    x[2][0] = a;
880
 
    x[2][1] = a;
881
 
    x[2][2] = a;
882
 
    return *this;
883
 
}
884
 
 
885
 
template <class T>
886
 
inline T *
887
 
Matrix33<T>::getValue ()
888
 
{
889
 
    return (T *) &x[0][0];
890
 
}
891
 
 
892
 
template <class T>
893
 
inline const T *
894
 
Matrix33<T>::getValue () const
895
 
{
896
 
    return (const T *) &x[0][0];
897
 
}
898
 
 
899
 
template <class T>
900
 
template <class S>
901
 
inline void
902
 
Matrix33<T>::getValue (Matrix33<S> &v) const
903
 
{
904
 
    v.x[0][0] = x[0][0];
905
 
    v.x[0][1] = x[0][1];
906
 
    v.x[0][2] = x[0][2];
907
 
    v.x[1][0] = x[1][0];
908
 
    v.x[1][1] = x[1][1];
909
 
    v.x[1][2] = x[1][2];
910
 
    v.x[2][0] = x[2][0];
911
 
    v.x[2][1] = x[2][1];
912
 
    v.x[2][2] = x[2][2];
913
 
}
914
 
 
915
 
template <class T>
916
 
template <class S>
917
 
inline Matrix33<T> &
918
 
Matrix33<T>::setValue (const Matrix33<S> &v)
919
 
{
920
 
    x[0][0] = v.x[0][0];
921
 
    x[0][1] = v.x[0][1];
922
 
    x[0][2] = v.x[0][2];
923
 
    x[1][0] = v.x[1][0];
924
 
    x[1][1] = v.x[1][1];
925
 
    x[1][2] = v.x[1][2];
926
 
    x[2][0] = v.x[2][0];
927
 
    x[2][1] = v.x[2][1];
928
 
    x[2][2] = v.x[2][2];
929
 
    return *this;
930
 
}
931
 
 
932
 
template <class T>
933
 
template <class S>
934
 
inline Matrix33<T> &
935
 
Matrix33<T>::setTheMatrix (const Matrix33<S> &v)
936
 
{
937
 
    x[0][0] = v.x[0][0];
938
 
    x[0][1] = v.x[0][1];
939
 
    x[0][2] = v.x[0][2];
940
 
    x[1][0] = v.x[1][0];
941
 
    x[1][1] = v.x[1][1];
942
 
    x[1][2] = v.x[1][2];
943
 
    x[2][0] = v.x[2][0];
944
 
    x[2][1] = v.x[2][1];
945
 
    x[2][2] = v.x[2][2];
946
 
    return *this;
947
 
}
948
 
 
949
 
template <class T>
950
 
inline void
951
 
Matrix33<T>::makeIdentity()
952
 
{
953
 
    x[0][0] = 1;
954
 
    x[0][1] = 0;
955
 
    x[0][2] = 0;
956
 
    x[1][0] = 0;
957
 
    x[1][1] = 1;
958
 
    x[1][2] = 0;
959
 
    x[2][0] = 0;
960
 
    x[2][1] = 0;
961
 
    x[2][2] = 1;
962
 
}
963
 
 
964
 
template <class T>
965
 
bool
966
 
Matrix33<T>::operator == (const Matrix33 &v) const
967
 
{
968
 
    return x[0][0] == v.x[0][0] &&
969
 
           x[0][1] == v.x[0][1] &&
970
 
           x[0][2] == v.x[0][2] &&
971
 
           x[1][0] == v.x[1][0] &&
972
 
           x[1][1] == v.x[1][1] &&
973
 
           x[1][2] == v.x[1][2] &&
974
 
           x[2][0] == v.x[2][0] &&
975
 
           x[2][1] == v.x[2][1] &&
976
 
           x[2][2] == v.x[2][2];
977
 
}
978
 
 
979
 
template <class T>
980
 
bool
981
 
Matrix33<T>::operator != (const Matrix33 &v) const
982
 
{
983
 
    return x[0][0] != v.x[0][0] ||
984
 
           x[0][1] != v.x[0][1] ||
985
 
           x[0][2] != v.x[0][2] ||
986
 
           x[1][0] != v.x[1][0] ||
987
 
           x[1][1] != v.x[1][1] ||
988
 
           x[1][2] != v.x[1][2] ||
989
 
           x[2][0] != v.x[2][0] ||
990
 
           x[2][1] != v.x[2][1] ||
991
 
           x[2][2] != v.x[2][2];
992
 
}
993
 
 
994
 
template <class T>
995
 
bool
996
 
Matrix33<T>::equalWithAbsError (const Matrix33<T> &m, T e) const
997
 
{
998
 
    for (int i = 0; i < 3; i++)
999
 
        for (int j = 0; j < 3; j++)
1000
 
            if (!Imath::equalWithAbsError ((*this)[i][j], m[i][j], e))
1001
 
                return false;
1002
 
 
1003
 
    return true;
1004
 
}
1005
 
 
1006
 
template <class T>
1007
 
bool
1008
 
Matrix33<T>::equalWithRelError (const Matrix33<T> &m, T e) const
1009
 
{
1010
 
    for (int i = 0; i < 3; i++)
1011
 
        for (int j = 0; j < 3; j++)
1012
 
            if (!Imath::equalWithRelError ((*this)[i][j], m[i][j], e))
1013
 
                return false;
1014
 
 
1015
 
    return true;
1016
 
}
1017
 
 
1018
 
template <class T>
1019
 
const Matrix33<T> &
1020
 
Matrix33<T>::operator += (const Matrix33<T> &v)
1021
 
{
1022
 
    x[0][0] += v.x[0][0];
1023
 
    x[0][1] += v.x[0][1];
1024
 
    x[0][2] += v.x[0][2];
1025
 
    x[1][0] += v.x[1][0];
1026
 
    x[1][1] += v.x[1][1];
1027
 
    x[1][2] += v.x[1][2];
1028
 
    x[2][0] += v.x[2][0];
1029
 
    x[2][1] += v.x[2][1];
1030
 
    x[2][2] += v.x[2][2];
1031
 
 
1032
 
    return *this;
1033
 
}
1034
 
 
1035
 
template <class T>
1036
 
const Matrix33<T> &
1037
 
Matrix33<T>::operator += (T a)
1038
 
{
1039
 
    x[0][0] += a;
1040
 
    x[0][1] += a;
1041
 
    x[0][2] += a;
1042
 
    x[1][0] += a;
1043
 
    x[1][1] += a;
1044
 
    x[1][2] += a;
1045
 
    x[2][0] += a;
1046
 
    x[2][1] += a;
1047
 
    x[2][2] += a;
1048
 
  
1049
 
    return *this;
1050
 
}
1051
 
 
1052
 
template <class T>
1053
 
Matrix33<T>
1054
 
Matrix33<T>::operator + (const Matrix33<T> &v) const
1055
 
{
1056
 
    return Matrix33 (x[0][0] + v.x[0][0],
1057
 
                     x[0][1] + v.x[0][1],
1058
 
                     x[0][2] + v.x[0][2],
1059
 
                     x[1][0] + v.x[1][0],
1060
 
                     x[1][1] + v.x[1][1],
1061
 
                     x[1][2] + v.x[1][2],
1062
 
                     x[2][0] + v.x[2][0],
1063
 
                     x[2][1] + v.x[2][1],
1064
 
                     x[2][2] + v.x[2][2]);
1065
 
}
1066
 
 
1067
 
template <class T>
1068
 
const Matrix33<T> &
1069
 
Matrix33<T>::operator -= (const Matrix33<T> &v)
1070
 
{
1071
 
    x[0][0] -= v.x[0][0];
1072
 
    x[0][1] -= v.x[0][1];
1073
 
    x[0][2] -= v.x[0][2];
1074
 
    x[1][0] -= v.x[1][0];
1075
 
    x[1][1] -= v.x[1][1];
1076
 
    x[1][2] -= v.x[1][2];
1077
 
    x[2][0] -= v.x[2][0];
1078
 
    x[2][1] -= v.x[2][1];
1079
 
    x[2][2] -= v.x[2][2];
1080
 
  
1081
 
    return *this;
1082
 
}
1083
 
 
1084
 
template <class T>
1085
 
const Matrix33<T> &
1086
 
Matrix33<T>::operator -= (T a)
1087
 
{
1088
 
    x[0][0] -= a;
1089
 
    x[0][1] -= a;
1090
 
    x[0][2] -= a;
1091
 
    x[1][0] -= a;
1092
 
    x[1][1] -= a;
1093
 
    x[1][2] -= a;
1094
 
    x[2][0] -= a;
1095
 
    x[2][1] -= a;
1096
 
    x[2][2] -= a;
1097
 
  
1098
 
    return *this;
1099
 
}
1100
 
 
1101
 
template <class T>
1102
 
Matrix33<T>
1103
 
Matrix33<T>::operator - (const Matrix33<T> &v) const
1104
 
{
1105
 
    return Matrix33 (x[0][0] - v.x[0][0],
1106
 
                     x[0][1] - v.x[0][1],
1107
 
                     x[0][2] - v.x[0][2],
1108
 
                     x[1][0] - v.x[1][0],
1109
 
                     x[1][1] - v.x[1][1],
1110
 
                     x[1][2] - v.x[1][2],
1111
 
                     x[2][0] - v.x[2][0],
1112
 
                     x[2][1] - v.x[2][1],
1113
 
                     x[2][2] - v.x[2][2]);
1114
 
}
1115
 
 
1116
 
template <class T>
1117
 
Matrix33<T>
1118
 
Matrix33<T>::operator - () const
1119
 
{
1120
 
    return Matrix33 (-x[0][0],
1121
 
                     -x[0][1],
1122
 
                     -x[0][2],
1123
 
                     -x[1][0],
1124
 
                     -x[1][1],
1125
 
                     -x[1][2],
1126
 
                     -x[2][0],
1127
 
                     -x[2][1],
1128
 
                     -x[2][2]);
1129
 
}
1130
 
 
1131
 
template <class T>
1132
 
const Matrix33<T> &
1133
 
Matrix33<T>::negate ()
1134
 
{
1135
 
    x[0][0] = -x[0][0];
1136
 
    x[0][1] = -x[0][1];
1137
 
    x[0][2] = -x[0][2];
1138
 
    x[1][0] = -x[1][0];
1139
 
    x[1][1] = -x[1][1];
1140
 
    x[1][2] = -x[1][2];
1141
 
    x[2][0] = -x[2][0];
1142
 
    x[2][1] = -x[2][1];
1143
 
    x[2][2] = -x[2][2];
1144
 
 
1145
 
    return *this;
1146
 
}
1147
 
 
1148
 
template <class T>
1149
 
const Matrix33<T> &
1150
 
Matrix33<T>::operator *= (T a)
1151
 
{
1152
 
    x[0][0] *= a;
1153
 
    x[0][1] *= a;
1154
 
    x[0][2] *= a;
1155
 
    x[1][0] *= a;
1156
 
    x[1][1] *= a;
1157
 
    x[1][2] *= a;
1158
 
    x[2][0] *= a;
1159
 
    x[2][1] *= a;
1160
 
    x[2][2] *= a;
1161
 
  
1162
 
    return *this;
1163
 
}
1164
 
 
1165
 
template <class T>
1166
 
Matrix33<T>
1167
 
Matrix33<T>::operator * (T a) const
1168
 
{
1169
 
    return Matrix33 (x[0][0] * a,
1170
 
                     x[0][1] * a,
1171
 
                     x[0][2] * a,
1172
 
                     x[1][0] * a,
1173
 
                     x[1][1] * a,
1174
 
                     x[1][2] * a,
1175
 
                     x[2][0] * a,
1176
 
                     x[2][1] * a,
1177
 
                     x[2][2] * a);
1178
 
}
1179
 
 
1180
 
template <class T>
1181
 
inline Matrix33<T>
1182
 
operator * (T a, const Matrix33<T> &v)
1183
 
{
1184
 
    return v * a;
1185
 
}
1186
 
 
1187
 
template <class T>
1188
 
const Matrix33<T> &
1189
 
Matrix33<T>::operator *= (const Matrix33<T> &v)
1190
 
{
1191
 
    Matrix33 tmp (T (0));
1192
 
 
1193
 
    for (int i = 0; i < 3; i++)
1194
 
        for (int j = 0; j < 3; j++)
1195
 
            for (int k = 0; k < 3; k++)
1196
 
                tmp.x[i][j] += x[i][k] * v.x[k][j];
1197
 
 
1198
 
    *this = tmp;
1199
 
    return *this;
1200
 
}
1201
 
 
1202
 
template <class T>
1203
 
Matrix33<T>
1204
 
Matrix33<T>::operator * (const Matrix33<T> &v) const
1205
 
{
1206
 
    Matrix33 tmp (T (0));
1207
 
 
1208
 
    for (int i = 0; i < 3; i++)
1209
 
        for (int j = 0; j < 3; j++)
1210
 
            for (int k = 0; k < 3; k++)
1211
 
                tmp.x[i][j] += x[i][k] * v.x[k][j];
1212
 
 
1213
 
    return tmp;
1214
 
}
1215
 
 
1216
 
template <class T>
1217
 
template <class S>
1218
 
void
1219
 
Matrix33<T>::multVecMatrix(const Vec2<S> &src, Vec2<S> &dst) const
1220
 
{
1221
 
    S a, b, w;
1222
 
 
1223
 
    a = src[0] * x[0][0] + src[1] * x[1][0] + x[2][0];
1224
 
    b = src[0] * x[0][1] + src[1] * x[1][1] + x[2][1];
1225
 
    w = src[0] * x[0][2] + src[1] * x[1][2] + x[2][2];
1226
 
 
1227
 
    dst.x = a / w;
1228
 
    dst.y = b / w;
1229
 
}
1230
 
 
1231
 
template <class T>
1232
 
template <class S>
1233
 
void
1234
 
Matrix33<T>::multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const
1235
 
{
1236
 
    S a, b;
1237
 
 
1238
 
    a = src[0] * x[0][0] + src[1] * x[1][0];
1239
 
    b = src[0] * x[0][1] + src[1] * x[1][1];
1240
 
 
1241
 
    dst.x = a;
1242
 
    dst.y = b;
1243
 
}
1244
 
 
1245
 
template <class T>
1246
 
const Matrix33<T> &
1247
 
Matrix33<T>::operator /= (T a)
1248
 
{
1249
 
    x[0][0] /= a;
1250
 
    x[0][1] /= a;
1251
 
    x[0][2] /= a;
1252
 
    x[1][0] /= a;
1253
 
    x[1][1] /= a;
1254
 
    x[1][2] /= a;
1255
 
    x[2][0] /= a;
1256
 
    x[2][1] /= a;
1257
 
    x[2][2] /= a;
1258
 
  
1259
 
    return *this;
1260
 
}
1261
 
 
1262
 
template <class T>
1263
 
Matrix33<T>
1264
 
Matrix33<T>::operator / (T a) const
1265
 
{
1266
 
    return Matrix33 (x[0][0] / a,
1267
 
                     x[0][1] / a,
1268
 
                     x[0][2] / a,
1269
 
                     x[1][0] / a,
1270
 
                     x[1][1] / a,
1271
 
                     x[1][2] / a,
1272
 
                     x[2][0] / a,
1273
 
                     x[2][1] / a,
1274
 
                     x[2][2] / a);
1275
 
}
1276
 
 
1277
 
template <class T>
1278
 
const Matrix33<T> &
1279
 
Matrix33<T>::transpose ()
1280
 
{
1281
 
    Matrix33 tmp (x[0][0],
1282
 
                  x[1][0],
1283
 
                  x[2][0],
1284
 
                  x[0][1],
1285
 
                  x[1][1],
1286
 
                  x[2][1],
1287
 
                  x[0][2],
1288
 
                  x[1][2],
1289
 
                  x[2][2]);
1290
 
    *this = tmp;
1291
 
    return *this;
1292
 
}
1293
 
 
1294
 
template <class T>
1295
 
Matrix33<T>
1296
 
Matrix33<T>::transposed () const
1297
 
{
1298
 
    return Matrix33 (x[0][0],
1299
 
                     x[1][0],
1300
 
                     x[2][0],
1301
 
                     x[0][1],
1302
 
                     x[1][1],
1303
 
                     x[2][1],
1304
 
                     x[0][2],
1305
 
                     x[1][2],
1306
 
                     x[2][2]);
1307
 
}
1308
 
 
1309
 
template <class T>
1310
 
const Matrix33<T> &
1311
 
Matrix33<T>::gjInvert (bool singExc) throw (Iex::MathExc)
1312
 
{
1313
 
    *this = gjInverse (singExc);
1314
 
    return *this;
1315
 
}
1316
 
 
1317
 
template <class T>
1318
 
Matrix33<T>
1319
 
Matrix33<T>::gjInverse (bool singExc) const throw (Iex::MathExc)
1320
 
{
1321
 
    int i, j, k;
1322
 
    Matrix33 s;
1323
 
    Matrix33 t (*this);
1324
 
 
1325
 
    // Forward elimination
1326
 
 
1327
 
    for (i = 0; i < 2 ; i++)
1328
 
    {
1329
 
        int pivot = i;
1330
 
 
1331
 
        T pivotsize = t[i][i];
1332
 
 
1333
 
        if (pivotsize < 0)
1334
 
            pivotsize = -pivotsize;
1335
 
 
1336
 
        for (j = i + 1; j < 3; j++)
1337
 
        {
1338
 
            T tmp = t[j][i];
1339
 
 
1340
 
            if (tmp < 0)
1341
 
                tmp = -tmp;
1342
 
 
1343
 
            if (tmp > pivotsize)
1344
 
            {
1345
 
                pivot = j;
1346
 
                pivotsize = tmp;
1347
 
            }
1348
 
        }
1349
 
 
1350
 
        if (pivotsize == 0)
1351
 
        {
1352
 
            if (singExc)
1353
 
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
1354
 
 
1355
 
            return Matrix33();
1356
 
        }
1357
 
 
1358
 
        if (pivot != i)
1359
 
        {
1360
 
            for (j = 0; j < 3; j++)
1361
 
            {
1362
 
                T tmp;
1363
 
 
1364
 
                tmp = t[i][j];
1365
 
                t[i][j] = t[pivot][j];
1366
 
                t[pivot][j] = tmp;
1367
 
 
1368
 
                tmp = s[i][j];
1369
 
                s[i][j] = s[pivot][j];
1370
 
                s[pivot][j] = tmp;
1371
 
            }
1372
 
        }
1373
 
 
1374
 
        for (j = i + 1; j < 3; j++)
1375
 
        {
1376
 
            T f = t[j][i] / t[i][i];
1377
 
 
1378
 
            for (k = 0; k < 3; k++)
1379
 
            {
1380
 
                t[j][k] -= f * t[i][k];
1381
 
                s[j][k] -= f * s[i][k];
1382
 
            }
1383
 
        }
1384
 
    }
1385
 
 
1386
 
    // Backward substitution
1387
 
 
1388
 
    for (i = 2; i >= 0; --i)
1389
 
    {
1390
 
        T f;
1391
 
 
1392
 
        if ((f = t[i][i]) == 0)
1393
 
        {
1394
 
            if (singExc)
1395
 
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
1396
 
 
1397
 
            return Matrix33();
1398
 
        }
1399
 
 
1400
 
        for (j = 0; j < 3; j++)
1401
 
        {
1402
 
            t[i][j] /= f;
1403
 
            s[i][j] /= f;
1404
 
        }
1405
 
 
1406
 
        for (j = 0; j < i; j++)
1407
 
        {
1408
 
            f = t[j][i];
1409
 
 
1410
 
            for (k = 0; k < 3; k++)
1411
 
            {
1412
 
                t[j][k] -= f * t[i][k];
1413
 
                s[j][k] -= f * s[i][k];
1414
 
            }
1415
 
        }
1416
 
    }
1417
 
 
1418
 
    return s;
1419
 
}
1420
 
 
1421
 
template <class T>
1422
 
const Matrix33<T> &
1423
 
Matrix33<T>::invert (bool singExc) throw (Iex::MathExc)
1424
 
{
1425
 
    *this = inverse (singExc);
1426
 
    return *this;
1427
 
}
1428
 
 
1429
 
template <class T>
1430
 
Matrix33<T>
1431
 
Matrix33<T>::inverse (bool singExc) const throw (Iex::MathExc)
1432
 
{
1433
 
    if (x[0][2] != 0 || x[1][2] != 0 || x[2][2] != 1)
1434
 
    {
1435
 
        Matrix33 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
1436
 
                    x[2][1] * x[0][2] - x[0][1] * x[2][2],
1437
 
                    x[0][1] * x[1][2] - x[1][1] * x[0][2],
1438
 
 
1439
 
                    x[2][0] * x[1][2] - x[1][0] * x[2][2],
1440
 
                    x[0][0] * x[2][2] - x[2][0] * x[0][2],
1441
 
                    x[1][0] * x[0][2] - x[0][0] * x[1][2],
1442
 
 
1443
 
                    x[1][0] * x[2][1] - x[2][0] * x[1][1],
1444
 
                    x[2][0] * x[0][1] - x[0][0] * x[2][1],
1445
 
                    x[0][0] * x[1][1] - x[1][0] * x[0][1]);
1446
 
 
1447
 
        T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
1448
 
 
1449
 
        if (Imath::abs (r) >= 1)
1450
 
        {
1451
 
            for (int i = 0; i < 3; ++i)
1452
 
            {
1453
 
                for (int j = 0; j < 3; ++j)
1454
 
                {
1455
 
                    s[i][j] /= r;
1456
 
                }
1457
 
            }
1458
 
        }
1459
 
        else
1460
 
        {
1461
 
            T mr = Imath::abs (r) / limits<T>::smallest();
1462
 
 
1463
 
            for (int i = 0; i < 3; ++i)
1464
 
            {
1465
 
                for (int j = 0; j < 3; ++j)
1466
 
                {
1467
 
                    if (mr > Imath::abs (s[i][j]))
1468
 
                    {
1469
 
                        s[i][j] /= r;
1470
 
                    }
1471
 
                    else
1472
 
                    {
1473
 
                        if (singExc)
1474
 
                            throw SingMatrixExc ("Cannot invert "
1475
 
                                                 "singular matrix.");
1476
 
                        return Matrix33();
1477
 
                    }
1478
 
                }
1479
 
            }
1480
 
        }
1481
 
 
1482
 
        return s;
1483
 
    }
1484
 
    else
1485
 
    {
1486
 
        Matrix33 s ( x[1][1],
1487
 
                    -x[0][1],
1488
 
                     0, 
1489
 
 
1490
 
                    -x[1][0],
1491
 
                     x[0][0],
1492
 
                     0,
1493
 
 
1494
 
                     0,
1495
 
                     0,
1496
 
                     1);
1497
 
 
1498
 
        T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
1499
 
 
1500
 
        if (Imath::abs (r) >= 1)
1501
 
        {
1502
 
            for (int i = 0; i < 2; ++i)
1503
 
            {
1504
 
                for (int j = 0; j < 2; ++j)
1505
 
                {
1506
 
                    s[i][j] /= r;
1507
 
                }
1508
 
            }
1509
 
        }
1510
 
        else
1511
 
        {
1512
 
            T mr = Imath::abs (r) / limits<T>::smallest();
1513
 
 
1514
 
            for (int i = 0; i < 2; ++i)
1515
 
            {
1516
 
                for (int j = 0; j < 2; ++j)
1517
 
                {
1518
 
                    if (mr > Imath::abs (s[i][j]))
1519
 
                    {
1520
 
                        s[i][j] /= r;
1521
 
                    }
1522
 
                    else
1523
 
                    {
1524
 
                        if (singExc)
1525
 
                            throw SingMatrixExc ("Cannot invert "
1526
 
                                                 "singular matrix.");
1527
 
                        return Matrix33();
1528
 
                    }
1529
 
                }
1530
 
            }
1531
 
        }
1532
 
 
1533
 
        s[2][0] = -x[2][0] * s[0][0] - x[2][1] * s[1][0];
1534
 
        s[2][1] = -x[2][0] * s[0][1] - x[2][1] * s[1][1];
1535
 
 
1536
 
        return s;
1537
 
    }
1538
 
}
1539
 
 
1540
 
template <class T>
1541
 
template <class S>
1542
 
const Matrix33<T> &
1543
 
Matrix33<T>::setRotation (S r)
1544
 
{
1545
 
    S cos_r, sin_r;
1546
 
 
1547
 
    cos_r = Math<T>::cos (r);
1548
 
    sin_r = Math<T>::sin (r);
1549
 
 
1550
 
    x[0][0] =  cos_r;
1551
 
    x[0][1] =  sin_r;
1552
 
    x[0][2] =  0;
1553
 
 
1554
 
    x[1][0] =  -sin_r;
1555
 
    x[1][1] =  cos_r;
1556
 
    x[1][2] =  0;
1557
 
 
1558
 
    x[2][0] =  0;
1559
 
    x[2][1] =  0;
1560
 
    x[2][2] =  1;
1561
 
 
1562
 
    return *this;
1563
 
}
1564
 
 
1565
 
template <class T>
1566
 
template <class S>
1567
 
const Matrix33<T> &
1568
 
Matrix33<T>::rotate (S r)
1569
 
{
1570
 
    *this *= Matrix33<T>().setRotation (r);
1571
 
    return *this;
1572
 
}
1573
 
 
1574
 
template <class T>
1575
 
const Matrix33<T> &
1576
 
Matrix33<T>::setScale (T s)
1577
 
{
1578
 
    x[0][0] = s;
1579
 
    x[0][1] = 0;
1580
 
    x[0][2] = 0;
1581
 
 
1582
 
    x[1][0] = 0;
1583
 
    x[1][1] = s;
1584
 
    x[1][2] = 0;
1585
 
 
1586
 
    x[2][0] = 0;
1587
 
    x[2][1] = 0;
1588
 
    x[2][2] = 1;
1589
 
 
1590
 
    return *this;
1591
 
}
1592
 
 
1593
 
template <class T>
1594
 
template <class S>
1595
 
const Matrix33<T> &
1596
 
Matrix33<T>::setScale (const Vec2<S> &s)
1597
 
{
1598
 
    x[0][0] = s[0];
1599
 
    x[0][1] = 0;
1600
 
    x[0][2] = 0;
1601
 
 
1602
 
    x[1][0] = 0;
1603
 
    x[1][1] = s[1];
1604
 
    x[1][2] = 0;
1605
 
 
1606
 
    x[2][0] = 0;
1607
 
    x[2][1] = 0;
1608
 
    x[2][2] = 1;
1609
 
 
1610
 
    return *this;
1611
 
}
1612
 
 
1613
 
template <class T>
1614
 
template <class S>
1615
 
const Matrix33<T> &
1616
 
Matrix33<T>::scale (const Vec2<S> &s)
1617
 
{
1618
 
    x[0][0] *= s[0];
1619
 
    x[0][1] *= s[0];
1620
 
    x[0][2] *= s[0];
1621
 
 
1622
 
    x[1][0] *= s[1];
1623
 
    x[1][1] *= s[1];
1624
 
    x[1][2] *= s[1];
1625
 
 
1626
 
    return *this;
1627
 
}
1628
 
 
1629
 
template <class T>
1630
 
template <class S>
1631
 
const Matrix33<T> &
1632
 
Matrix33<T>::setTranslation (const Vec2<S> &t)
1633
 
{
1634
 
    x[0][0] = 1;
1635
 
    x[0][1] = 0;
1636
 
    x[0][2] = 0;
1637
 
 
1638
 
    x[1][0] = 0;
1639
 
    x[1][1] = 1;
1640
 
    x[1][2] = 0;
1641
 
 
1642
 
    x[2][0] = t[0];
1643
 
    x[2][1] = t[1];
1644
 
    x[2][2] = 1;
1645
 
 
1646
 
    return *this;
1647
 
}
1648
 
 
1649
 
template <class T>
1650
 
inline Vec2<T> 
1651
 
Matrix33<T>::translation () const
1652
 
{
1653
 
    return Vec2<T> (x[2][0], x[2][1]);
1654
 
}
1655
 
 
1656
 
template <class T>
1657
 
template <class S>
1658
 
const Matrix33<T> &
1659
 
Matrix33<T>::translate (const Vec2<S> &t)
1660
 
{
1661
 
    x[2][0] += t[0] * x[0][0] + t[1] * x[1][0];
1662
 
    x[2][1] += t[0] * x[0][1] + t[1] * x[1][1];
1663
 
    x[2][2] += t[0] * x[0][2] + t[1] * x[1][2];
1664
 
 
1665
 
    return *this;
1666
 
}
1667
 
 
1668
 
template <class T>
1669
 
template <class S>
1670
 
const Matrix33<T> &
1671
 
Matrix33<T>::setShear (const S &xy)
1672
 
{
1673
 
    x[0][0] = 1;
1674
 
    x[0][1] = 0;
1675
 
    x[0][2] = 0;
1676
 
 
1677
 
    x[1][0] = xy;
1678
 
    x[1][1] = 1;
1679
 
    x[1][2] = 0;
1680
 
 
1681
 
    x[2][0] = 0;
1682
 
    x[2][1] = 0;
1683
 
    x[2][2] = 1;
1684
 
 
1685
 
    return *this;
1686
 
}
1687
 
 
1688
 
template <class T>
1689
 
template <class S>
1690
 
const Matrix33<T> &
1691
 
Matrix33<T>::setShear (const Vec2<S> &h)
1692
 
{
1693
 
    x[0][0] = 1;
1694
 
    x[0][1] = h[1];
1695
 
    x[0][2] = 0;
1696
 
 
1697
 
    x[1][0] = h[0];
1698
 
    x[1][1] = 1;
1699
 
    x[1][2] = 0;
1700
 
 
1701
 
    x[2][0] = 0;
1702
 
    x[2][1] = 0;
1703
 
    x[2][2] = 1;
1704
 
 
1705
 
    return *this;
1706
 
}
1707
 
 
1708
 
template <class T>
1709
 
template <class S>
1710
 
const Matrix33<T> &
1711
 
Matrix33<T>::shear (const S &xy)
1712
 
{
1713
 
    //
1714
 
    // In this case, we don't need a temp. copy of the matrix 
1715
 
    // because we never use a value on the RHS after we've 
1716
 
    // changed it on the LHS.
1717
 
    // 
1718
 
 
1719
 
    x[1][0] += xy * x[0][0];
1720
 
    x[1][1] += xy * x[0][1];
1721
 
    x[1][2] += xy * x[0][2];
1722
 
 
1723
 
    return *this;
1724
 
}
1725
 
 
1726
 
template <class T>
1727
 
template <class S>
1728
 
const Matrix33<T> &
1729
 
Matrix33<T>::shear (const Vec2<S> &h)
1730
 
{
1731
 
    Matrix33<T> P (*this);
1732
 
    
1733
 
    x[0][0] = P[0][0] + h[1] * P[1][0];
1734
 
    x[0][1] = P[0][1] + h[1] * P[1][1];
1735
 
    x[0][2] = P[0][2] + h[1] * P[1][2];
1736
 
    
1737
 
    x[1][0] = P[1][0] + h[0] * P[0][0];
1738
 
    x[1][1] = P[1][1] + h[0] * P[0][1];
1739
 
    x[1][2] = P[1][2] + h[0] * P[0][2];
1740
 
 
1741
 
    return *this;
1742
 
}
1743
 
 
1744
 
 
1745
 
//---------------------------
1746
 
// Implementation of Matrix44
1747
 
//---------------------------
1748
 
 
1749
 
template <class T>
1750
 
inline T *
1751
 
Matrix44<T>::operator [] (int i)
1752
 
{
1753
 
    return x[i];
1754
 
}
1755
 
 
1756
 
template <class T>
1757
 
inline const T *
1758
 
Matrix44<T>::operator [] (int i) const
1759
 
{
1760
 
    return x[i];
1761
 
}
1762
 
 
1763
 
template <class T>
1764
 
inline
1765
 
Matrix44<T>::Matrix44 ()
1766
 
{
1767
 
    x[0][0] = 1;
1768
 
    x[0][1] = 0;
1769
 
    x[0][2] = 0;
1770
 
    x[0][3] = 0;
1771
 
    x[1][0] = 0;
1772
 
    x[1][1] = 1;
1773
 
    x[1][2] = 0;
1774
 
    x[1][3] = 0;
1775
 
    x[2][0] = 0;
1776
 
    x[2][1] = 0;
1777
 
    x[2][2] = 1;
1778
 
    x[2][3] = 0;
1779
 
    x[3][0] = 0;
1780
 
    x[3][1] = 0;
1781
 
    x[3][2] = 0;
1782
 
    x[3][3] = 1;
1783
 
}
1784
 
 
1785
 
template <class T>
1786
 
inline
1787
 
Matrix44<T>::Matrix44 (T a)
1788
 
{
1789
 
    x[0][0] = a;
1790
 
    x[0][1] = a;
1791
 
    x[0][2] = a;
1792
 
    x[0][3] = a;
1793
 
    x[1][0] = a;
1794
 
    x[1][1] = a;
1795
 
    x[1][2] = a;
1796
 
    x[1][3] = a;
1797
 
    x[2][0] = a;
1798
 
    x[2][1] = a;
1799
 
    x[2][2] = a;
1800
 
    x[2][3] = a;
1801
 
    x[3][0] = a;
1802
 
    x[3][1] = a;
1803
 
    x[3][2] = a;
1804
 
    x[3][3] = a;
1805
 
}
1806
 
 
1807
 
template <class T>
1808
 
inline
1809
 
Matrix44<T>::Matrix44 (const T a[4][4]) 
1810
 
{
1811
 
    x[0][0] = a[0][0];
1812
 
    x[0][1] = a[0][1];
1813
 
    x[0][2] = a[0][2];
1814
 
    x[0][3] = a[0][3];
1815
 
    x[1][0] = a[1][0];
1816
 
    x[1][1] = a[1][1];
1817
 
    x[1][2] = a[1][2];
1818
 
    x[1][3] = a[1][3];
1819
 
    x[2][0] = a[2][0];
1820
 
    x[2][1] = a[2][1];
1821
 
    x[2][2] = a[2][2];
1822
 
    x[2][3] = a[2][3];
1823
 
    x[3][0] = a[3][0];
1824
 
    x[3][1] = a[3][1];
1825
 
    x[3][2] = a[3][2];
1826
 
    x[3][3] = a[3][3];
1827
 
}
1828
 
 
1829
 
template <class T>
1830
 
inline
1831
 
Matrix44<T>::Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h,
1832
 
                       T i, T j, T k, T l, T m, T n, T o, T p)
1833
 
{
1834
 
    x[0][0] = a;
1835
 
    x[0][1] = b;
1836
 
    x[0][2] = c;
1837
 
    x[0][3] = d;
1838
 
    x[1][0] = e;
1839
 
    x[1][1] = f;
1840
 
    x[1][2] = g;
1841
 
    x[1][3] = h;
1842
 
    x[2][0] = i;
1843
 
    x[2][1] = j;
1844
 
    x[2][2] = k;
1845
 
    x[2][3] = l;
1846
 
    x[3][0] = m;
1847
 
    x[3][1] = n;
1848
 
    x[3][2] = o;
1849
 
    x[3][3] = p;
1850
 
}
1851
 
 
1852
 
 
1853
 
template <class T>
1854
 
inline
1855
 
Matrix44<T>::Matrix44 (Matrix33<T> r, Vec3<T> t)
1856
 
{
1857
 
    x[0][0] = r[0][0];
1858
 
    x[0][1] = r[0][1];
1859
 
    x[0][2] = r[0][2];
1860
 
    x[0][3] = 0;
1861
 
    x[1][0] = r[1][0];
1862
 
    x[1][1] = r[1][1];
1863
 
    x[1][2] = r[1][2];
1864
 
    x[1][3] = 0;
1865
 
    x[2][0] = r[2][0];
1866
 
    x[2][1] = r[2][1];
1867
 
    x[2][2] = r[2][2];
1868
 
    x[2][3] = 0;
1869
 
    x[3][0] = t[0];
1870
 
    x[3][1] = t[1];
1871
 
    x[3][2] = t[2];
1872
 
    x[3][3] = 1;
1873
 
}
1874
 
 
1875
 
template <class T>
1876
 
inline
1877
 
Matrix44<T>::Matrix44 (const Matrix44 &v)
1878
 
{
1879
 
    x[0][0] = v.x[0][0];
1880
 
    x[0][1] = v.x[0][1];
1881
 
    x[0][2] = v.x[0][2];
1882
 
    x[0][3] = v.x[0][3];
1883
 
    x[1][0] = v.x[1][0];
1884
 
    x[1][1] = v.x[1][1];
1885
 
    x[1][2] = v.x[1][2];
1886
 
    x[1][3] = v.x[1][3];
1887
 
    x[2][0] = v.x[2][0];
1888
 
    x[2][1] = v.x[2][1];
1889
 
    x[2][2] = v.x[2][2];
1890
 
    x[2][3] = v.x[2][3];
1891
 
    x[3][0] = v.x[3][0];
1892
 
    x[3][1] = v.x[3][1];
1893
 
    x[3][2] = v.x[3][2];
1894
 
    x[3][3] = v.x[3][3];
1895
 
}
1896
 
 
1897
 
template <class T>
1898
 
inline const Matrix44<T> &
1899
 
Matrix44<T>::operator = (const Matrix44 &v)
1900
 
{
1901
 
    x[0][0] = v.x[0][0];
1902
 
    x[0][1] = v.x[0][1];
1903
 
    x[0][2] = v.x[0][2];
1904
 
    x[0][3] = v.x[0][3];
1905
 
    x[1][0] = v.x[1][0];
1906
 
    x[1][1] = v.x[1][1];
1907
 
    x[1][2] = v.x[1][2];
1908
 
    x[1][3] = v.x[1][3];
1909
 
    x[2][0] = v.x[2][0];
1910
 
    x[2][1] = v.x[2][1];
1911
 
    x[2][2] = v.x[2][2];
1912
 
    x[2][3] = v.x[2][3];
1913
 
    x[3][0] = v.x[3][0];
1914
 
    x[3][1] = v.x[3][1];
1915
 
    x[3][2] = v.x[3][2];
1916
 
    x[3][3] = v.x[3][3];
1917
 
    return *this;
1918
 
}
1919
 
 
1920
 
template <class T>
1921
 
inline const Matrix44<T> &
1922
 
Matrix44<T>::operator = (T a)
1923
 
{
1924
 
    x[0][0] = a;
1925
 
    x[0][1] = a;
1926
 
    x[0][2] = a;
1927
 
    x[0][3] = a;
1928
 
    x[1][0] = a;
1929
 
    x[1][1] = a;
1930
 
    x[1][2] = a;
1931
 
    x[1][3] = a;
1932
 
    x[2][0] = a;
1933
 
    x[2][1] = a;
1934
 
    x[2][2] = a;
1935
 
    x[2][3] = a;
1936
 
    x[3][0] = a;
1937
 
    x[3][1] = a;
1938
 
    x[3][2] = a;
1939
 
    x[3][3] = a;
1940
 
    return *this;
1941
 
}
1942
 
 
1943
 
template <class T>
1944
 
inline T *
1945
 
Matrix44<T>::getValue ()
1946
 
{
1947
 
    return (T *) &x[0][0];
1948
 
}
1949
 
 
1950
 
template <class T>
1951
 
inline const T *
1952
 
Matrix44<T>::getValue () const
1953
 
{
1954
 
    return (const T *) &x[0][0];
1955
 
}
1956
 
 
1957
 
template <class T>
1958
 
template <class S>
1959
 
inline void
1960
 
Matrix44<T>::getValue (Matrix44<S> &v) const
1961
 
{
1962
 
    v.x[0][0] = x[0][0];
1963
 
    v.x[0][1] = x[0][1];
1964
 
    v.x[0][2] = x[0][2];
1965
 
    v.x[0][3] = x[0][3];
1966
 
    v.x[1][0] = x[1][0];
1967
 
    v.x[1][1] = x[1][1];
1968
 
    v.x[1][2] = x[1][2];
1969
 
    v.x[1][3] = x[1][3];
1970
 
    v.x[2][0] = x[2][0];
1971
 
    v.x[2][1] = x[2][1];
1972
 
    v.x[2][2] = x[2][2];
1973
 
    v.x[2][3] = x[2][3];
1974
 
    v.x[3][0] = x[3][0];
1975
 
    v.x[3][1] = x[3][1];
1976
 
    v.x[3][2] = x[3][2];
1977
 
    v.x[3][3] = x[3][3];
1978
 
}
1979
 
 
1980
 
template <class T>
1981
 
template <class S>
1982
 
inline Matrix44<T> &
1983
 
Matrix44<T>::setValue (const Matrix44<S> &v)
1984
 
{
1985
 
    x[0][0] = v.x[0][0];
1986
 
    x[0][1] = v.x[0][1];
1987
 
    x[0][2] = v.x[0][2];
1988
 
    x[0][3] = v.x[0][3];
1989
 
    x[1][0] = v.x[1][0];
1990
 
    x[1][1] = v.x[1][1];
1991
 
    x[1][2] = v.x[1][2];
1992
 
    x[1][3] = v.x[1][3];
1993
 
    x[2][0] = v.x[2][0];
1994
 
    x[2][1] = v.x[2][1];
1995
 
    x[2][2] = v.x[2][2];
1996
 
    x[2][3] = v.x[2][3];
1997
 
    x[3][0] = v.x[3][0];
1998
 
    x[3][1] = v.x[3][1];
1999
 
    x[3][2] = v.x[3][2];
2000
 
    x[3][3] = v.x[3][3];
2001
 
    return *this;
2002
 
}
2003
 
 
2004
 
template <class T>
2005
 
template <class S>
2006
 
inline Matrix44<T> &
2007
 
Matrix44<T>::setTheMatrix (const Matrix44<S> &v)
2008
 
{
2009
 
    x[0][0] = v.x[0][0];
2010
 
    x[0][1] = v.x[0][1];
2011
 
    x[0][2] = v.x[0][2];
2012
 
    x[0][3] = v.x[0][3];
2013
 
    x[1][0] = v.x[1][0];
2014
 
    x[1][1] = v.x[1][1];
2015
 
    x[1][2] = v.x[1][2];
2016
 
    x[1][3] = v.x[1][3];
2017
 
    x[2][0] = v.x[2][0];
2018
 
    x[2][1] = v.x[2][1];
2019
 
    x[2][2] = v.x[2][2];
2020
 
    x[2][3] = v.x[2][3];
2021
 
    x[3][0] = v.x[3][0];
2022
 
    x[3][1] = v.x[3][1];
2023
 
    x[3][2] = v.x[3][2];
2024
 
    x[3][3] = v.x[3][3];
2025
 
    return *this;
2026
 
}
2027
 
 
2028
 
template <class T>
2029
 
inline void
2030
 
Matrix44<T>::makeIdentity()
2031
 
{
2032
 
    x[0][0] = 1;
2033
 
    x[0][1] = 0;
2034
 
    x[0][2] = 0;
2035
 
    x[0][3] = 0;
2036
 
    x[1][0] = 0;
2037
 
    x[1][1] = 1;
2038
 
    x[1][2] = 0;
2039
 
    x[1][3] = 0;
2040
 
    x[2][0] = 0;
2041
 
    x[2][1] = 0;
2042
 
    x[2][2] = 1;
2043
 
    x[2][3] = 0;
2044
 
    x[3][0] = 0;
2045
 
    x[3][1] = 0;
2046
 
    x[3][2] = 0;
2047
 
    x[3][3] = 1;
2048
 
}
2049
 
 
2050
 
template <class T>
2051
 
bool
2052
 
Matrix44<T>::operator == (const Matrix44 &v) const
2053
 
{
2054
 
    return x[0][0] == v.x[0][0] &&
2055
 
           x[0][1] == v.x[0][1] &&
2056
 
           x[0][2] == v.x[0][2] &&
2057
 
           x[0][3] == v.x[0][3] &&
2058
 
           x[1][0] == v.x[1][0] &&
2059
 
           x[1][1] == v.x[1][1] &&
2060
 
           x[1][2] == v.x[1][2] &&
2061
 
           x[1][3] == v.x[1][3] &&
2062
 
           x[2][0] == v.x[2][0] &&
2063
 
           x[2][1] == v.x[2][1] &&
2064
 
           x[2][2] == v.x[2][2] &&
2065
 
           x[2][3] == v.x[2][3] &&
2066
 
           x[3][0] == v.x[3][0] &&
2067
 
           x[3][1] == v.x[3][1] &&
2068
 
           x[3][2] == v.x[3][2] &&
2069
 
           x[3][3] == v.x[3][3];
2070
 
}
2071
 
 
2072
 
template <class T>
2073
 
bool
2074
 
Matrix44<T>::operator != (const Matrix44 &v) const
2075
 
{
2076
 
    return x[0][0] != v.x[0][0] ||
2077
 
           x[0][1] != v.x[0][1] ||
2078
 
           x[0][2] != v.x[0][2] ||
2079
 
           x[0][3] != v.x[0][3] ||
2080
 
           x[1][0] != v.x[1][0] ||
2081
 
           x[1][1] != v.x[1][1] ||
2082
 
           x[1][2] != v.x[1][2] ||
2083
 
           x[1][3] != v.x[1][3] ||
2084
 
           x[2][0] != v.x[2][0] ||
2085
 
           x[2][1] != v.x[2][1] ||
2086
 
           x[2][2] != v.x[2][2] ||
2087
 
           x[2][3] != v.x[2][3] ||
2088
 
           x[3][0] != v.x[3][0] ||
2089
 
           x[3][1] != v.x[3][1] ||
2090
 
           x[3][2] != v.x[3][2] ||
2091
 
           x[3][3] != v.x[3][3];
2092
 
}
2093
 
 
2094
 
template <class T>
2095
 
bool
2096
 
Matrix44<T>::equalWithAbsError (const Matrix44<T> &m, T e) const
2097
 
{
2098
 
    for (int i = 0; i < 4; i++)
2099
 
        for (int j = 0; j < 4; j++)
2100
 
            if (!Imath::equalWithAbsError ((*this)[i][j], m[i][j], e))
2101
 
                return false;
2102
 
 
2103
 
    return true;
2104
 
}
2105
 
 
2106
 
template <class T>
2107
 
bool
2108
 
Matrix44<T>::equalWithRelError (const Matrix44<T> &m, T e) const
2109
 
{
2110
 
    for (int i = 0; i < 4; i++)
2111
 
        for (int j = 0; j < 4; j++)
2112
 
            if (!Imath::equalWithRelError ((*this)[i][j], m[i][j], e))
2113
 
                return false;
2114
 
 
2115
 
    return true;
2116
 
}
2117
 
 
2118
 
template <class T>
2119
 
const Matrix44<T> &
2120
 
Matrix44<T>::operator += (const Matrix44<T> &v)
2121
 
{
2122
 
    x[0][0] += v.x[0][0];
2123
 
    x[0][1] += v.x[0][1];
2124
 
    x[0][2] += v.x[0][2];
2125
 
    x[0][3] += v.x[0][3];
2126
 
    x[1][0] += v.x[1][0];
2127
 
    x[1][1] += v.x[1][1];
2128
 
    x[1][2] += v.x[1][2];
2129
 
    x[1][3] += v.x[1][3];
2130
 
    x[2][0] += v.x[2][0];
2131
 
    x[2][1] += v.x[2][1];
2132
 
    x[2][2] += v.x[2][2];
2133
 
    x[2][3] += v.x[2][3];
2134
 
    x[3][0] += v.x[3][0];
2135
 
    x[3][1] += v.x[3][1];
2136
 
    x[3][2] += v.x[3][2];
2137
 
    x[3][3] += v.x[3][3];
2138
 
 
2139
 
    return *this;
2140
 
}
2141
 
 
2142
 
template <class T>
2143
 
const Matrix44<T> &
2144
 
Matrix44<T>::operator += (T a)
2145
 
{
2146
 
    x[0][0] += a;
2147
 
    x[0][1] += a;
2148
 
    x[0][2] += a;
2149
 
    x[0][3] += a;
2150
 
    x[1][0] += a;
2151
 
    x[1][1] += a;
2152
 
    x[1][2] += a;
2153
 
    x[1][3] += a;
2154
 
    x[2][0] += a;
2155
 
    x[2][1] += a;
2156
 
    x[2][2] += a;
2157
 
    x[2][3] += a;
2158
 
    x[3][0] += a;
2159
 
    x[3][1] += a;
2160
 
    x[3][2] += a;
2161
 
    x[3][3] += a;
2162
 
 
2163
 
    return *this;
2164
 
}
2165
 
 
2166
 
template <class T>
2167
 
Matrix44<T>
2168
 
Matrix44<T>::operator + (const Matrix44<T> &v) const
2169
 
{
2170
 
    return Matrix44 (x[0][0] + v.x[0][0],
2171
 
                     x[0][1] + v.x[0][1],
2172
 
                     x[0][2] + v.x[0][2],
2173
 
                     x[0][3] + v.x[0][3],
2174
 
                     x[1][0] + v.x[1][0],
2175
 
                     x[1][1] + v.x[1][1],
2176
 
                     x[1][2] + v.x[1][2],
2177
 
                     x[1][3] + v.x[1][3],
2178
 
                     x[2][0] + v.x[2][0],
2179
 
                     x[2][1] + v.x[2][1],
2180
 
                     x[2][2] + v.x[2][2],
2181
 
                     x[2][3] + v.x[2][3],
2182
 
                     x[3][0] + v.x[3][0],
2183
 
                     x[3][1] + v.x[3][1],
2184
 
                     x[3][2] + v.x[3][2],
2185
 
                     x[3][3] + v.x[3][3]);
2186
 
}
2187
 
 
2188
 
template <class T>
2189
 
const Matrix44<T> &
2190
 
Matrix44<T>::operator -= (const Matrix44<T> &v)
2191
 
{
2192
 
    x[0][0] -= v.x[0][0];
2193
 
    x[0][1] -= v.x[0][1];
2194
 
    x[0][2] -= v.x[0][2];
2195
 
    x[0][3] -= v.x[0][3];
2196
 
    x[1][0] -= v.x[1][0];
2197
 
    x[1][1] -= v.x[1][1];
2198
 
    x[1][2] -= v.x[1][2];
2199
 
    x[1][3] -= v.x[1][3];
2200
 
    x[2][0] -= v.x[2][0];
2201
 
    x[2][1] -= v.x[2][1];
2202
 
    x[2][2] -= v.x[2][2];
2203
 
    x[2][3] -= v.x[2][3];
2204
 
    x[3][0] -= v.x[3][0];
2205
 
    x[3][1] -= v.x[3][1];
2206
 
    x[3][2] -= v.x[3][2];
2207
 
    x[3][3] -= v.x[3][3];
2208
 
 
2209
 
    return *this;
2210
 
}
2211
 
 
2212
 
template <class T>
2213
 
const Matrix44<T> &
2214
 
Matrix44<T>::operator -= (T a)
2215
 
{
2216
 
    x[0][0] -= a;
2217
 
    x[0][1] -= a;
2218
 
    x[0][2] -= a;
2219
 
    x[0][3] -= a;
2220
 
    x[1][0] -= a;
2221
 
    x[1][1] -= a;
2222
 
    x[1][2] -= a;
2223
 
    x[1][3] -= a;
2224
 
    x[2][0] -= a;
2225
 
    x[2][1] -= a;
2226
 
    x[2][2] -= a;
2227
 
    x[2][3] -= a;
2228
 
    x[3][0] -= a;
2229
 
    x[3][1] -= a;
2230
 
    x[3][2] -= a;
2231
 
    x[3][3] -= a;
2232
 
 
2233
 
    return *this;
2234
 
}
2235
 
 
2236
 
template <class T>
2237
 
Matrix44<T>
2238
 
Matrix44<T>::operator - (const Matrix44<T> &v) const
2239
 
{
2240
 
    return Matrix44 (x[0][0] - v.x[0][0],
2241
 
                     x[0][1] - v.x[0][1],
2242
 
                     x[0][2] - v.x[0][2],
2243
 
                     x[0][3] - v.x[0][3],
2244
 
                     x[1][0] - v.x[1][0],
2245
 
                     x[1][1] - v.x[1][1],
2246
 
                     x[1][2] - v.x[1][2],
2247
 
                     x[1][3] - v.x[1][3],
2248
 
                     x[2][0] - v.x[2][0],
2249
 
                     x[2][1] - v.x[2][1],
2250
 
                     x[2][2] - v.x[2][2],
2251
 
                     x[2][3] - v.x[2][3],
2252
 
                     x[3][0] - v.x[3][0],
2253
 
                     x[3][1] - v.x[3][1],
2254
 
                     x[3][2] - v.x[3][2],
2255
 
                     x[3][3] - v.x[3][3]);
2256
 
}
2257
 
 
2258
 
template <class T>
2259
 
Matrix44<T>
2260
 
Matrix44<T>::operator - () const
2261
 
{
2262
 
    return Matrix44 (-x[0][0],
2263
 
                     -x[0][1],
2264
 
                     -x[0][2],
2265
 
                     -x[0][3],
2266
 
                     -x[1][0],
2267
 
                     -x[1][1],
2268
 
                     -x[1][2],
2269
 
                     -x[1][3],
2270
 
                     -x[2][0],
2271
 
                     -x[2][1],
2272
 
                     -x[2][2],
2273
 
                     -x[2][3],
2274
 
                     -x[3][0],
2275
 
                     -x[3][1],
2276
 
                     -x[3][2],
2277
 
                     -x[3][3]);
2278
 
}
2279
 
 
2280
 
template <class T>
2281
 
const Matrix44<T> &
2282
 
Matrix44<T>::negate ()
2283
 
{
2284
 
    x[0][0] = -x[0][0];
2285
 
    x[0][1] = -x[0][1];
2286
 
    x[0][2] = -x[0][2];
2287
 
    x[0][3] = -x[0][3];
2288
 
    x[1][0] = -x[1][0];
2289
 
    x[1][1] = -x[1][1];
2290
 
    x[1][2] = -x[1][2];
2291
 
    x[1][3] = -x[1][3];
2292
 
    x[2][0] = -x[2][0];
2293
 
    x[2][1] = -x[2][1];
2294
 
    x[2][2] = -x[2][2];
2295
 
    x[2][3] = -x[2][3];
2296
 
    x[3][0] = -x[3][0];
2297
 
    x[3][1] = -x[3][1];
2298
 
    x[3][2] = -x[3][2];
2299
 
    x[3][3] = -x[3][3];
2300
 
 
2301
 
    return *this;
2302
 
}
2303
 
 
2304
 
template <class T>
2305
 
const Matrix44<T> &
2306
 
Matrix44<T>::operator *= (T a)
2307
 
{
2308
 
    x[0][0] *= a;
2309
 
    x[0][1] *= a;
2310
 
    x[0][2] *= a;
2311
 
    x[0][3] *= a;
2312
 
    x[1][0] *= a;
2313
 
    x[1][1] *= a;
2314
 
    x[1][2] *= a;
2315
 
    x[1][3] *= a;
2316
 
    x[2][0] *= a;
2317
 
    x[2][1] *= a;
2318
 
    x[2][2] *= a;
2319
 
    x[2][3] *= a;
2320
 
    x[3][0] *= a;
2321
 
    x[3][1] *= a;
2322
 
    x[3][2] *= a;
2323
 
    x[3][3] *= a;
2324
 
 
2325
 
    return *this;
2326
 
}
2327
 
 
2328
 
template <class T>
2329
 
Matrix44<T>
2330
 
Matrix44<T>::operator * (T a) const
2331
 
{
2332
 
    return Matrix44 (x[0][0] * a,
2333
 
                     x[0][1] * a,
2334
 
                     x[0][2] * a,
2335
 
                     x[0][3] * a,
2336
 
                     x[1][0] * a,
2337
 
                     x[1][1] * a,
2338
 
                     x[1][2] * a,
2339
 
                     x[1][3] * a,
2340
 
                     x[2][0] * a,
2341
 
                     x[2][1] * a,
2342
 
                     x[2][2] * a,
2343
 
                     x[2][3] * a,
2344
 
                     x[3][0] * a,
2345
 
                     x[3][1] * a,
2346
 
                     x[3][2] * a,
2347
 
                     x[3][3] * a);
2348
 
}
2349
 
 
2350
 
template <class T>
2351
 
inline Matrix44<T>
2352
 
operator * (T a, const Matrix44<T> &v)
2353
 
{
2354
 
    return v * a;
2355
 
}
2356
 
 
2357
 
template <class T>
2358
 
inline const Matrix44<T> &
2359
 
Matrix44<T>::operator *= (const Matrix44<T> &v)
2360
 
{
2361
 
    Matrix44 tmp (T (0));
2362
 
 
2363
 
    multiply (*this, v, tmp);
2364
 
    *this = tmp;
2365
 
    return *this;
2366
 
}
2367
 
 
2368
 
template <class T>
2369
 
inline Matrix44<T>
2370
 
Matrix44<T>::operator * (const Matrix44<T> &v) const
2371
 
{
2372
 
    Matrix44 tmp (T (0));
2373
 
 
2374
 
    multiply (*this, v, tmp);
2375
 
    return tmp;
2376
 
}
2377
 
 
2378
 
template <class T>
2379
 
void
2380
 
Matrix44<T>::multiply (const Matrix44<T> &a,
2381
 
                       const Matrix44<T> &b,
2382
 
                       Matrix44<T> &c)
2383
 
{
2384
 
    register const T * restrict ap = &a.x[0][0];
2385
 
    register const T * restrict bp = &b.x[0][0];
2386
 
    register       T * restrict cp = &c.x[0][0];
2387
 
 
2388
 
    register T a0, a1, a2, a3;
2389
 
 
2390
 
    a0 = ap[0];
2391
 
    a1 = ap[1];
2392
 
    a2 = ap[2];
2393
 
    a3 = ap[3];
2394
 
 
2395
 
    cp[0]  = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
2396
 
    cp[1]  = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
2397
 
    cp[2]  = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
2398
 
    cp[3]  = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
2399
 
 
2400
 
    a0 = ap[4];
2401
 
    a1 = ap[5];
2402
 
    a2 = ap[6];
2403
 
    a3 = ap[7];
2404
 
 
2405
 
    cp[4]  = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
2406
 
    cp[5]  = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
2407
 
    cp[6]  = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
2408
 
    cp[7]  = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
2409
 
 
2410
 
    a0 = ap[8];
2411
 
    a1 = ap[9];
2412
 
    a2 = ap[10];
2413
 
    a3 = ap[11];
2414
 
 
2415
 
    cp[8]  = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
2416
 
    cp[9]  = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
2417
 
    cp[10] = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
2418
 
    cp[11] = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
2419
 
 
2420
 
    a0 = ap[12];
2421
 
    a1 = ap[13];
2422
 
    a2 = ap[14];
2423
 
    a3 = ap[15];
2424
 
 
2425
 
    cp[12] = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
2426
 
    cp[13] = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
2427
 
    cp[14] = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
2428
 
    cp[15] = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
2429
 
}
2430
 
 
2431
 
template <class T> template <class S>
2432
 
void
2433
 
Matrix44<T>::multVecMatrix(const Vec3<S> &src, Vec3<S> &dst) const
2434
 
{
2435
 
    S a, b, c, w;
2436
 
 
2437
 
    a = src[0] * x[0][0] + src[1] * x[1][0] + src[2] * x[2][0] + x[3][0];
2438
 
    b = src[0] * x[0][1] + src[1] * x[1][1] + src[2] * x[2][1] + x[3][1];
2439
 
    c = src[0] * x[0][2] + src[1] * x[1][2] + src[2] * x[2][2] + x[3][2];
2440
 
    w = src[0] * x[0][3] + src[1] * x[1][3] + src[2] * x[2][3] + x[3][3];
2441
 
 
2442
 
    dst.x = a / w;
2443
 
    dst.y = b / w;
2444
 
    dst.z = c / w;
2445
 
}
2446
 
 
2447
 
template <class T> template <class S>
2448
 
void
2449
 
Matrix44<T>::multDirMatrix(const Vec3<S> &src, Vec3<S> &dst) const
2450
 
{
2451
 
    S a, b, c;
2452
 
 
2453
 
    a = src[0] * x[0][0] + src[1] * x[1][0] + src[2] * x[2][0];
2454
 
    b = src[0] * x[0][1] + src[1] * x[1][1] + src[2] * x[2][1];
2455
 
    c = src[0] * x[0][2] + src[1] * x[1][2] + src[2] * x[2][2];
2456
 
 
2457
 
    dst.x = a;
2458
 
    dst.y = b;
2459
 
    dst.z = c;
2460
 
}
2461
 
 
2462
 
template <class T>
2463
 
const Matrix44<T> &
2464
 
Matrix44<T>::operator /= (T a)
2465
 
{
2466
 
    x[0][0] /= a;
2467
 
    x[0][1] /= a;
2468
 
    x[0][2] /= a;
2469
 
    x[0][3] /= a;
2470
 
    x[1][0] /= a;
2471
 
    x[1][1] /= a;
2472
 
    x[1][2] /= a;
2473
 
    x[1][3] /= a;
2474
 
    x[2][0] /= a;
2475
 
    x[2][1] /= a;
2476
 
    x[2][2] /= a;
2477
 
    x[2][3] /= a;
2478
 
    x[3][0] /= a;
2479
 
    x[3][1] /= a;
2480
 
    x[3][2] /= a;
2481
 
    x[3][3] /= a;
2482
 
 
2483
 
    return *this;
2484
 
}
2485
 
 
2486
 
template <class T>
2487
 
Matrix44<T>
2488
 
Matrix44<T>::operator / (T a) const
2489
 
{
2490
 
    return Matrix44 (x[0][0] / a,
2491
 
                     x[0][1] / a,
2492
 
                     x[0][2] / a,
2493
 
                     x[0][3] / a,
2494
 
                     x[1][0] / a,
2495
 
                     x[1][1] / a,
2496
 
                     x[1][2] / a,
2497
 
                     x[1][3] / a,
2498
 
                     x[2][0] / a,
2499
 
                     x[2][1] / a,
2500
 
                     x[2][2] / a,
2501
 
                     x[2][3] / a,
2502
 
                     x[3][0] / a,
2503
 
                     x[3][1] / a,
2504
 
                     x[3][2] / a,
2505
 
                     x[3][3] / a);
2506
 
}
2507
 
 
2508
 
template <class T>
2509
 
const Matrix44<T> &
2510
 
Matrix44<T>::transpose ()
2511
 
{
2512
 
    Matrix44 tmp (x[0][0],
2513
 
                  x[1][0],
2514
 
                  x[2][0],
2515
 
                  x[3][0],
2516
 
                  x[0][1],
2517
 
                  x[1][1],
2518
 
                  x[2][1],
2519
 
                  x[3][1],
2520
 
                  x[0][2],
2521
 
                  x[1][2],
2522
 
                  x[2][2],
2523
 
                  x[3][2],
2524
 
                  x[0][3],
2525
 
                  x[1][3],
2526
 
                  x[2][3],
2527
 
                  x[3][3]);
2528
 
    *this = tmp;
2529
 
    return *this;
2530
 
}
2531
 
 
2532
 
template <class T>
2533
 
Matrix44<T>
2534
 
Matrix44<T>::transposed () const
2535
 
{
2536
 
    return Matrix44 (x[0][0],
2537
 
                     x[1][0],
2538
 
                     x[2][0],
2539
 
                     x[3][0],
2540
 
                     x[0][1],
2541
 
                     x[1][1],
2542
 
                     x[2][1],
2543
 
                     x[3][1],
2544
 
                     x[0][2],
2545
 
                     x[1][2],
2546
 
                     x[2][2],
2547
 
                     x[3][2],
2548
 
                     x[0][3],
2549
 
                     x[1][3],
2550
 
                     x[2][3],
2551
 
                     x[3][3]);
2552
 
}
2553
 
 
2554
 
template <class T>
2555
 
const Matrix44<T> &
2556
 
Matrix44<T>::gjInvert (bool singExc) throw (Iex::MathExc)
2557
 
{
2558
 
    *this = gjInverse (singExc);
2559
 
    return *this;
2560
 
}
2561
 
 
2562
 
template <class T>
2563
 
Matrix44<T>
2564
 
Matrix44<T>::gjInverse (bool singExc) const throw (Iex::MathExc)
2565
 
{
2566
 
    int i, j, k;
2567
 
    Matrix44 s;
2568
 
    Matrix44 t (*this);
2569
 
 
2570
 
    // Forward elimination
2571
 
 
2572
 
    for (i = 0; i < 3 ; i++)
2573
 
    {
2574
 
        int pivot = i;
2575
 
 
2576
 
        T pivotsize = t[i][i];
2577
 
 
2578
 
        if (pivotsize < 0)
2579
 
            pivotsize = -pivotsize;
2580
 
 
2581
 
        for (j = i + 1; j < 4; j++)
2582
 
        {
2583
 
            T tmp = t[j][i];
2584
 
 
2585
 
            if (tmp < 0)
2586
 
                tmp = -tmp;
2587
 
 
2588
 
            if (tmp > pivotsize)
2589
 
            {
2590
 
                pivot = j;
2591
 
                pivotsize = tmp;
2592
 
            }
2593
 
        }
2594
 
 
2595
 
        if (pivotsize == 0)
2596
 
        {
2597
 
            if (singExc)
2598
 
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
2599
 
 
2600
 
            return Matrix44();
2601
 
        }
2602
 
 
2603
 
        if (pivot != i)
2604
 
        {
2605
 
            for (j = 0; j < 4; j++)
2606
 
            {
2607
 
                T tmp;
2608
 
 
2609
 
                tmp = t[i][j];
2610
 
                t[i][j] = t[pivot][j];
2611
 
                t[pivot][j] = tmp;
2612
 
 
2613
 
                tmp = s[i][j];
2614
 
                s[i][j] = s[pivot][j];
2615
 
                s[pivot][j] = tmp;
2616
 
            }
2617
 
        }
2618
 
 
2619
 
        for (j = i + 1; j < 4; j++)
2620
 
        {
2621
 
            T f = t[j][i] / t[i][i];
2622
 
 
2623
 
            for (k = 0; k < 4; k++)
2624
 
            {
2625
 
                t[j][k] -= f * t[i][k];
2626
 
                s[j][k] -= f * s[i][k];
2627
 
            }
2628
 
        }
2629
 
    }
2630
 
 
2631
 
    // Backward substitution
2632
 
 
2633
 
    for (i = 3; i >= 0; --i)
2634
 
    {
2635
 
        T f;
2636
 
 
2637
 
        if ((f = t[i][i]) == 0)
2638
 
        {
2639
 
            if (singExc)
2640
 
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
2641
 
 
2642
 
            return Matrix44();
2643
 
        }
2644
 
 
2645
 
        for (j = 0; j < 4; j++)
2646
 
        {
2647
 
            t[i][j] /= f;
2648
 
            s[i][j] /= f;
2649
 
        }
2650
 
 
2651
 
        for (j = 0; j < i; j++)
2652
 
        {
2653
 
            f = t[j][i];
2654
 
 
2655
 
            for (k = 0; k < 4; k++)
2656
 
            {
2657
 
                t[j][k] -= f * t[i][k];
2658
 
                s[j][k] -= f * s[i][k];
2659
 
            }
2660
 
        }
2661
 
    }
2662
 
 
2663
 
    return s;
2664
 
}
2665
 
 
2666
 
template <class T>
2667
 
const Matrix44<T> &
2668
 
Matrix44<T>::invert (bool singExc) throw (Iex::MathExc)
2669
 
{
2670
 
    *this = inverse (singExc);
2671
 
    return *this;
2672
 
}
2673
 
 
2674
 
template <class T>
2675
 
Matrix44<T>
2676
 
Matrix44<T>::inverse (bool singExc) const throw (Iex::MathExc)
2677
 
{
2678
 
    if (x[0][3] != 0 || x[1][3] != 0 || x[2][3] != 0 || x[3][3] != 1)
2679
 
        return gjInverse(singExc);
2680
 
 
2681
 
    Matrix44 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
2682
 
                x[2][1] * x[0][2] - x[0][1] * x[2][2],
2683
 
                x[0][1] * x[1][2] - x[1][1] * x[0][2],
2684
 
                0,
2685
 
 
2686
 
                x[2][0] * x[1][2] - x[1][0] * x[2][2],
2687
 
                x[0][0] * x[2][2] - x[2][0] * x[0][2],
2688
 
                x[1][0] * x[0][2] - x[0][0] * x[1][2],
2689
 
                0,
2690
 
 
2691
 
                x[1][0] * x[2][1] - x[2][0] * x[1][1],
2692
 
                x[2][0] * x[0][1] - x[0][0] * x[2][1],
2693
 
                x[0][0] * x[1][1] - x[1][0] * x[0][1],
2694
 
                0,
2695
 
 
2696
 
                0,
2697
 
                0,
2698
 
                0,
2699
 
                1);
2700
 
 
2701
 
    T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
2702
 
 
2703
 
    if (Imath::abs (r) >= 1)
2704
 
    {
2705
 
        for (int i = 0; i < 3; ++i)
2706
 
        {
2707
 
            for (int j = 0; j < 3; ++j)
2708
 
            {
2709
 
                s[i][j] /= r;
2710
 
            }
2711
 
        }
2712
 
    }
2713
 
    else
2714
 
    {
2715
 
        T mr = Imath::abs (r) / limits<T>::smallest();
2716
 
 
2717
 
        for (int i = 0; i < 3; ++i)
2718
 
        {
2719
 
            for (int j = 0; j < 3; ++j)
2720
 
            {
2721
 
                if (mr > Imath::abs (s[i][j]))
2722
 
                {
2723
 
                    s[i][j] /= r;
2724
 
                }
2725
 
                else
2726
 
                {
2727
 
                    if (singExc)
2728
 
                        throw SingMatrixExc ("Cannot invert singular matrix.");
2729
 
 
2730
 
                    return Matrix44();
2731
 
                }
2732
 
            }
2733
 
        }
2734
 
    }
2735
 
 
2736
 
    s[3][0] = -x[3][0] * s[0][0] - x[3][1] * s[1][0] - x[3][2] * s[2][0];
2737
 
    s[3][1] = -x[3][0] * s[0][1] - x[3][1] * s[1][1] - x[3][2] * s[2][1];
2738
 
    s[3][2] = -x[3][0] * s[0][2] - x[3][1] * s[1][2] - x[3][2] * s[2][2];
2739
 
 
2740
 
    return s;
2741
 
}
2742
 
 
2743
 
template <class T>
2744
 
template <class S>
2745
 
const Matrix44<T> &
2746
 
Matrix44<T>::setEulerAngles (const Vec3<S>& r)
2747
 
{
2748
 
    S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
2749
 
    
2750
 
    cos_rz = Math<T>::cos (r[2]);
2751
 
    cos_ry = Math<T>::cos (r[1]);
2752
 
    cos_rx = Math<T>::cos (r[0]);
2753
 
    
2754
 
    sin_rz = Math<T>::sin (r[2]);
2755
 
    sin_ry = Math<T>::sin (r[1]);
2756
 
    sin_rx = Math<T>::sin (r[0]);
2757
 
    
2758
 
    x[0][0] =  cos_rz * cos_ry;
2759
 
    x[0][1] =  sin_rz * cos_ry;
2760
 
    x[0][2] = -sin_ry;
2761
 
    x[0][3] =  0;
2762
 
    
2763
 
    x[1][0] = -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx;
2764
 
    x[1][1] =  cos_rz * cos_rx + sin_rz * sin_ry * sin_rx;
2765
 
    x[1][2] =  cos_ry * sin_rx;
2766
 
    x[1][3] =  0;
2767
 
    
2768
 
    x[2][0] =  sin_rz * sin_rx + cos_rz * sin_ry * cos_rx;
2769
 
    x[2][1] = -cos_rz * sin_rx + sin_rz * sin_ry * cos_rx;
2770
 
    x[2][2] =  cos_ry * cos_rx;
2771
 
    x[2][3] =  0;
2772
 
 
2773
 
    x[3][0] =  0;
2774
 
    x[3][1] =  0;
2775
 
    x[3][2] =  0;
2776
 
    x[3][3] =  1;
2777
 
 
2778
 
    return *this;
2779
 
}
2780
 
 
2781
 
template <class T>
2782
 
template <class S>
2783
 
const Matrix44<T> &
2784
 
Matrix44<T>::setAxisAngle (const Vec3<S>& axis, S angle)
2785
 
{
2786
 
    Vec3<S> unit (axis.normalized());
2787
 
    S sine   = Math<T>::sin (angle);
2788
 
    S cosine = Math<T>::cos (angle);
2789
 
 
2790
 
    x[0][0] = unit[0] * unit[0] * (1 - cosine) + cosine;
2791
 
    x[0][1] = unit[0] * unit[1] * (1 - cosine) + unit[2] * sine;
2792
 
    x[0][2] = unit[0] * unit[2] * (1 - cosine) - unit[1] * sine;
2793
 
    x[0][3] = 0;
2794
 
 
2795
 
    x[1][0] = unit[0] * unit[1] * (1 - cosine) - unit[2] * sine;
2796
 
    x[1][1] = unit[1] * unit[1] * (1 - cosine) + cosine;
2797
 
    x[1][2] = unit[1] * unit[2] * (1 - cosine) + unit[0] * sine;
2798
 
    x[1][3] = 0;
2799
 
 
2800
 
    x[2][0] = unit[0] * unit[2] * (1 - cosine) + unit[1] * sine;
2801
 
    x[2][1] = unit[1] * unit[2] * (1 - cosine) - unit[0] * sine;
2802
 
    x[2][2] = unit[2] * unit[2] * (1 - cosine) + cosine;
2803
 
    x[2][3] = 0;
2804
 
 
2805
 
    x[3][0] = 0;
2806
 
    x[3][1] = 0;
2807
 
    x[3][2] = 0;
2808
 
    x[3][3] = 1;
2809
 
 
2810
 
    return *this;
2811
 
}
2812
 
 
2813
 
template <class T>
2814
 
template <class S>
2815
 
const Matrix44<T> &
2816
 
Matrix44<T>::rotate (const Vec3<S> &r)
2817
 
{
2818
 
    S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
2819
 
    S m00, m01, m02;
2820
 
    S m10, m11, m12;
2821
 
    S m20, m21, m22;
2822
 
 
2823
 
    cos_rz = Math<S>::cos (r[2]);
2824
 
    cos_ry = Math<S>::cos (r[1]);
2825
 
    cos_rx = Math<S>::cos (r[0]);
2826
 
    
2827
 
    sin_rz = Math<S>::sin (r[2]);
2828
 
    sin_ry = Math<S>::sin (r[1]);
2829
 
    sin_rx = Math<S>::sin (r[0]);
2830
 
 
2831
 
    m00 =  cos_rz *  cos_ry;
2832
 
    m01 =  sin_rz *  cos_ry;
2833
 
    m02 = -sin_ry;
2834
 
    m10 = -sin_rz *  cos_rx + cos_rz * sin_ry * sin_rx;
2835
 
    m11 =  cos_rz *  cos_rx + sin_rz * sin_ry * sin_rx;
2836
 
    m12 =  cos_ry *  sin_rx;
2837
 
    m20 = -sin_rz * -sin_rx + cos_rz * sin_ry * cos_rx;
2838
 
    m21 =  cos_rz * -sin_rx + sin_rz * sin_ry * cos_rx;
2839
 
    m22 =  cos_ry *  cos_rx;
2840
 
 
2841
 
    Matrix44<T> P (*this);
2842
 
 
2843
 
    x[0][0] = P[0][0] * m00 + P[1][0] * m01 + P[2][0] * m02;
2844
 
    x[0][1] = P[0][1] * m00 + P[1][1] * m01 + P[2][1] * m02;
2845
 
    x[0][2] = P[0][2] * m00 + P[1][2] * m01 + P[2][2] * m02;
2846
 
    x[0][3] = P[0][3] * m00 + P[1][3] * m01 + P[2][3] * m02;
2847
 
 
2848
 
    x[1][0] = P[0][0] * m10 + P[1][0] * m11 + P[2][0] * m12;
2849
 
    x[1][1] = P[0][1] * m10 + P[1][1] * m11 + P[2][1] * m12;
2850
 
    x[1][2] = P[0][2] * m10 + P[1][2] * m11 + P[2][2] * m12;
2851
 
    x[1][3] = P[0][3] * m10 + P[1][3] * m11 + P[2][3] * m12;
2852
 
 
2853
 
    x[2][0] = P[0][0] * m20 + P[1][0] * m21 + P[2][0] * m22;
2854
 
    x[2][1] = P[0][1] * m20 + P[1][1] * m21 + P[2][1] * m22;
2855
 
    x[2][2] = P[0][2] * m20 + P[1][2] * m21 + P[2][2] * m22;
2856
 
    x[2][3] = P[0][3] * m20 + P[1][3] * m21 + P[2][3] * m22;
2857
 
 
2858
 
    return *this;
2859
 
}
2860
 
 
2861
 
template <class T>
2862
 
const Matrix44<T> &
2863
 
Matrix44<T>::setScale (T s)
2864
 
{
2865
 
    x[0][0] = s;
2866
 
    x[0][1] = 0;
2867
 
    x[0][2] = 0;
2868
 
    x[0][3] = 0;
2869
 
 
2870
 
    x[1][0] = 0;
2871
 
    x[1][1] = s;
2872
 
    x[1][2] = 0;
2873
 
    x[1][3] = 0;
2874
 
 
2875
 
    x[2][0] = 0;
2876
 
    x[2][1] = 0;
2877
 
    x[2][2] = s;
2878
 
    x[2][3] = 0;
2879
 
 
2880
 
    x[3][0] = 0;
2881
 
    x[3][1] = 0;
2882
 
    x[3][2] = 0;
2883
 
    x[3][3] = 1;
2884
 
 
2885
 
    return *this;
2886
 
}
2887
 
 
2888
 
template <class T>
2889
 
template <class S>
2890
 
const Matrix44<T> &
2891
 
Matrix44<T>::setScale (const Vec3<S> &s)
2892
 
{
2893
 
    x[0][0] = s[0];
2894
 
    x[0][1] = 0;
2895
 
    x[0][2] = 0;
2896
 
    x[0][3] = 0;
2897
 
 
2898
 
    x[1][0] = 0;
2899
 
    x[1][1] = s[1];
2900
 
    x[1][2] = 0;
2901
 
    x[1][3] = 0;
2902
 
 
2903
 
    x[2][0] = 0;
2904
 
    x[2][1] = 0;
2905
 
    x[2][2] = s[2];
2906
 
    x[2][3] = 0;
2907
 
 
2908
 
    x[3][0] = 0;
2909
 
    x[3][1] = 0;
2910
 
    x[3][2] = 0;
2911
 
    x[3][3] = 1;
2912
 
 
2913
 
    return *this;
2914
 
}
2915
 
 
2916
 
template <class T>
2917
 
template <class S>
2918
 
const Matrix44<T> &
2919
 
Matrix44<T>::scale (const Vec3<S> &s)
2920
 
{
2921
 
    x[0][0] *= s[0];
2922
 
    x[0][1] *= s[0];
2923
 
    x[0][2] *= s[0];
2924
 
    x[0][3] *= s[0];
2925
 
 
2926
 
    x[1][0] *= s[1];
2927
 
    x[1][1] *= s[1];
2928
 
    x[1][2] *= s[1];
2929
 
    x[1][3] *= s[1];
2930
 
 
2931
 
    x[2][0] *= s[2];
2932
 
    x[2][1] *= s[2];
2933
 
    x[2][2] *= s[2];
2934
 
    x[2][3] *= s[2];
2935
 
 
2936
 
    return *this;
2937
 
}
2938
 
 
2939
 
template <class T>
2940
 
template <class S>
2941
 
const Matrix44<T> &
2942
 
Matrix44<T>::setTranslation (const Vec3<S> &t)
2943
 
{
2944
 
    x[0][0] = 1;
2945
 
    x[0][1] = 0;
2946
 
    x[0][2] = 0;
2947
 
    x[0][3] = 0;
2948
 
 
2949
 
    x[1][0] = 0;
2950
 
    x[1][1] = 1;
2951
 
    x[1][2] = 0;
2952
 
    x[1][3] = 0;
2953
 
 
2954
 
    x[2][0] = 0;
2955
 
    x[2][1] = 0;
2956
 
    x[2][2] = 1;
2957
 
    x[2][3] = 0;
2958
 
 
2959
 
    x[3][0] = t[0];
2960
 
    x[3][1] = t[1];
2961
 
    x[3][2] = t[2];
2962
 
    x[3][3] = 1;
2963
 
 
2964
 
    return *this;
2965
 
}
2966
 
 
2967
 
template <class T>
2968
 
inline const Vec3<T>
2969
 
Matrix44<T>::translation () const
2970
 
{
2971
 
    return Vec3<T> (x[3][0], x[3][1], x[3][2]);
2972
 
}
2973
 
 
2974
 
template <class T>
2975
 
template <class S>
2976
 
const Matrix44<T> &
2977
 
Matrix44<T>::translate (const Vec3<S> &t)
2978
 
{
2979
 
    x[3][0] += t[0] * x[0][0] + t[1] * x[1][0] + t[2] * x[2][0];
2980
 
    x[3][1] += t[0] * x[0][1] + t[1] * x[1][1] + t[2] * x[2][1];
2981
 
    x[3][2] += t[0] * x[0][2] + t[1] * x[1][2] + t[2] * x[2][2];
2982
 
    x[3][3] += t[0] * x[0][3] + t[1] * x[1][3] + t[2] * x[2][3];
2983
 
 
2984
 
    return *this;
2985
 
}
2986
 
 
2987
 
template <class T>
2988
 
template <class S>
2989
 
const Matrix44<T> &
2990
 
Matrix44<T>::setShear (const Vec3<S> &h)
2991
 
{
2992
 
    x[0][0] = 1;
2993
 
    x[0][1] = 0;
2994
 
    x[0][2] = 0;
2995
 
    x[0][3] = 0;
2996
 
 
2997
 
    x[1][0] = h[0];
2998
 
    x[1][1] = 1;
2999
 
    x[1][2] = 0;
3000
 
    x[1][3] = 0;
3001
 
 
3002
 
    x[2][0] = h[1];
3003
 
    x[2][1] = h[2];
3004
 
    x[2][2] = 1;
3005
 
    x[2][3] = 0;
3006
 
 
3007
 
    x[3][0] = 0;
3008
 
    x[3][1] = 0;
3009
 
    x[3][2] = 0;
3010
 
    x[3][3] = 1;
3011
 
 
3012
 
    return *this;
3013
 
}
3014
 
 
3015
 
template <class T>
3016
 
template <class S>
3017
 
const Matrix44<T> &
3018
 
Matrix44<T>::setShear (const Shear6<S> &h)
3019
 
{
3020
 
    x[0][0] = 1;
3021
 
    x[0][1] = h.yx;
3022
 
    x[0][2] = h.zx;
3023
 
    x[0][3] = 0;
3024
 
 
3025
 
    x[1][0] = h.xy;
3026
 
    x[1][1] = 1;
3027
 
    x[1][2] = h.zy;
3028
 
    x[1][3] = 0;
3029
 
 
3030
 
    x[2][0] = h.xz;
3031
 
    x[2][1] = h.yz;
3032
 
    x[2][2] = 1;
3033
 
    x[2][3] = 0;
3034
 
 
3035
 
    x[3][0] = 0;
3036
 
    x[3][1] = 0;
3037
 
    x[3][2] = 0;
3038
 
    x[3][3] = 1;
3039
 
 
3040
 
    return *this;
3041
 
}
3042
 
 
3043
 
template <class T>
3044
 
template <class S>
3045
 
const Matrix44<T> &
3046
 
Matrix44<T>::shear (const Vec3<S> &h)
3047
 
{
3048
 
    //
3049
 
    // In this case, we don't need a temp. copy of the matrix 
3050
 
    // because we never use a value on the RHS after we've 
3051
 
    // changed it on the LHS.
3052
 
    // 
3053
 
 
3054
 
    for (int i=0; i < 4; i++)
3055
 
    {
3056
 
        x[2][i] += h[1] * x[0][i] + h[2] * x[1][i];
3057
 
        x[1][i] += h[0] * x[0][i];
3058
 
    }
3059
 
 
3060
 
    return *this;
3061
 
}
3062
 
 
3063
 
template <class T>
3064
 
template <class S>
3065
 
const Matrix44<T> &
3066
 
Matrix44<T>::shear (const Shear6<S> &h)
3067
 
{
3068
 
    Matrix44<T> P (*this);
3069
 
 
3070
 
    for (int i=0; i < 4; i++)
3071
 
    {
3072
 
        x[0][i] = P[0][i] + h.yx * P[1][i] + h.zx * P[2][i];
3073
 
        x[1][i] = h.xy * P[0][i] + P[1][i] + h.zy * P[2][i];
3074
 
        x[2][i] = h.xz * P[0][i] + h.yz * P[1][i] + P[2][i];
3075
 
    }
3076
 
 
3077
 
    return *this;
3078
 
}
3079
 
 
3080
 
 
3081
 
//--------------------------------
3082
 
// Implementation of stream output
3083
 
//--------------------------------
3084
 
 
3085
 
template <class T>
3086
 
std::ostream &
3087
 
operator << (std::ostream &s, const Matrix33<T> &m)
3088
 
{
3089
 
    std::ios_base::fmtflags oldFlags = s.flags();
3090
 
    int width;
3091
 
 
3092
 
    if (s.flags() & std::ios_base::fixed)
3093
 
    {
3094
 
        s.setf (std::ios_base::showpoint);
3095
 
        width = s.precision() + 5;
3096
 
    }
3097
 
    else
3098
 
    {
3099
 
        s.setf (std::ios_base::scientific);
3100
 
        s.setf (std::ios_base::showpoint);
3101
 
        width = s.precision() + 8;
3102
 
    }
3103
 
 
3104
 
    s << "(" << std::setw (width) << m[0][0] <<
3105
 
         " " << std::setw (width) << m[0][1] <<
3106
 
         " " << std::setw (width) << m[0][2] << "\n" <<
3107
 
 
3108
 
         " " << std::setw (width) << m[1][0] <<
3109
 
         " " << std::setw (width) << m[1][1] <<
3110
 
         " " << std::setw (width) << m[1][2] << "\n" <<
3111
 
 
3112
 
         " " << std::setw (width) << m[2][0] <<
3113
 
         " " << std::setw (width) << m[2][1] <<
3114
 
         " " << std::setw (width) << m[2][2] << ")\n";
3115
 
 
3116
 
    s.flags (oldFlags);
3117
 
    return s;
3118
 
}
3119
 
 
3120
 
template <class T>
3121
 
std::ostream &
3122
 
operator << (std::ostream &s, const Matrix44<T> &m)
3123
 
{
3124
 
    std::ios_base::fmtflags oldFlags = s.flags();
3125
 
    int width;
3126
 
 
3127
 
    if (s.flags() & std::ios_base::fixed)
3128
 
    {
3129
 
        s.setf (std::ios_base::showpoint);
3130
 
        width = s.precision() + 5;
3131
 
    }
3132
 
    else
3133
 
    {
3134
 
        s.setf (std::ios_base::scientific);
3135
 
        s.setf (std::ios_base::showpoint);
3136
 
        width = s.precision() + 8;
3137
 
    }
3138
 
 
3139
 
    s << "(" << std::setw (width) << m[0][0] <<
3140
 
         " " << std::setw (width) << m[0][1] <<
3141
 
         " " << std::setw (width) << m[0][2] <<
3142
 
         " " << std::setw (width) << m[0][3] << "\n" <<
3143
 
 
3144
 
         " " << std::setw (width) << m[1][0] <<
3145
 
         " " << std::setw (width) << m[1][1] <<
3146
 
         " " << std::setw (width) << m[1][2] <<
3147
 
         " " << std::setw (width) << m[1][3] << "\n" <<
3148
 
 
3149
 
         " " << std::setw (width) << m[2][0] <<
3150
 
         " " << std::setw (width) << m[2][1] <<
3151
 
         " " << std::setw (width) << m[2][2] <<
3152
 
         " " << std::setw (width) << m[2][3] << "\n" <<
3153
 
 
3154
 
         " " << std::setw (width) << m[3][0] <<
3155
 
         " " << std::setw (width) << m[3][1] <<
3156
 
         " " << std::setw (width) << m[3][2] <<
3157
 
         " " << std::setw (width) << m[3][3] << ")\n";
3158
 
 
3159
 
    s.flags (oldFlags);
3160
 
    return s;
3161
 
}
3162
 
 
3163
 
 
3164
 
//---------------------------------------------------------------
3165
 
// Implementation of vector-times-matrix multiplication operators
3166
 
//---------------------------------------------------------------
3167
 
 
3168
 
template <class S, class T>
3169
 
inline const Vec2<S> &
3170
 
operator *= (Vec2<S> &v, const Matrix33<T> &m)
3171
 
{
3172
 
    S x = S(v.x * m[0][0] + v.y * m[1][0] + m[2][0]);
3173
 
    S y = S(v.x * m[0][1] + v.y * m[1][1] + m[2][1]);
3174
 
    S w = S(v.x * m[0][2] + v.y * m[1][2] + m[2][2]);
3175
 
 
3176
 
    v.x = x / w;
3177
 
    v.y = y / w;
3178
 
 
3179
 
    return v;
3180
 
}
3181
 
 
3182
 
template <class S, class T>
3183
 
inline Vec2<S>
3184
 
operator * (const Vec2<S> &v, const Matrix33<T> &m)
3185
 
{
3186
 
    S x = S(v.x * m[0][0] + v.y * m[1][0] + m[2][0]);
3187
 
    S y = S(v.x * m[0][1] + v.y * m[1][1] + m[2][1]);
3188
 
    S w = S(v.x * m[0][2] + v.y * m[1][2] + m[2][2]);
3189
 
 
3190
 
    return Vec2<S> (x / w, y / w);
3191
 
}
3192
 
 
3193
 
 
3194
 
template <class S, class T>
3195
 
inline const Vec3<S> &
3196
 
operator *= (Vec3<S> &v, const Matrix33<T> &m)
3197
 
{
3198
 
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0]);
3199
 
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1]);
3200
 
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2]);
3201
 
 
3202
 
    v.x = x;
3203
 
    v.y = y;
3204
 
    v.z = z;
3205
 
 
3206
 
    return v;
3207
 
}
3208
 
 
3209
 
 
3210
 
template <class S, class T>
3211
 
inline Vec3<S>
3212
 
operator * (const Vec3<S> &v, const Matrix33<T> &m)
3213
 
{
3214
 
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0]);
3215
 
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1]);
3216
 
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2]);
3217
 
 
3218
 
    return Vec3<S> (x, y, z);
3219
 
}
3220
 
 
3221
 
 
3222
 
template <class S, class T>
3223
 
inline const Vec3<S> &
3224
 
operator *= (Vec3<S> &v, const Matrix44<T> &m)
3225
 
{
3226
 
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]);
3227
 
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]);
3228
 
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]);
3229
 
    S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3]);
3230
 
 
3231
 
    v.x = x / w;
3232
 
    v.y = y / w;
3233
 
    v.z = z / w;
3234
 
 
3235
 
    return v;
3236
 
}
3237
 
 
3238
 
template <class S, class T>
3239
 
inline Vec3<S>
3240
 
operator * (const Vec3<S> &v, const Matrix44<T> &m)
3241
 
{
3242
 
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]);
3243
 
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]);
3244
 
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]);
3245
 
    S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3]);
3246
 
 
3247
 
    return Vec3<S> (x / w, y / w, z / w);
3248
 
}
3249
 
 
3250
 
} // namespace Imath
3251
 
 
3252
 
 
3253
 
 
3254
 
#endif
 
1
///////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
 
4
// Digital Ltd. LLC
 
5
// 
 
6
// All rights reserved.
 
7
// 
 
8
// Redistribution and use in source and binary forms, with or without
 
9
// modification, are permitted provided that the following conditions are
 
10
// met:
 
11
// *       Redistributions of source code must retain the above copyright
 
12
// notice, this list of conditions and the following disclaimer.
 
13
// *       Redistributions in binary form must reproduce the above
 
14
// copyright notice, this list of conditions and the following disclaimer
 
15
// in the documentation and/or other materials provided with the
 
16
// distribution.
 
17
// *       Neither the name of Industrial Light & Magic nor the names of
 
18
// its contributors may be used to endorse or promote products derived
 
19
// from this software without specific prior written permission. 
 
20
// 
 
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
32
//
 
33
///////////////////////////////////////////////////////////////////////////
 
34
 
 
35
 
 
36
 
 
37
#ifndef INCLUDED_IMATHMATRIX_H
 
38
#define INCLUDED_IMATHMATRIX_H
 
39
 
 
40
//----------------------------------------------------------------
 
41
//
 
42
//      2D (3x3) and 3D (4x4) transformation matrix templates.
 
43
//
 
44
//----------------------------------------------------------------
 
45
 
 
46
#include "ImathPlatform.h"
 
47
#include "ImathFun.h"
 
48
#include "ImathExc.h"
 
49
#include "ImathVec.h"
 
50
#include "ImathShear.h"
 
51
 
 
52
#include <iostream>
 
53
#include <iomanip>
 
54
 
 
55
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
 
56
// suppress exception specification warnings
 
57
#pragma warning(disable:4290)
 
58
#endif
 
59
 
 
60
 
 
61
namespace Imath {
 
62
 
 
63
 
 
64
template <class T> class Matrix33
 
65
{
 
66
  public:
 
67
 
 
68
    //-------------------
 
69
    // Access to elements
 
70
    //-------------------
 
71
 
 
72
    T           x[3][3];
 
73
 
 
74
    T *         operator [] (int i);
 
75
    const T *   operator [] (int i) const;
 
76
 
 
77
 
 
78
    //-------------
 
79
    // Constructors
 
80
    //-------------
 
81
 
 
82
    Matrix33 ();
 
83
                                // 1 0 0
 
84
                                // 0 1 0
 
85
                                // 0 0 1
 
86
 
 
87
    Matrix33 (T a);
 
88
                                // a a a
 
89
                                // a a a
 
90
                                // a a a
 
91
 
 
92
    Matrix33 (const T a[3][3]);
 
93
                                // a[0][0] a[0][1] a[0][2]
 
94
                                // a[1][0] a[1][1] a[1][2]
 
95
                                // a[2][0] a[2][1] a[2][2]
 
96
 
 
97
    Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i);
 
98
 
 
99
                                // a b c
 
100
                                // d e f
 
101
                                // g h i
 
102
 
 
103
 
 
104
    //--------------------------------
 
105
    // Copy constructor and assignment
 
106
    //--------------------------------
 
107
 
 
108
    Matrix33 (const Matrix33 &v);
 
109
 
 
110
    const Matrix33 &    operator = (const Matrix33 &v);
 
111
    const Matrix33 &    operator = (T a);
 
112
 
 
113
 
 
114
    //----------------------
 
115
    // Compatibility with Sb
 
116
    //----------------------
 
117
    
 
118
    T *                 getValue ();
 
119
    const T *           getValue () const;
 
120
 
 
121
    template <class S>
 
122
    void                getValue (Matrix33<S> &v) const;
 
123
    template <class S>
 
124
    Matrix33 &          setValue (const Matrix33<S> &v);
 
125
 
 
126
    template <class S>
 
127
    Matrix33 &          setTheMatrix (const Matrix33<S> &v);
 
128
 
 
129
 
 
130
    //---------
 
131
    // Identity
 
132
    //---------
 
133
 
 
134
    void                makeIdentity();
 
135
 
 
136
 
 
137
    //---------
 
138
    // Equality
 
139
    //---------
 
140
 
 
141
    bool                operator == (const Matrix33 &v) const;
 
142
    bool                operator != (const Matrix33 &v) const;
 
143
 
 
144
    //-----------------------------------------------------------------------
 
145
    // Compare two matrices and test if they are "approximately equal":
 
146
    //
 
147
    // equalWithAbsError (m, e)
 
148
    //
 
149
    //      Returns true if the coefficients of this and m are the same with
 
150
    //      an absolute error of no more than e, i.e., for all i, j
 
151
    //
 
152
    //      abs (this[i][j] - m[i][j]) <= e
 
153
    //
 
154
    // equalWithRelError (m, e)
 
155
    //
 
156
    //      Returns true if the coefficients of this and m are the same with
 
157
    //      a relative error of no more than e, i.e., for all i, j
 
158
    //
 
159
    //      abs (this[i] - v[i][j]) <= e * abs (this[i][j])
 
160
    //-----------------------------------------------------------------------
 
161
 
 
162
    bool                equalWithAbsError (const Matrix33<T> &v, T e) const;
 
163
    bool                equalWithRelError (const Matrix33<T> &v, T e) const;
 
164
 
 
165
 
 
166
    //------------------------
 
167
    // Component-wise addition
 
168
    //------------------------
 
169
 
 
170
    const Matrix33 &    operator += (const Matrix33 &v);
 
171
    const Matrix33 &    operator += (T a);
 
172
    Matrix33            operator + (const Matrix33 &v) const;
 
173
 
 
174
 
 
175
    //---------------------------
 
176
    // Component-wise subtraction
 
177
    //---------------------------
 
178
 
 
179
    const Matrix33 &    operator -= (const Matrix33 &v);
 
180
    const Matrix33 &    operator -= (T a);
 
181
    Matrix33            operator - (const Matrix33 &v) const;
 
182
 
 
183
 
 
184
    //------------------------------------
 
185
    // Component-wise multiplication by -1
 
186
    //------------------------------------
 
187
 
 
188
    Matrix33            operator - () const;
 
189
    const Matrix33 &    negate ();
 
190
 
 
191
 
 
192
    //------------------------------
 
193
    // Component-wise multiplication
 
194
    //------------------------------
 
195
 
 
196
    const Matrix33 &    operator *= (T a);
 
197
    Matrix33            operator * (T a) const;
 
198
 
 
199
 
 
200
    //-----------------------------------
 
201
    // Matrix-times-matrix multiplication
 
202
    //-----------------------------------
 
203
 
 
204
    const Matrix33 &    operator *= (const Matrix33 &v);
 
205
    Matrix33            operator * (const Matrix33 &v) const;
 
206
 
 
207
 
 
208
    //---------------------------------------------
 
209
    // Vector-times-matrix multiplication; see also
 
210
    // the "operator *" functions defined below.
 
211
    //---------------------------------------------
 
212
 
 
213
    template <class S>
 
214
    void                multVecMatrix(const Vec2<S> &src, Vec2<S> &dst) const;
 
215
 
 
216
    template <class S>
 
217
    void                multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const;
 
218
 
 
219
 
 
220
    //------------------------
 
221
    // Component-wise division
 
222
    //------------------------
 
223
 
 
224
    const Matrix33 &    operator /= (T a);
 
225
    Matrix33            operator / (T a) const;
 
226
 
 
227
 
 
228
    //------------------
 
229
    // Transposed matrix
 
230
    //------------------
 
231
 
 
232
    const Matrix33 &    transpose ();
 
233
    Matrix33            transposed () const;
 
234
 
 
235
 
 
236
    //------------------------------------------------------------
 
237
    // Inverse matrix: If singExc is false, inverting a singular
 
238
    // matrix produces an identity matrix.  If singExc is true,
 
239
    // inverting a singular matrix throws a SingMatrixExc.
 
240
    //
 
241
    // inverse() and invert() invert matrices using determinants;
 
242
    // gjInverse() and gjInvert() use the Gauss-Jordan method.
 
243
    //
 
244
    // inverse() and invert() are significantly faster than
 
245
    // gjInverse() and gjInvert(), but the results may be slightly
 
246
    // less accurate.
 
247
    // 
 
248
    //------------------------------------------------------------
 
249
 
 
250
    const Matrix33 &    invert (bool singExc = false)
 
251
                        throw (Iex::MathExc);
 
252
 
 
253
    Matrix33<T>         inverse (bool singExc = false) const
 
254
                        throw (Iex::MathExc);
 
255
 
 
256
    const Matrix33 &    gjInvert (bool singExc = false)
 
257
                        throw (Iex::MathExc);
 
258
 
 
259
    Matrix33<T>         gjInverse (bool singExc = false) const
 
260
                        throw (Iex::MathExc);
 
261
 
 
262
 
 
263
    //-----------------------------------------
 
264
    // Set matrix to rotation by r (in radians)
 
265
    //-----------------------------------------
 
266
 
 
267
    template <class S>
 
268
    const Matrix33 &    setRotation (S r);
 
269
 
 
270
 
 
271
    //-----------------------------
 
272
    // Rotate the given matrix by r
 
273
    //-----------------------------
 
274
 
 
275
    template <class S>
 
276
    const Matrix33 &    rotate (S r);
 
277
 
 
278
 
 
279
    //--------------------------------------------
 
280
    // Set matrix to scale by given uniform factor
 
281
    //--------------------------------------------
 
282
 
 
283
    const Matrix33 &    setScale (T s);
 
284
 
 
285
 
 
286
    //------------------------------------
 
287
    // Set matrix to scale by given vector
 
288
    //------------------------------------
 
289
 
 
290
    template <class S>
 
291
    const Matrix33 &    setScale (const Vec2<S> &s);
 
292
 
 
293
 
 
294
    //----------------------
 
295
    // Scale the matrix by s
 
296
    //----------------------
 
297
 
 
298
    template <class S>
 
299
    const Matrix33 &    scale (const Vec2<S> &s);
 
300
 
 
301
 
 
302
    //------------------------------------------
 
303
    // Set matrix to translation by given vector
 
304
    //------------------------------------------
 
305
 
 
306
    template <class S>
 
307
    const Matrix33 &    setTranslation (const Vec2<S> &t);
 
308
 
 
309
 
 
310
    //-----------------------------
 
311
    // Return translation component
 
312
    //-----------------------------
 
313
 
 
314
    Vec2<T>             translation () const;
 
315
 
 
316
 
 
317
    //--------------------------
 
318
    // Translate the matrix by t
 
319
    //--------------------------
 
320
 
 
321
    template <class S>
 
322
    const Matrix33 &    translate (const Vec2<S> &t);
 
323
 
 
324
 
 
325
    //-----------------------------------------------------------
 
326
    // Set matrix to shear x for each y coord. by given factor xy
 
327
    //-----------------------------------------------------------
 
328
 
 
329
    template <class S>
 
330
    const Matrix33 &    setShear (const S &h);
 
331
 
 
332
 
 
333
    //-------------------------------------------------------------
 
334
    // Set matrix to shear x for each y coord. by given factor h[0]
 
335
    // and to shear y for each x coord. by given factor h[1]
 
336
    //-------------------------------------------------------------
 
337
 
 
338
    template <class S>
 
339
    const Matrix33 &    setShear (const Vec2<S> &h);
 
340
 
 
341
 
 
342
    //-----------------------------------------------------------
 
343
    // Shear the matrix in x for each y coord. by given factor xy
 
344
    //-----------------------------------------------------------
 
345
 
 
346
    template <class S>
 
347
    const Matrix33 &    shear (const S &xy);
 
348
 
 
349
 
 
350
    //-----------------------------------------------------------
 
351
    // Shear the matrix in x for each y coord. by given factor xy
 
352
    // and shear y for each x coord. by given factor yx
 
353
    //-----------------------------------------------------------
 
354
 
 
355
    template <class S>
 
356
    const Matrix33 &    shear (const Vec2<S> &h);
 
357
 
 
358
 
 
359
    //-------------------------------------------------
 
360
    // Limitations of type T (see also class limits<T>)
 
361
    //-------------------------------------------------
 
362
 
 
363
    static T            baseTypeMin()           {return limits<T>::min();}
 
364
    static T            baseTypeMax()           {return limits<T>::max();}
 
365
    static T            baseTypeSmallest()      {return limits<T>::smallest();}
 
366
    static T            baseTypeEpsilon()       {return limits<T>::epsilon();}
 
367
};
 
368
 
 
369
 
 
370
template <class T> class Matrix44
 
371
{
 
372
  public:
 
373
 
 
374
    //-------------------
 
375
    // Access to elements
 
376
    //-------------------
 
377
 
 
378
    T           x[4][4];
 
379
 
 
380
    T *         operator [] (int i);
 
381
    const T *   operator [] (int i) const;
 
382
 
 
383
 
 
384
    //-------------
 
385
    // Constructors
 
386
    //-------------
 
387
 
 
388
    Matrix44 ();
 
389
                                // 1 0 0 0
 
390
                                // 0 1 0 0
 
391
                                // 0 0 1 0
 
392
                                // 0 0 0 1
 
393
 
 
394
    Matrix44 (T a);
 
395
                                // a a a a
 
396
                                // a a a a
 
397
                                // a a a a
 
398
                                // a a a a
 
399
 
 
400
    Matrix44 (const T a[4][4]) ;
 
401
                                // a[0][0] a[0][1] a[0][2] a[0][3]
 
402
                                // a[1][0] a[1][1] a[1][2] a[1][3]
 
403
                                // a[2][0] a[2][1] a[2][2] a[2][3]
 
404
                                // a[3][0] a[3][1] a[3][2] a[3][3]
 
405
 
 
406
    Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h,
 
407
              T i, T j, T k, T l, T m, T n, T o, T p);
 
408
 
 
409
                                // a b c d
 
410
                                // e f g h
 
411
                                // i j k l
 
412
                                // m n o p
 
413
 
 
414
    Matrix44 (Matrix33<T> r, Vec3<T> t);
 
415
                                // r r r 0
 
416
                                // r r r 0
 
417
                                // r r r 0
 
418
                                // t t t 1
 
419
 
 
420
 
 
421
    //--------------------------------
 
422
    // Copy constructor and assignment
 
423
    //--------------------------------
 
424
 
 
425
    Matrix44 (const Matrix44 &v);
 
426
 
 
427
    const Matrix44 &    operator = (const Matrix44 &v);
 
428
    const Matrix44 &    operator = (T a);
 
429
 
 
430
 
 
431
    //----------------------
 
432
    // Compatibility with Sb
 
433
    //----------------------
 
434
    
 
435
    T *                 getValue ();
 
436
    const T *           getValue () const;
 
437
 
 
438
    template <class S>
 
439
    void                getValue (Matrix44<S> &v) const;
 
440
    template <class S>
 
441
    Matrix44 &          setValue (const Matrix44<S> &v);
 
442
 
 
443
    template <class S>
 
444
    Matrix44 &          setTheMatrix (const Matrix44<S> &v);
 
445
 
 
446
    //---------
 
447
    // Identity
 
448
    //---------
 
449
 
 
450
    void                makeIdentity();
 
451
 
 
452
 
 
453
    //---------
 
454
    // Equality
 
455
    //---------
 
456
 
 
457
    bool                operator == (const Matrix44 &v) const;
 
458
    bool                operator != (const Matrix44 &v) const;
 
459
 
 
460
    //-----------------------------------------------------------------------
 
461
    // Compare two matrices and test if they are "approximately equal":
 
462
    //
 
463
    // equalWithAbsError (m, e)
 
464
    //
 
465
    //      Returns true if the coefficients of this and m are the same with
 
466
    //      an absolute error of no more than e, i.e., for all i, j
 
467
    //
 
468
    //      abs (this[i][j] - m[i][j]) <= e
 
469
    //
 
470
    // equalWithRelError (m, e)
 
471
    //
 
472
    //      Returns true if the coefficients of this and m are the same with
 
473
    //      a relative error of no more than e, i.e., for all i, j
 
474
    //
 
475
    //      abs (this[i] - v[i][j]) <= e * abs (this[i][j])
 
476
    //-----------------------------------------------------------------------
 
477
 
 
478
    bool                equalWithAbsError (const Matrix44<T> &v, T e) const;
 
479
    bool                equalWithRelError (const Matrix44<T> &v, T e) const;
 
480
 
 
481
 
 
482
    //------------------------
 
483
    // Component-wise addition
 
484
    //------------------------
 
485
 
 
486
    const Matrix44 &    operator += (const Matrix44 &v);
 
487
    const Matrix44 &    operator += (T a);
 
488
    Matrix44            operator + (const Matrix44 &v) const;
 
489
 
 
490
 
 
491
    //---------------------------
 
492
    // Component-wise subtraction
 
493
    //---------------------------
 
494
 
 
495
    const Matrix44 &    operator -= (const Matrix44 &v);
 
496
    const Matrix44 &    operator -= (T a);
 
497
    Matrix44            operator - (const Matrix44 &v) const;
 
498
 
 
499
 
 
500
    //------------------------------------
 
501
    // Component-wise multiplication by -1
 
502
    //------------------------------------
 
503
 
 
504
    Matrix44            operator - () const;
 
505
    const Matrix44 &    negate ();
 
506
 
 
507
 
 
508
    //------------------------------
 
509
    // Component-wise multiplication
 
510
    //------------------------------
 
511
 
 
512
    const Matrix44 &    operator *= (T a);
 
513
    Matrix44            operator * (T a) const;
 
514
 
 
515
 
 
516
    //-----------------------------------
 
517
    // Matrix-times-matrix multiplication
 
518
    //-----------------------------------
 
519
 
 
520
    const Matrix44 &    operator *= (const Matrix44 &v);
 
521
    Matrix44            operator * (const Matrix44 &v) const;
 
522
 
 
523
    static void         multiply (const Matrix44 &a,    // assumes that
 
524
                                  const Matrix44 &b,    // &a != &c and
 
525
                                  Matrix44 &c);         // &b != &c.
 
526
 
 
527
 
 
528
    //---------------------------------------------
 
529
    // Vector-times-matrix multiplication; see also
 
530
    // the "operator *" functions defined below.
 
531
    //---------------------------------------------
 
532
 
 
533
    template <class S>
 
534
    void                multVecMatrix(const Vec3<S> &src, Vec3<S> &dst) const;
 
535
 
 
536
    template <class S>
 
537
    void                multDirMatrix(const Vec3<S> &src, Vec3<S> &dst) const;
 
538
 
 
539
 
 
540
    //------------------------
 
541
    // Component-wise division
 
542
    //------------------------
 
543
 
 
544
    const Matrix44 &    operator /= (T a);
 
545
    Matrix44            operator / (T a) const;
 
546
 
 
547
 
 
548
    //------------------
 
549
    // Transposed matrix
 
550
    //------------------
 
551
 
 
552
    const Matrix44 &    transpose ();
 
553
    Matrix44            transposed () const;
 
554
 
 
555
 
 
556
    //------------------------------------------------------------
 
557
    // Inverse matrix: If singExc is false, inverting a singular
 
558
    // matrix produces an identity matrix.  If singExc is true,
 
559
    // inverting a singular matrix throws a SingMatrixExc.
 
560
    //
 
561
    // inverse() and invert() invert matrices using determinants;
 
562
    // gjInverse() and gjInvert() use the Gauss-Jordan method.
 
563
    //
 
564
    // inverse() and invert() are significantly faster than
 
565
    // gjInverse() and gjInvert(), but the results may be slightly
 
566
    // less accurate.
 
567
    // 
 
568
    //------------------------------------------------------------
 
569
 
 
570
    const Matrix44 &    invert (bool singExc = false)
 
571
                        throw (Iex::MathExc);
 
572
 
 
573
    Matrix44<T>         inverse (bool singExc = false) const
 
574
                        throw (Iex::MathExc);
 
575
 
 
576
    const Matrix44 &    gjInvert (bool singExc = false)
 
577
                        throw (Iex::MathExc);
 
578
 
 
579
    Matrix44<T>         gjInverse (bool singExc = false) const
 
580
                        throw (Iex::MathExc);
 
581
 
 
582
 
 
583
    //--------------------------------------------------------
 
584
    // Set matrix to rotation by XYZ euler angles (in radians)
 
585
    //--------------------------------------------------------
 
586
 
 
587
    template <class S>
 
588
    const Matrix44 &    setEulerAngles (const Vec3<S>& r);
 
589
 
 
590
 
 
591
    //--------------------------------------------------------
 
592
    // Set matrix to rotation around given axis by given angle
 
593
    //--------------------------------------------------------
 
594
 
 
595
    template <class S>
 
596
    const Matrix44 &    setAxisAngle (const Vec3<S>& ax, S ang);
 
597
 
 
598
 
 
599
    //-------------------------------------------
 
600
    // Rotate the matrix by XYZ euler angles in r
 
601
    //-------------------------------------------
 
602
 
 
603
    template <class S>
 
604
    const Matrix44 &    rotate (const Vec3<S> &r);
 
605
 
 
606
 
 
607
    //--------------------------------------------
 
608
    // Set matrix to scale by given uniform factor
 
609
    //--------------------------------------------
 
610
 
 
611
    const Matrix44 &    setScale (T s);
 
612
 
 
613
 
 
614
    //------------------------------------
 
615
    // Set matrix to scale by given vector
 
616
    //------------------------------------
 
617
 
 
618
    template <class S>
 
619
    const Matrix44 &    setScale (const Vec3<S> &s);
 
620
 
 
621
 
 
622
    //----------------------
 
623
    // Scale the matrix by s
 
624
    //----------------------
 
625
 
 
626
    template <class S>
 
627
    const Matrix44 &    scale (const Vec3<S> &s);
 
628
 
 
629
 
 
630
    //------------------------------------------
 
631
    // Set matrix to translation by given vector
 
632
    //------------------------------------------
 
633
 
 
634
    template <class S>
 
635
    const Matrix44 &    setTranslation (const Vec3<S> &t);
 
636
 
 
637
 
 
638
    //-----------------------------
 
639
    // Return translation component
 
640
    //-----------------------------
 
641
 
 
642
    const Vec3<T>       translation () const;
 
643
 
 
644
 
 
645
    //--------------------------
 
646
    // Translate the matrix by t
 
647
    //--------------------------
 
648
 
 
649
    template <class S>
 
650
    const Matrix44 &    translate (const Vec3<S> &t);
 
651
 
 
652
 
 
653
    //-------------------------------------------------------------
 
654
    // Set matrix to shear by given vector h.  The resulting matrix
 
655
    //    will shear x for each y coord. by a factor of h[0] ;
 
656
    //    will shear x for each z coord. by a factor of h[1] ;
 
657
    //    will shear y for each z coord. by a factor of h[2] .
 
658
    //-------------------------------------------------------------
 
659
 
 
660
    template <class S>
 
661
    const Matrix44 &    setShear (const Vec3<S> &h);
 
662
 
 
663
 
 
664
    //------------------------------------------------------------
 
665
    // Set matrix to shear by given factors.  The resulting matrix
 
666
    //    will shear x for each y coord. by a factor of h.xy ;
 
667
    //    will shear x for each z coord. by a factor of h.xz ;
 
668
    //    will shear y for each z coord. by a factor of h.yz ; 
 
669
    //    will shear y for each x coord. by a factor of h.yx ;
 
670
    //    will shear z for each x coord. by a factor of h.zx ;
 
671
    //    will shear z for each y coord. by a factor of h.zy .
 
672
    //------------------------------------------------------------
 
673
 
 
674
    template <class S>
 
675
    const Matrix44 &    setShear (const Shear6<S> &h);
 
676
 
 
677
 
 
678
    //--------------------------------------------------------
 
679
    // Shear the matrix by given vector.  The composed matrix 
 
680
    // will be <shear> * <this>, where the shear matrix ...
 
681
    //    will shear x for each y coord. by a factor of h[0] ;
 
682
    //    will shear x for each z coord. by a factor of h[1] ;
 
683
    //    will shear y for each z coord. by a factor of h[2] .
 
684
    //--------------------------------------------------------
 
685
 
 
686
    template <class S>
 
687
    const Matrix44 &    shear (const Vec3<S> &h);
 
688
 
 
689
 
 
690
    //------------------------------------------------------------
 
691
    // Shear the matrix by the given factors.  The composed matrix 
 
692
    // will be <shear> * <this>, where the shear matrix ...
 
693
    //    will shear x for each y coord. by a factor of h.xy ;
 
694
    //    will shear x for each z coord. by a factor of h.xz ;
 
695
    //    will shear y for each z coord. by a factor of h.yz ;
 
696
    //    will shear y for each x coord. by a factor of h.yx ;
 
697
    //    will shear z for each x coord. by a factor of h.zx ;
 
698
    //    will shear z for each y coord. by a factor of h.zy .
 
699
    //------------------------------------------------------------
 
700
 
 
701
    template <class S>
 
702
    const Matrix44 &    shear (const Shear6<S> &h);
 
703
 
 
704
 
 
705
    //-------------------------------------------------
 
706
    // Limitations of type T (see also class limits<T>)
 
707
    //-------------------------------------------------
 
708
 
 
709
    static T            baseTypeMin()           {return limits<T>::min();}
 
710
    static T            baseTypeMax()           {return limits<T>::max();}
 
711
    static T            baseTypeSmallest()      {return limits<T>::smallest();}
 
712
    static T            baseTypeEpsilon()       {return limits<T>::epsilon();}
 
713
};
 
714
 
 
715
 
 
716
//--------------
 
717
// Stream output
 
718
//--------------
 
719
 
 
720
template <class T>
 
721
std::ostream &  operator << (std::ostream & s, const Matrix33<T> &m); 
 
722
 
 
723
template <class T>
 
724
std::ostream &  operator << (std::ostream & s, const Matrix44<T> &m); 
 
725
 
 
726
 
 
727
//---------------------------------------------
 
728
// Vector-times-matrix multiplication operators
 
729
//---------------------------------------------
 
730
 
 
731
template <class S, class T>
 
732
const Vec2<S> &            operator *= (Vec2<S> &v, const Matrix33<T> &m);
 
733
 
 
734
template <class S, class T>
 
735
Vec2<S>                    operator * (const Vec2<S> &v, const Matrix33<T> &m);
 
736
 
 
737
template <class S, class T>
 
738
const Vec3<S> &            operator *= (Vec3<S> &v, const Matrix33<T> &m);
 
739
 
 
740
template <class S, class T>
 
741
Vec3<S>                    operator * (const Vec3<S> &v, const Matrix33<T> &m);
 
742
 
 
743
template <class S, class T>
 
744
const Vec3<S> &            operator *= (Vec3<S> &v, const Matrix44<T> &m);
 
745
 
 
746
template <class S, class T>
 
747
Vec3<S>                    operator * (const Vec3<S> &v, const Matrix44<T> &m);
 
748
 
 
749
 
 
750
//-------------------------
 
751
// Typedefs for convenience
 
752
//-------------------------
 
753
 
 
754
typedef Matrix33 <float>  M33f;
 
755
typedef Matrix33 <double> M33d;
 
756
typedef Matrix44 <float>  M44f;
 
757
typedef Matrix44 <double> M44d;
 
758
 
 
759
 
 
760
//---------------------------
 
761
// Implementation of Matrix33
 
762
//---------------------------
 
763
 
 
764
template <class T>
 
765
inline T *
 
766
Matrix33<T>::operator [] (int i)
 
767
{
 
768
    return x[i];
 
769
}
 
770
 
 
771
template <class T>
 
772
inline const T *
 
773
Matrix33<T>::operator [] (int i) const
 
774
{
 
775
    return x[i];
 
776
}
 
777
 
 
778
template <class T>
 
779
inline
 
780
Matrix33<T>::Matrix33 ()
 
781
{
 
782
    x[0][0] = 1;
 
783
    x[0][1] = 0;
 
784
    x[0][2] = 0;
 
785
    x[1][0] = 0;
 
786
    x[1][1] = 1;
 
787
    x[1][2] = 0;
 
788
    x[2][0] = 0;
 
789
    x[2][1] = 0;
 
790
    x[2][2] = 1;
 
791
}
 
792
 
 
793
template <class T>
 
794
inline
 
795
Matrix33<T>::Matrix33 (T a)
 
796
{
 
797
    x[0][0] = a;
 
798
    x[0][1] = a;
 
799
    x[0][2] = a;
 
800
    x[1][0] = a;
 
801
    x[1][1] = a;
 
802
    x[1][2] = a;
 
803
    x[2][0] = a;
 
804
    x[2][1] = a;
 
805
    x[2][2] = a;
 
806
}
 
807
 
 
808
template <class T>
 
809
inline
 
810
Matrix33<T>::Matrix33 (const T a[3][3]) 
 
811
{
 
812
    x[0][0] = a[0][0];
 
813
    x[0][1] = a[0][1];
 
814
    x[0][2] = a[0][2];
 
815
    x[1][0] = a[1][0];
 
816
    x[1][1] = a[1][1];
 
817
    x[1][2] = a[1][2];
 
818
    x[2][0] = a[2][0];
 
819
    x[2][1] = a[2][1];
 
820
    x[2][2] = a[2][2];
 
821
}
 
822
 
 
823
template <class T>
 
824
inline
 
825
Matrix33<T>::Matrix33 (T a, T b, T c, T d, T e, T f, T g, T h, T i)
 
826
{
 
827
    x[0][0] = a;
 
828
    x[0][1] = b;
 
829
    x[0][2] = c;
 
830
    x[1][0] = d;
 
831
    x[1][1] = e;
 
832
    x[1][2] = f;
 
833
    x[2][0] = g;
 
834
    x[2][1] = h;
 
835
    x[2][2] = i;
 
836
}
 
837
 
 
838
template <class T>
 
839
inline
 
840
Matrix33<T>::Matrix33 (const Matrix33 &v)
 
841
{
 
842
    x[0][0] = v.x[0][0];
 
843
    x[0][1] = v.x[0][1];
 
844
    x[0][2] = v.x[0][2];
 
845
    x[1][0] = v.x[1][0];
 
846
    x[1][1] = v.x[1][1];
 
847
    x[1][2] = v.x[1][2];
 
848
    x[2][0] = v.x[2][0];
 
849
    x[2][1] = v.x[2][1];
 
850
    x[2][2] = v.x[2][2];
 
851
}
 
852
 
 
853
template <class T>
 
854
inline const Matrix33<T> &
 
855
Matrix33<T>::operator = (const Matrix33 &v)
 
856
{
 
857
    x[0][0] = v.x[0][0];
 
858
    x[0][1] = v.x[0][1];
 
859
    x[0][2] = v.x[0][2];
 
860
    x[1][0] = v.x[1][0];
 
861
    x[1][1] = v.x[1][1];
 
862
    x[1][2] = v.x[1][2];
 
863
    x[2][0] = v.x[2][0];
 
864
    x[2][1] = v.x[2][1];
 
865
    x[2][2] = v.x[2][2];
 
866
    return *this;
 
867
}
 
868
 
 
869
template <class T>
 
870
inline const Matrix33<T> &
 
871
Matrix33<T>::operator = (T a)
 
872
{
 
873
    x[0][0] = a;
 
874
    x[0][1] = a;
 
875
    x[0][2] = a;
 
876
    x[1][0] = a;
 
877
    x[1][1] = a;
 
878
    x[1][2] = a;
 
879
    x[2][0] = a;
 
880
    x[2][1] = a;
 
881
    x[2][2] = a;
 
882
    return *this;
 
883
}
 
884
 
 
885
template <class T>
 
886
inline T *
 
887
Matrix33<T>::getValue ()
 
888
{
 
889
    return (T *) &x[0][0];
 
890
}
 
891
 
 
892
template <class T>
 
893
inline const T *
 
894
Matrix33<T>::getValue () const
 
895
{
 
896
    return (const T *) &x[0][0];
 
897
}
 
898
 
 
899
template <class T>
 
900
template <class S>
 
901
inline void
 
902
Matrix33<T>::getValue (Matrix33<S> &v) const
 
903
{
 
904
    v.x[0][0] = x[0][0];
 
905
    v.x[0][1] = x[0][1];
 
906
    v.x[0][2] = x[0][2];
 
907
    v.x[1][0] = x[1][0];
 
908
    v.x[1][1] = x[1][1];
 
909
    v.x[1][2] = x[1][2];
 
910
    v.x[2][0] = x[2][0];
 
911
    v.x[2][1] = x[2][1];
 
912
    v.x[2][2] = x[2][2];
 
913
}
 
914
 
 
915
template <class T>
 
916
template <class S>
 
917
inline Matrix33<T> &
 
918
Matrix33<T>::setValue (const Matrix33<S> &v)
 
919
{
 
920
    x[0][0] = v.x[0][0];
 
921
    x[0][1] = v.x[0][1];
 
922
    x[0][2] = v.x[0][2];
 
923
    x[1][0] = v.x[1][0];
 
924
    x[1][1] = v.x[1][1];
 
925
    x[1][2] = v.x[1][2];
 
926
    x[2][0] = v.x[2][0];
 
927
    x[2][1] = v.x[2][1];
 
928
    x[2][2] = v.x[2][2];
 
929
    return *this;
 
930
}
 
931
 
 
932
template <class T>
 
933
template <class S>
 
934
inline Matrix33<T> &
 
935
Matrix33<T>::setTheMatrix (const Matrix33<S> &v)
 
936
{
 
937
    x[0][0] = v.x[0][0];
 
938
    x[0][1] = v.x[0][1];
 
939
    x[0][2] = v.x[0][2];
 
940
    x[1][0] = v.x[1][0];
 
941
    x[1][1] = v.x[1][1];
 
942
    x[1][2] = v.x[1][2];
 
943
    x[2][0] = v.x[2][0];
 
944
    x[2][1] = v.x[2][1];
 
945
    x[2][2] = v.x[2][2];
 
946
    return *this;
 
947
}
 
948
 
 
949
template <class T>
 
950
inline void
 
951
Matrix33<T>::makeIdentity()
 
952
{
 
953
    x[0][0] = 1;
 
954
    x[0][1] = 0;
 
955
    x[0][2] = 0;
 
956
    x[1][0] = 0;
 
957
    x[1][1] = 1;
 
958
    x[1][2] = 0;
 
959
    x[2][0] = 0;
 
960
    x[2][1] = 0;
 
961
    x[2][2] = 1;
 
962
}
 
963
 
 
964
template <class T>
 
965
bool
 
966
Matrix33<T>::operator == (const Matrix33 &v) const
 
967
{
 
968
    return x[0][0] == v.x[0][0] &&
 
969
           x[0][1] == v.x[0][1] &&
 
970
           x[0][2] == v.x[0][2] &&
 
971
           x[1][0] == v.x[1][0] &&
 
972
           x[1][1] == v.x[1][1] &&
 
973
           x[1][2] == v.x[1][2] &&
 
974
           x[2][0] == v.x[2][0] &&
 
975
           x[2][1] == v.x[2][1] &&
 
976
           x[2][2] == v.x[2][2];
 
977
}
 
978
 
 
979
template <class T>
 
980
bool
 
981
Matrix33<T>::operator != (const Matrix33 &v) const
 
982
{
 
983
    return x[0][0] != v.x[0][0] ||
 
984
           x[0][1] != v.x[0][1] ||
 
985
           x[0][2] != v.x[0][2] ||
 
986
           x[1][0] != v.x[1][0] ||
 
987
           x[1][1] != v.x[1][1] ||
 
988
           x[1][2] != v.x[1][2] ||
 
989
           x[2][0] != v.x[2][0] ||
 
990
           x[2][1] != v.x[2][1] ||
 
991
           x[2][2] != v.x[2][2];
 
992
}
 
993
 
 
994
template <class T>
 
995
bool
 
996
Matrix33<T>::equalWithAbsError (const Matrix33<T> &m, T e) const
 
997
{
 
998
    for (int i = 0; i < 3; i++)
 
999
        for (int j = 0; j < 3; j++)
 
1000
            if (!Imath::equalWithAbsError ((*this)[i][j], m[i][j], e))
 
1001
                return false;
 
1002
 
 
1003
    return true;
 
1004
}
 
1005
 
 
1006
template <class T>
 
1007
bool
 
1008
Matrix33<T>::equalWithRelError (const Matrix33<T> &m, T e) const
 
1009
{
 
1010
    for (int i = 0; i < 3; i++)
 
1011
        for (int j = 0; j < 3; j++)
 
1012
            if (!Imath::equalWithRelError ((*this)[i][j], m[i][j], e))
 
1013
                return false;
 
1014
 
 
1015
    return true;
 
1016
}
 
1017
 
 
1018
template <class T>
 
1019
const Matrix33<T> &
 
1020
Matrix33<T>::operator += (const Matrix33<T> &v)
 
1021
{
 
1022
    x[0][0] += v.x[0][0];
 
1023
    x[0][1] += v.x[0][1];
 
1024
    x[0][2] += v.x[0][2];
 
1025
    x[1][0] += v.x[1][0];
 
1026
    x[1][1] += v.x[1][1];
 
1027
    x[1][2] += v.x[1][2];
 
1028
    x[2][0] += v.x[2][0];
 
1029
    x[2][1] += v.x[2][1];
 
1030
    x[2][2] += v.x[2][2];
 
1031
 
 
1032
    return *this;
 
1033
}
 
1034
 
 
1035
template <class T>
 
1036
const Matrix33<T> &
 
1037
Matrix33<T>::operator += (T a)
 
1038
{
 
1039
    x[0][0] += a;
 
1040
    x[0][1] += a;
 
1041
    x[0][2] += a;
 
1042
    x[1][0] += a;
 
1043
    x[1][1] += a;
 
1044
    x[1][2] += a;
 
1045
    x[2][0] += a;
 
1046
    x[2][1] += a;
 
1047
    x[2][2] += a;
 
1048
  
 
1049
    return *this;
 
1050
}
 
1051
 
 
1052
template <class T>
 
1053
Matrix33<T>
 
1054
Matrix33<T>::operator + (const Matrix33<T> &v) const
 
1055
{
 
1056
    return Matrix33 (x[0][0] + v.x[0][0],
 
1057
                     x[0][1] + v.x[0][1],
 
1058
                     x[0][2] + v.x[0][2],
 
1059
                     x[1][0] + v.x[1][0],
 
1060
                     x[1][1] + v.x[1][1],
 
1061
                     x[1][2] + v.x[1][2],
 
1062
                     x[2][0] + v.x[2][0],
 
1063
                     x[2][1] + v.x[2][1],
 
1064
                     x[2][2] + v.x[2][2]);
 
1065
}
 
1066
 
 
1067
template <class T>
 
1068
const Matrix33<T> &
 
1069
Matrix33<T>::operator -= (const Matrix33<T> &v)
 
1070
{
 
1071
    x[0][0] -= v.x[0][0];
 
1072
    x[0][1] -= v.x[0][1];
 
1073
    x[0][2] -= v.x[0][2];
 
1074
    x[1][0] -= v.x[1][0];
 
1075
    x[1][1] -= v.x[1][1];
 
1076
    x[1][2] -= v.x[1][2];
 
1077
    x[2][0] -= v.x[2][0];
 
1078
    x[2][1] -= v.x[2][1];
 
1079
    x[2][2] -= v.x[2][2];
 
1080
  
 
1081
    return *this;
 
1082
}
 
1083
 
 
1084
template <class T>
 
1085
const Matrix33<T> &
 
1086
Matrix33<T>::operator -= (T a)
 
1087
{
 
1088
    x[0][0] -= a;
 
1089
    x[0][1] -= a;
 
1090
    x[0][2] -= a;
 
1091
    x[1][0] -= a;
 
1092
    x[1][1] -= a;
 
1093
    x[1][2] -= a;
 
1094
    x[2][0] -= a;
 
1095
    x[2][1] -= a;
 
1096
    x[2][2] -= a;
 
1097
  
 
1098
    return *this;
 
1099
}
 
1100
 
 
1101
template <class T>
 
1102
Matrix33<T>
 
1103
Matrix33<T>::operator - (const Matrix33<T> &v) const
 
1104
{
 
1105
    return Matrix33 (x[0][0] - v.x[0][0],
 
1106
                     x[0][1] - v.x[0][1],
 
1107
                     x[0][2] - v.x[0][2],
 
1108
                     x[1][0] - v.x[1][0],
 
1109
                     x[1][1] - v.x[1][1],
 
1110
                     x[1][2] - v.x[1][2],
 
1111
                     x[2][0] - v.x[2][0],
 
1112
                     x[2][1] - v.x[2][1],
 
1113
                     x[2][2] - v.x[2][2]);
 
1114
}
 
1115
 
 
1116
template <class T>
 
1117
Matrix33<T>
 
1118
Matrix33<T>::operator - () const
 
1119
{
 
1120
    return Matrix33 (-x[0][0],
 
1121
                     -x[0][1],
 
1122
                     -x[0][2],
 
1123
                     -x[1][0],
 
1124
                     -x[1][1],
 
1125
                     -x[1][2],
 
1126
                     -x[2][0],
 
1127
                     -x[2][1],
 
1128
                     -x[2][2]);
 
1129
}
 
1130
 
 
1131
template <class T>
 
1132
const Matrix33<T> &
 
1133
Matrix33<T>::negate ()
 
1134
{
 
1135
    x[0][0] = -x[0][0];
 
1136
    x[0][1] = -x[0][1];
 
1137
    x[0][2] = -x[0][2];
 
1138
    x[1][0] = -x[1][0];
 
1139
    x[1][1] = -x[1][1];
 
1140
    x[1][2] = -x[1][2];
 
1141
    x[2][0] = -x[2][0];
 
1142
    x[2][1] = -x[2][1];
 
1143
    x[2][2] = -x[2][2];
 
1144
 
 
1145
    return *this;
 
1146
}
 
1147
 
 
1148
template <class T>
 
1149
const Matrix33<T> &
 
1150
Matrix33<T>::operator *= (T a)
 
1151
{
 
1152
    x[0][0] *= a;
 
1153
    x[0][1] *= a;
 
1154
    x[0][2] *= a;
 
1155
    x[1][0] *= a;
 
1156
    x[1][1] *= a;
 
1157
    x[1][2] *= a;
 
1158
    x[2][0] *= a;
 
1159
    x[2][1] *= a;
 
1160
    x[2][2] *= a;
 
1161
  
 
1162
    return *this;
 
1163
}
 
1164
 
 
1165
template <class T>
 
1166
Matrix33<T>
 
1167
Matrix33<T>::operator * (T a) const
 
1168
{
 
1169
    return Matrix33 (x[0][0] * a,
 
1170
                     x[0][1] * a,
 
1171
                     x[0][2] * a,
 
1172
                     x[1][0] * a,
 
1173
                     x[1][1] * a,
 
1174
                     x[1][2] * a,
 
1175
                     x[2][0] * a,
 
1176
                     x[2][1] * a,
 
1177
                     x[2][2] * a);
 
1178
}
 
1179
 
 
1180
template <class T>
 
1181
inline Matrix33<T>
 
1182
operator * (T a, const Matrix33<T> &v)
 
1183
{
 
1184
    return v * a;
 
1185
}
 
1186
 
 
1187
template <class T>
 
1188
const Matrix33<T> &
 
1189
Matrix33<T>::operator *= (const Matrix33<T> &v)
 
1190
{
 
1191
    Matrix33 tmp (T (0));
 
1192
 
 
1193
    for (int i = 0; i < 3; i++)
 
1194
        for (int j = 0; j < 3; j++)
 
1195
            for (int k = 0; k < 3; k++)
 
1196
                tmp.x[i][j] += x[i][k] * v.x[k][j];
 
1197
 
 
1198
    *this = tmp;
 
1199
    return *this;
 
1200
}
 
1201
 
 
1202
template <class T>
 
1203
Matrix33<T>
 
1204
Matrix33<T>::operator * (const Matrix33<T> &v) const
 
1205
{
 
1206
    Matrix33 tmp (T (0));
 
1207
 
 
1208
    for (int i = 0; i < 3; i++)
 
1209
        for (int j = 0; j < 3; j++)
 
1210
            for (int k = 0; k < 3; k++)
 
1211
                tmp.x[i][j] += x[i][k] * v.x[k][j];
 
1212
 
 
1213
    return tmp;
 
1214
}
 
1215
 
 
1216
template <class T>
 
1217
template <class S>
 
1218
void
 
1219
Matrix33<T>::multVecMatrix(const Vec2<S> &src, Vec2<S> &dst) const
 
1220
{
 
1221
    S a, b, w;
 
1222
 
 
1223
    a = src[0] * x[0][0] + src[1] * x[1][0] + x[2][0];
 
1224
    b = src[0] * x[0][1] + src[1] * x[1][1] + x[2][1];
 
1225
    w = src[0] * x[0][2] + src[1] * x[1][2] + x[2][2];
 
1226
 
 
1227
    dst.x = a / w;
 
1228
    dst.y = b / w;
 
1229
}
 
1230
 
 
1231
template <class T>
 
1232
template <class S>
 
1233
void
 
1234
Matrix33<T>::multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const
 
1235
{
 
1236
    S a, b;
 
1237
 
 
1238
    a = src[0] * x[0][0] + src[1] * x[1][0];
 
1239
    b = src[0] * x[0][1] + src[1] * x[1][1];
 
1240
 
 
1241
    dst.x = a;
 
1242
    dst.y = b;
 
1243
}
 
1244
 
 
1245
template <class T>
 
1246
const Matrix33<T> &
 
1247
Matrix33<T>::operator /= (T a)
 
1248
{
 
1249
    x[0][0] /= a;
 
1250
    x[0][1] /= a;
 
1251
    x[0][2] /= a;
 
1252
    x[1][0] /= a;
 
1253
    x[1][1] /= a;
 
1254
    x[1][2] /= a;
 
1255
    x[2][0] /= a;
 
1256
    x[2][1] /= a;
 
1257
    x[2][2] /= a;
 
1258
  
 
1259
    return *this;
 
1260
}
 
1261
 
 
1262
template <class T>
 
1263
Matrix33<T>
 
1264
Matrix33<T>::operator / (T a) const
 
1265
{
 
1266
    return Matrix33 (x[0][0] / a,
 
1267
                     x[0][1] / a,
 
1268
                     x[0][2] / a,
 
1269
                     x[1][0] / a,
 
1270
                     x[1][1] / a,
 
1271
                     x[1][2] / a,
 
1272
                     x[2][0] / a,
 
1273
                     x[2][1] / a,
 
1274
                     x[2][2] / a);
 
1275
}
 
1276
 
 
1277
template <class T>
 
1278
const Matrix33<T> &
 
1279
Matrix33<T>::transpose ()
 
1280
{
 
1281
    Matrix33 tmp (x[0][0],
 
1282
                  x[1][0],
 
1283
                  x[2][0],
 
1284
                  x[0][1],
 
1285
                  x[1][1],
 
1286
                  x[2][1],
 
1287
                  x[0][2],
 
1288
                  x[1][2],
 
1289
                  x[2][2]);
 
1290
    *this = tmp;
 
1291
    return *this;
 
1292
}
 
1293
 
 
1294
template <class T>
 
1295
Matrix33<T>
 
1296
Matrix33<T>::transposed () const
 
1297
{
 
1298
    return Matrix33 (x[0][0],
 
1299
                     x[1][0],
 
1300
                     x[2][0],
 
1301
                     x[0][1],
 
1302
                     x[1][1],
 
1303
                     x[2][1],
 
1304
                     x[0][2],
 
1305
                     x[1][2],
 
1306
                     x[2][2]);
 
1307
}
 
1308
 
 
1309
template <class T>
 
1310
const Matrix33<T> &
 
1311
Matrix33<T>::gjInvert (bool singExc) throw (Iex::MathExc)
 
1312
{
 
1313
    *this = gjInverse (singExc);
 
1314
    return *this;
 
1315
}
 
1316
 
 
1317
template <class T>
 
1318
Matrix33<T>
 
1319
Matrix33<T>::gjInverse (bool singExc) const throw (Iex::MathExc)
 
1320
{
 
1321
    int i, j, k;
 
1322
    Matrix33 s;
 
1323
    Matrix33 t (*this);
 
1324
 
 
1325
    // Forward elimination
 
1326
 
 
1327
    for (i = 0; i < 2 ; i++)
 
1328
    {
 
1329
        int pivot = i;
 
1330
 
 
1331
        T pivotsize = t[i][i];
 
1332
 
 
1333
        if (pivotsize < 0)
 
1334
            pivotsize = -pivotsize;
 
1335
 
 
1336
        for (j = i + 1; j < 3; j++)
 
1337
        {
 
1338
            T tmp = t[j][i];
 
1339
 
 
1340
            if (tmp < 0)
 
1341
                tmp = -tmp;
 
1342
 
 
1343
            if (tmp > pivotsize)
 
1344
            {
 
1345
                pivot = j;
 
1346
                pivotsize = tmp;
 
1347
            }
 
1348
        }
 
1349
 
 
1350
        if (pivotsize == 0)
 
1351
        {
 
1352
            if (singExc)
 
1353
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
 
1354
 
 
1355
            return Matrix33();
 
1356
        }
 
1357
 
 
1358
        if (pivot != i)
 
1359
        {
 
1360
            for (j = 0; j < 3; j++)
 
1361
            {
 
1362
                T tmp;
 
1363
 
 
1364
                tmp = t[i][j];
 
1365
                t[i][j] = t[pivot][j];
 
1366
                t[pivot][j] = tmp;
 
1367
 
 
1368
                tmp = s[i][j];
 
1369
                s[i][j] = s[pivot][j];
 
1370
                s[pivot][j] = tmp;
 
1371
            }
 
1372
        }
 
1373
 
 
1374
        for (j = i + 1; j < 3; j++)
 
1375
        {
 
1376
            T f = t[j][i] / t[i][i];
 
1377
 
 
1378
            for (k = 0; k < 3; k++)
 
1379
            {
 
1380
                t[j][k] -= f * t[i][k];
 
1381
                s[j][k] -= f * s[i][k];
 
1382
            }
 
1383
        }
 
1384
    }
 
1385
 
 
1386
    // Backward substitution
 
1387
 
 
1388
    for (i = 2; i >= 0; --i)
 
1389
    {
 
1390
        T f;
 
1391
 
 
1392
        if ((f = t[i][i]) == 0)
 
1393
        {
 
1394
            if (singExc)
 
1395
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
 
1396
 
 
1397
            return Matrix33();
 
1398
        }
 
1399
 
 
1400
        for (j = 0; j < 3; j++)
 
1401
        {
 
1402
            t[i][j] /= f;
 
1403
            s[i][j] /= f;
 
1404
        }
 
1405
 
 
1406
        for (j = 0; j < i; j++)
 
1407
        {
 
1408
            f = t[j][i];
 
1409
 
 
1410
            for (k = 0; k < 3; k++)
 
1411
            {
 
1412
                t[j][k] -= f * t[i][k];
 
1413
                s[j][k] -= f * s[i][k];
 
1414
            }
 
1415
        }
 
1416
    }
 
1417
 
 
1418
    return s;
 
1419
}
 
1420
 
 
1421
template <class T>
 
1422
const Matrix33<T> &
 
1423
Matrix33<T>::invert (bool singExc) throw (Iex::MathExc)
 
1424
{
 
1425
    *this = inverse (singExc);
 
1426
    return *this;
 
1427
}
 
1428
 
 
1429
template <class T>
 
1430
Matrix33<T>
 
1431
Matrix33<T>::inverse (bool singExc) const throw (Iex::MathExc)
 
1432
{
 
1433
    if (x[0][2] != 0 || x[1][2] != 0 || x[2][2] != 1)
 
1434
    {
 
1435
        Matrix33 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
 
1436
                    x[2][1] * x[0][2] - x[0][1] * x[2][2],
 
1437
                    x[0][1] * x[1][2] - x[1][1] * x[0][2],
 
1438
 
 
1439
                    x[2][0] * x[1][2] - x[1][0] * x[2][2],
 
1440
                    x[0][0] * x[2][2] - x[2][0] * x[0][2],
 
1441
                    x[1][0] * x[0][2] - x[0][0] * x[1][2],
 
1442
 
 
1443
                    x[1][0] * x[2][1] - x[2][0] * x[1][1],
 
1444
                    x[2][0] * x[0][1] - x[0][0] * x[2][1],
 
1445
                    x[0][0] * x[1][1] - x[1][0] * x[0][1]);
 
1446
 
 
1447
        T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
 
1448
 
 
1449
        if (Imath::abs (r) >= 1)
 
1450
        {
 
1451
            for (int i = 0; i < 3; ++i)
 
1452
            {
 
1453
                for (int j = 0; j < 3; ++j)
 
1454
                {
 
1455
                    s[i][j] /= r;
 
1456
                }
 
1457
            }
 
1458
        }
 
1459
        else
 
1460
        {
 
1461
            T mr = Imath::abs (r) / limits<T>::smallest();
 
1462
 
 
1463
            for (int i = 0; i < 3; ++i)
 
1464
            {
 
1465
                for (int j = 0; j < 3; ++j)
 
1466
                {
 
1467
                    if (mr > Imath::abs (s[i][j]))
 
1468
                    {
 
1469
                        s[i][j] /= r;
 
1470
                    }
 
1471
                    else
 
1472
                    {
 
1473
                        if (singExc)
 
1474
                            throw SingMatrixExc ("Cannot invert "
 
1475
                                                 "singular matrix.");
 
1476
                        return Matrix33();
 
1477
                    }
 
1478
                }
 
1479
            }
 
1480
        }
 
1481
 
 
1482
        return s;
 
1483
    }
 
1484
    else
 
1485
    {
 
1486
        Matrix33 s ( x[1][1],
 
1487
                    -x[0][1],
 
1488
                     0, 
 
1489
 
 
1490
                    -x[1][0],
 
1491
                     x[0][0],
 
1492
                     0,
 
1493
 
 
1494
                     0,
 
1495
                     0,
 
1496
                     1);
 
1497
 
 
1498
        T r = x[0][0] * x[1][1] - x[1][0] * x[0][1];
 
1499
 
 
1500
        if (Imath::abs (r) >= 1)
 
1501
        {
 
1502
            for (int i = 0; i < 2; ++i)
 
1503
            {
 
1504
                for (int j = 0; j < 2; ++j)
 
1505
                {
 
1506
                    s[i][j] /= r;
 
1507
                }
 
1508
            }
 
1509
        }
 
1510
        else
 
1511
        {
 
1512
            T mr = Imath::abs (r) / limits<T>::smallest();
 
1513
 
 
1514
            for (int i = 0; i < 2; ++i)
 
1515
            {
 
1516
                for (int j = 0; j < 2; ++j)
 
1517
                {
 
1518
                    if (mr > Imath::abs (s[i][j]))
 
1519
                    {
 
1520
                        s[i][j] /= r;
 
1521
                    }
 
1522
                    else
 
1523
                    {
 
1524
                        if (singExc)
 
1525
                            throw SingMatrixExc ("Cannot invert "
 
1526
                                                 "singular matrix.");
 
1527
                        return Matrix33();
 
1528
                    }
 
1529
                }
 
1530
            }
 
1531
        }
 
1532
 
 
1533
        s[2][0] = -x[2][0] * s[0][0] - x[2][1] * s[1][0];
 
1534
        s[2][1] = -x[2][0] * s[0][1] - x[2][1] * s[1][1];
 
1535
 
 
1536
        return s;
 
1537
    }
 
1538
}
 
1539
 
 
1540
template <class T>
 
1541
template <class S>
 
1542
const Matrix33<T> &
 
1543
Matrix33<T>::setRotation (S r)
 
1544
{
 
1545
    S cos_r, sin_r;
 
1546
 
 
1547
    cos_r = Math<T>::cos (r);
 
1548
    sin_r = Math<T>::sin (r);
 
1549
 
 
1550
    x[0][0] =  cos_r;
 
1551
    x[0][1] =  sin_r;
 
1552
    x[0][2] =  0;
 
1553
 
 
1554
    x[1][0] =  -sin_r;
 
1555
    x[1][1] =  cos_r;
 
1556
    x[1][2] =  0;
 
1557
 
 
1558
    x[2][0] =  0;
 
1559
    x[2][1] =  0;
 
1560
    x[2][2] =  1;
 
1561
 
 
1562
    return *this;
 
1563
}
 
1564
 
 
1565
template <class T>
 
1566
template <class S>
 
1567
const Matrix33<T> &
 
1568
Matrix33<T>::rotate (S r)
 
1569
{
 
1570
    *this *= Matrix33<T>().setRotation (r);
 
1571
    return *this;
 
1572
}
 
1573
 
 
1574
template <class T>
 
1575
const Matrix33<T> &
 
1576
Matrix33<T>::setScale (T s)
 
1577
{
 
1578
    x[0][0] = s;
 
1579
    x[0][1] = 0;
 
1580
    x[0][2] = 0;
 
1581
 
 
1582
    x[1][0] = 0;
 
1583
    x[1][1] = s;
 
1584
    x[1][2] = 0;
 
1585
 
 
1586
    x[2][0] = 0;
 
1587
    x[2][1] = 0;
 
1588
    x[2][2] = 1;
 
1589
 
 
1590
    return *this;
 
1591
}
 
1592
 
 
1593
template <class T>
 
1594
template <class S>
 
1595
const Matrix33<T> &
 
1596
Matrix33<T>::setScale (const Vec2<S> &s)
 
1597
{
 
1598
    x[0][0] = s[0];
 
1599
    x[0][1] = 0;
 
1600
    x[0][2] = 0;
 
1601
 
 
1602
    x[1][0] = 0;
 
1603
    x[1][1] = s[1];
 
1604
    x[1][2] = 0;
 
1605
 
 
1606
    x[2][0] = 0;
 
1607
    x[2][1] = 0;
 
1608
    x[2][2] = 1;
 
1609
 
 
1610
    return *this;
 
1611
}
 
1612
 
 
1613
template <class T>
 
1614
template <class S>
 
1615
const Matrix33<T> &
 
1616
Matrix33<T>::scale (const Vec2<S> &s)
 
1617
{
 
1618
    x[0][0] *= s[0];
 
1619
    x[0][1] *= s[0];
 
1620
    x[0][2] *= s[0];
 
1621
 
 
1622
    x[1][0] *= s[1];
 
1623
    x[1][1] *= s[1];
 
1624
    x[1][2] *= s[1];
 
1625
 
 
1626
    return *this;
 
1627
}
 
1628
 
 
1629
template <class T>
 
1630
template <class S>
 
1631
const Matrix33<T> &
 
1632
Matrix33<T>::setTranslation (const Vec2<S> &t)
 
1633
{
 
1634
    x[0][0] = 1;
 
1635
    x[0][1] = 0;
 
1636
    x[0][2] = 0;
 
1637
 
 
1638
    x[1][0] = 0;
 
1639
    x[1][1] = 1;
 
1640
    x[1][2] = 0;
 
1641
 
 
1642
    x[2][0] = t[0];
 
1643
    x[2][1] = t[1];
 
1644
    x[2][2] = 1;
 
1645
 
 
1646
    return *this;
 
1647
}
 
1648
 
 
1649
template <class T>
 
1650
inline Vec2<T> 
 
1651
Matrix33<T>::translation () const
 
1652
{
 
1653
    return Vec2<T> (x[2][0], x[2][1]);
 
1654
}
 
1655
 
 
1656
template <class T>
 
1657
template <class S>
 
1658
const Matrix33<T> &
 
1659
Matrix33<T>::translate (const Vec2<S> &t)
 
1660
{
 
1661
    x[2][0] += t[0] * x[0][0] + t[1] * x[1][0];
 
1662
    x[2][1] += t[0] * x[0][1] + t[1] * x[1][1];
 
1663
    x[2][2] += t[0] * x[0][2] + t[1] * x[1][2];
 
1664
 
 
1665
    return *this;
 
1666
}
 
1667
 
 
1668
template <class T>
 
1669
template <class S>
 
1670
const Matrix33<T> &
 
1671
Matrix33<T>::setShear (const S &xy)
 
1672
{
 
1673
    x[0][0] = 1;
 
1674
    x[0][1] = 0;
 
1675
    x[0][2] = 0;
 
1676
 
 
1677
    x[1][0] = xy;
 
1678
    x[1][1] = 1;
 
1679
    x[1][2] = 0;
 
1680
 
 
1681
    x[2][0] = 0;
 
1682
    x[2][1] = 0;
 
1683
    x[2][2] = 1;
 
1684
 
 
1685
    return *this;
 
1686
}
 
1687
 
 
1688
template <class T>
 
1689
template <class S>
 
1690
const Matrix33<T> &
 
1691
Matrix33<T>::setShear (const Vec2<S> &h)
 
1692
{
 
1693
    x[0][0] = 1;
 
1694
    x[0][1] = h[1];
 
1695
    x[0][2] = 0;
 
1696
 
 
1697
    x[1][0] = h[0];
 
1698
    x[1][1] = 1;
 
1699
    x[1][2] = 0;
 
1700
 
 
1701
    x[2][0] = 0;
 
1702
    x[2][1] = 0;
 
1703
    x[2][2] = 1;
 
1704
 
 
1705
    return *this;
 
1706
}
 
1707
 
 
1708
template <class T>
 
1709
template <class S>
 
1710
const Matrix33<T> &
 
1711
Matrix33<T>::shear (const S &xy)
 
1712
{
 
1713
    //
 
1714
    // In this case, we don't need a temp. copy of the matrix 
 
1715
    // because we never use a value on the RHS after we've 
 
1716
    // changed it on the LHS.
 
1717
    // 
 
1718
 
 
1719
    x[1][0] += xy * x[0][0];
 
1720
    x[1][1] += xy * x[0][1];
 
1721
    x[1][2] += xy * x[0][2];
 
1722
 
 
1723
    return *this;
 
1724
}
 
1725
 
 
1726
template <class T>
 
1727
template <class S>
 
1728
const Matrix33<T> &
 
1729
Matrix33<T>::shear (const Vec2<S> &h)
 
1730
{
 
1731
    Matrix33<T> P (*this);
 
1732
    
 
1733
    x[0][0] = P[0][0] + h[1] * P[1][0];
 
1734
    x[0][1] = P[0][1] + h[1] * P[1][1];
 
1735
    x[0][2] = P[0][2] + h[1] * P[1][2];
 
1736
    
 
1737
    x[1][0] = P[1][0] + h[0] * P[0][0];
 
1738
    x[1][1] = P[1][1] + h[0] * P[0][1];
 
1739
    x[1][2] = P[1][2] + h[0] * P[0][2];
 
1740
 
 
1741
    return *this;
 
1742
}
 
1743
 
 
1744
 
 
1745
//---------------------------
 
1746
// Implementation of Matrix44
 
1747
//---------------------------
 
1748
 
 
1749
template <class T>
 
1750
inline T *
 
1751
Matrix44<T>::operator [] (int i)
 
1752
{
 
1753
    return x[i];
 
1754
}
 
1755
 
 
1756
template <class T>
 
1757
inline const T *
 
1758
Matrix44<T>::operator [] (int i) const
 
1759
{
 
1760
    return x[i];
 
1761
}
 
1762
 
 
1763
template <class T>
 
1764
inline
 
1765
Matrix44<T>::Matrix44 ()
 
1766
{
 
1767
    x[0][0] = 1;
 
1768
    x[0][1] = 0;
 
1769
    x[0][2] = 0;
 
1770
    x[0][3] = 0;
 
1771
    x[1][0] = 0;
 
1772
    x[1][1] = 1;
 
1773
    x[1][2] = 0;
 
1774
    x[1][3] = 0;
 
1775
    x[2][0] = 0;
 
1776
    x[2][1] = 0;
 
1777
    x[2][2] = 1;
 
1778
    x[2][3] = 0;
 
1779
    x[3][0] = 0;
 
1780
    x[3][1] = 0;
 
1781
    x[3][2] = 0;
 
1782
    x[3][3] = 1;
 
1783
}
 
1784
 
 
1785
template <class T>
 
1786
inline
 
1787
Matrix44<T>::Matrix44 (T a)
 
1788
{
 
1789
    x[0][0] = a;
 
1790
    x[0][1] = a;
 
1791
    x[0][2] = a;
 
1792
    x[0][3] = a;
 
1793
    x[1][0] = a;
 
1794
    x[1][1] = a;
 
1795
    x[1][2] = a;
 
1796
    x[1][3] = a;
 
1797
    x[2][0] = a;
 
1798
    x[2][1] = a;
 
1799
    x[2][2] = a;
 
1800
    x[2][3] = a;
 
1801
    x[3][0] = a;
 
1802
    x[3][1] = a;
 
1803
    x[3][2] = a;
 
1804
    x[3][3] = a;
 
1805
}
 
1806
 
 
1807
template <class T>
 
1808
inline
 
1809
Matrix44<T>::Matrix44 (const T a[4][4]) 
 
1810
{
 
1811
    x[0][0] = a[0][0];
 
1812
    x[0][1] = a[0][1];
 
1813
    x[0][2] = a[0][2];
 
1814
    x[0][3] = a[0][3];
 
1815
    x[1][0] = a[1][0];
 
1816
    x[1][1] = a[1][1];
 
1817
    x[1][2] = a[1][2];
 
1818
    x[1][3] = a[1][3];
 
1819
    x[2][0] = a[2][0];
 
1820
    x[2][1] = a[2][1];
 
1821
    x[2][2] = a[2][2];
 
1822
    x[2][3] = a[2][3];
 
1823
    x[3][0] = a[3][0];
 
1824
    x[3][1] = a[3][1];
 
1825
    x[3][2] = a[3][2];
 
1826
    x[3][3] = a[3][3];
 
1827
}
 
1828
 
 
1829
template <class T>
 
1830
inline
 
1831
Matrix44<T>::Matrix44 (T a, T b, T c, T d, T e, T f, T g, T h,
 
1832
                       T i, T j, T k, T l, T m, T n, T o, T p)
 
1833
{
 
1834
    x[0][0] = a;
 
1835
    x[0][1] = b;
 
1836
    x[0][2] = c;
 
1837
    x[0][3] = d;
 
1838
    x[1][0] = e;
 
1839
    x[1][1] = f;
 
1840
    x[1][2] = g;
 
1841
    x[1][3] = h;
 
1842
    x[2][0] = i;
 
1843
    x[2][1] = j;
 
1844
    x[2][2] = k;
 
1845
    x[2][3] = l;
 
1846
    x[3][0] = m;
 
1847
    x[3][1] = n;
 
1848
    x[3][2] = o;
 
1849
    x[3][3] = p;
 
1850
}
 
1851
 
 
1852
 
 
1853
template <class T>
 
1854
inline
 
1855
Matrix44<T>::Matrix44 (Matrix33<T> r, Vec3<T> t)
 
1856
{
 
1857
    x[0][0] = r[0][0];
 
1858
    x[0][1] = r[0][1];
 
1859
    x[0][2] = r[0][2];
 
1860
    x[0][3] = 0;
 
1861
    x[1][0] = r[1][0];
 
1862
    x[1][1] = r[1][1];
 
1863
    x[1][2] = r[1][2];
 
1864
    x[1][3] = 0;
 
1865
    x[2][0] = r[2][0];
 
1866
    x[2][1] = r[2][1];
 
1867
    x[2][2] = r[2][2];
 
1868
    x[2][3] = 0;
 
1869
    x[3][0] = t[0];
 
1870
    x[3][1] = t[1];
 
1871
    x[3][2] = t[2];
 
1872
    x[3][3] = 1;
 
1873
}
 
1874
 
 
1875
template <class T>
 
1876
inline
 
1877
Matrix44<T>::Matrix44 (const Matrix44 &v)
 
1878
{
 
1879
    x[0][0] = v.x[0][0];
 
1880
    x[0][1] = v.x[0][1];
 
1881
    x[0][2] = v.x[0][2];
 
1882
    x[0][3] = v.x[0][3];
 
1883
    x[1][0] = v.x[1][0];
 
1884
    x[1][1] = v.x[1][1];
 
1885
    x[1][2] = v.x[1][2];
 
1886
    x[1][3] = v.x[1][3];
 
1887
    x[2][0] = v.x[2][0];
 
1888
    x[2][1] = v.x[2][1];
 
1889
    x[2][2] = v.x[2][2];
 
1890
    x[2][3] = v.x[2][3];
 
1891
    x[3][0] = v.x[3][0];
 
1892
    x[3][1] = v.x[3][1];
 
1893
    x[3][2] = v.x[3][2];
 
1894
    x[3][3] = v.x[3][3];
 
1895
}
 
1896
 
 
1897
template <class T>
 
1898
inline const Matrix44<T> &
 
1899
Matrix44<T>::operator = (const Matrix44 &v)
 
1900
{
 
1901
    x[0][0] = v.x[0][0];
 
1902
    x[0][1] = v.x[0][1];
 
1903
    x[0][2] = v.x[0][2];
 
1904
    x[0][3] = v.x[0][3];
 
1905
    x[1][0] = v.x[1][0];
 
1906
    x[1][1] = v.x[1][1];
 
1907
    x[1][2] = v.x[1][2];
 
1908
    x[1][3] = v.x[1][3];
 
1909
    x[2][0] = v.x[2][0];
 
1910
    x[2][1] = v.x[2][1];
 
1911
    x[2][2] = v.x[2][2];
 
1912
    x[2][3] = v.x[2][3];
 
1913
    x[3][0] = v.x[3][0];
 
1914
    x[3][1] = v.x[3][1];
 
1915
    x[3][2] = v.x[3][2];
 
1916
    x[3][3] = v.x[3][3];
 
1917
    return *this;
 
1918
}
 
1919
 
 
1920
template <class T>
 
1921
inline const Matrix44<T> &
 
1922
Matrix44<T>::operator = (T a)
 
1923
{
 
1924
    x[0][0] = a;
 
1925
    x[0][1] = a;
 
1926
    x[0][2] = a;
 
1927
    x[0][3] = a;
 
1928
    x[1][0] = a;
 
1929
    x[1][1] = a;
 
1930
    x[1][2] = a;
 
1931
    x[1][3] = a;
 
1932
    x[2][0] = a;
 
1933
    x[2][1] = a;
 
1934
    x[2][2] = a;
 
1935
    x[2][3] = a;
 
1936
    x[3][0] = a;
 
1937
    x[3][1] = a;
 
1938
    x[3][2] = a;
 
1939
    x[3][3] = a;
 
1940
    return *this;
 
1941
}
 
1942
 
 
1943
template <class T>
 
1944
inline T *
 
1945
Matrix44<T>::getValue ()
 
1946
{
 
1947
    return (T *) &x[0][0];
 
1948
}
 
1949
 
 
1950
template <class T>
 
1951
inline const T *
 
1952
Matrix44<T>::getValue () const
 
1953
{
 
1954
    return (const T *) &x[0][0];
 
1955
}
 
1956
 
 
1957
template <class T>
 
1958
template <class S>
 
1959
inline void
 
1960
Matrix44<T>::getValue (Matrix44<S> &v) const
 
1961
{
 
1962
    v.x[0][0] = x[0][0];
 
1963
    v.x[0][1] = x[0][1];
 
1964
    v.x[0][2] = x[0][2];
 
1965
    v.x[0][3] = x[0][3];
 
1966
    v.x[1][0] = x[1][0];
 
1967
    v.x[1][1] = x[1][1];
 
1968
    v.x[1][2] = x[1][2];
 
1969
    v.x[1][3] = x[1][3];
 
1970
    v.x[2][0] = x[2][0];
 
1971
    v.x[2][1] = x[2][1];
 
1972
    v.x[2][2] = x[2][2];
 
1973
    v.x[2][3] = x[2][3];
 
1974
    v.x[3][0] = x[3][0];
 
1975
    v.x[3][1] = x[3][1];
 
1976
    v.x[3][2] = x[3][2];
 
1977
    v.x[3][3] = x[3][3];
 
1978
}
 
1979
 
 
1980
template <class T>
 
1981
template <class S>
 
1982
inline Matrix44<T> &
 
1983
Matrix44<T>::setValue (const Matrix44<S> &v)
 
1984
{
 
1985
    x[0][0] = v.x[0][0];
 
1986
    x[0][1] = v.x[0][1];
 
1987
    x[0][2] = v.x[0][2];
 
1988
    x[0][3] = v.x[0][3];
 
1989
    x[1][0] = v.x[1][0];
 
1990
    x[1][1] = v.x[1][1];
 
1991
    x[1][2] = v.x[1][2];
 
1992
    x[1][3] = v.x[1][3];
 
1993
    x[2][0] = v.x[2][0];
 
1994
    x[2][1] = v.x[2][1];
 
1995
    x[2][2] = v.x[2][2];
 
1996
    x[2][3] = v.x[2][3];
 
1997
    x[3][0] = v.x[3][0];
 
1998
    x[3][1] = v.x[3][1];
 
1999
    x[3][2] = v.x[3][2];
 
2000
    x[3][3] = v.x[3][3];
 
2001
    return *this;
 
2002
}
 
2003
 
 
2004
template <class T>
 
2005
template <class S>
 
2006
inline Matrix44<T> &
 
2007
Matrix44<T>::setTheMatrix (const Matrix44<S> &v)
 
2008
{
 
2009
    x[0][0] = v.x[0][0];
 
2010
    x[0][1] = v.x[0][1];
 
2011
    x[0][2] = v.x[0][2];
 
2012
    x[0][3] = v.x[0][3];
 
2013
    x[1][0] = v.x[1][0];
 
2014
    x[1][1] = v.x[1][1];
 
2015
    x[1][2] = v.x[1][2];
 
2016
    x[1][3] = v.x[1][3];
 
2017
    x[2][0] = v.x[2][0];
 
2018
    x[2][1] = v.x[2][1];
 
2019
    x[2][2] = v.x[2][2];
 
2020
    x[2][3] = v.x[2][3];
 
2021
    x[3][0] = v.x[3][0];
 
2022
    x[3][1] = v.x[3][1];
 
2023
    x[3][2] = v.x[3][2];
 
2024
    x[3][3] = v.x[3][3];
 
2025
    return *this;
 
2026
}
 
2027
 
 
2028
template <class T>
 
2029
inline void
 
2030
Matrix44<T>::makeIdentity()
 
2031
{
 
2032
    x[0][0] = 1;
 
2033
    x[0][1] = 0;
 
2034
    x[0][2] = 0;
 
2035
    x[0][3] = 0;
 
2036
    x[1][0] = 0;
 
2037
    x[1][1] = 1;
 
2038
    x[1][2] = 0;
 
2039
    x[1][3] = 0;
 
2040
    x[2][0] = 0;
 
2041
    x[2][1] = 0;
 
2042
    x[2][2] = 1;
 
2043
    x[2][3] = 0;
 
2044
    x[3][0] = 0;
 
2045
    x[3][1] = 0;
 
2046
    x[3][2] = 0;
 
2047
    x[3][3] = 1;
 
2048
}
 
2049
 
 
2050
template <class T>
 
2051
bool
 
2052
Matrix44<T>::operator == (const Matrix44 &v) const
 
2053
{
 
2054
    return x[0][0] == v.x[0][0] &&
 
2055
           x[0][1] == v.x[0][1] &&
 
2056
           x[0][2] == v.x[0][2] &&
 
2057
           x[0][3] == v.x[0][3] &&
 
2058
           x[1][0] == v.x[1][0] &&
 
2059
           x[1][1] == v.x[1][1] &&
 
2060
           x[1][2] == v.x[1][2] &&
 
2061
           x[1][3] == v.x[1][3] &&
 
2062
           x[2][0] == v.x[2][0] &&
 
2063
           x[2][1] == v.x[2][1] &&
 
2064
           x[2][2] == v.x[2][2] &&
 
2065
           x[2][3] == v.x[2][3] &&
 
2066
           x[3][0] == v.x[3][0] &&
 
2067
           x[3][1] == v.x[3][1] &&
 
2068
           x[3][2] == v.x[3][2] &&
 
2069
           x[3][3] == v.x[3][3];
 
2070
}
 
2071
 
 
2072
template <class T>
 
2073
bool
 
2074
Matrix44<T>::operator != (const Matrix44 &v) const
 
2075
{
 
2076
    return x[0][0] != v.x[0][0] ||
 
2077
           x[0][1] != v.x[0][1] ||
 
2078
           x[0][2] != v.x[0][2] ||
 
2079
           x[0][3] != v.x[0][3] ||
 
2080
           x[1][0] != v.x[1][0] ||
 
2081
           x[1][1] != v.x[1][1] ||
 
2082
           x[1][2] != v.x[1][2] ||
 
2083
           x[1][3] != v.x[1][3] ||
 
2084
           x[2][0] != v.x[2][0] ||
 
2085
           x[2][1] != v.x[2][1] ||
 
2086
           x[2][2] != v.x[2][2] ||
 
2087
           x[2][3] != v.x[2][3] ||
 
2088
           x[3][0] != v.x[3][0] ||
 
2089
           x[3][1] != v.x[3][1] ||
 
2090
           x[3][2] != v.x[3][2] ||
 
2091
           x[3][3] != v.x[3][3];
 
2092
}
 
2093
 
 
2094
template <class T>
 
2095
bool
 
2096
Matrix44<T>::equalWithAbsError (const Matrix44<T> &m, T e) const
 
2097
{
 
2098
    for (int i = 0; i < 4; i++)
 
2099
        for (int j = 0; j < 4; j++)
 
2100
            if (!Imath::equalWithAbsError ((*this)[i][j], m[i][j], e))
 
2101
                return false;
 
2102
 
 
2103
    return true;
 
2104
}
 
2105
 
 
2106
template <class T>
 
2107
bool
 
2108
Matrix44<T>::equalWithRelError (const Matrix44<T> &m, T e) const
 
2109
{
 
2110
    for (int i = 0; i < 4; i++)
 
2111
        for (int j = 0; j < 4; j++)
 
2112
            if (!Imath::equalWithRelError ((*this)[i][j], m[i][j], e))
 
2113
                return false;
 
2114
 
 
2115
    return true;
 
2116
}
 
2117
 
 
2118
template <class T>
 
2119
const Matrix44<T> &
 
2120
Matrix44<T>::operator += (const Matrix44<T> &v)
 
2121
{
 
2122
    x[0][0] += v.x[0][0];
 
2123
    x[0][1] += v.x[0][1];
 
2124
    x[0][2] += v.x[0][2];
 
2125
    x[0][3] += v.x[0][3];
 
2126
    x[1][0] += v.x[1][0];
 
2127
    x[1][1] += v.x[1][1];
 
2128
    x[1][2] += v.x[1][2];
 
2129
    x[1][3] += v.x[1][3];
 
2130
    x[2][0] += v.x[2][0];
 
2131
    x[2][1] += v.x[2][1];
 
2132
    x[2][2] += v.x[2][2];
 
2133
    x[2][3] += v.x[2][3];
 
2134
    x[3][0] += v.x[3][0];
 
2135
    x[3][1] += v.x[3][1];
 
2136
    x[3][2] += v.x[3][2];
 
2137
    x[3][3] += v.x[3][3];
 
2138
 
 
2139
    return *this;
 
2140
}
 
2141
 
 
2142
template <class T>
 
2143
const Matrix44<T> &
 
2144
Matrix44<T>::operator += (T a)
 
2145
{
 
2146
    x[0][0] += a;
 
2147
    x[0][1] += a;
 
2148
    x[0][2] += a;
 
2149
    x[0][3] += a;
 
2150
    x[1][0] += a;
 
2151
    x[1][1] += a;
 
2152
    x[1][2] += a;
 
2153
    x[1][3] += a;
 
2154
    x[2][0] += a;
 
2155
    x[2][1] += a;
 
2156
    x[2][2] += a;
 
2157
    x[2][3] += a;
 
2158
    x[3][0] += a;
 
2159
    x[3][1] += a;
 
2160
    x[3][2] += a;
 
2161
    x[3][3] += a;
 
2162
 
 
2163
    return *this;
 
2164
}
 
2165
 
 
2166
template <class T>
 
2167
Matrix44<T>
 
2168
Matrix44<T>::operator + (const Matrix44<T> &v) const
 
2169
{
 
2170
    return Matrix44 (x[0][0] + v.x[0][0],
 
2171
                     x[0][1] + v.x[0][1],
 
2172
                     x[0][2] + v.x[0][2],
 
2173
                     x[0][3] + v.x[0][3],
 
2174
                     x[1][0] + v.x[1][0],
 
2175
                     x[1][1] + v.x[1][1],
 
2176
                     x[1][2] + v.x[1][2],
 
2177
                     x[1][3] + v.x[1][3],
 
2178
                     x[2][0] + v.x[2][0],
 
2179
                     x[2][1] + v.x[2][1],
 
2180
                     x[2][2] + v.x[2][2],
 
2181
                     x[2][3] + v.x[2][3],
 
2182
                     x[3][0] + v.x[3][0],
 
2183
                     x[3][1] + v.x[3][1],
 
2184
                     x[3][2] + v.x[3][2],
 
2185
                     x[3][3] + v.x[3][3]);
 
2186
}
 
2187
 
 
2188
template <class T>
 
2189
const Matrix44<T> &
 
2190
Matrix44<T>::operator -= (const Matrix44<T> &v)
 
2191
{
 
2192
    x[0][0] -= v.x[0][0];
 
2193
    x[0][1] -= v.x[0][1];
 
2194
    x[0][2] -= v.x[0][2];
 
2195
    x[0][3] -= v.x[0][3];
 
2196
    x[1][0] -= v.x[1][0];
 
2197
    x[1][1] -= v.x[1][1];
 
2198
    x[1][2] -= v.x[1][2];
 
2199
    x[1][3] -= v.x[1][3];
 
2200
    x[2][0] -= v.x[2][0];
 
2201
    x[2][1] -= v.x[2][1];
 
2202
    x[2][2] -= v.x[2][2];
 
2203
    x[2][3] -= v.x[2][3];
 
2204
    x[3][0] -= v.x[3][0];
 
2205
    x[3][1] -= v.x[3][1];
 
2206
    x[3][2] -= v.x[3][2];
 
2207
    x[3][3] -= v.x[3][3];
 
2208
 
 
2209
    return *this;
 
2210
}
 
2211
 
 
2212
template <class T>
 
2213
const Matrix44<T> &
 
2214
Matrix44<T>::operator -= (T a)
 
2215
{
 
2216
    x[0][0] -= a;
 
2217
    x[0][1] -= a;
 
2218
    x[0][2] -= a;
 
2219
    x[0][3] -= a;
 
2220
    x[1][0] -= a;
 
2221
    x[1][1] -= a;
 
2222
    x[1][2] -= a;
 
2223
    x[1][3] -= a;
 
2224
    x[2][0] -= a;
 
2225
    x[2][1] -= a;
 
2226
    x[2][2] -= a;
 
2227
    x[2][3] -= a;
 
2228
    x[3][0] -= a;
 
2229
    x[3][1] -= a;
 
2230
    x[3][2] -= a;
 
2231
    x[3][3] -= a;
 
2232
 
 
2233
    return *this;
 
2234
}
 
2235
 
 
2236
template <class T>
 
2237
Matrix44<T>
 
2238
Matrix44<T>::operator - (const Matrix44<T> &v) const
 
2239
{
 
2240
    return Matrix44 (x[0][0] - v.x[0][0],
 
2241
                     x[0][1] - v.x[0][1],
 
2242
                     x[0][2] - v.x[0][2],
 
2243
                     x[0][3] - v.x[0][3],
 
2244
                     x[1][0] - v.x[1][0],
 
2245
                     x[1][1] - v.x[1][1],
 
2246
                     x[1][2] - v.x[1][2],
 
2247
                     x[1][3] - v.x[1][3],
 
2248
                     x[2][0] - v.x[2][0],
 
2249
                     x[2][1] - v.x[2][1],
 
2250
                     x[2][2] - v.x[2][2],
 
2251
                     x[2][3] - v.x[2][3],
 
2252
                     x[3][0] - v.x[3][0],
 
2253
                     x[3][1] - v.x[3][1],
 
2254
                     x[3][2] - v.x[3][2],
 
2255
                     x[3][3] - v.x[3][3]);
 
2256
}
 
2257
 
 
2258
template <class T>
 
2259
Matrix44<T>
 
2260
Matrix44<T>::operator - () const
 
2261
{
 
2262
    return Matrix44 (-x[0][0],
 
2263
                     -x[0][1],
 
2264
                     -x[0][2],
 
2265
                     -x[0][3],
 
2266
                     -x[1][0],
 
2267
                     -x[1][1],
 
2268
                     -x[1][2],
 
2269
                     -x[1][3],
 
2270
                     -x[2][0],
 
2271
                     -x[2][1],
 
2272
                     -x[2][2],
 
2273
                     -x[2][3],
 
2274
                     -x[3][0],
 
2275
                     -x[3][1],
 
2276
                     -x[3][2],
 
2277
                     -x[3][3]);
 
2278
}
 
2279
 
 
2280
template <class T>
 
2281
const Matrix44<T> &
 
2282
Matrix44<T>::negate ()
 
2283
{
 
2284
    x[0][0] = -x[0][0];
 
2285
    x[0][1] = -x[0][1];
 
2286
    x[0][2] = -x[0][2];
 
2287
    x[0][3] = -x[0][3];
 
2288
    x[1][0] = -x[1][0];
 
2289
    x[1][1] = -x[1][1];
 
2290
    x[1][2] = -x[1][2];
 
2291
    x[1][3] = -x[1][3];
 
2292
    x[2][0] = -x[2][0];
 
2293
    x[2][1] = -x[2][1];
 
2294
    x[2][2] = -x[2][2];
 
2295
    x[2][3] = -x[2][3];
 
2296
    x[3][0] = -x[3][0];
 
2297
    x[3][1] = -x[3][1];
 
2298
    x[3][2] = -x[3][2];
 
2299
    x[3][3] = -x[3][3];
 
2300
 
 
2301
    return *this;
 
2302
}
 
2303
 
 
2304
template <class T>
 
2305
const Matrix44<T> &
 
2306
Matrix44<T>::operator *= (T a)
 
2307
{
 
2308
    x[0][0] *= a;
 
2309
    x[0][1] *= a;
 
2310
    x[0][2] *= a;
 
2311
    x[0][3] *= a;
 
2312
    x[1][0] *= a;
 
2313
    x[1][1] *= a;
 
2314
    x[1][2] *= a;
 
2315
    x[1][3] *= a;
 
2316
    x[2][0] *= a;
 
2317
    x[2][1] *= a;
 
2318
    x[2][2] *= a;
 
2319
    x[2][3] *= a;
 
2320
    x[3][0] *= a;
 
2321
    x[3][1] *= a;
 
2322
    x[3][2] *= a;
 
2323
    x[3][3] *= a;
 
2324
 
 
2325
    return *this;
 
2326
}
 
2327
 
 
2328
template <class T>
 
2329
Matrix44<T>
 
2330
Matrix44<T>::operator * (T a) const
 
2331
{
 
2332
    return Matrix44 (x[0][0] * a,
 
2333
                     x[0][1] * a,
 
2334
                     x[0][2] * a,
 
2335
                     x[0][3] * a,
 
2336
                     x[1][0] * a,
 
2337
                     x[1][1] * a,
 
2338
                     x[1][2] * a,
 
2339
                     x[1][3] * a,
 
2340
                     x[2][0] * a,
 
2341
                     x[2][1] * a,
 
2342
                     x[2][2] * a,
 
2343
                     x[2][3] * a,
 
2344
                     x[3][0] * a,
 
2345
                     x[3][1] * a,
 
2346
                     x[3][2] * a,
 
2347
                     x[3][3] * a);
 
2348
}
 
2349
 
 
2350
template <class T>
 
2351
inline Matrix44<T>
 
2352
operator * (T a, const Matrix44<T> &v)
 
2353
{
 
2354
    return v * a;
 
2355
}
 
2356
 
 
2357
template <class T>
 
2358
inline const Matrix44<T> &
 
2359
Matrix44<T>::operator *= (const Matrix44<T> &v)
 
2360
{
 
2361
    Matrix44 tmp (T (0));
 
2362
 
 
2363
    multiply (*this, v, tmp);
 
2364
    *this = tmp;
 
2365
    return *this;
 
2366
}
 
2367
 
 
2368
template <class T>
 
2369
inline Matrix44<T>
 
2370
Matrix44<T>::operator * (const Matrix44<T> &v) const
 
2371
{
 
2372
    Matrix44 tmp (T (0));
 
2373
 
 
2374
    multiply (*this, v, tmp);
 
2375
    return tmp;
 
2376
}
 
2377
 
 
2378
template <class T>
 
2379
void
 
2380
Matrix44<T>::multiply (const Matrix44<T> &a,
 
2381
                       const Matrix44<T> &b,
 
2382
                       Matrix44<T> &c)
 
2383
{
 
2384
    register const T * restrict ap = &a.x[0][0];
 
2385
    register const T * restrict bp = &b.x[0][0];
 
2386
    register       T * restrict cp = &c.x[0][0];
 
2387
 
 
2388
    register T a0, a1, a2, a3;
 
2389
 
 
2390
    a0 = ap[0];
 
2391
    a1 = ap[1];
 
2392
    a2 = ap[2];
 
2393
    a3 = ap[3];
 
2394
 
 
2395
    cp[0]  = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
 
2396
    cp[1]  = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
 
2397
    cp[2]  = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
 
2398
    cp[3]  = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
 
2399
 
 
2400
    a0 = ap[4];
 
2401
    a1 = ap[5];
 
2402
    a2 = ap[6];
 
2403
    a3 = ap[7];
 
2404
 
 
2405
    cp[4]  = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
 
2406
    cp[5]  = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
 
2407
    cp[6]  = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
 
2408
    cp[7]  = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
 
2409
 
 
2410
    a0 = ap[8];
 
2411
    a1 = ap[9];
 
2412
    a2 = ap[10];
 
2413
    a3 = ap[11];
 
2414
 
 
2415
    cp[8]  = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
 
2416
    cp[9]  = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
 
2417
    cp[10] = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
 
2418
    cp[11] = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
 
2419
 
 
2420
    a0 = ap[12];
 
2421
    a1 = ap[13];
 
2422
    a2 = ap[14];
 
2423
    a3 = ap[15];
 
2424
 
 
2425
    cp[12] = a0 * bp[0]  + a1 * bp[4]  + a2 * bp[8]  + a3 * bp[12];
 
2426
    cp[13] = a0 * bp[1]  + a1 * bp[5]  + a2 * bp[9]  + a3 * bp[13];
 
2427
    cp[14] = a0 * bp[2]  + a1 * bp[6]  + a2 * bp[10] + a3 * bp[14];
 
2428
    cp[15] = a0 * bp[3]  + a1 * bp[7]  + a2 * bp[11] + a3 * bp[15];
 
2429
}
 
2430
 
 
2431
template <class T> template <class S>
 
2432
void
 
2433
Matrix44<T>::multVecMatrix(const Vec3<S> &src, Vec3<S> &dst) const
 
2434
{
 
2435
    S a, b, c, w;
 
2436
 
 
2437
    a = src[0] * x[0][0] + src[1] * x[1][0] + src[2] * x[2][0] + x[3][0];
 
2438
    b = src[0] * x[0][1] + src[1] * x[1][1] + src[2] * x[2][1] + x[3][1];
 
2439
    c = src[0] * x[0][2] + src[1] * x[1][2] + src[2] * x[2][2] + x[3][2];
 
2440
    w = src[0] * x[0][3] + src[1] * x[1][3] + src[2] * x[2][3] + x[3][3];
 
2441
 
 
2442
    dst.x = a / w;
 
2443
    dst.y = b / w;
 
2444
    dst.z = c / w;
 
2445
}
 
2446
 
 
2447
template <class T> template <class S>
 
2448
void
 
2449
Matrix44<T>::multDirMatrix(const Vec3<S> &src, Vec3<S> &dst) const
 
2450
{
 
2451
    S a, b, c;
 
2452
 
 
2453
    a = src[0] * x[0][0] + src[1] * x[1][0] + src[2] * x[2][0];
 
2454
    b = src[0] * x[0][1] + src[1] * x[1][1] + src[2] * x[2][1];
 
2455
    c = src[0] * x[0][2] + src[1] * x[1][2] + src[2] * x[2][2];
 
2456
 
 
2457
    dst.x = a;
 
2458
    dst.y = b;
 
2459
    dst.z = c;
 
2460
}
 
2461
 
 
2462
template <class T>
 
2463
const Matrix44<T> &
 
2464
Matrix44<T>::operator /= (T a)
 
2465
{
 
2466
    x[0][0] /= a;
 
2467
    x[0][1] /= a;
 
2468
    x[0][2] /= a;
 
2469
    x[0][3] /= a;
 
2470
    x[1][0] /= a;
 
2471
    x[1][1] /= a;
 
2472
    x[1][2] /= a;
 
2473
    x[1][3] /= a;
 
2474
    x[2][0] /= a;
 
2475
    x[2][1] /= a;
 
2476
    x[2][2] /= a;
 
2477
    x[2][3] /= a;
 
2478
    x[3][0] /= a;
 
2479
    x[3][1] /= a;
 
2480
    x[3][2] /= a;
 
2481
    x[3][3] /= a;
 
2482
 
 
2483
    return *this;
 
2484
}
 
2485
 
 
2486
template <class T>
 
2487
Matrix44<T>
 
2488
Matrix44<T>::operator / (T a) const
 
2489
{
 
2490
    return Matrix44 (x[0][0] / a,
 
2491
                     x[0][1] / a,
 
2492
                     x[0][2] / a,
 
2493
                     x[0][3] / a,
 
2494
                     x[1][0] / a,
 
2495
                     x[1][1] / a,
 
2496
                     x[1][2] / a,
 
2497
                     x[1][3] / a,
 
2498
                     x[2][0] / a,
 
2499
                     x[2][1] / a,
 
2500
                     x[2][2] / a,
 
2501
                     x[2][3] / a,
 
2502
                     x[3][0] / a,
 
2503
                     x[3][1] / a,
 
2504
                     x[3][2] / a,
 
2505
                     x[3][3] / a);
 
2506
}
 
2507
 
 
2508
template <class T>
 
2509
const Matrix44<T> &
 
2510
Matrix44<T>::transpose ()
 
2511
{
 
2512
    Matrix44 tmp (x[0][0],
 
2513
                  x[1][0],
 
2514
                  x[2][0],
 
2515
                  x[3][0],
 
2516
                  x[0][1],
 
2517
                  x[1][1],
 
2518
                  x[2][1],
 
2519
                  x[3][1],
 
2520
                  x[0][2],
 
2521
                  x[1][2],
 
2522
                  x[2][2],
 
2523
                  x[3][2],
 
2524
                  x[0][3],
 
2525
                  x[1][3],
 
2526
                  x[2][3],
 
2527
                  x[3][3]);
 
2528
    *this = tmp;
 
2529
    return *this;
 
2530
}
 
2531
 
 
2532
template <class T>
 
2533
Matrix44<T>
 
2534
Matrix44<T>::transposed () const
 
2535
{
 
2536
    return Matrix44 (x[0][0],
 
2537
                     x[1][0],
 
2538
                     x[2][0],
 
2539
                     x[3][0],
 
2540
                     x[0][1],
 
2541
                     x[1][1],
 
2542
                     x[2][1],
 
2543
                     x[3][1],
 
2544
                     x[0][2],
 
2545
                     x[1][2],
 
2546
                     x[2][2],
 
2547
                     x[3][2],
 
2548
                     x[0][3],
 
2549
                     x[1][3],
 
2550
                     x[2][3],
 
2551
                     x[3][3]);
 
2552
}
 
2553
 
 
2554
template <class T>
 
2555
const Matrix44<T> &
 
2556
Matrix44<T>::gjInvert (bool singExc) throw (Iex::MathExc)
 
2557
{
 
2558
    *this = gjInverse (singExc);
 
2559
    return *this;
 
2560
}
 
2561
 
 
2562
template <class T>
 
2563
Matrix44<T>
 
2564
Matrix44<T>::gjInverse (bool singExc) const throw (Iex::MathExc)
 
2565
{
 
2566
    int i, j, k;
 
2567
    Matrix44 s;
 
2568
    Matrix44 t (*this);
 
2569
 
 
2570
    // Forward elimination
 
2571
 
 
2572
    for (i = 0; i < 3 ; i++)
 
2573
    {
 
2574
        int pivot = i;
 
2575
 
 
2576
        T pivotsize = t[i][i];
 
2577
 
 
2578
        if (pivotsize < 0)
 
2579
            pivotsize = -pivotsize;
 
2580
 
 
2581
        for (j = i + 1; j < 4; j++)
 
2582
        {
 
2583
            T tmp = t[j][i];
 
2584
 
 
2585
            if (tmp < 0)
 
2586
                tmp = -tmp;
 
2587
 
 
2588
            if (tmp > pivotsize)
 
2589
            {
 
2590
                pivot = j;
 
2591
                pivotsize = tmp;
 
2592
            }
 
2593
        }
 
2594
 
 
2595
        if (pivotsize == 0)
 
2596
        {
 
2597
            if (singExc)
 
2598
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
 
2599
 
 
2600
            return Matrix44();
 
2601
        }
 
2602
 
 
2603
        if (pivot != i)
 
2604
        {
 
2605
            for (j = 0; j < 4; j++)
 
2606
            {
 
2607
                T tmp;
 
2608
 
 
2609
                tmp = t[i][j];
 
2610
                t[i][j] = t[pivot][j];
 
2611
                t[pivot][j] = tmp;
 
2612
 
 
2613
                tmp = s[i][j];
 
2614
                s[i][j] = s[pivot][j];
 
2615
                s[pivot][j] = tmp;
 
2616
            }
 
2617
        }
 
2618
 
 
2619
        for (j = i + 1; j < 4; j++)
 
2620
        {
 
2621
            T f = t[j][i] / t[i][i];
 
2622
 
 
2623
            for (k = 0; k < 4; k++)
 
2624
            {
 
2625
                t[j][k] -= f * t[i][k];
 
2626
                s[j][k] -= f * s[i][k];
 
2627
            }
 
2628
        }
 
2629
    }
 
2630
 
 
2631
    // Backward substitution
 
2632
 
 
2633
    for (i = 3; i >= 0; --i)
 
2634
    {
 
2635
        T f;
 
2636
 
 
2637
        if ((f = t[i][i]) == 0)
 
2638
        {
 
2639
            if (singExc)
 
2640
                throw ::Imath::SingMatrixExc ("Cannot invert singular matrix.");
 
2641
 
 
2642
            return Matrix44();
 
2643
        }
 
2644
 
 
2645
        for (j = 0; j < 4; j++)
 
2646
        {
 
2647
            t[i][j] /= f;
 
2648
            s[i][j] /= f;
 
2649
        }
 
2650
 
 
2651
        for (j = 0; j < i; j++)
 
2652
        {
 
2653
            f = t[j][i];
 
2654
 
 
2655
            for (k = 0; k < 4; k++)
 
2656
            {
 
2657
                t[j][k] -= f * t[i][k];
 
2658
                s[j][k] -= f * s[i][k];
 
2659
            }
 
2660
        }
 
2661
    }
 
2662
 
 
2663
    return s;
 
2664
}
 
2665
 
 
2666
template <class T>
 
2667
const Matrix44<T> &
 
2668
Matrix44<T>::invert (bool singExc) throw (Iex::MathExc)
 
2669
{
 
2670
    *this = inverse (singExc);
 
2671
    return *this;
 
2672
}
 
2673
 
 
2674
template <class T>
 
2675
Matrix44<T>
 
2676
Matrix44<T>::inverse (bool singExc) const throw (Iex::MathExc)
 
2677
{
 
2678
    if (x[0][3] != 0 || x[1][3] != 0 || x[2][3] != 0 || x[3][3] != 1)
 
2679
        return gjInverse(singExc);
 
2680
 
 
2681
    Matrix44 s (x[1][1] * x[2][2] - x[2][1] * x[1][2],
 
2682
                x[2][1] * x[0][2] - x[0][1] * x[2][2],
 
2683
                x[0][1] * x[1][2] - x[1][1] * x[0][2],
 
2684
                0,
 
2685
 
 
2686
                x[2][0] * x[1][2] - x[1][0] * x[2][2],
 
2687
                x[0][0] * x[2][2] - x[2][0] * x[0][2],
 
2688
                x[1][0] * x[0][2] - x[0][0] * x[1][2],
 
2689
                0,
 
2690
 
 
2691
                x[1][0] * x[2][1] - x[2][0] * x[1][1],
 
2692
                x[2][0] * x[0][1] - x[0][0] * x[2][1],
 
2693
                x[0][0] * x[1][1] - x[1][0] * x[0][1],
 
2694
                0,
 
2695
 
 
2696
                0,
 
2697
                0,
 
2698
                0,
 
2699
                1);
 
2700
 
 
2701
    T r = x[0][0] * s[0][0] + x[0][1] * s[1][0] + x[0][2] * s[2][0];
 
2702
 
 
2703
    if (Imath::abs (r) >= 1)
 
2704
    {
 
2705
        for (int i = 0; i < 3; ++i)
 
2706
        {
 
2707
            for (int j = 0; j < 3; ++j)
 
2708
            {
 
2709
                s[i][j] /= r;
 
2710
            }
 
2711
        }
 
2712
    }
 
2713
    else
 
2714
    {
 
2715
        T mr = Imath::abs (r) / limits<T>::smallest();
 
2716
 
 
2717
        for (int i = 0; i < 3; ++i)
 
2718
        {
 
2719
            for (int j = 0; j < 3; ++j)
 
2720
            {
 
2721
                if (mr > Imath::abs (s[i][j]))
 
2722
                {
 
2723
                    s[i][j] /= r;
 
2724
                }
 
2725
                else
 
2726
                {
 
2727
                    if (singExc)
 
2728
                        throw SingMatrixExc ("Cannot invert singular matrix.");
 
2729
 
 
2730
                    return Matrix44();
 
2731
                }
 
2732
            }
 
2733
        }
 
2734
    }
 
2735
 
 
2736
    s[3][0] = -x[3][0] * s[0][0] - x[3][1] * s[1][0] - x[3][2] * s[2][0];
 
2737
    s[3][1] = -x[3][0] * s[0][1] - x[3][1] * s[1][1] - x[3][2] * s[2][1];
 
2738
    s[3][2] = -x[3][0] * s[0][2] - x[3][1] * s[1][2] - x[3][2] * s[2][2];
 
2739
 
 
2740
    return s;
 
2741
}
 
2742
 
 
2743
template <class T>
 
2744
template <class S>
 
2745
const Matrix44<T> &
 
2746
Matrix44<T>::setEulerAngles (const Vec3<S>& r)
 
2747
{
 
2748
    S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
 
2749
    
 
2750
    cos_rz = Math<T>::cos (r[2]);
 
2751
    cos_ry = Math<T>::cos (r[1]);
 
2752
    cos_rx = Math<T>::cos (r[0]);
 
2753
    
 
2754
    sin_rz = Math<T>::sin (r[2]);
 
2755
    sin_ry = Math<T>::sin (r[1]);
 
2756
    sin_rx = Math<T>::sin (r[0]);
 
2757
    
 
2758
    x[0][0] =  cos_rz * cos_ry;
 
2759
    x[0][1] =  sin_rz * cos_ry;
 
2760
    x[0][2] = -sin_ry;
 
2761
    x[0][3] =  0;
 
2762
    
 
2763
    x[1][0] = -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx;
 
2764
    x[1][1] =  cos_rz * cos_rx + sin_rz * sin_ry * sin_rx;
 
2765
    x[1][2] =  cos_ry * sin_rx;
 
2766
    x[1][3] =  0;
 
2767
    
 
2768
    x[2][0] =  sin_rz * sin_rx + cos_rz * sin_ry * cos_rx;
 
2769
    x[2][1] = -cos_rz * sin_rx + sin_rz * sin_ry * cos_rx;
 
2770
    x[2][2] =  cos_ry * cos_rx;
 
2771
    x[2][3] =  0;
 
2772
 
 
2773
    x[3][0] =  0;
 
2774
    x[3][1] =  0;
 
2775
    x[3][2] =  0;
 
2776
    x[3][3] =  1;
 
2777
 
 
2778
    return *this;
 
2779
}
 
2780
 
 
2781
template <class T>
 
2782
template <class S>
 
2783
const Matrix44<T> &
 
2784
Matrix44<T>::setAxisAngle (const Vec3<S>& axis, S angle)
 
2785
{
 
2786
    Vec3<S> unit (axis.normalized());
 
2787
    S sine   = Math<T>::sin (angle);
 
2788
    S cosine = Math<T>::cos (angle);
 
2789
 
 
2790
    x[0][0] = unit[0] * unit[0] * (1 - cosine) + cosine;
 
2791
    x[0][1] = unit[0] * unit[1] * (1 - cosine) + unit[2] * sine;
 
2792
    x[0][2] = unit[0] * unit[2] * (1 - cosine) - unit[1] * sine;
 
2793
    x[0][3] = 0;
 
2794
 
 
2795
    x[1][0] = unit[0] * unit[1] * (1 - cosine) - unit[2] * sine;
 
2796
    x[1][1] = unit[1] * unit[1] * (1 - cosine) + cosine;
 
2797
    x[1][2] = unit[1] * unit[2] * (1 - cosine) + unit[0] * sine;
 
2798
    x[1][3] = 0;
 
2799
 
 
2800
    x[2][0] = unit[0] * unit[2] * (1 - cosine) + unit[1] * sine;
 
2801
    x[2][1] = unit[1] * unit[2] * (1 - cosine) - unit[0] * sine;
 
2802
    x[2][2] = unit[2] * unit[2] * (1 - cosine) + cosine;
 
2803
    x[2][3] = 0;
 
2804
 
 
2805
    x[3][0] = 0;
 
2806
    x[3][1] = 0;
 
2807
    x[3][2] = 0;
 
2808
    x[3][3] = 1;
 
2809
 
 
2810
    return *this;
 
2811
}
 
2812
 
 
2813
template <class T>
 
2814
template <class S>
 
2815
const Matrix44<T> &
 
2816
Matrix44<T>::rotate (const Vec3<S> &r)
 
2817
{
 
2818
    S cos_rz, sin_rz, cos_ry, sin_ry, cos_rx, sin_rx;
 
2819
    S m00, m01, m02;
 
2820
    S m10, m11, m12;
 
2821
    S m20, m21, m22;
 
2822
 
 
2823
    cos_rz = Math<S>::cos (r[2]);
 
2824
    cos_ry = Math<S>::cos (r[1]);
 
2825
    cos_rx = Math<S>::cos (r[0]);
 
2826
    
 
2827
    sin_rz = Math<S>::sin (r[2]);
 
2828
    sin_ry = Math<S>::sin (r[1]);
 
2829
    sin_rx = Math<S>::sin (r[0]);
 
2830
 
 
2831
    m00 =  cos_rz *  cos_ry;
 
2832
    m01 =  sin_rz *  cos_ry;
 
2833
    m02 = -sin_ry;
 
2834
    m10 = -sin_rz *  cos_rx + cos_rz * sin_ry * sin_rx;
 
2835
    m11 =  cos_rz *  cos_rx + sin_rz * sin_ry * sin_rx;
 
2836
    m12 =  cos_ry *  sin_rx;
 
2837
    m20 = -sin_rz * -sin_rx + cos_rz * sin_ry * cos_rx;
 
2838
    m21 =  cos_rz * -sin_rx + sin_rz * sin_ry * cos_rx;
 
2839
    m22 =  cos_ry *  cos_rx;
 
2840
 
 
2841
    Matrix44<T> P (*this);
 
2842
 
 
2843
    x[0][0] = P[0][0] * m00 + P[1][0] * m01 + P[2][0] * m02;
 
2844
    x[0][1] = P[0][1] * m00 + P[1][1] * m01 + P[2][1] * m02;
 
2845
    x[0][2] = P[0][2] * m00 + P[1][2] * m01 + P[2][2] * m02;
 
2846
    x[0][3] = P[0][3] * m00 + P[1][3] * m01 + P[2][3] * m02;
 
2847
 
 
2848
    x[1][0] = P[0][0] * m10 + P[1][0] * m11 + P[2][0] * m12;
 
2849
    x[1][1] = P[0][1] * m10 + P[1][1] * m11 + P[2][1] * m12;
 
2850
    x[1][2] = P[0][2] * m10 + P[1][2] * m11 + P[2][2] * m12;
 
2851
    x[1][3] = P[0][3] * m10 + P[1][3] * m11 + P[2][3] * m12;
 
2852
 
 
2853
    x[2][0] = P[0][0] * m20 + P[1][0] * m21 + P[2][0] * m22;
 
2854
    x[2][1] = P[0][1] * m20 + P[1][1] * m21 + P[2][1] * m22;
 
2855
    x[2][2] = P[0][2] * m20 + P[1][2] * m21 + P[2][2] * m22;
 
2856
    x[2][3] = P[0][3] * m20 + P[1][3] * m21 + P[2][3] * m22;
 
2857
 
 
2858
    return *this;
 
2859
}
 
2860
 
 
2861
template <class T>
 
2862
const Matrix44<T> &
 
2863
Matrix44<T>::setScale (T s)
 
2864
{
 
2865
    x[0][0] = s;
 
2866
    x[0][1] = 0;
 
2867
    x[0][2] = 0;
 
2868
    x[0][3] = 0;
 
2869
 
 
2870
    x[1][0] = 0;
 
2871
    x[1][1] = s;
 
2872
    x[1][2] = 0;
 
2873
    x[1][3] = 0;
 
2874
 
 
2875
    x[2][0] = 0;
 
2876
    x[2][1] = 0;
 
2877
    x[2][2] = s;
 
2878
    x[2][3] = 0;
 
2879
 
 
2880
    x[3][0] = 0;
 
2881
    x[3][1] = 0;
 
2882
    x[3][2] = 0;
 
2883
    x[3][3] = 1;
 
2884
 
 
2885
    return *this;
 
2886
}
 
2887
 
 
2888
template <class T>
 
2889
template <class S>
 
2890
const Matrix44<T> &
 
2891
Matrix44<T>::setScale (const Vec3<S> &s)
 
2892
{
 
2893
    x[0][0] = s[0];
 
2894
    x[0][1] = 0;
 
2895
    x[0][2] = 0;
 
2896
    x[0][3] = 0;
 
2897
 
 
2898
    x[1][0] = 0;
 
2899
    x[1][1] = s[1];
 
2900
    x[1][2] = 0;
 
2901
    x[1][3] = 0;
 
2902
 
 
2903
    x[2][0] = 0;
 
2904
    x[2][1] = 0;
 
2905
    x[2][2] = s[2];
 
2906
    x[2][3] = 0;
 
2907
 
 
2908
    x[3][0] = 0;
 
2909
    x[3][1] = 0;
 
2910
    x[3][2] = 0;
 
2911
    x[3][3] = 1;
 
2912
 
 
2913
    return *this;
 
2914
}
 
2915
 
 
2916
template <class T>
 
2917
template <class S>
 
2918
const Matrix44<T> &
 
2919
Matrix44<T>::scale (const Vec3<S> &s)
 
2920
{
 
2921
    x[0][0] *= s[0];
 
2922
    x[0][1] *= s[0];
 
2923
    x[0][2] *= s[0];
 
2924
    x[0][3] *= s[0];
 
2925
 
 
2926
    x[1][0] *= s[1];
 
2927
    x[1][1] *= s[1];
 
2928
    x[1][2] *= s[1];
 
2929
    x[1][3] *= s[1];
 
2930
 
 
2931
    x[2][0] *= s[2];
 
2932
    x[2][1] *= s[2];
 
2933
    x[2][2] *= s[2];
 
2934
    x[2][3] *= s[2];
 
2935
 
 
2936
    return *this;
 
2937
}
 
2938
 
 
2939
template <class T>
 
2940
template <class S>
 
2941
const Matrix44<T> &
 
2942
Matrix44<T>::setTranslation (const Vec3<S> &t)
 
2943
{
 
2944
    x[0][0] = 1;
 
2945
    x[0][1] = 0;
 
2946
    x[0][2] = 0;
 
2947
    x[0][3] = 0;
 
2948
 
 
2949
    x[1][0] = 0;
 
2950
    x[1][1] = 1;
 
2951
    x[1][2] = 0;
 
2952
    x[1][3] = 0;
 
2953
 
 
2954
    x[2][0] = 0;
 
2955
    x[2][1] = 0;
 
2956
    x[2][2] = 1;
 
2957
    x[2][3] = 0;
 
2958
 
 
2959
    x[3][0] = t[0];
 
2960
    x[3][1] = t[1];
 
2961
    x[3][2] = t[2];
 
2962
    x[3][3] = 1;
 
2963
 
 
2964
    return *this;
 
2965
}
 
2966
 
 
2967
template <class T>
 
2968
inline const Vec3<T>
 
2969
Matrix44<T>::translation () const
 
2970
{
 
2971
    return Vec3<T> (x[3][0], x[3][1], x[3][2]);
 
2972
}
 
2973
 
 
2974
template <class T>
 
2975
template <class S>
 
2976
const Matrix44<T> &
 
2977
Matrix44<T>::translate (const Vec3<S> &t)
 
2978
{
 
2979
    x[3][0] += t[0] * x[0][0] + t[1] * x[1][0] + t[2] * x[2][0];
 
2980
    x[3][1] += t[0] * x[0][1] + t[1] * x[1][1] + t[2] * x[2][1];
 
2981
    x[3][2] += t[0] * x[0][2] + t[1] * x[1][2] + t[2] * x[2][2];
 
2982
    x[3][3] += t[0] * x[0][3] + t[1] * x[1][3] + t[2] * x[2][3];
 
2983
 
 
2984
    return *this;
 
2985
}
 
2986
 
 
2987
template <class T>
 
2988
template <class S>
 
2989
const Matrix44<T> &
 
2990
Matrix44<T>::setShear (const Vec3<S> &h)
 
2991
{
 
2992
    x[0][0] = 1;
 
2993
    x[0][1] = 0;
 
2994
    x[0][2] = 0;
 
2995
    x[0][3] = 0;
 
2996
 
 
2997
    x[1][0] = h[0];
 
2998
    x[1][1] = 1;
 
2999
    x[1][2] = 0;
 
3000
    x[1][3] = 0;
 
3001
 
 
3002
    x[2][0] = h[1];
 
3003
    x[2][1] = h[2];
 
3004
    x[2][2] = 1;
 
3005
    x[2][3] = 0;
 
3006
 
 
3007
    x[3][0] = 0;
 
3008
    x[3][1] = 0;
 
3009
    x[3][2] = 0;
 
3010
    x[3][3] = 1;
 
3011
 
 
3012
    return *this;
 
3013
}
 
3014
 
 
3015
template <class T>
 
3016
template <class S>
 
3017
const Matrix44<T> &
 
3018
Matrix44<T>::setShear (const Shear6<S> &h)
 
3019
{
 
3020
    x[0][0] = 1;
 
3021
    x[0][1] = h.yx;
 
3022
    x[0][2] = h.zx;
 
3023
    x[0][3] = 0;
 
3024
 
 
3025
    x[1][0] = h.xy;
 
3026
    x[1][1] = 1;
 
3027
    x[1][2] = h.zy;
 
3028
    x[1][3] = 0;
 
3029
 
 
3030
    x[2][0] = h.xz;
 
3031
    x[2][1] = h.yz;
 
3032
    x[2][2] = 1;
 
3033
    x[2][3] = 0;
 
3034
 
 
3035
    x[3][0] = 0;
 
3036
    x[3][1] = 0;
 
3037
    x[3][2] = 0;
 
3038
    x[3][3] = 1;
 
3039
 
 
3040
    return *this;
 
3041
}
 
3042
 
 
3043
template <class T>
 
3044
template <class S>
 
3045
const Matrix44<T> &
 
3046
Matrix44<T>::shear (const Vec3<S> &h)
 
3047
{
 
3048
    //
 
3049
    // In this case, we don't need a temp. copy of the matrix 
 
3050
    // because we never use a value on the RHS after we've 
 
3051
    // changed it on the LHS.
 
3052
    // 
 
3053
 
 
3054
    for (int i=0; i < 4; i++)
 
3055
    {
 
3056
        x[2][i] += h[1] * x[0][i] + h[2] * x[1][i];
 
3057
        x[1][i] += h[0] * x[0][i];
 
3058
    }
 
3059
 
 
3060
    return *this;
 
3061
}
 
3062
 
 
3063
template <class T>
 
3064
template <class S>
 
3065
const Matrix44<T> &
 
3066
Matrix44<T>::shear (const Shear6<S> &h)
 
3067
{
 
3068
    Matrix44<T> P (*this);
 
3069
 
 
3070
    for (int i=0; i < 4; i++)
 
3071
    {
 
3072
        x[0][i] = P[0][i] + h.yx * P[1][i] + h.zx * P[2][i];
 
3073
        x[1][i] = h.xy * P[0][i] + P[1][i] + h.zy * P[2][i];
 
3074
        x[2][i] = h.xz * P[0][i] + h.yz * P[1][i] + P[2][i];
 
3075
    }
 
3076
 
 
3077
    return *this;
 
3078
}
 
3079
 
 
3080
 
 
3081
//--------------------------------
 
3082
// Implementation of stream output
 
3083
//--------------------------------
 
3084
 
 
3085
template <class T>
 
3086
std::ostream &
 
3087
operator << (std::ostream &s, const Matrix33<T> &m)
 
3088
{
 
3089
    std::ios_base::fmtflags oldFlags = s.flags();
 
3090
    int width;
 
3091
 
 
3092
    if (s.flags() & std::ios_base::fixed)
 
3093
    {
 
3094
        s.setf (std::ios_base::showpoint);
 
3095
        width = s.precision() + 5;
 
3096
    }
 
3097
    else
 
3098
    {
 
3099
        s.setf (std::ios_base::scientific);
 
3100
        s.setf (std::ios_base::showpoint);
 
3101
        width = s.precision() + 8;
 
3102
    }
 
3103
 
 
3104
    s << "(" << std::setw (width) << m[0][0] <<
 
3105
         " " << std::setw (width) << m[0][1] <<
 
3106
         " " << std::setw (width) << m[0][2] << "\n" <<
 
3107
 
 
3108
         " " << std::setw (width) << m[1][0] <<
 
3109
         " " << std::setw (width) << m[1][1] <<
 
3110
         " " << std::setw (width) << m[1][2] << "\n" <<
 
3111
 
 
3112
         " " << std::setw (width) << m[2][0] <<
 
3113
         " " << std::setw (width) << m[2][1] <<
 
3114
         " " << std::setw (width) << m[2][2] << ")\n";
 
3115
 
 
3116
    s.flags (oldFlags);
 
3117
    return s;
 
3118
}
 
3119
 
 
3120
template <class T>
 
3121
std::ostream &
 
3122
operator << (std::ostream &s, const Matrix44<T> &m)
 
3123
{
 
3124
    std::ios_base::fmtflags oldFlags = s.flags();
 
3125
    int width;
 
3126
 
 
3127
    if (s.flags() & std::ios_base::fixed)
 
3128
    {
 
3129
        s.setf (std::ios_base::showpoint);
 
3130
        width = s.precision() + 5;
 
3131
    }
 
3132
    else
 
3133
    {
 
3134
        s.setf (std::ios_base::scientific);
 
3135
        s.setf (std::ios_base::showpoint);
 
3136
        width = s.precision() + 8;
 
3137
    }
 
3138
 
 
3139
    s << "(" << std::setw (width) << m[0][0] <<
 
3140
         " " << std::setw (width) << m[0][1] <<
 
3141
         " " << std::setw (width) << m[0][2] <<
 
3142
         " " << std::setw (width) << m[0][3] << "\n" <<
 
3143
 
 
3144
         " " << std::setw (width) << m[1][0] <<
 
3145
         " " << std::setw (width) << m[1][1] <<
 
3146
         " " << std::setw (width) << m[1][2] <<
 
3147
         " " << std::setw (width) << m[1][3] << "\n" <<
 
3148
 
 
3149
         " " << std::setw (width) << m[2][0] <<
 
3150
         " " << std::setw (width) << m[2][1] <<
 
3151
         " " << std::setw (width) << m[2][2] <<
 
3152
         " " << std::setw (width) << m[2][3] << "\n" <<
 
3153
 
 
3154
         " " << std::setw (width) << m[3][0] <<
 
3155
         " " << std::setw (width) << m[3][1] <<
 
3156
         " " << std::setw (width) << m[3][2] <<
 
3157
         " " << std::setw (width) << m[3][3] << ")\n";
 
3158
 
 
3159
    s.flags (oldFlags);
 
3160
    return s;
 
3161
}
 
3162
 
 
3163
 
 
3164
//---------------------------------------------------------------
 
3165
// Implementation of vector-times-matrix multiplication operators
 
3166
//---------------------------------------------------------------
 
3167
 
 
3168
template <class S, class T>
 
3169
inline const Vec2<S> &
 
3170
operator *= (Vec2<S> &v, const Matrix33<T> &m)
 
3171
{
 
3172
    S x = S(v.x * m[0][0] + v.y * m[1][0] + m[2][0]);
 
3173
    S y = S(v.x * m[0][1] + v.y * m[1][1] + m[2][1]);
 
3174
    S w = S(v.x * m[0][2] + v.y * m[1][2] + m[2][2]);
 
3175
 
 
3176
    v.x = x / w;
 
3177
    v.y = y / w;
 
3178
 
 
3179
    return v;
 
3180
}
 
3181
 
 
3182
template <class S, class T>
 
3183
inline Vec2<S>
 
3184
operator * (const Vec2<S> &v, const Matrix33<T> &m)
 
3185
{
 
3186
    S x = S(v.x * m[0][0] + v.y * m[1][0] + m[2][0]);
 
3187
    S y = S(v.x * m[0][1] + v.y * m[1][1] + m[2][1]);
 
3188
    S w = S(v.x * m[0][2] + v.y * m[1][2] + m[2][2]);
 
3189
 
 
3190
    return Vec2<S> (x / w, y / w);
 
3191
}
 
3192
 
 
3193
 
 
3194
template <class S, class T>
 
3195
inline const Vec3<S> &
 
3196
operator *= (Vec3<S> &v, const Matrix33<T> &m)
 
3197
{
 
3198
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0]);
 
3199
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1]);
 
3200
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2]);
 
3201
 
 
3202
    v.x = x;
 
3203
    v.y = y;
 
3204
    v.z = z;
 
3205
 
 
3206
    return v;
 
3207
}
 
3208
 
 
3209
 
 
3210
template <class S, class T>
 
3211
inline Vec3<S>
 
3212
operator * (const Vec3<S> &v, const Matrix33<T> &m)
 
3213
{
 
3214
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0]);
 
3215
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1]);
 
3216
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2]);
 
3217
 
 
3218
    return Vec3<S> (x, y, z);
 
3219
}
 
3220
 
 
3221
 
 
3222
template <class S, class T>
 
3223
inline const Vec3<S> &
 
3224
operator *= (Vec3<S> &v, const Matrix44<T> &m)
 
3225
{
 
3226
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]);
 
3227
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]);
 
3228
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]);
 
3229
    S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3]);
 
3230
 
 
3231
    v.x = x / w;
 
3232
    v.y = y / w;
 
3233
    v.z = z / w;
 
3234
 
 
3235
    return v;
 
3236
}
 
3237
 
 
3238
template <class S, class T>
 
3239
inline Vec3<S>
 
3240
operator * (const Vec3<S> &v, const Matrix44<T> &m)
 
3241
{
 
3242
    S x = S(v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + m[3][0]);
 
3243
    S y = S(v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + m[3][1]);
 
3244
    S z = S(v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + m[3][2]);
 
3245
    S w = S(v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + m[3][3]);
 
3246
 
 
3247
    return Vec3<S> (x / w, y / w, z / w);
 
3248
}
 
3249
 
 
3250
} // namespace Imath
 
3251
 
 
3252
 
 
3253
 
 
3254
#endif