00001
00002
00003
00004
00005
00006
00007 #ifndef synaps_topology_point_H
00008 #define synaps_topology_point_H
00009
00010 #include <vector>
00011 #include <synaps/init.h>
00012 #include <synaps/base/shared_object.h>
00013
00014
00015
00016 __BEGIN_NAMESPACE_SYNAPS
00017
00019
00027 namespace topology
00028 {
00030
00043 template<class C, class R = shared_object<std::vector<C> > >
00044 struct point
00045 {
00046 typedef unsigned size_type;
00047 typedef typename ReferTo<R>::value_type::iterator iterator;
00048 typedef typename ReferTo<R>::value_type::const_iterator const_iterator;
00049
00051 typedef typename ReferTo<C>::value_type coeff_t;
00052
00056 R data;
00057
00059 point():data() {}
00060
00062 ~point() {}
00063
00069 point(const R & r): data(r) {}
00070
00072 point(const point<C,R> & P): data(P.data) {}
00073
00081 point(unsigned d, C* t):data()
00082 {
00083 rep(data).resize(d);
00084 iterator it = rep(data).begin();
00085 C* v=t;
00086 for(unsigned i = 0; i < d; ++i) *it++=*v++;
00087 }
00088
00093 point(unsigned d, char *str):data()
00094 {
00095 rep(data).resize(d);
00096 std::istringstream ins(str);
00097 for(unsigned i = 0; i < d; ++i) ins >> rep(data)[i];
00098 }
00099
00101 point(unsigned d):data()
00102 {
00103 rep(data).resize(d);
00104 }
00105
00113 point(const C* b, const C* e):data()
00114 {
00115 rep(data).resize(std::distance(b,e));
00116 iterator it = rep(data).begin();
00117 const C* tt = b;
00118 for(;tt != e; ) *it++ = *tt++;
00119 }
00120
00125 point(const coeff_t & x, const coeff_t & y)
00126 {
00127 rep(data).resize(2);
00128 rep(data)[0] = x;
00129 rep(data)[1] = y;
00130 }
00131
00136 point(const coeff_t & x, const coeff_t & y, const coeff_t & z)
00137 {
00138 rep(data).resize(3);
00139 rep(data)[0] = x;
00140 rep(data)[1] = y;
00141 rep(data)[2] = z;
00142 }
00143
00147 typename ReferTo<R>::value_type::size_type size() const
00148 { return rep(data).size(); }
00149
00150
00151 iterator begin() {return rep(data).begin();}
00152 const_iterator begin() const {return rep(data).begin();}
00153 iterator end() {return rep(data).end();}
00154 const_iterator end() const {return rep(data).end();}
00155
00156
00162 void resize(int d)
00163 { rep(data).resize(d); }
00164
00166 C operator[](unsigned i) const
00167 {if(i<size()) return (rep(data))[i]; else return C(0); }
00168
00170 C & operator[](unsigned i) {assert(i<size()); return (rep(data))[i];}
00171
00173 bool operator==(const point<C,R>& p) const {return this == &p;}
00174
00176 bool operator!=(const point<C,R>& p) {return !(rep(data)==rep(p.data));}
00177
00179 point<C,R> operator=(const point<C,R>& p)
00180 {
00181 rep(data).resize(rep(p.data).size());
00182 for(int i = 0; i < (int)rep(data).size(); ++i)
00183 rep(data)[i] = rep(p.data)[i];
00184 return (*this);
00185 }
00186
00188 point<C,R> operator+(point<C,R>& p) const
00189 {
00190 assert(rep(data).size() == rep(p.data).size());
00191 C* tab = new C[rep(data).size()];
00192 for(int i = 0; i < rep(data).size(); ++i)
00193 rep(tab)[i] = rep(data)[i]+rep(p.data)[i];
00194 return point(rep(data).size(),rep(tab));
00195 }
00196
00198 point<C,R> operator+=(point<C,R>& p)
00199 {
00200 assert(rep(data).size() == rep(p.data).size());
00201 for(int i = 0; i < rep(data).size(); ++i)
00202 rep(data)[i] += rep(p.data)[i];
00203 return *this;
00204 }
00205
00207 point<C,R> operator-(point<C,R>& p) const
00208 {
00209 assert(rep(data).size() == rep(p.data).size());
00210 C* tab = new C[rep(data).size()];
00211 for(int i = 0; i < rep(data).size(); ++i)
00212 rep(tab)[i] = rep(data)[i]-rep(p.data)[i];
00213 return point(rep(data).size(),rep(tab));
00214 }
00215
00217 point<C,R> operator-=(point<C,R>& p)
00218 {
00219 assert(rep(data).size() == rep(p.data).size());
00220 for(int i = 0; i < rep(data).size(); ++i)
00221 rep(data)[i] -= rep(p.data)[i];
00222 return *this;
00223 }
00224
00226 point<C,R> operator-() const
00227 {
00228 C* tab = new C[rep(data).size()];
00229 for(int i = 0; i < rep(data).size(); ++i)
00230 rep(tab)[i] = -rep(data)[i];
00231 return point(rep(data).size(),rep(tab));
00232 }
00233
00235 point<C,R> operator+() const {return this;}
00236
00238 point<C,R> operator*(C cte) const
00239 {
00240 C* tab = new C[rep(data).size()];
00241 for(int i = 0; i < rep(data).size(); ++i)
00242 rep(tab)[i] = rep(data)[i]*cte;
00243 return point(rep(data).size(),rep(tab));
00244 }
00245
00247 point<C,R> operator/(C cte) const
00248 {
00249 C* tab = new C[rep(data).size()];
00250 for(int i = 0; i < rep(data).size(); ++i)
00251 rep(tab)[i] = rep(data)[i]/cte;
00252 return point(rep(data).size(),rep(tab));
00253 }
00254
00256 friend point<C,R> operator*(C cte, point<C,R>& p) {return p*cte;}
00257
00259 friend point<C,R> operator/(C cte, point<C,R>& p) {return p/cte;}
00260
00267 bool operator<(const point<C,R>& p) const
00268 {
00269 C eps = 1e-5;
00270 for(int j = 0; j < (int)rep(data).size(); ++j)
00271 {
00272 if (rep(p.data)[j]>rep(data)[j]+eps)
00273 return true;
00274 else if(rep(p.data)[j]<rep(data)[j]-eps)
00275 return false;
00276 }
00277 return false;
00278 }
00279
00280 };
00281
00292 template<class C,class R>
00293 int
00294 ptcmp(const topology::point<C,R> & p1, const topology::point<C,R> & p2)
00295 {
00296 assert( p1.size() == p2.size() );
00297 for(int i = 0; i < (int)p1.size(); ++i)
00298 {
00299 if ( (rep(p1.data)[i] > rep(p2.data)[i]))
00300 return 1;
00301 else if ( (rep(p1.data)[i] < rep(p2.data)[i]))
00302 return -1;
00303 }
00304 return 0;
00305 }
00306
00307 #ifdef synaps_HAVE_LIBGMP
00308
00312 template<class R>
00313 std::ostream &
00314 operator<<(std::ostream & os, const topology::point<QQ,R>& P)
00315 {
00316 os <<"Point(";
00317 if((int)rep(P.data).size())
00318 {
00319 int i=0;
00320 os<<P[0].get_d();
00321 for(int i = 1; i < (int)rep(P.data).size(); ++i)
00322 os <<","<< P[i].get_d();
00323 }
00324 os <<")";
00325 return os;
00326 }
00327
00332 template<class R>
00333 std::ostream &
00334 operator<<(std::ostream & os, const topology::point<RR,R>& P)
00335 {
00336 os <<"Point(";
00337 if((int)rep(P.data).size())
00338 {
00339 int i=0;
00340 os<<P[0].get_d();
00341 for(int i = 1; i < (int)rep(P.data).size(); ++i)
00342 os <<","<< P[i].get_d();
00343 }
00344 os <<")";
00345 return os;
00346 }
00347
00348 #endif // synaps_HAVE_LIBGMP
00349
00354 template<class C, class R>
00355 std::ostream &
00356 operator<<(std::ostream & os, const topology::point<C,R>& P)
00357 {
00358 os <<"Point(";
00359 if((int)rep(P.data).size())
00360 {
00361 os<<P[0];
00362 for(int i = 1; i < (int)rep(P.data).size(); ++i)
00363 os <<","<< P[i];
00364 }
00365 os <<")";
00366 return os;
00367 }
00368
00373 template<class C, class R>
00374 std::istream &
00375 operator>>(std::istream & is, topology::point<C,R>& P)
00376 {
00377 rep(P.data).resize(3);
00378 is>>P[0]>>P[1]>>P[2];
00379 return is;
00380 }
00381
00382 }
00383
00384
00385
00386 __END_NAMESPACE_SYNAPS
00387
00388 #endif // synaps_topology_point_H