-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglviewer.cpp
More file actions
1556 lines (1432 loc) · 49.6 KB
/
glviewer.cpp
File metadata and controls
1556 lines (1432 loc) · 49.6 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
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
#include "glviewer.h"
#include <QMouseEvent>
#include <QCoreApplication>
#include <QGLShaderProgram>
#include <QMessageBox>
#include <QScrollBar>
#include <QtGui>
#include <iostream>
#include <fstream>
#include "glut.h"
using std::vector;
using std::cout;
using std::string;
using std::allocator;
using pcl::Vertices;
GLViewer::GLViewer(QWidget *parent) :
QGLWidget(parent)
{
//Setup OpenGL Widget
m_nxRot = 0; // Total rotation of camera about the x-axis
m_nyRot = 0; // Total rotation of camera about the y-axis
m_nzRot = 0; // Total rotation of camera about the z-axis
m_lfDx = 0.01; // Total camera translation change in the x-direction
m_lfDy = 0.01; // Total camera translation change in the y-direction
m_lfDz = 0.01; // Total camera translation change in the z-direction
m_lfxLOS = 0.0; // Camera line of sight x-direction initialization
m_lfyLOS = 0.0; // Camera line of sight y-direction initialization
m_lfzLOS = -1.0; // Camera line of sight z-direction initialization
m_lfxUp = 0.0; // Camera line of sight x-direction initialization
m_lfyUp = 1.0; // Camera line of sight y-direction initialization
m_lfzUp = 0.0; // Camera line of sight z-direction initialization
m_lfxTrans = 0; // Total camera translation change in the x-direction
m_lfyTrans = 0; // Total camera translation change in the y-direction
m_lfzTrans = 0; // Total camera translation change in the z-direction
m_bshift = false; // Whether the Shift key is held down
m_bPoint = false; // Whether to grab point info or not
m_bMouse = true; // Whether to do zooming and panning with mouse
m_bRotate = true; // Whether to rotate around object (turn off @ zoom)
m_lfXMin = 1e12; // Minimum x-value of objects;
m_lfYMin = 1e12; // Minimum y-value of objects;
m_lfZMin = 1e12; // Minimum z-value of objects;
m_lfXMax = -1e12; // Maximum x-value of objects;
m_lfYMax = -1e12; // Maximum y-value of objects;
m_lfZMax = -1e12; // Maximum z-value of objects;
m_lfXAvg = 0.0; // Average x-value of objects;
m_lfYAvg = 0.0; // Average y-value of objects;
m_lfZAvg = 0.0; // Average z-value of objects;
m_lfXTot = 0.0; // Total x-value of object points;
m_lfYTot = 0.0; // Total y-value of object points;
m_lfZTot = 0.0; // Total z-value of object points;
m_nPTot = 0; // Total number of object points;
m_bDrawAxis = true; // Whether to draw the x, y, z axis
m_qrTotalScaleFactor = 1.0; // Scale factor for pinch zoom
// MIP testing only
m_bCKey = false;
m_bHKey = false;
// Refresh rendering after camera position/rotation changes
setAutoBufferSwap(true);
// Makes sure glviewer is in focus for keypress events
setFocusPolicy(Qt::StrongFocus);
// Sets up touch events
setAttribute(Qt::WA_AcceptTouchEvents);
this->setAttribute(Qt::WA_AcceptTouchEvents);
grabGesture(Qt::PanGesture);
grabGesture(Qt::PinchGesture);
grabGesture(Qt::SwipeGesture);
}
// The main function to pass in point cloud polygon mesh data for the viewer
void GLViewer::showPolygonMesh(pcl::PolygonMesh mesh, QString meshName){
try
{
checkMeshNameExists(meshName);
}
catch(GLViewer::NameExists)
{
cout << "\033[1;31mException: " << meshName.toUtf8().constData()
<< " already exists." << "\033[0m" << endl;
return;
}
getDiffs(mesh); // Determining object size for translation steps
m_vQSMeshNames.push_back(meshName);
m_vbShowMeshes.push_back(true);
addMesh(mesh); // Adds mesh to m_VTriangles
initializeGL(); // Resets GL values for rendering
paintGL(); // Draws the meshes and clouds in the glviewer
}
// Shows polygon mesh that was previously hidden
void GLViewer::showPolygonMesh(QString meshName){
try
{
checkMeshNameMissing(meshName, true);
}
catch(GLViewer::NameMissing)
{
cout << "\033[1;31mException: " << meshName.toUtf8().constData()
<< " does not exist." << "\033[0m" << endl;
return;
}
initializeGL(); // Resets GL values for rendering
paintGL(); // Draws the meshes and clouds in the glviewer
}
// Hides polygon mesh that was previously seen
void GLViewer::hidePolygonMesh(QString meshName){
try
{
checkMeshNameMissing(meshName, false);
}
catch(GLViewer::NameMissing)
{
cout << "\033[1;31mException: " << meshName.toUtf8().constData()
<< " does not exist." << "\033[0m" << endl;
return;
}
initializeGL(); // Resets GL values for rendering
paintGL(); // Draws the meshes and clouds in the glviewer
}
void GLViewer::showCloud(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud,
QString cloudName)
{
try
{
checkCloudNameExists(cloudName);
}
catch(GLViewer::NameExists)
{
cout << "\033[1;31mException: " << cloudName.toUtf8().constData()
<< " already exists." << "\033[0m" << endl;
return;
}
getDiffs(cloud); // Determining object size for translation steps
m_vQSCloudNames.push_back(cloudName);
m_vbShowClouds.push_back(true);
addCloud(cloud); // Adds mesh to m_VTriangles
initializeGL(); // Resets GL values for rendering
paintGL(); // Draws the meshes and clouds in the glviewer
}
void GLViewer::showCloud(QString cloudName)
{
try
{
checkCloudNameMissing(cloudName, true);
}
catch(GLViewer::NameMissing)
{
cout << "\033[1;31mException: " << cloudName.toUtf8().constData()
<< " does not exist." << "\033[0m" << endl;
return;
}
initializeGL(); // Resets GL values for rendering
paintGL(); // Draws the meshes and clouds in the glviewer
}
void GLViewer::hideCloud(QString cloudName)
{
try
{
checkCloudNameMissing(cloudName, false);
}
catch(GLViewer::NameMissing)
{
cout << "\033[1;31mException: " << cloudName.toUtf8().constData()
<< " does not exist." << "\033[0m" << endl;
return;
}
initializeGL(); // Resets GL values for rendering
paintGL(); // Draws the meshes and clouds in the glviewer
}
// Throw an exception if the name already exists
void GLViewer::checkMeshNameExists(QString meshName)
{
for(size_t i=0; i<m_vQSMeshNames.size(); ++i)
{
if(m_vQSMeshNames[i] == meshName)
{
throw NameExists();
}
}
}
// Throw an exception if the name is missing to show/hide this mesh
// also includes a boolean for whether to show/hide mesh to reduce operations
void GLViewer::checkMeshNameMissing(QString meshName, bool bShow)
{
bool bFound = false;
for(size_t i=0; i<m_vQSMeshNames.size(); ++i)
{
if(m_vQSMeshNames[i] == meshName)
{
bFound = true;
bool bCheck = true;
try
{
bCheck = checkShowMeshes(i);
bCheck = true;
}
catch(GLViewer::SmallDimension)
{
cout << "\033[1;31mException: Mesh[" << i << "] does not exist"
<< "\033[0m" << endl;
bCheck = false;
}
if(bCheck)
{
cout << "show = " << bShow << " for "
<< meshName.toUtf8().constData() << endl;
m_vbShowMeshes[i] = bShow;
}
}
}
if(!bFound)
{
throw NameMissing();
}
}
// Throw an exception if the name already exists
void GLViewer::checkCloudNameExists(QString cloudName)
{
for(size_t i=0; i<m_vQSCloudNames.size(); ++i)
{
if(m_vQSCloudNames[i] == cloudName)
{
throw NameExists();
}
}
}
// Throw an exception if the name is missing to show/hide this cloud
// also includes a boolean for whether to show/hide cloud to reduce operations
void GLViewer::checkCloudNameMissing(QString cloudName, bool bShow)
{
bool bFound = false;
for(size_t i=0; i<m_vQSCloudNames.size(); ++i)
{
if(m_vQSCloudNames[i] == cloudName)
{
bFound = true;
bool bCheck = true;
try
{
bCheck = checkShowClouds(i);
bCheck = true;
}
catch(GLViewer::SmallDimension)
{
cout << "\033[1;31mException: Cloud[" << i << "] does not exist"
<< "\033[0m" << endl;
bCheck = false;
}
if(bCheck)
{
cout << "show = " << bShow << " for "
<< cloudName.toUtf8().constData() << endl;
m_vbShowClouds[i] = bShow;
}
}
}
if(!bFound)
{
throw NameMissing();
}
}
// Determine the best translation changes based on the object size
void GLViewer::getDiffs(pcl::PolygonMesh mesh)
{
// typedefs used to make cloud point type easier to define
// typedef pcl::PointXYZRGBA PointT;
// typedef pcl::PointCloud<PointT> PointCloudT;
// Create normal point cloud
PointCloudT* meshCloud = new PointCloudT;
// Transfer pointcloud2 data to point cloud data
pcl::fromPCLPointCloud2(mesh.cloud, *meshCloud);
for(size_t i=0; i<meshCloud->points.size(); ++i)
{
PointT p1 = meshCloud->points[i];
if(p1.x < m_lfXMin)
{
m_lfXMin = p1.x;
}
if(m_lfXMax < p1.x)
{
m_lfXMax = p1.x;
}
if(p1.y < m_lfYMin)
{
m_lfYMin = p1.y;
}
if(m_lfYMax < p1.y)
{
m_lfYMax = p1.y;
}
if(p1.z < m_lfZMin)
{
m_lfZMin = p1.z;
}
if(m_lfZMax < p1.z)
{
m_lfZMax = p1.z;
}
m_lfXTot += p1.x;
m_lfYTot += p1.y;
m_lfZTot += p1.z;
}
// Determined through trial and error for visualization translation
m_lfDx = (m_lfXMax-m_lfXMin)/250.0;
m_lfDy = (m_lfYMax-m_lfYMin)/250.0;
m_lfDz = (m_lfZMax-m_lfZMin)/20.0;
m_nPTot += int(meshCloud->points.size());
m_lfXAvg = m_lfXTot/double(m_nPTot);
m_lfYAvg = m_lfYTot/double(m_nPTot);
m_lfZAvg = m_lfZTot/double(m_nPTot);
// Finds the maximum distance between object sides
double maxDist = m_lfXMax - m_lfXMin;
if(maxDist < m_lfYMax - m_lfYMin)
{
maxDist = m_lfYMax - m_lfYMin;
}
if(maxDist < m_lfZMax - m_lfZMin)
{
maxDist = m_lfZMax - m_lfZMin;
}
// Initial translation of the camera to place object in center view
m_lfxTrans = -m_lfXAvg;
m_lfyTrans = -m_lfYAvg;
m_lfzTrans = -fabs(maxDist);
delete meshCloud; // Cleans up memory
}
// Determine the best translation changes based on the object size
void GLViewer::getDiffs(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud)
{
for(size_t i=0; i<cloud->points.size(); ++i)
{
PointT p1 = cloud->points[i];
if(p1.x < m_lfXMin)
{
m_lfXMin = p1.x;
}
if(m_lfXMax < p1.x)
{
m_lfXMax = p1.x;
}
if(p1.y < m_lfYMin)
{
m_lfYMin = p1.y;
}
if(m_lfYMax < p1.y)
{
m_lfYMax = p1.y;
}
if(p1.z < m_lfZMin)
{
m_lfZMin = p1.z;
}
if(m_lfZMax < p1.z)
{
m_lfZMax = p1.z;
}
m_lfXTot += p1.x;
m_lfYTot += p1.y;
m_lfZTot += p1.z;
}
// Determined through trial and error for visualization translation
m_lfDx = (m_lfXMax-m_lfXMin)/250.0;
m_lfDy = (m_lfYMax-m_lfYMin)/250.0;
m_lfDz = (m_lfZMax-m_lfZMin)/20.0;
m_nPTot += int(cloud->points.size());
m_lfXAvg = m_lfXTot/double(m_nPTot);
m_lfYAvg = m_lfYTot/double(m_nPTot);
m_lfZAvg = m_lfZTot/double(m_nPTot);
// Finds the maximum distance between object sides
double maxDist = m_lfXMax - m_lfXMin;
if(maxDist < m_lfYMax - m_lfYMin)
{
maxDist = m_lfYMax - m_lfYMin;
}
if(maxDist < m_lfZMax - m_lfZMin)
{
maxDist = m_lfZMax - m_lfZMin;
}
// Initial translation of the camera to place object in center view
m_lfxTrans = -m_lfXAvg;
m_lfyTrans = -m_lfYAvg;
m_lfzTrans = -fabs(maxDist);
}
// Adds a cloud to m_vClouds
void GLViewer::addCloud(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud)
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr sourceCloud(cloud);
m_vClouds.push_back(sourceCloud);
}
// Adds a mesh to m_vMeshes
void GLViewer::addMesh(pcl::PolygonMesh mesh)
{
// Create normal point cloud
PointCloudT::Ptr meshCloud(new PointCloudT);
// Transfer pointcloud2 data to point cloud data
pcl::fromPCLPointCloud2(mesh.cloud, *meshCloud);
m_vMeshes.push_back(mesh);
m_vbShowClouds.push_back(false); // Already showing mesh. Do not show cloud
showCloud(meshCloud, "meshCloud1"); // Add cloud for future use
}
// GL window size
QSize GLViewer::sizeHint() const{
return QSize(400,400);
}
// Makes sure the rotation angle of the camera is always between 0 and 360 (deg)
static void qNormalizeAngle(int &angle)
{
while (angle < 0)
{
angle += 360 * 16;
}
while (angle > 360 * 16)
{
angle -= 360 * 16;
}
}
// Update the x-rotation based on mouse/touch events
void GLViewer::setXRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != m_nxRot)
{
m_nxRot = angle;
emit xRotationChanged(angle);
updateGL();
}
}
// Update the y-rotation based on mouse/touch events
void GLViewer::setYRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != m_nyRot)
{
m_nyRot = angle;
emit yRotationChanged(angle);
update();
}
}
// Update the z-rotation based on mouse/touch events
void GLViewer::setZRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != m_nzRot)
{
m_nzRot = angle;
emit zRotationChanged(angle);
update();
}
}
// Update the x-translation based on mouse/touch events
void GLViewer::setXTranslation(double dist)
{
if (dist != m_lfxTrans)
{
m_lfxTrans = dist;
emit xTranslationChanged(dist);
update();
}
}
// Update the y-translation based on mouse/touch events
void GLViewer::setYTranslation(double dist)
{
if (dist != m_lfyTrans)
{
m_lfyTrans = dist;
emit yTranslationChanged(dist);
update();
}
}
// Update the z-translation based on mouse/touch events
void GLViewer::setZTranslation(double dist)
{
if (dist != m_lfzTrans)
{
m_lfzTrans = dist;
emit zTranslationChanged(dist);
update();
}
}
// Initialize GL environment settings for lighting, rendering, and depth testing
void GLViewer::initializeGL()
{
// glClearColor(0.0, 0.0, 0.0, 0.0); // black background
glEnable(GL_DEPTH_TEST); // Occludes objects behind closer objects
glDisable(GL_CULL_FACE); // Makes sure triangle normals render correctly
// The following were disabled in hopes of better color mapping
glDisable(GL_BLEND);
glDisable(GL_DITHER);
glDisable(GL_FOG);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_2D);
// glDisable(GL_TEXTURE_3D);
glShadeModel(GL_FLAT);
// glCullFace(GL_FRONT);
glMatrixMode(GL_PROJECTION); // Matrix mode for camera coordinates
glLoadIdentity(); // Resets camera transformation matrix
// Sets camera fov, aspect ratio, and near/far values
gluPerspective(45.0, 1.0, 0.01f, 350.0f);
// glEnable(GL_MULTISAMPLE); // Antialiasing
// These may be helpful later? MIP
// glShadeModel(GL_SMOOTH);
// glEnable(GL_LIGHTING);
// glEnable(GL_LIGHT0);
// static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
// glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}
// Allows the glviewer window to be resized if needed
void GLViewer::resizeGL(int w, int h)
{
glLoadIdentity(); // Reset camera xform
gluPerspective(45.0f, GLfloat(w) / h, 0.01f, 100.0f); // Set camera values
}
// Checks if current event is a gesture event
bool GLViewer::event(QEvent *event)
{
// Gesture events override mouse events
if (event->type() == QEvent::Gesture)
{
m_bMouse = false;
return gestureEvent(static_cast<QGestureEvent*>(event));
}
return QGLWidget::event(event);
}
// Swipe event is not needed at this time
void GLViewer::swipeTriggered(QSwipeGesture *gesture)
{
}
// Pan with 2-3 fingers
void GLViewer::panTriggered(QPanGesture *gesture)
{
// Do not rotate around object while panning
m_bRotate = false;
int dx = gesture->delta().x(); // Touch change in x-direction
int dy = gesture->delta().y(); // Touch change in y-direction
setXTranslation(m_lfxTrans + m_lfDx * dx);
setYTranslation(m_lfyTrans - m_lfDy * dy);
}
// Pinch with two fingers - moving together zooms out & moving apart zooms in
void GLViewer::pinchTriggered(QPinchGesture *gesture)
{
// Do not rotate around object while zooming
m_bRotate = false;
// Track the pinch change
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
double dz;
if (changeFlags & QPinchGesture::ScaleFactorChanged) {
// Determine how the scale has changed due to pinching
qreal value = gesture->property("scaleFactor").toReal();
// cout << fabs(value - 1.0) << endl;
// Zoom out && make sure panning does not affect zoom as easily
if(value < 1.0 && fabs(value - 1.0) > 0.025)
{
dz = m_lfzTrans - (2.0 - value) * 5.0 * m_lfDz;
setZTranslation(dz);
}
// Zoom in && make sure panning does not affect zoom as easily
if(value > 1.0 && fabs(value - 1.0) > 0.025)
{
dz = m_lfzTrans + value * 5.0 * m_lfDz;
setZTranslation(dz);
}
}
}
// Checks for gesture events
bool GLViewer::gestureEvent(QGestureEvent *event)
{
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
{
swipeTriggered(static_cast<QSwipeGesture *>(swipe));
}
else if (QGesture *pan = event->gesture(Qt::PanGesture))
{
panTriggered(static_cast<QPanGesture *>(pan));
}
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
{
pinchTriggered(static_cast<QPinchGesture *>(pinch));
}
return true;
}
// Captures when a mouse key is double clicked
void GLViewer::mouseDoubleClickEvent(QMouseEvent *event)
{
m_bshift = true;
// cout << "Panning enabled." << endl;
}
// Captures when a mouse key is released
void GLViewer::mouseReleaseEvent(QMouseEvent *event)
{
m_bshift = false;
m_bRotate = true;
// cout << "Panning disabled." << endl;
}
// Captures when a mouse key is pressed
void GLViewer::mousePressEvent(QMouseEvent *event)
{
// Makes sure glviewer is in focus for keypress events
setFocusPolicy(Qt::StrongFocus);
m_QPlastPos = event->pos(); // Stores previous mouse position
if(m_bPoint && event->buttons() & Qt::LeftButton)
{
m_vlfPoint = getPointCoords(event->x(), event->y());
getPointIndex();
cout << "Point: (" << m_vlfPoint[0] << ", " << m_vlfPoint[1]
<< ", " << m_vlfPoint[2] << ")" << endl;
cout << "Index for " << m_sIndex << ": " << m_nIndex << endl;
if(m_nPolygonIndex > -1)
{
cout << m_sIndex << " polygon: " << m_nPolygonIndex << endl;
}
}
}
void GLViewer::mouseMoveEvent(QMouseEvent *event)
{
// Makes sure glviewer is in focus for keypress events
setFocusPolicy(Qt::StrongFocus);
int dx = event->x() - m_QPlastPos.x(); // Mouse change in x-direction
int dy = event->y() - m_QPlastPos.y(); // Mouse change in y-direction
QSize windowSize = size(); // View window height/width size
// Left mouse button held down (without Shift key) rotates camera
// around object
if(!m_bshift && !m_bPoint && m_bRotate && event->buttons() & Qt::LeftButton)
{
// rotate about x-axis only when mouse is fairly close to object center
if(event->x() < int(2.0f/3.0f * float(windowSize.width())) &&
event->x() > int(1.0f/3.0f * float(windowSize.width())))
{
setXRotation(m_nxRot + 8 * dy);
}
// rotate about z-axis when on the edge of object
else if(event->x() > int(2.0f/3.0f * float(windowSize.width())))
{
if(m_nyRot <= 90*16 && m_nyRot >= -90*16)
{
setZRotation(m_nzRot - 8 * dy);
}
else if(m_nyRot >= 270*16 && m_nyRot <= 360*16)
{
setZRotation(m_nzRot - 8 * dy);
}
else if(m_nyRot > 90*16 && m_nyRot < 270*16)
{
setZRotation(m_nzRot + 8 * dy);
}
}
// rotate opposite about z-axis when on the opposite edge of object
else if(event->x() < int(1.0f/3.0f * float(windowSize.width())))
{
if(m_nyRot <= 90*16 && m_nyRot >= -90*16)
{
setZRotation(m_nzRot + 8 * dy);
}
else if(m_nyRot >= 270*16 && m_nyRot <= 360*16)
{
setZRotation(m_nzRot + 8 * dy);
}
else if(m_nyRot > 90*16 && m_nyRot < 270*16)
{
setZRotation(m_nzRot - 8 * dy);
}
}
// rotate about y-axis only when mouse is fairly close to object center
if(event->y() < int(2.0f/3.0f * float(windowSize.height())) &&
event->y() > int(1.0f/3.0f * float(windowSize.height())))
{
setYRotation(m_nyRot + 8 * dx);
}
// rotate about z-axis when on the edge of object
else if(event->y() > int(2.0f/3.0f * float(windowSize.height())))
{
if(m_nyRot <= 90*16 && m_nyRot >= -90*16)
{
setZRotation(m_nzRot + 8 * dx);
}
else if(m_nyRot >= 270*16 && m_nyRot <= 360*16)
{
setZRotation(m_nzRot + 8 * dx);
}
else if(m_nyRot > 90*16 && m_nyRot < 270*16)
{
setZRotation(m_nzRot - 8 * dx);
}
}
// rotate opposite about z-axis when on the opposite edge of object
else if(event->y() < int(1.0f/3.0f * float(windowSize.height())))
{
if(m_nyRot <= 90*16 && m_nyRot >= -90*16)
{
setZRotation(m_nzRot - 8 * dx);
}
else if(m_nyRot >= 270*16 && m_nyRot <= 360*16)
{
setZRotation(m_nzRot - 8 * dx);
}
else if(m_nyRot > 90*16 && m_nyRot < 270*16)
{
setZRotation(m_nzRot + 8 * dx);
}
}
}
// Right button held down zooms in/out on object
else if (m_bMouse && m_bRotate && event->buttons() & Qt::RightButton)
{
setZTranslation(m_lfzTrans - m_lfDz * dy);
}
// Shift and left button held down translates object in xy-coordinate space
else if (m_bMouse && m_bshift && event->buttons() & Qt::LeftButton)
{
setXTranslation(m_lfxTrans + m_lfDx * dx);
setYTranslation(m_lfyTrans - m_lfDy * dy);
}
m_QPlastPos = event->pos(); // Saves mouse position after movement ends
}
// Track when a key is pressed with glviewer window in focus
void GLViewer::keyPressEvent(QKeyEvent *event)
{
// cout << event->text().toUtf8().constData() << endl;
string sKey = event->text().toUtf8().constData();
// MIP Testing purposes with keys
// Checks for when the "P" or "p" key is pressed
if (sKey == "p" || sKey == "P")
{
pointPicker();
}
if (sKey == "c" || sKey == "C")
{
if(m_bCKey)
{
m_bCKey = false;
hideCloud("meshCloud1");
showPolygonMesh("mesh1");
update();
}
else
{
m_bCKey = true;
hidePolygonMesh("mesh1");
showCloud("meshCloud1");
update();
}
}
if (sKey == "h" || sKey == "H")
{
if(m_bHKey)
{
m_bHKey = false;
}
else
{
m_bHKey = true;
}
}
// Checks for when the Shift key is held down
if (event->modifiers() & Qt::ShiftModifier)
{
m_bshift = 1;
}
else
{
}
}
// Checks for when a key is released
void GLViewer::keyReleaseEvent(QKeyEvent *event)
{
m_bshift = 0; // Stores when the Shift key is released
}
vector<double> GLViewer::normalizeUP()
{
vector<double> normalizedUp;
// UP/||UP||
normalizedUp.push_back(m_lfxUp/sqrt(m_lfxUp * m_lfxUp + m_lfyUp * m_lfyUp +
m_lfzUp * m_lfzUp));
normalizedUp.push_back(m_lfyUp/sqrt(m_lfxUp * m_lfxUp + m_lfyUp * m_lfyUp +
m_lfzUp * m_lfzUp));
normalizedUp.push_back(m_lfzUp/sqrt(m_lfxUp * m_lfxUp + m_lfyUp * m_lfyUp +
m_lfzUp * m_lfzUp));
return normalizedUp;
}
vector<double> GLViewer::normalizeLOS()
{
// F/||F||
vector<double> normalizedLOS;
normalizedLOS.push_back(m_lfxLOS/sqrt(m_lfxLOS * m_lfxLOS +
m_lfyLOS * m_lfyLOS +
m_lfzLOS * m_lfzLOS));
normalizedLOS.push_back(m_lfyLOS/sqrt(m_lfxLOS * m_lfxLOS +
m_lfyLOS * m_lfyLOS +
m_lfzLOS * m_lfzLOS));
normalizedLOS.push_back(m_lfzLOS/sqrt(m_lfxLOS * m_lfxLOS +
m_lfyLOS * m_lfyLOS +
m_lfzLOS * m_lfzLOS));
return normalizedLOS;
}
vector<double> GLViewer::normalizeCross(std::vector<double> fVec,
std::vector<double> upVec)
{
vector<double>normalizedCross;
// i = j x k - k x j
normalizedCross.push_back(fVec[1] * upVec[2] - fVec[2] * upVec[1]);
// j = k x i - i x k
normalizedCross.push_back(fVec[2] * upVec[0] - fVec[0] * upVec[2]);
// k = i x j - j x i
normalizedCross.push_back(fVec[0] * upVec[1] - fVec[1] * upVec[0]);
return normalizedCross;
}
// Draws triangles
void GLViewer::drawGLTriangles(vector<pcl::PolygonMesh>::iterator it)
{
PointCloudT *meshCloud = new PointCloudT;
pcl::fromPCLPointCloud2(it->cloud, *meshCloud);
size_t nPolygons = it->polygons.size();
for(size_t i=0; i < nPolygons; ++i)
{
int v1, v2, v3; // Polygon vertices map
v1 = it->polygons[i].vertices[0];
v2 = it->polygons[i].vertices[1];
v3 = it->polygons[i].vertices[2];
PointT p1 = meshCloud->points[v1]; // Cloud points for vertices
PointT p2 = meshCloud->points[v2];
PointT p3 = meshCloud->points[v3];
// Average color for all three vertices
float rAvg = float(int(p1.r)+int(p2.r)+int(p3.r))/3.0f/255.0f;
float gAvg = float(int(p1.g)+int(p2.g)+int(p3.g))/3.0f/255.0f;
float bAvg = float(int(p1.b)+int(p2.b)+int(p3.b))/3.0f/255.0f;
float aAvg = float(int(p1.a)+int(p2.a)+int(p3.a))/3.0f/255.0f;
// Sets triangle color
glColor4f(rAvg, gAvg, bAvg, aAvg);
// Draws triangle
glBegin(GL_TRIANGLES);
glVertex3d(p1.x, p1.y, p1.z);
glVertex3d(p2.x, p2.y, p2.z);
glVertex3d(p3.x, p3.y, p3.z);
glEnd();
}
delete meshCloud; // Clean up memory
}
// Throw an exception if the cloud does not exist
bool GLViewer::checkShowClouds(size_t i)
{
if(m_vbShowClouds.size() <= i)
{
throw SmallDimension();
}
else
{
return m_vbShowClouds[i];
}
return false;
}
// Throw an exception if the mesh does not exist
bool GLViewer::checkShowMeshes(size_t i)
{
if(m_vbShowMeshes.size() <= i)
{
throw SmallDimension();
}
else
{
return m_vbShowMeshes[i];
}
return false;
}
// Renders the scene
void GLViewer::paintGL()
{
QSize viewport_size = size(); // View window height/width size
// Activates GL window; necessary for any rendering to occur
glViewport(0, 0, viewport_size.width(), viewport_size.height());
// Sets camera transformations
glMatrixMode(GL_PROJECTION);
// Clears buffers for drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); // Sets camera transformations
glLoadIdentity(); // Resets object transformations
// Sets camera fov, aspect ratio, and near/far values
gluPerspective(45.0, 1.0, 0.01f, 350.0);
vector<double> u1Vec = normalizeUP();
vector<double> f1Vec = normalizeLOS();
vector<double> s1Vec = normalizeCross(f1Vec, u1Vec);
float m1[16] = { s1Vec[0], s1Vec[1], s1Vec[2], 0,
u1Vec[0], u1Vec[1], u1Vec[2], 0,
-f1Vec[0], -f1Vec[1], -f1Vec[2], 0,
0, 0, 0, 1};
glMultMatrixf(m1);
double xDir = -m_lfxTrans - m_lfXAvg; // Treats object as if
double yDir = -m_lfyTrans - m_lfYAvg; // it is at the origin
double zDir = -m_lfzTrans - m_lfZAvg; // and rotates around it
glTranslated(-xDir, -yDir, -zDir);
glRotatef(m_nxRot / 16.0, 1.0, 0.0, 0.0); // Rotates camera
glRotatef(m_nyRot / 16.0, 0.0, 1.0, 0.0); // around origin
glRotatef(m_nzRot / 16.0, 0.0, 0.0, 1.0);
glTranslated(-m_lfXAvg, -m_lfYAvg, -m_lfZAvg); // Move the camera
// relative to object
// Origin axis
if(m_bDrawAxis)
{
xDir = m_lfXMax - m_lfXMin; // Shrink origin axis based on object
if(m_lfYMax - m_lfYMin < xDir) // size
{
xDir = m_lfYMax - m_lfYMin;
}
if(m_lfZMax - m_lfZMin < xDir)
{
xDir = m_lfZMax - m_lfZMin;
}
glBegin(GL_LINES);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(xDir, 0.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor4f(0.0, 1.0, 0.0, 1.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, xDir, 0.0);
glEnd();
glBegin(GL_LINES);
glColor4f(0.0, 0.0, 1.0, 1.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, xDir);