contrib/brl/bbas/imesh/algo/imesh_project.h
Go to the documentation of this file.
00001 // This is brl/bbas/imesh/algo/imesh_project.h
00002 #ifndef imesh_project_h_
00003 #define imesh_project_h_
00004 //:
00005 // \file
00006 // \brief Functions to project a mesh into an image
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date Nov. 8, 2005
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <none yet>
00013 // \endverbatim
00014 
00015 #include <imesh/imesh_mesh.h>
00016 #include <vpgl/vpgl_proj_camera.h>
00017 #include <vpgl/vpgl_perspective_camera.h>
00018 #include <bpgl/bpgl_lens_distortion.h>
00019 #include <vil/vil_image_view.h>
00020 #include <vgl/vgl_box_2d.h>
00021 
00022 //: project the 3D vertices into 2D using the camera
00023 void imesh_project_verts(const vcl_vector<vgl_point_3d<double> >& verts3d,
00024                          const vpgl_proj_camera<double>& camera,
00025                          vcl_vector<vgl_point_2d<double> >& verts2d);
00026 
00027 //: project the 3D vertices into 2D using the camera
00028 void imesh_project_verts(const imesh_vertex_array<3>& verts3d,
00029                          const vpgl_proj_camera<double>& camera,
00030                          vcl_vector<vgl_point_2d<double> >& verts2d);
00031 
00032 //: project the 3D vertices into 2D vertices and depths using the camera
00033 void imesh_project_verts(const vcl_vector<vgl_point_3d<double> >& verts3d,
00034                          const vpgl_proj_camera<double>& camera,
00035                          vcl_vector<vgl_point_2d<double> >& verts2d,
00036                          vcl_vector<double>& depths);
00037 
00038 //: project the 3D mesh vertices into 2D vertices and depths using the camera
00039 void imesh_project_verts(const imesh_vertex_array<3>& verts3d,
00040                          const vpgl_proj_camera<double>& camera,
00041                          vcl_vector<vgl_point_2d<double> >& verts2d,
00042                          vcl_vector<double>& depths);
00043 
00044 //: distort the 2D vertices using the lens
00045 void imesh_distort_verts(const vcl_vector<vgl_point_2d<double> >& in_verts,
00046                          const bpgl_lens_distortion<double>& lens,
00047                          vcl_vector<vgl_point_2d<double> >& out_verts);
00048 
00049 //: project the mesh onto the image plane using the camera and lens distortion
00050 //  Set each pixel of the image to true if the mesh projects onto it
00051 void imesh_project(const imesh_mesh& mesh,
00052                    const vpgl_proj_camera<double>& camera,
00053                    const bpgl_lens_distortion<double>& lens,
00054                    vil_image_view<bool>& image);
00055 
00056 //: project the front-facing triangles of the mesh onto the image plane
00057 //  Using the camera and lens distortion
00058 //  Set each pixel of the image to true if the mesh projects onto it
00059 //  The optional \p bbox returns a 2D bounding box for the projection in the image
00060 void imesh_project(const imesh_mesh& mesh,
00061                    const vcl_vector<vgl_vector_3d<double> >& normals,
00062                    const vpgl_proj_camera<double>& camera,
00063                    const bpgl_lens_distortion<double>& lens,
00064                    vil_image_view<bool>& image,
00065                    vgl_box_2d<unsigned int>* bbox = 0);
00066 
00067 //: project the mesh onto the image plane using the camera
00068 //  Set each pixel of the image to true if the mesh projects onto it
00069 void imesh_project(const imesh_mesh& mesh,
00070                    const vpgl_proj_camera<double>& camera,
00071                    vil_image_view<bool>& image);
00072 
00073 //: project the mesh onto the image plane using the camera
00074 //  Set each pixel of the image to the depth to the mesh
00075 void imesh_project_depth(const imesh_mesh& mesh,
00076                          const vpgl_proj_camera<double>& camera,
00077                          vil_image_view<double>& image);
00078 
00079 //: Render a triangle defined by its vertices
00080 void imesh_render_triangle_interp(const vgl_point_2d<double>& v1,
00081                                   const vgl_point_2d<double>& v2,
00082                                   const vgl_point_2d<double>& v3,
00083                                   const double& i1, const double& i2, const double& i3,
00084                                   vil_image_view<double>& image);
00085 
00086 //: Render the faces of the mesh into the image by interpolating the values at the vertices
00087 //  The minimum value is kept at each pixel (as in computing a depth map)
00088 void imesh_render_triangles_interp(const imesh_regular_face_array<3>& tris,
00089                                    const vcl_vector<vgl_point_2d<double> >& img_verts,
00090                                    const vcl_vector<double>& vals,
00091                                    vil_image_view<double>& image);
00092 
00093 //: Triangulates the faces and then calls imesh_render_triangles_interp
00094 void imesh_render_faces_interp(const imesh_mesh& mesh,
00095                                const vcl_vector<vgl_point_2d<double> >& img_verts,
00096                                const vcl_vector<double>& vals,
00097                                vil_image_view<double>& image);
00098 
00099 //: Compute the bounds of the projection of a set of image points
00100 //  The returned bounds are the intersection of the input bounds
00101 //  and the bounding box of the points
00102 void imesh_projection_bounds(const vcl_vector<vgl_point_2d<double> >& img_pts,
00103                              vgl_box_2d<unsigned int>& bbox);
00104 
00105 //: back project an image point onto the mesh using the camera
00106 //  Returns the index of the intersected triangle, or -1 for no intersection
00107 int imesh_project_onto_mesh(const imesh_mesh& mesh,
00108                             const vcl_vector<vgl_vector_3d<double> >& normals,
00109                             const vcl_vector<vgl_point_2d<double> >& verts2d,
00110                             const vpgl_perspective_camera<double>& camera,
00111                             const vgl_point_2d<double>& pt_2d,
00112                             vgl_point_3d<double>& pt_3d);
00113 
00114 //: project a texture point onto a mesh face index with barycentric coordinates
00115 //  Returns the index of the intersected triangle, or -1 for no intersection
00116 int imesh_project_texture_to_barycentric(const imesh_mesh& mesh,
00117                                          const vgl_point_2d<double>& pt_2d,
00118                                          vgl_point_2d<double>& pt_uv);
00119 
00120 //: project a texture polygon into barycentric coordinates
00121 //  \returns true if the polygon is not clipped by the mesh texture
00122 //  \returns the vector of barycentric points by reference
00123 //  \returns a vector of coded half edge indices
00124 //  the half edge indices are scaled by a factor of 4
00125 //  the last two bits indicate the intersection type:
00126 //  - 0 for face
00127 //  - 1 for edge
00128 //  - 2 for vertex
00129 //  barycentric coordinate refer to the adjacent triangle
00130 //  \returns a mapping from each original point into barycentric points.
00131 //  if an original point is not mapped the value is -1
00132 bool imesh_project_texture_to_barycentric(const imesh_mesh& mesh,
00133                                           const vcl_vector<vgl_point_2d<double> >& pts_2d,
00134                                           vcl_vector<vgl_point_2d<double> >& pts_uv,
00135                                           vcl_vector<unsigned long>& idxs,
00136                                           vcl_vector<int>& map_back);
00137 
00138 //: compute the matrix that maps texture points to 3-d for a given triangle index
00139 // $(x,y,1)$ maps into 3-d $(x,y,z)$
00140 vnl_matrix_fixed<double,3,3>
00141 imesh_project_texture_to_3d_map(const imesh_mesh& mesh, unsigned int tidx);
00142 
00143 //: compute the affine matrix that maps triangle (a1,b1,c1) to (a2,b2,c2)
00144 vnl_matrix_fixed<double,3,3>
00145 imesh_affine_map(const vgl_point_2d<double>& a1,
00146                  const vgl_point_2d<double>& b1,
00147                  const vgl_point_2d<double>& c1,
00148                  const vgl_point_2d<double>& a2,
00149                  const vgl_point_2d<double>& b2,
00150                  const vgl_point_2d<double>& c2);
00151 
00152 //: project barycentric coordinates with an index to texture space
00153 //  \param idx is the face index
00154 vgl_point_2d<double>
00155 imesh_project_barycentric_to_texture(const imesh_mesh& mesh,
00156                                      const vgl_point_2d<double>& pt_uv,
00157                                      unsigned int idx);
00158 
00159 //: project barycentric coordinates with an index the mesh surface (3D)
00160 //  \param idx is the face index
00161 vgl_point_3d<double>
00162 imesh_project_barycentric_to_mesh(const imesh_mesh& mesh,
00163                                   const vgl_point_2d<double>& pt_uv,
00164                                   unsigned int idx);
00165 
00166 //: back project image points onto the mesh using the camera
00167 //  Returns a vector of all valid 3d points and indices to corresponding 2d points
00168 void imesh_project_onto_mesh(const imesh_mesh& mesh,
00169                              const vcl_vector<vgl_vector_3d<double> >& normals,
00170                              const vpgl_perspective_camera<double>& camera,
00171                              const vcl_vector< vgl_point_2d<double> >& pts_2d,
00172                              vcl_vector<unsigned int >& idx_2d,
00173                              vcl_vector<vgl_point_3d<double> >& pts_3d);
00174 
00175 //: back project an image point onto the mesh using the camera
00176 //  The resulting point is in barycentric coordinates for the returned triangle
00177 //  Returns the index of the intersected triangle, or -1 for no intersection
00178 int imesh_project_onto_mesh_barycentric(const imesh_mesh& mesh,
00179                                         const vcl_vector<vgl_vector_3d<double> >& normals,
00180                                         const vcl_vector<vgl_point_2d<double> >& verts2d,
00181                                         const vpgl_perspective_camera<double>& camera,
00182                                         const vgl_point_2d<double>& pt_img,
00183                                         vgl_point_2d<double>& pt_bary);
00184 
00185 //: back project an image point onto the mesh using the camera
00186 //  Then project from the mesh into normalized texture coordinate (UV)
00187 //  Assumes the mesh has both normals and texture coordinates
00188 //  Returns the index of the intersected triangle, or -1 for no intersection
00189 int imesh_project_onto_mesh_texture(const imesh_mesh& mesh,
00190                                     const vcl_vector<vgl_point_2d<double> >& verts2d,
00191                                     const vpgl_perspective_camera<double>& camera,
00192                                     const vgl_point_2d<double>& pt_img,
00193                                     vgl_point_2d<double>& pt_uv);
00194 
00195 #endif // imesh_project_h_