File: enricomath.h

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (839 lines) | stat: -rw-r--r-- 28,644 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros                                      *
 *         2005-2006 Enrico Ros <koral@email.it>                           *
 *         2007-2009 Enrico Ros <enrico.ros@gmail.com>                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef __enricomath_h__
#define __enricomath_h__

// uncomment the following to perform checks on operations (slow, safer)
#define VECTOR_CHECK_MATH

#ifndef M_PI
#define M_PI 3.141592653589793238462643 
#endif // !M_PI

class Vector2;
class Control2;
class Vector3;
class Quaternion;
class Matrix3;


/**
 * @short 2D Vector
 *
 * This class is mainly handled by operators. Common Operators:
 *   +, -, * (scalar product), / (scalar division), [](get x,y components)
 *
 * Plus, we define those Additional ones:
 *   ! return a NORMALIZEd copy of the vector
 *       V1 = !Vector3( 0.21, 0.1, 0.3 ); // 0.564 0.261 0.783
 *
 *   & return the PROJECTION on another VECTOR (often used over a normalized)
 *       V1 & Vector3( 3.123, 0, 0 );     // 1.761 0.000 0.000
 *       V1 & !Vector3( 3.123, 0, 0 );    // 0.553 0.000 0.000
 */

class Vector2
{
    public:
        Vector2();
        Vector2( double x, double y );

        static const Vector2 X, Y;
        static const Vector2 Null;

        inline bool isNull() const;
        inline double x() const;
        inline double y() const;
        inline double module() const;
        inline double angle() const;
        inline double projectedLen( const Vector2 & v ) const;

        inline double & rx();
        inline double & ry();

        // operations
        inline Vector2 & setX( double );
        inline Vector2 & setY( double );
        inline Vector2 & reset();

        // operators
        inline Vector2 & operator+=( const Vector2 & );
        inline Vector2 & operator-=( const Vector2 & );
        inline Vector2 & operator*=( double );
        inline Vector2 & operator/=( double );
        inline Vector2 & operator&=( const Vector2 & );

        friend inline bool operator==( const Vector2 &, const Vector2 & );
        friend inline bool operator!=( const Vector2 &, const Vector2 & );
        friend inline const Vector2 operator!( const Vector2 & );
        friend inline const Vector2 operator+( const Vector2 &, const Vector2 & );
        friend inline const Vector2 operator-( const Vector2 &, const Vector2 & );
        friend inline const Vector2 operator*( const Vector2 &, double );
        friend inline const Vector2 operator*( double, const Vector2 & );
        friend inline const Vector2 operator/( const Vector2 &, double );
        friend inline const Vector2 operator&( const Vector2 &, const Vector2 & );

        inline void dump() const;

    private:
        double m_x, m_y;
};


/**
 * \brief 2D Control Point
 * ...
 */

class Control2
{
    public:
        Control2();
        Control2( double ro, double theta );

        static const Control2 X, Y;
        static const Control2 Null;
        //static fromXY( double x, double y );

        inline bool isNull() const;
        inline double ro() const;
        inline double theta() const;
        inline Vector2 toVector2() const;

        // operations
        inline Control2 & setRo( double );
        inline Control2 & setTheta( double );
        inline Control2 & addRo( double );
        inline Control2 & addTheta( double );

        // operators
        friend inline bool operator==( const Control2 &, const Control2 & );
        friend inline bool operator!=( const Control2 &, const Control2 & );

        inline void dump() const;

    private:
        double m_ro, m_theta;
};

/**
 * @short 3D Vector
 *
 * This class is mainly handled by operators. Common Operators:
 *   +, -, * (scalar product), / (scalar division), [](get x,y,z components)
 *
 * Plus, we define those Additional ones:
 *   ! return a NORMALIZEd copy of the vector
 *       V1 = !Vector3( 0.21, 0.1, 0.3 ); // 0.564 0.261 0.783
 *
 *   & return the PROJECTION on another VECTOR (often used over a normalized)
 *       V1 & Vector3( 3.123, 0, 0 );     // 1.761 0.000 0.000
 *       V1 & !Vector3( 3.123, 0, 0 );    // 0.553 0.000 0.000
 *
 *   ^ vector PRODUCT. returned vector is normal to both operands
 *       V2 = V1 ^ Vector3( 0, 3, 0 );    // 0.000 0.000 1.659
 */

