contrib/brl/bbas/imesh/algo/imesh_render.h
Go to the documentation of this file.
00001 // This is brl/bbas/imesh/algo/imesh_render.h
00002 #ifndef imesh_render_h_
00003 #define imesh_render_h_
00004 //:
00005 // \file
00006 // \brief Functions to render the faces of a mesh with projected coordinates
00007 // \author Matt Leotta (mleotta@lems.brown.edu)
00008 // \date June 2, 2008
00009 //
00010 // \verbatim
00011 //  Modifications
00012 //   <none yet>
00013 // \endverbatim
00014 
00015 #include <imesh/imesh_mesh.h>
00016 #include <vil/vil_image_view.h>
00017 #include <vgl/vgl_point_3d.h>
00018 #include <vgl/vgl_vector_3d.h>
00019 #include <vgl/vgl_triangle_scan_iterator.h>
00020 #include <vpgl/vpgl_proj_camera.h>
00021 #include <vcl_cassert.h>
00022 
00023 //: Render a triangle defined by its vertices.
00024 // For each 3d point, X and Y map to image coordinates and Z maps to depth.
00025 // Assign label to each pixel in image such that the pixel is within
00026 // the triangle and the interpolated depth is less than the value in depth_img.
00027 // The depth is also updated for each pixel that is labeled
00028 template <class T>
00029 void imesh_render_triangle_label(const vgl_point_3d<double>& v1,
00030                                  const vgl_point_3d<double>& v2,
00031                                  const vgl_point_3d<double>& v3,
00032                                  const T& label,
00033                                  vil_image_view<T>& image,
00034                                  vil_image_view<double>& depth_img)
00035 {
00036   assert(depth_img.ni() == image.ni());
00037   assert(depth_img.nj() == image.nj());
00038 
00039   vgl_triangle_scan_iterator<double> tsi;
00040   tsi.a.x = v1.x();  tsi.a.y = v1.y();
00041   tsi.b.x = v2.x();  tsi.b.y = v2.y();
00042   tsi.c.x = v3.x();  tsi.c.y = v3.y();
00043   vgl_vector_3d<double> b1(v2.x()-v1.x(), v2.y()-v1.y(), v2.z()-v1.z());
00044   vgl_vector_3d<double> b2(v3.x()-v1.x(), v3.y()-v1.y(), v3.z()-v1.z());
00045   vgl_vector_3d<double> n = cross_product(b1,b2);
00046   double A = -n.x()/n.z();
00047   double B = -n.y()/n.z();
00048   double C = (v1.x()*n.x() + v1.y()*n.y() + v1.z()*n.z())/n.z();
00049   for (tsi.reset(); tsi.next(); ) {
00050     int y = tsi.scany();
00051     if (y<0 || y>=int(image.nj())) continue;
00052     int min_x = tsi.startx();
00053     int max_x = tsi.endx();
00054     if (min_x >= (int)image.ni() || max_x < 0)
00055       continue;
00056     if (min_x < 0) min_x = 0;
00057     if (max_x >= (int)image.ni()) max_x = image.ni()-1;
00058     double new_i = B*y+C;
00059     for (int x = min_x; x <= max_x; ++x) {
00060       double depth = new_i + A*x;
00061       if (depth < depth_img(x,y)) {
00062         depth_img(x,y) = depth;
00063         image(x,y) = label;
00064       }
00065     }
00066   }
00067 }
00068 
00069 
00070 //: Render a textured triangle defined by its vertices
00071 // \p v1,v2,v3 are coordinates in the projected image (plus depth)
00072 // \p t1,t2,t3 are corresponding texture coordinates (in the unit square)
00073 void imesh_render_triangle_texture(const vgl_point_3d<double>& v1,
00074                                    const vgl_point_3d<double>& v2,
00075                                    const vgl_point_3d<double>& v3,
00076                                    const vgl_point_2d<double>& t1,
00077                                    const vgl_point_2d<double>& t2,
00078                                    const vgl_point_2d<double>& t3,
00079                                    const vil_image_view<vxl_byte>& texture,
00080                                    vil_image_view<vxl_byte>& image,
00081                                    vil_image_view<double>& depth_img);
00082 
00083 
00084 //: Render the mesh using the camera and a texture image
00085 //  A depth map is also computed and used for occlusion.
00086 //  Texture mapping uses interpolates from the texture image with no
00087 //  additional lighting calculations.
00088 void imesh_render_textured(const imesh_mesh& mesh,
00089                            const vpgl_proj_camera<double>& camera,
00090                            const vil_image_view<vxl_byte>& texture,
00091                            vil_image_view<vxl_byte>& image,
00092                            vil_image_view<double>& depth_img);
00093 
00094 #endif // imesh_render_h_