00001 #include "vector.hh" 00002 #include <math.h> 00003 #include <string.h> 00004 #include <stdlib.h> 00005 #include <stdio.h> 00006 00007 namespace anoid { 00008 namespace simple { 00009 00010 using namespace std; 00011 00012 void Vector::init(const char *s) { 00013 char *c = strdup(s); 00014 if (sscanf(c, "(%lf,%lf,%lf)", &x, &y, &z) != 3) 00015 x = y = z = 0.0; 00016 free(c); 00017 } 00018 00019 Vector::Vector(double getx, double gety, double getz): Point(getx, gety, getz) { 00020 } 00021 00022 Vector::Vector(string &s): Point() { 00023 init(s.c_str()); 00024 } 00025 00026 Vector::Vector(char *s): Point() { 00027 init(s); 00028 } 00029 00030 double Vector::getNorm() const { 00031 return sqrt((double)((*this)*(*this))); 00032 } 00033 00034 void Vector::setNorm(double newnorm) { 00035 double temp = getNorm(); 00036 x = (double)((x*newnorm)/temp); 00037 y = (double)((y*newnorm)/temp); 00038 z = (double)((z*newnorm)/temp); 00039 } 00040 00041 void Vector::scale(const Vector& v) { 00042 x *= v.x; 00043 y *= v.y; 00044 z *= v.z; 00045 } 00046 00047 Vector operator+(const Vector& a, const Vector& b) { 00048 return Vector(a.x+b.x,a.y+b.y,a.z+b.z); 00049 } 00050 00051 Vector operator+=(Vector &a, const Vector& b) { 00052 a.x += b.x; 00053 a.y += b.y; 00054 a.z += b.z; 00055 return a; 00056 } 00057 00058 Vector Vector::operator=(const Vector& a) { 00059 x = a.x; 00060 y = a.y; 00061 z = a.z; 00062 return *this; 00063 } 00064 00065 Vector operator-(const Vector& a, const Vector& b) { 00066 return a+((-1)*b); 00067 } 00068 00069 Vector operator*(const double c, const Vector& a) { 00070 return Vector(c*a.x,c*a.y,c*a.z); 00071 } 00072 00073 Vector operator*(const Vector& a, const double c) { 00074 return c*a; 00075 } 00076 00077 double operator*(const Vector& a, const Vector& b) { 00078 return a.x*b.x+a.y*b.y+a.z*b.z; 00079 } 00080 00081 Vector operator%(const Vector& a, const Vector& b) { 00082 return Vector(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x); 00083 } 00084 00085 ostream& operator<<(ostream& s, const Vector& a) { 00086 s << '(' << a.x << ',' << a.y << ',' << a.z << ')'; 00087 return s; 00088 } 00089 00090 Vector operator*(const double *d, const Vector &a) { 00091 return Vector(d[0] * a.x + d[4] * a.y + d[8] * a.z, 00092 d[1] * a.x + d[5] * a.y + d[9] * a.z, 00093 d[2] * a.x + d[6] * a.y + d[10] * a.z); 00094 } 00095 00096 Vector operator*(const Vector &a, const double *d) { 00097 return Vector((d[0] + d[1] + d[2]) * a.x, 00098 (d[4] + d[5] + d[6]) * a.y, 00099 (d[8] + d[9] + d[10]) * a.z); 00100 } 00101 00102 }; 00103 }; 00104 00105 double *matMult(double *A, const double* B) { 00106 double tmp[16]; 00107 tmp[0] = A[0] * B[0] + A[4] * B[1] + A[8] * B[2]; 00108 tmp[4] = A[0] * B[4] + A[4] * B[5] + A[8] * B[6]; 00109 tmp[8] = A[0] * B[8] + A[4] * B[9] + A[8] * B[10]; 00110 00111 tmp[1] = A[1] * B[0] + A[5] * B[1] + A[9] * B[2]; 00112 tmp[5] = A[1] * B[4] + A[5] * B[5] + A[9] * B[6]; 00113 tmp[9] = A[1] * B[8] + A[5] * B[9] + A[9] * B[10]; 00114 00115 tmp[2] = A[2] * B[0] + A[6] * B[1] + A[10] * B[2]; 00116 tmp[6] = A[2] * B[4] + A[6] * B[5] + A[10] * B[6]; 00117 tmp[10] = A[2] * B[8] + A[6] * B[9] + A[10] * B[10]; 00118 00119 A[0] = tmp[0]; 00120 A[1] = tmp[1]; 00121 A[2] = tmp[2]; 00122 A[4] = tmp[4]; 00123 A[5] = tmp[5]; 00124 A[6] = tmp[6]; 00125 A[8] = tmp[8]; 00126 A[9] = tmp[9]; 00127 A[10] = tmp[10]; 00128 00129 return A; 00130 } 00131 00132 double *matAdd(double *A, const double *B) { 00133 A[0] += B[0]; 00134 A[1] += B[1]; 00135 A[2] += B[2]; 00136 A[4] += B[4]; 00137 A[5] += B[5]; 00138 A[6] += B[6]; 00139 A[8] += B[8]; 00140 A[9] += B[9]; 00141 A[10] += B[10]; 00142 00143 return A; 00144 } 00145 00146 double *copyMatrix(double *A, double *B) { 00147 A[0] = B[0]; 00148 A[1] = B[1]; 00149 A[2] = B[2]; 00150 A[3] = B[3]; 00151 A[4] = B[4]; 00152 A[5] = B[5]; 00153 A[6] = B[6]; 00154 A[7] = B[7]; 00155 A[8] = B[8]; 00156 A[9] = B[9]; 00157 A[10] = B[10]; 00158 A[11] = B[11]; 00159 A[12] = B[12]; 00160 A[13] = B[13]; 00161 A[14] = B[14]; 00162 A[15] = B[15]; 00163 00164 return A; 00165 } 00166 00167 double *transpose(double *A) { 00168 double tmp; 00169 00170 tmp = A[2]; 00171 A[2] = A[8]; 00172 A[8] = tmp; 00173 00174 tmp = A[1]; 00175 A[1] = A[4]; 00176 A[4] = tmp; 00177 00178 tmp = A[6]; 00179 A[6] = A[9]; 00180 A[9] = tmp; 00181 00182 return A; 00183 } 00184 00185 double *tilde(double *A, const anoid::simple::Vector v) { 00186 A[0] = A[5] = A[10] = A[3] = A[7] = A[11] = A[12] = A[13] = A[14] = 0.0; 00187 A[15] = 1.0; 00188 A[1] = v.getZ(); 00189 A[2] = -v.getY(); 00190 A[4] = -v.getZ(); 00191 A[6] = v.getX(); 00192 A[8] = v.getY(); 00193 A[9] = -v.getX(); 00194 00195 return A; 00196 }