Sunny:tpoint3.h

/**/ ///
// 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,definedfortemplateclasses
t_point3()
...{}

//Constructapointwith3giveninputs
t_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);

//Constructapointusingsphericalcoordinates
template<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)));
}


//Reassignapointwithoutmakingatemporarystructure
voidassign(T_x,T_y,T_z)
...{
x
=_x;y=_y;z=_z;
}


//Returnthemagnitudeofapoint
Tmag()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;
}


//Computesthedistancebetweentwovectors
staticTdist(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();
}


//Computesthedistancesquarebetweentwovectors
staticTdistsq(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:
//
//Accumulativeadditionoftwovectors
t_point3&operator+=(constt_point3&in)
...{
x
+=in.x;
y
+=in.y;
z
+=in.z;
return*this;
}


//Accumulativesubtractionoftwovectors
t_point3&operator-=(constt_point3&in)
...{
x
-=in.x;
y
-=in.y;
z
-=in.z;
return*this;
}


//Accumulativemultiplicationofavectorbyascalar
t_point3&operator*=(constT&s)
...{
x
*=s;
y
*=s;
z
*=s;
return*this;
}


//Accumulativedivisionofavectorbyascalar
t_point3&operator/=(constT&s)
...{
Tinv
=1.f/s;
x
*=inv;
y
*=inv;
z
*=inv;
return*this;
}


t_point3
operator-()
...{
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_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值