Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Examples  

PolyLines.H

Go to the documentation of this file.
00001 
00015 #ifndef POLYLINES_H
00016 #define POLYLINES_H
00017 
00018 #include <Vector.H>
00019 #include <Model.H>
00020 #include <fstream.h>
00021 #include <Transform.H>
00022 
00023 
00024 namespace xchen
00025 {
00027   template <int dim=3>
00028   class PolyLine : public Model
00029   {
00030       typedef Point<double, dim> point_t;
00031       vector<point_t> pnts;
00032   public:
00033       PolyLine()                                                    {  } 
00034       PolyLine(istream& is)                                         { is >> *this; } 
00035       vector<point_t>& operator() ()                                { return pnts; }
00036 
00037       void Transform(xchen::Transform const& trans);
00038       
00039 
00040       void Draw() const;
00041 
00042       friend ostream& operator<<<>(ostream &os, const PolyLine &pl);
00043       friend istream& operator>><>(istream &is, PolyLine &pl);
00044   };
00045 
00046 
00048   template<int dim=3>
00049   class PolyLines : public Model
00050   {
00051       vector< PolyLine<dim> > branches;
00052   public:
00053       PolyLines()              {  } 
00054       PolyLines(istream& is)   { is >> *this; } 
00055 
00056       static bool FindNextPolyline( istream& is );
00057       
00058       void Draw() const;
00059 
00060       void Transform(xchen::Transform const& trans);
00061       
00062 
00063       friend ostream& operator<<<>(ostream &os, const PolyLines &pls);
00064       friend istream& operator>><>(istream &is, PolyLines &pls);
00065 
00066   private:
00067   };
00068  
00069 
00070   /*_____________________________ implementing PolyLine _________________________________ */
00071 
00072   template <int dim>
00073   ostream &operator<<(ostream& os, const PolyLine<dim>& pl)
00074   {
00075     copy(pl().begin(), pl().end(), ostream_iterator< Point<double,dim> >(os, "\t"));
00076     return os << endl;
00077   }
00078   
00079 
00080   /* input polyline format: total# [pnt1] [pnt2] .... */
00081   template <int dim>
00082   istream& operator>>(istream &is, PolyLine<dim> &pl)
00083   {
00084     int total_pnts; is >> total_pnts;
00085     pl().resize(total_pnts);
00086     char c;
00087     is >> c;
00088     if(c=='{') // attribute block.
00089     {
00090       string str;
00091       while( (c=is.get()) != '}' ) str += c;
00092       
00093       cout << "str is  : " << str << endl;
00094       pl.SetAttributes(str);
00095       cout << "attrs is \n" << pl.attrs << endl;
00096       
00097     }
00098     else is.putback(c);
00099     
00100     for(typename vector< Point<double, dim> >::iterator itr = pl().begin(); itr != pl().end(); ++itr)
00101     {
00102       is >> c >> (*itr) >> c;
00103     }
00104     return is;
00105   }
00106 
00107   template<int dim>
00108   inline void PolyLine<dim> :: Draw() const  
00109   {
00110     glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
00111     glDisable(GL_LIGHTING);
00112 
00113     glBegin(GL_LINE_STRIP);    
00114     for(typename vector< point_t >::const_iterator itr = pnts.begin(); itr != pnts.end(); ++itr)
00115     {
00116       wglVertex(*itr);
00117     }
00118     glEnd();
00119 
00120     glPopAttrib();
00121   }
00122 
00123 
00124   template<int dim>
00125   inline void PolyLine<dim> :: Transform(xchen::Transform const& trans)
00126   {
00127     for(typename vector<point_t>::iterator itr=pnts.begin(); itr!=pnts.end(); ++itr)
00128     {
00129       *itr = trans(*itr);
00130     }
00131   }
00132   
00133       
00134 
00135   /*_____________________________ implementing PolyLines _________________________________ */
00136 
00137 
00138   template<int dim>
00139   inline bool PolyLines<dim> :: FindNextPolyline( istream& is )
00140   {
00141     char buf[255];
00142     while( is >> buf ) 
00143       if( strcmp(buf, "[POLYLINE")==0)  return true; 
00144     
00145     return false;
00146   }
00147 
00148   template <int dim>
00149   istream& operator>>(istream &is, PolyLines<dim> &pls)
00150   {
00151     pls.branches.clear();
00152     do
00153     {
00154       pls.branches.push_back(PolyLine<dim>(is));
00155     } while ( pls.FindNextPolyline( is ) );
00156     
00157     return is;
00158   }
00159   template <int dim>
00160   ostream &operator<<(ostream& os, const PolyLines<dim>& pls)
00161   {
00162     copy(pls.branches.begin(), pls.branches.end(), ostream_iterator< PolyLine<dim> >(os, "\n"));
00163     return os;
00164   }
00165 
00166 
00167   template<int dim>
00168   inline void PolyLines<dim> :: Draw() const  
00169   {
00170     for(typename vector< PolyLine<dim> > :: const_iterator iter = branches.begin(); iter != branches.end(); iter++) 
00171       iter->Draw();
00172   }
00173 
00174   template<int dim>
00175   inline void PolyLines<dim> :: Transform(xchen::Transform const& trans)
00176   {
00177     for(typename vector< PolyLine<dim> >::iterator itr=branches.begin(); itr!=branches.end(); ++itr)
00178     {
00179       itr->Transform(trans);
00180     }
00181   }
00182 
00183   typedef PolyLine<> PolyLine3D;
00184   typedef PolyLines<> PolyLines3D;
00185 }
00186 
00187 
00188 #endif

Generated on Wed Apr 7 21:40:49 2004 by doxygen1.2.18