1 #include <ios>
2 #include <iostream>
3 #include <fstream>
4 #include <sstream>
5 #include <iomanip>
6 #include "bwm_observer_fiducial.h"
7 //:
8 // \file
9 #include <bwm/bwm_observer_mgr.h>
10 #include <bwm/algo/bwm_algo.h>
11 #include <bwm/algo/bwm_image_processor.h>
12 #include <bwm/bwm_tableau_mgr.h>
13
14 #ifdef _MSC_VER
15 # include <vcl_msvc_warnings.h>
16 #endif
17 #include <cassert>
18 #include <vgui/vgui.h>
19 #include <vnl/vnl_math.h>
20 #include <bwm/io/bwm_fiducial_io.h>
21 #include <bwm/io/bwm_io_config_parser.h>
22 #include <bwm/bwm_site_mgr.h>
23 #include <vgui/vgui_dialog.h>
24 #include <vgui/vgui_dialog_extensions.h>
25 #include <vgui/vgui_projection_inspector.h>
26 #include <bgui/bgui_vsol_soview2D.h>
27 #include "bwm_observer_fiducial.h"
28 #include "bwm_utils.h"
29 float fiducial_corr::cross_radius_ = 2.0f;
add_fiducial_corrs(bwm_io_config_parser * parser)30 bool bwm_observer_fiducial::add_fiducial_corrs(bwm_io_config_parser* parser){
31 if(!parser){
32 std::cerr <<"null parser in add_fiducial_corrs" << std::endl;
33 return false;
34 }
35 site_name_ = parser->fid_site_name();
36 mode_ = parser->fid_mode();
37 type_ = parser->fid_type();
38 composite_image_path_ = parser->fid_image_path();
39 std::vector<std::pair<float, float> > fid_corrs = parser->fiducial_locations();
40 size_t n = fid_corrs.size();
41 for(size_t i = 0; i<n; ++i){
42 fiducial_corr fc;
43 float u = fid_corrs[i].first, v = fid_corrs[i].second;
44 bwm_soview2D_cross* cross = new bwm_soview2D_cross(u, v, fiducial_corr::cross_radius_);
45 cross->set_style(COMMITED_STYLE);
46 fc.soview_ = cross;
47 fc.loc_ = vgl_point_2d<double>(u, v);
48 vgui_displaylist2D_tableau::add(cross);
49 committed_fid_corrs_.push_back(fc);
50 }
51 return true;
52 }
bwm_observer_fiducial(bgui_image_tableau_sptr const & img,std::string const & site_name,std::string const & image_path,std::string const & fid_path,bool display_image_path)53 bwm_observer_fiducial::bwm_observer_fiducial(bgui_image_tableau_sptr const& img,
54 std::string const& site_name,
55 std::string const& image_path,
56 std::string const& fid_path,
57 bool display_image_path): bwm_observer_vgui(img)
58 {
59 COMMITED_STYLE = vgui_style::new_style(0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
60 img->show_image_path(display_image_path);
61 if(image_path != "" && fid_path == ""){
62 site_name_ = site_name;
63 composite_image_path_=image_path;
64 }else if(image_path == "" && fid_path != ""){
65 // user must delete
66 bwm_io_config_parser* parser = bwm_site_mgr::instance()->site_parser(fid_path);
67 if(parser == nullptr){
68 bwm_utils::show_error("Fiducial [" + image_path + "] is NOT found");
69 return;
70 }
71 this->add_fiducial_corrs(parser);
72 delete parser;
73 }
74 // LOAD IMAGE
75 vgui_range_map_params_sptr params;
76 vil_image_resource_sptr img_res = bwm_utils::load_image(composite_image_path_, params);
77 if (!img_res) {
78 bwm_utils::show_error("Image [" + image_path + "] is NOT found");
79 return;
80 }
81 img->set_image_resource(img_res, params);
82 img->set_file_name(composite_image_path_);
83 std::string temp = site_name_;
84 if(temp == "")
85 temp = image_path;
86 set_tab_name(temp);
87 bwm_observer_mgr::instance()->add(this);
88 }
~bwm_observer_fiducial()89 bwm_observer_fiducial::~bwm_observer_fiducial(){
90 if(pending_corr_.soview_ != nullptr){
91 this->remove(pending_corr_.soview_);
92 pending_corr_.soview_ = nullptr;
93 }
94 for(size_t i = 0; i<committed_fid_corrs_.size(); ++i){
95 fiducial_corr& fc = committed_fid_corrs_[i];
96 if(fc.soview_ != nullptr){
97 this->remove(fc.soview_);
98 fc.soview_ = nullptr;
99 }
100 }
101 }
102
handle(const vgui_event & e)103 bool bwm_observer_fiducial::handle(const vgui_event &e){
104 if (e.type==vgui_KEY_PRESS && e.key == 'a')
105 {
106 this->commit_pending();
107 return true;
108 }
109
110 if (e.type==vgui_KEY_PRESS && e.key == 'r')
111 {
112 std::cout << "REMOVE.." << std::endl;
113 this->remove_sel_committed();
114 return true;
115 }
116
117 if (e.type == vgui_BUTTON_DOWN &&
118 e.button == vgui_LEFT &&
119 e.modifier == vgui_SHIFT &&
120 bwm_observer_mgr::instance()->in_corr_picking())
121 {
122 vgui_projection_inspector pi;
123 float x,y;
124 pi.window_to_image_coordinates(e.wx, e.wy, x, y);
125 vgl_point_2d<double> pt(x,y);
126 this->update_pending(pt);
127 return true;
128 }
129
130 return base::handle(e);
131 }
update_pending(vgl_point_2d<double> const & pt)132 void bwm_observer_fiducial::update_pending(vgl_point_2d<double> const& pt){
133 if(pending_corr_.soview_ != nullptr){
134 this->remove(pending_corr_.soview_);
135 pending_corr_.soview_ = nullptr;
136 }
137 bwm_soview2D_cross* cross = new bwm_soview2D_cross(pt.x(), pt.y(), fiducial_corr::cross_radius_);
138 pending_corr_.soview_ = cross;
139 pending_corr_.loc_ = pt;
140 this->add(cross);
141 this->post_redraw();
142 std::cout << "UPDATE..." << std::endl;
143 }
commit_pending()144 void bwm_observer_fiducial::commit_pending(){
145 std::cout << "COMMIT.." << std::endl;
146 fiducial_corr fid_corr = pending_corr_;
147 if (fid_corr.soview_ == nullptr) {
148 std::cerr << "no pending corr to commit" << std::endl;
149 return;
150 }
151 pending_corr_.soview_ = nullptr;
152 fid_corr.soview_->set_style(COMMITED_STYLE);
153 committed_fid_corrs_.push_back(fid_corr);
154 this->post_redraw();
155 }
remove_sel_committed()156 void bwm_observer_fiducial::remove_sel_committed(){
157 size_t nc = committed_fid_corrs_.size();
158 if(nc == 0){
159 std::cerr << "no committed fiducial corrsespondences" << std::endl;
160 return;
161 }
162 std::vector<vgui_soview*> select_list = this->get_selected_soviews();
163 size_t n = select_list.size();
164 if(n == 0){
165 std::cerr << "no fiducial corrsespondence selected to delete" << std::endl;
166 return;
167 }
168 bool found = false;
169 for(size_t i = 0; i<n&&!found; ++i)
170 for(size_t j = 0; j<committed_fid_corrs_.size()&&!found; ++j){
171 if(select_list[i]->get_id() == committed_fid_corrs_[j].soview_->get_id()){
172 found = true;
173 if(committed_fid_corrs_[j].soview_ != nullptr){
174 this->remove(committed_fid_corrs_[j].soview_);
175 committed_fid_corrs_[j].soview_ = nullptr;
176 }
177 committed_fid_corrs_.erase(committed_fid_corrs_.begin() + j);
178 }
179 }
180 this->post_redraw();
181 }
182
save_fiducial_corrs(std::string path)183 bool bwm_observer_fiducial::save_fiducial_corrs(std::string path){
184 std::ofstream ostr(path.c_str());
185 if(!ostr){
186 std::cerr << "Can't open " << path << " to write fiducial correspondences" << std::endl;
187 return false;
188 }
189 bwm_fiducial_io fid_io(site_name_, composite_image_path_, fiducial_path_, committed_fid_corrs_);
190 fid_io.x_write(ostr);
191 return true;
192 }
start_fid_corrs()193 void bwm_observer_fiducial::start_fid_corrs(){
194 bwm_observer_mgr::instance()->start_corr();
195 this->post_redraw();
196 }
197
stop_fid_corrs()198 void bwm_observer_fiducial::stop_fid_corrs(){
199 bwm_observer_mgr::instance()->stop_corr();
200 this->post_redraw();
201 }
202