shape_doc 0.1
/Users/mourrain/Devel/mmx/shape/include/shape/tpl3d.hpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * M a t h e m a g i x
00003  *****************************************************************************
00004  * TopologyCurve
00005  * 2008-05-16
00006  * Julien Wintz & Bernard Mourrain
00007  *****************************************************************************
00008  *               Copyright (C) 2006 INRIA Sophia-Antipolis
00009  *****************************************************************************
00010  * Comments :
00011  ****************************************************************************/
00012 
00013 # ifndef shape_topology_surface_hpp_H
00014 # define shape_topology_surface_hpp_H
00015 
00016 # include <shape/graphic.hpp>
00017 # include <shape/vertex.hpp>
00018 # include <shape/edge.hpp>
00019 # include <shape/face.hpp>
00020 # include <shape/kdtree.hpp>
00021 # include <shape/list.hpp>
00022 # include <shape/topology.hpp>
00023 # include <shape/cell3d_factory.hpp>
00024 # include <algorithm>
00025 
00026 # define TMPL   template<class C,class V>
00027 # define TMPL1  template<class V>
00028 # define Viewer viewer<axel,V>
00029 # define SELF   tpl3d<C,V> 
00030 
00031 namespace mmx {
00032 namespace shape {
00033 
00034 //--------------------------------------------------------------------
00035 struct tpl3d_def {};
00036 
00037 template<> struct use<tpl3d_def>
00038   :public use<topology_def>
00039   ,public use<surface_def>
00040 {
00041 
00042   typedef graphic<double> Graphic;
00043 
00044   template<class TOPOLOGY> 
00045   static Graphic* as_graphic(const TOPOLOGY& tp) {
00046 
00047     typedef typename TOPOLOGY::Point  Point; 
00048     typedef typename TOPOLOGY::Edge   Edge;
00049     typedef typename TOPOLOGY::Face   Face;
00050   
00051     int nbi=0;
00052     
00053     if( tp.nbe()>0){
00054       nbi=2*tp.nbe();
00055       int c=0;
00056       Graphic* res = new Graphic( Graphic::E_LINE, nbi, nbi);
00057       foreach(Edge* e, tp.edges()) { 
00058         res->vertices[3*c]  =e->source()->x();
00059         res->vertices[3*c+1]=e->source()->y();
00060         res->vertices[3*c+2]=e->source()->z();
00061         c++;
00062         res->vertices[3*c]  =e->destination()->x();
00063         res->vertices[3*c+1]=e->destination()->y();
00064         res->vertices[3*c+2]=e->destination()->z();
00065         c++;
00066         res->indices[c-2]=c-1;
00067         res->indices[c-1]=c-2;
00068       }
00069       return res;
00070     }
00071     
00072     if( tp.nbf()>0){
00073       foreach(Face* f, tp.faces()) 
00074         nbi += f->size()-2;
00075       nbi*=3;
00076       Graphic* res = new Graphic( Graphic::E_TRIANGLE, tp.nbv(), nbi);
00077       std::map<Point*,int> index;
00078       
00079       if(tp.nbv()>0){
00080         unsigned cp=0;
00081         foreach(Point* p, tp.vertices()) {
00082           res->vertices[3*cp]  =p->x();
00083           res->vertices[3*cp+1]=p->y();
00084           res->vertices[3*cp+2]=p->z();
00085           index[p]=cp;cp++;
00086         }
00087       }
00088       
00089       unsigned n=0;
00090       foreach(Face* f, tp.faces()) {
00091         for(unsigned i=1;i<f->size();i++) {
00092           if( i+1 < f->size()) {
00093             res->indices[n]=index[f->points(0)];
00094             res->indices[n+1]=index[f->points(i)];
00095             res->indices[n+2]=index[f->points(i+1)]; 
00096             n+=3;
00097           }
00098         }
00099       }
00100       return res;
00101     }
00102     return NULL;
00103   }
00104 
00105   //--------------------------------------------------------------------
00106   template<class VIEWER, class TOPOLOGY> 
00107   static VIEWER& print_as_graphic(VIEWER& out, const TOPOLOGY& tp, unsigned d=0) {
00108   
00109   typedef typename TOPOLOGY::Point  Point; 
00110   typedef typename TOPOLOGY::Edge   Edge;
00111   typedef typename TOPOLOGY::Face   Face;
00112   
00113   if (d<1 && tp.nbv()>0){
00114     out<<"<pointset size=\""<<tp.nbv()<<"\" color=\"rgb\">\n";
00115     foreach(Point* p, tp.vertices()) {
00116       out <<" "<<p->x()<<" "<<p->y()<<" "<<p->z()<<" 255 75 75\n";
00117     }
00118     out<<" </pointset>\n ";
00119   }
00120   
00121   
00122   if (d<2 && tp.nbe()>0){
00123     out<<" <curve type=\"mesh\">\n<vect>\nVECT\n";
00124     out<<tp.nbe()<<" "
00125        <<2*tp.nbe()<<" "
00126        <<tp.nbe()<<"\n";
00127     
00128     for(int i=0; i<tp.nbe();i++) out<<"2 ";
00129     out<<"\n";
00130     for(int i=0; i<tp.nbe();i++) out<<"1 ";
00131     out<<"\n";
00132     foreach(Edge* e, tp.edges()) {
00133       out <<e->source()->x()<<" "<<e->source()->y()<<"  "<<e->source()->z()<<" "
00134           <<e->destination()->x()<<" "<<e->destination()->y()<<" "<<e->destination()->z()
00135           <<"\n";
00136     }
00137     for(unsigned i=0; i<tp.edges().size();i++) 
00138       out<< "0.814 0.279 0.2 1\n";
00139     out<<" </vect>\n </curve>\n";
00140   }
00141 
00143 //  std::srand((unsigned)time(0));
00144 //  foreach(Face* f, tp.faces()) 
00145 //    out << f;
00146 
00147 //  out<< "<camera>\n<Parameters fieldOfView=\"0.785398\" Type=\"PERSPECTIVE\" zNearCoefficient=\"0.005\" orthoCoef=\"0.414214\" zClippingCoefficient=\"1.73205\"/>\n  <Stereo distToScreen=\"0.5\" focusDistance=\"19.3137\" physScreenWidth=\"0.4\" IODist=\"0.062\"/>\n  <ManipulatedCameraFrame>\n   <position x=\"2.95543\" y=\"0.574994\" z=\"-0.756089\"/>\n   <orientation q0=\"0.0149472\" q1=\"-0.910158\" q2=\"-0.0595418\" q3=\"-0.409687\"/>\n   <ManipulatedParameters transSens=\"1\" rotSens=\"1\" wheelSens=\"1\" spinSens=\"0.3\"/>\n   <ManipulatedCameraParameters flySpeed=\"0.08\">\n    <flyUpVector x=\"0\" y=\"1\" z=\"0\"/>\n   </ManipulatedCameraParameters>\n  </ManipulatedCameraFrame>\n </camera>\n";
00148 
00149   if(tp.nbf()>0) {
00150     std::map<Point*,int> index;
00151     int c=0;
00152     foreach(Point* p, tp.vertices()) {
00153       index[p]=c;c++;
00154     }
00155 
00156     out<<"<surface type=\"mesh\" color=\"255 10 0\">\n<off>\n";
00157     out<<c<<" "<<tp.nbf()<<" 0\n";
00158     foreach(Point* p, tp.vertices()) {
00159       out <<p->x()<<" "<<p->y()<<" "<<p->z()<<" \n";
00160     }
00161 
00162     foreach(Face* f, tp.faces()) {
00163       //      out<<"3 ";
00164       out<<f->size()<<" ";
00165       int n=0;
00166       foreach(Point* p, f->points())
00167         //      if (n<3) 
00168         {
00169           out <<index[p]<<" ";
00170           n++;
00171         }
00172       out <<"\n";
00173     }
00174     out<<"</off>\n</surface>\n";
00175     }
00176 
00177   return out;
00178   }
00179 };
00180 //--------------------------------------------------------------------
00181 TMPL  class cell_list ;
00182 TMPL1 class surface ;
00183 template <class Object, class CELL> class node;
00184 template <class Object, class CELL> class kdtree;
00185 
00186 //--------------------------------------------------------------------  
00187 template<class C,class V=default_env>
00188 class tpl3d : public topology<C,V> {
00189 public:
00190   typedef topology<C,V> Topology;
00191   typedef REF_OF(V)     Ref;
00192   typedef typename Topology::Point    Point;  
00193   typedef typename Topology::Edge     Edge;
00194   typedef typename Topology::Face     Face;
00195   typedef cell<C,Ref>        Cell;
00196   typedef cell3d<C,Ref>      Cell3d;
00197   typedef surface<Ref>       Surface;
00198   typedef typename Cell::BoundingBox         BoundingBox;
00199 
00200   tpl3d(){};
00201    tpl3d(const BoundingBox & bx);
00202   ~tpl3d(void) ;
00203   
00205   virtual void clear();
00206 
00207   virtual void insert(Point*);
00208   virtual void insert(Edge *);
00209   virtual void insert(Face *);
00210   
00211   virtual void insert_singular(Point*); ;
00212   virtual void insert_singular(BoundingBox*);
00213 
00214   int nbv() const { return m_vertices.size(); }
00215   int nbe() const { return m_edges.size(); }
00216   int nbf() const { return m_faces.size(); }
00217   
00218   inline Seq<Point *> vertices(void) const { return m_vertices ; }
00219   inline Seq<Edge *>  edges(void)    const { return m_edges ; }
00220   inline Seq<Face *>  faces(void)    const { return m_faces ; }
00221   
00222   virtual Point* vertices(int i)     { return m_vertices[i] ; }
00223   virtual Face*  faces(int i)        { return m_faces[i] ; }
00224   
00225   //  protected:
00226   bool insert_regular (Cell * cell);
00227 
00228   //private:
00229 
00230   Seq<Point *>        m_vertices ;
00231   Seq<Edge  *>        m_edges ;
00232   Seq<Face  *>        m_faces ;
00233 };
00234 //--------------------------------------------------------------------
00235 TMPL SELF::tpl3d(const BoundingBox & bx) : Topology(bx) {
00236 }
00237 
00238 TMPL SELF::~tpl3d(void) {
00239 }
00240 
00241 
00242 TMPL bool 
00243 SELF::insert_regular(Cell* cl) {
00244   //  m_leaves <<cl;// put in the leaves and will be treated at the end
00245   return cl->insert_regular(this);
00246 //  return true; 
00247 }
00248 
00249 
00250 TMPL void 
00251 SELF::insert(Point * p) {
00252   use<point_def,V>::point_insertor(m_vertices, p); 
00253 }
00254 
00255 TMPL void 
00256 SELF::insert(Edge* e) { 
00257   m_edges<<e; 
00258 }
00259 
00260 TMPL void 
00261 SELF::insert(Face * f) { 
00262   m_faces<<f; 
00263 }
00264 
00265 TMPL void 
00266 SELF::insert_singular(BoundingBox * ) {
00267 }
00268 
00269 TMPL void 
00270 SELF::insert_singular(Point * ) {
00271   //  m_specials<<p;
00272 }
00273 
00274 TMPL void
00275 SELF::clear() {
00276   // Should we clean up these points?
00277   m_faces.resize( 0 );
00278   m_edges.resize( 0 );
00279   m_vertices.resize( 0 );
00280 }
00281 
00282 //--------------------------------------------------------------------
00283 TMPL Viewer&
00284 operator<<(Viewer& out, const SELF& tp) {
00285   use<tpl3d_def,V>::print_as_graphic(out,tp);
00286   return out;
00287 }
00288 //--------------------------------------------------------------------
00289 TMPL typename use<tpl3d_def,V>::Graphic*
00290 as_graphic(const SELF& tp) {
00291   return use<tpl3d_def,V>::as_graphic(tp);
00292 }
00293 //====================================================================
00294 } ; // namespace shape
00295 } ; // namespace mmx
00296 //====================================================================
00297 # undef TMPL
00298 # undef TMPL1
00299 # undef AlgebraicCurve 
00300 # undef AlgebraicSurface
00301 # undef Cell
00302 # undef Cell3dAlgebraicCurve
00303 # undef Cell3dAlgebraicSurface
00304 # undef Cell3dParametricCurve
00305 # undef Cell3dList
00306 # undef Cell3dFactory
00307 # undef Shape
00308 # undef Viewer
00309 # undef Graphic
00310 # undef SELF 
00311 # endif