/
ManualAlignment.cpp
1355 lines (1180 loc) · 43.1 KB
/
ManualAlignment.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
/***************************************************************************
* Copyright (c) 2012 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QAction>
# include <QApplication>
# include <QLabel>
# include <QMenu>
# include <QMessageBox>
# include <QPainter>
# include <QSplitter>
# include <QTimer>
# include <QVBoxLayout>
# include <Inventor/SoPickedPoint.h>
# include <Inventor/actions/SoSearchAction.h>
# include <Inventor/events/SoMouseButtonEvent.h>
# include <Inventor/fields/SoSFImage.h>
# include <Inventor/nodes/SoImage.h>
# include <Inventor/nodes/SoMaterial.h>
# include <Inventor/nodes/SoOrthographicCamera.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoTranslation.h>
# include <Inventor/sensors/SoNodeSensor.h>
#endif
#include <App/Document.h>
#include <App/GeoFeature.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
#include <Gui/MainWindow.h>
#include <Gui/Selection.h>
#include <Gui/SplitView3DInventor.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/ViewProviderGeometryObject.h>
#include <Gui/WaitCursor.h>
#include "ManualAlignment.h"
#include "BitmapFactory.h"
#include "SoAxisCrossKit.h"
#include "Tools.h"
using namespace Gui;
namespace bp = boost::placeholders;
AlignmentGroup::AlignmentGroup()
{
}
AlignmentGroup::~AlignmentGroup()
{
}
void AlignmentGroup::addView(App::DocumentObject* pView)
{
if (pView) {
App::Document* rDoc = pView->getDocument();
Gui::Document* pDoc = Gui::Application::Instance->getDocument(rDoc);
auto pProvider = static_cast<Gui::ViewProviderDocumentObject*>
(pDoc->getViewProvider(pView));
this->_views.push_back(pProvider);
}
}
std::vector<App::DocumentObject*> AlignmentGroup::getViews() const
{
std::vector<App::DocumentObject*> views;
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it) {
App::DocumentObject* pView = (*it)->getObject();
views.push_back(pView);
}
return views;
}
bool AlignmentGroup::hasView(Gui::ViewProviderDocumentObject* pView) const
{
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it) {
if (*it == pView)
return true;
}
return false;
}
void AlignmentGroup::removeView(Gui::ViewProviderDocumentObject* pView)
{
std::vector<Gui::ViewProviderDocumentObject*>::iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it) {
if (*it == pView) {
this->_views.erase(it);
break;
}
}
}
void AlignmentGroup::addToViewer(Gui::View3DInventorViewer* viewer) const
{
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it)
viewer->addViewProvider(*it);
viewer->viewAll();
}
void AlignmentGroup::removeFromViewer(Gui::View3DInventorViewer* viewer) const
{
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it)
viewer->removeViewProvider(*it);
}
void AlignmentGroup::setRandomColor()
{
std::vector<Gui::ViewProviderDocumentObject*>::iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it) {
float r = /*(float)rand()/(float)RAND_MAX*/0.0f;
float g = (float)rand()/(float)RAND_MAX;
float b = (float)rand()/(float)RAND_MAX;
if ((*it)->isDerivedFrom(Gui::ViewProviderGeometryObject::getClassTypeId())) {
SoSearchAction searchAction;
searchAction.setType(SoMaterial::getClassTypeId());
searchAction.setInterest(SoSearchAction::FIRST);
searchAction.apply((*it)->getRoot());
SoPath* selectionPath = searchAction.getPath();
if (selectionPath) {
auto material = static_cast<SoMaterial*>(selectionPath->getTail());
material->diffuseColor.setValue(r, g, b);
}
}
}
}
Gui::Document* AlignmentGroup::getDocument() const
{
if (this->_views.empty())
return nullptr;
App::DocumentObject* pView = this->_views[0]->getObject();
if (pView) {
App::Document* rDoc = pView->getDocument();
Gui::Document* pDoc = Gui::Application::Instance->getDocument(rDoc);
return pDoc;
}
return nullptr;
}
void AlignmentGroup::addPoint(const PickedPoint& pnt)
{
this->_pickedPoints.push_back(pnt);
}
void AlignmentGroup::removeLastPoint()
{
this->_pickedPoints.pop_back();
}
int AlignmentGroup::countPoints() const
{
return this->_pickedPoints.size();
}
const std::vector<PickedPoint>& AlignmentGroup::getPoints() const
{
return this->_pickedPoints;
}
void AlignmentGroup::clearPoints()
{
this->_pickedPoints.clear();
}
void AlignmentGroup::setAlignable(bool align)
{
std::vector<Gui::ViewProviderDocumentObject*>::iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it) {
auto pAlignMode = dynamic_cast<App::PropertyBool*>((*it)->getPropertyByName("AlignMode"));
if (pAlignMode) {
pAlignMode->setValue(align);
}
// leaving alignment mode
else if (!align){
auto pColor = dynamic_cast<App::PropertyColor*>((*it)->getPropertyByName("ShapeColor"));
if (pColor)
pColor->touch(); // resets to color defined by property
}
}
}
void AlignmentGroup::moveTo(AlignmentGroup& that)
{
std::vector<Gui::ViewProviderDocumentObject*>::iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it)
that._views.push_back(*it);
this->_views.clear();
}
void AlignmentGroup::clear()
{
this->_views.clear();
this->_pickedPoints.clear();
}
bool AlignmentGroup::isEmpty() const
{
return this->_views.empty();
}
int AlignmentGroup::count() const
{
return this->_views.size();
}
Base::BoundBox3d AlignmentGroup::getBoundingBox() const
{
Base::BoundBox3d box;
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it;
for (it = this->_views.begin(); it != this->_views.end(); ++it) {
if ((*it)->isDerivedFrom(Gui::ViewProviderGeometryObject::getClassTypeId())) {
auto geo = static_cast<App::GeoFeature*>((*it)->getObject());
const App::PropertyComplexGeoData* prop = geo->getPropertyOfGeometry();
if (prop)
box.Add(prop->getBoundingBox());
}
}
return box;
}
// ------------------------------------------------------------------
MovableGroup::MovableGroup()
{
}
MovableGroup::~MovableGroup()
{
}
// ------------------------------------------------------------------
FixedGroup::FixedGroup()
{
}
FixedGroup::~FixedGroup()
{
}
// ------------------------------------------------------------------
MovableGroupModel::MovableGroupModel()
{
}
MovableGroupModel::~MovableGroupModel()
{
}
void MovableGroupModel::addGroup(const MovableGroup& grp)
{
this->_groups.push_back(grp);
}
void MovableGroupModel::addGroups(const std::map<int, MovableGroup>& grps)
{
for (const auto & grp : grps)
this->_groups.push_back(grp.second);
}
void MovableGroupModel::removeActiveGroup()
{
this->_groups.erase(this->_groups.begin());
}
MovableGroup& MovableGroupModel::activeGroup()
{
// Make sure that the array is not empty
if (this->_groups.empty())
throw Base::RuntimeError("Empty group");
return *(this->_groups.begin());
}
const MovableGroup& MovableGroupModel::activeGroup() const
{
// Make sure that the array is not empty
if (this->_groups.empty())
throw Base::RuntimeError("Empty group");
return this->_groups.front();
}
void MovableGroupModel::continueAlignment()
{
if (!isEmpty())
removeActiveGroup();
}
void MovableGroupModel::clear()
{
this->_groups.clear();
}
bool MovableGroupModel::isEmpty() const
{
return this->_groups.empty();
}
int MovableGroupModel::count() const
{
return this->_groups.size();
}
const MovableGroup& MovableGroupModel::getGroup(int i) const
{
if (i >= count())
throw Base::IndexError("Index out of range");
return this->_groups[i];
}
Base::BoundBox3d MovableGroupModel::getBoundingBox() const
{
Base::BoundBox3d box;
std::vector<MovableGroup>::const_iterator it;
for (it = this->_groups.begin(); it != this->_groups.end(); ++it) {
box.Add(it->getBoundingBox());
}
return box;
}
// ------------------------------------------------------------------
namespace Gui {
class AlignmentView : public Gui::AbstractSplitView
{
public:
QLabel* myLabel;
AlignmentView(Gui::Document* pcDocument, QWidget* parent, Qt::WindowFlags wflags=Qt::WindowFlags())
: AbstractSplitView(pcDocument, parent, wflags)
{
//anti-aliasing settings
bool smoothing = false;
bool glformat = false;
int samples = View3DInventorViewer::getNumSamples();
QtGLFormat f;
if (samples > 1) {
glformat = true;
f.setSamples(samples);
}
else if (samples > 0) {
smoothing = true;
}
QSplitter* mainSplitter=nullptr;
mainSplitter = new QSplitter(Qt::Horizontal, this);
if (glformat) {
_viewer.push_back(new View3DInventorViewer(f, mainSplitter));
_viewer.back()->setDocument(pcDocument);
_viewer.push_back(new View3DInventorViewer(f, mainSplitter));
_viewer.back()->setDocument(pcDocument);
}
else {
_viewer.push_back(new View3DInventorViewer(mainSplitter));
_viewer.back()->setDocument(pcDocument);
_viewer.push_back(new View3DInventorViewer(mainSplitter));
_viewer.back()->setDocument(pcDocument);
}
auto vbox = new QFrame(this);
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
vbox->setLayout(layout);
myLabel = new QLabel(this);
myLabel->setAutoFillBackground(true);
QPalette pal = myLabel->palette();
pal.setColor(QPalette::Window, Qt::darkGray);
pal.setColor(QPalette::WindowText, Qt::white);
myLabel->setPalette(pal);
mainSplitter->setPalette(pal);
myLabel->setAlignment(Qt::AlignCenter);
myLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QFont font = myLabel->font();
font.setPointSize(14);
myLabel->setFont(font);
layout->addWidget(myLabel);
layout->addWidget(mainSplitter);
vbox->show();
setCentralWidget(vbox);
// apply the user settings
setupSettings();
if (smoothing) {
for (const auto & i : _viewer)
i->getSoRenderManager()->getGLRenderAction()->setSmoothing(true);
}
static_cast<SoGroup*>(getViewer(0)->getSoRenderManager()->getSceneGraph())->
addChild(setupHeadUpDisplay(tr("Movable object")));
static_cast<SoGroup*>(getViewer(1)->getSoRenderManager()->getSceneGraph())->
addChild(setupHeadUpDisplay(tr("Fixed object")));
}
~AlignmentView() override
{
}
PyObject* getPyObject() override
{
Py_Return;
}
bool canClose() override
{
return false;
}
SoNode* setupHeadUpDisplay(const QString& text) const
{
auto hudRoot = new SoSeparator;
hudRoot->ref();
auto hudCam = new SoOrthographicCamera();
hudCam->viewportMapping = SoCamera::LEAVE_ALONE;
// Set the position in the window.
// [0, 0] is in the center of the screen.
//
auto hudTrans = new SoTranslation;
hudTrans->translation.setValue(-0.95f, -0.95f, 0.0f);
QFont font = this->font();
font.setPointSize(24);
QFontMetrics fm(font);
QColor front;
front.setRgbF(0.8f, 0.8f, 0.8f);
int w = QtTools::horizontalAdvance(fm, text);
int h = fm.height();
QImage image(w,h,QImage::Format_ARGB32_Premultiplied);
image.fill(0x00000000);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(front);
painter.setFont(font);
painter.drawText(0,0,w,h,Qt::AlignLeft,text);
painter.end();
SoSFImage sfimage;
Gui::BitmapFactory().convert(image, sfimage);
auto hudImage = new SoImage();
hudImage->image = sfimage;
// Assemble the parts...
//
hudRoot->addChild(hudCam);
hudRoot->addChild(hudTrans);
hudRoot->addChild(hudImage);
return hudRoot;
}
};
}
class ManualAlignment::Private {
public:
SoSeparator * picksepLeft;
SoSeparator * picksepRight;
SoNodeSensor* sensorCam1;
SoNodeSensor* sensorCam2;
SbRotation rot_cam1, rot_cam2;
SbVec3f pos_cam1, pos_cam2;
Private()
: sensorCam1(nullptr), sensorCam2(nullptr)
{
// left view
picksepLeft = new SoSeparator;
picksepLeft->ref();
// right view
picksepRight = new SoSeparator;
picksepRight->ref();
}
~Private()
{
picksepLeft->unref();
picksepRight->unref();
delete sensorCam1;
delete sensorCam2;
}
static
void reorientCamera(SoCamera * cam, const SbRotation & rot)
{
if (!cam)
return;
// Find global coordinates of focal point.
SbVec3f direction;
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);
SbVec3f focalpoint = cam->position.getValue() +
cam->focalDistance.getValue() * direction;
// Set new orientation value by accumulating the new rotation.
cam->orientation = rot * cam->orientation.getValue();
// Reposition camera so we are still pointing at the same old focal point.
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);
cam->position = focalpoint - cam->focalDistance.getValue() * direction;
}
static
void copyCameraSettings(SoCamera* cam1, SbRotation& rot_cam1, SbVec3f& pos_cam1,
SoCamera* cam2, SbRotation& rot_cam2, SbVec3f& pos_cam2)
{
Q_UNUSED(pos_cam2);
// recompute the diff we have applied to the camera's orientation
SbRotation rot = cam1->orientation.getValue();
SbRotation dif = rot * rot_cam1.inverse();
rot_cam1 = rot;
// copy the values
cam2->enableNotify(false);
cam2->nearDistance = cam1->nearDistance;
cam2->farDistance = cam1->farDistance;
cam2->focalDistance = cam1->focalDistance;
reorientCamera(cam2,dif);
rot_cam2 = cam2->orientation.getValue();
// reverse engineer the translation part in wc
SbVec3f pos = cam1->position.getValue();
SbVec3f difpos = pos - pos_cam1;
pos_cam1 = pos;
// the translation in pixel coords
cam1->orientation.getValue().inverse().multVec(difpos,difpos);
// the translation again in wc for the second camera
cam2->orientation.getValue().multVec(difpos,difpos);
cam2->position.setValue(cam2->position.getValue()+difpos);
if (cam1->getTypeId() == cam2->getTypeId()) {
if (cam1->getTypeId() == SoOrthographicCamera::getClassTypeId())
static_cast<SoOrthographicCamera*>(cam2)->height =
static_cast<SoOrthographicCamera*>(cam1)->height;
}
cam2->enableNotify(true);
}
static
void syncCameraCB(void * data, SoSensor * s)
{
auto self = static_cast<ManualAlignment*>(data);
if (!self->myViewer)
return; // already destroyed
SoCamera* cam1 = self->myViewer->getViewer(0)->getSoRenderManager()->getCamera();
SoCamera* cam2 = self->myViewer->getViewer(1)->getSoRenderManager()->getCamera();
if (!cam1 || !cam2)
return; // missing camera
auto sensor = static_cast<SoNodeSensor*>(s);
SoNode* node = sensor->getAttachedNode();
if (node && node->getTypeId().isDerivedFrom(SoCamera::getClassTypeId())) {
if (node == cam1) {
Private::copyCameraSettings(cam1, self->d->rot_cam1, self->d->pos_cam1,
cam2, self->d->rot_cam2, self->d->pos_cam2);
self->myViewer->getViewer(1)->redraw();
}
else if (node == cam2) {
Private::copyCameraSettings(cam2, self->d->rot_cam2, self->d->pos_cam2,
cam1, self->d->rot_cam1, self->d->pos_cam1);
self->myViewer->getViewer(0)->redraw();
}
}
}
static Base::Placement
transformation2x2(const Base::Vector3d& plane1_base,
const Base::Vector3d& plane1_xaxis,
const Base::Vector3d& plane2_base,
const Base::Vector3d& plane2_xaxis)
{
// the transformation is:
// * move from plane1_base to plane2_base
// * rotate from plane1_zaxis to plane2_zaxis around plane2_base as center point
Base::Rotation rot(plane1_xaxis, plane2_xaxis);
Base::Vector3d pln_base;
rot.multVec(plane1_base,pln_base);
Base::Vector3d dif = plane2_base - pln_base;
return Base::Placement(dif, rot);
}
static Base::Placement
transformation3x3(const Base::Vector3d& plane1_base,
const Base::Vector3d& plane1_zaxis,
const Base::Vector3d& plane1_xaxis,
const Base::Vector3d& plane2_base,
const Base::Vector3d& plane2_zaxis,
const Base::Vector3d& plane2_xaxis)
{
// the transformation is:
// * move from plane1_base to plane2_base
// * rotate from plane1_zaxis to plane2_zaxis around plane2_base as center point
Base::Rotation rot(plane1_zaxis, plane2_zaxis);
// first transformation to align the plane normals and base points
Base::Vector3d dif1 = plane1_base;
rot.multVec(dif1,dif1);
dif1 = plane2_base - dif1;
Base::Placement plm1(dif1, rot);
// second transformation to align the planes' x axes
Base::Vector3d pln_xaxis;
rot.multVec(plane1_xaxis,pln_xaxis);
Base::Rotation rot2(pln_xaxis, plane2_xaxis);
Base::Vector3d dif2 = plane2_base;
rot2.multVec(dif2,dif2);
dif2 = plane2_base - dif2;
Base::Placement plm2(dif2, rot2);
plm2 = plm2 * plm1;
return plm2;
}
};
/* TRANSLATOR Gui::ManualAlignment */
ManualAlignment* ManualAlignment::_instance = nullptr;
/**
* Construction.
*/
ManualAlignment::ManualAlignment()
: myViewer(nullptr), myDocument(nullptr), myPickPoints(3), d(new Private)
{
// connect with the application's signal for deletion of documents
this->connectApplicationDeletedDocument = Gui::Application::Instance->signalDeleteDocument
.connect(boost::bind(&ManualAlignment::slotDeletedDocument, this, bp::_1));
// setup sensor connection
d->sensorCam1 = new SoNodeSensor(Private::syncCameraCB, this);
d->sensorCam2 = new SoNodeSensor(Private::syncCameraCB, this);
}
/**
* Destruction.
*/
ManualAlignment::~ManualAlignment()
{
this->connectDocumentDeletedObject.disconnect();
this->connectApplicationDeletedDocument.disconnect();
closeViewer();
delete d;
_instance = nullptr;
}
/**
* Creates the one and only instance of this class.
*/
ManualAlignment* ManualAlignment::instance()
{
// not initialized?
if (!_instance)
_instance = new ManualAlignment();
return _instance;
}
/**
* Destructs the one and only instance of this class.
*/
void ManualAlignment::destruct()
{
if (_instance) {
ManualAlignment* tmp = _instance;
_instance = nullptr;
delete tmp;
}
}
/**
* Checks whether the one instance exists.
*/
bool ManualAlignment::hasInstance()
{
return _instance != nullptr;
}
void ManualAlignment::setMinPoints(int minPoints)
{
if ((minPoints > 0) && (minPoints <= 3))
myPickPoints = minPoints;
}
void ManualAlignment::setFixedGroup(const FixedGroup& fixed)
{
this->myFixedGroup = fixed;
this->myDocument = fixed.getDocument();
}
void ManualAlignment::setModel(const MovableGroupModel& model)
{
this->myAlignModel = model;
}
void ManualAlignment::clearAll()
{
myFixedGroup.clear();
myAlignModel.clear();
myDocument = nullptr;
}
void ManualAlignment::setViewingDirections(const Base::Vector3d& view1, const Base::Vector3d& up1,
const Base::Vector3d& view2, const Base::Vector3d& up2)
{
if (myViewer.isNull())
return;
{
SbVec3f vz(-view1.x, -view1.y, -view1.z);
vz.normalize();
SbVec3f vy(up1.x, up1.y, up1.z);
vy.normalize();
SbVec3f vx = vy.cross(vz);
vy = vz.cross(vx);
SbMatrix rot = SbMatrix::identity();
rot[0][0] = vx[0];
rot[0][1] = vx[1];
rot[0][2] = vx[2];
rot[1][0] = vy[0];
rot[1][1] = vy[1];
rot[1][2] = vy[2];
rot[2][0] = vz[0];
rot[2][1] = vz[1];
rot[2][2] = vz[2];
SbRotation total(rot);
myViewer->getViewer(0)->getSoRenderManager()->getCamera()->orientation.setValue(total);
myViewer->getViewer(0)->viewAll();
}
{
SbVec3f vz(-view2.x, -view2.y, -view2.z);
vz.normalize();
SbVec3f vy(up2.x, up2.y, up2.z);
vy.normalize();
SbVec3f vx = vy.cross(vz);
vy = vz.cross(vx);
SbMatrix rot = SbMatrix::identity();
rot[0][0] = vx[0];
rot[0][1] = vx[1];
rot[0][2] = vx[2];
rot[1][0] = vy[0];
rot[1][1] = vy[1];
rot[1][2] = vy[2];
rot[2][0] = vz[0];
rot[2][1] = vz[1];
rot[2][2] = vz[2];
SbRotation total(rot);
myViewer->getViewer(1)->getSoRenderManager()->getCamera()->orientation.setValue(total);
myViewer->getViewer(1)->viewAll();
}
}
/**
* Performs the alignment for the specified aligned and non-aligned views specified by setModel() and setFixedGroup().
*/
void ManualAlignment::startAlignment(Base::Type mousemodel)
{
// allow only one alignment at a time
if (!myViewer.isNull()) {
QMessageBox::warning(qApp->activeWindow(), tr("Manual alignment"), tr("The alignment is already in progress."));
return;
}
myTransform = Base::Placement();
if (myFixedGroup.isEmpty())
return;
if (myAlignModel.isEmpty())
return;
// create a split window for picking the points
myViewer = new AlignmentView(myDocument,Gui::getMainWindow());
myViewer->setWindowTitle(tr("Alignment[*]"));
myViewer->setWindowIcon(QApplication::windowIcon());
myViewer->resize(400, 300);
Gui::getMainWindow()->addWindow(myViewer);
myViewer->showMaximized();
int n = this->myPickPoints;
QString msg = n == 1
? tr("Please, select at least one point in the left and the right view")
: tr("Please, select at least %1 points in the left and the right view").arg(n);
myViewer->myLabel->setText(msg);
connect(myViewer, SIGNAL(destroyed()), this, SLOT(reset()));
// show all aligned views in the 2nd view
myFixedGroup.addToViewer(myViewer->getViewer(1));
myFixedGroup.setAlignable(true);
// set picked points root
SoNode* node1 = myViewer->getViewer(0)->getSceneGraph();
if (node1->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())){
((SoGroup*)node1)->addChild(d->picksepLeft);
}
SoNode* node2 = myViewer->getViewer(1)->getSceneGraph();
if (node2->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())){
((SoGroup*)node2)->addChild(d->picksepRight);
}
myViewer->getViewer(0)->setEditing(true);
myViewer->getViewer(0)->addEventCallback(SoMouseButtonEvent::getClassTypeId(),
ManualAlignment::probePickedCallback);
myViewer->getViewer(1)->setEditing(true);
myViewer->getViewer(1)->addEventCallback(SoMouseButtonEvent::getClassTypeId(),
ManualAlignment::probePickedCallback);
// apply the mouse model
myViewer->getViewer(0)->setNavigationType(mousemodel);
myViewer->getViewer(1)->setNavigationType(mousemodel);
// Connect to the document's signal as we want to be notified when something happens
if (this->connectDocumentDeletedObject.connected())
this->connectDocumentDeletedObject.disconnect();
this->connectDocumentDeletedObject = myDocument->signalDeletedObject.connect(boost::bind
(&ManualAlignment::slotDeletedObject, this, bp::_1));
continueAlignment();
}
/**
* If still one view needs to be aligned then it is shown in the first window. If all views are aligned the process will be terminated.
*/
void ManualAlignment::continueAlignment()
{
myFixedGroup.clearPoints();
coinRemoveAllChildren(d->picksepLeft);
coinRemoveAllChildren(d->picksepRight);
if (!myAlignModel.isEmpty()) {
AlignmentGroup& grp = myAlignModel.activeGroup();
grp.clearPoints();
grp.addToViewer(myViewer->getViewer(0));
grp.setAlignable(true);
Gui::getMainWindow()->showMessage(tr("Please pick points in the left and right view"));
myViewer->getViewer(0)->setEditingCursor(QCursor(Qt::PointingHandCursor));
myViewer->getViewer(1)->setEditingCursor(QCursor(Qt::PointingHandCursor));
}
else {
finish();
}
}
void ManualAlignment::closeViewer()
{
if (!myViewer)
return;
// Close the viewer
if (myViewer->parentWidget())
myViewer->parentWidget()->deleteLater();
myViewer = nullptr;
}
/**
* Make all views unpickable and resets internal data.
*/
void ManualAlignment::reset()
{
if (!myAlignModel.isEmpty()) {
myAlignModel.activeGroup().setAlignable(false);
myAlignModel.activeGroup().clear();
myAlignModel.clear();
}
myFixedGroup.setAlignable(false);
myFixedGroup.clear();
coinRemoveAllChildren(d->picksepLeft);
coinRemoveAllChildren(d->picksepRight);
if (myDocument) {
this->connectDocumentDeletedObject.disconnect();
myDocument = nullptr;
}
}
/**
* Terminates the process and closes the windows.
*/
void ManualAlignment::finish()
{
if (myViewer.isNull())
return;
if (myDocument)
myDocument->getDocument()->recompute();
closeViewer();
reset();
Gui::getMainWindow()->showMessage(tr("The alignment has finished"));
// If an event receiver has been defined send the manual alignment finished event to it
Q_EMIT emitFinished();
}
/**
* Cancels the process and closes the windows without performing an alignment.
*/
void ManualAlignment::cancel()
{
if (myViewer.isNull())
return;
closeViewer();
myTransform = Base::Placement();
reset();
Gui::getMainWindow()->showMessage(tr("The alignment has been canceled"));
// If an event receiver has been defined send the manual alignment cancelled event to it
Q_EMIT emitCanceled();
}
void ManualAlignment::align()
{
// Now we can start the actual alignment
if (myAlignModel.activeGroup().countPoints() < myPickPoints) {
QMessageBox::warning(myViewer, tr("Manual alignment"),
tr("Too few points picked in the left view."
" At least %1 points are needed.").arg(myPickPoints));
}
else if (myFixedGroup.countPoints() < myPickPoints) {
QMessageBox::warning(myViewer, tr("Manual alignment"),
tr("Too few points picked in the right view."
" At least %1 points are needed.").arg(myPickPoints));
}
else if (myAlignModel.activeGroup().countPoints() != myFixedGroup.countPoints()) {
QMessageBox::warning(myViewer, tr("Manual alignment"),
tr("Different number of points picked in left and right view.\n"
"On the left view %1 points are picked,\n"
"on the right view %2 points are picked.")
.arg(myAlignModel.activeGroup().countPoints())
.arg(myFixedGroup.countPoints()));
}
else {
// do not allow to pick further points
myAlignModel.activeGroup().removeFromViewer(myViewer->getViewer(0));
myAlignModel.activeGroup().setAlignable(false);
std::vector<App::DocumentObject*> pViews = myAlignModel.activeGroup().getViews();
Gui::getMainWindow()->showMessage(tr("Try to align group of views"));
// Compute alignment
bool ok = computeAlignment(myAlignModel.activeGroup().getPoints(), myFixedGroup.getPoints());
if (ok && myDocument) {
// Align views
myDocument->openCommand(QT_TRANSLATE_NOOP("Command", "Align"));
for (const auto & pView : pViews)
alignObject(pView);
myDocument->commitCommand();
// the alignment was successful so show it in the right view now
//myAlignModel.activeGroup().setRandomColor();
myAlignModel.activeGroup().setAlignable(true);
myAlignModel.activeGroup().addToViewer(myViewer->getViewer(1));
myAlignModel.activeGroup().moveTo(myFixedGroup);
myAlignModel.continueAlignment();
}
else {