EMAN2
vecmath.h
Go to the documentation of this file.
1/*
2 * Author: Tao Ju, 5/16/2007 <taoju@cs.wustl.edu>, code ported by Grant Tang
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef _VECMATH_H_
33#define _VECMATH_H_
34
35//#include "cse452.h"
36#include <iostream>
37#include <cmath>
38#include <cstring>
39#include "emassert.h"
40
41namespace EMAN
42{
43
44 inline bool isZero(double in_d, double in_dEps = 1e-16 )
45 {
46 return (in_d < in_dEps && in_d > -in_dEps)? true : false;
47 }
48
49
50 // Dot product, subtraction, ofstream, etc., are declared after class
52 public:
53 ScreenVector() : x(0), y(0) {}
54 ScreenVector(const ScreenVector& v) : x(v[0]), y(v[1]) {}
55 ScreenVector(int _x, int _y) : x(_x), y(_y) {}
56
58 x = a[0]; y = a[1];
59 return *this;
60 }
61
62 const int &operator[](int n) const { return (&x)[n]; }
63 int &operator[](int n) { return (&x)[n]; }
64
66 x += a[0]; y += a[1];
67 return *this;
68 }
69
71 x -= a[0]; y -= a[1];
72 return *this;
73 }
74
76 x *= s; y *= s;
77 return *this;
78 }
79
81 return ScreenVector(-x, -y);
82 }
83
85 return *this;
86 }
87
89 return ScreenVector( x + v.x, y + v.y );
90 }
91
93 return ScreenVector( x - v.x, y - v.y );
94 }
95
96 ScreenVector operator*( const double s ) const {
97 return ScreenVector( (int)(x * s), (int)(y * s) );
98 }
99
100 // Dot
101 int operator*( const ScreenVector &v ) const {
102 return x * v.x + y * v.y;
103 }
104
105 double length() const {
106 return (double) sqrt( (double) (x * x + y * y) );
107 }
108
109 int lengthSquared() const {
110 return x * x + y * y;
111 }
112
113 bool operator==( const ScreenVector &v ) const {
114 return x == v.x && y == v.y;
115 }
116
117 bool operator!=( const ScreenVector &v ) const {
118 return x != v.x || y != v.y;
119 }
120
121 void print() const {
122 std::cout << "(" << x << ", " << y << ")";
123 }
124
125 private:
126 int x, y;
127 };
128
129 inline ScreenVector operator*( const double s, const ScreenVector &v ) {
130 return ScreenVector( (int)(v[0] * s), (int)(v[1] * s) );
131 }
132
133 inline std::ostream& operator<<(std::ostream& os, const ScreenVector& v) {
134 os << "(" << v[0] << ", " << v[1] << ")";
135 return os;
136 }
137
138
140 public:
141 ScreenPoint() : x(0), y(0) {}
142 ScreenPoint(const ScreenPoint & p) : x(p[0]), y(p[1]) {}
143 ScreenPoint(int _x, int _y) : x(_x), y(_y) {}
144
146 x = a[0]; y = a[1];
147 return *this;
148 }
149
150 const int &operator[](int n) const { return (&x)[n]; }
151 int &operator[](int n) { return (&x)[n]; }
152
154 x += v[0]; y += v[1];
155 return *this;
156 }
157
159 x -= v[0]; y -= v[1];
160 return *this;
161 }
162
164 x *= s; y *= s;
165 return *this;
166 }
167
169 return ScreenPoint( x + v[0], y + v[1] );
170 }
171
173 return ScreenVector( x - p.x, y - p.y );
174 }
175
177 return ScreenPoint( x - v[0], y - v[1] );
178 }
179
180 bool operator==( const ScreenPoint &p ) const {
181 return x == p.x && y == p.y;
182 }
183
184 bool operator!=( const ScreenPoint &p ) const {
185 return x != p.x || y != p.y;
186 }
187
188 void print() const {
189 std::cout << "(" << x << ", " << y << ")";
190 }
191
192 private:
193 int x, y;
194 };
195
196 inline std::ostream& operator<<(std::ostream& os, const ScreenPoint& p) {
197 os << "(" << p[0] << ", " << p[1] << ")";
198 return os;
199 }
200
201
202 class Vector3 {
203 public:
204 Vector3() : x(0), y(0), z(0) {}
205 Vector3(const Vector3& v) : x(v[0]), y(v[1]), z(v[2]) {}
206 Vector3(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
207
209 x = a[0]; y = a[1]; z = a[2];
210 return *this;
211 }
212
213 const double &operator[](int n) const { return (&x)[n]; }
214 double &operator[](int n) { return (&x)[n]; }
215
217 x += a[0]; y += a[1]; z += a[2];
218 return *this;
219 }
220
222 x -= a[0]; y -= a[1]; z -= a[2];
223 return *this;
224 }
225
226 Vector3& operator*=(double s) {
227 x *= s; y *= s; z *= s;
228 return *this;
229 }
230
232 return Vector3(-x, -y, -z);
233 }
234
236 return *this;
237 }
238
239 Vector3 operator+( const Vector3 &v ) const {
240 return Vector3( x + v.x, y + v.y, z + v.z );
241 }
242
243 Vector3 operator-( const Vector3 &v ) const {
244 return Vector3( x - v.x, y - v.y, z - v.z );
245 }
246
247 Vector3 operator/( const double s ) const {
248 Assert( s > 0.0 );
249 return Vector3( x / s, y / s, z / s );
250 }
251
252 Vector3 operator*( const double s ) const {
253 return Vector3( x * s, y * s, z * s );
254 }
255
256 // Dot
257 double operator*( const Vector3 &v ) const {
258 return x * v.x + y * v.y + z * v.z;
259 }
260
261 // Cross product
262 Vector3 operator^( const Vector3 &v ) const {
263 return Vector3( y * v.z - z * v.y,
264 z * v.x - x * v.z,
265 x * v.y - y * v.x );
266 }
267
268 double length() const {
269 return (double) sqrt(x * x + y * y + z * z);
270 }
271
272 double lengthSquared() const {
273 return x * x + y * y + z * z;
274 }
275
276 void normalize() {
277 double s = 1.0 / (double) sqrt(x * x + y * y + z * z);
278 x *= s; y *= s; z *= s;
279 }
280
281 bool operator==( const Vector3 &v ) const {
282 return x == v.x && y == v.y && z == v.z;
283 }
284
285 bool operator!=( const Vector3 &v ) const {
286 return x != v.x || y != v.y || z != v.z;
287 }
288
289 bool approxEqual( const Vector3 &v, double eps = 1e-12 ) const {
290 return isZero( x - v.x, eps ) && isZero( y - v.y, eps ) && isZero( z - v.z, eps );
291 }
292
293 void print() const {
294 std::cout << x << " " << y << " " << z << "\n";
295 }
296
297 private:
298 double x, y, z;
299 };
300
301 inline Vector3 operator*( const double s, const Vector3 &v ) {
302 return Vector3( v[0] * s, v[1] * s, v[2] * s );
303 }
304
305 inline double dot( const Vector3 &w, const Vector3 &v ) {
306 return w * v;
307 }
308
309 inline Vector3 cross( const Vector3 &w, const Vector3 &v ) {
310 return w ^ v;
311 }
312
313 inline double length( const Vector3 &v ) { return v.length(); }
314 inline Vector3 unit( const Vector3 &v ) { const double len = v.length(); return v / len; }
315
316 inline std::ostream& operator<<(std::ostream& os, const Vector3& v) {
317 os << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")";
318 return os;
319 }
320
321 class Point3 {
322 public:
323 Point3() : x(0), y(0), z(0) {}
324 Point3(const Point3& p) : x(p[0]), y(p[1]), z(p[2]) {}
325 Point3(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
326
328 x = a[0]; y = a[1]; z = a[2];
329 return *this;
330 }
331
332 const double &operator[](int n) const { return (&x)[n]; }
333 double &operator[](int n) { return (&x)[n]; }
334
336 x += v[0]; y += v[1]; z += v[2];
337 return *this;
338 }
339
341 x -= v[0]; y -= v[1]; z -= v[2];
342 return *this;
343 }
344
345 Point3& operator*=(double s) {
346 x *= s; y *= s; z *= s;
347 return *this;
348 }
349
350 Vector3 operator-(const Point3 & p) const {
351 return Vector3(x - p.x, y - p.y, z - p.z);
352 }
353
354 Point3 operator+(const Vector3 & v) const {
355 return Point3(x + v[0], y + v[1], z + v[2]);
356 }
357
358 Point3 operator-(const Vector3 & v) const {
359 return Point3(x - v[0], y - v[1], z - v[2]);
360 }
361
362 double distanceTo(const Point3& p) const {
363 return (double) sqrt((p[0] - x) * (p[0] - x) +
364 (p[1] - y) * (p[1] - y) +
365 (p[2] - z) * (p[2] - z));
366 }
367
368 double distanceToSquared(const Point3& p) const {
369 return ((p[0] - x) * (p[0] - x) +
370 (p[1] - y) * (p[1] - y) +
371 (p[2] - z) * (p[2] - z));
372 }
373
374 double distanceFromOrigin() const {
375 return (double) sqrt(x * x + y * y + z * z);
376 }
377
379 return x * x + y * y + z * z;
380 }
381
382 bool operator==( const Point3 &p ) const {
383 return x == p.x && y == p.y && z == p.z;
384 }
385
386 bool operator!=( const Point3 &p ) const {
387 return x != p.x || y != p.y || z != p.z;
388 }
389
390 bool approxEqual( const Point3 &p, double eps = 1e-12 ) const {
391 return isZero( x - p.x, eps ) && isZero( y - p.y, eps ) && isZero( z - p.z, eps );
392 }
393
394 void print() const {
395 std::cout << x << " " << y << " " << z << "\n";
396 }
397
398 private:
399 double x, y, z;
400 };
401
402 inline Point3 lerp( const Point3 &p0, const Point3 &p1, double dT )
403 {
404 const double dTMinus = 1.0 - dT;
405 return Point3( dTMinus * p0[0] + dT * p1[0], dTMinus * p0[1] + dT * p1[1], dTMinus * p0[2] + dT * p1[2] );
406 }
407
408 inline std::ostream& operator<<(std::ostream& os, const Point3& p) {
409 os << "(" << p[0] << ", " << p[1] << ", " << p[2] << ")";
410 return os;
411 }
412
413 class Matrix3 {
414 public:
416 for ( int i = 0; i < 3; i++ )
417 for ( int j = 0; j < 3; j++ )
418 mat[ index(i,j) ] = (i == j) ? 1.0 : 0.0;
419 }
420
421 Matrix3(const Vector3& row0, const Vector3& row1, const Vector3& row2) {
422 for ( int i = 0; i < 3; i++ ) {
423 mat[ index( 0, i ) ] = row0[i];
424 mat[ index( 1, i ) ] = row1[i];
425 mat[ index( 2, i ) ] = row2[i];
426 }
427 }
428
429 Matrix3(const Matrix3& m) {
430 (*this) = m;
431 }
432
434 memcpy( &mat[0], &m.mat[0], sizeof(double) * 16 );
435 return *this;
436 }
437
438 // The indexing is this way to match OpenGL
439 int index( int row, int col ) const { Assert( row >= 0 && row < 3 ); Assert( col >= 0 && col < 3 ); return col * 3 + row; }
440
441 const double & operator()( int row, int col ) const { return mat[ index(row,col) ]; }
442 double & operator()( int row, int col ) { return mat[ index(row,col) ]; }
443
444 Vector3 row(int r) const {
445 return Vector3( mat[index(r,0)], mat[index(r,1)], mat[index(r,2)] );
446 }
447
448 Vector3 column(int c) const {
449 return Vector3( mat[index(0,c)], mat[index(1,c)], mat[index(2,c)] );
450 }
451
453 Matrix3 matRet;
454 for ( int i = 0; i < 3; i++ )
455 for ( int j = 0; j < 3; j++ )
456 matRet(i,j) = (*this)(j,i);
457 return matRet;
458 }
459
460 Matrix3 operator+( const Matrix3 &m) const {
461 Matrix3 matRet;
462 for ( int i = 0; i < 9; i++ )
463 matRet.mat[i] = mat[i] + m.mat[i];
464 return matRet;
465 }
466
467 Matrix3& operator*=(double s) {
468 for ( int i = 0; i < 9; i++ )
469 mat[i] *= s;
470 return *this;
471 }
472
473 // pre-multiply column vector by a 3x3 matrix
474 Vector3 operator*(const Vector3& v) const {
475 return Vector3((*this)(0,0) * v[0] + (*this)(0,1) * v[1] + (*this)(0,2) * v[2],
476 (*this)(1,0) * v[0] + (*this)(1,1) * v[1] + (*this)(1,2) * v[2],
477 (*this)(2,0) * v[0] + (*this)(2,1) * v[1] + (*this)(2,2) * v[2]);
478 }
479
480 // pre-multiply column point by a 3x3 matrix
481 Point3 operator*(const Point3& p) const {
482 return Point3((*this)(0,0) * p[0] + (*this)(0,1) * p[1] + (*this)(0,2) * p[2],
483 (*this)(1,0) * p[0] + (*this)(1,1) * p[1] + (*this)(1,2) * p[2],
484 (*this)(2,0) * p[0] + (*this)(2,1) * p[1] + (*this)(2,2) * p[2]);
485 }
486
487 Matrix3 operator*( const Matrix3 & m ) const {
488 Matrix3 matRet;
489 for ( int i = 0; i < 3; i++ ) {
490 for ( int j = 0; j < 3; j++ ) {
491 matRet(i,j) = 0.0;
492 for ( int k = 0; k < 3; k++ )
493 matRet(i,j) += (*this)(i,k) * m(k,j);
494 }
495 }
496 return matRet;
497 }
498
499 static Matrix3 identity() {
500 return Matrix3(Vector3(1, 0, 0),
501 Vector3(0, 1, 0),
502 Vector3(0, 0, 1));
503 }
504
506 return Matrix3(Vector3(u[0], v[0], w[0]),
507 Vector3(u[1], v[1], w[1]),
508 Vector3(u[2], v[2], w[2]));
509 }
510
511 static double det2x2(double a, double b, double c, double d) {
512 return a * d - b * c;
513 }
514
515 double determinant() const {
516 return ((*this)(0,0) * (*this)(1,1) * (*this)(2,2) +
517 (*this)(0,1) * (*this)(1,2) * (*this)(2,0) +
518 (*this)(0,2) * (*this)(1,0) * (*this)(2,1) -
519 (*this)(0,2) * (*this)(1,1) * (*this)(2,0) -
520 (*this)(0,0) * (*this)(1,2) * (*this)(2,1) -
521 (*this)(0,1) * (*this)(1,0) * (*this)(2,2));
522 }
523
524 Matrix3 inverse() const {
525 Matrix3 adjoint = Matrix3( Vector3( det2x2((*this)(1,1), (*this)(1,2), (*this)(2,1), (*this)(2,2)),
526 -det2x2((*this)(1,0), (*this)(1,2), (*this)(2,0), (*this)(2,2)),
527 det2x2((*this)(1,0), (*this)(1,1), (*this)(2,0), (*this)(2,1)) ),
528 Vector3( -det2x2((*this)(0,1), (*this)(0,2), (*this)(2,1), (*this)(2,2)),
529 det2x2((*this)(0,0), (*this)(0,2), (*this)(2,0), (*this)(2,2)),
530 -det2x2((*this)(0,0), (*this)(0,1), (*this)(2,0), (*this)(2,1)) ),
531 Vector3( det2x2((*this)(0,1), (*this)(0,2), (*this)(1,1), (*this)(1,2)),
532 -det2x2((*this)(0,0), (*this)(0,2), (*this)(1,0), (*this)(1,2)),
533 det2x2((*this)(0,0), (*this)(0,1), (*this)(1,0), (*this)(1,1)) ) );
534 const double dDet = determinant();
535
536 Assert( isZero( dDet ) == false );
537 adjoint *= 1.0 / dDet;
538
539 return adjoint;
540 }
541
542 bool operator==( const Matrix3 &m ) const {
543 for ( int i = 0; i < 9; i++ )
544 if ( mat[i] != m.mat[i] )
545 return false;
546 return true;
547 }
548
549 bool approxEqual( const Matrix3 &m, double eps = 1e-12 ) const {
550 for ( int i = 0; i < 9; i++ )
551 if ( isZero( mat[i] - m.mat[i], eps ) )
552 return false;
553 return true;
554 }
555
556 void print() const {
557 std::cout << "( " << (*this)(0,0) << ", " << (*this)(0,1) << ", " << (*this)(0,2) << "\n";
558 std::cout << " " << (*this)(1,0) << ", " << (*this)(1,1) << ", " << (*this)(1,2) << "\n";
559 std::cout << " " << (*this)(2,0) << ", " << (*this)(2,1) << ", " << (*this)(2,2) << ")\n";
560 }
561
562 private:
563 double mat[9];
564 };
565
566 // post-multiply row vector by a 3x3 matrix
567 inline Vector3 operator*(const Vector3& v, const Matrix3& m) {
568 return Vector3(m(0,0) * v[0] + m(1,0) * v[1] + m(2,0) * v[2],
569 m(0,1) * v[0] + m(1,1) * v[1] + m(2,1) * v[2],
570 m(0,2) * v[0] + m(1,2) * v[1] + m(2,2) * v[2]);
571 }
572
573 // post-multiply row point by a 3x3 matrix
574 inline Point3 operator*(const Point3& p, const Matrix3& m) {
575 return Point3(m(0,0) * p[0] + m(1,0) * p[1] + m(2,0) * p[2],
576 m(0,1) * p[0] + m(1,1) * p[1] + m(2,1) * p[2],
577 m(0,2) * p[0] + m(1,2) * p[1] + m(2,2) * p[2]);
578 }
579
580 inline std::ostream& operator<<(std::ostream& os, const Matrix3& m) {
581 os << m.row(0) << std::endl;
582 os << m.row(1) << std::endl;
583 os << m.row(2) << std::endl;
584 return os;
585 }
586
587
588 class Vector4 {
589 public:
590 Vector4() : x(0), y(0), z(0), w(0) {}
591 Vector4(const Vector4& v) : x(v[0]), y(v[1]), z(v[2]), w(v[3]) {}
592 Vector4(double _x, double _y, double _z, double _w) : x(_x), y(_y), z(_z), w(_w) {}
593
595 x = a[0]; y = a[1]; z = a[2]; w = a[3];
596 return *this;
597 }
598
599 const double &operator[](int n) const { return ((double *) this)[n]; }
600 double &operator[](int n) { return ((double *) this)[n]; }
601
603 x += a[0]; y += a[1]; z += a[2]; w += a[3];
604 return *this;
605 }
606
608 x -= a[0]; y -= a[1]; z -= a[2]; w -= a[3];
609 return *this;
610 }
611
612 Vector4& operator*=(double s) {
613 x *= s; y *= s; z *= s; w *= s;
614 return *this;
615 }
616
618 return Vector4(-x, -y, -z, -w);
619 }
620
622 return *this;
623 }
624
625 Vector4 operator+( const Vector4 &v ) const {
626 return Vector4( x + v.x, y + v.y, z + v.z, w + v.w );
627 }
628
629 Vector4 operator-( const Vector4 &v ) const {
630 return Vector4( x - v.x, y - v.y, z - v.z, w - v.w );
631 }
632
633 Vector4 operator/( const double s ) const {
634 Assert( s > 0.0 );
635 return Vector4( x / s, y / s, z / s, w / s );
636 }
637
638 Vector4 operator*( const double s ) const {
639 return Vector4( x * s, y * s, z * s, w * s );
640 }
641
642 // Dot
643 double operator*( const Vector4 &v ) const {
644 return x * v.x + y * v.y + z * v.z + w * v.w;
645 }
646
647 double length() const {
648 return (double) sqrt(x * x + y * y + z * z + w * w);
649 }
650
651 double lengthSquared() const {
652 return x * x + y * y + z * z + w * w;
653 }
654
655 void normalize() {
656 double s = 1.0 / length();
657 x *= s; y *= s; z *= s; w *= s;
658 }
659
660 bool operator==( const Vector4 &v ) const {
661 return x == v.x && y == v.y && z == v.z && w == v.w;
662 }
663
664 bool operator!=( const Vector4 &v ) const {
665 return x != v.x || y != v.y || z != v.z || w != v.w;
666 }
667
668 bool approxEqual( const Vector4 &v, double eps = 1e-12 ) const {
669 return isZero( x - v.x, eps ) && isZero( y - v.y, eps ) && isZero( z - v.z, eps ) && isZero( w - v.w, eps );
670 }
671
672 void print() const {
673 std::cout << x << " " << y << " " << z << " " << w << "\n";
674 }
675
676 private:
677 double x, y, z, w;
678 };
679
680 inline Vector4 operator*( const double s, const Vector4 &v ) {
681 return Vector4( v[0] * s, v[1] * s, v[2] * s, v[3] * s );
682 }
683
684 inline double length( const Vector4 &v ) { return v.length(); }
685 inline Vector4 unit( const Vector4 &v ) { const double len = v.length(); return v / len; }
686 inline std::ostream& operator<<(std::ostream& os, const Vector4& v) {
687 os << "(" << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ")";
688 return os;
689 }
690
691 class Matrix4 {
692 public:
694 for ( int i = 0; i < 4; i++ )
695 for ( int j = 0; j < 4; j++ )
696 mat[ index(i,j) ] = (i == j) ? 1.0 : 0.0;
697 }
698
699 Matrix4(const Vector4& row0, const Vector4& row1, const Vector4& row2, const Vector4& row3) {
700 for ( int i = 0; i < 4; i++ ) {
701 mat[ index( 0, i ) ] = row0[i];
702 mat[ index( 1, i ) ] = row1[i];
703 mat[ index( 2, i ) ] = row2[i];
704 mat[ index( 3, i ) ] = row3[i];
705 }
706 }
707
708 Matrix4(const Vector3& row0, const Vector3& row1, const Vector3& row2 ) {
709 for ( int i = 0; i < 3; i++ ) {
710 mat[ index( 0, i ) ] = row0[i];
711 mat[ index( 1, i ) ] = row1[i];
712 mat[ index( 2, i ) ] = row2[i];
713 mat[ index(i,3) ] = 0.0;
714 mat[ index(3,i) ] = 0.0;
715 }
716 mat[ index(3,3) ] = 1.0;
717 }
718
719 Matrix4(const Matrix4& m) {
720 (*this) = m;
721 }
722
724 memcpy( &mat[0], &m.mat[0], sizeof(double) * 16 );
725 return *this;
726 }
727
728 // The indexing is this way to match OpenGL
729 int index( int row, int col ) const { Assert( row >= 0 && row < 4 ); Assert( col >= 0 && col < 4 ); return col * 4 + row; }
730
731 const double & operator()( int row, int col ) const { return mat[ index(row,col) ]; }
732 double & operator()( int row, int col ) { return mat[ index(row,col) ]; }
733
734 Vector4 row(int r) const {
735 return Vector4( mat[index(r,0)], mat[index(r,1)], mat[index(r,2)], mat[index(r,3)] );
736 }
737
738 Vector4 column(int c) const {
739 return Vector4( mat[index(0,c)], mat[index(1,c)], mat[index(2,c)], mat[index(3,c)] );
740 }
741
743 const Matrix4 matRet = (*this) * m;
744 (*this) = matRet;
745 return *this;
746 }
747
749 const Matrix4 matRet = (*this) + m;
750 (*this) = matRet;
751 return *this;
752 }
753
755 const Matrix4 matRet = (*this) - m;
756 (*this) = matRet;
757 return *this;
758 }
759
761 Matrix4 matRet;
762 for ( int i = 0; i < 4; i++ )
763 for ( int j = 0; j < 4; j++ )
764 matRet(i,j) = (*this)(j,i);
765 return matRet;
766 }
767
768 Matrix4 operator+( const Matrix4 &m) const {
769 Matrix4 matRet;
770 for ( int i = 0; i < 16; i++ )
771 matRet.mat[i] = mat[i] + m.mat[i];
772 return matRet;
773 }
774
775 Matrix4 operator-( const Matrix4 &m) const {
776 Matrix4 matRet;
777 for ( int i = 0; i < 16; i++ )
778 matRet.mat[i] = mat[i] - m.mat[i];
779 return matRet;
780 }
781
782 Vector3 operator*(const Vector3& v) const {
783 return Vector3((*this)(0,0) * v[0] + (*this)(0,1) * v[1] + (*this)(0,2) * v[2],
784 (*this)(1,0) * v[0] + (*this)(1,1) * v[1] + (*this)(1,2) * v[2],
785 (*this)(2,0) * v[0] + (*this)(2,1) * v[1] + (*this)(2,2) * v[2]);
786 }
787
788 Point3 operator*(const Point3& p) const {
789 const Point3 pt((*this)(0,0) * p[0] + (*this)(0,1) * p[1] + (*this)(0,2) * p[2] + (*this)(0,3),
790 (*this)(1,0) * p[0] + (*this)(1,1) * p[1] + (*this)(1,2) * p[2] + (*this)(1,3),
791 (*this)(2,0) * p[0] + (*this)(2,1) * p[1] + (*this)(2,2) * p[2] + (*this)(2,3));
792 const double w = (*this)(3,0) * p[0] + (*this)(3,1) * p[1] + (*this)(3,2) * p[2] + (*this)(3,3);
793 Assert( isZero( w ) == false );
794 return Point3( pt[0] / w, pt[1] / w, pt[2] / w );
795 }
796
797 Vector4 operator*(const Vector4& v) const {
798 return Vector4((*this)(0,0) * v[0] + (*this)(0,1) * v[1] + (*this)(0,2) * v[2] + (*this)(0,3) * v[3],
799 (*this)(1,0) * v[0] + (*this)(1,1) * v[1] + (*this)(1,2) * v[2] + (*this)(1,3) * v[3],
800 (*this)(2,0) * v[0] + (*this)(2,1) * v[1] + (*this)(2,2) * v[2] + (*this)(2,3) * v[3],
801 (*this)(3,0) * v[0] + (*this)(3,1) * v[1] + (*this)(3,2) * v[2] + (*this)(3,3) * v[3]);
802 }
803
804 inline Matrix4 operator*(const Matrix4& b) const{
805 Matrix4 matRet;
806 for ( int i = 0; i < 4; i++ ) {
807 for ( int j = 0; j < 4; j++ ) {
808 matRet(i,j) = 0.0;
809 for ( int k = 0; k < 4; k++ )
810 matRet(i,j) += (*this)(i,k) * b(k,j);
811 }
812 }
813 return matRet;
814 }
815
816 Matrix4 inverse() const;
817
818 bool operator==( const Matrix4 &m ) const {
819 for ( int i = 0; i < 16; i++ )
820 if ( mat[i] != m.mat[i] )
821 return false;
822 return true;
823 }
824
825 bool approxEqual( const Matrix4 &m, double eps = 1e-12 ) const {
826 for ( int i = 0; i < 16; i++ )
827 if ( isZero( mat[i] - m.mat[i], eps ) )
828 return false;
829 return true;
830 }
831
832 void print() const {
833 std::cout << "( " << (*this)(0,0) << ", " << (*this)(0,1) << ", " << (*this)(0,2) << ", " << (*this)(0,3) << "\n";
834 std::cout << " " << (*this)(1,0) << ", " << (*this)(1,1) << ", " << (*this)(1,2) << ", " << (*this)(1,3) << "\n";
835 std::cout << " " << (*this)(2,0) << ", " << (*this)(2,1) << ", " << (*this)(2,2) << ", " << (*this)(2,3) << "\n";
836 std::cout << " " << (*this)(3,0) << ", " << (*this)(3,1) << ", " << (*this)(3,2) << ", " << (*this)(3,3) << ")\n";
837 }
838
839 static Matrix4 identity() {
840 return Matrix4(Vector4(1, 0, 0, 0),
841 Vector4(0, 1, 0, 0),
842 Vector4(0, 0, 1, 0),
843 Vector4(0, 0, 0, 1));
844 }
845
846 static Matrix4 translation(const Point3& p) {
847 return Matrix4(Vector4(1, 0, 0, p[0]),
848 Vector4(0, 1, 0, p[1]),
849 Vector4(0, 0, 1, p[2]),
850 Vector4(0, 0, 0, 1));
851 }
852
853 static Matrix4 translation(const Vector3& v) {
854 return Matrix4(Vector4(1, 0, 0, v[0]),
855 Vector4(0, 1, 0, v[1]),
856 Vector4(0, 0, 1, v[2]),
857 Vector4(0, 0, 0, 1));
858 }
859
860 static Matrix4 rotation(const Vector3& u, const Vector3& v, const Vector3& w) {
861 return Matrix4(Vector4(u[0], u[1], u[2], 0),
862 Vector4(v[0], v[1], v[2], 0),
863 Vector4(w[0], w[1], w[2], 0),
864 Vector4(0 , 0 , 0 , 1));
865 }
866
867 static Matrix4 rotation(const Vector3& axis, double angle) {
868 Vector3 a = axis;
869 a.normalize();
870 const double c = cos(angle);
871 const double s = sin(angle);
872 const double t = 1 - c;
873
874 return Matrix4(Vector4(t * a[0] * a[0] + c,
875 t * a[0] * a[1] - s * a[2],
876 t * a[0] * a[2] + s * a[1],
877 0),
878 Vector4(t * a[0] * a[1] + s * a[2],
879 t * a[1] * a[1] + c,
880 t * a[1] * a[2] - s * a[0],
881 0),
882 Vector4(t * a[0] * a[2] - s * a[1],
883 t * a[1] * a[2] + s * a[0],
884 t * a[2] * a[2] + c,
885 0),
886 Vector4(0, 0, 0, 1));
887 }
888
889 static Matrix4 xrotation(double angle) {
890 const double c = cos(angle);
891 const double s = sin(angle);
892
893 return Matrix4(Vector4(1, 0, 0, 0),
894 Vector4(0, c, -s, 0),
895 Vector4(0, s, c, 0),
896 Vector4(0, 0, 0, 1));
897 }
898
899 static Matrix4 yrotation(double angle) {
900 double c = cos(angle);
901 double s = sin(angle);
902
903 return Matrix4(Vector4( c, 0, s, 0),
904 Vector4( 0, 1, 0, 0),
905 Vector4(-s, 0, c, 0),
906 Vector4( 0, 0, 0, 1));
907 }
908
909 static Matrix4 zrotation(double angle) {
910 const double c = cos(angle);
911 const double s = sin(angle);
912
913 return Matrix4(Vector4(c, -s, 0, 0),
914 Vector4(s, c, 0, 0),
915 Vector4(0, 0, 1, 0),
916 Vector4(0, 0, 0, 1));
917 }
918
919 static Matrix4 scaling(const Vector3& s) {
920 return Matrix4(Vector4(s[0], 0 , 0 , 0),
921 Vector4(0 , s[1], 0 , 0),
922 Vector4(0 , 0 , s[2], 0),
923 Vector4(0 , 0 , 0 , 1));
924 }
925
926 static Matrix4 scaling( double x, double y, double z) {
927 return Matrix4(Vector4(x, 0, 0, 0),
928 Vector4(0, y, 0, 0),
929 Vector4(0, 0, z, 0),
930 Vector4(0, 0, 0, 1));
931 }
932
933 static Matrix4 scaling(double scale) {
934 return scaling(Vector3(scale, scale, scale));
935 }
936
937 private:
938 double mat[16];
939 };
940
941
942
943
944
945
946 // **** Matrix4 operators ****
947
948 inline std::ostream& operator<<(std::ostream& os, const Matrix4& m) {
949 os << m.row(0) << std::endl;
950 os << m.row(1) << std::endl;
951 os << m.row(2) << std::endl;
952 os << m.row(3) << std::endl;
953 return os;
954 }
955
956 inline Matrix4 Matrix4::inverse() const {
957 // Compute inverse using Gauss-Jordan elimination; caller is responsible
958 // for ensuring that the matrix isn't singular.
959 Matrix4 a(*this);
961 int i, j;
962 int p;
963
964 for (j = 0; j < 4; j++) {
965 p = j;
966 for (i = j + 1; i < 4; i++) {
967 if (fabs(a(i,j)) > fabs(a(p,j)))
968 p = i;
969 }
970 // Swap rows p and j
971 if ( p != j ) {
972 for ( i = 0; i < 4; i++ ) {
973 const double ta = a(p,i);
974 a(p,i) = a(j,i);
975 a(j,i) = ta;
976
977 const double tb = b(p,i);
978 b(p,i) = b(j,i);
979 b(j,i) = tb;
980 }
981 }
982
983 const double s = a(j,j); // if s == 0, the matrix is singular
984 Assert( isZero( s ) == false );
985 for ( i = 0; i < 4; i++ ) {
986 a(j,i) *= (1.0 / s);
987 b(j,i) *= (1.0 / s);
988 }
989 // Eliminate off-diagonal elements
990 for (i = 0; i < 4; i++) {
991 if (i != j) {
992 for ( int k = 0; k < 4; k++ ) {
993 b(i,k) -= a(i,j) * b(j,k);
994 a(i,k) -= a(i,j) * a(j,k);
995 }
996 }
997 }
998 }
999 return b;
1000 }
1001
1002}
1003
1004#endif /* _VECMATH_H_ */
Vector3 operator*(const Vector3 &v) const
Definition: vecmath.h:474
int index(int row, int col) const
Definition: vecmath.h:439
static double det2x2(double a, double b, double c, double d)
Definition: vecmath.h:511
Matrix3 transpose() const
Definition: vecmath.h:452
Point3 operator*(const Point3 &p) const
Definition: vecmath.h:481
double mat[9]
Definition: vecmath.h:563
Matrix3 operator+(const Matrix3 &m) const
Definition: vecmath.h:460
const double & operator()(int row, int col) const
Definition: vecmath.h:441
Matrix3 & operator*=(double s)
Definition: vecmath.h:467
Vector3 row(int r) const
Definition: vecmath.h:444
void print() const
Definition: vecmath.h:556
Matrix3(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2)
Definition: vecmath.h:421
bool approxEqual(const Matrix3 &m, double eps=1e-12) const
Definition: vecmath.h:549
Matrix3 & operator=(const Matrix3 &m)
Definition: vecmath.h:433
static Matrix3 rotationXYZtoUVW(Vector3 u, Vector3 v, Vector3 w)
Definition: vecmath.h:505
static Matrix3 identity()
Definition: vecmath.h:499
double determinant() const
Definition: vecmath.h:515
double & operator()(int row, int col)
Definition: vecmath.h:442
Matrix3(const Matrix3 &m)
Definition: vecmath.h:429
Vector3 column(int c) const
Definition: vecmath.h:448
Matrix3 inverse() const
Definition: vecmath.h:524
bool operator==(const Matrix3 &m) const
Definition: vecmath.h:542
Matrix3 operator*(const Matrix3 &m) const
Definition: vecmath.h:487
Matrix4(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2)
Definition: vecmath.h:708
static Matrix4 rotation(const Vector3 &u, const Vector3 &v, const Vector3 &w)
Definition: vecmath.h:860
int index(int row, int col) const
Definition: vecmath.h:729
static Matrix4 translation(const Point3 &p)
Definition: vecmath.h:846
void print() const
Definition: vecmath.h:832
double mat[16]
Definition: vecmath.h:938
Point3 operator*(const Point3 &p) const
Definition: vecmath.h:788
static Matrix4 rotation(const Vector3 &axis, double angle)
Definition: vecmath.h:867
Matrix4 operator-(const Matrix4 &m) const
Definition: vecmath.h:775
static Matrix4 yrotation(double angle)
Definition: vecmath.h:899
Matrix4 operator*(const Matrix4 &b) const
Definition: vecmath.h:804
Matrix4 & operator*=(const Matrix4 &m)
Definition: vecmath.h:742
Matrix4 transpose() const
Definition: vecmath.h:760
static Matrix4 scaling(double scale)
Definition: vecmath.h:933
static Matrix4 xrotation(double angle)
Definition: vecmath.h:889
bool approxEqual(const Matrix4 &m, double eps=1e-12) const
Definition: vecmath.h:825
Matrix4 & operator-=(const Matrix4 &m)
Definition: vecmath.h:754
Vector4 column(int c) const
Definition: vecmath.h:738
Matrix4 operator+(const Matrix4 &m) const
Definition: vecmath.h:768
static Matrix4 zrotation(double angle)
Definition: vecmath.h:909
Matrix4(const Matrix4 &m)
Definition: vecmath.h:719
Vector3 operator*(const Vector3 &v) const
Definition: vecmath.h:782
Vector4 operator*(const Vector4 &v) const
Definition: vecmath.h:797
Matrix4(const Vector4 &row0, const Vector4 &row1, const Vector4 &row2, const Vector4 &row3)
Definition: vecmath.h:699
Matrix4 inverse() const
Definition: vecmath.h:956
static Matrix4 identity()
Definition: vecmath.h:839
Matrix4 & operator+=(const Matrix4 &m)
Definition: vecmath.h:748
static Matrix4 scaling(const Vector3 &s)
Definition: vecmath.h:919
bool operator==(const Matrix4 &m) const
Definition: vecmath.h:818
const double & operator()(int row, int col) const
Definition: vecmath.h:731
Vector4 row(int r) const
Definition: vecmath.h:734
static Matrix4 scaling(double x, double y, double z)
Definition: vecmath.h:926
static Matrix4 translation(const Vector3 &v)
Definition: vecmath.h:853
double & operator()(int row, int col)
Definition: vecmath.h:732
Matrix4 & operator=(const Matrix4 &m)
Definition: vecmath.h:723
Point3 & operator-=(const Vector3 &v)
Definition: vecmath.h:340
bool approxEqual(const Point3 &p, double eps=1e-12) const
Definition: vecmath.h:390
const double & operator[](int n) const
Definition: vecmath.h:332
double distanceToSquared(const Point3 &p) const
Definition: vecmath.h:368
Point3(double _x, double _y, double _z)
Definition: vecmath.h:325
double distanceFromOrigin() const
Definition: vecmath.h:374
Point3 & operator+=(const Vector3 &v)
Definition: vecmath.h:335
bool operator!=(const Point3 &p) const
Definition: vecmath.h:386
Point3 operator+(const Vector3 &v) const
Definition: vecmath.h:354
Point3 & operator*=(double s)
Definition: vecmath.h:345
double distanceFromOriginSquared() const
Definition: vecmath.h:378
Point3(const Point3 &p)
Definition: vecmath.h:324
double y
Definition: vecmath.h:399
double & operator[](int n)
Definition: vecmath.h:333
bool operator==(const Point3 &p) const
Definition: vecmath.h:382
Vector3 operator-(const Point3 &p) const
Definition: vecmath.h:350
void print() const
Definition: vecmath.h:394
Point3 & operator=(const Point3 &a)
Definition: vecmath.h:327
double x
Definition: vecmath.h:399
double distanceTo(const Point3 &p) const
Definition: vecmath.h:362
double z
Definition: vecmath.h:399
Point3 operator-(const Vector3 &v) const
Definition: vecmath.h:358
int & operator[](int n)
Definition: vecmath.h:151
ScreenPoint & operator*=(int s)
Definition: vecmath.h:163
void print() const
Definition: vecmath.h:188
const int & operator[](int n) const
Definition: vecmath.h:150
bool operator==(const ScreenPoint &p) const
Definition: vecmath.h:180
bool operator!=(const ScreenPoint &p) const
Definition: vecmath.h:184
ScreenPoint & operator-=(const ScreenVector &v)
Definition: vecmath.h:158
ScreenPoint & operator+=(const ScreenVector &v)
Definition: vecmath.h:153
ScreenPoint operator+(const ScreenVector &v) const
Definition: vecmath.h:168
ScreenPoint operator-(const ScreenVector &v) const
Definition: vecmath.h:176
ScreenPoint & operator=(const ScreenPoint &a)
Definition: vecmath.h:145
ScreenVector operator-(const ScreenPoint &p) const
Definition: vecmath.h:172
ScreenPoint(int _x, int _y)
Definition: vecmath.h:143
ScreenPoint(const ScreenPoint &p)
Definition: vecmath.h:142
int operator*(const ScreenVector &v) const
Definition: vecmath.h:101
bool operator!=(const ScreenVector &v) const
Definition: vecmath.h:117
ScreenVector operator-(const ScreenVector &v) const
Definition: vecmath.h:92
ScreenVector operator*(const double s) const
Definition: vecmath.h:96
ScreenVector operator+() const
Definition: vecmath.h:84
ScreenVector(const ScreenVector &v)
Definition: vecmath.h:54
const int & operator[](int n) const
Definition: vecmath.h:62
ScreenVector & operator*=(int s)
Definition: vecmath.h:75
bool operator==(const ScreenVector &v) const
Definition: vecmath.h:113
void print() const
Definition: vecmath.h:121
ScreenVector & operator-=(const ScreenVector &a)
Definition: vecmath.h:70
ScreenVector operator-() const
Definition: vecmath.h:80
ScreenVector & operator=(const ScreenVector &a)
Definition: vecmath.h:57
int lengthSquared() const
Definition: vecmath.h:109
int & operator[](int n)
Definition: vecmath.h:63
ScreenVector & operator+=(const ScreenVector &a)
Definition: vecmath.h:65
ScreenVector(int _x, int _y)
Definition: vecmath.h:55
double length() const
Definition: vecmath.h:105
ScreenVector operator+(const ScreenVector &v) const
Definition: vecmath.h:88
double & operator[](int n)
Definition: vecmath.h:214
Vector3 operator+(const Vector3 &v) const
Definition: vecmath.h:239
Vector3(double _x, double _y, double _z)
Definition: vecmath.h:206
Vector3 operator*(const double s) const
Definition: vecmath.h:252
Vector3 & operator+=(const Vector3 &a)
Definition: vecmath.h:216
double operator*(const Vector3 &v) const
Definition: vecmath.h:257
bool approxEqual(const Vector3 &v, double eps=1e-12) const
Definition: vecmath.h:289
Vector3 & operator=(const Vector3 &a)
Definition: vecmath.h:208
double x
Definition: vecmath.h:298
Vector3 operator-(const Vector3 &v) const
Definition: vecmath.h:243
Vector3(const Vector3 &v)
Definition: vecmath.h:205
bool operator==(const Vector3 &v) const
Definition: vecmath.h:281
Vector3 & operator*=(double s)
Definition: vecmath.h:226
bool operator!=(const Vector3 &v) const
Definition: vecmath.h:285
void normalize()
Definition: vecmath.h:276
Vector3 & operator-=(const Vector3 &a)
Definition: vecmath.h:221
Vector3 operator-() const
Definition: vecmath.h:231
void print() const
Definition: vecmath.h:293
double lengthSquared() const
Definition: vecmath.h:272
double z
Definition: vecmath.h:298
double length() const
Definition: vecmath.h:268
const double & operator[](int n) const
Definition: vecmath.h:213
Vector3 operator+() const
Definition: vecmath.h:235
Vector3 operator/(const double s) const
Definition: vecmath.h:247
double y
Definition: vecmath.h:298
Vector3 operator^(const Vector3 &v) const
Definition: vecmath.h:262
void normalize()
Definition: vecmath.h:655
Vector4 & operator+=(const Vector4 &a)
Definition: vecmath.h:602
bool approxEqual(const Vector4 &v, double eps=1e-12) const
Definition: vecmath.h:668
Vector4(const Vector4 &v)
Definition: vecmath.h:591
Vector4 operator-()
Definition: vecmath.h:617
double w
Definition: vecmath.h:677
Vector4 operator+()
Definition: vecmath.h:621
Vector4 & operator=(const Vector4 &a)
Definition: vecmath.h:594
double operator*(const Vector4 &v) const
Definition: vecmath.h:643
Vector4 operator*(const double s) const
Definition: vecmath.h:638
double z
Definition: vecmath.h:677
bool operator!=(const Vector4 &v) const
Definition: vecmath.h:664
Vector4(double _x, double _y, double _z, double _w)
Definition: vecmath.h:592
bool operator==(const Vector4 &v) const
Definition: vecmath.h:660
double length() const
Definition: vecmath.h:647
double lengthSquared() const
Definition: vecmath.h:651
Vector4 operator/(const double s) const
Definition: vecmath.h:633
double y
Definition: vecmath.h:677
double x
Definition: vecmath.h:677
const double & operator[](int n) const
Definition: vecmath.h:599
Vector4 & operator*=(double s)
Definition: vecmath.h:612
void print() const
Definition: vecmath.h:672
Vector4 & operator-=(const Vector4 &a)
Definition: vecmath.h:607
Vector4 operator+(const Vector4 &v) const
Definition: vecmath.h:625
double & operator[](int n)
Definition: vecmath.h:600
Vector4 operator-(const Vector4 &v) const
Definition: vecmath.h:629
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
Definition: emassert.h:42
EMData * sqrt() const
return square root of current image
E2Exception class.
Definition: aligner.h:40
Vector3 cross(const Vector3 &w, const Vector3 &v)
Definition: vecmath.h:309
Vector3 unit(const Vector3 &v)
Definition: vecmath.h:314
double dot(const Vector3 &w, const Vector3 &v)
Definition: vecmath.h:305
double length(const Vector3 &v)
Definition: vecmath.h:313
bool isZero(double in_d, double in_dEps=1e-16)
Definition: vecmath.h:44
EMData * operator*(const EMData &em, float n)
Definition: emdata.cpp:3201
Point3 lerp(const Point3 &p0, const Point3 &p1, double dT)
Definition: vecmath.h:402
std::ostream & operator<<(std::ostream &os, const ScreenVector &v)
Definition: vecmath.h:133
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517