class Vector3
{
    public:
        Vector3();
        Vector3( double x, double y, double z );

        static const Vector3 X, Y, Z;
        static const Vector3 Null;

        inline bool isNull() const;
        inline double x() const;
        inline double y() const;
        inline double z() const;
        inline double module() const;
        inline double projectedLen( const Vector3 & v ) const;

        // operations
        inline Vector3 & setX( double );
        inline Vector3 & setY( double );
        inline Vector3 & setZ( double );
        inline Vector3 & rotate( double radians, const Vector3 & axis );
        inline Vector3 & rotate( const Quaternion & quat );

        // operators
        inline Vector3 & operator+=( const Vector3 & );
        inline Vector3 & operator-=( const Vector3 & );
        inline Vector3 & operator*=( double );
        inline Vector3 & operator*=( const Quaternion & );
        inline Vector3 & operator*=( const Matrix3 & );
        inline Vector3 & operator/=( double );
        inline Vector3 & operator&=( const Vector3 & );
        inline Vector3 & operator^=( const Vector3 & );

        friend inline bool operator==( const Vector3 &, const Vector3 & );
        friend inline bool operator!=( const Vector3 &, const Vector3 & );
        friend inline const Vector3 operator!( const Vector3 & );
        friend inline const Vector3 operator+( const Vector3 &, const Vector3 & );
        friend inline const Vector3 operator-( const Vector3 &, const Vector3 & );
        friend inline const Vector3 operator*( const Vector3 &, double );
        friend inline const Vector3 operator*( double, const Vector3 & );
        friend inline const Vector3 operator*( const Vector3 &, const Quaternion & );
        friend inline const Vector3 operator*( const Matrix3 &, const Vector3 & );
        friend inline const Vector3 operator/( const Vector3 &, double );
        friend inline const Vector3 operator&( const Vector3 &, const Vector3 & );
        friend inline const Vector3 operator^( const Vector3 &, const Vector3 & );

        inline void dump() const;

    private:
        double m_x, m_y, m_z;
};

/**
 * @short Quaternion.
 *
 * Mainly for representing rotations, this class identifies a quaternion.
 * There is only 1 implemented operator:
 *   * quaternions multiplication. adds the second rotation the the first
 *       Q1 = Quaternion( +PI/2, Vector3(1,0,0) );  // +90deg over X
 *       Q2 = Quaternion( -PI/2, Vector3(0,1,0) );  // -90deg over Y
 *       (Q1*Q2).dump();                  // +120deg over 0.577 -0.577 0.577
 */
class Quaternion
{
    public:
        Quaternion();
        Quaternion( double radians, const Vector3 & axis );
        Quaternion( double yaw, double pitch, double roll );
        Quaternion( double a, double b, double c, double d );

        static const Quaternion Null;

        inline bool isNull() const;
        inline bool isIdentity() const;
        inline double yaw() const;
        inline double pitch() const;
        inline double roll() const;
        inline double angle() const;
        inline const Vector3 axis() const;
        inline double module() const;

        // operations
        inline void normalize();

        // operators
        inline Quaternion & operator*=( const Quaternion & );
        friend inline const Quaternion operator!( const Quaternion & );
        friend inline const Quaternion operator*( const Quaternion &, const Quaternion & );

        inline void dump() const;

    private:
        friend class Vector3;
        double m_a, m_b, m_c, m_d;
};

/**
 * @short 3x3 Matrix.
 * ..used only by Vector3 for now..
 */
class Matrix3
{
    public:
        Matrix3();
        Matrix3( double a1, double a2, double a3,
                 double b1, double b2, double b3,
                 double c1, double c2, double c3 );

        static const Matrix3 Identity;
        static const Matrix3 Null;

        // operators
        inline Matrix3 & operator+=( const Matrix3 & );
        inline Matrix3 & operator-=( const Matrix3 & );
        inline Matrix3 & operator*=( double );

        friend inline bool operator==( const Matrix3 &, const Matrix3 & );
        friend inline bool operator!=( const Matrix3 &, const Matrix3 & );
        friend inline const Matrix3 operator+( const Matrix3 &, const Matrix3 & );
        friend inline const Matrix3 operator-( const Matrix3 &, const Matrix3 & );
        friend inline const Matrix3 operator*( const Matrix3 &, double );
        friend inline const Matrix3 operator*( double, const Matrix3 & );
        friend inline const Vector3 operator*( const Matrix3 &, const Vector3 & );

