-
Notifications
You must be signed in to change notification settings - Fork 23
/
ExtrinsicCalibrator.cpp
1468 lines (1261 loc) · 53.8 KB
/
ExtrinsicCalibrator.cpp
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
ExtrinsicCalibrator - Utility to calibrate a 3D camera with a 6-DOF
tracking system.
Copyright (c) 2016-2018 Oliver Kreylos
This file is part of the Virtual Reality User Interface Library (Vrui).
The Virtual Reality User Interface Library 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.
The Virtual Reality User Interface Library 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 the Virtual Reality User Interface Library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <string.h>
#include <stdlib.h>
#include <utility>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <Misc/FunctionCalls.h>
#include <Misc/MessageLogger.h>
#include <Threads/TripleBuffer.h>
#include <IO/File.h>
#include <IO/OStream.h>
#include <Cluster/OpenPipe.h>
#include <Math/Math.h>
#include <Math/Constants.h>
#include <Math/Random.h>
#include <Math/Matrix.h>
#include <Math/VarianceAccumulator.h>
#include <Geometry/AffineCombiner.h>
#include <Geometry/Rotation.h>
#include <Geometry/OrthogonalTransformation.h>
#include <Geometry/AffineTransformation.h>
#include <Geometry/GeometryValueCoders.h>
#include <Geometry/AlignPoints.h>
#include <GL/gl.h>
#include <GL/GLGeometryWrappers.h>
#include <GL/GLTransformationWrappers.h>
#include <GLMotif/StyleSheet.h>
#include <GLMotif/WidgetManager.h>
#include <GLMotif/PopupWindow.h>
#include <GLMotif/Margin.h>
#include <GLMotif/Pager.h>
#include <GLMotif/RowColumn.h>
#include <GLMotif/Button.h>
#include <GLMotif/ToggleButton.h>
#include <GLMotif/TextField.h>
#include <GLMotif/TextFieldSlider.h>
#include <Vrui/Vrui.h>
#include <Vrui/Application.h>
#include <Vrui/Viewer.h>
#include <Vrui/ObjectSnapperTool.h>
#include <Vrui/OpenFile.h>
#include <Vrui/Internal/VRDeviceState.h>
#include <Vrui/Internal/VRDeviceDescriptor.h>
#include <Vrui/Internal/VRDeviceClient.h>
#include <Kinect/Config.h>
#include <Kinect/DirectFrameSource.h>
#include <Kinect/OpenDirectFrameSource.h>
#include <Kinect/MultiplexedFrameSource.h>
#include <Kinect/ProjectorType.h>
#include <Kinect/ProjectorHeader.h>
#include <Kinect/DiskExtractor.h>
#include <Kinect/Internal/Config.h>
/* Flag to enable the experimental full calibration routine: */
#define RUN_FULL_CALIBRATION 1
class ExtrinsicCalibrator:public Vrui::Application
{
/* Embedded classes: */
private:
typedef Vrui::VRDeviceState::TrackerState TS;
typedef TS::PositionOrientation PO;
typedef PO::Scalar Scalar;
typedef PO::Point Point;
typedef PO::Vector Vector;
typedef PO::Rotation Rotation;
typedef Geometry::AffineTransformation<Scalar,3> ATransform;
typedef std::pair<Point,Point> TiePoint;
typedef std::pair<PO,Point> FullCalibTiePoint;
typedef Geometry::OrthogonalTransformation<double,3> CameraTransform; // Type for camera transformations
#if RUN_FULL_CALIBRATION
struct Calibration // Structure to hold (preliminary) calibration results
{
/* Elements: */
public:
Point diskCenter; // Disk center in controller's local coordinates
CameraTransform cameraTransform; // Transformation from camera space to tracker space
double rms; // RMS error for all provided tie points
double linf; // L-infinity error for all provided tie points
};
#endif
/* Elements: */
Vrui::VRDeviceClient* deviceClient; // Connection to the VRDeviceDaemon
std::vector<const Vrui::VRDeviceDescriptor*> controllers; // List of input devices that have buttons
int trackerIndex; // Index of tracker used to collect tie points; if -1, the tracker attached to the most-recently pressed button will be used
int buttonIndex; // Index of button to trigger tie point collection; if -1, any button will do
Vrui::Point diskCenter; // Position of disk center in controller's local coordinate system
Kinect::FrameSource* camera; // 3D video source to calibrate
std::string cameraSerialNumber; // Camera's serial number if camera is a direct frame source; some dummy string otherwise
Kinect::DiskExtractor* diskExtractor; // Object to extract disk shapes from a 3D video stream
Kinect::ProjectorType* projector; // A projeftor to render the 3D video stream
GLMotif::PopupWindow* configurationDialog; // Dialog window to configure the extrinsic calibrator
GLMotif::TextField* diskCenterTextFields[3]; // Text fields displaying the calibrated disk center
GLMotif::TextField* alignmentResidualTextField; // Text field showing the RMS residual of the most recent point set alignment
Threads::TripleBuffer<PO*> controllerStates; // Triple buffer of arrays of current controller tracking states
int previousControllerIndex; // Index of previously active controller
Threads::TripleBuffer<int> controllerIndex; // Triple buffer containing index of the controller owning the currently pressed button, or -1
int lastActiveControllerIndex; // Index of the controller that was last used to collect a tie point, or -1
bool calibratingDiskCenter; // Flag whether currently collecting controller transformations for disk center calibration
std::vector<ATransform> calibTransforms; // Vector of controller transformations collected during calibration for residual calculation
Math::Matrix calibAta,calibAtb; // Accumulated disk center calibration least-squares linear system
bool movingCamera; // Flag whether the camera instead of the calibration disk is attached to the tracked controller
Threads::TripleBuffer<Kinect::DiskExtractor::DiskList> diskList; // Triple buffer of lists of extracted disks
Point::AffineCombiner tiePointCombiners[2]; // Combiners for controller and disk center tie points
std::vector<TiePoint> tiePoints; // List of collected calibration tie points
bool haveCalibration; // True after the first calibration has been calculated
CameraTransform cameraTransform; // The current extrinsic camera transformation
#if RUN_FULL_CALIBRATION
std::vector<FullCalibTiePoint> fullCalibTiePoints; // List of collected tie points (controller transformation and disk center) for full calibration
unsigned int fullCalibNumSamples; // Number of samples accumulated into the full calibration system
unsigned int numAlignIterations; // Number of iterations for non-linear point set alignment
unsigned int numRansacIterations; // Number of RANSAC iterations, duh
double ransacMaxInlier; // Maximum inlier distance for RANSAC
#endif
/* Private methods: */
void depthStreamingCallback(const Kinect::FrameBuffer& frameBuffer); // Callback receiving depth frames from the frame source
#if !KINECT_CONFIG_USE_SHADERPROJECTOR
void meshStreamingCallback(const Kinect::MeshBuffer& meshBuffer); // Callback receiving projected meshes from the Kinect projector
#endif
void calibrateToggleCallback(GLMotif::ToggleButton::ValueChangedCallbackData* cbData); // Callback called when the "Calibrate" toggle button changes
void maxBlobMergeDistSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void minNumPixelsSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void diskRadiusSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void diskRadiusMarginSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void diskFlatnessSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void movingCameraToggleCallback(GLMotif::ToggleButton::ValueChangedCallbackData* cbData);
#if RUN_FULL_CALIBRATION
void ransacMaxInlierSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
#endif
void saveAlignmentCallback(Misc::CallbackData* cbData);
GLMotif::PopupWindow* createConfigurationDialog(void); // Creates the configuration dialog
void trackingCallback(Vrui::VRDeviceClient* client); // Called when new tracking data arrives
void diskExtractionCallback(const Kinect::DiskExtractor::DiskList& disks); // Called when a new list of disks has been extracted
void objectSnapCallback(Vrui::ObjectSnapperToolFactory::SnapRequest& snapRequest); // Handles a snap request from an object snapper tool
static Vrui::OGTransform calcOGTransform(const std::vector<TiePoint>& tiePoints); // Calculates the optimal orthogonal alignment transformation for the given list of tie points
#if RUN_FULL_CALIBRATION
Calibration fullCalibration(const std::vector<FullCalibTiePoint>& tiePoints); // Calculates full calibration
#endif
bool calcCameraTransform(void); // Calculates the extrinsic camera transformation; returns true if calibration was successful
/* Constructors and destructors: */
public:
ExtrinsicCalibrator(int& argc,char**& argv);
virtual ~ExtrinsicCalibrator(void);
/* Methods from Vrui::Application: */
virtual void frame(void);
virtual void display(GLContextData& contextData) const;
virtual void resetNavigation(void);
};
/************************************
Methods of class ExtrinsicCalibrator:
************************************/
void ExtrinsicCalibrator::depthStreamingCallback(const Kinect::FrameBuffer& frameBuffer)
{
/* Forward depth frame to the sphere extractor: */
diskExtractor->submitFrame(frameBuffer);
/* Forward depth frame to the projector: */
projector->setDepthFrame(frameBuffer);
#if KINECT_CONFIG_USE_SHADERPROJECTOR
/* Update application state: */
Vrui::requestUpdate();
#endif
}
#if !KINECT_CONFIG_USE_SHADERPROJECTOR
void ExtrinsicCalibrator::meshStreamingCallback(const Kinect::MeshBuffer& meshBuffer)
{
/* Update application state: */
Vrui::requestUpdate();
}
#endif
void ExtrinsicCalibrator::calibrateToggleCallback(GLMotif::ToggleButton::ValueChangedCallbackData* cbData)
{
/* Start or stop calibrating the disk center: */
calibratingDiskCenter=cbData->set;
if(calibratingDiskCenter)
{
/* Initialize calibration state: */
calibTransforms.clear();
calibAta=Math::Matrix(6,6,0.0);
calibAtb=Math::Matrix(6,1,0.0);
}
else
{
/* Solve for the local and global disk center position: */
Math::Matrix x=calibAtb;
x.divideFullPivot(calibAta);
/* Evaluate all collected transformations to calculate calibration error: */
ATransform::Point lp(x(0),x(1),x(2));
ATransform::Point gp(x(3),x(4),x(5));
Math::VarianceAccumulator err[3];
for(std::vector<ATransform>::const_iterator ctIt=calibTransforms.begin();ctIt!=calibTransforms.end();++ctIt)
{
Vector d=ctIt->transform(lp)-gp;
for(int i=0;i<3;++i)
err[i].addSample(d[i]);
}
Misc::formattedConsoleNote("Calibrate Disk Center: Disk center is (%.4f. %.4f, %.4f)",double(x(0)),double(x(1)),double(x(2)));
Misc::formattedUserNote("Calibrate Disk Center: Calibration standard deviation (%.4f, %.4f, %.4f)",err[0].calcStdDeviation(),err[1].calcStdDeviation(),err[2].calcStdDeviation());
/* Store the calibrated disk center and update the display: */
for(int i=0;i<3;++i)
{
diskCenter[i]=Scalar(x(i));
diskCenterTextFields[i]->setValue(x(i));
}
}
}
void ExtrinsicCalibrator::maxBlobMergeDistSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData)
{
diskExtractor->setMaxBlobMergeDist(int(Math::floor(cbData->value+0.5)));
}
void ExtrinsicCalibrator::minNumPixelsSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData)
{
diskExtractor->setMinNumPixels((unsigned int)(Math::floor(cbData->value+0.5)));
}
void ExtrinsicCalibrator::diskRadiusSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData)
{
diskExtractor->setDiskRadius(cbData->value);
}
void ExtrinsicCalibrator::diskRadiusMarginSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData)
{
diskExtractor->setDiskRadiusMargin(cbData->value);
}
void ExtrinsicCalibrator::diskFlatnessSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData)
{
diskExtractor->setDiskFlatness(cbData->value);
}
void ExtrinsicCalibrator::movingCameraToggleCallback(GLMotif::ToggleButton::ValueChangedCallbackData* cbData)
{
/* Update the calibration mode: */
movingCamera=cbData->set;
/* Reset all calibration data: */
#if RUN_FULL_CALIBRATION
fullCalibTiePoints.clear();
fullCalibNumSamples=0;
#else
tiePoints.clear();
#endif
haveCalibration=false;
cameraTransform=CameraTransform::identity;
}
#if RUN_FULL_CALIBRATION
void ExtrinsicCalibrator::ransacMaxInlierSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData)
{
/* Set the maximum inlier distance for RANSAC: */
ransacMaxInlier=cbData->value;
/* Re-run camera calibration if there are enough tie points: */
if(tiePoints.size()>=5)
{
/* If this was the first calibration, reset the view: */
if(calcCameraTransform()&&!haveCalibration)
{
resetNavigation();
haveCalibration=true;
}
}
}
#endif
void ExtrinsicCalibrator::saveAlignmentCallback(Misc::CallbackData* cbData)
{
#if RUN_FULL_CALIBRATION
/* Run a full-circuit RANSAC optimization: */
numAlignIterations=1000;
numRansacIterations=10000;
calcCameraTransform();
numAlignIterations=0;
numRansacIterations=1000;
#endif
/* Open the camera's extrinsic parameter file: */
std::string extrinsicFileName=KINECT_INTERNAL_CONFIG_CONFIGDIR;
extrinsicFileName.push_back('/');
extrinsicFileName.append(KINECT_INTERNAL_CONFIG_CAMERA_EXTRINSICPARAMETERSFILENAMEPREFIX);
extrinsicFileName.push_back('-');
extrinsicFileName.append(cameraSerialNumber);
extrinsicFileName.append(".txt");
IO::OStream extrinsicFile(Vrui::openFile(extrinsicFileName.c_str(),IO::File::WriteOnly));
/* Write the extrinsic calibration transformation: */
extrinsicFile<<Misc::ValueCoder<Vrui::OGTransform>::encode(cameraTransform)<<std::endl;
}
GLMotif::PopupWindow* ExtrinsicCalibrator::createConfigurationDialog(void)
{
const GLMotif::StyleSheet& ss=*Vrui::getWidgetManager()->getStyleSheet();
GLMotif::PopupWindow* configurationDialogPopup=new GLMotif::PopupWindow("ConfigurationDialogPopup",Vrui::getWidgetManager(),"Configuration");
GLMotif::Pager* configurationPager=new GLMotif::Pager("ConfigurationPager",configurationDialogPopup,false);
configurationPager->setMarginWidth(ss.size);
/* Create the disk center calibration page: */
configurationPager->setNextPageName("Disk Center");
GLMotif::Margin* diskCenterDialogMargin=new GLMotif::Margin("DiskCenterDialogMargin",configurationPager,false);
diskCenterDialogMargin->setAlignment(GLMotif::Alignment(GLMotif::Alignment::VCENTER));
GLMotif::RowColumn* diskCenterDialog=new GLMotif::RowColumn("DiskCenterDialog",diskCenterDialogMargin,false);
diskCenterDialog->setOrientation(GLMotif::RowColumn::VERTICAL);
diskCenterDialog->setPacking(GLMotif::RowColumn::PACK_TIGHT);
diskCenterDialog->setNumMinorWidgets(1);
GLMotif::RowColumn* diskCenterBox=new GLMotif::RowColumn("DiskCenterBox",diskCenterDialog,false);
diskCenterBox->setOrientation(GLMotif::RowColumn::HORIZONTAL);
diskCenterBox->setPacking(GLMotif::RowColumn::PACK_TIGHT);
diskCenterBox->setNumMinorWidgets(1);
new GLMotif::Label("DiskCenterLabel",diskCenterBox,"Disk Center");
for(int i=0;i<3;++i)
{
char textFieldName[]="DiskCenterTextField0";
textFieldName[19]='0'+i;
diskCenterTextFields[i]=new GLMotif::TextField(textFieldName,diskCenterBox,8);
diskCenterTextFields[i]->setPrecision(4);
diskCenterTextFields[i]->setFloatFormat(GLMotif::TextField::FIXED);
diskCenterTextFields[i]->setValue(diskCenter[i]);
diskCenterBox->setColumnWeight(i+1,1.0f);
}
diskCenterBox->manageChild();
GLMotif::Margin* calibrateMargin=new GLMotif::Margin("CalibrateMargin",diskCenterDialog,false);
calibrateMargin->setAlignment(GLMotif::Alignment(GLMotif::Alignment::RIGHT));
GLMotif::ToggleButton* calibrateToggle=new GLMotif::ToggleButton("CalibrateToggle",calibrateMargin,"Calibrate Disk Center");
calibrateToggle->setToggle(false);
calibrateToggle->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::calibrateToggleCallback);
calibrateMargin->manageChild();
diskCenterDialog->manageChild();
diskCenterDialogMargin->manageChild();
/* Create the disk extractor configuration page: */
configurationPager->setNextPageName("Disk Extractor");
GLMotif::Margin* diskExtractorDialogMargin=new GLMotif::Margin("DiskExtractorDialogMargin",configurationPager,false);
diskExtractorDialogMargin->setAlignment(GLMotif::Alignment(GLMotif::Alignment::VCENTER));
GLMotif::RowColumn* diskExtractorDialog=new GLMotif::RowColumn("DiskExtractorDialog",diskExtractorDialogMargin,false);
diskExtractorDialog->setOrientation(GLMotif::RowColumn::VERTICAL);
diskExtractorDialog->setPacking(GLMotif::RowColumn::PACK_TIGHT);
diskExtractorDialog->setNumMinorWidgets(2);
new GLMotif::Label("MaxBlobMergeDistLabel",diskExtractorDialog,"Max Blob Merge Dist");
GLMotif::TextFieldSlider* maxBlobMergeDistSlider=new GLMotif::TextFieldSlider("MaxBlobMergeDistSlider",diskExtractorDialog,6,ss.fontHeight*10.0f);
maxBlobMergeDistSlider->setSliderMapping(GLMotif::TextFieldSlider::LINEAR);
maxBlobMergeDistSlider->setValueType(GLMotif::TextFieldSlider::INT);
maxBlobMergeDistSlider->setValueRange(0,100,1);
maxBlobMergeDistSlider->setValue(diskExtractor->getMaxBlobMergeDist());
maxBlobMergeDistSlider->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::maxBlobMergeDistSliderCallback);
new GLMotif::Label("MinNumPixelsLabel",diskExtractorDialog,"Min Num Pixels");
GLMotif::TextFieldSlider* minNumPixelsSlider=new GLMotif::TextFieldSlider("MinNumPixelsSlider",diskExtractorDialog,6,ss.fontHeight*10.0f);
minNumPixelsSlider->setSliderMapping(GLMotif::TextFieldSlider::EXP10);
minNumPixelsSlider->setValueType(GLMotif::TextFieldSlider::UINT);
minNumPixelsSlider->setValueRange(1,50000,0.01);
minNumPixelsSlider->setValue(diskExtractor->getMinNumPixels());
minNumPixelsSlider->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::minNumPixelsSliderCallback);
new GLMotif::Label("DiskRadiusLabel",diskExtractorDialog,"Disk Radius");
GLMotif::TextFieldSlider* diskRadiusSlider=new GLMotif::TextFieldSlider("DiskRadiusSlider",diskExtractorDialog,6,ss.fontHeight*10.0f);
diskRadiusSlider->setSliderMapping(GLMotif::TextFieldSlider::EXP10);
diskRadiusSlider->setValueType(GLMotif::TextFieldSlider::FLOAT);
diskRadiusSlider->getTextField()->setFloatFormat(GLMotif::TextField::FIXED);
diskRadiusSlider->getTextField()->setPrecision(2);
diskRadiusSlider->setValueRange(1.0,100.0,0.01);
diskRadiusSlider->setValue(diskExtractor->getDiskRadius());
diskRadiusSlider->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::diskRadiusSliderCallback);
new GLMotif::Label("DiskRadiusMarginLabel",diskExtractorDialog,"Disk Radius Margin");
GLMotif::TextFieldSlider* diskRadiusMarginSlider=new GLMotif::TextFieldSlider("DiskRadiusMarginSlider",diskExtractorDialog,6,ss.fontHeight*10.0f);
diskRadiusMarginSlider->setSliderMapping(GLMotif::TextFieldSlider::LINEAR);
diskRadiusMarginSlider->setValueType(GLMotif::TextFieldSlider::FLOAT);
diskRadiusMarginSlider->getTextField()->setFloatFormat(GLMotif::TextField::FIXED);
diskRadiusMarginSlider->getTextField()->setPrecision(2);
diskRadiusMarginSlider->setValueRange(1.0,2.0,0.01);
diskRadiusMarginSlider->setValue(diskExtractor->getDiskRadiusMargin());
diskRadiusMarginSlider->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::diskRadiusMarginSliderCallback);
new GLMotif::Label("DiskFlatnessLabel",diskExtractorDialog,"Disk Flatness");
GLMotif::TextFieldSlider* diskFlatnessSlider=new GLMotif::TextFieldSlider("DiskFlatnessSlider",diskExtractorDialog,6,ss.fontHeight*10.0f);
diskFlatnessSlider->setSliderMapping(GLMotif::TextFieldSlider::LINEAR);
diskFlatnessSlider->setValueType(GLMotif::TextFieldSlider::FLOAT);
diskFlatnessSlider->getTextField()->setFloatFormat(GLMotif::TextField::FIXED);
diskFlatnessSlider->getTextField()->setPrecision(2);
diskFlatnessSlider->setValueRange(0.0,50.0,0.01);
diskFlatnessSlider->setValue(diskExtractor->getDiskFlatness());
diskFlatnessSlider->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::diskFlatnessSliderCallback);
diskExtractorDialog->manageChild();
diskExtractorDialogMargin->manageChild();
/* Create the extrinsic calibration page: */
configurationPager->setNextPageName("Extrinsic Calibration");
GLMotif::Margin* extrinsicCalibrationDialogMargin=new GLMotif::Margin("ExtrinsicCalibrationDialogMargin",configurationPager,false);
extrinsicCalibrationDialogMargin->setAlignment(GLMotif::Alignment(GLMotif::Alignment::VCENTER));
GLMotif::RowColumn* extrinsicCalibrationDialog=new GLMotif::RowColumn("ExtrinsicCalibrationDialog",extrinsicCalibrationDialogMargin,false);
extrinsicCalibrationDialog->setOrientation(GLMotif::RowColumn::VERTICAL);
extrinsicCalibrationDialog->setPacking(GLMotif::RowColumn::PACK_TIGHT);
extrinsicCalibrationDialog->setNumMinorWidgets(1);
GLMotif::Margin* movingCameraMargin=new GLMotif::Margin("MovingCameraMargin",extrinsicCalibrationDialog,false);
movingCameraMargin->setAlignment(GLMotif::Alignment(GLMotif::Alignment::RIGHT));
GLMotif::ToggleButton* movingCameraToggle=new GLMotif::ToggleButton("MovingCameraToggle",movingCameraMargin,"Moving Camera");
movingCameraToggle->setBorderType(GLMotif::Widget::PLAIN);
movingCameraToggle->setToggle(movingCamera);
movingCameraToggle->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::movingCameraToggleCallback);
movingCameraMargin->manageChild();
#if RUN_FULL_CALIBRATION
GLMotif::RowColumn* ransacMaxInlierBox=new GLMotif::RowColumn("RansacMaxInlierBox",extrinsicCalibrationDialog,false);
ransacMaxInlierBox->setOrientation(GLMotif::RowColumn::HORIZONTAL);
ransacMaxInlierBox->setPacking(GLMotif::RowColumn::PACK_TIGHT);
ransacMaxInlierBox->setNumMinorWidgets(1);
new GLMotif::Label("RansacMaxInlierLabel",ransacMaxInlierBox,"Max Inlier Distance");
GLMotif::TextFieldSlider* ransacMaxInlierSlider=new GLMotif::TextFieldSlider("RansacMaxInlierSlider",ransacMaxInlierBox,6,ss.fontHeight*10.0f);
ransacMaxInlierSlider->setSliderMapping(GLMotif::TextFieldSlider::EXP10);
ransacMaxInlierSlider->setValueType(GLMotif::TextFieldSlider::FLOAT);
ransacMaxInlierSlider->getTextField()->setFloatFormat(GLMotif::TextField::FIXED);
ransacMaxInlierSlider->getTextField()->setPrecision(3);
ransacMaxInlierSlider->setValueRange(0.001,0.1,0.01);
ransacMaxInlierSlider->setValue(ransacMaxInlier);
ransacMaxInlierSlider->getValueChangedCallbacks().add(this,&ExtrinsicCalibrator::ransacMaxInlierSliderCallback);
ransacMaxInlierBox->manageChild();
#endif
GLMotif::RowColumn* alignmentResidualBox=new GLMotif::RowColumn("AlignmentResidualBox",extrinsicCalibrationDialog,false);
alignmentResidualBox->setOrientation(GLMotif::RowColumn::HORIZONTAL);
alignmentResidualBox->setPacking(GLMotif::RowColumn::PACK_TIGHT);
alignmentResidualBox->setNumMinorWidgets(1);
new GLMotif::Label("AlignmentResidualLabel",alignmentResidualBox,"Alignment RMS Residual");
alignmentResidualTextField=new GLMotif::TextField("AlignmentResidualTextField",alignmentResidualBox,8);
alignmentResidualTextField->setPrecision(4);
alignmentResidualTextField->setFloatFormat(GLMotif::TextField::FIXED);
alignmentResidualTextField->setString("");
alignmentResidualBox->manageChild();
GLMotif::Margin* saveAlignmentMargin=new GLMotif::Margin("SaveAlignmentMargin",extrinsicCalibrationDialog,false);
saveAlignmentMargin->setAlignment(GLMotif::Alignment(GLMotif::Alignment::RIGHT));
GLMotif::Button* saveAlignmentButton=new GLMotif::Button("SaveAlignmentButton",saveAlignmentMargin,"Save Alignment");
saveAlignmentButton->getSelectCallbacks().add(this,&ExtrinsicCalibrator::saveAlignmentCallback);
saveAlignmentMargin->manageChild();
extrinsicCalibrationDialog->manageChild();
extrinsicCalibrationDialogMargin->manageChild();
configurationPager->setCurrentChildIndex(0);
configurationPager->manageChild();
return configurationDialogPopup;
}
void ExtrinsicCalibrator::trackingCallback(Vrui::VRDeviceClient* client)
{
/* Lock and retrieve the most recent input device states: */
deviceClient->lockState();
const Vrui::VRDeviceState& state=deviceClient->getState();
/* Extract all controllers' current tracking states into a new triple buffer slot: */
PO* tss=controllerStates.startNewValue();
for(unsigned int i=0;i<controllers.size();++i)
tss[i]=state.getTrackerState(controllers[i]->trackerIndex).positionOrientation;
/* Check if the active controller changed: */
int newControllerIndex=previousControllerIndex;
if(newControllerIndex==-1)
{
if(buttonIndex>=0)
{
/* Check if the specified button is pressed: */
if(state.getButtonState(buttonIndex))
{
/* Find the controller belonging to the specified button: */
for(unsigned int i=0;i<controllers.size();++i)
for(int j=0;j<controllers[i]->numButtons;++j)
{
if(controllers[i]->buttonIndices[j]==buttonIndex)
newControllerIndex=i;
}
}
}
else
{
/* Check if any controller buttons are pressed: */
for(unsigned int i=0;i<controllers.size();++i)
for(int j=0;j<controllers[i]->numButtons;++j)
{
if(state.getButtonState(controllers[i]->buttonIndices[j]))
newControllerIndex=i;
}
}
}
else
{
/* Check if the previously active controller is still active: */
newControllerIndex=-1;
for(int j=0;j<controllers[previousControllerIndex]->numButtons;++j)
{
if(state.getButtonState(controllers[previousControllerIndex]->buttonIndices[j]))
newControllerIndex=previousControllerIndex;
}
}
if(previousControllerIndex!=newControllerIndex)
{
controllerIndex.postNewValue(newControllerIndex);
previousControllerIndex=newControllerIndex;
}
if(newControllerIndex>=0)
lastActiveControllerIndex=newControllerIndex;
/* Release input device state lock: */
deviceClient->unlockState();
/* Post the new controller states and wake up the main thread: */
controllerStates.postNewValue();
Vrui::requestUpdate();
}
void ExtrinsicCalibrator::diskExtractionCallback(const Kinect::DiskExtractor::DiskList& disks)
{
/* Store the new disk list in the triple buffer: */
Kinect::DiskExtractor::DiskList& newList=diskList.startNewValue();
newList=disks;
diskList.postNewValue();
/* Wake up the main thread: */
Vrui::requestUpdate();
}
void ExtrinsicCalibrator::objectSnapCallback(Vrui::ObjectSnapperToolFactory::SnapRequest& snapRequest)
{
/* Snap the requested ray or point against all currently extracted disks: */
if(snapRequest.rayBased)
{
/* Intersect the snap ray with all current disks: */
const Kinect::DiskExtractor::DiskList& disks=diskList.getLockedValue();
for(Kinect::DiskExtractor::DiskList::const_iterator dIt=disks.begin();dIt!=disks.end();++dIt)
{
/* Intersect the ray with the disk's plane: */
Vrui::Scalar divisor=dIt->normal*snapRequest.snapRay.getDirection();
if(divisor!=Vrui::Scalar(0))
{
Vrui::Scalar lambda=((dIt->center-snapRequest.snapRay.getOrigin())*dIt->normal)/divisor;
if(lambda>=Vrui::Scalar(0)&&lambda<snapRequest.snapRayMax)
{
/* Check if the intersection point lies within the disk: */
Vrui::Point p=snapRequest.snapRay(lambda);
if(Geometry::sqrDist(p,dIt->center)<=Math::sqr(dIt->radius))
{
/* Store the hit: */
snapRequest.snapped=true;
snapRequest.snapRayMax=lambda;
snapRequest.snapResult=Vrui::ONTransform(dIt->center-Vrui::Point::origin,Vrui::Rotation::rotateFromTo(Vrui::Vector(0,1,0),dIt->normal));
}
}
}
}
}
else
{
/* Check the snap point against all current disks: */
const Kinect::DiskExtractor::DiskList& disks=diskList.getLockedValue();
for(Kinect::DiskExtractor::DiskList::const_iterator dIt=disks.begin();dIt!=disks.end();++dIt)
{
Vrui::Vector sc=snapRequest.snapPosition-dIt->center;
Vrui::Scalar scn=sc*dIt->normal;
Vrui::Scalar dist2=Math::sqr(scn);
if(dist2<Math::sqr(snapRequest.snapRadius))
{
sc-=dIt->normal*scn;
dist2+=Geometry::sqr(sc);
if(dist2<Math::sqr(snapRequest.snapRadius))
{
/* Store the hit: */
snapRequest.snapped=true;
snapRequest.snapRadius=Math::sqrt(dist2);
snapRequest.snapResult=Vrui::ONTransform(dIt->center-Vrui::Point::origin,Vrui::Rotation::rotateFromTo(Vrui::Vector(0,1,0),dIt->normal));
}
}
}
}
}
Vrui::OGTransform ExtrinsicCalibrator::calcOGTransform(const std::vector<ExtrinsicCalibrator::TiePoint>& tiePoints)
{
/* Calculate both point sets' centroids: */
Point::AffineCombiner cc0;
Point::AffineCombiner cc1;
for(std::vector<TiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
cc0.addPoint(tpIt->first);
cc1.addPoint(tpIt->second);
}
Point c0=cc0.getPoint();
Point c1=cc1.getPoint();
/* Calculate both point sets' inner products: */
double ip0=0.0;
double ip1=0.0;
for(std::vector<TiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
Vector d0=tpIt->first-c0;
Vector d1=tpIt->second-c1;
ip0+=Math::sqr(d0[0])+Math::sqr(d0[1])+Math::sqr(d0[2]);
ip1+=Math::sqr(d1[0])+Math::sqr(d1[1])+Math::sqr(d1[2]);
}
/* Calculate the normalizing scaling factors: */
double scale0=Math::sqrt(ip0);
double scale1=Math::sqrt(ip1);
/* Move both point sets to their centroids and scale them to uniform size: */
Vrui::OGTransform centroidTransform0=Vrui::OGTransform::translateToOriginFrom(c0);
centroidTransform0.leftMultiply(Vrui::OGTransform::scale(1.0/scale0));
Vrui::OGTransform centroidTransform1=Vrui::OGTransform::translateToOriginFrom(c1);
centroidTransform1.leftMultiply(Vrui::OGTransform::scale(1.0/scale1));
/* Calculate the inner product between the two scaled and centroid-aligned point sets: */
double m[3][3];
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
m[i][j]=0.0;
for(std::vector<TiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
/* Normalize both tie point components: */
Point p0=centroidTransform0.transform(tpIt->first);
Point p1=centroidTransform1.transform(tpIt->second);
/* Add their inner product to the matrix: */
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
m[i][j]+=p0[i]*p1[j];
}
/* Calculate the coefficients of the quaternion-based characteristic polynomial of the quaternion key matrix: */
double q4=1.0;
double q3=0.0;
double q2=0.0;
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
q2-=2.0*Math::sqr(m[i][j]);
double q1=8.0*(m[0][0]*m[1][2]*m[2][1]+m[1][1]*m[2][0]*m[0][2]+m[2][2]*m[0][1]*m[1][0])
-8.0*(m[0][0]*m[1][1]*m[2][2]+m[1][2]*m[2][0]*m[0][1]+m[2][1]*m[1][0]*m[0][2]);
double qd0=Math::sqr(Math::sqr(m[0][1])+Math::sqr(m[0][2])-Math::sqr(m[1][0])-Math::sqr(m[2][0]));
double qd1=(-Math::sqr(m[0][0])+Math::sqr(m[1][1])+Math::sqr(m[2][2])+Math::sqr(m[1][2])+Math::sqr(m[2][1])-2.0*(m[1][1]*m[2][2]-m[1][2]*m[2][1]))
*(-Math::sqr(m[0][0])+Math::sqr(m[1][1])+Math::sqr(m[2][2])+Math::sqr(m[1][2])+Math::sqr(m[2][1])+2.0*(m[1][1]*m[2][2]-m[1][2]*m[2][1]));
double qd2=(-(m[0][2]+m[2][0])*(m[1][2]-m[2][1])+(m[0][1]-m[1][0])*(m[0][0]-m[1][1]-m[2][2]))
*(-(m[0][2]-m[2][0])*(m[1][2]+m[2][1])+(m[0][1]-m[1][0])*(m[0][0]-m[1][1]+m[2][2]));
double qd3=(-(m[0][2]+m[2][0])*(m[1][2]+m[2][1])-(m[0][1]+m[1][0])*(m[0][0]+m[1][1]-m[2][2]))
*(-(m[0][2]-m[2][0])*(m[1][2]-m[2][1])-(m[0][1]+m[1][0])*(m[0][0]+m[1][1]+m[2][2]));
double qd4=((m[0][1]+m[1][0])*(m[1][2]+m[2][1])+(m[0][2]+m[2][0])*(m[0][0]-m[1][1]+m[2][2]))
*(-(m[0][1]-m[1][0])*(m[1][2]-m[2][1])+(m[0][2]+m[2][0])*(m[0][0]+m[1][1]+m[2][2]));
double qd5=((m[0][1]+m[1][0])*(m[1][2]-m[2][1])+(m[0][2]-m[2][0])*(m[0][0]-m[1][1]-m[2][2]))
*(-(m[0][1]-m[1][0])*(m[1][2]+m[2][1])+(m[0][2]-m[2][0])*(m[0][0]+m[1][1]-m[2][2]));
double q0=qd0+qd1+qd2+qd3+qd4+qd5;
/* Calculate the optimal rotation: */
double lambda=Math::mid(ip0,ip1);
double lambda0;
do
{
lambda0=lambda;
double poly=(((q4*lambda+q3)*lambda+q2)*lambda+q1)*lambda+q0;
double dPoly=((4.0*q4*lambda+3.0*q3)*lambda+2.0*q2)*lambda+q1;
lambda-=poly/dPoly;
}
while(Math::abs(lambda-lambda0)<1.0e-10);
/* Find the eigenvector corresponding to the largest eigenvalue: */
Math::Matrix k(4,4);
k(0,0)=m[0][0]+m[1][1]+m[2][2];
k(0,1)=m[1][2]-m[2][1];
k(0,2)=m[2][0]-m[0][2];
k(0,3)=m[0][1]-m[1][0];
k(1,0)=m[1][2]-m[2][1];
k(1,1)=m[0][0]-m[1][1]-m[2][2];
k(1,2)=m[0][1]+m[1][0];
k(1,3)=m[2][0]+m[0][2];
k(2,0)=m[2][0]-m[0][2];
k(2,1)=m[0][1]+m[1][0];
k(2,2)=-m[0][0]+m[1][1]-m[2][2];
k(2,3)=m[1][2]+m[2][1];
k(3,0)=m[0][1]-m[1][0];
k(3,1)=m[2][0]+m[0][2];
k(3,2)=m[1][2]+m[2][1];
k(3,3)=-m[0][0]-m[1][1]+m[2][2];
/* Calculate the optimal rotation: */
std::pair<Math::Matrix,Math::Matrix> jacobi=k.jacobiIteration();
double maxE=jacobi.second(0);
int maxEIndex=0;
for(int i=1;i<4;++i)
if(maxE<jacobi.second(i))
{
maxE=jacobi.second(i);
maxEIndex=i;
}
Vrui::Rotation rotation=Vrui::Rotation::fromQuaternion(jacobi.first(1,maxEIndex),jacobi.first(2,maxEIndex),jacobi.first(3,maxEIndex),jacobi.first(0,maxEIndex));
/* Assemble and return the optimal alignment transformation: */
Vrui::OGTransform result=Geometry::invert(centroidTransform1);
result*=Vrui::OGTransform::rotate(rotation);
result*=centroidTransform0;
result.renormalize();
return result;
}
#if RUN_FULL_CALIBRATION
ExtrinsicCalibrator::Calibration ExtrinsicCalibrator::fullCalibration(const std::vector<ExtrinsicCalibrator::FullCalibTiePoint>& tiePoints)
{
/* Create the least-squares system: */
Math::Matrix ata(15,15,0.0);
Math::Matrix atb(15,1,0.0);
for(std::vector<FullCalibTiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
/* Convert the tracker transformation to a matrix: */
ATransform::Matrix tracker;
tpIt->first.writeMatrix(tracker);
/* Add the three equations one at a time: */
double lhs[15];
double rhs;
for(int eq=0;eq<3;++eq)
{
/* Add coefficients depending on disk center: */
for(int j=0;j<3;++j)
lhs[j]=double(tracker(eq,j));
/* Add coefficients depending on camera-to-tracker transformation: */
for(int j=0;j<3;++j)
lhs[3+eq*4+j]=-double(tpIt->second[j])*0.01; // Scale from cm to meters to condition the least-squares matrix
lhs[3+eq*4+3]=-1.0;
/* Zero out the rest of the left hand side: */
for(int j=0;j<4;++j)
{
lhs[3+((eq+1)%3)*4+j]=0.0;
lhs[3+((eq+2)%3)*4+j]=0.0;
}
/* Assign right-hand side: */
rhs=-double(tracker(eq,3));
/* Add the equation to the least-squares matrix: */
for(int i=0;i<15;++i)
{
/* Add the left-hand side: */
for(int j=0;j<15;++j)
ata(i,j)+=lhs[i]*lhs[j];
/* Add the right-hand side: */
atb(i)+=lhs[i]*rhs;
}
}
}
try
{
/* Solve the least-squares system using Gaussian elimination: */
Math::Matrix sol=atb;
sol.divideFullPivot(ata);
Calibration result;
/* Extract the disk center in controller's local coordinates: */
result.diskCenter=Point(sol(0),sol(1),sol(2));
/* Use the calculated disk center to run an orthogonal point alignment algorithm: */
std::vector<Point> p0s;
std::vector<Point> p1s;
for(std::vector<FullCalibTiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
p0s.push_back(tpIt->second);
p1s.push_back(tpIt->first.transform(result.diskCenter));
}
Geometry::AlignResult<CameraTransform> ar=Geometry::alignPointsOGTransform(p0s,p1s,numAlignIterations);
result.cameraTransform=ar.transform;
result.rms=ar.rms;
result.linf=ar.linf;
return result;
}
catch(const Math::Matrix::RankDeficientError& err)
{
/* System was under-determined; return bogus result: */
Calibration result;
result.diskCenter=Point::origin;
result.rms=Math::Constants<Scalar>::infinity;
result.linf=Math::Constants<Scalar>::infinity;
return result;
}
}
#endif
bool ExtrinsicCalibrator::calcCameraTransform(void)
{
#if RUN_FULL_CALIBRATION
// std::cout<<"Calibration: "<<fullCalibTiePoints.size()<<" tie points";
/* Run RANSAC to find the best affine camera transformation: */
double bestRms=Math::Constants<double>::infinity;
// double bestLinf=Math::Constants<double>::infinity;
CameraTransform bestTransform;
Point bestDiskCenter;
// double bestPercent=0.0;
double maxInlier2=Math::sqr(ransacMaxInlier);
double minPercent=75.0;
for(unsigned int ransac=0;ransac<numRansacIterations;++ransac)
{
/* Pick a random subset of five tie points: */
std::vector<FullCalibTiePoint> rtps;
for(int i=0;i<5;++i)
rtps.push_back(fullCalibTiePoints[Math::randUniformCO(0,fullCalibTiePoints.size())]); // Let's ignore for now that we may get duplicates
/* Calibrate based on the minimal subset: */
Calibration cal1=fullCalibration(rtps);
/* Find all inliers based on the minimal calibration: */
std::vector<FullCalibTiePoint> tps;
for(std::vector<FullCalibTiePoint>::iterator tpIt=fullCalibTiePoints.begin();tpIt!=fullCalibTiePoints.end();++tpIt)
{
/* Calculate this tie point's approximation error: */
Point trackerPos=tpIt->first.transform(cal1.diskCenter);
Point kinectPos=cal1.cameraTransform.transform(tpIt->second);
double dist2=Geometry::sqrDist(trackerPos,kinectPos);
if(dist2<=maxInlier2)
tps.push_back(*tpIt);
}
/* Check if the inlier set is large enough: */
if(double(tps.size())/double(fullCalibTiePoints.size())*100.0>=minPercent)
{
/* Run calibration based on all inliers: */
Calibration cal2=fullCalibration(tps);
/* Check if this calibration is better than the current best: */
if(bestRms>cal2.rms)
{
/* Keep it: */
bestRms=cal2.rms;
// bestLinf=cal2.linf;
bestTransform=cal2.cameraTransform;
bestDiskCenter=cal2.diskCenter;
// bestPercent=double(tps.size())/double(fullCalibTiePoints.size())*100.0;
}
}
}
/* Check if RANSAC came up with a calibration solution: */
if(Math::isFinite(bestRms))
{
/* Store the best disk center and camera transformation: */
diskCenter=bestDiskCenter;
cameraTransform=bestTransform;
// std::cout<<", error RMS="<<bestRms<<", Linf="<<bestLinf<<", "<<bestPercent<<"% inliers"<<std::endl;
/* Update the GUI: */
for(int i=0;i<3;++i)
diskCenterTextFields[i]->setValue(diskCenter[i]);
alignmentResidualTextField->setValue(bestRms);
// DEBUGGING
/* Print the camera transformation estimate: */
// std::cout<<"Camera transform: "<<Misc::ValueCoder<CameraTransform>::encode(cameraTransform)<<std::endl;
return true;
}
else
return false;
#else
/* DEBUGGING: Save tie points to a file */
{
std::ofstream points0File("KinectPoints.csv");
std::ofstream points1File("TrackerPoints.csv");
for(std::vector<TiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
points0File<<tpIt->first[0]<<','<<tpIt->first[1]<<','<<tpIt->first[2]<<std::endl;
points1File<<tpIt->second[0]<<','<<tpIt->second[1]<<','<<tpIt->second[2]<<std::endl;
}
}
/* Calculate the alignment transformation: */
cameraTransform=calcOGTransform(tiePoints);
/* Calculate the residual: */
double rmsd=0.0;
for(std::vector<TiePoint>::const_iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
rmsd+=Geometry::sqrDist(cameraTransform.transform(tpIt->first),Vrui::Point(tpIt->second));
alignmentResidualTextField->setValue(Math::sqrt(rmsd/double(tiePoints.size())));
// std::cout<<"Best distance: "<<Math::sqrt(rmsd/double(tiePoints.size()))<<std::endl;
return true;
#endif
}
ExtrinsicCalibrator::ExtrinsicCalibrator(int& argc,char**& argv)
:Vrui::Application(argc,argv),
deviceClient(0),trackerIndex(-1),buttonIndex(-1),
diskCenter(Point::origin),
configurationDialog(0),
previousControllerIndex(-1),lastActiveControllerIndex(-1),
calibratingDiskCenter(false),
movingCamera(false),
haveCalibration(false),cameraTransform(Vrui::OGTransform::identity)
{
/* Parse command line: */
const char* serverName="localhost:8555";
Kinect::MultiplexedFrameSource* remoteSource=0;
int cameraIndex=0;
unsigned int projectorTriangleDepthRange=30;
for(int i=1;i<argc;++i)