shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/point.hpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * M a t h e m a g i x
00003  *****************************************************************************
00004  * point
00005  * 2010-08-17
00006  * Bernard Mourrain & Julien Wintz
00007  *****************************************************************************
00008  *               Copyright (C) 2006 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 
00013 # ifndef shape_point_hpp
00014 # define shape_point_hpp
00015 
00016 # include <math.h>
00017 # include <realroot/Seq.hpp>
00018 # include <shape/shape.hpp>
00019 
00020 # define TMPL_DEF template<class C, class V=REF_OF(C), int N=3>
00021 # define TMPL  template<class C, class V, int N>
00022 # define TMPL1 template<class K>
00023 # define SELF   point<C,V,N>
00024 # define Viewer viewer<axel,W>
00025 # define Ostream std::ostream
00026 
00027 # undef Scalar
00028 
00029 
00030 namespace mmx {
00031 namespace shape {
00032 
00033 
00034 TMPL_DEF struct point;
00035 
00036 //--------------------------------------------------------------------
00037 struct point_def {}; 
00038 template<>   struct use<point_def>
00039 {
00040   //  typedef typename use<scalar_def,K>::Scalar Scalar;
00041   typedef point<double> Point;
00042 
00043   template<class SEQ,class POINT>
00044   static inline void point_insertor(SEQ& vertices, POINT* p) {
00045     vertices <<p;
00046   }
00047 
00048   template<class OSTREAM,class POINT>
00049   static inline void print_with_color(OSTREAM& out, const POINT& p) {
00050     out<<p.x()<<" "<<p.y()<<" "<<p.z()<<" 255 75 75";
00051   }
00052 };
00053 
00054 TMPL
00055 class point : public SHAPE_OF(V)
00056 {
00057 public:
00058   typedef C   Scalar;
00059   typedef typename SHAPE_OF(V) Shape;
00060 
00061   point(void) ;
00062   point(Scalar x, Scalar y, Scalar z= 0) ;
00063   point(const SELF & p) ;
00064   
00065   inline Scalar  x(void) const { return m_x ; }
00066   inline Scalar& x(void)       { return m_x ; }
00067   inline Scalar  y(void) const { return m_y ; }
00068   inline Scalar& y(void)       { return m_y ; }
00069   inline Scalar  z(void) const { return m_z ; }
00070   inline Scalar& z(void)       { return m_z ; }
00071 
00072   Scalar   operator [] (const int & i) const ;
00073   Scalar & operator [] (const int & i) ;
00074 
00075   inline void setx(Scalar x) { this->m_x = x ; }
00076   inline void sety(Scalar y) { this->m_y = y ; }
00077   inline void setz(Scalar z) { this->m_z = z ; }
00078 
00079   bool     operator == (const point & other) const ;
00080   bool     operator != (const point & other) const ;
00081   SELF &  operator  = (const point & other) ;
00082 
00083 
00084 private:
00085   Scalar m_x ;
00086   Scalar m_y ;
00087   Scalar m_z ;
00088 };
00089 
00090 TMPL
00091 SELF::point(void) : Shape()
00092 {
00093   this->m_x = (Scalar)0 ;
00094   this->m_y = (Scalar)0 ;
00095   this->m_z = (Scalar)0 ;
00096 }
00097 
00098 TMPL
00099 SELF::point(Scalar x, Scalar y, Scalar z) : Shape()
00100 {
00101   this->m_x = x ;
00102   this->m_y = y ;
00103   this->m_z = z ;
00104 }
00105 TMPL
00106 SELF::point(const SELF & other) : Shape()
00107 {
00108   this->m_x = other.x() ;
00109   this->m_y = other.y() ;
00110   this->m_z = other.z() ;
00111 }
00112 
00113 TMPL
00114 SELF & SELF::operator = (const SELF & other) 
00115 {
00116   if(this == &other)
00117     return *this ;
00118   
00119   this->m_x = other.x() ;
00120   this->m_y = other.y() ;
00121   this->m_z = other.z() ;
00122   
00123   return * this ;
00124 }
00125 
00126 TMPL bool 
00127 SELF::operator == (const SELF & other) const {
00128   return ((m_x == other.x()) && (m_y == other.y()) && (m_z == other.z())) ;
00129 }
00130 
00131 TMPL bool 
00132 SELF::operator != (const SELF & other) const {
00133   return ((m_x != other.x()) || (m_y != other.y()) || (m_z != other.z())) ;
00134 }
00135 
00136 TMPL typename SELF::Scalar & SELF::operator [] (const int & i) {
00137   switch(i) {
00138   case 0:
00139     return m_x ; 
00140     break ;
00141   case 1:
00142     return m_y ;
00143         break ;
00144   case 2:
00145     return m_z ;
00146     break ;
00147   default:
00148         break ;
00149   }
00150   
00151   return *(new Scalar(0)) ;
00152 }
00153 
00154 TMPL typename SELF::Scalar
00155 SELF::operator [] (const int & i) const {
00156   switch(i) {
00157   case 0:
00158     return m_x ; 
00159     break ;
00160   case 1:
00161     return m_y ;
00162         break ;
00163   case 2:
00164     return m_z ;
00165     break ;
00166     default:
00167       break ;
00168   }
00169   
00170   return (Scalar)0 ;
00171 }
00172 
00173 TMPL inline typename SELF::Scalar read (const SELF& v, unsigned i) {
00174  return v[i]; 
00175 }
00176 
00177 TMPL inline typename SELF::Scalar distance (const SELF& p1, const SELF& p2) {
00178   typename SELF::Scalar d=0;
00179   d += (p1.x()-p2.x())*(p1.x()-p2.x());
00180   d += (p1.y()-p2.y())*(p1.y()-p2.y());
00181   d += (p1.z()-p2.z())*(p1.z()-p2.z());
00182   return std::sqrt(d);
00183 }
00184 
00185 //--------------------------------------------------------------------
00186 TMPL Ostream&
00187 operator<<(Ostream& os, const SELF& p) {
00188   os <<p.x()<<(char *)" "<<p.y()<<(char *)" "<<p.z();
00189   return os;
00190 }
00191 //--------------------------------------------------------------------
00192 template<class O, class W> struct viewer; struct axel;
00193 template<class W, class C, class V, int N>
00194 Viewer&
00195 operator<<(Viewer& os, const SELF& p) {
00196   os<<"<point color=\""<<(int)(255*os.color.r)<<" "<<(int)(255*os.color.g)<<" "<<(int)(255*os.color.b)<<"\">"
00197     <<p.x()<<" "<<p.y()<<" "<<p.z()
00198     <<"</point>\n";
00199   return os;
00200 }
00201 //--------------------------------------------------------------------
00202 
00203 } ; // namespace mmx
00204 } ; // namespace shape
00205 //====================================================================
00206 # undef TMPL_DEF
00207 # undef TMPL
00208 # undef TMPL1
00209 # undef SELF
00210 # undef Scalar
00211 # undef Viewer
00212 # undef Ostream
00213 # endif // shape_point_hpp