contrib/brl/bseg/bmdl/bmdl_mesh.h
Go to the documentation of this file.
00001 // This is brl/bseg/bmdl/bmdl_mesh.h
00002 #ifndef bmdl_mesh_h_
00003 #define bmdl_mesh_h_
00004 //:
00005 // \file
00006 // \brief Construct a 3-d mesh from classified lidar images
00007 //
00008 // \author Matt Leotta
00009 // \date Oct 14, 2008
00010 
00011 #include <vcl_cstddef.h>
00012 
00013 #include <vil/vil_image_view.h>
00014 #include <imesh/imesh_mesh.h>
00015 
00016 struct bmdl_edge
00017 {
00018   bmdl_edge(unsigned int b1, unsigned int b2)
00019   : building1(b1), building2(b2) {}
00020 
00021   unsigned int building1;
00022   unsigned int building2;
00023   unsigned int joint1;
00024   unsigned int joint2;
00025   vcl_vector<vgl_point_2d<double> > pts;
00026 };
00027 
00028 
00029 struct bmdl_region
00030 {
00031   vcl_vector<unsigned int> edge_idxs;
00032   vcl_vector<vcl_vector<unsigned int> > hole_edge_idxs;
00033 };
00034 
00035 
00036 class bmdl_mesh
00037 {
00038  public:
00039   //: find the next trace point and direction
00040   static bool next_trace_point(unsigned int& i, unsigned int& j, int& dir,
00041                                const unsigned int* &p, unsigned int value,
00042                                unsigned int ni, unsigned int nj,
00043                                vcl_ptrdiff_t istep, vcl_ptrdiff_t jstep);
00044 
00045   //: trace a single boundary starting and location (i,j)
00046   static bool trace_boundary(vcl_vector<vgl_point_2d<double> >& pts,
00047                              unsigned int value,
00048                              const vil_image_view<unsigned int>& labels,
00049                              vil_image_view<bool>& visited,
00050                              unsigned int i, unsigned int j);
00051 
00052   //: trace the boundaries of the building labels into polygons
00053   // If \a dropped_clipped is true then buildings clipped by the image boundaries are not traced
00054   static vcl_vector<vgl_polygon<double> >
00055       trace_boundaries(const vil_image_view<unsigned int>& labels, bool drop_clipped = true);
00056 
00057   //: extract shared boundary edges from the polygon boundaries
00058   // \returns the number of joints linking the edges
00059   static unsigned int link_boundary_edges(const vil_image_view<unsigned int>& labels,
00060                                           const vcl_vector<vgl_polygon<double> >& polygons,
00061                                           vcl_vector<bmdl_edge>& edges,
00062                                           vcl_vector<bmdl_region>& regions);
00063 
00064 
00065   //: test if a boundary is clipped by the image of size \a ni by \a nj
00066   static bool is_clipped(const vcl_vector<vgl_point_2d<double> >& poly,
00067                          unsigned ni, unsigned nj);
00068 
00069   //: simplify a polygon by fitting lines
00070   // \a tol is the tolerance for line fitting
00071   static void simplify_polygon( vcl_vector<vgl_point_2d<double> >& polygon, double tol);
00072 
00073   //: simplify the boundaries by fitting lines
00074   static void simplify_boundaries( vcl_vector<vgl_polygon<double> >& boundaries );
00075 
00076   //: simplify an edge by fitting lines
00077   // \a tol is the tolerance for line fitting
00078   static void simplify_edge( vcl_vector<vgl_point_2d<double> >& pts, double tol );
00079 
00080   //: simplify the linked edges by fitting lines
00081   static void simplify_edges( vcl_vector<bmdl_edge>& edges );
00082   
00083   //: Subtract a hole from an existing face in a mesh
00084   static void roof_subtract_hole(const imesh_vertex_array<3>& verts,
00085                                  vcl_vector<unsigned int>& face, 
00086                                  const vcl_vector<unsigned int>& hole);
00087 
00088   //: construct a mesh out of data and labels
00089   // The coordinate system is flipped over the x-axis to make it right handed
00090   // i.e. (x,y) -> (x,-y)
00091   static void mesh_lidar(const vcl_vector<vgl_polygon<double> >& boundaries,
00092                          const vil_image_view<unsigned int>& labels,
00093                          const vil_image_view<double>& heights,
00094                          const vil_image_view<double>& ground,
00095                          imesh_mesh& mesh);
00096 
00097   //: construct a mesh out of data and labels using linked edges
00098   // The coordinate system is flipped over the x-axis to make it right handed
00099   // i.e. (x,y) -> (x,-y)
00100   static void mesh_lidar(const vcl_vector<bmdl_edge>& edges,
00101                          const vcl_vector<bmdl_region>& regions,
00102                          unsigned int num_joints,
00103                          const vil_image_view<unsigned int>& labels,
00104                          const vil_image_view<double>& heights,
00105                          const vil_image_view<double>& ground,
00106                          imesh_mesh& mesh);
00107 };
00108 
00109 #endif // bmdl_mesh_h_