www.pudn.com > MyGame.rar > Point3.h


/***************************************************************************** 
 * VCGLib                                                                    * 
 *																																					 * 
 * Visual Computing Group                                                o>  * 
 * IEI Institute, CNUCE Institute, CNR Pisa                             <|   * 
 *                                                                      / \  * 
 * Copyright(C) 1999 by Paolo Cignoni, Claudio Rocchini                      * 
 * All rights reserved.                                                      * 
 *																																					 * 
 * Permission  to use, copy, modify, distribute  and sell this  software and * 
 * its documentation for any purpose is hereby granted without fee, provided * 
 * that  the above copyright notice appear  in all copies and that both that * 
 * copyright   notice  and  this  permission  notice  appear  in  supporting * 
 * documentation. the author makes  no representations about the suitability * 
 * of this software for any purpose. It is provided  "as is" without express * 
 * or implied warranty.                                                      * 
 *					                                                         * 
 *****************************************************************************/ 
/**************************************************************************** 
 History 
 
 1999 Feb 02 First Draft. 
			Mar 15 First Working release  
			Jul 10 Reindented and reformatted 
						 Fixed != bug 
						 Fixed Eq bug 
 
						 
****************************************************************************/ 
#ifndef __VCGLIB_POINT3 
#define __VCGLIB_POINT3 
 
//#include  
#include  
#include  
 
namespace vcg { 
 
template  class Point3 
{ 
	public: 
 
	FLTYPE v[3]; 
 
	typedef Point3 Point3_FT; 
 
  inline FLTYPE &x() {return v[0];} ; 
  inline FLTYPE &y() {return v[1];}; 
	inline FLTYPE &z() {return v[2];}; 
  inline FLTYPE & operator [] ( const int i ){ 
			assert(i>=0 && i<3); 
			return v[i]; 
	} 
	inline const FLTYPE & operator [] ( const int i ) const { 
			assert(i>=0 && i<3); 
			return v[i]; 
	} 
 
  inline Point3 ( void ) { } 
  inline Point3 ( const FLTYPE nx, const FLTYPE ny, const FLTYPE nz ){ 
			v[0] = nx; v[1] = ny; v[2] = nz; 
	} 
	inline Point3 ( Point3 const & p)  {    
			v[0]= p.v[0];    v[1]= p.v[1];    v[2]= p.v[2]; 
	} 
	inline Point3 & operator =( Point3 const & p){ 
			v[0]= p.v[0]; v[1]= p.v[1]; v[2]= p.v[2]; 
			return *this; 
	} 
  inline Point3 operator + ( Point3 const & p) const {  
			return Point3( v[0]+p.v[0], v[1]+p.v[1], v[2]+p.v[2] ); 
	} 
  inline Point3 operator - ( Point3 const & p) const { 
			return Point3( v[0]-p.v[0], v[1]-p.v[1], v[2]-p.v[2] ); 
	} 
  inline Point3 operator * ( const FLTYPE s ) const { 
			return Point3( v[0] * s, v[1] * s, v[2] * s ); 
	} 
	inline Point3 operator / ( const FLTYPE s ) const { 
			return Point3( v[0] / s, v[1] / s, v[2] / s ); 
	} 
  inline FLTYPE operator * ( Point3 const & p ) const { 
			return ( v[0]*p.v[0] + v[1]*p.v[1] + v[2]*p.v[2] ); 
	}   
  inline Point3 operator ^ ( Point3 const & p ) const { 
			return Point3( 
				    v[1]*p.v[2] - v[2]*p.v[1], 
						v[2]*p.v[0] - v[0]*p.v[2], 
						v[0]*p.v[1] - v[1]*p.v[0] 
				); 
	}   
  inline Point3 & operator += ( Point3 const & p){ 
			v[0] += p.v[0];    v[1] += p.v[1];     v[2] += p.v[2]; 
			return *this; 
	} 
  inline Point3 & operator -= ( Point3 const & p){ 
			v[0] -= p.v[0];    v[1] -= p.v[1];     v[2] -= p.v[2]; 
			return *this; 
	} 
  inline Point3 & operator *= ( const FLTYPE s ){ 
			v[0] *= s;    v[1] *= s;    v[2] *= s; 
			return *this; 
	} 
  inline Point3 & operator /= ( const FLTYPE s ){ 
			v[0] /= s;    v[1] /= s;    v[2] /= s; 
			return *this; 
	} 
  inline FLTYPE Norm( void ) const { 
			return vcg::sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ); 
	} 
  inline FLTYPE Norm2( void ) const{ 
			return ( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ); 
	} 
  inline Point3 & Scale( const FLTYPE sx, const FLTYPE sy, const FLTYPE sz ); 
 