        inline void dump() const;

    private:
        friend class Vector3;
        double m_a1, m_a2, m_a3, m_b1, m_b2, m_b3, m_c1, m_c2, m_c3;
};


/****************************************************************************
 inline functions
 ****************************************************************************/
#include <math.h>
#include <stdio.h>

#if defined(Q_OS_WIN32) || defined(Q_OS_MAC)
//sincos is not defined in win32 and MAC
static inline void sincos(double th, double *s, double *c)
{
    *s = sin(th);
    *c = cos(th);
}
#endif

/****************************************************************************
 Vector2 - inline functions
 ****************************************************************************/
inline Vector2::Vector2()
    : m_x( 0.0 ), m_y( 0.0 ) {}

inline Vector2::Vector2( double x, double y )
    : m_x( x ), m_y( y ) {}

inline bool Vector2::isNull() const
{ return m_x == 0.0 && m_y == 0.0; }

inline double Vector2::x() const
{ return m_x; }

inline double Vector2::y() const
{ return m_y; }

inline double Vector2::module() const
{ return sqrt( m_x*m_x + m_y*m_y ); }

inline double Vector2::angle() const
{ return atan2( m_y, m_x ); }

inline double Vector2::projectedLen( const Vector2 & v2 ) const
{ return m_x*v2.m_x + m_y*v2.m_y; }

inline double & Vector2::rx()
{ return m_x; }

inline double & Vector2::ry()
{ return m_y; }

inline Vector2 & Vector2::setX( double x )
{ m_x = x; return *this; }

inline Vector2 & Vector2::setY( double y )
{ m_y = y; return *this; }

inline Vector2 & Vector2::reset()
{ m_x = 0; m_y = 0; return *this; }

inline Vector2 & Vector2::operator+=( const Vector2 &v )
{ m_x+=v.m_x; m_y+=v.m_y; return *this; }

inline Vector2 & Vector2::operator-=( const Vector2 &v )
{ m_x-=v.m_x; m_y-=v.m_y; return *this; }

inline Vector2 & Vector2::operator*=( double a )
{ m_x *= a; m_y *= a; return *this; }

inline Vector2 & Vector2::operator/=( double a )
{
#ifdef VECTOR_CHECK_MATH
    if ( a == 0.0 )
        fprintf(stderr,"Vector2: scalar division by zero.\n");
#endif
    m_x /= a; m_y /= a;
    return *this;
}

inline Vector2 & Vector2::operator&=( const Vector2 & v )
{
    if ( isNull() || v.isNull() )
        m_x = m_y = 0.0;
    else {
        double mod = m_x*v.m_x + m_y*v.m_y; // |this| * |v| * cos(angle)
        mod /= v.module(); // 'prenormalize' v before applying 'len'
        m_x = mod * v.m_x; m_y = mod * v.m_y;
    }
    return *this;
}

inline bool operator==( const Vector2 &v1, const Vector2 &v2 )
{ return v1.m_x == v2.m_x && v1.m_y == v2.m_y; }

inline bool operator!=( const Vector2 &v1, const Vector2 &v2 )
{ return v1.m_x != v2.m_x || v1.m_y != v2.m_y; }

inline const Vector2 operator!( const Vector2 &v )
{
#ifdef VECTOR_CHECK_MATH
    if ( v.isNull() ) {
        fprintf(stderr,"Vector2: normalizing a null vector.\n");
        return Vector2( 0.0, 0.0 );
    }
#endif
    double mod = v.module();
    return Vector2( v.m_x / mod, v.m_y / mod );
}

inline const Vector2 operator+( const Vector2 & v1, const Vector2 & v2 )
{ return Vector2( v1.m_x+v2.m_x, v1.m_y+v2.m_y ); }

inline const Vector2 operator-( const Vector2 & v1, const Vector2 & v2 )
{ return Vector2( v1.m_x-v2.m_x, v1.m_y-v2.m_y ); }

inline const Vector2 operator*( const Vector2 & v, double a )
{ return Vector2( a*v.m_x, a*v.m_y ); }

inline const Vector2 operator*( double a, const Vector2 &v )
{ return Vector2( a*v.m_x, a*v.m_y ); }

