HFOVDialog.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00028 #include "hugin_config.h"
00029 #include "panoinc_WX.h"
00030 #include "panoinc.h"
00031 
00032 #include "wxPlatform.h"
00033 #include "platform.h"
00034 #include "LensTools.h"
00035 #include "HFOVDialog.h"
00036 
00037 BEGIN_EVENT_TABLE(HFOVDialog, wxDialog)
00038     EVT_CHOICE (XRCID("lensdlg_type_choice"),HFOVDialog::OnTypeChanged)
00039     EVT_TEXT ( XRCID("lensdlg_cropfactor_text"), HFOVDialog::OnCropFactorChanged )
00040     EVT_TEXT ( XRCID("lensdlg_hfov_text"), HFOVDialog::OnHFOVChanged )
00041     EVT_TEXT ( XRCID("lensdlg_focallength_text"), HFOVDialog::OnFocalLengthChanged )
00042     EVT_BUTTON( XRCID("lensdlg_load_lens_button"), HFOVDialog::OnLoadLensParameters )
00043     EVT_BUTTON ( wxID_OK, HFOVDialog::OnOk)
00044 END_EVENT_TABLE()
00045 
00046 HFOVDialog::HFOVDialog(wxWindow * parent, HuginBase::SrcPanoImage & srcImg)
00047     : m_srcImg(srcImg)
00048 {
00049     m_HFOV = srcImg.getHFOV();
00050     m_focalLength = srcImg.getExifFocalLength();
00051     m_cropFactor = srcImg.getCropFactor();
00052     wxXmlResource::Get()->LoadDialog(this, parent, wxT("dlg_focallength"));
00053 
00054     m_cropText = XRCCTRL(*this, "lensdlg_cropfactor_text", wxTextCtrl);
00055     DEBUG_ASSERT(m_cropText);
00056 
00057     m_hfovText = XRCCTRL(*this, "lensdlg_hfov_text", wxTextCtrl);
00058     DEBUG_ASSERT(m_hfovText);
00059 
00060     m_focalLengthText = XRCCTRL(*this, "lensdlg_focallength_text", wxTextCtrl);
00061     DEBUG_ASSERT(m_focalLengthText);
00062 
00063     m_projChoice = XRCCTRL(*this, "lensdlg_type_choice", wxChoice);
00064     DEBUG_ASSERT(m_projChoice);
00065     FillLensProjectionList(m_projChoice);
00066 
00067     m_okButton = XRCCTRL(*this, "wxID_OK", wxButton);
00068     DEBUG_ASSERT(m_okButton);
00069 
00070     // fill fields
00071     wxString fn(srcImg.getFilename().c_str(), HUGIN_CONV_FILENAME);
00072     wxString message;
00073     message.Printf(_("No or only partial information about field of view was found in image file\n%s\n\nPlease enter the horizontal field of view (HFOV) or the focal length and crop factor."), fn.c_str());
00074     XRCCTRL(*this, "lensdlg_message", wxStaticText)->SetLabel(message);
00075     SelectListValue(m_projChoice, m_srcImg.getProjection());
00076 
00077     if (m_cropFactor > 0 && m_focalLength > 0) {
00078         // everything is well known.. compute HFOV
00079         m_HFOV = HuginBase::SrcPanoImage::calcHFOV(m_srcImg.getProjection(), m_focalLength,
00080                           m_cropFactor, m_srcImg.getSize());
00081 
00082         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00083         m_hfovText->SetValue(m_HFOVStr);
00084         m_focalLengthStr = hugin_utils::doubleTowxString(m_focalLength,2);
00085         m_focalLengthText->SetValue(m_focalLengthStr);
00086         m_cropFactorStr = hugin_utils::doubleTowxString(m_cropFactor,2);
00087         m_cropText->SetValue(m_cropFactorStr);
00088     } else if (m_cropFactor > 0 && m_focalLength <= 0) {
00089         // focal length unknown
00090         m_cropFactorStr = hugin_utils::doubleTowxString(m_cropFactor,2);
00091         m_cropText->SetValue(m_cropFactorStr);
00092         m_okButton->Disable();
00093     } else if (m_cropFactor <= 0 && m_focalLength > 0) {
00094         // crop factor unknown
00095         m_focalLengthStr = hugin_utils::doubleTowxString(m_focalLength,2);
00096         m_focalLengthText->SetValue(m_focalLengthStr);
00097         m_okButton->Disable();
00098     } else {
00099         // everything unknown
00100         // assume a crop factor of one
00101         m_cropFactor = 1;
00102         m_cropFactorStr = hugin_utils::doubleTowxString(m_cropFactor,2);
00103         m_cropText->SetValue(m_cropFactorStr);
00104         m_okButton->Disable();
00105     }
00106     // set a proper size for this dialog
00107     this->GetSizer()->SetSizeHints(this);
00108 }
00109 
00110 void HFOVDialog::OnTypeChanged(wxCommandEvent & e)
00111 {
00112     HuginBase::SrcPanoImage::Projection new_proj = (HuginBase::SrcPanoImage::Projection)(GetSelectedValue(m_projChoice));
00113     DEBUG_DEBUG("new type: " << new_proj);
00114     m_srcImg.setProjection(new_proj);
00115     if (m_cropFactor > 0 && m_focalLength > 0) {
00116         m_HFOV = HuginBase::SrcPanoImage::calcHFOV(m_srcImg.getProjection(), m_focalLength,
00117                           m_cropFactor, m_srcImg.getSize());
00118         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00119         m_hfovText->SetValue(m_HFOVStr);
00120     }
00121 }
00122 
00123 void HFOVDialog::OnHFOVChanged(wxCommandEvent & e)
00124 {
00125     wxString text = m_hfovText->GetValue();
00126     DEBUG_DEBUG("state: " <<  m_HFOVStr.mb_str(wxConvLocal) << ", change:" << text.mb_str(wxConvLocal));
00127     DEBUG_DEBUG("cmd str: " << e.GetString().mb_str(wxConvLocal));
00128     if (text.empty()) {
00129         // ignore all empty hfov changes
00130         return;
00131     }
00132     // ignore changes caused by ourself
00133     if (m_hfovText->GetValue() == m_HFOVStr) {
00134         DEBUG_DEBUG("ignoring programatic HFOV change");
00135         return;
00136     }
00137 
00138     // accept change
00139     m_HFOVStr = text;
00140 
00141     if (text.empty()) {
00142         m_HFOV = 0;
00143         m_okButton->Disable();
00144         return;
00145     }
00146 
00147     if (!hugin_utils::str2double(text, m_HFOV)) {
00148         m_okButton->Disable();
00149         return;
00150     }
00151 
00152     DEBUG_DEBUG(m_HFOV);
00153 
00154     if (m_HFOV <= 0) {
00155         wxMessageBox(_("The horizontal field of view must be positive."));
00156         m_HFOV = 50;
00157         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00158         m_hfovText->SetValue(m_HFOVStr);
00159         return;
00160     }
00161 
00162     if (m_srcImg.getProjection() == HuginBase::SrcPanoImage::RECTILINEAR && m_HFOV > 179) {
00163         DEBUG_DEBUG("HFOV " << m_HFOV << " too big, resetting to 179");
00164         m_HFOV=179;
00165         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00166         m_hfovText->SetValue(m_HFOVStr);
00167     }
00168 
00169     if (m_cropFactor > 0) {
00170         // set focal length only if crop factor is known
00171         m_focalLength = HuginBase::SrcPanoImage::calcFocalLength(m_srcImg.getProjection(), m_HFOV, m_cropFactor, m_srcImg.getSize());
00172         m_focalLengthStr = hugin_utils::doubleTowxString(m_focalLength,2);
00173         m_focalLengthText->SetValue(m_focalLengthStr);
00174     }
00175     m_okButton->Enable();
00176 }
00177 
00178 void HFOVDialog::OnFocalLengthChanged(wxCommandEvent & e)
00179 {
00180     wxString text = m_focalLengthText->GetValue();
00181     DEBUG_DEBUG(m_focalLengthStr.mb_str(wxConvLocal) << " => " << text.mb_str(wxConvLocal));
00182     // ignore changes caused by ourself
00183     if (m_focalLengthText->GetValue() == m_focalLengthStr) {
00184         DEBUG_DEBUG("ignore focal length change");
00185         return;
00186     }
00187     // accept change
00188     m_focalLengthStr = text;
00189 
00190     if (text.empty()) {
00191         m_focalLength = 0;
00192         return;
00193     }
00194     if (!hugin_utils::str2double(text, m_focalLength)) {
00195         return;
00196     }
00197     DEBUG_DEBUG(m_focalLength);
00198 
00199     // ignore leading zeros..
00200     if (m_focalLength == 0) {
00201         return;
00202     }
00203     if (m_focalLength <= 0) {
00204         m_focalLength=1;
00205         m_focalLengthStr = hugin_utils::doubleTowxString(m_focalLength,2);
00206         m_focalLengthText->SetValue(m_focalLengthStr);
00207         wxMessageBox(_("The focal length must be positive."));
00208     }
00209 
00210     if (m_cropFactor > 0) {
00211         // calculate HFOV.
00212         m_HFOV = HuginBase::SrcPanoImage::calcHFOV(m_srcImg.getProjection(), m_focalLength,
00213                           m_cropFactor, m_srcImg.getSize());
00214         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00215         m_hfovText->SetValue(m_HFOVStr);
00216         m_okButton->Enable();
00217     }
00218 }
00219 
00220 void HFOVDialog::OnCropFactorChanged(wxCommandEvent & e)
00221 {
00222     // ignore changesd cause by ourself
00223     wxString text = m_cropText->GetValue();
00224     DEBUG_DEBUG(m_cropFactorStr.mb_str(wxConvLocal) << " => " << text.mb_str(wxConvLocal));
00225     if (text == m_cropFactorStr) {
00226         DEBUG_DEBUG("ignore crop change");
00227         return;
00228     }
00229         // accept change
00230     m_cropFactorStr = text;
00231 
00232     if (text.empty()) {
00233         m_cropFactor = 0;
00234         return;
00235     }
00236     if (!hugin_utils::str2double(text, m_cropFactor)) {
00237         return;
00238     }
00239 
00240     // ignore leading zeros..
00241     if (m_cropFactor == 0) {
00242         m_cropFactorStr = text;
00243         return;
00244     }
00245 
00246     if (m_cropFactor <= 0) {
00247         wxMessageBox(_("The crop factor must be positive."));
00248         m_cropFactor=1;
00249         m_cropFactorStr = hugin_utils::doubleTowxString(m_cropFactor,2);
00250         m_cropText->SetValue(m_cropFactorStr);
00251         return;
00252     }
00253 
00254     if (m_focalLength > 0) {
00255         m_HFOV = HuginBase::SrcPanoImage::calcHFOV(m_srcImg.getProjection(), m_focalLength,
00256                           m_cropFactor, m_srcImg.getSize());
00257         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00258         m_hfovText->SetValue(m_HFOVStr);
00259         m_okButton->Enable();
00260     }
00261 }
00262 
00263 void HFOVDialog::OnLoadLensParameters(wxCommandEvent & e)
00264 {
00265     HuginBase::Lens lens;
00266     lens.setImageSize(m_srcImg.getSize());
00267 
00268     bool cropped=false;
00269     bool autoCenterCrop=false;
00270     vigra::Rect2D cropRect;
00271 
00272     if (LoadLensParametersChoose(this, lens, cropped, autoCenterCrop, cropRect)) {
00273         m_HFOV = lens.getHFOV();
00274         m_cropFactor = lens.getCropFactor();
00275 
00276         m_srcImg.setCropFactor(lens.getCropFactor());
00277         m_srcImg.setHFOV(const_map_get(lens.variables,"v").getValue());
00278         m_srcImg.setProjection((HuginBase::SrcPanoImage::Projection) lens.getProjection());
00279 
00280         m_focalLength = HuginBase::SrcPanoImage::calcFocalLength(m_srcImg.getProjection(), m_HFOV, m_cropFactor, m_srcImg.getSize());
00281 
00282         // geometrical distortion correction
00283         std::vector<double> radialDist(4);
00284         radialDist[0] = const_map_get(lens.variables,"a").getValue();
00285         radialDist[1] = const_map_get(lens.variables,"b").getValue();
00286         radialDist[2] = const_map_get(lens.variables,"c").getValue();
00287         radialDist[3] = 1 - radialDist[0] - radialDist[1] - radialDist[2];
00288         m_srcImg.setRadialDistortion(radialDist);
00289         hugin_utils::FDiff2D t;
00290         t.x = const_map_get(lens.variables,"d").getValue();
00291         t.y = const_map_get(lens.variables,"e").getValue();
00292         m_srcImg.setRadialDistortionCenterShift(t);
00293         t.x = const_map_get(lens.variables,"g").getValue();
00294         t.y = const_map_get(lens.variables,"t").getValue();
00295         m_srcImg.setShear(t);
00296 
00297     // vignetting
00298         std::vector<double> vigCorrCoeff(4);
00299         vigCorrCoeff[0] = const_map_get(lens.variables,"Va").getValue();
00300         vigCorrCoeff[1] = const_map_get(lens.variables,"Vb").getValue();
00301         vigCorrCoeff[2] = const_map_get(lens.variables,"Vc").getValue();
00302         vigCorrCoeff[3] = const_map_get(lens.variables,"Vd").getValue();
00303         m_srcImg.setRadialVigCorrCoeff(vigCorrCoeff);
00304         t.x = const_map_get(lens.variables,"Vx").getValue();
00305         t.y = const_map_get(lens.variables,"Vy").getValue();
00306         m_srcImg.setRadialVigCorrCenterShift(t);
00307 
00308 //        m_srcImg.setExposureValue(const_map_get(vars,"Eev").getValue());
00309         m_srcImg.setWhiteBalanceRed(const_map_get(lens.variables,"Er").getValue());
00310         m_srcImg.setWhiteBalanceBlue(const_map_get(lens.variables,"Eb").getValue());
00311 
00312         std::vector<float> resp(5);
00313         resp[0] = const_map_get(lens.variables,"Ra").getValue();
00314         resp[1] = const_map_get(lens.variables,"Rb").getValue();
00315         resp[2] = const_map_get(lens.variables,"Rc").getValue();
00316         resp[3] = const_map_get(lens.variables,"Rd").getValue();
00317         resp[4] = const_map_get(lens.variables,"Re").getValue();
00318         m_srcImg.setEMoRParams(resp);
00319 
00320         if (!cropped)
00321         {
00322             m_srcImg.setCropMode(HuginBase::SrcPanoImage::NO_CROP);
00323         }
00324         else
00325         {
00326             if (m_srcImg.isCircularCrop())
00327             {
00328                 m_srcImg.setCropMode(HuginBase::SrcPanoImage::CROP_CIRCLE);
00329             }
00330             else
00331             {
00332                 m_srcImg.setCropMode(HuginBase::SrcPanoImage::CROP_RECTANGLE);
00333             };
00334             m_srcImg.setCropRect(cropRect);
00335         };
00336 
00337         // display in GUI
00338         m_focalLengthStr = hugin_utils::doubleTowxString(m_focalLength,2);
00339         m_focalLengthText->SetValue(m_focalLengthStr);
00340         m_cropFactorStr = hugin_utils::doubleTowxString(m_cropFactor,2);
00341         m_cropText->SetValue(m_cropFactorStr);
00342         m_HFOVStr = hugin_utils::doubleTowxString(m_HFOV,2);
00343         m_hfovText->SetValue(m_HFOVStr);
00344         SelectListValue(m_projChoice, m_srcImg.getProjection());
00345 
00346         // update lens type
00347         m_okButton->Enable();
00348     }
00349 }
00350 
00351 
00352 HuginBase::SrcPanoImage HFOVDialog::GetSrcImage()
00353 {
00354     m_srcImg.setCropFactor(m_cropFactor);
00355     m_srcImg.setHFOV(m_HFOV);
00356     return m_srcImg;
00357 }
00358 
00359 double HFOVDialog::GetCropFactor()
00360 {
00361     return m_cropFactor;
00362 }
00363 
00364 double HFOVDialog::GetFocalLength()
00365 {
00366     return m_focalLength;
00367 }
00368 
00369 void HFOVDialog::OnOk(wxCommandEvent & e)
00370 {
00371     if (m_srcImg.getProjection() == HuginBase::SrcPanoImage::FISHEYE_ORTHOGRAPHIC && m_HFOV>190)
00372     {
00373         if(wxMessageBox(
00374             wxString::Format(_("You have given a field of view of %.2f degrees.\n But the orthographic projection is limited to a field of view of 180 degress.\nDo you want still use that high value?"), m_HFOV),
00375 #ifdef __WXMSW__
00376             _("Hugin"),
00377 #else
00378             wxT(""),
00379 #endif
00380             wxICON_EXCLAMATION | wxYES_NO)==wxNO)
00381         {
00382             return;
00383         };
00384     };
00385     EndModal(wxID_OK);
00386 };

Generated on 26 Apr 2018 for Hugintrunk by  doxygen 1.4.7