1 #ifndef bwm_observer_cam_h_
2 #define bwm_observer_cam_h_
3 //:
4 // \file
5 
6 #include <iostream>
7 #include "bwm_observer_vgui.h"
8 #include "bwm_observable_sptr.h"
9 
10 #ifdef _MSC_VER
11 #  include <vcl_msvc_warnings.h>
12 #endif
13 
14 #include <vgui/vgui_easy2D_tableau.h>
15 #include <vgui/vgui_viewer2D_tableau.h>
16 
17 #include <vgl/vgl_point_2d.h>
18 #include <vgl/vgl_point_3d.h>
19 #include <vgl/vgl_plane_3d.h>
20 #include <vgl/vgl_box_3d.h>
21 
22 #include <vsol/vsol_point_2d_sptr.h>
23 #include <vsol/vsol_point_3d_sptr.h>
24 #include <vsol/vsol_polygon_2d_sptr.h>
25 #include <vsol/vsol_polygon_3d_sptr.h>
26 #include <vnl/vnl_math.h>
27 
28 #include <vpgl/vpgl_camera.h>
29 #include <depth_map/depth_map_scene.h>
30 #include <volm/volm_io.h>
31 
32 void bwm_project_meshes(std::vector<std::string> paths,
33                         vpgl_camera<double>* cam,
34                         std::vector<vgl_polygon<double> > &poly_2d_list);
35 
36 class bwm_observer_cam : public bwm_observer_vgui
37 {
38  public:
39 
40   typedef bwm_observer_vgui base;
41 
bwm_observer_cam(bgui_image_tableau_sptr const & img,vpgl_camera<double> * camera,std::string cam_path)42   bwm_observer_cam(bgui_image_tableau_sptr const& img, vpgl_camera<double> *camera, std::string cam_path)
43   : bwm_observer_vgui(img), sun_elev_angle_(vnl_math::pi_over_4), sun_azim_angle_(vnl_math::pi_over_4),
44     camera_(camera), cam_path_(cam_path), cam_adjusted_(false),
45     proj_plane_(vgl_plane_3d<double>(0, 0, 1, 0)), extrude_mode_(false), show_geo_position_(false), focal_length_(3000.0), cam_height_(1.6),horizon_(nullptr),
46     horizon_soview_(nullptr)
47   {}
48   // set the initial projection plane to z=0
49   bwm_observer_cam(bgui_image_tableau_sptr const& img, const char* /*n*/="unnamed")
bwm_observer_vgui(img)50   : bwm_observer_vgui(img), sun_elev_angle_(vnl_math::pi_over_4), sun_azim_angle_(vnl_math::pi_over_4),
51     camera_(nullptr), cam_adjusted_(false),
52     proj_plane_(vgl_plane_3d<double>(0, 0, 1, 0)), extrude_mode_(false), show_geo_position_(false), focal_length_(3000.0), cam_height_(1.6),horizon_(nullptr),
53     horizon_soview_(nullptr)
54   {}
55 
~bwm_observer_cam()56   virtual ~bwm_observer_cam() { delete camera_; }
57 
image_tableau()58   bgui_image_tableau_sptr image_tableau() { return img_tab_; }
59 
camera_path()60   std::string camera_path() const { return cam_path_; }
61 
set_camera_path(std::string const & cam_path)62   void set_camera_path(std::string const& cam_path) { cam_path_=cam_path; }
63 
64   bool handle(const vgui_event &e);
65 
type_name()66   virtual std::string type_name() const { return "bwm_observer_cam"; }
67 
set_camera(vpgl_camera<double> * camera,std::string cam_path)68   void set_camera(vpgl_camera<double> *camera, std::string cam_path)
69   { camera_ = camera; cam_path_ = cam_path; cam_adjusted_ = false; }
70 
camera()71   vpgl_camera<double> * camera() { return camera_; }
72 
73   //: function to project shadow
74   void project_shadow();
75 
camera_adjusted()76   bool camera_adjusted() const { return cam_adjusted_; }
77 
set_camera_adjusted(bool status)78   void set_camera_adjusted(bool status) { cam_adjusted_ = status; }
79 
set_proj_plane(vgl_plane_3d<double> proj_plane)80   void set_proj_plane(vgl_plane_3d<double> proj_plane) { proj_plane_ = proj_plane; }
81 
82   void select_proj_plane();
83 
84   void move_ground_plane(vgl_plane_3d<double> master_plane,
85                          vsol_point_2d_sptr new_pt);
86 
87 #if 0  //replaced by translate along optical cone (remove after testing)
88   //: Translate *this projection plane
89   void translate_along_optical_axis(double da);
90 #endif
91   //: Translate/scale according to the optical cone
92   // The object vertices are adjusted so that the projection in
93   // the master frame is invariant to the motion. This method is
94   // useful for perpective cameras where object scale changes with distance
95   // from the center of projection
96   void translate_along_optical_cone(double da);
97 
get_proj_plane()98   vgl_plane_3d<double> get_proj_plane() const { return proj_plane_; }
99 
100   void set_ground_plane(double x1, double y1, double x2, double y2);
101 
102   //: if true, makes all the objects selectable, otherwise unselectable
103   void set_selection(bool status);
104 
105   void backproj_point(vsol_point_2d_sptr p2d, vsol_point_3d_sptr& p3d);
106 
107   void backproj_point(vsol_point_2d_sptr p2d,
108                       vsol_point_3d_sptr& p3d,
109                       vgl_plane_3d<double> proj_plane);
110 
111   //: Special case of backprojecting onto the projection plane
backproj_poly(vsol_polygon_2d_sptr poly2d,vsol_polygon_3d_sptr & poly3d)112   virtual void backproj_poly(vsol_polygon_2d_sptr poly2d,
113                              vsol_polygon_3d_sptr& poly3d) {
114     backproj_poly(poly2d, poly3d, proj_plane_);
115   }
116 
117   //: Special case of backprojecting onto the projection plane translated by dist in the direction of the normal
118   void backproj_poly(vsol_polygon_2d_sptr poly2d,
119                      vsol_polygon_3d_sptr& poly3d,
120                      double dist);
121 
122   virtual void proj_point(vgl_point_3d<double> world_pt,
123                           vgl_point_2d<double> &image_pt);
124 
125   virtual void proj_line(vsol_line_3d_sptr line_3d,
126                          vsol_line_2d_sptr &line_2d);
127 
128   virtual void proj_poly(vsol_polygon_3d_sptr poly3d,
129                          vsol_polygon_2d_sptr& poly2d);
130 
131 #if 0
132   void proj_poly(std::vector<bmsh3d_vertex*> verts,
133                  std::vector<vgl_point_2d<double> > &projections);
134 #endif
135 
136   bool intersect(float x1, float y1, float x2, float y2);
137 
138   bool intersect(bwm_observable_sptr obj, unsigned face_id,
139                  float x1, float y1, float x2, float y2);
140 
141   bool intersect(bwm_observable_sptr obj, float img_x, float img_y,
142                  unsigned face_id, vgl_point_3d<double> &pt3d);
143 
144   bool find_intersection_point(vgl_point_2d<double> img_point,
145                                vsol_polygon_3d_sptr poly3d,
146                                vgl_point_3d<double>& point3d);
147 
camera_center(vgl_point_3d<double> &)148   virtual void camera_center(vgl_point_3d<double> & /*center*/) {}
149 
corr_pt(vgl_point_2d<double> & p)150   bool corr_pt(vgl_point_2d<double> &p) const
151   { if (corr_.size()>0) { p = corr_[corr_.size()-1].first; return true; } else return false; }
152 
153   //virtual vgl_vector_3d<double> camera_direction(vgl_point_3d<double> origin)=0;
154 
155   void extrude_face();
156 
157   void extrude_face(vsol_point_2d_sptr pt);
158 
159   void divide_face(bwm_observable_sptr obs, unsigned face_id,
160                    float x1, float y1, float x2, float y2);
161 
162   void scan_regions();
163 
164   void load_mesh();
165 
166   void load_mesh_multiple();
167 
168   void save();
169   void save(std::string path);
170   void save_all();
171   void save_all(std::string path);
172 
173   void triangulate_meshes();
174 
175   void move_corr_point(vsol_point_2d_sptr new_pt);
176 
177   void set_corr_to_vertex();
178 
179   void world_pt_corr();
180 
181   void show_geo_position();
182 
183   //: displays position at vertex as text, either geo or local coordinates
184   void position_vertex(bool show_as_geo = true);
185 
186   void scroll_to_point(double lx, double ly, double lz);
187 
188   void create_terrain();
189 
190   void create_circular_polygon(std::vector< vsol_point_2d_sptr > ps_list,
191                                vsol_polygon_3d_sptr &circle,
192                                int num_sect, double &r, vgl_point_2d<double> &c);
193 
set_draw_mode(BWM_DRAW_MODE mode)194   void set_draw_mode(BWM_DRAW_MODE mode) { mode_=mode; update_all(); }
set_mesh_mode()195   void set_mesh_mode() { mode_ = bwm_observer_vgui::MODE_MESH; update_all(); }
set_face_mode()196   void set_face_mode() { mode_ = bwm_observer_vgui::MODE_POLY; update_all(); }
set_edge_mode()197   void set_edge_mode() { mode_ = bwm_observer_vgui::MODE_EDGE; update_all(); }
set_vertex_mode()198   void set_vertex_mode() { mode_ = bwm_observer_vgui::MODE_VERTEX; update_all(); }
199 
print_camera(std::ostream & s)200   virtual std::ostream& print_camera(std::ostream& s) { return s; }
201 
202   static void project_meshes(std::vector<std::string> paths,
203                              vpgl_camera<double>* cam,
204                              std::vector<vgl_polygon<double> > &poly_2d_list);
205 
206 #if 0
207   void create_boxm_scene();
208   void load_boxm_scene();
209 #endif
210   // ==================  camera calibration methods ================
211   void set_horizon();
set_focal_length(double focal_length)212   void set_focal_length(double focal_length){focal_length_ = focal_length;}
set_cam_height(double cam_height)213   void set_cam_height(double cam_height){cam_height_ = cam_height;}
214   void calibrate_cam_from_horizon();
215   void toggle_cam_horizon();
216   //=====================  depth map methods ========================
set_depth_map_scene(depth_map_scene const & scene)217   void set_depth_map_scene(depth_map_scene const& scene){scene_ = scene;}
218   void set_ground_plane();
219   void add_ground_plane(unsigned order, unsigned nlcd_id, std::string name);
220   void set_sky();
221   void add_sky(unsigned order, std::string name);
222   void add_region(std::string name, double min_depth, double max_depth, unsigned order, unsigned orient, unsigned land_id, double height);
223   void add_vertical_depth_region(double min_depth, double max_depth,
224                                  std::string name);
weights()225   std::vector<volm_weight> weights() { return weights_; }
set_weights(std::vector<volm_weight> weights)226   void set_weights(std::vector<volm_weight> weights) { weights_ = weights; }
227   void set_image_path(std::string const& ipath);
228   void save_depth_map_scene(std::string const& path);
229   void save_weight_params(std::string const& path);
230   void display_depth_map_scene();
231   std::vector<depth_map_region_sptr> scene_regions();
scene()232   depth_map_scene scene() {return scene_;}
233   void set_ground_plane_max_depth();
234  protected:
235 
236   //: to compute direction of sun.
237   double sun_elev_angle_;
238   double sun_azim_angle_;
239   bool shadow_mode_;
240 
241   std::vector<bgui_vsol_soview2D_line_seg*> shadow_line_segs_;
242 
243   vpgl_camera<double> *camera_;
244 
245   std::string cam_path_;
246 
247   bool cam_adjusted_;
248 
249   vgl_plane_3d<double> proj_plane_;
250 
251   //: face that is involved with moving
252   vgl_plane_3d<double> moving_face_plane_;
253 
254   //: controls extrude for keyboard manipulation
255   bool extrude_mode_;
256   bwm_observable_sptr extrude_obj_;
257 
258   bool show_geo_position_;
259 
260   //: list of selected soview objects after the last deselect_all
261   std::vector<vgui_soview*> selected_soviews_;
262 
263   bool geo_position(double u, double v, double& x, double& y, double& z);
264 
bwm_observer_cam()265   bwm_observer_cam() {}
266 
intersect_ray_and_plane(vgl_point_2d<double>,vgl_plane_3d<double>,vgl_point_3d<double> &)267   virtual bool intersect_ray_and_plane(vgl_point_2d<double> /*img_point*/,
268                                        vgl_plane_3d<double> /*plane*/,
269                                        vgl_point_3d<double>& /*world_point*/)
270   { std::cout << "ERROR!  USING CAM OBSERVER's intersect_ray_and_plane"; return false; }
271 
272   bool intersect_ray_and_box(vgl_box_3d<double> box,
273                              vgl_point_2d<double> img_point,
274                              vgl_point_3d <double> &point);
275 
276   bool backproj_poly(vsol_polygon_2d_sptr poly2d,
277                      vsol_polygon_3d_sptr& poly3d,
278                      vgl_plane_3d<double> proj_plane);
279 
280   bool find_intersection_points(vgl_point_2d<double> const img_point1,
281                                 vgl_point_2d<double> const img_point2,
282                                 vsol_polygon_3d_sptr poly3d,
283                                 vgl_point_3d<double>& point1,
284                                 vgl_point_3d<double>& l1,
285                                 vgl_point_3d<double>& l2,
286                                 vgl_point_3d<double>& point2,
287                                 vgl_point_3d<double>& l3,
288                                 vgl_point_3d<double>& l4);
289 
290   unsigned find_index_of_v(bwm_soview2D_vertex* vertex, bgui_vsol_soview2D_polygon* polygon);
291 
292   void make_object_selectable(bwm_observable_sptr obj, bool status);
293 
294   //: deselects all the selected objects and sets their styles back to mesh
295   void deselect();
296   //: objects for camera calibration
297   double focal_length_;
298   double cam_height_;
299   vsol_line_2d_sptr horizon_;//set manually
300   vgui_soview* horizon_soview_;//computed from camera
301   //: objects for depth map
302   depth_map_scene scene_;
303   //: weight parameter for depth_map_scene
304   std::vector<volm_weight> weights_;
305 };
306 
307 #endif
308