contrib/brl/bmvl/brct/brct_algos.h
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief collection class for various reconstruction functions
00004 // \author Kongbin Kang (Kongbin_Kang@Brown.edu)
00005 // \date   April 24, 2003
00006 // \verbatim
00007 //  Modifications
00008 //   none yet
00009 // \endverbatim
00010 
00011 #ifndef bcrt_algos_h_
00012 #define bcrt_algos_h_
00013 
00014 #if defined(_MSC_VER) && ( _MSC_VER > 1000 )
00015 #pragma once
00016 #endif // _MSC_VER > 1000
00017 #include <vcl_fstream.h>
00018 #include <vgl/vgl_point_3d.h>
00019 #include <vgl/vgl_sphere_3d.h>
00020 #include <vgl/vgl_point_2d.h>
00021 #include <vgl/vgl_line_segment_2d.h>
00022 #include <vgl/vgl_homg_point_2d.h>
00023 #include <vgl/vgl_homg_point_3d.h>
00024 #include <vgl/algo/vgl_h_matrix_2d.h>
00025 #include <vgl/algo/vgl_p_matrix.h>
00026 #include <vnl/vnl_double_2.h>
00027 #include <vnl/vnl_double_3.h>
00028 #include <vnl/vnl_double_3x3.h>
00029 #include <vnl/vnl_double_3x4.h>
00030 #include <vnl/vnl_double_4x4.h>
00031 #include <vil/vil_image_resource_sptr.h>
00032 #include <vsol/vsol_box_3d_sptr.h>
00033 #include <vsol/vsol_point_3d_sptr.h>
00034 #include <vsol/vsol_polygon_2d_sptr.h>
00035 #include <vsol/vsol_polygon_3d_sptr.h>
00036 #include <vdgl/vdgl_digital_curve_sptr.h>
00037 #include <bugl/bugl_gaussian_point_2d.h>
00038 #include <bugl/bugl_gaussian_point_3d.h>
00039 #include <brct/brct_corr_sptr.h>
00040 #include <vpgl/vpgl_proj_camera.h>
00041 struct brct_error_index
00042 {
00043   brct_error_index(int i, double error){i_ = i; e_ = error;}
00044   ~brct_error_index() {}
00045   int i() const {return i_;}
00046   double error() const {return e_;}
00047  private:
00048   int i_;
00049   double e_;
00050 };
00051 
00052 
00053 class brct_algos
00054 {
00055  public:
00056   brct_algos();
00057   virtual ~brct_algos();
00058 
00059   //operators
00060   static void add_box_vrml(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax);
00061   static vsol_box_3d_sptr get_bounding_box(vcl_vector<vgl_point_3d<double> > &pts_3d);
00062   static vgl_point_3d<double> bundle_reconstruct_3d_point(vcl_vector<vnl_double_2> &pts,
00063                                                           vcl_vector<vnl_double_3x4> &P);
00064   static vgl_point_2d<double> projection_3d_point(const vgl_point_3d<double> & x, const vnl_double_3x4& P);
00065 
00066   static bugl_gaussian_point_2d<double> project_3d_point(const vnl_double_3x4 &P,
00067                                                          const bugl_gaussian_point_3d<double> & X);
00068 
00069   static vnl_double_2 projection_3d_point(const vnl_double_3x4& P, const vnl_double_3 & X);
00070   //: get closet point from a digital curve
00071   static vgl_point_2d<double>  closest_point(vdgl_digital_curve_sptr dc, vgl_point_2d<double> pt);
00072   //: get point on a digital curve which is most possible to the guassian pdf
00073   static vgl_point_2d<double> most_possible_point(vdgl_digital_curve_sptr dc, bugl_gaussian_point_2d<double> &pt);
00074 
00075   //: pointwise reconstruction
00076   static vgl_point_3d<double> triangulate_3d_point(const vgl_point_2d<double>& x1, const vnl_double_3x4& P1,
00077                                                    const vgl_point_2d<double>& x2, const vnl_double_3x4& P2);
00078 
00079   //: solve a general projective P matrix
00080   static bool solve_p_matrix(vcl_vector<vgl_homg_point_2d<double> >const& image_points,
00081                              vcl_vector<vgl_homg_point_3d<double> >const& world_points,
00082                              vnl_double_3x4& P);
00083 
00084   //: compute the Euclidean camera from 3d-2d correspondences given K
00085   static bool compute_euclidean_camera(vcl_vector<vgl_point_2d<double> > const& image_points,
00086                                        vcl_vector<vgl_point_3d<double> > const& world_points,
00087                                        vnl_double_3x3 const & K,
00088                                        vnl_double_3x4& P);
00089 
00090   //: compute a world to image homography from Euclidean Points
00091   static bool homography(vcl_vector<vgl_point_3d<double> > const& world_points,
00092                          vcl_vector<vgl_point_2d<double> > const& image_points,
00093                          vgl_h_matrix_2d<double> & H, bool optimize = false);
00094 
00095   //: compute a world to image homography from Euclidean Points (ransac)
00096   static bool
00097   homography_ransac(vcl_vector<vgl_point_3d<double> > const& world_points,
00098                     vcl_vector<vgl_point_2d<double> > const& image_points,
00099                     vgl_h_matrix_2d<double> & H, bool optimize = false);
00100 
00101   //: compute a world to image homography from Euclidean Points (muse)
00102   static bool
00103   homography_muse(vcl_vector<vgl_point_3d<double> > const& world_points,
00104                   vcl_vector<vgl_point_2d<double> > const& image_points,
00105                   vgl_h_matrix_2d<double> & H, bool optimize = false);
00106 
00107   //: form a planar 3x4 projection matrix from a planar homography
00108   static vgl_p_matrix<double> p_from_h(vgl_h_matrix_2d<double> const& H);
00109 
00110   //: form a 3-d 3x4 projection matrix from a planar homography and y-z vals
00111   // It is assumed that the vanishing point of the world z axis is
00112   // the image y axis.
00113   static vgl_p_matrix<double>
00114   p_from_h(vgl_h_matrix_2d<double> const& H, vcl_vector<double> const& image_y,
00115            vcl_vector<vgl_point_3d<double> > const& world_p);
00116 
00117   //:compute a planar mapping from a p_matrix by projecting onto the x-y plane
00118   static vgl_h_matrix_2d<double> h_from_p(vgl_p_matrix<double> const& P);
00119 
00120   //: change the world coordinates to be at image scale and position
00121   static void scale_and_translate_world(vcl_vector<vgl_point_3d<double> > const& world_points,
00122                                         const double magnification, vgl_h_matrix_2d<double> & H);
00123 
00124   //: change the world coordinates to be at image scale and position
00125   static void scale_and_translate_world(const double world_x_min,
00126                                         const double world_y_min,
00127                                         const double magnification,
00128                                         vgl_h_matrix_2d<double> & H);
00129 
00130   //: project world points into an image using a homography
00131   static void project(vcl_vector<vgl_point_3d<double> > const& world_points,
00132                       vgl_h_matrix_2d<double> const& H,
00133                       vcl_vector<vgl_point_2d<double> > & image_points);
00134 
00135   //: project world points into an image using a projection matrix
00136   static void project(vcl_vector<vgl_point_3d<double> > const& world_points,
00137                       vgl_p_matrix<double> const& P,
00138                       vcl_vector<vgl_point_2d<double> > & image_points);
00139 
00140   //: project a world polygon onto an image using a homgraphy
00141   static vsol_polygon_2d_sptr
00142     project(vsol_polygon_3d_sptr const& world_poly,
00143             vgl_h_matrix_2d<double> const& H);
00144 
00145   //: project world polygons onto an image using a homgraphy
00146   static void project(vcl_vector<vsol_polygon_3d_sptr> const& world_polys,
00147                       vgl_h_matrix_2d<double> const& H,
00148                       vcl_vector<vsol_polygon_2d_sptr > & image_polys);
00149 
00150   //: project a world polygon onto an image using a projective matrix
00151   static vsol_polygon_2d_sptr
00152     project(vsol_polygon_3d_sptr const& world_poly,
00153             vgl_p_matrix<double> const& P);
00154 
00155   //: project world polygons onto an image using a projective matrix
00156   static void project(vcl_vector<vsol_polygon_3d_sptr> const& world_polys,
00157                       vgl_p_matrix<double> const& P,
00158                       vcl_vector<vsol_polygon_2d_sptr > & image_polys);
00159 
00160   //: back_project an image polygon onto the world x-y plane
00161   static vsol_polygon_3d_sptr
00162     back_project(vsol_polygon_2d_sptr const& image_poly,
00163                  vgl_h_matrix_2d<double> const& H);
00164 
00165   //: back_project a polygon onto a x-y plane at some heightp
00166   static vsol_polygon_3d_sptr
00167     back_project(vsol_polygon_2d_sptr const& image_poly,
00168                  vgl_p_matrix<double> const& P,
00169                  const double height);
00170 
00171   //: compute a TargetJr style 4x4 projection matrix from a 3x4 matrix
00172   static vnl_double_4x4 convert_to_target(vnl_double_3x4 const& P);
00173 
00174   //: TargetJr CoolTransform Projection Method
00175   static  vnl_double_2 target_project(vnl_double_4x4 const& T,
00176                                       vnl_double_3 const& v);
00177 
00178   //: filter outliers for camera translation
00179   static void filter_outliers(const vnl_double_3x3& K,
00180                               const vnl_double_3& trans,
00181                               vcl_vector<vnl_double_2> & pts_2d,
00182                               vcl_vector<vnl_double_3> & pts_3d,
00183                               double fraction = 0.1);
00184 
00185   //: find camera translation from matched 2-d/3-d points
00186   static bool camera_translation(const vnl_double_3x3& K,
00187                                  vcl_vector<vnl_double_2> & pts_2d,
00188                                  vcl_vector<vnl_double_3> & pts_3d,
00189                                  vnl_double_3& trans);
00190 
00191   //: use uncertainty and point weeding to improve solution
00192   static void robust_camera_translation(const vnl_double_3x3& K,
00193                                         vcl_vector<bugl_gaussian_point_2d<double> > & pts_2d,
00194                                         vcl_vector<vgl_point_3d<double> > & pts_3d,
00195                                         vnl_double_3& trans);
00196 
00197   //: compute camera translation using epipolar geometry
00198   static void camera_translation(vnl_double_3x3 const & K,
00199                                  vnl_double_2 const& image_epipole,
00200                                  vcl_vector<vnl_double_2> const& points_0,
00201                                  vcl_vector<vnl_double_2> const& points_1,
00202                                  vcl_vector<vnl_double_2> const& points,
00203                                  vnl_double_3& T);
00204 
00205   static double motion_constant(vnl_double_2 const& image_epipole,
00206                                 int i,
00207                                 vnl_double_2& p_i,
00208                                 vnl_double_2& p_i1);
00209 
00210   static void print_motion_array(vnl_matrix<double>& H);
00211 
00212   static void read_vrml_points(vcl_ifstream& str,
00213                                vcl_vector<vsol_point_3d_sptr>& pts3d);
00214 
00215   static void write_vrml_header(vcl_ofstream& str);
00216 
00217   static void write_vrml_trailer(vcl_ofstream& str);
00218 
00219   static void write_vrml_points(vcl_ofstream& str,
00220                                 vcl_vector<vsol_point_3d_sptr> const& pts3d);
00221 
00222   static void write_vrml_points(vcl_ofstream& str,
00223                                 vcl_vector<vgl_point_3d<double> > const& pts3d,
00224                                 vcl_vector<vgl_point_3d<float> > const& color);
00225 
00226   static void write_vrml_polyline(vcl_ofstream& str,
00227                                   vcl_vector<vgl_point_3d<double> > const& vts,
00228                                   const float r,
00229                                   const float g,
00230                                   const float b);
00231 
00232   static void write_vrml_box(vcl_ofstream& str, vsol_box_3d_sptr const& box,
00233                              const float r = 1.0, const float g = 1.0,
00234                              const float b = 1.0,
00235                              const float transparency = 0.0);
00236 
00237   static void write_vrml_sphere(vcl_ofstream& str,
00238                                 vgl_sphere_3d<double> const& sphere,
00239                                 const float r = 1.0, const float g =1.0,
00240                                 const float b=1.0,
00241                                 const float transparency = 0);
00242 
00243   static bool read_world_points(vcl_ifstream& str,
00244                                 vcl_vector<vgl_point_3d<double> >& world_points);
00245 
00246   static bool read_world(vcl_ifstream& str,
00247                          vcl_vector<vgl_point_3d<double> >& world_points,
00248                          vcl_vector<vsol_polygon_3d_sptr>& polys,
00249                          vcl_vector<vcl_vector<unsigned> >& indexed_face_set);
00250 
00251   static void write_world_points(vcl_ofstream& str,
00252                                  vcl_vector<vgl_point_3d<double> >const& world_points);
00253 
00254   static void write_world(vcl_ofstream& str,
00255                           vcl_vector<vgl_point_3d<double> > const& world_points,
00256                           vcl_vector<vcl_vector<unsigned> > const& polys);
00257 
00258   static void write_world_ply2(vcl_ofstream& str,
00259                                vcl_vector<vgl_point_3d<double> > const& world_points,
00260                                vcl_vector<vcl_vector<unsigned> > const& polys);
00261 
00262   static bool read_world_ply2(vcl_ifstream& str,
00263                               vcl_vector<vgl_point_3d<double> >& world_points,
00264                               vcl_vector<vsol_polygon_3d_sptr>& polys,
00265                               vcl_vector<vcl_vector<unsigned> >& indexed_face_set);
00266 
00267   static bool write_ifs_box(vcl_ofstream& ostr,
00268                             vcl_vector<vgl_point_3d<double> > const& verts,
00269                             vcl_vector<vcl_vector<unsigned> > const& faces,
00270                             const float r, const float g, const float b);
00271 
00272   static bool write_ifs(vcl_ofstream& ostr,
00273                         vcl_vector<vgl_point_3d<double> > const& verts,
00274                         vcl_vector<vcl_vector<unsigned> > const& faces,
00275                         const float r, const float g, const float b);
00276 
00277   static bool translate_ply2_to_vrml(vcl_ifstream& istr, vcl_ofstream& ostr,
00278                                      const float r, const float g,
00279                                      const float b);
00280 
00281   static bool read_target_corrs(vcl_ifstream& str,
00282                                 vcl_vector<bool>& valid,
00283                                 vcl_vector<vgl_point_2d<double> >& image_points,
00284                                 vcl_vector<vgl_point_3d<double> >& world_points);
00285 
00286   static bool write_corrs(vcl_ofstream& str,
00287                           vcl_vector<bool>& valid,
00288                           vcl_vector<vgl_point_2d<double> >& image_points,
00289                           vcl_vector<vgl_point_3d<double> >& world_points);
00290 
00291   static bool read_brct_corrs(vcl_ifstream& str,
00292                               vcl_vector<brct_corr_sptr>& corrs);
00293 
00294   static bool write_brct_corrs(vcl_ofstream& str,
00295                                vcl_vector<brct_corr_sptr> const& corrs);
00296 
00297   static void reconstruct_corrs(vcl_vector<brct_corr_sptr> const& image_corrs,
00298                                 vpgl_proj_camera<double> const& cam0,
00299                                 vpgl_proj_camera<double> const& cam1,
00300                                 vcl_vector<vgl_point_3d<double> >& world_points);
00301 
00302   static void write_target_camera(vcl_ofstream& str, vnl_double_3x4 const& P);
00303 
00304   static  vil_image_resource_sptr map_image_to_world(vil_image_resource_sptr const& image,
00305                                                      vgl_p_matrix<double> const& cam,
00306                                                      const double feet_per_pixel = 1.0);
00307 
00308   static bool save_constraint_file(vcl_vector<vgl_point_2d<double> >const& image_pts,
00309                                    vcl_vector<bool> const& valid,
00310                                    vcl_vector<vgl_point_3d<double> >const& world_pts,
00311                                    vcl_vector<vgl_line_segment_2d<double> > const& vertls,
00312                                    vcl_ofstream& str);
00313 
00314   // modeling functions
00315 
00316   static void box_3d(vgl_point_3d<double> const& c0,
00317                      vgl_point_3d<double> const& c1,
00318                      vgl_point_3d<double> const& c2,
00319                      vcl_vector<vgl_point_3d<double> >& world_points,
00320                      vcl_vector<vsol_polygon_3d_sptr>& polys,
00321                      vcl_vector<vcl_vector<unsigned> >& indexed_face_set);
00322 };
00323 
00324 #endif // bcrt_algos_h_