44 inline bool isZero(
double in_d,
double in_dEps = 1e-16 )
46 return (in_d < in_dEps && in_d > -in_dEps)? true :
false;
102 return x * v.
x +
y * v.
y;
106 return (
double)
sqrt( (
double) (
x *
x +
y *
y) );
110 return x *
x +
y *
y;
114 return x == v.
x &&
y == v.
y;
118 return x != v.
x ||
y != v.
y;
122 std::cout <<
"(" <<
x <<
", " <<
y <<
")";
130 return ScreenVector( (
int)(v[0] * s), (
int)(v[1] * s) );
134 os <<
"(" << v[0] <<
", " << v[1] <<
")";
154 x += v[0];
y += v[1];
159 x -= v[0];
y -= v[1];
181 return x == p.
x &&
y == p.
y;
185 return x != p.
x ||
y != p.
y;
189 std::cout <<
"(" <<
x <<
", " <<
y <<
")";
197 os <<
"(" << p[0] <<
", " << p[1] <<
")";
206 Vector3(
double _x,
double _y,
double _z) :
x(_x),
y(_y),
z(_z) {}
209 x = a[0];
y = a[1];
z = a[2];
217 x += a[0];
y += a[1];
z += a[2];
222 x -= a[0];
y -= a[1];
z -= a[2];
227 x *= s;
y *= s;
z *= s;
258 return x * v.
x +
y * v.
y +
z * v.
z;
273 return x *
x +
y *
y +
z *
z;
277 double s = 1.0 / (double)
sqrt(
x *
x +
y *
y +
z *
z);
278 x *= s;
y *= s;
z *= s;
282 return x == v.
x &&
y == v.
y &&
z == v.
z;
286 return x != v.
x ||
y != v.
y ||
z != v.
z;
294 std::cout <<
x <<
" " <<
y <<
" " <<
z <<
"\n";
302 return Vector3( v[0] * s, v[1] * s, v[2] * s );
317 os <<
"(" << v[0] <<
", " << v[1] <<
", " << v[2] <<
")";
325 Point3(
double _x,
double _y,
double _z) :
x(_x),
y(_y),
z(_z) {}
328 x = a[0];
y = a[1];
z = a[2];
336 x += v[0];
y += v[1];
z += v[2];
341 x -= v[0];
y -= v[1];
z -= v[2];
346 x *= s;
y *= s;
z *= s;
355 return Point3(
x + v[0],
y + v[1],
z + v[2]);
359 return Point3(
x - v[0],
y - v[1],
z - v[2]);
363 return (
double)
sqrt((p[0] -
x) * (p[0] -
x) +
364 (p[1] -
y) * (p[1] -
y) +
365 (p[2] -
z) * (p[2] -
z));
369 return ((p[0] -
x) * (p[0] -
x) +
370 (p[1] -
y) * (p[1] -
y) +
371 (p[2] -
z) * (p[2] -
z));
379 return x *
x +
y *
y +
z *
z;
383 return x == p.
x &&
y == p.
y &&
z == p.
z;
387 return x != p.
x ||
y != p.
y ||
z != p.
z;
395 std::cout <<
x <<
" " <<
y <<
" " <<
z <<
"\n";
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] );
409 os <<
"(" << p[0] <<
", " << p[1] <<
", " << p[2] <<
")";
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;
422 for (
int i = 0; i < 3; i++ ) {
434 memcpy( &
mat[0], &m.
mat[0],
sizeof(
double) * 16 );
454 for (
int i = 0; i < 3; i++ )
455 for (
int j = 0; j < 3; j++ )
456 matRet(i,j) = (*this)(j,i);
462 for (
int i = 0; i < 9; i++ )
468 for (
int i = 0; i < 9; i++ )
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]);
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]);
489 for (
int i = 0; i < 3; i++ ) {
490 for (
int j = 0; j < 3; j++ ) {
492 for (
int k = 0; k < 3; k++ )
493 matRet(i,j) += (*this)(i,k) * m(k,j);
511 static double det2x2(
double a,
double b,
double c,
double d) {
512 return a * d - b * c;
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));
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)) ) );
537 adjoint *= 1.0 / dDet;
543 for (
int i = 0; i < 9; i++ )
550 for (
int i = 0; i < 9; i++ )
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";
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]);
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]);
581 os << m.
row(0) << std::endl;
582 os << m.
row(1) << std::endl;
583 os << m.
row(2) << std::endl;
592 Vector4(
double _x,
double _y,
double _z,
double _w) :
x(_x),
y(_y),
z(_z),
w(_w) {}
595 x = a[0];
y = a[1];
z = a[2];
w = a[3];
599 const double &
operator[](
int n)
const {
return ((
double *)
this)[n]; }
603 x += a[0];
y += a[1];
z += a[2];
w += a[3];
608 x -= a[0];
y -= a[1];
z -= a[2];
w -= a[3];
613 x *= s;
y *= s;
z *= s;
w *= s;
644 return x * v.
x +
y * v.
y +
z * v.
z +
w * v.
w;
652 return x *
x +
y *
y +
z *
z +
w *
w;
656 double s = 1.0 /
length();
657 x *= s;
y *= s;
z *= s;
w *= s;
661 return x == v.
x &&
y == v.
y &&
z == v.
z &&
w == v.
w;
665 return x != v.
x ||
y != v.
y ||
z != v.
z ||
w != v.
w;
673 std::cout <<
x <<
" " <<
y <<
" " <<
z <<
" " <<
w <<
"\n";
681 return Vector4( v[0] * s, v[1] * s, v[2] * s, v[3] * s );
687 os <<
"(" << v[0] <<
", " << v[1] <<
", " << v[2] <<
", " << v[3] <<
")";
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;
700 for (
int i = 0; i < 4; i++ ) {
709 for (
int i = 0; i < 3; i++ ) {
724 memcpy( &
mat[0], &m.
mat[0],
sizeof(
double) * 16 );
743 const Matrix4 matRet = (*this) * m;
749 const Matrix4 matRet = (*this) + m;
755 const Matrix4 matRet = (*this) - m;
762 for (
int i = 0; i < 4; i++ )
763 for (
int j = 0; j < 4; j++ )
764 matRet(i,j) = (*this)(j,i);
770 for (
int i = 0; i < 16; i++ )
777 for (
int i = 0; i < 16; i++ )
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]);
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);
794 return Point3( pt[0] / w, pt[1] / w, pt[2] / w );
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]);
806 for (
int i = 0; i < 4; i++ ) {
807 for (
int j = 0; j < 4; j++ ) {
809 for (
int k = 0; k < 4; k++ )
810 matRet(i,j) += (*this)(i,k) * b(k,j);
819 for (
int i = 0; i < 16; i++ )
826 for (
int i = 0; i < 16; i++ )
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";
870 const double c = cos(angle);
871 const double s = sin(angle);
872 const double t = 1 - c;
875 t * a[0] * a[1] - s * a[2],
876 t * a[0] * a[2] + s * a[1],
878 Vector4(t * a[0] * a[1] + s * a[2],
880 t * a[1] * a[2] - s * a[0],
882 Vector4(t * a[0] * a[2] - s * a[1],
883 t * a[1] * a[2] + s * a[0],
890 const double c = cos(angle);
891 const double s = sin(angle);
900 double c = cos(angle);
901 double s = sin(angle);
910 const double c = cos(angle);
911 const double s = sin(angle);
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;
964 for (j = 0; j < 4; j++) {
966 for (i = j + 1; i < 4; i++) {
967 if (fabs(a(i,j)) > fabs(a(p,j)))
972 for ( i = 0; i < 4; i++ ) {
973 const double ta = a(p,i);
977 const double tb = b(p,i);
983 const double s = a(j,j);
985 for ( i = 0; i < 4; i++ ) {
990 for (i = 0; i < 4; i++) {
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);
Vector3 operator*(const Vector3 &v) const
int index(int row, int col) const
static double det2x2(double a, double b, double c, double d)
Matrix3 transpose() const
Point3 operator*(const Point3 &p) const
Matrix3 operator+(const Matrix3 &m) const
const double & operator()(int row, int col) const
Matrix3 & operator*=(double s)
Matrix3(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2)
bool approxEqual(const Matrix3 &m, double eps=1e-12) const
Matrix3 & operator=(const Matrix3 &m)
static Matrix3 rotationXYZtoUVW(Vector3 u, Vector3 v, Vector3 w)
static Matrix3 identity()
double determinant() const
double & operator()(int row, int col)
Matrix3(const Matrix3 &m)
Vector3 column(int c) const
bool operator==(const Matrix3 &m) const
Matrix3 operator*(const Matrix3 &m) const
Matrix4(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2)
static Matrix4 rotation(const Vector3 &u, const Vector3 &v, const Vector3 &w)
int index(int row, int col) const
static Matrix4 translation(const Point3 &p)
Point3 operator*(const Point3 &p) const
static Matrix4 rotation(const Vector3 &axis, double angle)
Matrix4 operator-(const Matrix4 &m) const
static Matrix4 yrotation(double angle)
Matrix4 operator*(const Matrix4 &b) const
Matrix4 & operator*=(const Matrix4 &m)
Matrix4 transpose() const
static Matrix4 scaling(double scale)
static Matrix4 xrotation(double angle)
bool approxEqual(const Matrix4 &m, double eps=1e-12) const
Matrix4 & operator-=(const Matrix4 &m)
Vector4 column(int c) const
Matrix4 operator+(const Matrix4 &m) const
static Matrix4 zrotation(double angle)
Matrix4(const Matrix4 &m)
Vector3 operator*(const Vector3 &v) const
Vector4 operator*(const Vector4 &v) const
Matrix4(const Vector4 &row0, const Vector4 &row1, const Vector4 &row2, const Vector4 &row3)
static Matrix4 identity()
Matrix4 & operator+=(const Matrix4 &m)
static Matrix4 scaling(const Vector3 &s)
bool operator==(const Matrix4 &m) const
const double & operator()(int row, int col) const
static Matrix4 scaling(double x, double y, double z)
static Matrix4 translation(const Vector3 &v)
double & operator()(int row, int col)
Matrix4 & operator=(const Matrix4 &m)
Point3 & operator-=(const Vector3 &v)
bool approxEqual(const Point3 &p, double eps=1e-12) const
const double & operator[](int n) const
double distanceToSquared(const Point3 &p) const
Point3(double _x, double _y, double _z)
double distanceFromOrigin() const
Point3 & operator+=(const Vector3 &v)
bool operator!=(const Point3 &p) const
Point3 operator+(const Vector3 &v) const
Point3 & operator*=(double s)
double distanceFromOriginSquared() const
double & operator[](int n)
bool operator==(const Point3 &p) const
Vector3 operator-(const Point3 &p) const
Point3 & operator=(const Point3 &a)
double distanceTo(const Point3 &p) const
Point3 operator-(const Vector3 &v) const
ScreenPoint & operator*=(int s)
const int & operator[](int n) const
bool operator==(const ScreenPoint &p) const
bool operator!=(const ScreenPoint &p) const
ScreenPoint & operator-=(const ScreenVector &v)
ScreenPoint & operator+=(const ScreenVector &v)
ScreenPoint operator+(const ScreenVector &v) const
ScreenPoint operator-(const ScreenVector &v) const
ScreenPoint & operator=(const ScreenPoint &a)
ScreenVector operator-(const ScreenPoint &p) const
ScreenPoint(int _x, int _y)
ScreenPoint(const ScreenPoint &p)
int operator*(const ScreenVector &v) const
bool operator!=(const ScreenVector &v) const
ScreenVector operator-(const ScreenVector &v) const
ScreenVector operator*(const double s) const
ScreenVector operator+() const
ScreenVector(const ScreenVector &v)
const int & operator[](int n) const
ScreenVector & operator*=(int s)
bool operator==(const ScreenVector &v) const
ScreenVector & operator-=(const ScreenVector &a)
ScreenVector operator-() const
ScreenVector & operator=(const ScreenVector &a)
int lengthSquared() const
ScreenVector & operator+=(const ScreenVector &a)
ScreenVector(int _x, int _y)
ScreenVector operator+(const ScreenVector &v) const
double & operator[](int n)
Vector3 operator+(const Vector3 &v) const
Vector3(double _x, double _y, double _z)
Vector3 operator*(const double s) const
Vector3 & operator+=(const Vector3 &a)
double operator*(const Vector3 &v) const
bool approxEqual(const Vector3 &v, double eps=1e-12) const
Vector3 & operator=(const Vector3 &a)
Vector3 operator-(const Vector3 &v) const
Vector3(const Vector3 &v)
bool operator==(const Vector3 &v) const
Vector3 & operator*=(double s)
bool operator!=(const Vector3 &v) const
Vector3 & operator-=(const Vector3 &a)
Vector3 operator-() const
double lengthSquared() const
const double & operator[](int n) const
Vector3 operator+() const
Vector3 operator/(const double s) const
Vector3 operator^(const Vector3 &v) const
Vector4 & operator+=(const Vector4 &a)
bool approxEqual(const Vector4 &v, double eps=1e-12) const
Vector4(const Vector4 &v)
Vector4 & operator=(const Vector4 &a)
double operator*(const Vector4 &v) const
Vector4 operator*(const double s) const
bool operator!=(const Vector4 &v) const
Vector4(double _x, double _y, double _z, double _w)
bool operator==(const Vector4 &v) const
double lengthSquared() const
Vector4 operator/(const double s) const
const double & operator[](int n) const
Vector4 & operator*=(double s)
Vector4 & operator-=(const Vector4 &a)
Vector4 operator+(const Vector4 &v) const
double & operator[](int n)
Vector4 operator-(const Vector4 &v) const
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
EMData * sqrt() const
return square root of current image
Vector3 cross(const Vector3 &w, const Vector3 &v)
Vector3 unit(const Vector3 &v)
double dot(const Vector3 &w, const Vector3 &v)
double length(const Vector3 &v)
bool isZero(double in_d, double in_dEps=1e-16)
EMData * operator*(const EMData &em, float n)
Point3 lerp(const Point3 &p0, const Point3 &p1, double dT)
std::ostream & operator<<(std::ostream &os, const ScreenVector &v)