$search
00001 #ifndef VEC3D_HH 00002 #define VEC3D_HH 00003 00004 /* 00005 James R. Diebel 00006 Stanford University 00007 00008 Started: 23 August 2004 00009 Last revised: 27 Sugust 2004 00010 00011 vec3d.hh - Class definition of basic 3d vector class 00012 00013 Depends on nothing 00014 00015 The only point of this class is to allow for convenient vector notation 00016 and to thereby make the code easier to read 00017 00018 -- Vectors are initialized with "Vec3d(x,y,z)" or "Vec3d()", which yields 00019 the zero vector. 00020 -- The values of existing vectors may be set with "v[0] = x" or 00021 "v.set(x,y,z)" or "v1 = v2" or "v = d" where d is a float[3] 00022 -- Values may be referenced with the [] notation too: "x = v[0]" 00023 -- Vectors add and subtract like scalars: "u = v + w; u -= v;" 00024 -- Most reasonable operations with scalars are allowed: "v = a*u + b" or 00025 "w = (-v + a)/b" or "v = (-(a - u)/b)+c" 00026 -- Dot and cross products are defined: "u.dot(v)" and "u.cross(v)" 00027 -- Equality of two vectors may be tested with "if (u==v || u!=v)" as usual 00028 -- Relative vectors may be formed with "u.to(v)" or "v.from(u)" which are 00029 both equal to "(v-u)" and are convenient when thinking of vectors as 00030 arrows between points in space: the arrow from u to v is "u.to(v)" 00031 -- The length of a vector can be computed as "v.len()" or "~v" 00032 -- The distance from one vector to another can be computed as 00033 "v.distTo(u)" which is the same as "(v-u).len()" or "~(v-u)" 00034 -- The squared length or squared distance can be computed with the 00035 related "v.len2()" and "v.dist2To(u)" 00036 -- A vector may be normalized with "v.normalize()" 00037 -- A vector may be printed with a standard "cout << v << endl" 00038 or "v.print(); cout << endl" or "v.printcr()", and to print the length 00039 too, "v.printall()" 00040 */ 00041 00042 #include <math.h> 00043 #include <stdlib.h> 00044 #include <iostream> 00045 #ifndef PI 00046 #define PI 3.14159265358979323846 00047 #endif 00048 00049 namespace bmtk { 00050 00051 using namespace std; 00052 00053 class Vec3d { 00054 public: // everything is public 00055 00056 // The data is a simple array of size 3 00057 float x[3]; 00058 00059 // Constructors 00060 Vec3d(); 00061 Vec3d(const float x0, const float x1, const float x2); 00062 Vec3d(const float a); 00063 Vec3d(const Vec3d& v); 00064 Vec3d(const float* x_); 00065 00066 // Assignments 00067 void set(const float x0, const float x1, const float x2); 00068 Vec3d operator = (const Vec3d &v); 00069 Vec3d operator = (const float d[3]); 00070 Vec3d operator = (float a); 00071 Vec3d operator = (int a); 00072 00073 // Reference operator: zero-based square bracket referencing 00074 float operator [] (const int index) const; 00075 float& operator [] (const int index); 00076 00077 // Return relative vectors (vector subtraction) 00078 Vec3d to(const Vec3d &v) const; 00079 Vec3d from(const Vec3d &v) const; 00080 00081 // Addition and subtraction: <vector> +/- <vector or scalar> 00082 // note: does not perform <scalar> +/- <vector> 00083 // (see non-class functions below) 00084 Vec3d operator + (const Vec3d &v) const; 00085 Vec3d operator + (const float a) const; 00086 void operator += (const Vec3d &v); 00087 void operator += (const float a); 00088 Vec3d operator - (const Vec3d &v) const; 00089 Vec3d operator - (const float a) const; 00090 void operator -= (const Vec3d &v); 00091 void operator -= (const float a); 00092 Vec3d operator - () const; 00093 00094 // Multiplication and division by scalars: <vector> *// <scalar> 00095 // note: does not perform <scalar> * <vector> 00096 // (see non-class functions below) 00097 Vec3d operator * (const float a) const; 00098 Vec3d operator / (const float a) const; 00099 Vec3d operator * (const Vec3d v) const; 00100 Vec3d operator / (const Vec3d v) const; 00101 Vec3d operator *= (const float a); 00102 Vec3d operator /= (const float a); 00103 Vec3d operator *= (const Vec3d v); 00104 Vec3d operator /= (const Vec3d v); 00105 00106 // Dot and Cross Products 00107 float dot(const Vec3d &v) const; 00108 Vec3d cross(const Vec3d &v) const; 00109 00110 // Equality tests make sense 00111 bool operator == (const Vec3d &v) const; 00112 bool operator != (const Vec3d &v) const; 00113 00114 // Length and Normalize 00115 float len(void) const; 00116 float operator ~ (void) const; 00117 float len2(void) const; 00118 float distTo(Vec3d &v) const; 00119 float dist2To(Vec3d &v) const; 00120 void normalize(void); 00121 00122 // Print routines 00123 void print(void); 00124 void printcr(void); 00125 void printall(void); 00126 }; 00127 00128 // Define a few global operators to handle cases where 00129 // a Vec3d is the second argument in a binary operator 00130 Vec3d operator + (const float a, const Vec3d &v); 00131 Vec3d operator - (const float a, const Vec3d &v); 00132 Vec3d operator * (const float a, const Vec3d &v); 00133 00134 // Define << operator to handle output to screen 00135 ostream& operator << (ostream& os, const Vec3d &v); 00136 00137 } // namespace bmtk 00138 00139 #endif