[1482e6]: src / hugin1 / hugin / OptimizePanel.cpp  Maximize  Restore  History

Download this file

878 lines (772 with data), 30.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// -*- c-basic-offset: 4 -*-
/** @file OptimizePanel.cpp
*
* @brief implementation of OptimizePanel
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <config.h>
#include "panoinc_WX.h"
#include "panoinc.h"
#include "PT/PTOptimise.h"
#include "common/stl_utils.h"
#include "hugin/OptimizePanel.h"
#include "hugin/CommandHistory.h"
#include "hugin/MainFrame.h"
#include "base_wx/MyProgressDialog.h"
#include "base_wx/PTWXDlg.h"
#include "hugin/config_defaults.h"
using namespace std;
using namespace PT;
using namespace utils;
//============================================================================
//============================================================================
//============================================================================
BEGIN_EVENT_TABLE(OptimizePanel, wxPanel)
EVT_CLOSE(OptimizePanel::OnClose)
EVT_BUTTON(XRCID("optimize_frame_optimize"), OptimizePanel::OnOptimizeButton)
EVT_BUTTON(XRCID("opt_yaw_select"), OptimizePanel::OnListButton)
EVT_BUTTON(XRCID("opt_yaw_clear"), OptimizePanel::OnListButton)
EVT_BUTTON(XRCID("opt_pitch_select"), OptimizePanel::OnListButton)
EVT_BUTTON(XRCID("opt_pitch_clear"), OptimizePanel::OnListButton)
// EVT_BUTTON(XRCID("opt_pitch_equalize"), OptimizePanel::OnEqPitch)
EVT_BUTTON(XRCID("opt_roll_select"), OptimizePanel::OnListButton)
EVT_BUTTON(XRCID("opt_roll_clear"), OptimizePanel::OnListButton)
EVT_CHOICE(XRCID("optimize_panel_mode"), OptimizePanel::OnChangeMode)
// EVT_BUTTON(XRCID("opt_roll_equalize"), OptimizePanel::OnEqRoll)
EVT_CHECKLISTBOX(XRCID("optimizer_yaw_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_pitch_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_roll_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_v_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_a_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_b_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_c_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_d_list"), OptimizePanel::OnCheckBoxChanged)
EVT_CHECKLISTBOX(XRCID("optimizer_e_list"), OptimizePanel::OnCheckBoxChanged)
END_EVENT_TABLE()
// local optimize definition. need to be in sync with the xrc file
enum OptimizeMode { OPT_PAIRWISE=0, OPT_YRP, OPT_YRP_V,
OPT_YRP_B, OPT_YRP_BV, OPT_ALL, OPT_CUSTOM,
OPT_END_MARKER};
OptimizePanel::OptimizePanel()
{
DEBUG_TRACE("");
}
bool OptimizePanel::Create(wxWindow* parent, wxWindowID id , const wxPoint& pos, const wxSize& size, long style, const wxString& name)
{
DEBUG_TRACE("");
// Not needed here, wxPanel::Create is called by LoadPanel below
if (! wxPanel::Create(parent, id, pos, size, style, name) ) {
return false;
}
// create a sub-panel and load class into it!
// wxPanel::Create is called in here!
wxXmlResource::Get()->LoadPanel(this, wxT("optimize_panel"));
wxPanel * panel = XRCCTRL(*this, "optimize_panel", wxPanel);
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
topsizer->Add(panel, 1, wxEXPAND, 0);
SetSizer( topsizer );
m_only_active_images_cb = XRCCTRL(*this, "optimizer_only_active_images", wxCheckBox);
DEBUG_ASSERT(m_only_active_images_cb);
m_only_active_images_cb->SetValue(wxConfigBase::Get()->Read(wxT("/OptimizePanel/OnlyActiveImages"),1l) != 0);
m_yaw_list = XRCCTRL(*this, "optimizer_yaw_list", wxCheckListBox);
m_pitch_list = XRCCTRL(*this, "optimizer_pitch_list", wxCheckListBox);
m_roll_list = XRCCTRL(*this, "optimizer_roll_list", wxCheckListBox);
m_v_list = XRCCTRL(*this, "optimizer_v_list", wxCheckListBox);
m_a_list = XRCCTRL(*this, "optimizer_a_list", wxCheckListBox);
m_b_list = XRCCTRL(*this, "optimizer_b_list", wxCheckListBox);
m_c_list = XRCCTRL(*this, "optimizer_c_list", wxCheckListBox);
m_d_list = XRCCTRL(*this, "optimizer_d_list", wxCheckListBox);
m_e_list = XRCCTRL(*this, "optimizer_e_list", wxCheckListBox);
m_edit_cb = XRCCTRL(*this, "optimizer_edit_script", wxCheckBox);
DEBUG_ASSERT(m_edit_cb);
m_mode_cb = XRCCTRL(*this, "optimize_panel_mode", wxChoice);
DEBUG_ASSERT(m_mode_cb);
m_opt_ctrls = XRCCTRL(*this, "optimize_controls_panel", wxScrolledWindow);
DEBUG_ASSERT(m_opt_ctrls);
m_opt_ctrls->SetSizeHints(20, 20);
m_opt_ctrls->FitInside();
m_opt_ctrls->SetScrollRate(10, 10);
// disable the optimize panel controls by default
XRCCTRL(*this, "optimize_frame_optimize", wxButton)->Disable();
m_mode_cb->Disable();
m_edit_cb->Disable();
return true;
}
void OptimizePanel::Init(PT::Panorama * pano)
{
DEBUG_TRACE("");
m_pano = pano;
// wxConfigBase * config = wxConfigBase::Get();
// long w = config->Read(wxT("/OptimizerPanel/width"),-1);
// long h = config->Read(wxT("/OptimizerPanel/height"),-1);
// if (w != -1) {
// SetClientSize(w,h);
// }
wxCommandEvent dummy;
dummy.SetInt(m_mode_cb->GetSelection());
OnChangeMode(dummy);
// observe the panorama
m_pano->addObserver(this);
}
OptimizePanel::~OptimizePanel()
{
DEBUG_TRACE("dtor, writing config");
wxConfigBase::Get()->Write(wxT("/OptimizePanel/OnlyActiveImages"),m_only_active_images_cb->IsChecked() ? 1l : 0l);
// wxSize sz = GetClientSize();
// wxConfigBase * config = wxConfigBase::Get();
// config->Write(wxT("/OptimizerPanel/width"),sz.GetWidth());
// config->Write(wxT("/OptimizerPanel/height"),sz.GetHeight());
m_pano->removeObserver(this);
DEBUG_TRACE("dtor end");
}
void OptimizePanel::OnListButton(wxCommandEvent & e)
{
DEBUG_TRACE("");
if (e.GetId() == XRCID("opt_yaw_select")) {
SetCheckMark(m_yaw_list,true);
} else if (e.GetId() == XRCID("opt_yaw_clear")) {
SetCheckMark(m_yaw_list,false);
} else if (e.GetId() == XRCID("opt_pitch_select")) {
SetCheckMark(m_pitch_list,true);
} else if (e.GetId() == XRCID("opt_pitch_clear")) {
SetCheckMark(m_pitch_list,false);
} else if (e.GetId() == XRCID("opt_roll_select")) {
SetCheckMark(m_roll_list,true);
} else if (e.GetId() == XRCID("opt_roll_clear")) {
SetCheckMark(m_roll_list,false);
} else {
DEBUG_FATAL("An error has occured");
}
GlobalCmdHist::getInstance().addCommand(
new PT::UpdateOptimizeVectorCmd(*m_pano,getOptimizeVector()));
}
void OptimizePanel::OnOptimizeButton(wxCommandEvent & e)
{
DEBUG_TRACE("");
// run optimizer
// take the OptimizeVector from somewhere...
OptimizeVector optvars = getOptimizeVector();
m_pano->setOptimizeVector(optvars);
UIntSet imgs;
if (m_only_active_images_cb->IsChecked())
{
// use only selected images.
imgs = m_pano->getActiveImages();
if (imgs.size() == 0) {
// FIXME: pop up a dialog here alerting user no images are selected
// in preview, yet use only selected images is checked. We are in
// a string freeze at the moment.
return;
}
} else {
for (unsigned int i = 0 ; i < m_pano->getNrOfImages(); i++) {
imgs.insert(i);
}
}
runOptimizer(imgs);
}
void OptimizePanel::SetCheckMark(wxCheckListBox * l, int check)
{
int n = l->GetCount();
for (int i=0; i < n; i++) {
l->Check(i, check != 0);
}
}
OptimizeVector OptimizePanel::getOptimizeVector()
{
int nrLI = m_yaw_list->GetCount();
assert(nrLI >=0);
unsigned int nr = (unsigned int) nrLI;
unsigned int nImages = m_pano->getNrOfImages();
assert(nr == nImages);
OptimizeVector optvars;
for (unsigned int i=0; i < nImages; i++) {
set<string> imgopt;
// lens variables
unsigned int lensNr = m_pano->getImage(i).getLensNr();
if (m_v_list->IsChecked(lensNr)) {
imgopt.insert("v");
}
if (m_a_list->IsChecked(lensNr)) {
imgopt.insert("a");
}
if (m_b_list->IsChecked(lensNr)) {
imgopt.insert("b");
}
if (m_c_list->IsChecked(lensNr)) {
imgopt.insert("c");
}
if (m_d_list->IsChecked(lensNr)) {
imgopt.insert("d");
}
if (m_e_list->IsChecked(lensNr)) {
imgopt.insert("e");
}
// image variables
if (m_roll_list->IsChecked(i)) {
imgopt.insert("r");
}
if (m_pitch_list->IsChecked(i)) {
imgopt.insert("p");
}
if (m_yaw_list->IsChecked(i)) {
imgopt.insert("y");
}
optvars.push_back(imgopt);
}
return optvars;
}
void OptimizePanel::panoramaChanged(PT::Panorama & pano)
{
DEBUG_TRACE("");
setOptimizeVector(pano.getOptimizeVector());
// update accordingly to the choosen mode
wxCommandEvent dummy;
dummy.SetInt(m_mode_cb->GetSelection());
OnChangeMode(dummy);
}
void OptimizePanel::panoramaImagesChanged(PT::Panorama &pano,
const PT::UIntSet & imgNr)
{
DEBUG_TRACE("nr of changed images: " << imgNr.size());
if (pano.getNrOfImages() == 0)
{
XRCCTRL(*this, "optimize_frame_optimize", wxButton)->Disable();
m_mode_cb->Disable();
m_edit_cb->Disable();
} else {
XRCCTRL(*this, "optimize_frame_optimize", wxButton)->Enable();
m_mode_cb->Enable();
m_edit_cb->Enable();
}
// update lens values
int nrLensList = m_v_list->GetCount();
assert(nrLensList >=0);
unsigned int nr = (unsigned int) nrLensList;
unsigned int nLens = m_pano->getNrOfLenses();
while (nr < nLens) {
// add checkboxes.
m_v_list->Append(wxString::Format(wxT("%d"),nr));
m_a_list->Append(wxString::Format(wxT("%d"),nr));
m_b_list->Append(wxString::Format(wxT("%d"),nr));
m_c_list->Append(wxString::Format(wxT("%d"),nr));
m_d_list->Append(wxString::Format(wxT("%d"),nr));
m_e_list->Append(wxString::Format(wxT("%d"),nr));
nr++;
}
while (nr > nLens) {
if (nr == 0)
break;
m_v_list->Delete(nr-1);
m_a_list->Delete(nr-1);
m_b_list->Delete(nr-1);
m_c_list->Delete(nr-1);
m_d_list->Delete(nr-1);
m_e_list->Delete(nr-1);
nr--;
}
// add/remove items
int nrLI = m_yaw_list->GetCount();
assert(nrLI >=0);
nr = (unsigned int) nrLI;
unsigned int nImages = m_pano->getNrOfImages();
while (nr < nImages) {
// add checkboxes.
m_yaw_list->Append(wxString::Format(wxT("%d"),nr));
m_pitch_list->Append(wxString::Format(wxT("%d"),nr));
m_roll_list->Append(wxString::Format(wxT("%d"),nr));
nr++;
}
while (nr > nImages) {
if (nr == 0)
break;
m_yaw_list->Delete(nr-1);
m_pitch_list->Delete(nr-1);
m_roll_list->Delete(nr-1);
nr--;
}
// display values of the variables.
UIntSet::const_iterator it;
for (it = imgNr.begin(); it != imgNr.end(); it++) {
DEBUG_DEBUG("setting checkmarks for image " << *it)
const VariableMap & vars = pano.getImageVariables(*it);
// keep selections
bool sel = m_yaw_list->IsChecked(*it);
m_yaw_list->SetString(*it, wxString::Format(wxT("%d (%.3f)"),
*it, const_map_get(vars,"y").getValue()));
m_yaw_list->Check(*it,sel);
sel = m_pitch_list->IsChecked(*it);
m_pitch_list->SetString(*it, wxString::Format(wxT("%d (%.3f)"),
*it, const_map_get(vars,"p").getValue()));
m_pitch_list->Check(*it,sel);
sel = m_roll_list->IsChecked(*it);
m_roll_list->SetString(*it, wxString::Format(wxT("%d (%.3f)"),
*it, const_map_get(vars,"r").getValue()));
m_roll_list->Check(*it,sel);
}
// display lens values if they are linked
for (unsigned int i=0; i < nLens; i++) {
const Lens & lens = pano.getLens(i);
const LensVariable & v = const_map_get(lens.variables,"v");
bool sel = m_v_list->IsChecked(i);
if (v.isLinked()) {
m_v_list->SetString(i,wxString::Format(wxT("%d (%.2f)"),i, v.getValue()));
} else {
m_v_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_v_list->Check(i,sel);
sel = m_a_list->IsChecked(i);
const LensVariable & a = const_map_get(lens.variables,"a");
if (a.isLinked()) {
m_a_list->SetString(i,wxString::Format(wxT("%d (%.3f)"),i, a.getValue()));
} else {
m_a_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_a_list->Check(i,sel);
sel = m_b_list->IsChecked(i);
const LensVariable & b = const_map_get(lens.variables,"b");
if (b.isLinked()) {
m_b_list->SetString(i,wxString::Format(wxT("%d (%.3f)"),i, b.getValue()));
} else {
m_b_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_b_list->Check(i,sel);
sel = m_c_list->IsChecked(i);
const LensVariable & c = const_map_get(lens.variables,"c");
if (c.isLinked()) {
m_c_list->SetString(i,wxString::Format(wxT("%d (%.3f)"),i, c.getValue()));
} else {
m_c_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_c_list->Check(i,sel);
sel = m_d_list->IsChecked(i);
const LensVariable & d = const_map_get(lens.variables,"d");
if (d.isLinked()) {
m_d_list->SetString(i,wxString::Format(wxT("%d (%.1f)"),i, d.getValue()));
} else {
m_d_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_d_list->Check(i,sel);
sel = m_e_list->IsChecked(i);
const LensVariable & e = const_map_get(lens.variables,"e");
if (e.isLinked()) {
m_e_list->SetString(i,wxString::Format(wxT("%d (%.1f)"),i, e.getValue()));
} else {
m_e_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_e_list->Check(i,sel);
}
// update automatic checkmarks
wxCommandEvent dummy;
dummy.SetInt(m_mode_cb->GetSelection());
OnChangeMode(dummy);
}
void OptimizePanel::setModeCustom()
{
DEBUG_TRACE("Setting optmizer preset to custom");
m_mode_cb->SetSelection(OPT_CUSTOM);
}
void OptimizePanel::setOptimizeVector(const OptimizeVector & optvec)
{
DEBUG_ASSERT((int)optvec.size() == (int)m_yaw_list->GetCount());
for (int i=0; i < (int) m_pano->getNrOfLenses(); i++) {
m_v_list->Check(i,false);
m_a_list->Check(i,false);
m_b_list->Check(i,false);
m_c_list->Check(i,false);
m_d_list->Check(i,false);
m_e_list->Check(i,false);
}
unsigned int nImages = optvec.size();
for (unsigned int i=0; i < nImages; i++) {
m_yaw_list->Check(i,false);
m_pitch_list->Check(i,false);
m_roll_list->Check(i,false);
unsigned int lensNr = m_pano->getImage(i).getLensNr();
for(set<string>::const_iterator it = optvec[i].begin();
it != optvec[i].end(); ++it)
{
if (*it == "y") {
m_yaw_list->Check(i);
}
if (*it == "p") {
m_pitch_list->Check(i);
}
if (*it == "r") {
m_roll_list->Check(i);
}
if (*it == "v") {
m_v_list->Check(lensNr);
}
if (*it == "a") {
m_a_list->Check(lensNr);
}
if (*it == "b") {
m_b_list->Check(lensNr);
}
if (*it == "c") {
m_c_list->Check(lensNr);
}
if (*it == "d") {
m_d_list->Check(lensNr);
}
if (*it == "e") {
m_e_list->Check(lensNr);
}
}
}
}
void OptimizePanel::runOptimizer(const UIntSet & imgs)
{
DEBUG_TRACE("");
// open window that shows a status dialog, and allows to
// apply the results
int mode = m_mode_cb->GetSelection();
Panorama optPano = m_pano->getSubset(imgs);
PanoramaOptions opts = optPano.getOptions();
switch(opts.getProjection()) {
case PanoramaOptions::RECTILINEAR:
case PanoramaOptions::CYLINDRICAL:
case PanoramaOptions::EQUIRECTANGULAR:
break;
default:
// temporarily change to equirectangular
opts.setProjection(PanoramaOptions::EQUIRECTANGULAR);
optPano.setOptions(opts);
break;
}
UIntSet allImg;
fill_set(allImg,0, imgs.size()-1);
char *p = setlocale(LC_ALL,NULL);
char *oldlocale = strdup(p);
setlocale(LC_ALL,"C");
if (mode == OPT_PAIRWISE) {
std::set<std::string> optvars2;
optvars2.insert("y");
optvars2.insert("p");
optvars2.insert("r");
// remove vertical and horizontal control points
CPVector cps = optPano.getCtrlPoints();
CPVector newCP;
for (CPVector::const_iterator it = cps.begin(); it != cps.end(); it++) {
if (it->mode == ControlPoint::X_Y)
{
newCP.push_back(*it);
}
}
optPano.setCtrlPoints(newCP);
// temporarily disable PT progress dialog..
deregisterPTWXDlgFcn();
{
wxBusyCursor bc;
// run pairwise optimizer
PTools::autoOptimise(optPano);
}
#ifdef DEBUG
// print optimized script to cout
DEBUG_DEBUG("panorama after autoOptimise():");
optPano.printPanoramaScript(std::cerr, optPano.getOptimizeVector(), optPano.getOptions(), allImg, false);
#endif
registerPTWXDlgFcn(MainFrame::Get());
// do global optimisation
optPano.setCtrlPoints(cps);
PTools::optimize(optPano);
#ifdef DEBUG
// print optimized script to cout
DEBUG_DEBUG("panorama after optimise():");
optPano.printPanoramaScript(std::cerr, optPano.getOptimizeVector(), optPano.getOptions(), allImg, false);
#endif
} else {
if (m_edit_cb->IsChecked()) {
// show and edit script..
ostringstream scriptbuf;
optPano.printPanoramaScript(scriptbuf, optPano.getOptimizeVector(), optPano.getOptions(), allImg, true);
// open a text dialog with an editor inside
wxDialog * edit_dlg = wxXmlResource::Get()->LoadDialog(this, wxT("edit_script_dialog"));
wxTextCtrl *txtCtrl=XRCCTRL(*edit_dlg,"script_edit_text",wxTextCtrl);
txtCtrl->SetValue(wxString(scriptbuf.str().c_str(), *wxConvCurrent));
char * script = 0;
if (edit_dlg->ShowModal() == wxID_OK) {
script = strdup(txtCtrl->GetValue().mb_str(*wxConvCurrent));
} else {
return;
}
PTools::optimize(optPano, script);
free(script);
} else {
PTools::optimize(optPano);
}
#ifdef DEBUG
// print optimized script to cout
DEBUG_DEBUG("panorama after optimise():");
optPano.printPanoramaScript(std::cerr, optPano.getOptimizeVector(), optPano.getOptions(), allImg, false);
#endif
}
setlocale(LC_ALL,oldlocale);
free(oldlocale);
// calculate control point errors and display text.
if (AskApplyResult(optPano)) {
GlobalCmdHist::getInstance().addCommand(
new PT::UpdateVariablesCPSetCmd(*m_pano, imgs, optPano.getVariables(), optPano.getCtrlPoints())
);
}
}
bool OptimizePanel::AskApplyResult(const Panorama & pano)
{
double min;
double max;
double mean;
double var;
pano.calcCtrlPntsErrorStats( min, max, mean, var);
// check for HFOV lines. if smaller than 1 report a warning;
// also check for high distortion coefficients.
bool smallHFOV=false;
bool highDist = false;
const VariableMapVector & vars = pano.getVariables();
for (VariableMapVector::const_iterator it = vars.begin() ; it != vars.end(); it++)
{
if (const_map_get(*it,"v").getValue() < 1.0) smallHFOV = true;
if (fabs(const_map_get(*it,"a").getValue()) > 0.8) highDist = true;
if (fabs(const_map_get(*it,"b").getValue()) > 0.8) highDist = true;
if (fabs(const_map_get(*it,"c").getValue()) > 0.8) highDist = true;
}
wxString msg;
int style=0;
if (smallHFOV) {
msg.Printf( _("Optimizer run finished.\nWARNING: a very small Field of View (v) has been estimated\n\nThe results are probably invalid.\n\nOptimization of the Field of View (v) of partial panoramas can lead to bad results.\nTry adding more images and control points.\n\nApply the changes anyway?"));
style = wxYES_NO;
} else if (highDist) {
msg.Printf(_("Optimizer run finished.\nResults:\n average control point distance: %f\n standard deviation: %f\n maximum: %f\n\n*WARNING*: very high distortion coefficients (a,b,c) have been estimated.\nThe results are probably invalid.\nOnly optimize all distortion parameters when many, well spread control points are used.\nPlease reset the a,b and c parameters to zero and add more control points\n\nApply the changes anyway?"),
mean, sqrt(var), max);
style = wxYES_NO | wxICON_EXCLAMATION;
} else {
msg.Printf(_("Optimizer run finished.\nResults:\n average control point distance: %f\n standard deviation: %f\n maximum: %f\n\nApply the changes?"),
mean, sqrt(var), max);
style = wxYES_NO | wxICON_EXCLAMATION;
}
int id = wxMessageBox(msg,_("Optimization result"),style,this);
return id == wxYES;
}
void OptimizePanel::OnClose(wxCloseEvent& event)
{
DEBUG_TRACE("OnClose");
// do not close, just hide if we're not forced
if (event.CanVeto()) {
event.Veto();
Hide();
DEBUG_DEBUG("Hiding");
} else {
DEBUG_DEBUG("Closing");
Destroy();
}
}
void OptimizePanel::OnChangeMode(wxCommandEvent & e)
{
DEBUG_TRACE("");
int mode = m_mode_cb->GetSelection();
DEBUG_ASSERT(mode >= 0 && mode < OPT_END_MARKER);
if (m_pano->getNrOfImages() == 0)
{
XRCCTRL(*this, "opt_yaw_select", wxButton)->Disable();
XRCCTRL(*this, "opt_roll_select", wxButton)->Disable();
XRCCTRL(*this, "opt_pitch_select", wxButton)->Disable();
XRCCTRL(*this, "opt_yaw_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_roll_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_pitch_clear", wxButton)->Disable();
} else {
switch (mode) {
case OPT_PAIRWISE:
// smart auto optimize
SetCheckMark(m_yaw_list,true);
SetCheckMark(m_roll_list,true);
SetCheckMark(m_pitch_list,true);
SetCheckMark(m_v_list,false);
SetCheckMark(m_a_list,false);
SetCheckMark(m_b_list,false);
SetCheckMark(m_c_list,false);
SetCheckMark(m_d_list,false);
SetCheckMark(m_e_list,false);
break;
case OPT_YRP:
// simple position
SetCheckMark(m_yaw_list,true);
SetCheckMark(m_roll_list,true);
SetCheckMark(m_pitch_list,true);
SetCheckMark(m_v_list,false);
SetCheckMark(m_a_list,false);
SetCheckMark(m_b_list,false);
SetCheckMark(m_c_list,false);
SetCheckMark(m_d_list,false);
SetCheckMark(m_e_list,false);
break;
case OPT_YRP_V:
// v + position
SetCheckMark(m_yaw_list,true);
SetCheckMark(m_roll_list,true);
SetCheckMark(m_pitch_list,true);
SetCheckMark(m_v_list,true);
SetCheckMark(m_a_list,false);
SetCheckMark(m_b_list,false);
SetCheckMark(m_c_list,false);
SetCheckMark(m_d_list,false);
SetCheckMark(m_e_list,false);
break;
case OPT_YRP_B:
// important lens distortion + position
SetCheckMark(m_yaw_list,true);
SetCheckMark(m_roll_list,true);
SetCheckMark(m_pitch_list,true);
SetCheckMark(m_v_list,false);
SetCheckMark(m_a_list,false);
SetCheckMark(m_b_list,true);
SetCheckMark(m_c_list,false);
SetCheckMark(m_d_list,false);
SetCheckMark(m_e_list,false);
break;
case OPT_YRP_BV:
// important lens distortion + v + position
SetCheckMark(m_yaw_list,true);
SetCheckMark(m_roll_list,true);
SetCheckMark(m_pitch_list,true);
SetCheckMark(m_v_list,true);
SetCheckMark(m_a_list,false);
SetCheckMark(m_b_list,true);
SetCheckMark(m_c_list,false);
SetCheckMark(m_d_list,false);
SetCheckMark(m_e_list,false);
break;
case OPT_ALL:
// everything
SetCheckMark(m_yaw_list,true);
SetCheckMark(m_roll_list,true);
SetCheckMark(m_pitch_list,true);
SetCheckMark(m_v_list,true);
SetCheckMark(m_a_list,true);
SetCheckMark(m_b_list,true);
SetCheckMark(m_c_list,true);
SetCheckMark(m_d_list,true);
SetCheckMark(m_e_list,true);
break;
case OPT_CUSTOM:
break;
}
// do not try to do anything on our own
// if the user selected custom
if (mode != OPT_CUSTOM && m_pano->getNrOfImages() > 0)
{
// get anchor image
unsigned int refImg = m_pano->getOptions().optimizeReferenceImage;
// count number of vertical/horizontal control points
int nHCP = 0;
int nVCP = 0;
const CPVector & cps = m_pano->getCtrlPoints();
for (CPVector::const_iterator it = cps.begin(); it != cps.end(); it++)
{
// control points
if (it->mode == ControlPoint::X)
{
nVCP++;
} else if (it->mode == ControlPoint::Y)
{
nHCP++;
}
}
// try to select sensible position optimisation parameters,
// dependent on output projection
switch (m_pano->getOptions().getProjection()) {
case PT::PanoramaOptions::RECTILINEAR:
m_roll_list->Check(refImg, (nVCP+nHCP) >=1 );
m_yaw_list->Check(refImg, (nVCP+nHCP) >=3 && (nVCP >= 1) && (nHCP >= 1) );
m_pitch_list->Check(refImg, (nVCP+nHCP) >= 2 );
break;
case PT::PanoramaOptions::CYLINDRICAL:
case PT::PanoramaOptions::EQUIRECTANGULAR:
m_yaw_list->Check(refImg,false);
m_pitch_list->Check(refImg, (nHCP+nVCP > 1));
m_roll_list->Check(refImg, (nHCP+nVCP >= 1));
break;
default:
break;
}
// disable all manual settings
m_yaw_list->Disable();
m_pitch_list->Disable();
m_roll_list->Disable();
m_v_list->Disable();
m_a_list->Disable();
m_b_list->Disable();
m_c_list->Disable();
m_d_list->Disable();
m_e_list->Disable();
XRCCTRL(*this, "opt_yaw_select", wxButton)->Disable();
XRCCTRL(*this, "opt_roll_select", wxButton)->Disable();
XRCCTRL(*this, "opt_pitch_select", wxButton)->Disable();
XRCCTRL(*this, "opt_yaw_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_roll_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_pitch_clear", wxButton)->Disable();
} else {
m_yaw_list->Enable();
m_pitch_list->Enable();
m_roll_list->Enable();
m_v_list->Enable();
m_a_list->Enable();
m_b_list->Enable();
m_c_list->Enable();
m_d_list->Enable();
m_e_list->Enable();
XRCCTRL(*this, "opt_yaw_select", wxButton)->Enable();
XRCCTRL(*this, "opt_roll_select", wxButton)->Enable();
XRCCTRL(*this, "opt_pitch_select", wxButton)->Enable();
XRCCTRL(*this, "opt_yaw_clear", wxButton)->Enable();
XRCCTRL(*this, "opt_roll_clear", wxButton)->Enable();
XRCCTRL(*this, "opt_pitch_clear", wxButton)->Enable();
}
}
m_edit_cb->Enable(mode != OPT_PAIRWISE);
}
void OptimizePanel::OnCheckBoxChanged(wxCommandEvent & e)
{
DEBUG_TRACE("");
GlobalCmdHist::getInstance().addCommand(
new PT::UpdateOptimizeVectorCmd(*m_pano,getOptimizeVector()));
}
IMPLEMENT_DYNAMIC_CLASS(OptimizePanel, wxPanel)
OptimizePanelXmlHandler::OptimizePanelXmlHandler()
: wxXmlResourceHandler()
{
AddWindowStyles();
}
wxObject *OptimizePanelXmlHandler::DoCreateResource()
{
XRC_MAKE_INSTANCE(cp, OptimizePanel)
cp->Create(m_parentAsWindow,
GetID(),
GetPosition(), GetSize(),
GetStyle(wxT("style")),
GetName());
SetupWindow( cp);
return cp;
}
bool OptimizePanelXmlHandler::CanHandle(wxXmlNode *node)
{
return IsOfClass(node, wxT("OptimizePanel"));
}
IMPLEMENT_DYNAMIC_CLASS(OptimizePanelXmlHandler, wxXmlResourceHandler)