inline const Vector2 operator/( const Vector2 &v, double a )
{
#ifdef VECTOR_CHECK_MATH
    if ( a == 0.0 )
        fprintf(stderr,"Vector2: scalar division by zero.\n");
#endif
    return Vector2( v.m_x/a, v.m_y/a );
}

inline const Vector2 operator&( const Vector2 & v1, const Vector2 & v2 )
{
    if ( v1.isNull() || v2.isNull() )
        return Vector2( 0.0, 0.0 );
    double mod = v1.m_x*v2.m_x + v1.m_y*v2.m_y; // |v1| * |v2| * cos(angle)
    mod /= v2.module(); // 'prenormalize' v2 before applying 'len'
    return Vector2( mod * v2.m_x, mod * v2.m_y );
}

inline void Vector2::dump() const
{ printf( "%f %f [%f]\n", m_x, m_y, module() ); }



/****************************************************************************
 Control2 - inline functions
 ****************************************************************************/
inline Control2::Control2()
    : m_ro( 0.0 ), m_theta( 0.0 ) {}

inline Control2::Control2( double ro, double theta )
    : m_ro( ro ), m_theta( theta ) {}

inline bool Control2::isNull() const
{ return m_ro == 0.0 && m_theta == 0.0; }

inline double Control2::ro() const
{ return m_ro; }

inline double Control2::theta() const
{ return m_theta; }

Vector2 Control2::toVector2() const
{ return Vector2( m_ro * cos(m_theta), m_ro * sin(m_theta) ); }

inline Control2 & Control2::setRo( double ro )
{ m_ro = ro; return *this; }

inline Control2 & Control2::setTheta( double theta )
{ m_theta = theta; return *this; }

inline Control2 & Control2::addRo( double ro )
{ m_ro += ro; return *this; }

inline Control2 & Control2::addTheta( double theta )
{ m_theta += theta; return *this; }

inline bool operator==( const Control2 &c1, const Control2 &c2 )
{ return c1.m_ro == c2.m_ro && c1.m_theta == c1.m_theta; }

inline bool operator!=( const Control2 &c1, const Control2 &c2 )
{ return c1.m_ro != c2.m_ro || c1.m_theta != c2.m_theta; }

void Control2::dump() const
{ printf( "%f %f deg\n", m_ro, 180.0 * m_theta / M_PI ); }



/****************************************************************************
 Vector3 - inline functions
 ****************************************************************************/
inline Vector3::Vector3()
    : m_x( 0.0 ), m_y( 0.0 ), m_z( 0.0 ) {}

inline Vector3::Vector3( double x, double y, double z )
    : m_x( x ), m_y( y ), m_z( z ) {}

inline bool Vector3::isNull() const
{ return m_x == 0.0 && m_y == 0.0 && m_z == 0.0; }

inline double Vector3::x() const
{ return m_x; }

inline double Vector3::y() const
{ return m_y; }

inline double Vector3::z() const
{ return m_z; }

inline double Vector3::module() const
{ return sqrt( m_x*m_x + m_y*m_y + m_z*m_z ); }

inline double Vector3::projectedLen( const Vector3 & v2 ) const
{ return m_x*v2.m_x + m_y*v2.m_y + m_z*v2.m_z; }

inline Vector3 & Vector3::setX( double x )
{ m_x = x; return *this; }

inline Vector3 & Vector3::setY( double y )
{ m_y = y; return *this; }

inline Vector3 & Vector3::setZ( double z )
{ m_z = z; return *this; }

inline Vector3 & Vector3::rotate( double radians, const Vector3 & axis )
/// rotazione con Quaternione! [2pg di polinomi..3h di conti.. :-]
/// versione superfast da 0.24 microsecondi a rotazione ;-))
{
#ifdef VECTOR_CHECK_MATH
    if ( axis.isNull() ) {
        printf("Vector3: rotating around a null axis!\n");
        return *this;
    }
#endif
    // definisce costanti del quaternione (re: rot, imm: axis)
    double mSin, mCos;
    sincos( radians / 2.0, &mSin, &mCos );
    mSin /= axis.module();
    double a = mCos, b = mSin*axis.m_x, c = mSin*axis.m_y, d = mSin*axis.m_z;
    // precalcola 10 costanti di rotazione
    double a2 = a*a,   b2 = b*b,   c2 = c*c,   d2 = d*d,
           ab = 2*a*b, ac = 2*a*c, ad = 2*a*d,
           bc = 2*b*c, bd = 2*b*d, cd = 2*c*d;
    // calcola le nuove componenti ruotate
    double i = (a2+b2-c2-d2) * m_x + (bc-ad) * m_y + (ac+bd) * m_z,
           j = (ad+bc) * m_x + (a2-b2+c2-d2) * m_y + (cd-ab) * m_z,
           k = (bd-ac) * m_x + (ab+cd) * m_y + (a2-b2-c2+d2) * m_z;
    // ed ecco il vettore ruotato!
    m_x = i; m_y = j; m_z = k;
    return *this;
}

