Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
wxProjectionPanel.cpp
1 #ifdef WIN32
2 # pragma warning (disable: 4005) // for VS8 + WX
3 #endif
4 
5 #include <Gui/wxProjectionPanel.hh>
6 #include <Geometry/ProjectionParametersSpherical.hh>
7 #include <Gui/StringConv.hh>
8 #include <sstream>
9 
10 using namespace std;
11 using namespace BIAS;
12 
13 
14 /*
15 ===============================================================================
16 wxProjectionPanel
17 ===============================================================================
18 */
19 
20 // event ids
21 enum eids
22 {
23  ID_New,
24  ID_Load,
25  ID_Save,
26  ID_NormalizeQuaternion
27 };
28 
29 // panel ids
30 enum pids
31 {
32  ID_Center,
33  ID_Quaternion,
34  ID_RMatrix,
35  ID_ImageSize,
36  ID_AspectRatio,
37  ID_Principal,
38  ID_KMatrix,
39  ID_FocalLength,
40  ID_Undistortion,
41  ID_Radius
42 };
43 
44 BEGIN_EVENT_TABLE(wxProjectionPanel, wxPanel)
45  EVT_TEXT_ENTER(-1, wxProjectionPanel::OnModified)
46  EVT_BUTTON(ID_NormalizeQuaternion, wxProjectionPanel::OnNormalizeQuaternion)
47  EVT_BUTTON(ID_New, wxProjectionPanel::OnNew)
48  EVT_BUTTON(ID_Load, wxProjectionPanel::OnLoad)
49  EVT_BUTTON(ID_Save, wxProjectionPanel::OnSave)
50 END_EVENT_TABLE()
51 
53  const wxPoint& pos, const wxSize& size,
54  long style, const wxString& name, bool createButtons)
55  : wxPanel(parent, id, pos, size, style, name)
56 {
57  selfModifying_ = false;
58 
59  projectionPanel_ = new wxScrolledWindow(this);
60  projectionPanel_->SetScrollRate(1, 1);
61 
62  wxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);
63 
64  if (createButtons)
65  {
66  buttonSizer->Add(new wxButton(this, ID_New, wxT("New")));
67  buttonSizer->Add(new wxButton(this, ID_Load, wxT("Load")));
68  buttonSizer->Add(new wxButton(this, ID_Save, wxT("Save")));
69  }
70 
71  wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
72  mainSizer->Add(buttonSizer, 0, wxALL, 5);
73  mainSizer->Add(projectionPanel_, 1, wxEXPAND | wxALL, 5);
74 
75  SetSizer(mainSizer);
76 }
77 
78 void wxProjectionPanel::SetProjection(const Projection& projection)
79 {
80  // clear old projection panel
81  projectionPanel_->DestroyChildren();
82  notebookPages_.clear();
83  panelCenter_.clear();
84 
85  panelQuaternion_.clear();
86  panelRMatrix_.clear();
87  panelImageSize_.clear();
88  panelAspectRatio_.clear();
89  panelPrincipal_.clear();
90  panelKMatrix_.clear();
91  panelFocalLength_.clear();
92  panelUndistortion_.clear();
93  panelRadius_.clear();
94 
95  // create sizer
96  wxFlexGridSizer* sizer = new wxFlexGridSizer(1, 5, 5);
97 
98  sizer->AddGrowableCol(1);
99  projectionPanel_->SetSizer(sizer);
100  noteBook_ = new wxNotebook(projectionPanel_, -1,
101  // wxDefaultPosition,wxSize(450,450), wxNB_TOP);
102  wxDefaultPosition,wxSize(-1,-1), wxNB_TOP);
103  sizer->Add(noteBook_, 0, wxALL, 5);
104 
105  projection_ = projection;
106 
107  for(unsigned i=0;i<projection_.Size();i++){
108 
109  notebookPages_.push_back(new wxScrolledWindow(noteBook_,-1,
110  wxDefaultPosition,wxSize(-1,-1)));
111  wxFlexGridSizer* sizerPage = new wxFlexGridSizer(2, 1,1);
112 
113  sizerPage->AddGrowableCol(1);
114  notebookPages_.back()->SetSizer(sizerPage);
115  // base parameters
116  const ProjectionParametersBase* paramsBase = projection.GetParameters(i);
117 
118 
119  sizerPage->Add(new wxStaticText(notebookPages_.back(), -1,
120  wxT("Identifier:"),wxDefaultPosition,wxSize(-1,-1)));
121  sizerPage->Add(new wxStaticText(notebookPages_.back(), -1,
122  AsciiToWx(paramsBase->GetIdentifier()),wxDefaultPosition,wxSize(-1,-1)));
123 
124  panelCenter_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Center"), ID_Center, true));
125  panelQuaternion_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Quaternion"), ID_Quaternion, true));
126 
127  sizerPage->AddSpacer(0);
128  sizerPage->Add(new wxButton(notebookPages_.back(), ID_NormalizeQuaternion, wxT("Normalize Quaternion")));
129 
130  panelRMatrix_.push_back(AddMatrixPanel(notebookPages_.back(),wxT("R Matrix"), ID_RMatrix, false));
131  panelImageSize_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Image Size"), ID_ImageSize, true));
132  panelAspectRatio_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Aspect Ratio"), ID_AspectRatio, true));
133  panelPrincipal_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Principal"), ID_Principal, true));
134 
135  // perspective parameters
136  const ProjectionParametersPerspective* paramsPersp =
137  dynamic_cast<const ProjectionParametersPerspective*>(paramsBase);
138  if (paramsPersp != NULL)
139  {
140  panelKMatrix_.push_back(AddMatrixPanel(notebookPages_.back(),wxT("K Matrix"), ID_KMatrix, false));
141  panelFocalLength_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Focal Length"), ID_FocalLength, true));
142  panelUndistortion_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Undistortion"), ID_Undistortion, true));
143  }
144  else
145  {
146  panelKMatrix_.push_back(NULL);
147  panelFocalLength_.push_back(NULL);
148  panelUndistortion_.push_back(NULL);
149  }
150 
151  // spherical parameters
152  const ProjectionParametersSpherical* paramsSpherical =
153  dynamic_cast<const ProjectionParametersSpherical*>(paramsBase);
154  if (paramsSpherical != NULL)
155  {
156  panelRadius_.push_back(AddVectorPanel(notebookPages_.back(),wxT("Radius"), ID_Radius, true));
157  }
158  else
159  {
160  panelRadius_.push_back(NULL);
161  }
162 
163  sizerPage->Layout();
164  // fill gui with values from projection
165  UpdateGUI(i);
166 
167  //add notebook page
168  wxString name;
169  name.Printf(_T("%s %d"),wxT("Camera:"), i);
170  noteBook_->AddPage(notebookPages_.back(), name,false);
171  }
172 
173 
174  sizer->Layout();
175  Layout();
176 }
177 
178 const Projection& wxProjectionPanel::GetProjection() const
179 {
180  return projection_;
181 }
182 
183 wxVectorPanel* wxProjectionPanel::
184 AddVectorPanel(wxWindow* parent,
185  const wxString& name,
186  wxWindowID id, bool editable)
187 {
188  wxVectorPanel* panel = new wxVectorPanel(parent, id, editable);
189  panel->SetSize(wxSize(-1,-1));
190  wxSizer* sizer = parent->GetSizer();
191  sizer->Add(new wxStaticText(parent, -1, name));
192  sizer->Add(panel, 1, wxEXPAND);
193 
194  return panel;
195 }
196 
197 wxMatrixPanel* wxProjectionPanel::
198 AddMatrixPanel(wxWindow* parent,const wxString& name,
199  wxWindowID id, bool editable)
200 {
201  wxMatrixPanel* panel = new wxMatrixPanel(parent, id, editable);
202  panel->SetSize(wxSize(-1,-1));
203  wxSizer* sizer = parent->GetSizer();
204  sizer->Add(new wxStaticText(parent, -1, name));
205  sizer->Add(panel, 1, wxEXPAND);
206 
207  return panel;
208 }
209 
210 void wxProjectionPanel::UpdateGUI(unsigned int cameraNr)
211 {
212  selfModifying_ = true;
213 
214  // base parameters
215  const ProjectionParametersBase* paramsBase = projection_.GetParameters(cameraNr);
216 
217  unsigned int imageWidth, imageHeight;
218  paramsBase->GetImageSize(imageWidth, imageHeight);
219 
220  double principalX, principalY;
221  paramsBase->GetPrincipal(principalX, principalY);
222  if (paramsBase->CValid())
223  panelCenter_[cameraNr]->SetVector(paramsBase->GetC());
224  else{
225  Vector3<double> C(0,0,0);
226  panelCenter_[cameraNr]->SetVector(C);
227  }
228  if (paramsBase->QValid()) {
229  panelQuaternion_[cameraNr]->SetVector(paramsBase->GetQ());
230  panelRMatrix_[cameraNr]->SetMatrix(BIAS::Matrix<double>(paramsBase->GetR()));
231  }
232  else{
233  panelQuaternion_[cameraNr]->SetVector(Quaternion<double>(0,0,0,1));
234  Matrix<double> R(3,3,0); R.SetIdentity();
235  //cout<<"Setting identity 3x3 matrix"<<endl;
236  panelRMatrix_[cameraNr]->SetMatrix(R);
237  }
238  panelImageSize_[cameraNr]->SetVector(Vector2<double>(imageWidth, imageHeight));
239  panelAspectRatio_[cameraNr]->SetVector(Vector<double>(1, paramsBase->GetAspectratio()));
240  panelPrincipal_[cameraNr]->SetVector(Vector2<double>(principalX, principalY));
241 
242  // perspective parameters
243  const ProjectionParametersPerspective* paramsPersp =
244  dynamic_cast<const ProjectionParametersPerspective*>(paramsBase);
245  if (paramsPersp != NULL)
246  {
247  double focalLength;
248  double kc1, kc2, kc3, kc4;
249  paramsPersp->GetFocalLength(focalLength);
250  paramsPersp->GetUndistortion(kc1, kc2, kc3, kc4);
251 
252  panelKMatrix_[cameraNr]->SetMatrix((Matrix<double>)(paramsPersp->GetK()));
253  panelFocalLength_[cameraNr]->SetVector(Vector<double>(1, focalLength));
254  panelUndistortion_[cameraNr]->SetVector(Vector4<double>(kc1, kc2, kc3, kc4));
255  }
256 
257  // spherical parameters
258  const ProjectionParametersSpherical* paramsSpherical =
259  dynamic_cast<const ProjectionParametersSpherical*>(paramsBase);
260  if (paramsSpherical != NULL)
261  {
262  panelRadius_[cameraNr]->SetVector(Vector<double>(1, paramsSpherical->GetRadius()));
263  }
264 
265  selfModifying_ = false;
266 }
267 
268 void wxProjectionPanel::OnNormalizeQuaternion(wxCommandEvent& /*event*/)
269 {
270  unsigned cameraNr = noteBook_->GetSelection();
271  ProjectionParametersBase* paramsBase = projection_.GetParameters(cameraNr);
272  Quaternion<double> Q = paramsBase->GetQ();
273  Q.Normalize();
274  paramsBase->SetQ(Q);
275  UpdateGUI(cameraNr);
276 }
277 
278 void wxProjectionPanel::OnModified(wxCommandEvent& event)
279 {
280  cout << "Modifying" << endl;
281  if (selfModifying_)
282  {
283  return;
284  }
285  unsigned cameraNr = noteBook_->GetSelection();
286  ProjectionParametersBase* paramsBase = projection_.GetParameters(cameraNr);
287  ProjectionParametersPerspective* paramsPersp =
288  dynamic_cast<ProjectionParametersPerspective*>(paramsBase);
289  ProjectionParametersSpherical* paramsSpherical =
290  dynamic_cast<ProjectionParametersSpherical*>(paramsBase);
291 
292  switch (event.GetId())
293  {
294  case ID_Center:
295  {
296  Vector<double> C = panelCenter_[cameraNr]->GetVector();
297  paramsBase->SetC(Vector3<double>(C));
298 
299  } break;
300 
301  case ID_Quaternion:
302  {
303  Vector<double> Q = panelQuaternion_[cameraNr]->GetVector();
304  paramsBase->SetQ(Quaternion<double>(Q[0], Q[1], Q[2], Q[3]));
305  } break;
306 
307  case ID_RMatrix:
308  {
309  Matrix<double> R = panelRMatrix_[cameraNr]->GetMatrix();
310  paramsBase->SetR(Matrix3x3<double>(R));
311  } break;
312 
313  case ID_ImageSize:
314  {
315  Vector<double> imageSize = panelImageSize_[cameraNr]->GetVector();
316  paramsBase->SetImageSize((int)imageSize[0], (int)imageSize[1]);
317  } break;
318 
319  case ID_AspectRatio:
320  {
321  Vector<double> aspectratio = panelAspectRatio_[cameraNr]->GetVector();
322  paramsBase->SetAspectratio(aspectratio[0]);
323  BIASASSERT(false); // aspect ratio is read-only
324  } break;
325 
326  case ID_Principal:
327  {
328  Vector<double> principal = panelPrincipal_[cameraNr]->GetVector();
329  paramsBase->SetPrincipal(principal[0], principal[1]);
330  } break;
331 
332  case ID_KMatrix:
333  {
334  BIASASSERT(false); // K matrix is read-only
335  } break;
336 
337  case ID_FocalLength:
338  {
339  Vector<double> focalLength = panelFocalLength_[cameraNr]->GetVector();
340  paramsPersp->SetFocalLengthAndAspect(focalLength[0],
341  paramsPersp->GetAspectratio());
342  } break;
343 
344  case ID_Undistortion:
345  {
346  Vector<double> undist = panelUndistortion_[cameraNr]->GetVector();
347  paramsPersp->SetUndistortion(undist[0], undist[1], undist[2], undist[3]);
348  } break;
349 
350  case ID_Radius:
351  {
352  Vector<double> radius = panelRadius_[cameraNr]->GetVector();
353  paramsSpherical->SetRadius(radius[0]);
354  } break;
355  }
356 
357  UpdateGUI(cameraNr);
358 }
359 
360 void wxProjectionPanel::OnNew(wxCommandEvent& /*event*/)
361 {
362  wxString choices[] = { wxT("Perspective"), wxT("Spherical") };
363  int numChoices = 2;
364 
365  wxSingleChoiceDialog dlg(NULL, wxT("Choose projection type:"),
366  wxT("New Projection"), numChoices, choices);
367  if (dlg.ShowModal() == wxID_OK)
368  {
369  switch (dlg.GetSelection())
370  {
371  case 0:
373  break;
374  case 1:
375  SetProjection(Projection(ProjectionParametersSpherical()));
376  break;
377  }
378  }
379 }
380 
381 void wxProjectionPanel::OnLoad(wxCommandEvent& /*event*/)
382 {
383 #ifdef BIAS_HAVE_XML2
384  wxFileDialog dlg(NULL, wxT("Load Projection"), wxT(""), wxT(""), wxT("*.*"),
385  wxFD_OPEN | wxFD_FILE_MUST_EXIST);
386  if (dlg.ShowModal() == wxID_OK)
387  {
388  Projection loadedProjection;
389  if (loadedProjection.Load(WxToAscii(dlg.GetPath())) != 0)
390  {
391  wxMessageDialog dlg(this, wxT("Failed to load projection"), wxT("Error"));
392  dlg.ShowModal();
393  }
394  else
395  {
396  SetProjection(loadedProjection);
397  }
398  }
399 #else
400  BIASERR("XML2 missing");
401 #endif
402 }
403 
404 void wxProjectionPanel::OnSave(wxCommandEvent& /*event*/)
405 {
406 #ifdef BIAS_HAVE_XML2
407  wxFileDialog dlg(NULL, wxT("Save Projection"), wxT(""), wxT(""), wxT("*.*"), wxFD_SAVE);
408  if (dlg.ShowModal() == wxID_OK)
409  {
410  if (GetProjection().XMLWrite(WxToAscii(dlg.GetPath())) != 0)
411  {
412  wxMessageDialog dlg(this, wxT("Failed to save projection"), wxT("Error"));
413  dlg.ShowModal();
414  }
415  }
416 #else
417  BIASERR("XML2 missing");
418 #endif
419 }
virtual BIAS::Vector3< double > GetC() const
Get projection center.
virtual void SetPrincipal(const double x, const double y)
Set principal point (in pixels relative to top left corner).
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual int GetPrincipal(double &PrincipalX, double &PrincipalY) const
Get principal point (in pixels relative to top left corner).
virtual void SetImageSize(const unsigned int w, const unsigned int h)
Set image dimensions (in pixels).
virtual std::string GetIdentifier() const
void GetUndistortion(double &kc1, double &kc2, double &kc3, double &kc4) const
void SetRadius(const double r)
Set radius of spherical image in pixels.
void Normalize()
Scales quaternion to unit length, i.e.
Definition: Quaternion.hh:236
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
virtual void SetQ(const BIAS::Quaternion< double > &Q)
Set orientation from unit quaternion Q.
Panel for displaying and editing projections.
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
virtual double GetAspectratio() const
Return aspectratio (i.e.
void GetFocalLength(double &f) const
Get the current camera focal length.
virtual bool QValid() const
Check if current orientation is valid.
virtual BIAS::RMatrix GetR() const
Get orientation as rotation matrix R.
virtual bool CValid() const
Check of current projection center is valid.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
virtual void SetAspectratio(const double AspectRatio)
Set CCD aspect ratio (i.e.
Panel for displaying and editing vectors.
void SetFocalLengthAndAspect(double f, double AspectRatio)
Set the current camera focal length in pixel and the a spect ratio.
double GetRadius() const
Return radius of spherical image in pixels.
Panel for displaying and editing matrices.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
virtual BIAS::Quaternion< double > GetQ() const
Get orientation as unit quaternion.
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429