	inline Point3 & Normalize( void ){ 
			FLTYPE n = vcg::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); 
			if(n>0.0) {	v[0] /= n;	v[1] /= n;	v[2] /= n;  } 
			return *this; 
	} 
  inline bool operator == ( Point3 const & p ) const { 
			return (v[0]==p.v[0] && v[1]==p.v[1] && v[2]==p.v[2]); 
	}  
  inline bool operator != ( Point3 const & p ) const { 
			return ( (v[0]!=p.v[0]) || (v[1]!=p.v[1]) || (v[2]!=p.v[2])); 
	} 
  inline bool operator <  ( Point3 const & p ) const { 
			return	(v[2]!=p.v[2])?(v[2]  ( Point3 const & p ) const { 
			return	(v[2]!=p.v[2])?(v[2]>p.v[2]): 
							(v[1]!=p.v[1])?(v[1]>p.v[1]): 
							(v[0]>p.v[0]); 
	} 
 
	inline bool operator <= ( Point3 const & p ) const { 
			return	(v[2]!=p.v[2])?(v[2]< p.v[2]): 
							(v[1]!=p.v[1])?(v[1]< p.v[1]): 
							(v[0]<=p.v[0]); 
	} 
 
	inline bool operator >= ( Point3 const & p ) const { 
			return	(v[2]!=p.v[2])?(v[2]> p.v[2]): 
							(v[1]!=p.v[1])?(v[1]> p.v[1]): 
							(v[0]>=p.v[0]); 
	} 
  inline FLTYPE Distance( Point3 const & p ) const{ 
			return (*this-p).Norm(); 
	} 
 
	inline FLTYPE SquaredDistance( Point3 const & p ) const { 
			return (*this-p).Norm2(); 
	}	 
 
	inline int Hash() const { 
	FLTYPE vv = v[0]*1234567 + v[1]*2345671 + v[2]*3456712; 
		int *ip = (int *)&vv; 
		return *ip; 
	}	 
 
 
/* 
inline bool Eq( Point3 const & p ) const { 
  return (vcg::abs(v[0]-p.v[0]) < std::numeric_limits::epsilon()*4 && 
		  vcg::abs(v[1]-p.v[1]) < std::numeric_limits::epsilon()*4 && 
		  vcg::abs(v[2]-p.v[2]) < std::numeric_limits::epsilon()*4 ); 
}    
 
inline bool NotEq( Point3 const & p ) const { 
  return (vcg::abs(v[0]-p.v[0]) > std::numeric_limits::epsilon()*4 || 
		  vcg::abs(v[1]-p.v[1]) > std::numeric_limits::epsilon()*4 || 
		  vcg::abs(v[2]-p.v[2]) > std::numeric_limits::epsilon()*4 ); 
}    
*/ 
}; // end class definition 
 
 
template  
inline FLTYPE Angle( Point3 const & p1, Point3 const & p2 ){ 
    return (FLTYPE) acos( (p1*p2)/(p1.Norm()*p2.Norm()) ); 
} 
 
template  
inline Point3 operator - ( Point3 const & p ){ 
    return Point3( -p.v[0], -p.v[1], -p.v[2] ); 
} 
 
template  
inline Point3 operator * ( const FLTYPE s, Point3 const & p ){ 
    return Point3( p.v[0] * s, p.v[1] * s, p.v[2] * s ); 
} 
 
template  
inline FLTYPE Norm( Point3 const & p ){ 
		return vcg::sqrt( p.v[0]*p.v[0] + p.v[1]*p.v[1] + p.v[2]*p.v[2] ); 
} 
 
template  
inline FLTYPE Norm2( Point3 const & p ){ 
    return ( p.v[0]*p.v[0] + p.v[1]*p.v[1] + p.v[2]*p.v[2] ); 
} 
 
template  
inline Point3 & Normalize( Point3 & p ){ 
		FLTYPE n = vcg::sqrt( p.v[0]*p.v[0] + p.v[1]*p.v[1] + p.v[2]*p.v[2] ); 
    if(n>0.0) p/=n; 
    return p; 
} 
 
template  
inline FLTYPE Distance( Point3 const & p1,Point3 const & p2 ){ 
    return Norm(p1-p2); 
} 
 
template  
inline FLTYPE SquaredDistance( Point3 const & p1,Point3 const & p2 ){ 
    return SquaredNorm(p1-p2); 
} 
 
template  
inline Point3 & RotateZ( Point3 & p, FLTYPE _angle){ 
  FLTYPE c=cos(_angle); 
	FLTYPE s=sin(_angle); 
  FLTYPE cx=c*p.x()-s*p.y(); 
  p.y()= s*p.x()+c*p.y(); 
	p.x()= cx; 
	return p; 
} 
 
#ifdef __GL_H__ 
inline void glVertex(Point3 const & p)   { glVertex3iv(p.v);}; 
inline void glVertex(Point3 const & p) { glVertex3sv(p.v);}; 
inline void glVertex(Point3 const & p) { glVertex3fv(p.v);}; 
inline void glVertex(Point3 const & p){ glVertex3dv(p.v);}; 
inline void glNormal(Point3 const & p)   { glNormal3iv(p.v);}; 
inline void glNormal(Point3 const & p) { glNormal3sv(p.v);}; 
inline void glNormal(Point3 const & p) { glNormal3fv(p.v);}; 
inline void glNormal(Point3 const & p){ glNormal3dv(p.v);}; 
inline void glTexCoord(Point3 const & p)   { glTexCoord3iv(p.v);}; 
inline void glTexCoord(Point3 const & p) { glTexCoord3sv(p.v);}; 
inline void glTexCoord(Point3 const & p) { glTexCoord3fv(p.v);}; 
inline void glTexCoord(Point3 const & p){ glTexCoord3dv(p.v);}; 
inline void glTranslate(Point3 const & p) { glTranslatef(p.v[0],p.v[1],p.v[2]);}; 
inline void glTranslate(Point3 const & p){ glTranslated(p.v[0],p.v[1],p.v[2]);}; 
inline void glScale(Point3 const & p) { glScalef(p.v[0],p.v[1],p.v[2]);}; 
inline void glScale(Point3 const & p){ glScaled(p.v[0],p.v[1],p.v[2]);}; 
#endif 
 
typedef Point3  Point3s; 
typedef Point3	   Point3i; 
typedef Point3  Point3f; 
typedef Point3 Point3d; 
 
 
 
 
 
 
 
 
 
} // end namespace 
#endif