inline Vector3 & Vector3::rotate( const Quaternion & quat )
{
    // prende i coefficienti dal quaternione
    double a = quat.m_a, b = quat.m_b, c = quat.m_c, d = quat.m_d;
    // precalcola 10 costanti di rotazione
    double a2 = a*a,   b2 = b*b,   c2 = c*c,   d2 = d*d,
           ab = 2*a*b, ac = 2*a*c, ad = 2*a*d,
           bc = 2*b*c, bd = 2*b*d, cd = 2*c*d;
    // calcola le nuove componenti ruotate
    double i = (a2+b2-c2-d2) * m_x + (bc-ad) * m_y + (ac+bd) * m_z,
           j = (ad+bc) * m_x + (a2-b2+c2-d2) * m_y + (cd-ab) * m_z,
           k = (bd-ac) * m_x + (ab+cd) * m_y + (a2-b2-c2+d2) * m_z;
    // ed ecco il vettore ruotato!
    m_x = i; m_y = j; m_z = k;
    return *this;
}

inline Vector3 & Vector3::operator+=( const Vector3 &v )
{ m_x+=v.m_x; m_y+=v.m_y; m_z+=v.m_z; return *this; }

inline Vector3 & Vector3::operator-=( const Vector3 &v )
{ m_x-=v.m_x; m_y-=v.m_y; m_z-=v.m_z; return *this; }

inline Vector3 & Vector3::operator*=( double a )
{ m_x *= a; m_y *= a; m_z *= a; return *this; }

inline Vector3 & Vector3::operator*=( const Quaternion & quat )
{ return rotate( quat ); }

inline Vector3 & Vector3::operator*=( const Matrix3 & m )
{
    double a = m.m_a1 * m_x + m.m_a2 * m_y + m.m_a3 * m_z,
           b = m.m_b1 * m_x + m.m_b2 * m_y + m.m_b3 * m_z,
           c = m.m_c1 * m_x + m.m_c2 * m_y + m.m_c3 * m_z;
    m_x = a, m_y = b, m_z = c; return *this;
}

inline Vector3 & Vector3::operator/=( double a )
{
#ifdef VECTOR_CHECK_MATH
    if ( a == 0.0 )
        fprintf(stderr,"Vector3: scalar division by zero.\n");
#endif
    m_x /= a; m_y /= a; m_z /= a;
    return *this;
}

inline Vector3 & Vector3::operator&=( const Vector3 & v )
{
    if ( isNull() || v.isNull() )
        m_x = m_y = m_z = 0.0;
    else {
        double mod = m_x*v.m_x + m_y*v.m_y + m_z*v.m_z; // |this| * |v| * cos(angle)
        mod /= v.module(); // 'prenormalize' v before applying 'len'
        m_x = mod * v.m_x; m_y = mod * v.m_y; m_z = mod * v.m_z;
    }
    return *this;
}

inline Vector3 & Vector3::operator^=( const Vector3 & v )
{
    double i = m_y*v.m_z - m_z*v.m_y, j = m_z*v.m_x - m_x*v.m_z, k = m_x*v.m_y - m_y*v.m_x;
    m_x = i; m_y = j; m_z = k;
    return * this;
}

inline bool operator==( const Vector3 &v1, const Vector3 &v2 )
{ return v1.m_x == v2.m_x && v1.m_y == v2.m_y && v1.m_z == v2.m_z; }

inline bool operator!=( const Vector3 &v1, const Vector3 &v2 )
{ return v1.m_x != v2.m_x || v1.m_y != v2.m_y || v1.m_z != v2.m_z; }

