00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __UIUC_PPL_CHARM_VECTOR_3D_H
00011 #define __UIUC_PPL_CHARM_VECTOR_3D_H
00012
00013 #include "pup.h"
00014
00015 #include <math.h>
00016
00017
00018 #ifdef max
00019 # undef max
00020 # undef min
00021 #endif
00022
00023
00024
00025
00026 template <class real>
00027 class CkVector3dT {
00028 typedef CkVector3dT<real> vec;
00029 public:
00030 real x,y,z;
00031 CkVector3dT(void) {}
00032
00033 explicit CkVector3dT(int init) {x=y=z=(real)init;}
00034 explicit CkVector3dT(float init) {x=y=z=(real)init;}
00035 explicit CkVector3dT(double init) {x=y=z=(real)init;}
00036
00037 CkVector3dT(const real Nx,const real Ny,const real Nz) {x=Nx;y=Ny;z=Nz;}
00038
00039 CkVector3dT(const real *arr) {x=arr[0];y=arr[1];z=arr[2];}
00040
00041
00042 CkVector3dT(const CkVector3dT<float> &src)
00043 {x=(real)src.x; y=(real)src.y; z=(real)src.z;}
00044 CkVector3dT(const CkVector3dT<double> &src)
00045 {x=(real)src.x; y=(real)src.y; z=(real)src.z;}
00046 CkVector3dT(const CkVector3dT<int> &src)
00047 {x=(real)src.x; y=(real)src.y; z=(real)src.z;}
00048
00049
00050
00051
00052 operator real *() {return (real *)&x;}
00053 operator const real *() const {return (const real *)&x;}
00054
00055
00056 int operator==(const vec &b) const {return (x==b.x)&&(y==b.y)&&(z==b.z);}
00057 int operator!=(const vec &b) const {return (x!=b.x)||(y!=b.y)||(z!=b.z);}
00058 vec operator+(const vec &b) const {return vec(x+b.x,y+b.y,z+b.z);}
00059 vec operator-(const vec &b) const {return vec(x-b.x,y-b.y,z-b.z);}
00060 vec operator*(const real scale) const
00061 {return vec(x*scale,y*scale,z*scale);}
00062 friend vec operator*(const real scale,const vec &v)
00063 {return vec(v.x*scale,v.y*scale,v.z*scale);}
00064 vec operator/(const real &div) const
00065 {real scale=1.0/div;return vec(x*scale,y*scale,z*scale);}
00066 vec operator-(void) const {return vec(-x,-y,-z);}
00067 void operator+=(const vec &b) {x+=b.x;y+=b.y;z+=b.z;}
00068 void operator-=(const vec &b) {x-=b.x;y-=b.y;z-=b.z;}
00069 void operator*=(const real scale) {x*=scale;y*=scale;z*=scale;}
00070 void operator/=(const real div) {real scale=1.0/div;x*=scale;y*=scale;z*=scale;}
00071
00072
00073
00074 real magSqr(void) const {return x*x+y*y+z*z;}
00075
00076 real mag(void) const {return sqrt(magSqr());}
00077
00078
00079 real distSqr(const vec &b) const
00080 {return (x-b.x)*(x-b.x)+(y-b.y)*(y-b.y)+(z-b.z)*(z-b.z);}
00081
00082 real dist(const vec &b) const {return sqrt(distSqr(b));}
00083
00084
00085 real dot(const vec &b) const {return x*b.x+y*b.y+z*b.z;}
00086
00087 real cosAng(const vec &b) const {return dot(b)/(mag()*b.mag());}
00088
00089
00090 vec dir(void) const {return (*this)/mag();}
00091
00092 vec cross(const vec &b) const {
00093 return vec(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
00094 }
00095
00096
00097 real max(void) {
00098 real big=x;
00099 if (big<y) big=y;
00100 if (big<z) big=z;
00101 return big;
00102 }
00103
00104
00105 void enlarge(const vec &by) {
00106 if (x<by.x) x=by.x;
00107 if (y<by.y) y=by.y;
00108 if (z<by.z) z=by.z;
00109 }
00110
00111 #ifdef __CK_PUP_H
00112 void pup(PUP::er &p) {p|x;p|y;p|z;}
00113 #endif
00114 };
00115
00116 typedef CkVector3dT<double> CkVector3d;
00117 typedef CkVector3dT<float> CkVector3f;
00118 typedef CkVector3dT<int> CkVector3i;
00119
00120
00121
00122 class CkBbox3d {
00123 public:
00124 CkVector3d min,max;
00125 CkBbox3d() {empty();}
00126 void empty(void) {min.x=min.y=min.z=1e30;max.x=max.y=max.z=-1e30;}
00127 bool isEmpty(void) const {return min.x>max.x;}
00128 void add(const CkVector3d &p) {
00129 if (min.x>p.x) min.x=p.x;
00130 if (min.y>p.y) min.y=p.y;
00131 if (min.z>p.z) min.z=p.z;
00132 if (max.x<p.x) max.x=p.x;
00133 if (max.y<p.y) max.y=p.y;
00134 if (max.z<p.z) max.z=p.z;
00135 }
00136 void add(const CkBbox3d &b) {
00137 add(b.min); add(b.max);
00138 }
00139 #ifdef __CK_PUP_H
00140 void pup(PUP::er &p) {p|min;p|max;}
00141 #endif
00142 };
00143
00144
00145
00146 class CkHalfspace3d {
00147 public:
00148
00149 CkVector3d n;
00150 double d;
00151
00152 typedef const CkVector3d cv;
00153 CkHalfspace3d() {}
00154 CkHalfspace3d(cv &p1,cv &p2,cv &p3) {init(p1,p2,p3);}
00155 CkHalfspace3d(cv &p1,cv &p2,cv &p3,cv &in) {initCheck(p1,p2,p3,in);}
00156
00157 CkHalfspace3d(cv &norm,cv &p0) {n=norm;d=-n.dot(p0);}
00158
00159
00160
00161 void init(cv &p1,cv &p2,cv &p3) {
00162 n=(p2-p1).cross(p3-p1);
00163 d=-n.dot(p1);
00164 }
00165
00166
00167 void initCheck(cv &p1,cv &p2,cv &p3,cv &in)
00168 { init(p1,p2,p3); if (side(in)<0) {n=-n;d=-d;} }
00169
00170
00171
00172 double side(cv &pt) const
00173 {return n.dot(pt)+d;}
00174
00175
00176 double intersect(cv &pos,cv &dir) const
00177 {return -(d+n.dot(pos))/n.dot(dir);}
00178
00179
00180
00181 CkVector3d intersectPt(cv &start,cv &dir) const
00182 {
00183 return start+dir*intersect(start,dir);
00184 }
00185 };
00186
00187 #endif