1 #include "bwm_observer_generic_cam.h"
2 //
3 #include "bwm_observer_mgr.h"
4 #include "bwm_observer_proj_cam.h"
5 #include "algo/bwm_utils.h"
6 #include "vsl/vsl_binary_io.h"
7 #include "vgl/vgl_point_2d.h"
8 #include "vgl/vgl_point_3d.h"
9 #include "vgl/vgl_vector_3d.h"
10 #include "vgl/vgl_plane_3d.h"
11 #include "vgl/vgl_ray_3d.h"
12 #include "vgl/vgl_intersection.h"
13 #include "vpgl/vpgl_generic_camera.h"
14 #include <vpgl/algo/vpgl_camera_compute.h>
15 #include <vpgl/algo/vpgl_camera_convert.h>
16 #include "vil/vil_image_view.h"
17 #include "vil/vil_new.h"
18 #define DEBUG
19 
bwm_observer_generic_cam(bgui_image_tableau_sptr img,std::string name,std::string & image_path,std::string & cam_path,std::string & subtype,bool display_image_path)20 bwm_observer_generic_cam::bwm_observer_generic_cam(bgui_image_tableau_sptr img,
21                                                    std::string name,
22                                                    std::string& image_path,
23                                                    std::string& cam_path,
24                                                    std::string& subtype,
25                                                    bool display_image_path)
26 : bwm_observer_cam(img)
27 {
28   img->show_image_path(display_image_path);
29 
30   // LOAD IMAGE
31   vgui_range_map_params_sptr params;
32   vil_image_resource_sptr img_res = bwm_utils::load_image(image_path, params);
33 
34   if (!img_res) {
35     bwm_utils::show_error("Image [" + image_path + "] is NOT found");
36     return;
37   }
38 
39   img->set_image_resource(img_res, params);
40   img->set_file_name(image_path);
41   int ni = img_res->ni(), nj = img_res->nj();
42   // check if the camera path is not empty, if it is NITF, the camera
43   // info is in the image, not a separate file
44   if (cam_path.size() == 0)
45   {
46     bwm_utils::show_error("Camera tableaus need a valid camera path!");
47     return;
48   }
49   this->set_camera_path(cam_path);
50   bool local = false;
51   vpgl_camera<double>* cam =
52     bwm_observer_proj_cam::read_camera(cam_path, "perspective", ni, nj);
53   if (!cam)
54     cam = bwm_observer_proj_cam::read_camera(cam_path, "projective", ni, nj);
55   if (!cam)
56     cam = bwm_observer_rat_cam::read_camera(cam_path, local);
57   if (!cam||!local)
58     camera_ = nullptr;
59   else {
60     vpgl_generic_camera<double> gcam;
61     vpgl_generic_camera_convert::convert(cam, ni, nj, gcam);
62     camera_ = new vpgl_generic_camera<double>(gcam);
63   }
64 
65   //generate a unique tab name if null
66   if (name=="")
67     name = cam_path;
68   set_tab_name(name);
69   // add the observer to the observer pool
70   bwm_observer_mgr::instance()->add(this);
71 }
72 
intersect_ray_and_plane(vgl_point_2d<double> img_point,vgl_plane_3d<double> plane,vgl_point_3d<double> & world_point)73 bool bwm_observer_generic_cam::intersect_ray_and_plane(vgl_point_2d<double> img_point,
74                                                        vgl_plane_3d<double> plane,
75                                                        vgl_point_3d<double> &world_point)
76 {
77   vpgl_generic_camera<double>* generic_cam = static_cast<vpgl_generic_camera<double> *> (camera_);
78   double ni = generic_cam->cols(), nj = generic_cam->rows();
79   if (img_point.x()<0.0 ||img_point.y()<0.0||
80       img_point.x()>=ni||img_point.y()>=nj)
81     return false;
82   vgl_ray_3d<double> ray = generic_cam->ray(img_point.x(), img_point.y());
83   return vgl_intersection(ray, plane, world_point);
84 }
85 
86 
87 vil_image_resource_sptr
ray_image(int component,int level=0) const88 bwm_observer_generic_cam::ray_image(int component, int level=0) const
89 {
90   bool orgt = (component == 0);
91   bool dirt = (component == 1);
92   vpgl_generic_camera<double>* gcam =
93     static_cast<vpgl_generic_camera<double> *> (camera_);
94   if (!gcam) return nullptr;
95   vbl_array_2d<vgl_ray_3d<double> > rays = gcam->rays(level);
96   int nc = rays.cols(), nr = rays.rows();
97 
98   vil_image_view<float> view(nc, nr, 3);
99   for (int r = 0; r<nr; ++r)
100     for (int c = 0; c<nc; ++c) {
101       if (orgt) {
102         vgl_point_3d<double> org = rays[r][c].origin();
103         view(c,r,0) = static_cast<float>(org.x());
104         view(c,r,1) = static_cast<float>(org.y());
105         view(c,r,2) = static_cast<float>(org.z());
106       }
107       else if (dirt) {
108         vgl_vector_3d<double> dir = rays[r][c].direction();
109         view(c,r,0) = static_cast<float>(dir.x());
110         view(c,r,1) = static_cast<float>(dir.y());
111         view(c,r,2) = static_cast<float>(dir.z());
112       }
113       else return nullptr;
114     }
115   return vil_new_image_resource_of_view(view);
116 }
117