inline const Vector3 operator!( const Vector3 &v )
{
#ifdef VECTOR_CHECK_MATH
    if ( v.isNull() ) {
        fprintf(stderr,"Vector3: normalizing a null vector.\n");
        return Vector3( 0.0, 0.0, 0.0 );
    }
#endif
    double mod = v.module();
    return Vector3( v.m_x / mod, v.m_y / mod, v.m_z / mod );
}

inline const Vector3 operator+( const Vector3 & v1, const Vector3 & v2 )
{ return Vector3( v1.m_x+v2.m_x, v1.m_y+v2.m_y, v1.m_z+v2.m_z ); }

inline const Vector3 operator-( const Vector3 & v1, const Vector3 & v2 )
{ return Vector3( v1.m_x-v2.m_x, v1.m_y-v2.m_y, v1.m_z-v2.m_z ); }

inline const Vector3 operator*( const Vector3 & v, double a )
{ return Vector3( a*v.m_x, a*v.m_y, a*v.m_z ); }

inline const Vector3 operator*( double a, const Vector3 &v )
{ return Vector3( a*v.m_x, a*v.m_y, a*v.m_z ); }

inline const Vector3 operator*( const Vector3 & v, const Quaternion & quat )
{ return Vector3( v ).rotate( quat ); }

inline const Vector3 operator*( const Matrix3 & m, const Vector3 & v )
{ return Vector3( m.m_a1 * v.m_x + m.m_a2 * v.m_y + m.m_a3 * v.m_z,
                  m.m_b1 * v.m_x + m.m_b2 * v.m_y + m.m_b3 * v.m_z,
                  m.m_c1 * v.m_x + m.m_c2 * v.m_y + m.m_c3 * v.m_z ); }

inline const Vector3 operator/( const Vector3 &v, double a )
{
#ifdef VECTOR_CHECK_MATH
    if ( a == 0.0 )
        fprintf(stderr,"Vector3: scalar division by zero.\n");
#endif
    return Vector3( v.m_x/a, v.m_y/a, v.m_z/a );
}

inline const Vector3 operator&( const Vector3 & v1, const Vector3 & v2 )
{
    if ( v1.isNull() || v2.isNull() )
        return Vector3( 0.0, 0.0, 0.0 );
    double mod = v1.m_x*v2.m_x + v1.m_y*v2.m_y + v1.m_z*v2.m_z; // |v1| * |v2| * cos(angle)
    mod /= v2.module(); // 'prenormalize' v2 before applying 'len'
    return Vector3( mod * v2.m_x, mod * v2.m_y, mod * v2.m_z );
}

inline const Vector3 operator^( const Vector3 & v1, const Vector3 & v2 )
{ return Vector3( v1.m_y*v2.m_z - v1.m_z*v2.m_y, v1.m_z*v2.m_x - v1.m_x*v2.m_z,
                  v1.m_x*v2.m_y - v1.m_y*v2.m_x ); }

inline void Vector3::dump() const
{ printf( "%f %f %f [%f]\n", m_x, m_y, m_z, module() ); }



/****************************************************************************
 Quaternion - inline functions
 ****************************************************************************/
inline Quaternion::Quaternion()
    : m_a( 1.0 ), m_b( 0.0 ), m_c( 0.0 ), m_d( 0.0 ) {}

inline Quaternion::Quaternion( double radians, const Vector3 & axis )
    : m_a( 1.0 ), m_b( 0.0 ), m_c( 0.0 ), m_d( 0.0 )
{
    double mSin, mCos;
    sincos( radians / 2.0, &mSin, &mCos );
#ifdef VECTOR_CHECK_MATH
    if ( axis.isNull() ) {
        printf("Quaternion: constructing one with a null axis!\n");
        return;
    }
#endif
    mSin /= axis.module();
    m_a = mCos, m_b = mSin*axis.x(), m_c = mSin*axis.y(), m_d = mSin*axis.z();
}

inline Quaternion::Quaternion( double yaw, double pitch, double roll )
{
    double s_yaw, c_yaw, s_pitch, c_pitch, s_roll, c_roll;
    sincos( yaw / 2.0, &s_yaw, &c_yaw );
    sincos( pitch / 2.0, &s_pitch, &c_pitch );
    sincos( roll / 2.0, &s_roll, &c_roll );
    m_a = c_roll * c_pitch * c_yaw + s_roll * s_pitch * s_yaw;
    m_b = s_roll * c_pitch * c_yaw - c_roll * s_pitch * s_yaw;
    m_c = c_roll * s_pitch * c_yaw + s_roll * c_pitch * s_yaw;
    m_d = c_roll * c_pitch * s_yaw - s_roll * s_pitch * c_yaw;
}

