$search
00001 /* 00002 James R. Diebel 00003 Stanford University 00004 00005 Started: 23 August 2004 00006 00007 vec3d.cc - implementation for basic 3d vector class Vec3d 00008 00009 Depends on vec3d.hh 00010 */ 00011 00012 #include "bmtk/vec3d.hh" 00013 00014 namespace bmtk { 00015 00016 // Constructors 00017 Vec3d::Vec3d() { 00018 x[0] = x[1] = x[2] = 0; 00019 } 00020 Vec3d::Vec3d(const float x0, const float x1, const float x2) { 00021 x[0] = x0; x[1] = x1; x[2] = x2; 00022 } 00023 Vec3d::Vec3d(const float a) { 00024 x[0] = x[1] = x[2] = a; 00025 } 00026 Vec3d::Vec3d(const Vec3d& v) { 00027 x[0] = v.x[0]; x[1] = v.x[1]; x[2] = v.x[2]; 00028 } 00029 Vec3d::Vec3d(const float* x_) { 00030 x[0] = x_[0]; x[1] = x_[1]; x[2] = x_[2]; 00031 } 00032 00033 // Assignments 00034 void Vec3d::set(const float x0, const float x1, const float x2) { 00035 x[0] = x0; x[1] = x1; x[2] = x2; 00036 } 00037 Vec3d Vec3d::operator = (const Vec3d& v) { 00038 x[0] = v.x[0]; x[1] = v.x[1]; x[2] = v.x[2]; 00039 return *this; 00040 } 00041 Vec3d Vec3d::operator = (const float d[3]) { 00042 x[0] = d[0]; x[1] = d[1]; x[2] = d[2]; 00043 return *this; 00044 } 00045 Vec3d Vec3d::operator = (float a) { 00046 x[0] = a; x[1] = a; x[2] = a; 00047 return *this; 00048 } 00049 Vec3d Vec3d::operator = (int a) { 00050 x[0] = a; x[1] = a; x[2] = float(a); 00051 return *this; 00052 } 00053 00054 // Reference operator: zero-based square bracket referencing 00055 float Vec3d::operator [] (const int index) const { 00056 if (index < 0 || index > 2) cerr << "Index our of bounds" << endl << flush; 00057 return x[index]; 00058 } 00059 float& Vec3d::operator [] (const int index) { 00060 if (index < 0 || index > 2) cerr << "Index our of bounds" << endl << flush; 00061 return x[index]; 00062 } 00063 00064 // Return relative vectors (vector subtraction) 00065 Vec3d Vec3d::to(const Vec3d& v) const { 00066 return Vec3d(v.x[0] - x[0], v.x[1] - x[1], v.x[2] - x[2]); 00067 } 00068 Vec3d Vec3d::from(const Vec3d& v) const { 00069 return Vec3d(x[0] - v.x[0], x[1] - v.x[1], x[2] - v.x[2]); 00070 } 00071 00072 // Addition and subtraction: <vector> +/- <vector or scalar> 00073 // note: does not perform <scalar> +/- <vector> 00074 // (see non-class functions below) 00075 Vec3d Vec3d::operator + (const Vec3d& v) const { 00076 return Vec3d(x[0] + v.x[0], x[1] + v.x[1], x[2] + v.x[2]); 00077 } 00078 Vec3d Vec3d::operator + (const float a) const { 00079 return Vec3d(x[0] + a, x[1] + a, x[2] + a); 00080 } 00081 void Vec3d::operator += (const Vec3d& v) { 00082 x[0] += v.x[0]; x[1] += v.x[1]; x[2] += v.x[2]; 00083 } 00084 void Vec3d::operator += (const float a) { 00085 x[0] += a; x[1] += a; x[2] += a; 00086 } 00087 00088 Vec3d Vec3d::operator - (const Vec3d& v) const { 00089 return Vec3d(x[0] - v.x[0], x[1] - v.x[1], x[2] - v.x[2]); 00090 } 00091 Vec3d Vec3d::operator - (const float a) const { 00092 return Vec3d(x[0] - a, x[1] - a, x[2] - a); 00093 } 00094 void Vec3d::operator -= (const Vec3d& v) { 00095 x[0] -= v.x[0]; x[1] -= v.x[1]; x[2] -= v.x[2]; 00096 } 00097 void Vec3d::operator -= (const float a) { 00098 x[0] -= a; x[1] -= a; x[2] -= a; 00099 } 00100 Vec3d Vec3d::operator - (void) const { 00101 return Vec3d(-x[0], -x[1], -x[2]); 00102 } 00103 00104 // Multiplication and division by scalars: <vector> *// <scalar> 00105 // note: does not perform <scalar> * <vector> 00106 // (see non-class functions below) 00107 Vec3d Vec3d::operator * (const float a) const { 00108 return Vec3d(x[0]*a, x[1]*a, x[2]*a); 00109 } 00110 Vec3d Vec3d::operator / (const float a) const { 00111 float b = 1/a; 00112 return Vec3d(x[0]*b, x[1]*b, x[2]*b); 00113 } 00114 Vec3d Vec3d::operator * (const Vec3d v) const { 00115 return Vec3d(x[0]*v.x[0], x[1]*v.x[1], x[2]*v.x[2]); 00116 } 00117 Vec3d Vec3d::operator / (const Vec3d v) const { 00118 return Vec3d(x[0]/v.x[0], x[1]/v.x[1], x[2]/v.x[2]); 00119 } 00120 Vec3d Vec3d::operator *= (const float a) { 00121 x[0] *= a; x[1] *= a; x[2] *= a; 00122 return *this; 00123 } 00124 Vec3d Vec3d::operator /= (const float a) { 00125 float b = 1/a; 00126 x[0] *= b; x[1] *= b; x[2] *= b; 00127 return *this; 00128 } 00129 Vec3d Vec3d::operator *= (const Vec3d v) { 00130 x[0] *= v.x[0]; x[1] *= v.x[1]; x[2] *= v.x[2]; 00131 return *this; 00132 } 00133 Vec3d Vec3d::operator /= (const Vec3d v) { 00134 x[0] /= v.x[0]; x[1] /= v.x[1]; x[2] /= v.x[2]; 00135 return *this; 00136 } 00137 00138 // Dot and Cross Products 00139 float Vec3d::dot(const Vec3d& v) const { 00140 return v.x[0]*x[0] + v.x[1]*x[1] + v.x[2]*x[2]; 00141 } 00142 Vec3d Vec3d::cross(const Vec3d& v) const { 00143 return Vec3d(x[1]*v.x[2]-x[2]*v.x[1], 00144 x[2]*v.x[0]-x[0]*v.x[2], 00145 x[0]*v.x[1]-x[1]*v.x[0]); 00146 } 00147 00148 // Equality tests make sense 00149 bool Vec3d::operator == (const Vec3d& v) const { 00150 return (x[0]==v.x[0] && x[1]==v.x[1] && x[2]==v.x[2]); 00151 } 00152 bool Vec3d::operator != (const Vec3d& v) const { 00153 return (x[0]!=v.x[0] || x[1]!=v.x[1] || x[2]!=v.x[2]); 00154 } 00155 00156 // Length and Normalize 00157 float Vec3d::len(void) const { 00158 return sqrt(len2()); 00159 } 00160 float Vec3d::operator ~ (void) const { 00161 return len(); 00162 } 00163 float Vec3d::len2(void) const { 00164 return (x[0]*x[0] + x[1]*x[1] + x[2]*x[2]); 00165 } 00166 float Vec3d::distTo(Vec3d& v) const { 00167 return to(v).len(); 00168 } 00169 float Vec3d::dist2To(Vec3d& v) const { 00170 return to(v).len2(); 00171 } 00172 void Vec3d::normalize(void) { 00173 float l = len(); 00174 if (l > 0) {x[0] /= l; x[1] /= l; x[2] /= l;} 00175 } 00176 00177 // Print routines 00178 void Vec3d::print(void) { 00179 cout << "(" << x[0] << "," << x[1] << "," << x[2] << ")"; 00180 } 00181 void Vec3d::printcr(void) { 00182 cout << "(" << x[0] << "," << x[1] << "," << x[2] << ")" << endl; 00183 } 00184 void Vec3d::printall(void) { 00185 cout << "|" << x[0] << "," << x[1] << "," << x[2] << "| = " 00186 << len() << endl; 00187 } 00188 00189 // Define a few global operators to handle cases where 00190 // a Vec3d is the second argument in a binary operator 00191 Vec3d operator + (const float a, const Vec3d& v) { 00192 return Vec3d(a + v.x[0], a + v.x[1], a + v.x[2]); 00193 } 00194 Vec3d operator - (const float a, const Vec3d& v) { 00195 return Vec3d(a - v.x[0], a - v.x[1], a - v.x[2]); 00196 } 00197 Vec3d operator * (const float a, const Vec3d& v) { 00198 return Vec3d(a*v.x[0], a*v.x[1], a*v.x[2]); 00199 } 00200 00201 // Define << operator to handle output to screen 00202 ostream& operator << (ostream& os, const Vec3d& v) { 00203 return os << "(" << v.x[0] << "," << v.x[1] << "," << v.x[2] << ")"; 00204 } 00205 00206 } // namespace bmtk