proj home

Files   Classes   Functions   Hierarchy  

point.h

Go to the documentation of this file.
00001 #ifndef POINT_H
00002 #define POINT_H
00003 
00004 #include <cassert>
00005 #include <vector>
00006 #include <iostream>
00007 #include <string>
00008 #include <cmath>
00009 #include <sstream>
00010 using namespace std;
00011 
00012 #include <typedefs.h>
00013 
00014 // TODO -= issues: general template conflicting with same type.
00015 
00016 //  Multiplication and Memory Management Coupled,
00017 //  take care with writing expressions.
00018 //
00019 //  A good compiler should precompile the large include headers.
00020 
00030 template< typename T>
00031 class point2
00032 {
00033 public:
00034   
00036   T x;
00038   T y;
00039   
00041   point2() : x(0), y(0) {}
00043   point2(T x0, T y0)  : x(x0), y(y0) {}
00044 
00046   operator stringc () const
00047   { 
00048     string s;
00049     { stringstream ss; ss << x; s+=ss.str(); }
00050     s += " ";
00051     { stringstream ss; ss << y; s+=ss.str(); }
00052     return s;
00053   }
00055   void serializeInverse(stringc & s)
00056     {  stringstream ss(s); ss >> x >> y; }
00059   void serializeInverseBrackets(stringc & s);
00061   ostream& print(ostream& os) const
00062     { return os << x << " " << y; }
00064   istream & serializeInverse(istream & istr)
00065     { return istr >> x >> y; }
00066 
00067 
00068   // Arithmetic.
00070   point2<T>& operator += (point2<T> const & p) 
00071     { x+=p.x; y+=p.y; return *this; }
00073   point2<T> const operator + (point2<T> const & p) const
00074     { point2<T> z(*this); z+=p; return z; }
00076   point2<T>& operator -= (point2<T> const & p) 
00077     { x-=p.x; y-=p.y; return *this; }
00079   point2<T> const operator - (point2<T> const & p) const
00080     //{ point2<T> z(*this); z-=p; return z; } seg faulted?
00081     { return point2<T>(this->x-p.x,this->y-p.y); } // seg faulted too.
00083   point2<T>& operator *= (point2<T> const & p) 
00084     { x*=p.x; y*=p.y; return *this; }
00086   point2<T> const operator * (point2<T> const & p) const
00087     { point2<T> z(*this); z*=p; return z; }
00089   point2<T>& operator /= (point2<T> const & p) 
00090     { x/=p.x; y/=p.y; return *this; }
00092   point2<T> const operator / (point2<T> const & p) const
00093     { point2<T> z(*this); z/=p; return z; }
00094 
00095 
00097   bool const operator == (point2<T> const & p) const 
00098     { return (x==p.x)&&(y==p.y); }
00099 
00101   template< typename W>
00102   point2<T>& operator += (W const & w) 
00103     { x+=w; y+=w; return *this; }
00105   template< typename W >
00106   point2<T> const operator + (W const & w) const
00107     { point2<T> z(*this); z.x+=w; z.y+=w; return z; }
00108   template< typename W>
00110   point2<T>& operator -= (W const & w) 
00111     { x-=w; y-=w; return *this; }
00113   template< typename W >
00114   point2<T> const operator - (W const & w) const
00115     { point2<T> z(*this); z.x-=w; z.y-=w; return z; }
00117   template< typename W>
00118   point2<T>& operator *= (W const & w) 
00119     { x*=w; y*=w; return *this; }
00121   template< typename W >
00122   point2<T> const operator * (W const & p) const
00123     { point2<T> z(*this); z.x*=p; z.y*=p; return z; }
00125   template< typename W>
00126   point2<T>& operator /= (W const & w) 
00127     { x/=w; y/=w; return *this; }
00129   template< typename W >
00130   point2<T> const operator / (W const & p) const
00131     { point2<T> z(*this); z.x/=p; z.y/=p; return z; }
00133   template< typename W >
00134   boolc operator != (W const & w) const
00135     { if (x!=w.x) return true; if (y!=w.y) return true; return false; }
00136 
00137   // Access.
00139   T const & operator[](uintc i) const
00140     { assert(i<2); if ((i%2)==0) return x; return y; }
00142   T& operator[](uintc i)
00143     { assert(i<2); if ((i%2)==0) return x; return y; }
00144 
00145   // Misc
00146 
00148   void rotate90()
00149     { T t=x; x=-y; y=t; }
00150 
00152   T const dot() const 
00153     { return x*x + y*y; }
00154 
00156   T const dot( point2<T> const & w ) const
00157     { return x*w.x + y*w.y; }
00158 
00160   T const distance() const
00161     { return sqrt(x*x + y*y); }
00162 
00164   void normalize()
00165     { assert(distance()!=0); *this *= 1.0/distance(); }
00166 
00168   template< typename Z >
00169   Z const sumInRatio( Z const & A, Z const & B ) const
00170     { assert(x+y!=0); return (A*x+B*y)/(x+y); }
00171 
00173   boolc isinsideunitbox() const
00174   {
00175     if (x<(T)0)
00176       return false;
00177     if (y<(T)0)
00178       return false;
00179  
00180     if (x>(T)1)
00181       return false;
00182     if (y>(T)1)
00183       return false;
00184 
00185     return true;
00186   }
00187 
00188 //  point2<T> & operator = (point2<T> const & B)
00189 //    { x=B.x; y=B.y; return *this; }
00190 };
00191 
00192 //template< typename T >
00193 //point2<T> & operator = (point2<T> & A, point2<T> const & B)
00194 //{
00195 //  A = B;
00196 //  return A;
00197 //}
00198 
00200 template< typename T>
00201 ostream& operator << (ostream& os, point2<T> const & p) 
00202   { return p.print(os); }
00203 
00205 template< typename T >
00206 istream & operator >> (istream & istr, point2<T> & p)
00207   { return p.serializeInverse(istr); }
00208 
00209 
00212 //template< typename T>
00213 //bool const operator < (point2<T> const & a, point2<T> const & p)
00214 //  { if (p.x==0) return true; return a.y < p.y/p.x*a.x; }
00215 
00218 template< typename T>
00219 bool const operator > (point2<T> const & a, point2<T> const & p)
00220   { if (p.x==0) return false; return a.y > p.y/p.x*a.x; }
00221 
00222 
00223 
00231 template< typename T>
00232 class point3
00233 {
00234 public:
00235 
00237   T x;
00239   T y;
00241   T z;
00242 
00244   point3()  
00245     {x = y = z = 0; }
00247   point3(T x0, T y0, T z0) 
00248     : x(x0), y(y0), z(z0) {}
00250   point3(point2<T> const & p) : x(p.x), y(p.y), z(0) {}
00251 
00253   operator stringc () const
00254   { 
00255     string s;
00256     { stringstream ss; ss << x; s+=ss.str(); }
00257     s += " ";
00258     { stringstream ss; ss << y; s+=ss.str(); }
00259     s += " ";
00260     { stringstream ss; ss << z; s+=ss.str(); }
00261     return s;
00262   }
00263 
00265   void serializeInverse(stringc & s)
00266     { stringstream ss(s); ss >> x >> y >> z; }
00269   void serializeInverseBrackets(stringc & s);
00270 
00272   ostream& print(ostream& os) const
00273     { return os << x << " " << y << " " << z; }
00275   istream & serializeInverse(istream & istr)
00276     { return istr >> x >> y >> z; }
00277 
00279   static void readin
00280   (
00281     vector< point3<T> >& v, 
00282     T const * abeg, 
00283     T const * aend
00284   );
00285 
00286   // Arithmetic.
00288   point3<T>& operator += (point3<T> const & p)
00289     { x+=p.x; y+=p.y; z+=p.z; return *this; }
00291   point3<T> const operator + (point3<T> const & p) const
00292     { point3<T> z(*this); z+=p; return z; } 
00294   point3<T>& operator -= (point3<T> const & p)
00295     { x-=p.x; y-=p.y; z-=p.z; return *this; }
00297   point3<T> const operator - (point3<T> const & p) const
00298     { point3<T> z(*this); z-=p; return z; }
00300   point3<T>& operator *= (point3<T> const & p)
00301     { x*=p.x; y*=p.y; z*=p.z; return *this; }
00303   point3<T> const operator * (point3<T> const & p) const
00304     { point3<T> t(*this); t*= p; return t; }
00306   point3<T>& operator /= (point3<T> const & p)
00307     { x/=p.x; y/=p.y; z/=p.z; return *this; }
00309   point3<T> const operator / (point3<T> const & p) const
00310     { point3<T> t(*this); t/=p; return t; }
00311 
00313   bool const operator == (point3<T> const & p) const 
00314     { return (x==p.x)&&(y==p.y)&&(z==p.z); }
00315 
00317   template< typename W >
00318   point3<T>& operator += (W const & w)
00319     { x+=w; y+=w; z+=w; return *this; }
00321   template< typename W >
00322   point3<T> const operator + (W const & w) const
00323     { point3<T> t(*this); t.x+=w; t.y+=w; t.z+=w; return t; }
00325   template< typename W >
00326   point3<T>& operator -= (W const & w)
00327     { x-=w; y-=w; z-=w; return *this; }
00329   template< typename W >
00330   point3<T> const operator - (W const & w) const
00331     { point3<T> t(*this); t.x-=w; t.y-=w; t.z-=w; return t; }
00333   template< typename W >
00334   point3<T>& operator *= (W const & w)
00335     { x*=w; y*=w; z*=w; return *this; }
00337   template< typename W >
00338   point3<T> const operator * (W const & w) const
00339     { point3<T> t(*this); t.x*=w; t.y*=w; t.z*=w; return t; }
00341   template< typename W >
00342   point3<T>& operator /= (W const & w)
00343     { x/=w; y/=w; z/=w; return *this; }
00345   template< typename W >
00346   point3<T> const operator / (W const & w) const
00347     { point3<T> t(*this); t.x/=w; t.y/=w; t.z/=w; return t; }
00349   template< typename W >
00350   boolc operator != (W const & w) const
00351     { if (x!=w.x) return true; if (y!=w.y) return true; 
00352       if (z!=w.z) return true; return false; }
00353 
00354   // Access.
00356   T const & operator[](uintc i) const
00357     { uint k(i); k %= 3; if(k==0) return x; if(k==1) return y; return z; } 
00358   T& operator[](uintc i)
00359     { uint k(i); k %= 3; if(k==0) return x; if(k==1) return y; return z; }
00360 
00361   T const distance() const 
00362     { return sqrt(x*x + y*y + z*z); }
00363 
00364   T const distanceabs() const
00365     { return abs(x)+abs(y)+abs(z); }
00366   T const collapse() const
00367     { return x + y + z; }
00368 
00370   T const dot() const 
00371     { return x*x + y*y + z*z; }
00373   T const dot( point3<T> const & w) const
00374     { return x*w.x + y*w.y + z*w.z; }
00375 
00377   void normalize()
00378     { assert(distance()!=0); *this *= 1.0/distance(); }
00379 
00382   void crossproduct
00383   ( 
00384     point3<T> const & u,
00385     point3<T> const & v
00386   )
00387   {
00388     x=u.y*v.z-v.y*u.z;
00389     y=u.z*v.x-u.x*v.z;
00390     z=u.x*v.y-u.y*v.x;
00391   }
00392 
00394   operator point2<T> const () const 
00395     { return point2<T>(x,y); }
00396 
00398   boolc isinsideunitbox() const
00399   {
00400     if (x<(T)0)
00401       return false;
00402     if (y<(T)0)
00403       return false;
00404     if (z<(T)0)
00405       return false;
00406  
00407     if (x>(T)1)
00408       return false;
00409     if (y>(T)1)
00410       return false;
00411     if (z>(T)1)
00412       return false;
00413 
00414     return true;
00415   }
00416 
00417 };
00418 
00419 template< typename T>
00420 ostream& operator << (ostream& os, point3<T> const & p)
00421   { return p.print(os); }
00422 
00423 template< typename T >
00424 istream & operator >> (istream & istr, point3<T> & p)
00425   { return p.serializeInverse(istr); }
00426 
00427 
00428 
00432 template< typename T>
00433 class point4
00434 {
00435 public:
00436 
00437   T x;
00438   T y;
00439   T z;
00440   T w;
00441 
00442   point4()  
00443     {x = y = z = w = 0; }
00444   point4(T x0, T y0, T z0, T w0) 
00445     : x(x0), y(y0), z(z0), w(w0) {}
00446   point4(point4<T> const & p) : x(p.x), y(p.y), z(p.z), w(p.w) {}
00447 
00449   operator stringc () const
00450   { 
00451     string s;
00452     { stringstream ss; ss << x; s+=ss.str(); }
00453     s += " ";
00454     { stringstream ss; ss << y; s+=ss.str(); }
00455     s += " ";
00456     { stringstream ss; ss << z; s+=ss.str(); }
00457     s += " ";
00458     { stringstream ss; ss << w; s+=ss.str(); }
00459     return s;
00460   }
00461   void serializeInverse(stringc & s)
00462     {  stringstream ss(s); ss >> x >> y >> z >> w; }
00463   ostream & print(ostream & os) const
00464     { return os << x << " " << y << " " << z << " " << w; }
00465   istream & serializeInverse(istream & istr)
00466     { return istr >> x >> y >> z >> w; }
00467 
00468   // Arithmetic.
00469   point4<T>& operator += (point4<T> const & p)
00470     { x+=p.x; y+=p.y; z+=p.z; w+=p.w; return *this; }
00471   point4<T> operator + (point4<T> const & p) const
00472     { point4<T> z(*this); z+=p; return z; } 
00473   point4<T>& operator -= (point4<T> const & p)
00474     { x-=p.x; y-=p.y; z-=p.z; w-=p.w; return *this; }
00475   point4<T> const operator -= (point4<T> const & p) const
00476     { point4<T> z(*this); z-=p; return z; }
00477   point4<T>& operator *= (point4<T> const & p)
00478     { x*=p.x; y*=p.y; z*=p.z; w*=p.w; return *this; }
00479   point4<T> const operator * (point4<T> const & p) const
00480     { point4<T> t(*this); t*=p; return t; }
00481   point4<T>& operator /= (point4<T> const & p)
00482     { x/=p.x; y/=p.y; z/=p.z; w/=p.w; return *this; }
00483   point4<T> const operator / (point4<T> const & p) const
00484     { point4<T> t(*this); t/=p; return t; }
00485 
00486   bool const operator == (point4<T> const & p) const 
00487     { return (x==p.x)&&(y==p.y)&&(z==p.z)&&(w==p.w); }
00488 
00489   template< typename W >
00490   point4<T>& operator += (W const & q)
00491     { x+=q; y+=q; z+=q; w+=q; return *this; }
00492   template< typename W >
00493   point4<T> const operator + (W const & q) const
00494     { point4<T> t(*this); t.x+=q; t.y+=q; t.z+=q; t.w+=q; return t; }
00495 //  template< typename W >
00496 //  point4<T>& operator -= (W const & q)
00497 //    { x-=q; y-=q; z-=q; w-=q; return *this; }
00498 //  template< typename W >
00499 //  point4<T> const operator - (W const & q) const
00500 //    { point4<T> t(*this); t.x-=q; t.y-=q; t.z-=q; t.w-=q; return t; }
00501   template< typename W >
00502   point4<T>& operator *= (W const & q)
00503     { x*=q; y*=q; z*=q; w*=q; return *this; }
00504   template< typename W >
00505   point4<T> const operator * (W const & q) const
00506     { point4<T> t(*this); t.x*=q; t.y*=q; t.z*=q; t.w*=q; return t; }
00507   template< typename W >
00508   point4<T>& operator /= (W const & q)
00509     { x/=q; y/=q; z/=q; w/=q; return *this; }
00510   template< typename W >
00511   point4<T> const operator / (W const & q) const
00512     { point4<T> t(*this); t.x/=q; t.y/=q; t.z/=q; t.w/=q; return t; }
00514   template< typename W >
00515   boolc operator != (W const & q) const
00516     { if (x!=q.x) return true; if (y!=q.y) return true; 
00517       if (z!=q.z) return true; if (w!=q.w) return true; return false; }
00518 
00519   T const dot() const
00520     { return x*x + y*y + z*z + w*w; }
00521   T const dot( point4<T> const & q ) const
00522     { return x*q.x + y*q.y + z*q.z + w*q.w; }
00523 
00524 
00525   // Access.
00526   T const & operator[](uintc i) const
00527     { uint k(i); k %= 4; if(k==0) return x; 
00528       if(k==1) return y; if(k==2) return z; return w; } 
00529   T& operator[](uintc i)
00530     { uint k(i); k %= 4; if(k==0) return x; 
00531       if(k==1) return y; if(k==2) return z; return w; }
00532 
00533   // Conversion
00534   operator point3<T> const () const 
00535     { return point3<T>(x,y,z); }
00536   
00537 
00538 };
00539 
00540 template< typename T >
00541 ostream& operator << (ostream& os, point4<T> const & p)
00542   { return p.print(os); }
00543 
00544 template< typename T >
00545 istream & operator >> (istream & istr, point4<T> & p)
00546   { return p.serializeInverse(istr); }
00547 
00548 
00549 // --------------------------------------------------------  
00550 // Implementation
00551 
00552 template<class T>
00553 void point3<T>::readin
00554 (
00555   vector< point3<T> >& v, 
00556   T const * abeg, 
00557   T const * aend
00558 )
00559 {
00560   assert((aend-abeg)%3==0);
00561 
00562   uint n = (aend-abeg)/3;
00563   vector< point3<T> > v2(n);
00564   for (uint i=0; i<n; ++i)
00565   {
00566     v2[i] = point3<T>(*abeg,*(abeg+1),*(abeg+2));
00567     ++abeg; ++abeg; ++abeg;
00568   }
00569 
00570   v = v2;
00571 }
00572 
00573 template< typename T>
00574 void point2<T>::serializeInverseBrackets(stringc & s)
00575 {
00576   uintc sz(s.size());
00577   string s2(s);
00578 
00579   for (uint i=0; i<sz; ++i)
00580   {
00581     char ch = s[i];
00582     if (ch=='(')
00583       s2[i] = ' '; 
00584     if (ch==')')
00585       s2[i] = ' '; 
00586     if (ch==',')
00587       s2[i] = ' '; 
00588   }
00589 
00590   serializeInverse(s2);
00591 }
00592 
00593 template< typename T>
00594 void point3<T>::serializeInverseBrackets(stringc & s)
00595 {
00596   uintc sz(s.size());
00597   string s2(s);
00598 
00599   for (uint i=0; i<sz; ++i)
00600   {
00601     char ch = s[i];
00602     if (ch=='(')
00603       s2[i] = ' '; 
00604     if (ch==')')
00605       s2[i] = ' '; 
00606     if (ch==',')
00607       s2[i] = ' '; 
00608   }
00609 
00610   serializeInverse(s2);
00611 }
00612 
00613 #endif
00614 

Generated on Fri Mar 4 00:49:28 2011 for Chelton Evans Source by  doxygen 1.5.8