71
71
template<class T> class Vector2
75
inline Vector2(const Vector2<T>&);
75
inline Vector2(const Vector2<T>&);
78
78
inline Vector2& operator=(const Vector2<T>&);
79
79
inline Vector2& operator=(const T*);
80
inline void set(T, T);
80
inline void set(T, T);
82
82
inline bool operator==(const Vector2<T>&) const;
83
83
inline bool operator!=(const Vector2<T>&) const;
85
85
inline const T& operator[](int x) const;
86
inline T& operator[](int);
86
inline T& operator[](int);
87
87
inline operator const T*() const;
88
88
inline operator T*();
90
inline Vector2& operator+=(const Vector2<T>&);
91
inline Vector2& operator-=(const Vector2<T>&);
92
inline Vector2& operator*=(T);
90
inline Vector2& operator+=(const Vector2<T>&);
91
inline Vector2& operator-=(const Vector2<T>&);
92
inline Vector2& operator*=(T);
93
93
inline Vector2& operator/=(T);
95
inline Vector2 operator-(const Vector2<T>&) const;
96
inline Vector2 operator+(const Vector2<T>&) const;
95
inline Vector2 operator-(const Vector2<T>&) const;
96
inline Vector2 operator+(const Vector2<T>&) const;
98
98
inline Vector2 operator-() const;
99
99
inline Vector2 operator+() const;
102
102
inline Vector2 operator/(T) const;
105
inline T dot(const Vector2<T>&) const;
107
inline T length() const;
108
inline T lengthSquared() const;
109
inline void normalize();
105
inline T dot(const Vector2<T>&) const;
107
inline T length() const;
108
inline T lengthSquared() const;
109
inline void normalize();
114
115
//! @class Vector3
115
116
//! A templatized 3d vector compatible with openGL.
116
117
//! Use Vec3d or Vec3f typdef for vectors of double and float respectively.
117
118
template<class T> class Vector3
121
inline Vector3(const Vector3&);
122
inline Vector3(const Vector3&);
122
123
template <class T2> inline Vector3(const Vector3<T2>&);
123
inline Vector3(T, T, T);
124
inline Vector3(T, T, T);
124
125
inline Vector3(T);
126
127
inline Vector3& operator=(const Vector3&);
127
128
inline Vector3& operator=(const T*);
128
129
template <class T2> inline Vector3& operator=(const Vector3<T2>&);
129
inline void set(T, T, T);
130
inline void set(T, T, T);
131
132
inline bool operator==(const Vector3<T>&) const;
132
133
inline bool operator!=(const Vector3<T>&) const;
134
inline T& operator[](int);
135
inline const T& operator[](int) const;
135
inline T& operator[](int);
136
inline const T& operator[](int) const;
136
137
inline operator const T*() const;
137
138
inline operator T*();
139
inline const T* data() const {return v;}
140
inline T* data() {return v;}
139
inline Vector3& operator+=(const Vector3<T>&);
140
inline Vector3& operator-=(const Vector3<T>&);
141
inline Vector3& operator*=(T);
142
inline Vector3& operator+=(const Vector3<T>&);
143
inline Vector3& operator-=(const Vector3<T>&);
144
inline Vector3& operator*=(T);
142
145
inline Vector3& operator/=(T);
144
inline Vector3 operator-(const Vector3<T>&) const;
145
inline Vector3 operator+(const Vector3<T>&) const;
147
inline Vector3 operator-(const Vector3<T>&) const;
148
inline Vector3 operator+(const Vector3<T>&) const;
147
150
inline Vector3 operator-() const;
148
151
inline Vector3 operator+() const;
151
154
inline Vector3 operator/(T) const;
154
inline T dot(const Vector3<T>&) const;
157
inline T dot(const Vector3<T>&) const;
155
158
inline Vector3 operator^(const Vector3<T>&) const;
157
160
// Return latitude in rad
158
161
inline T latitude() const;
159
162
// Return longitude in rad
160
163
inline T longitude() const;
162
165
// Distance in radian between two
163
166
inline T angle(const Vector3<T>&) const;
165
inline T length() const;
166
inline T lengthSquared() const;
167
inline void normalize();
168
inline T length() const;
169
inline T lengthSquared() const;
170
inline void normalize();
169
172
inline void transfo4d(const Mat4d&);
170
173
inline void transfo4d(const Mat4f&);
171
174
T v[3]; // The 3 values
173
176
QString toString() const {return QString("[%1, %2, %3]").arg(v[0]).arg(v[1]).arg(v[2]);}
177
QString toStringLonLat() const {return QString("[") + QString::number(longitude()*180./M_PI, 'g', 12) + "," + QString::number(latitude()*180./M_PI, 'g', 12)+"]";}
189
193
inline Vector4& operator=(const Vector4<T>&);
190
194
inline Vector4& operator=(const Vector3<T>&);
191
195
inline Vector4& operator=(const T*);
192
inline void set(T, T, T, T);
196
inline void set(T, T, T, T);
194
198
inline bool operator==(const Vector4<T>&) const;
195
199
inline bool operator!=(const Vector4<T>&) const;
197
inline T& operator[](int);
198
inline const T& operator[](int) const;
201
inline T& operator[](int);
202
inline const T& operator[](int) const;
199
203
inline operator T*();
200
204
inline operator const T*() const;
202
inline Vector4& operator+=(const Vector4<T>&);
203
inline Vector4& operator-=(const Vector4<T>&);
204
inline Vector4& operator*=(T);
206
inline Vector4& operator+=(const Vector4<T>&);
207
inline Vector4& operator-=(const Vector4<T>&);
208
inline Vector4& operator*=(T);
205
209
inline Vector4& operator/=(T);
207
inline Vector4 operator-(const Vector4<T>&) const;
208
inline Vector4 operator+(const Vector4<T>&) const;
211
inline Vector4 operator-(const Vector4<T>&) const;
212
inline Vector4 operator+(const Vector4<T>&) const;
210
214
inline Vector4 operator-() const;
211
215
inline Vector4 operator+() const;
231
235
template<class T> class Matrix4
235
Matrix4(const Matrix4<T>& m);
236
Matrix4(T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T);
238
Matrix4(const Vector4<T>&, const Vector4<T>&,
239
const Vector4<T>&, const Vector4<T>&);
239
Matrix4(const Matrix4<T>& m);
240
Matrix4(T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T);
242
Matrix4(const Vector4<T>&, const Vector4<T>&,
243
const Vector4<T>&, const Vector4<T>&);
241
245
inline Matrix4& operator=(const Matrix4<T>&);
242
246
inline Matrix4& operator=(const T*);
243
247
inline void set(T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T);
245
inline T* operator[](int);
249
inline T& operator[](int);
246
250
inline operator T*();
247
251
inline operator const T*() const;
249
inline Matrix4 operator-(const Matrix4<T>&) const;
250
inline Matrix4 operator+(const Matrix4<T>&) const;
251
inline Matrix4 operator*(const Matrix4<T>&) const;
253
inline Matrix4 operator-(const Matrix4<T>&) const;
254
inline Matrix4 operator+(const Matrix4<T>&) const;
255
inline Matrix4 operator*(const Matrix4<T>&) const;
253
inline Vector3<T> operator*(const Vector3<T>&) const;
254
inline Vector3<T> multiplyWithoutTranslation(const Vector3<T>& a) const;
257
inline Vector3<T> operator*(const Vector3<T>&) const;
258
inline Vector3<T> multiplyWithoutTranslation(const Vector3<T>& a) const;
255
259
inline Vector4<T> operator*(const Vector4<T>&) const;
257
261
inline void transfo(Vector3<T>&) const;
259
static Matrix4<T> identity();
260
static Matrix4<T> translation(const Vector3<T>&);
262
// static Matrix4<T> rotation(const Vector3<T>&);
263
static Matrix4<T> rotation(const Vector3<T>&, T);
264
static Matrix4<T> xrotation(T);
265
static Matrix4<T> yrotation(T);
266
static Matrix4<T> zrotation(T);
267
static Matrix4<T> scaling(const Vector3<T>&);
268
static Matrix4<T> scaling(T);
270
Matrix4<T> transpose() const;
271
Matrix4<T> inverse() const;
263
static Matrix4<T> identity();
264
static Matrix4<T> translation(const Vector3<T>&);
266
// static Matrix4<T> rotation(const Vector3<T>&);
267
static Matrix4<T> rotation(const Vector3<T>&, T);
268
static Matrix4<T> xrotation(T);
269
static Matrix4<T> yrotation(T);
270
static Matrix4<T> zrotation(T);
271
static Matrix4<T> scaling(const Vector3<T>&);
272
static Matrix4<T> scaling(T);
274
Matrix4<T> transpose() const;
275
Matrix4<T> inverse() const;
273
277
inline void print(void) const;
282
//! Serialization routines.
283
template<class T> QDataStream& operator<<(QDataStream& out, const Vector2<T>& v) {out << v[0] << v[1]; return out;}
284
template<class T> QDataStream& operator<<(QDataStream& out, const Vector3<T>& v) {out << v[0] << v[1] << v[2]; return out;}
285
template<class T> QDataStream& operator<<(QDataStream& out, const Vector4<T>& v) {out << v[0] << v[1] << v[2] << v[3]; return out;}
286
template<class T> QDataStream& operator<<(QDataStream& out, const Matrix4<T>& m) {out << m[0] << m[1] << m[2] << m[3] << m[4] << m[5] << m[6] << m[7] << m[8] << m[9] << m[10] << m[11] << m[12] << m[13] << m[14] << m[15]; return out;}
288
template<class T> QDataStream& operator>>(QDataStream& in, Vector2<T>& v) {in >> v[0] >> v[1]; return in;}
289
template<class T> QDataStream& operator>>(QDataStream& in, Vector3<T>& v) {in >> v[0] >> v[1] >> v[2]; return in;}
290
template<class T> QDataStream& operator>>(QDataStream& in, Vector4<T>& v) {in >> v[0] >> v[1] >> v[2] >> v[3]; return in;}
291
template<class T> QDataStream& operator>>(QDataStream& in, Matrix4<T>& m) {in >> m[0] >> m[1] >> m[2] >> m[3] >> m[4] >> m[5] >> m[6] >> m[7] >> m[8] >> m[9] >> m[10] >> m[11] >> m[12] >> m[13] >> m[14] >> m[15]; return in;}
280
293
////////////////////////// Vector2 class methods ///////////////////////////////
343
356
template<class T> Vector2<T>& Vector2<T>::operator+=(const Vector2<T>& a)
345
v[0] += a.v[0]; v[1] += a.v[1];
358
v[0] += a.v[0]; v[1] += a.v[1];
349
362
template<class T> Vector2<T>& Vector2<T>::operator-=(const Vector2<T>& a)
351
v[0] -= a.v[0]; v[1] -= a.v[1];
364
v[0] -= a.v[0]; v[1] -= a.v[1];
355
368
template<class T> Vector2<T>& Vector2<T>::operator*=(T s)
357
v[0] *= s; v[1] *= s;
370
v[0] *= s; v[1] *= s;
361
374
template<class T> Vector2<T> Vector2<T>::operator-() const
363
return Vector2<T>(-v[0], -v[1]);
376
return Vector2<T>(-v[0], -v[1]);
366
379
template<class T> Vector2<T> Vector2<T>::operator+() const
371
384
template<class T> Vector2<T> Vector2<T>::operator+(const Vector2<T>& b) const
373
return Vector2<T>(v[0] + b.v[0], v[1] + b.v[1]);
386
return Vector2<T>(v[0] + b.v[0], v[1] + b.v[1]);
376
389
template<class T> Vector2<T> Vector2<T>::operator-(const Vector2<T>& b) const
378
return Vector2<T>(v[0] - b.v[0], v[1] - b.v[1]);
391
return Vector2<T>(v[0] - b.v[0], v[1] - b.v[1]);
381
394
template<class T> Vector2<T> Vector2<T>::operator*(T s) const
383
return Vector2<T>(s * v[0], s * v[1]);
396
return Vector2<T>(s * v[0], s * v[1]);
386
399
template<class T> Vector2<T> Vector2<T>::operator/(T s) const
388
return Vector2<T>(v[0]/s, v[1]/s);
401
return Vector2<T>(v[0]/s, v[1]/s);
392
405
template<class T> T Vector2<T>::dot(const Vector2<T>& b) const
394
return v[0] * b.v[0] + v[1] * b.v[1];
407
return v[0] * b.v[0] + v[1] * b.v[1];
398
411
template<class T> T Vector2<T>::length() const
400
return (T) std::sqrt(v[0] * v[0] + v[1] * v[1]);
413
return (T) std::sqrt(v[0] * v[0] + v[1] * v[1]);
403
416
template<class T> T Vector2<T>::lengthSquared() const
405
return v[0] * v[0] + v[1] * v[1];
418
return v[0] * v[0] + v[1] * v[1];
408
421
template<class T> void Vector2<T>::normalize()
410
T s = (T) 1 / std::sqrt(v[0] * v[0] + v[1] * v[1]);
423
T s = (T) 1 / std::sqrt(v[0] * v[0] + v[1] * v[1]);
416
429
// std::ostream& operator<<(std::ostream &o,const Vector2<T> &v) {
417
430
// return o << '[' << v[0] << ',' << v[1] << ']';
523
536
template<class T> Vector3<T> Vector3<T>::operator-() const
525
return Vector3<T>(-v[0], -v[1], -v[2]);
538
return Vector3<T>(-v[0], -v[1], -v[2]);
528
541
template<class T> Vector3<T> Vector3<T>::operator+() const
533
546
template<class T> Vector3<T> Vector3<T>::operator+(const Vector3<T>& b) const
535
return Vector3<T>(v[0] + b.v[0], v[1] + b.v[1], v[2] + b.v[2]);
548
return Vector3<T>(v[0] + b.v[0], v[1] + b.v[1], v[2] + b.v[2]);
538
551
template<class T> Vector3<T> Vector3<T>::operator-(const Vector3<T>& b) const
540
return Vector3<T>(v[0] - b.v[0], v[1] - b.v[1], v[2] - b.v[2]);
553
return Vector3<T>(v[0] - b.v[0], v[1] - b.v[1], v[2] - b.v[2]);
543
556
template<class T> Vector3<T> Vector3<T>::operator*(T s) const
545
return Vector3<T>(s * v[0], s * v[1], s * v[2]);
558
return Vector3<T>(s * v[0], s * v[1], s * v[2]);
548
561
template<class T> Vector3<T> Vector3<T>::operator/(T s) const
550
return Vector3<T>(v[0]/s, v[1]/s, v[2]/s);
563
return Vector3<T>(v[0]/s, v[1]/s, v[2]/s);
554
567
template<class T> T Vector3<T>::dot(const Vector3<T>& b) const
556
return v[0] * b.v[0] + v[1] * b.v[1] + v[2] * b.v[2];
569
return v[0] * b.v[0] + v[1] * b.v[1] + v[2] * b.v[2];
561
574
template<class T> Vector3<T> Vector3<T>::operator^(const Vector3<T>& b) const
563
return Vector3<T>(v[1] * b.v[2] - v[2] * b.v[1],
564
v[2] * b.v[0] - v[0] * b.v[2],
565
v[0] * b.v[1] - v[1] * b.v[0]);
576
return Vector3<T>(v[1] * b.v[2] - v[2] * b.v[1],
577
v[2] * b.v[0] - v[0] * b.v[2],
578
v[0] * b.v[1] - v[1] * b.v[0]);
568
581
// Angle in radian between two normalized vectors
574
587
template<class T> T Vector3<T>::length() const
576
return (T) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
589
return (T) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
579
592
template<class T> T Vector3<T>::lengthSquared() const
581
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
594
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
584
597
template<class T> void Vector3<T>::normalize()
586
T s = (T) (1. / std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]));
599
T s = (T) (1. / std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]));
592
605
template<class T> void Vector3<T>::transfo4d(const Mat4d& m)
609
v[0]=m.r[0]*v0 + m.r[4]*v1 + m.r[8]*v[2] + m.r[12];
610
v[1]=m.r[1]*v0 + m.r[5]*v1 + m.r[9]*v[2] + m.r[13];
611
v[2]=m.r[2]*v0 + m.r[6]*v1 + m.r[10]*v[2] + m.r[14];
597
614
template<class T> void Vector3<T>::transfo4d(const Mat4f& m)
618
v[0]=m.r[0]*v0 + m.r[4]*v1 + m.r[8]*v[2] + m.r[12];
619
v[1]=m.r[1]*v0 + m.r[5]*v1 + m.r[9]*v[2] + m.r[13];
620
v[2]=m.r[2]*v0 + m.r[6]*v1 + m.r[10]*v[2] + m.r[14];
602
623
// Return latitude in rad
710
713
template<class T> Vector4<T>& Vector4<T>::operator+=(const Vector4<T>& a)
712
v[0] += a.v[0]; v[1] += a.v[1]; v[2] += a.v[2]; v[3] += a.v[3];
715
v[0] += a.v[0]; v[1] += a.v[1]; v[2] += a.v[2]; v[3] += a.v[3];
716
719
template<class T> Vector4<T>& Vector4<T>::operator-=(const Vector4<T>& a)
718
v[0] -= a.v[0]; v[1] -= a.v[1]; v[2] -= a.v[2]; v[3] -= a/v[3];
721
v[0] -= a.v[0]; v[1] -= a.v[1]; v[2] -= a.v[2]; v[3] -= a/v[3];
722
725
template<class T> Vector4<T>& Vector4<T>::operator*=(T s)
724
v[0] *= s; v[1] *= s; v[2] *= s; v[3] *= s;
727
v[0] *= s; v[1] *= s; v[2] *= s; v[3] *= s;
728
731
template<class T> Vector4<T> Vector4<T>::operator-() const
730
return Vector4<T>(-v[0], -v[1], -v[2], -v[3]);
733
return Vector4<T>(-v[0], -v[1], -v[2], -v[3]);
733
736
template<class T> Vector4<T> Vector4<T>::operator+() const
738
741
template<class T> Vector4<T> Vector4<T>::operator+(const Vector4<T>& b) const
740
return Vector4<T>(v[0] + b.v[0], v[1] + b.v[1], v[2] + b.v[2], v[3] + b.v[3]);
743
return Vector4<T>(v[0] + b.v[0], v[1] + b.v[1], v[2] + b.v[2], v[3] + b.v[3]);
743
746
template<class T> Vector4<T> Vector4<T>::operator-(const Vector4<T>& b) const
745
return Vector4<T>(v[0] - b.v[0], v[1] - b.v[1], v[2] - b.v[2], v[3] - b.v[3]);
748
return Vector4<T>(v[0] - b.v[0], v[1] - b.v[1], v[2] - b.v[2], v[3] - b.v[3]);
748
751
template<class T> Vector4<T> Vector4<T>::operator*(T s) const
750
return Vector4<T>(s * v[0], s * v[1], s * v[2], s * v[3]);
753
return Vector4<T>(s * v[0], s * v[1], s * v[2], s * v[3]);
753
756
template<class T> Vector4<T> Vector4<T>::operator/(T s) const
755
return Vector4<T>(v[0]/s, v[1]/s, v[2]/s, v[3]/s);
758
return Vector4<T>(v[0]/s, v[1]/s, v[2]/s, v[3]/s);
758
761
template<class T> T Vector4<T>::dot(const Vector4<T>& b) const
760
return v[0] * b.v[0] + v[1] * b.v[1] + v[2] * b.v[2] + v[3] * b.v[3];
763
return v[0] * b.v[0] + v[1] * b.v[1] + v[2] * b.v[2] + v[3] * b.v[3];
763
766
template<class T> T Vector4<T>::length() const
765
return (T) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
768
return (T) sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
768
771
template<class T> T Vector4<T>::lengthSquared() const
770
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
773
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
773
776
template<class T> void Vector4<T>::normalize()
775
T s = (T) (1. / sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]));
778
T s = (T) (1. / sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]));
873
876
template<class T> Matrix4<T> Matrix4<T>::translation(const Vector3<T>& a)
875
return Matrix4<T>( 1, 0, 0, 0,
878
return Matrix4<T>( 1, 0, 0, 0,
878
881
a.v[0], a.v[1], a.v[2], 1);
881
template<class T> Matrix4<T> Matrix4<T>::rotation(const Vector3<T>& a,
886
// T c = (T) cos(angle);
887
// T s = (T) sin(angle);
890
// return Matrix4<T>(Vector4<T>(t * axis.v[0] * axis.v[0] + c,
891
// t * axis.v[0] * axis.v[1] - s * axis.v[2],
892
// t * axis.v[0] * axis.v[2] + s * axis.v[1],
894
// Vector4<T>(t * axis.v[0] * axis.v[1] + s * axis.v[2],
895
// t * axis.v[1] * axis.v[1] + c,
896
// t * axis.v[1] * axis.v[2] - s * axis.v[0],
898
// Vector4<T>(t * axis.v[0] * axis.v[2] - s * axis.v[1],
899
// t * axis.v[1] * axis.v[2] + s * axis.v[0],
900
// t * axis.v[2] * axis.v[2] + c,
902
// Vector4<T>(0, 0, 0, 1));
904
T sin_a = std::sin( angle / 2 );
905
T cos_a = std::cos( angle / 2 );
906
T X = axis[0] * sin_a;
907
T Y = axis[1] * sin_a;
908
T Z = axis[2] * sin_a;
924
1. - 2. * ( yy + zz ), 2. * ( xy + zw ), 2. * ( xz - yw ), 0.,
925
2. * ( xy - zw ), 1. - 2. * ( xx + zz ), 2. * ( yz + xw ), 0.,
926
2. * ( xz + yw ), 2. * ( yz - xw ), 1. - 2. * ( xx + yy ), 0.,
933
template<class T> Matrix4<T> Matrix4<T>::rotation(const Vector3<T>& a)
935
T c = (T) cos(angle);
936
T s = (T) sin(angle);
938
return Matrix4<T>( a.v[0]*a.v[0]*d+c, a.v[1]*a.v[0]*d+a.v[2]*s, a.v[0]*a.v[2]*d-a.v[1]*s, 0,
939
a.v[0]*a.v[1]*d-a.v[2]*s, a.v[1]*a.v[1]*d+c, a.v[1]*a.v[2]*d+a.v[0]*s, 0,
940
a.v[0]*a.v[2]*d+a.v[1]*s, a.v[1]*a.v[2]*d-a.v[0]*s, a.v[2]*a.v[2]*d+c, 0,
885
template<class T> Matrix4<T> Matrix4<T>::rotation(const Vector3<T>& axis, T angle)
889
const T c = (T) cos(angle);
890
const T s = (T) sin(angle);
892
return Matrix4<T>( a[0]*a[0]*d+c , a[1]*a[0]*d+a[2]*s, a[0]*a[2]*d-a[1]*s, 0,
893
a[0]*a[1]*d-a[2]*s, a[1]*a[1]*d+c , a[1]*a[2]*d+a[0]*s, 0,
894
a[0]*a[2]*d+a[1]*s, a[1]*a[2]*d-a[0]*s, a[2]*a[2]*d+c , 0,
944
898
template<class T> Matrix4<T> Matrix4<T>::xrotation(T angle)
946
T c = (T) cos(angle);
947
T s = (T) sin(angle);
900
T c = (T) cos(angle);
901
T s = (T) sin(angle);
949
return Matrix4<T>(1, 0, 0, 0,
903
return Matrix4<T>(1, 0, 0, 0,
956
910
template<class T> Matrix4<T> Matrix4<T>::yrotation(T angle)
958
T c = (T) cos(angle);
959
T s = (T) sin(angle);
912
T c = (T) cos(angle);
913
T s = (T) sin(angle);
961
return Matrix4<T>( c, 0,-s, 0,
915
return Matrix4<T>( c, 0,-s, 0,
968
922
template<class T> Matrix4<T> Matrix4<T>::zrotation(T angle)
970
T c = (T) cos(angle);
971
T s = (T) sin(angle);
924
T c = (T) cos(angle);
925
T s = (T) sin(angle);
973
return Matrix4<T>(c, s, 0, 0,
927
return Matrix4<T>(c, s, 0, 0,
980
934
template<class T> Matrix4<T> Matrix4<T>::scaling(const Vector3<T>& s)
982
return Matrix4<T>(s[0], 0 , 0 , 0,
936
return Matrix4<T>(s[0], 0 , 0 , 0,
989
943
template<class T> Matrix4<T> Matrix4<T>::scaling(T scale)
991
return scaling(Vector3<T>(scale, scale, scale));
945
return scaling(Vector3<T>(scale, scale, scale));
994
948
// multiply column vector by a 4x4 matrix in homogeneous coordinate (use a[3]=1)
995
949
template<class T> Vector3<T> Matrix4<T>::operator*(const Vector3<T>& a) const
997
return Vector3<T>( r[0]*a.v[0] + r[4]*a.v[1] + r[8]*a.v[2] + r[12],
951
return Vector3<T>( r[0]*a.v[0] + r[4]*a.v[1] + r[8]*a.v[2] + r[12],
998
952
r[1]*a.v[0] + r[5]*a.v[1] + r[9]*a.v[2] + r[13],
999
953
r[2]*a.v[0] + r[6]*a.v[1] + r[10]*a.v[2] + r[14] );
1002
956
template<class T> Vector3<T> Matrix4<T>::multiplyWithoutTranslation(const Vector3<T>& a) const
1004
return Vector3<T>( r[0]*a.v[0] + r[4]*a.v[1] + r[8]*a.v[2],
958
return Vector3<T>( r[0]*a.v[0] + r[4]*a.v[1] + r[8]*a.v[2],
1005
959
r[1]*a.v[0] + r[5]*a.v[1] + r[9]*a.v[2],
1006
960
r[2]*a.v[0] + r[6]*a.v[1] + r[10]*a.v[2] );
1032
986
template<class T> Matrix4<T> Matrix4<T>::operator*(const Matrix4<T>& a) const
1034
988
#define MATMUL(R, C) (r[R] * a.r[C] + r[R+4] * a.r[C+1] + r[R+8] * a.r[C+2] + r[R+12] * a.r[C+3])
1035
return Matrix4<T>( MATMUL(0,0), MATMUL(1,0), MATMUL(2,0), MATMUL(3,0),
989
return Matrix4<T>( MATMUL(0,0), MATMUL(1,0), MATMUL(2,0), MATMUL(3,0),
1036
990
MATMUL(0,4), MATMUL(1,4), MATMUL(2,4), MATMUL(3,4),
1037
991
MATMUL(0,8), MATMUL(1,8), MATMUL(2,8), MATMUL(3,8),
1038
992
MATMUL(0,12), MATMUL(1,12), MATMUL(2,12), MATMUL(3,12) );
1078
1032
r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
1080
1034
r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
1081
r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
1082
r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
1083
r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
1084
r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
1085
r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
1086
r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
1087
r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
1088
r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
1089
r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
1090
r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
1091
r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
1035
r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
1036
r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
1037
r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
1038
r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
1039
r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
1040
r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
1041
r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
1042
r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
1043
r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
1044
r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
1045
r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
1093
1047
/* choose pivot - or die */
1094
1048
if (fabs(r3[0]) > fabs(r2[0]))
1096
1050
if (fabs(r2[0]) > fabs(r1[0]))
1098
1052
if (fabs(r1[0]) > fabs(r0[0]))
1100
1054
if (0.0 == r0[0])
1101
return Matrix4<T>();
1055
return Matrix4<T>();
1103
1057
/* eliminate first variable */
1104
1058
m1 = r1[0] / r0[0];
1201
1155
m2 = r2[3]; /* now back substitute row 2 */
1202
1156
s = 1.0 / r2[2];
1203
1157
r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
1204
r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
1158
r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
1206
1160
r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
1207
r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
1161
r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
1209
1163
r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
1210
r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
1164
r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
1212
1166
m1 = r1[2]; /* now back substitute row 1 */
1213
1167
s = 1.0 / r1[1];
1214
1168
r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
1215
r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
1169
r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
1217
1171
r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
1218
r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
1172
r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
1220
1174
m0 = r0[1]; /* now back substitute row 0 */
1221
1175
s = 1.0 / r0[0];
1222
1176
r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
1223
r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
1177
r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
1225
1179
MAT(out, 0, 0) = r0[4];
1226
1180
MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];