00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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, int N= 3, class V=default_env>
00021 # define TMPL template<class C, int N, class V>
00022 # define TMPL1 template<class K>
00023 # define SELF point<C,N,V>
00024 # define Viewer viewer<axel,W>
00025 # define Ostream std::ostream
00026
00027 # undef Scalar
00028
00029 namespace mmx {
00030 namespace shape {
00031
00032 TMPL_DEF class point;
00033
00034
00035 struct point_def {};
00036 template<class C> struct use<point_def,C>
00037 {
00038
00039 typedef point<C> Point;
00040
00041 template<class SEQ,class POINT>
00042 static inline void point_insertor(SEQ& vertices, POINT* p) {
00043 vertices <<p;
00044 }
00045
00046 template<class OSTREAM,class POINT>
00047 static inline void print_with_color(OSTREAM& out, const POINT& p) {
00048 out<<p.x()<<" "<<p.y()<<" "<<p.z()<<" 255 75 75";
00049 }
00050 };
00051
00052 TMPL
00053 class point : public SHAPE_OF(V)
00054 {
00055 public:
00056 typedef C Scalar;
00057 typedef typename SHAPE_OF(V) Shape;
00058
00059 point(void) ;
00060 point(Scalar x, Scalar y=0, Scalar z= 0) ;
00061 point(const SELF & p) ;
00062
00063 inline Scalar x(void) const { return m_x ; }
00064 inline Scalar& x(void) { return m_x ; }
00065 inline Scalar y(void) const { return m_y ; }
00066 inline Scalar& y(void) { return m_y ; }
00067 inline Scalar z(void) const { return m_z ; }
00068 inline Scalar& z(void) { return m_z ; }
00069
00070 Scalar operator [] (const int & i) const ;
00071 Scalar & operator [] (const int & i) ;
00072
00073 inline void setx(Scalar x) { this->m_x = x ; }
00074 inline void sety(Scalar y) { this->m_y = y ; }
00075 inline void setz(Scalar z) { this->m_z = z ; }
00076
00077 bool operator == (const point & other) const ;
00078 bool operator != (const point & other) const ;
00079 SELF & operator = (const point & other) ;
00080
00081
00082 point operator+(const point &v)const;
00083 point operator-(const point &v)const;
00084 point operator*(const Scalar&k)const;
00085 point operator/(const Scalar&k)const
00086 { return *this*( Scalar(1)/k); };
00087 point operator*(const point &v)const;
00088
00089 point operator+=(const point &v)const
00090 {return *this+v; };
00091 point operator-=(const point &v)const
00092 {return *this-v; };
00093 point operator*=(const Scalar&k)const
00094 {return *this*k; };
00095 point operator*=(const point &v)const
00096 {return *this*v; };
00097
00098 point operator-()const;
00099 point cross(const point &v) const;
00100 Scalar dot(point v2)const;
00101 Scalar dist2(point v2)const;
00102 Scalar norm() const;
00103
00104 private:
00105 Scalar m_x ;
00106 Scalar m_y ;
00107 Scalar m_z ;
00108 };
00109
00110 TMPL
00111 SELF::point(void) : Shape()
00112 {
00113 this->m_x = (Scalar)0 ;
00114 this->m_y = (Scalar)0 ;
00115 this->m_z = (Scalar)0 ;
00116 }
00117
00118 TMPL
00119 SELF::point(Scalar x, Scalar y, Scalar z) : Shape()
00120 {
00121 this->m_x = x ;
00122 this->m_y = y ;
00123 this->m_z = z ;
00124 }
00125 TMPL
00126 SELF::point(const SELF & other) : Shape()
00127 {
00128 this->m_x = other.x() ;
00129 this->m_y = other.y() ;
00130 this->m_z = other.z() ;
00131 }
00132
00133 TMPL
00134 SELF & SELF::operator = (const SELF & other)
00135 {
00136 if(this == &other)
00137 return *this ;
00138
00139 this->m_x = other.x() ;
00140 this->m_y = other.y() ;
00141 this->m_z = other.z() ;
00142
00143 return * this ;
00144 }
00145
00146 TMPL bool
00147 SELF::operator == (const SELF & other) const {
00148 return ((m_x == other.x()) && (m_y == other.y()) && (m_z == other.z())) ;
00149 }
00150
00151 TMPL bool
00152 SELF::operator != (const SELF & other) const {
00153 return ((m_x != other.x()) || (m_y != other.y()) || (m_z != other.z())) ;
00154 }
00155
00156 TMPL typename SELF::Scalar & SELF::operator [] (const int & i) {
00157 switch(i) {
00158 case 0:
00159 return m_x ;
00160 break ;
00161 case 1:
00162 return m_y ;
00163 break ;
00164 case 2:
00165 return m_z ;
00166 break ;
00167 default:
00168 break ;
00169 }
00170
00171 return *(new Scalar(0)) ;
00172 }
00173
00174 TMPL typename SELF::Scalar
00175 SELF::operator [] (const int & i) const {
00176 switch(i) {
00177 case 0:
00178 return m_x ;
00179 break ;
00180 case 1:
00181 return m_y ;
00182 break ;
00183 case 2:
00184 return m_z ;
00185 break ;
00186 default:
00187 break ;
00188 }
00189
00190 return (Scalar)0 ;
00191 }
00192
00193 TMPL
00194 SELF SELF::operator +(const SELF &v)const
00195 {
00196 SELF res;
00197 res.x() = m_x + v.x();
00198 res.y() = m_y + v.y();
00199 res.z() = m_z + v.z();
00200 return res;
00201 }
00202
00203 TMPL
00204 SELF SELF::operator -(const SELF &v)const
00205 {
00206 SELF res;
00207 res.x() = m_x - v.x();
00208 res.y() = m_y - v.y();
00209 res.z() = m_z - v.z();
00210 return res;
00211 }
00212
00213 TMPL
00214 SELF SELF::operator *(const Scalar &k)const
00215 {
00216 SELF res;
00217 res.x() = k * m_x;
00218 res.y() = k * m_y;
00219 res.z() = k * m_z;
00220 return res;
00221 }
00222
00223 TMPL
00224 SELF operator *(typename SELF::Scalar k,const SELF &v)
00225 {
00226 return v * k;
00227 }
00228
00229 TMPL
00230 SELF SELF::operator -()const
00231 {
00232 SELF res;
00233 res.x() = -m_x;
00234 res.y() = -m_y;
00235 res.z() = -m_z;
00236 return res;
00237 }
00238
00239 TMPL
00240 SELF SELF::operator *(const SELF &v)const
00241 {
00242 SELF res;
00243 res.x() = m_x * v.x();
00244 res.y() = m_y * v.y();
00245 res.z() = m_z * v.z();
00246 return res;
00247 }
00248
00249
00250 TMPL
00251 SELF SELF::cross(const SELF &v)const
00252 {
00253 SELF res;
00254 res.x() = (m_y * v.z()) - (m_z * v.y());
00255 res.y() = (m_z * v.x()) - (m_x * v.z());
00256 res.z() = (m_x * v.y()) - (m_y * v.x());
00257 return res;
00258 }
00259
00260 TMPL
00261 SELF cross(const SELF v1, const SELF v2)
00262 {
00263 return v1.cross(v2);
00264 }
00265
00266 TMPL
00267 typename SELF::Scalar SELF::dot(const SELF v2) const
00268 {
00269 return (m_x * v2.x())+(m_y * v2.y())+(m_z * v2.z());
00270 }
00271
00272 TMPL
00273 typename SELF::Scalar SELF::dist2(const SELF v2) const
00274 {
00275 return (m_x - v2.x())*(m_x - v2.x())
00276 +(m_y - v2.y())*(m_y - v2.y())
00277 +(m_z - v2.z())*(m_z - v2.z());
00278 }
00279 TMPL
00280 typename SELF::Scalar dot(const SELF v1, const SELF v2)
00281 {
00282 return v1.dot(v2);
00283 }
00284
00285 TMPL
00286 typename SELF::Scalar SELF::norm() const
00287 {
00288 return std::sqrt( this->dot(*this) );
00289 }
00290
00291 TMPL inline typename SELF::Scalar read (const SELF& v, unsigned i) {
00292 return v[i];
00293 }
00294
00295 TMPL inline typename SELF::Scalar distance (const SELF& p1, const SELF& p2) {
00296 typename SELF::Scalar d=0;
00297 d += (p1.x()-p2.x())*(p1.x()-p2.x());
00298 d += (p1.y()-p2.y())*(p1.y()-p2.y());
00299 d += (p1.z()-p2.z())*(p1.z()-p2.z());
00300 return std::sqrt(d);
00301 }
00302
00303
00304 TMPL Ostream&
00305 operator<<(Ostream& os, const SELF& p) {
00306 os <<p.x()<<(char *)" "<<p.y()<<(char *)" "<<p.z();
00307 return os;
00308 }
00309
00310 TMPL void
00311 print (Ostream& os, const SELF& p) {
00312 os <<(char *)"("<<p.x()<<(char *)","<<p.y()<<(char *)","<<p.z()<<(char *)")";
00313 }
00314
00315 template<class O, class W> struct viewer; struct axel;
00316 template<class W, class C, class V, int N>
00317 Viewer&
00318 operator<<(Viewer& os, const SELF& p) {
00319 os<<"<point color=\""<<(int)(255*os.color.r)<<" "<<(int)(255*os.color.g)<<" "<<(int)(255*os.color.b)<<"\">"
00320 <<p.x()<<" "<<p.y()<<" "<<p.z()
00321 <<"</point>\n";
00322 return os;
00323 }
00324
00325
00326 } ;
00327 } ;
00328
00329 # undef TMPL_DEF
00330 # undef TMPL
00331 # undef TMPL1
00332 # undef SELF
00333 # undef Scalar
00334 # undef Viewer
00335 # undef Ostream
00336 # endif // shape_point_hpp