contrib/brl/bbas/imesh/imesh_mesh.h
Go to the documentation of this file.
00001 // This is brl/bbas/imesh/imesh_mesh.h
00002 #ifndef imesh_mesh_h_
00003 #define imesh_mesh_h_
00004 //:
00005 // \file
00006 // \brief An indexed face set mesh
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date May 2, 2008
00009 
00010 #include <vcl_vector.h>
00011 #include <vcl_memory.h>
00012 #include <vcl_cassert.h>
00013 
00014 #include <imesh/imesh_vertex.h>
00015 #include <imesh/imesh_face.h>
00016 #include <imesh/imesh_half_edge.h>
00017 
00018 #include <vgl/vgl_point_2d.h>
00019 
00020 //for brdb smart pointer
00021 #include <vbl/vbl_ref_count.h>
00022 #include <vbl/vbl_smart_ptr.h>
00023 #include <vsl/vsl_binary_io.h>
00024 
00025 //: A simple mesh
00026 class imesh_mesh : public vbl_ref_count
00027 {
00028  public:
00029   //: Default Constructor
00030   imesh_mesh() : tex_coord_status_(TEX_COORD_NONE) {}
00031 
00032   //: Constructor from vertex and face arrays
00033   //  Takes ownership of these arrays
00034   imesh_mesh(vcl_auto_ptr<imesh_vertex_array_base> verts, vcl_auto_ptr<imesh_face_array_base> faces)
00035   : verts_(verts), faces_(faces), tex_coord_status_(TEX_COORD_NONE) {}
00036 
00037   //: Copy Constructor
00038   imesh_mesh(const imesh_mesh& other);
00039 
00040   //: Assignment operator
00041   imesh_mesh& operator=(imesh_mesh const& other);
00042 
00043   //: Return the number of vertices
00044   unsigned int num_verts() const {return verts_->size();}
00045 
00046   //: Return the number of faces
00047   unsigned int num_faces() const {return faces_->size();}
00048 
00049   //: Return the number of edges
00050   unsigned int num_edges() const {return half_edges_.size()/2;}
00051 
00052   //: Merge the data from another mesh into this one
00053   //  Duplicates are not removed
00054   void merge(const imesh_mesh& other);
00055 
00056   //: Return true if the mesh has been initialized
00057   bool is_init() const { return verts_.get() && faces_.get(); }
00058 
00059   //: Access the vector of vertices
00060   const imesh_vertex_array_base& vertices() const { return *verts_; }
00061   imesh_vertex_array_base& vertices() { return *verts_; }
00062 
00063   //: Access the vector of vertices cast to a dimension
00064   template <unsigned int d>
00065   const imesh_vertex_array<d>& vertices() const
00066   {
00067     assert(dynamic_cast<imesh_vertex_array<d>*>(verts_.get()));
00068     return static_cast<const imesh_vertex_array<d>&>(*verts_);
00069   }
00070   template <unsigned int d>
00071   imesh_vertex_array<d>& vertices()
00072   {
00073     assert(dynamic_cast<imesh_vertex_array<d>*>(verts_.get()));
00074     return static_cast<imesh_vertex_array<d>&>(*verts_);
00075   }
00076 
00077   //: Access the vector of faces
00078   const imesh_face_array_base& faces() const { return *faces_; }
00079   imesh_face_array_base& faces() { return *faces_; }
00080 
00081   //: Set the vertices
00082   void set_vertices(vcl_auto_ptr<imesh_vertex_array_base> verts) { verts_ = verts; }
00083 
00084   //: Set the faces
00085   void set_faces(vcl_auto_ptr<imesh_face_array_base> faces) { faces_ = faces; }
00086 
00087   //: Returns true if the mesh has computed half edges
00088   bool has_half_edges() const { return half_edges_.size() > 0; }
00089 
00090   //: Return the half edge set
00091   const imesh_half_edge_set& half_edges() const { return half_edges_; }
00092 
00093   //: Construct the half edges graph structure
00094   void build_edge_graph();
00095 
00096   //: Remove the half edge graph structure
00097   void remove_edge_graph() { half_edges_.clear(); }
00098 
00099   //: Compute vertex normals
00100   void compute_vertex_normals();
00101 
00102   //: Compute vertex normals using face normals
00103   void compute_vertex_normals_from_faces();
00104 
00105   //: Compute face normals
00106   //  If norm == false the vector lengths are twice the area of the face
00107   void compute_face_normals(bool norm = true);
00108 
00109   //: This type indicates how texture coordinates are indexed
00110   // ON_VERT is one coordinate per vertex
00111   // ON_CORNER is one coordinate per half edge (i.e. corner)
00112   enum tex_coord_type { TEX_COORD_NONE = 0,
00113                         TEX_COORD_ON_VERT = 1,
00114                         TEX_COORD_ON_CORNER = 2 };
00115 
00116   //: Returns texture coordinate availability
00117   tex_coord_type has_tex_coords() const { return tex_coord_status_; }
00118 
00119   //: Return the texture coordinates
00120   const vcl_vector<vgl_point_2d<double> >& tex_coords() const { return tex_coords_; }
00121 
00122   //: Set the texture coordinates
00123   void set_tex_coords(const vcl_vector<vgl_point_2d<double> >& tc);
00124   
00125   //: set the texture sources
00126   void set_tex_source(const vcl_string ts) { tex_source_ = ts; }
00127   const vcl_string& tex_source() const { return tex_source_; }
00128 
00129   //: Return a vector indicating which faces have texture
00130   const vcl_vector<bool>& valid_tex_faces() const { return valid_tex_faces_; }
00131 
00132   //: Set the vector indicating which faces have texture
00133   void set_valid_tex_faces(const vcl_vector<bool>& valid);
00134 
00135   //: Label all faces with positive (counter clockwise orientation) area as valid
00136   //  This requirement refers to the texture map coordinates
00137   void label_ccw_tex_faces_valid();
00138 
00139   //: Map a barycentric coordinate (u,v) on triangle \param tri into texture space
00140   vgl_point_2d<double> texture_map(unsigned int tri,
00141                                    double u, double v) const;
00142 
00143 
00144  private:
00145   vcl_auto_ptr<imesh_vertex_array_base> verts_;
00146   vcl_auto_ptr<imesh_face_array_base> faces_;
00147   imesh_half_edge_set half_edges_;
00148 
00149   //: vector of texture coordinates
00150   vcl_vector<vgl_point_2d<double> > tex_coords_;
00151   
00152   //: vector of texture sources
00153   vcl_string tex_source_; 
00154   
00155   //: indicate which faces have texture data
00156   vcl_vector<bool> valid_tex_faces_;
00157   //: the type of texture coordinates
00158   tex_coord_type tex_coord_status_;
00159 };
00160 
00161 //smartptr
00162 typedef vbl_smart_ptr<imesh_mesh> imesh_mesh_sptr;
00163 
00164 //--- IO read/write for sptrs--------------------------------------------------
00165 //: Binary write boxm2_scene scene to stream
00166 void vsl_b_write(vsl_b_ostream& os, imesh_mesh_sptr& sptr);
00167 void vsl_b_write(vsl_b_ostream& os, imesh_mesh_sptr const& sptr);
00168 
00169 //: Binary load boxm2_scene scene from stream.
00170 void vsl_b_read(vsl_b_istream& is, imesh_mesh_sptr& sptr);
00171 void vsl_b_read(vsl_b_istream& is, imesh_mesh_sptr const& sptr);
00172 
00173 #endif // imesh_mesh_h_