inline Quaternion::Quaternion( double a, double b, double c, double d )
    : m_a( a ), m_b( b ), m_c( c ), m_d( d ) {}

inline bool Quaternion::isNull() const
{ return /*m_a == 0.0 &&*/ m_b == 0.0 && m_c == 0.0 && m_d == 0.0; }

inline bool Quaternion::isIdentity() const
{ return m_a == 1.0 && m_b == 0.0 && m_c == 0.0 && m_d == 0.0; }

inline double Quaternion::yaw() const
{ return atan2( 2*(m_b*m_c + m_a*m_d), m_a*m_a + m_b*m_b - m_c*m_c - m_d*m_d ); }

inline double Quaternion::pitch() const
{ return asin( -2*(m_b*m_d - m_a*m_c) ); }

inline double Quaternion::roll() const
{ return atan2( 2*(m_c*m_d + m_a*m_b), m_a*m_a - m_b*m_b - m_c*m_c + m_d*m_d ); }

inline double Quaternion::angle() const
{ return 2.0 * acos( m_a ); }

inline const Vector3 Quaternion::axis() const
{
    // if not rotating, axis is undefined
    if ( m_a >= 1.0 || m_a <= -1.0 )
        return Vector3();
    // divide m_x by module to get back vector dimensions
    double mod = sqrt( 1 - m_a*m_a );
    return Vector3( m_b/mod, m_c/mod, m_d/mod );
}

inline double Quaternion::module() const
{ return sqrt( m_a*m_a + m_b*m_b + m_c*m_c + m_d*m_d ); }

inline void Quaternion::normalize()
{
    if ( m_a == 0.0 && m_b == 0.0 && m_c == 0.0 && m_d == 0.0 ) {
#ifdef VECTOR_CHECK_MATH
        fprintf(stderr,"Quaternion: Normalizing a null quaternion!\n");
#endif
        return;
    }
    double mod = 1/sqrt( m_a*m_a + m_b*m_b + m_c*m_c + m_d*m_d );
    m_a *= mod; m_b *= mod; m_c *= mod; m_d *= mod;
}

inline Quaternion & Quaternion::operator*=( const Quaternion & q2 )
{
    double a = q2.m_a*m_a - q2.m_b*m_b - q2.m_c*m_c - q2.m_d*m_d,
           b = q2.m_a*m_b + q2.m_b*m_a + q2.m_c*m_d - q2.m_d*m_c,
           c = q2.m_a*m_c + q2.m_c*m_a + q2.m_d*m_b - q2.m_b*m_d,
           d = q2.m_a*m_d + q2.m_d*m_a + q2.m_b*m_c - q2.m_c*m_b;
    m_a = a; m_b = b; m_c = c; m_d = d;
    return *this;
}

inline const Quaternion operator!( const Quaternion & q )
{ return Quaternion( q.m_a, -q.m_b, -q.m_c, -q.m_d ); }

inline const Quaternion operator*( const Quaternion & q2, const Quaternion & q1 )
{
    double a = q1.m_a*q2.m_a - q1.m_b*q2.m_b - q1.m_c*q2.m_c - q1.m_d*q2.m_d,
           b = q1.m_a*q2.m_b + q1.m_b*q2.m_a + q1.m_c*q2.m_d - q1.m_d*q2.m_c,
           c = q1.m_a*q2.m_c + q1.m_c*q2.m_a + q1.m_d*q2.m_b - q1.m_b*q2.m_d,
           d = q1.m_a*q2.m_d + q1.m_d*q2.m_a + q1.m_b*q2.m_c - q1.m_c*q2.m_b;
    return Quaternion( a, b, c, d );
}

inline void Quaternion::dump() const
{
    printf("a:%f b:%f c:%f d:%f -- angle: %f vec:",m_a,m_b,m_c,m_d,angle());
    axis().dump();
}



/****************************************************************************
 Matrix3 - inline functions
 ****************************************************************************/
inline Matrix3::Matrix3()
    : m_a1( 0.0 ), m_a2( 0.0 ), m_a3( 0.0 ),
    m_b1( 0.0 ), m_b2( 0.0 ), m_b3( 0.0 ),
    m_c1( 0.0 ), m_c2( 0.0 ), m_c3( 0.0 ) {}

