/**/ /// // tpoint3.h // Sunny:2007-2-7 // Author:cheungmine-cheungmine@gmail.com // Copyrightbycheungmine /**/ /// #ifndef_tpoint3_h_ #define _tpoint3_h_ #include " geomdef.h " #include " tpoint2.h " template < typenameT > struct GEOM_EXPt_point4;template < typenameT > struct GEOM_EXPt_point3 ... {union...{struct...{Tx,y,z;//3realcomponentsofthevector};Tv[3];//Arrayaccessusefulinloops};//Defaultconstructor,definedfortemplateclassest_point3()...{}//Constructapointwith3giveninputst_point3(T_x,T_y,T_z):x(_x),y(_y),z(_z)...{}t_point3(constt_point3&in):x(in.x),y(in.y),z(in.z)...{}t_point3(constt_point2<T>&in):x(in.x),y(in.y),z(0.f)...{}t_point3(constt_point4<T>&in);//Constructapointusingsphericalcoordinatestemplate<typenameT>staticconstt_point3<T>Spherical(Ttheta,Trho,Tphi)...{returnt_point3<T>((T)(phi*cos(rho)*sin(theta)),(T)(phi*sin(rho)),(T)(phi*cos(rho)*cos(theta)));}//Reassignapointwithoutmakingatemporarystructurevoidassign(T_x,T_y,T_z)...{x=_x;y=_y;z=_z;}//ReturnthemagnitudeofapointTmag()const...{return(T)sqrt(x*x+y*y+z*z);}//Thelengthofthevectorsquared(toavoidthesqrt())Tmagsq()const...{return(T)(x*x+y*y+z*z);}//Normalizesavector(makesit'slength1)voidnorm()...{TinvMag=1.f/mag();(*this)*=invMag;}//ComputesthedistancebetweentwovectorsstaticTdist(constt_point3&a,constt_point3&b)...{//can'tusethesubtractionoperator(it'snotdefinedyet!)t_point3vec(b.x-a.x,b.y-a.y,b.z-a.z);returnvec.mag();}//ComputesthedistancesquarebetweentwovectorsstaticTdistsq(constt_point3&a,constt_point3&b)...{//can'tusethesubtractionoperator(it'snotdefinedyet!)t_point3vec(b.x-a.x,b.y-a.y,b.z-a.z);returnvec.magsq();}////operatorsoverload:////Accumulativeadditionoftwovectorst_point3&operator+=(constt_point3&in)...{x+=in.x;y+=in.y;z+=in.z;return*this;}//Accumulativesubtractionoftwovectorst_point3&operator-=(constt_point3&in)...{x-=in.x;y-=in.y;z-=in.z;return*this;}//Accumulativemultiplicationofavectorbyascalart_point3&operator*=(constT&s)...{x*=s;y*=s;z*=s;return*this;}//Accumulativedivisionofavectorbyascalart_point3&operator/=(constT&s)...{Tinv=1.f/s;x*=inv;y*=inv;z*=inv;return*this;}t_point3operator-()...{returnt_point3(-x,-y,-z);}////Constantvectors//staticconstt_point3<T>Zero;staticconstt_point3<T>i;staticconstt_point3<T>j;staticconstt_point3<T>k;} ; // Addstwopointstogether:ret=a+b template < typenameT > inline const t_point3 < T > operator + ( const t_point3 < T >& a, const t_point3 < T >& b) ... {returnt_point3<T>(a.x+b.x,a.y+b.y,a.z+b.z);} ; // Subtractstopoints:ret=a-b template < typenameT > inline const t_point3 < T > operator - ( const t_point3 < T >& a, const t_point3 < T >& b) ... {returnt_point3<T>(a.x-b.x,a.y-b.y,a.z-b.z);} ; // Scalesavectorbyascalar:ret=a*s template < typenameT > inline const t_point3 < T > operator * ( const t_point3 < T >& a, const T & s) ... {returnt_point3<T>(a.x*s,a.y*s,a.z*s);} ; // Scalesavectorbyascalar:ret=s*b template < typenameT > inline const t_point3 < T > operator * ( const T & s, const t_point3 < T >& b) ... {returnt_point3<T>(s*b.x,s*b.y,s*b.z);} ; // Dividesavectorbyascalar:ret=a/s template < typenameT > inline const t_point3 < T > operator / ( const t_point3 < T >& a, float const & s) ... {Tinv=1.0/s;returnt_point3<T>(a.x*inv,a.y*inv,a.z*inv);} ; // Performsavectorcrossproduct:ret=acrossb template < typenameT > inline const t_point3 < T > operator ^ ( const t_point3 < T >& a, const t_point3 < T >& b) ... {returnt_point3<T>((a.y*b.z-a.z*b.y),(a.z*b.x-a.x*b.z),(a.x*b.y-a.y*b.x));} // Performsavectordotproduct:ret=adotb template < typenameT > inline const T operator * ( const t_point3 < T >& a, const t_point3 < T >& b) ... {return(T)(a.x*b.x+a.y*b.y+a.z*b.z);} // VectorEquality,epsilonusedduetonumericalimprecision template < typenameT > inline bool operator == ( const t_point3 < T >& a, const t_point3 < T >& b) ... {if(fabs(a.x-b.x)<EPSILON&&fabs(a.y-b.y)<EPSILON&&fabs(a.z-b.z)<EPSILON)returntrue;returnfalse;} ; #endif // _tpoint2_h_