00001 #ifndef NMWR_GB_GENERAL_COORDS_TEMPLATES_H
00002 #define NMWR_GB_GENERAL_COORDS_TEMPLATES_H
00003
00004
00005
00006
00007
00008 #include <iostream.h>
00009 #include "Geometry/point-traits.h"
00010
00011 #ifndef NO_COORD_SPECIAL
00012
00013 #include "Geometry/coords_2_special.h"
00014 #include "Geometry/coords_3_special.h"
00015
00016 #endif
00017
00018 #include "Geometry/algebraic-primitives.h"
00019
00020
00046
00047
00048 typedef double coord_N_component;
00049
00050 template<unsigned N>
00051 class coordN {
00052 public:
00053 typedef double component;
00054 typedef unsigned index;
00055 typedef coordN<N> self;
00056
00057 coordN() {}
00058 coordN(const component& x) { for(index i=0; i<N; i++) X[i] = x;}
00059 coordN(const component Y[]) { for(index i=0; i<N; i++) X[i] = Y[i];}
00060 coordN(const self& rhs) { for(index i=0; i<N; i++) X[i] = rhs.X[i];}
00061 ~coordN() {}
00062
00063 const component& operator[](index i) const { _c(i); return X[--i];}
00064 component& operator[](index i) { _c(i); return X[--i];}
00065 const component& operator()(index i) const { _c(i); return X[--i];}
00066 component& operator()(index i) { _c(i); return X[--i];}
00067
00068 void _c(index i) const { REQUIRE ( (1 <= i && i <= N), "i = " << i << '\n',1);}
00069
00070 self& operator+=(const self& rhs) { for(index i=0; i<N; i++) X[i] += rhs.X[i]; return *this; }
00071 self& operator-=(const self& rhs) { for(index i=0; i<N; i++) X[i] -= rhs.X[i]; return *this; }
00072 self& operator*=(const component& rhs) { for(index i=0; i<N; i++) X[i] *= rhs; return *this; }
00073 self& operator/=(const component& rhs) { for(index i=0; i<N; i++) X[i] /= rhs; return *this; }
00074
00075 static self origin() { return self(component(0));}
00076 static self Origin() { return self(component(0));}
00077 static index dim() { return N;}
00078 private:
00079 component X[N];
00080 };
00081
00082
00083
00084 template<unsigned N>
00085 inline
00086 bool operator==(const coordN<N>& lhs, const coordN<N> rhs)
00087 { for(unsigned int i=1; i<=N; i++) if (lhs[i]!=rhs[i]) return false;
00088 return true; }
00089
00090 template<unsigned N>
00091 inline bool operator!=(const coordN<N>& lhs, const coordN<N> rhs)
00092 { return !(lhs == rhs);}
00093
00094 template<unsigned N>
00095 inline coordN<N> operator+(const coordN<N>& lhs, const coordN<N>& rhs)
00096 { coordN<N> tmp(lhs); return (tmp+= rhs);}
00097
00098 template<unsigned N>
00099 inline coordN<N> operator-(const coordN<N>& lhs, const coordN<N>& rhs)
00100 { coordN<N> tmp(lhs); return(tmp-= rhs);}
00101
00102 template<unsigned N>
00103 inline coordN<N> operator*(const coordN<N>& lhs, const coord_N_component& rhs)
00104 { coordN<N> tmp(lhs); return (tmp *= rhs);}
00105
00106 template<unsigned N>
00107 inline coordN<N> operator*( const coord_N_component& lhs, const coordN<N>& rhs)
00108 { return rhs*lhs;}
00109
00110 template<unsigned N>
00111 inline coordN<N> operator/(const coordN<N>& lhs, const coord_N_component& rhs)
00112 { coordN<N> tmp(lhs); return (tmp /= rhs); }
00113
00114
00115 template<unsigned N>
00116 inline coordN<N> operator-(const coordN<N>& rhs)
00117 { coordN<N> tmp(rhs); return(tmp *= -1);}
00118
00119
00120 template<unsigned N>
00121 inline std::ostream& operator<<(std::ostream& out, const coordN<N>& P)
00122 {
00123 for(unsigned i = 1; i<= N-1; i++)
00124 out << P[i] << ' ';
00125 out << P[N];
00126 return out;
00127 }
00128
00129
00130 template<unsigned N>
00131 inline std::istream& operator>>(std::istream& in, coordN<N>& P)
00132 {
00133 for(unsigned i = 1; i<= N; i++)
00134 in >> P[i];
00135 return in;
00136 }
00137
00138
00139 template<unsigned N>
00140 struct point_traits_for_coordN
00141 : public point_traits_base<coordN<N> > {
00142
00143
00144 typedef typename dim_tag<N>::dimension_tag dimension_tag;
00145 typedef coordN<N> Ptype;
00146 typedef typename Ptype::component component_type;
00147 typedef component_type value_type;
00148
00149 static int LowerIndex(const Ptype&) { return 1;}
00150 static int UpperIndex(const Ptype&) { return N;}
00151
00152 static unsigned Dim (const Ptype&) { return N;}
00153
00154
00155 static unsigned Dim () { return N;}
00156
00157 static void ConstructWithDim(unsigned, Ptype&) {}
00158
00159
00160 static Ptype Origin() { return Ptype(0.0);}
00161 static Ptype Origin(unsigned) { return Ptype(0.0);}
00162
00163 static component_type x(const Ptype& p) {return p[1];}
00164 static component_type& x( Ptype& p) {return p[1];}
00165 static component_type y(const Ptype& p) {return p[2];}
00166 static component_type& y( Ptype& p) {return p[2];}
00167 static component_type z(const Ptype& p) {return p[3];}
00168 static component_type& z( Ptype& p) {return p[3];}
00169
00170
00171 };
00172
00173
00174 struct point_traits_for_coordN_2
00175 : public point_traits_base<coordN<2> > {
00176 typedef coordN<2> Ptype;
00177 typedef Ptype::component component_type;
00178 typedef component_type value_type;
00179 typedef tag2D dimension_tag;
00180
00181 static int LowerIndex(const Ptype&) { return 1;}
00182 static int UpperIndex(const Ptype&) { return 2;}
00183
00184 static unsigned Dim (const Ptype&) { return 2;}
00185
00186
00187 static unsigned Dim() { return 2;}
00188 static void ConstructWithDim(unsigned, Ptype&) {}
00189
00190
00191 static Ptype Origin() { return Ptype(0.0);}
00192 static Ptype Origin(unsigned) { return Ptype(0.0);}
00193
00194 static component_type x(const Ptype& p) {return p[1];}
00195 static component_type& x( Ptype& p) {return p[1];}
00196 static component_type y(const Ptype& p) {return p[2];}
00197 static component_type& y( Ptype& p) {return p[2];}
00198 static component_type z(const Ptype& ) {return 0;}
00199
00200 static const char* name() { return "coord<2>"; }
00201 };
00202
00203 template<unsigned N>
00204 struct point_traits<coordN<N> > : public point_traits_for_coordN<N> {};
00205
00206 template<>
00207 struct point_traits<coordN<2> > : public point_traits_for_coordN_2 {};
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 #endif