inline Matrix3::Matrix3( double a1, double a2, double a3,
    double b1, double b2, double b3, double c1, double c2, double c3 )
    : m_a1( a1 ), m_a2( a2 ), m_a3( a3 ),
    m_b1( b1 ), m_b2( b2 ), m_b3( b3 ),
    m_c1( c1 ), m_c2( c2 ), m_c3( c3 ) {}

inline void Matrix3::dump() const
{
    printf("< %.2f\t%.2f\t%.2f\t|\n",m_a1, m_a2, m_a3);
    printf("| %.2f\t%.2f\t%.2f\t|\n",m_b1, m_b2, m_b3);
    printf("| %.2f\t%.2f\t%.2f\t>\n",m_c1, m_c2, m_c3);
}

inline Matrix3 & Matrix3::operator+=( const Matrix3 &b )
{   m_a1 += b.m_a1; m_a2 += b.m_a2; m_a3 += b.m_a3;
    m_b1 += b.m_b1; m_b2 += b.m_b2; m_b3 += b.m_b3;
    m_c1 += b.m_c1; m_c2 += b.m_c2; m_c3 += b.m_c3;
    return *this; }

inline Matrix3 & Matrix3::operator-=( const Matrix3 &b )
{   m_a1 -= b.m_a1; m_a2 -= b.m_a2; m_a3 -= b.m_a3;
    m_b1 -= b.m_b1; m_b2 -= b.m_b2; m_b3 -= b.m_b3;
    m_c1 -= b.m_c1; m_c2 -= b.m_c2; m_c3 -= b.m_c3;
    return *this; }

inline Matrix3 & Matrix3::operator*=( double k )
{   m_a1 *= k; m_a2 *= k; m_a3 *= k;
    m_b1 *= k; m_b2 *= k; m_b3 *= k;
    m_c1 *= k; m_c2 *= k; m_c3 *= k;
    return *this; }

inline bool operator==( const Matrix3 &a, const Matrix3 &b )
{ return a.m_a1 == b.m_a1 && a.m_a2 == b.m_a2 && a.m_a3 == b.m_a3 &&
         a.m_b1 == b.m_b1 && a.m_b2 == b.m_b2 && a.m_b3 == b.m_b3 &&
         a.m_c1 == b.m_c1 && a.m_c2 == b.m_c2 && a.m_c3 == b.m_c3; }

inline bool operator!=( const Matrix3 &a, const Matrix3 &b )
{ return a.m_a1 != b.m_a1 || a.m_a2 != b.m_a2 || a.m_a3 != b.m_a3 ||
         a.m_b1 != b.m_b1 || a.m_b2 != b.m_b2 || a.m_b3 != b.m_b3 ||
         a.m_c1 != b.m_c1 || a.m_c2 != b.m_c2 || a.m_c3 != b.m_c3; }

inline const Matrix3 operator+( const Matrix3 &a, const Matrix3 &b )
{ return Matrix3( a.m_a1 + b.m_a1, a.m_a2 + b.m_a2, a.m_a3 + b.m_a3,
                  a.m_b1 + b.m_b1, a.m_b2 + b.m_b2, a.m_b3 + b.m_b3,
                  a.m_c1 + b.m_c1, a.m_c2 + b.m_c2, a.m_c3 + b.m_c3 ); }

inline const Matrix3 operator-( const Matrix3 &a, const Matrix3 &b )
{ return Matrix3( a.m_a1 - b.m_a1, a.m_a2 - b.m_a2, a.m_a3 - b.m_a3,
                  a.m_b1 - b.m_b1, a.m_b2 - b.m_b2, a.m_b3 - b.m_b3,
                  a.m_c1 - b.m_c1, a.m_c2 - b.m_c2, a.m_c3 - b.m_c3 ); }

inline const Matrix3 operator*( const Matrix3 & a, double k )
{ return Matrix3( a.m_a1 * k, a.m_a2 * k, a.m_a3 * k,
                  a.m_b1 * k, a.m_b2 * k, a.m_b3 * k,
                  a.m_c1 * k, a.m_c2 * k, a.m_c3 * k ); }

inline const Matrix3 operator*( double k, const Matrix3 & a )
{ return a*k; }

#endif