| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2012 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QAction> |
| 25 |
#include <QApplication> |
| 26 |
#include <QLabel> |
| 27 |
#include <QMenu> |
| 28 |
#include <QMessageBox> |
| 29 |
#include <QMdiSubWindow> |
| 30 |
#include <QPainter> |
| 31 |
#include <QSplitter> |
| 32 |
#include <QSurfaceFormat> |
| 33 |
#include <QTimer> |
| 34 |
#include <QVBoxLayout> |
| 35 |
#include <Inventor/SoPickedPoint.h> |
| 36 |
#include <Inventor/actions/SoSearchAction.h> |
| 37 |
#include <Inventor/events/SoMouseButtonEvent.h> |
| 38 |
#include <Inventor/fields/SoSFImage.h> |
| 39 |
#include <Inventor/nodes/SoImage.h> |
| 40 |
#include <Inventor/nodes/SoMaterial.h> |
| 41 |
#include <Inventor/nodes/SoOrthographicCamera.h> |
| 42 |
#include <Inventor/nodes/SoSeparator.h> |
| 43 |
#include <Inventor/nodes/SoTranslation.h> |
| 44 |
#include <Inventor/sensors/SoNodeSensor.h> |
| 45 |
|
| 46 |
|
| 47 |
#include <App/Document.h> |
| 48 |
#include <App/GeoFeature.h> |
| 49 |
#include <Gui/Application.h> |
| 50 |
#include <Gui/BitmapFactory.h> |
| 51 |
#include <Gui/Document.h> |
| 52 |
#include <Gui/Inventor/SoAxisCrossKit.h> |
| 53 |
#include <Gui/MainWindow.h> |
| 54 |
#include <Gui/Selection/Selection.h> |
| 55 |
#include <Gui/SplitView3DInventor.h> |
| 56 |
#include <Gui/Tools.h> |
| 57 |
#include <Gui/View3DInventorViewer.h> |
| 58 |
#include <Gui/ViewProviderGeometryObject.h> |
| 59 |
#include <Gui/WaitCursor.h> |
| 60 |
#include "ManualAlignment.h" |
| 61 |
|
| 62 |
|
| 63 |
using namespace Gui; |
| 64 |
namespace sp = std::placeholders; |
| 65 |
|
| 66 |
AlignmentGroup::AlignmentGroup() = default; |
| 67 |
|
| 68 |
AlignmentGroup::~AlignmentGroup() = default; |
| 69 |
|
| 70 |
void AlignmentGroup::addView(App::DocumentObject* pView) |
| 71 |
{ |
| 72 |
if (pView) { |
| 73 |
App::Document* rDoc = pView->getDocument(); |
| 74 |
Gui::Document* pDoc = Gui::Application::Instance->getDocument(rDoc); |
| 75 |
auto pProvider = static_cast<Gui::ViewProviderDocumentObject*>(pDoc->getViewProvider(pView)); |
| 76 |
this->_views.push_back(pProvider); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
std::vector<App::DocumentObject*> AlignmentGroup::getViews() const |
| 81 |
{ |
| 82 |
std::vector<App::DocumentObject*> views; |
| 83 |
|
| 84 |
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it; |
| 85 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 86 |
App::DocumentObject* pView = (*it)->getObject(); |
| 87 |
views.push_back(pView); |
| 88 |
} |
| 89 |
|
| 90 |
return views; |
| 91 |
} |
| 92 |
|
| 93 |
bool AlignmentGroup::hasView(Gui::ViewProviderDocumentObject* pView) const |
| 94 |
{ |
| 95 |
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it; |
| 96 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 97 |
if (*it == pView) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
void AlignmentGroup::removeView(Gui::ViewProviderDocumentObject* pView) |
| 106 |
{ |
| 107 |
std::vector<Gui::ViewProviderDocumentObject*>::iterator it; |
| 108 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 109 |
if (*it == pView) { |
| 110 |
this->_views.erase(it); |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void AlignmentGroup::addToViewer(Gui::View3DInventorViewer* viewer) const |
| 117 |
{ |
| 118 |
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it; |
| 119 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 120 |
viewer->addViewProvider(*it); |
| 121 |
} |
| 122 |
|
| 123 |
viewer->viewAll(); |
| 124 |
} |
| 125 |
|
| 126 |
void AlignmentGroup::removeFromViewer(Gui::View3DInventorViewer* viewer) const |
| 127 |
{ |
| 128 |
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it; |
| 129 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 130 |
viewer->removeViewProvider(*it); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void AlignmentGroup::setRandomColor() |
| 135 |
{ |
| 136 |
std::vector<Gui::ViewProviderDocumentObject*>::iterator it; |
| 137 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 138 |
float r = /*(float)rand()/(float)RAND_MAX*/ 0.0f; |
| 139 |
float g = (float)rand() / (float)RAND_MAX; |
| 140 |
float b = (float)rand() / (float)RAND_MAX; |
| 141 |
if ((*it)->isDerivedFrom<Gui::ViewProviderGeometryObject>()) { |
| 142 |
SoSearchAction searchAction; |
| 143 |
searchAction.setType(SoMaterial::getClassTypeId()); |
| 144 |
searchAction.setInterest(SoSearchAction::FIRST); |
| 145 |
searchAction.apply((*it)->getRoot()); |
| 146 |
SoPath* selectionPath = searchAction.getPath(); |
| 147 |
|
| 148 |
if (selectionPath) { |
| 149 |
auto material = static_cast<SoMaterial*>(selectionPath->getTail()); |
| 150 |
material->diffuseColor.setValue(r, g, b); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
Gui::Document* AlignmentGroup::getDocument() const |
| 157 |
{ |
| 158 |
if (this->_views.empty()) { |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
App::DocumentObject* pView = this->_views[0]->getObject(); |
| 162 |
if (pView) { |
| 163 |
App::Document* rDoc = pView->getDocument(); |
| 164 |
Gui::Document* pDoc = Gui::Application::Instance->getDocument(rDoc); |
| 165 |
return pDoc; |
| 166 |
} |
| 167 |
|
| 168 |
return nullptr; |
| 169 |
} |
| 170 |
|
| 171 |
void AlignmentGroup::addPoint(const PickedPoint& pnt) |
| 172 |
{ |
| 173 |
this->_pickedPoints.push_back(pnt); |
| 174 |
} |
| 175 |
|
| 176 |
void AlignmentGroup::removeLastPoint() |
| 177 |
{ |
| 178 |
this->_pickedPoints.pop_back(); |
| 179 |
} |
| 180 |
|
| 181 |
int AlignmentGroup::countPoints() const |
| 182 |
{ |
| 183 |
return this->_pickedPoints.size(); |
| 184 |
} |
| 185 |
|
| 186 |
const std::vector<PickedPoint>& AlignmentGroup::getPoints() const |
| 187 |
{ |
| 188 |
return this->_pickedPoints; |
| 189 |
} |
| 190 |
|
| 191 |
void AlignmentGroup::clearPoints() |
| 192 |
{ |
| 193 |
this->_pickedPoints.clear(); |
| 194 |
} |
| 195 |
|
| 196 |
void AlignmentGroup::setAlignable(bool align) |
| 197 |
{ |
| 198 |
std::vector<Gui::ViewProviderDocumentObject*>::iterator it; |
| 199 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 200 |
auto pAlignMode = dynamic_cast<App::PropertyBool*>((*it)->getPropertyByName("AlignMode")); |
| 201 |
if (pAlignMode) { |
| 202 |
pAlignMode->setValue(align); |
| 203 |
} |
| 204 |
// leaving alignment mode |
| 205 |
else if (!align) { |
| 206 |
auto pAppearance = dynamic_cast<App::PropertyMaterial*>( |
| 207 |
(*it)->getPropertyByName("ShapeAppearance") |
| 208 |
); |
| 209 |
if (pAppearance) { |
| 210 |
pAppearance->touch(); // resets to color defined by property |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void AlignmentGroup::moveTo(AlignmentGroup& that) |
| 217 |
{ |
| 218 |
std::vector<Gui::ViewProviderDocumentObject*>::iterator it; |
| 219 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 220 |
that._views.push_back(*it); |
| 221 |
} |
| 222 |
|
| 223 |
this->_views.clear(); |
| 224 |
} |
| 225 |
|
| 226 |
void AlignmentGroup::clear() |
| 227 |
{ |
| 228 |
this->_views.clear(); |
| 229 |
this->_pickedPoints.clear(); |
| 230 |
} |
| 231 |
|
| 232 |
bool AlignmentGroup::isEmpty() const |
| 233 |
{ |
| 234 |
return this->_views.empty(); |
| 235 |
} |
| 236 |
|
| 237 |
int AlignmentGroup::count() const |
| 238 |
{ |
| 239 |
return this->_views.size(); |
| 240 |
} |
| 241 |
|
| 242 |
Base::BoundBox3d AlignmentGroup::getBoundingBox() const |
| 243 |
{ |
| 244 |
Base::BoundBox3d box; |
| 245 |
std::vector<Gui::ViewProviderDocumentObject*>::const_iterator it; |
| 246 |
for (it = this->_views.begin(); it != this->_views.end(); ++it) { |
| 247 |
if ((*it)->isDerivedFrom<Gui::ViewProviderGeometryObject>()) { |
| 248 |
auto geo = (*it)->getObject<App::GeoFeature>(); |
| 249 |
const App::PropertyComplexGeoData* prop = geo->getPropertyOfGeometry(); |
| 250 |
if (prop) { |
| 251 |
box.Add(prop->getBoundingBox()); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
return box; |
| 256 |
} |
| 257 |
|
| 258 |
// ------------------------------------------------------------------ |
| 259 |
|
| 260 |
MovableGroup::MovableGroup() = default; |
| 261 |
|
| 262 |
MovableGroup::~MovableGroup() = default; |
| 263 |
|
| 264 |
// ------------------------------------------------------------------ |
| 265 |
|
| 266 |
FixedGroup::FixedGroup() = default; |
| 267 |
|
| 268 |
FixedGroup::~FixedGroup() = default; |
| 269 |
|
| 270 |
// ------------------------------------------------------------------ |
| 271 |
|
| 272 |
MovableGroupModel::MovableGroupModel() = default; |
| 273 |
|
| 274 |
MovableGroupModel::~MovableGroupModel() = default; |
| 275 |
|
| 276 |
void MovableGroupModel::addGroup(const MovableGroup& grp) |
| 277 |
{ |
| 278 |
this->_groups.push_back(grp); |
| 279 |
} |
| 280 |
|
| 281 |
void MovableGroupModel::addGroups(const std::map<int, MovableGroup>& grps) |
| 282 |
{ |
| 283 |
for (const auto& grp : grps) { |
| 284 |
this->_groups.push_back(grp.second); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void MovableGroupModel::removeActiveGroup() |
| 289 |
{ |
| 290 |
this->_groups.erase(this->_groups.begin()); |
| 291 |
} |
| 292 |
|
| 293 |
MovableGroup& MovableGroupModel::activeGroup() |
| 294 |
{ |
| 295 |
// Make sure that the array is not empty |
| 296 |
if (this->_groups.empty()) { |
| 297 |
throw Base::RuntimeError("Empty group"); |
| 298 |
} |
| 299 |
return *(this->_groups.begin()); |
| 300 |
} |
| 301 |
|
| 302 |
const MovableGroup& MovableGroupModel::activeGroup() const |
| 303 |
{ |
| 304 |
// Make sure that the array is not empty |
| 305 |
if (this->_groups.empty()) { |
| 306 |
throw Base::RuntimeError("Empty group"); |
| 307 |
} |
| 308 |
return this->_groups.front(); |
| 309 |
} |
| 310 |
|
| 311 |
void MovableGroupModel::continueAlignment() |
| 312 |
{ |
| 313 |
if (!isEmpty()) { |
| 314 |
removeActiveGroup(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
void MovableGroupModel::clear() |
| 319 |
{ |
| 320 |
this->_groups.clear(); |
| 321 |
} |
| 322 |
|
| 323 |
bool MovableGroupModel::isEmpty() const |
| 324 |
{ |
| 325 |
return this->_groups.empty(); |
| 326 |
} |
| 327 |
|
| 328 |
int MovableGroupModel::count() const |
| 329 |
{ |
| 330 |
return this->_groups.size(); |
| 331 |
} |
| 332 |
|
| 333 |
const MovableGroup& MovableGroupModel::getGroup(int i) const |
| 334 |
{ |
| 335 |
if (i >= count()) { |
| 336 |
throw Base::IndexError("Index out of range"); |
| 337 |
} |
| 338 |
return this->_groups[i]; |
| 339 |
} |
| 340 |
|
| 341 |
Base::BoundBox3d MovableGroupModel::getBoundingBox() const |
| 342 |
{ |
| 343 |
Base::BoundBox3d box; |
| 344 |
std::vector<MovableGroup>::const_iterator it; |
| 345 |
for (it = this->_groups.begin(); it != this->_groups.end(); ++it) { |
| 346 |
box.Add(it->getBoundingBox()); |
| 347 |
} |
| 348 |
return box; |
| 349 |
} |
| 350 |
|
| 351 |
// ------------------------------------------------------------------ |
| 352 |
|
| 353 |
namespace Gui |
| 354 |
{ |
| 355 |
class AlignmentView: public Gui::AbstractSplitView |
| 356 |
{ |
| 357 |
public: |
| 358 |
QLabel* myLabel; |
| 359 |
|
| 360 |
AlignmentView(Gui::Document* pcDocument, QWidget* parent, Qt::WindowFlags wflags = Qt::WindowFlags()) |
| 361 |
: AbstractSplitView(pcDocument, parent, wflags) |
| 362 |
{ |
| 363 |
// anti-aliasing settings |
| 364 |
bool smoothing = false; |
| 365 |
bool glformat = false; |
| 366 |
int samples = View3DInventorViewer::getNumSamples(); |
| 367 |
QSurfaceFormat f; |
| 368 |
|
| 369 |
if (samples > 1) { |
| 370 |
glformat = true; |
| 371 |
f.setSamples(samples); |
| 372 |
} |
| 373 |
else if (samples > 0) { |
| 374 |
smoothing = true; |
| 375 |
} |
| 376 |
|
| 377 |
QSplitter* mainSplitter = nullptr; |
| 378 |
mainSplitter = new QSplitter(Qt::Horizontal, this); |
| 379 |
if (glformat) { |
| 380 |
_viewer.push_back(new View3DInventorViewer(f, mainSplitter)); |
| 381 |
_viewer.push_back(new View3DInventorViewer(f, mainSplitter)); |
| 382 |
} |
| 383 |
else { |
| 384 |
_viewer.push_back(new View3DInventorViewer(mainSplitter)); |
| 385 |
_viewer.push_back(new View3DInventorViewer(mainSplitter)); |
| 386 |
} |
| 387 |
setDocumentOfViewers(pcDocument); |
| 388 |
|
| 389 |
auto vbox = new QFrame(this); |
| 390 |
auto layout = new QVBoxLayout(); |
| 391 |
layout->setContentsMargins(0, 0, 0, 0); |
| 392 |
layout->setSpacing(0); |
| 393 |
vbox->setLayout(layout); |
| 394 |
|
| 395 |
myLabel = new QLabel(this); |
| 396 |
myLabel->setAutoFillBackground(true); |
| 397 |
QPalette pal = myLabel->palette(); |
| 398 |
pal.setColor(QPalette::Window, Qt::darkGray); |
| 399 |
pal.setColor(QPalette::WindowText, Qt::white); |
| 400 |
myLabel->setPalette(pal); |
| 401 |
mainSplitter->setPalette(pal); |
| 402 |
myLabel->setAlignment(Qt::AlignCenter); |
| 403 |
myLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
| 404 |
QFont font = myLabel->font(); |
| 405 |
font.setPointSize(14); |
| 406 |
myLabel->setFont(font); |
| 407 |
layout->addWidget(myLabel); |
| 408 |
layout->addWidget(mainSplitter); |
| 409 |
|
| 410 |
vbox->show(); |
| 411 |
setCentralWidget(vbox); |
| 412 |
|
| 413 |
// apply the user settings |
| 414 |
setupSettings(); |
| 415 |
|
| 416 |
if (smoothing) { |
| 417 |
for (const auto& i : _viewer) { |
| 418 |
i->getSoRenderManager()->getGLRenderAction()->setSmoothing(true); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static_cast<SoGroup*>(getViewer(0)->getSoRenderManager()->getSceneGraph()) |
| 423 |
->addChild(setupHeadUpDisplay(tr("Movable object"))); |
| 424 |
static_cast<SoGroup*>(getViewer(1)->getSoRenderManager()->getSceneGraph()) |
| 425 |
->addChild(setupHeadUpDisplay(tr("Fixed object"))); |
| 426 |
} |
| 427 |
~AlignmentView() override = default; |
| 428 |
PyObject* getPyObject() override |
| 429 |
{ |
| 430 |
Py_Return; |
| 431 |
} |
| 432 |
bool canClose() override |
| 433 |
{ |
| 434 |
return false; |
| 435 |
} |
| 436 |
SoNode* setupHeadUpDisplay(const QString& text) const |
| 437 |
{ |
| 438 |
auto hudRoot = new SoSeparator; |
| 439 |
hudRoot->ref(); |
| 440 |
|
| 441 |
auto hudCam = new SoOrthographicCamera(); |
| 442 |
hudCam->viewportMapping = SoCamera::LEAVE_ALONE; |
| 443 |
|
| 444 |
// Set the position in the window. |
| 445 |
// [0, 0] is in the center of the screen. |
| 446 |
// |
| 447 |
auto hudTrans = new SoTranslation; |
| 448 |
hudTrans->translation.setValue(-0.95f, -0.95f, 0.0f); |
| 449 |
|
| 450 |
QFont font = this->font(); |
| 451 |
font.setPointSize(24); |
| 452 |
QFontMetrics fm(font); |
| 453 |
|
| 454 |
QColor front; |
| 455 |
front.setRgbF(0.8f, 0.8f, 0.8f); |
| 456 |
|
| 457 |
int w = QtTools::horizontalAdvance(fm, text); |
| 458 |
int h = fm.height(); |
| 459 |
|
| 460 |
QImage image(w, h, QImage::Format_ARGB32_Premultiplied); |
| 461 |
image.fill(0x00000000); |
| 462 |
QPainter painter(&image); |
| 463 |
painter.setRenderHint(QPainter::Antialiasing); |
| 464 |
painter.setPen(front); |
| 465 |
painter.setFont(font); |
| 466 |
painter.drawText(0, 0, w, h, Qt::AlignLeft, text); |
| 467 |
painter.end(); |
| 468 |
SoSFImage sfimage; |
| 469 |
Gui::BitmapFactory().convert(image, sfimage); |
| 470 |
auto hudImage = new SoImage(); |
| 471 |
hudImage->image = sfimage; |
| 472 |
|
| 473 |
// Assemble the parts... |
| 474 |
// |
| 475 |
hudRoot->addChild(hudCam); |
| 476 |
hudRoot->addChild(hudTrans); |
| 477 |
hudRoot->addChild(hudImage); |
| 478 |
|
| 479 |
return hudRoot; |
| 480 |
} |
| 481 |
}; |
| 482 |
} // namespace Gui |
| 483 |
|
| 484 |
class ManualAlignment::Private |
| 485 |
{ |
| 486 |
public: |
| 487 |
SoSeparator* picksepLeft; |
| 488 |
SoSeparator* picksepRight; |
| 489 |
SoNodeSensor* sensorCam1 {nullptr}; |
| 490 |
SoNodeSensor* sensorCam2 {nullptr}; |
| 491 |
SbRotation rot_cam1, rot_cam2; |
| 492 |
SbVec3f pos_cam1, pos_cam2; |
| 493 |
|
| 494 |
Private() |
| 495 |
{ |
| 496 |
// left view |
| 497 |
picksepLeft = new SoSeparator; |
| 498 |
picksepLeft->ref(); |
| 499 |
// right view |
| 500 |
picksepRight = new SoSeparator; |
| 501 |
picksepRight->ref(); |
| 502 |
} |
| 503 |
~Private() |
| 504 |
{ |
| 505 |
picksepLeft->unref(); |
| 506 |
picksepRight->unref(); |
| 507 |
delete sensorCam1; |
| 508 |
delete sensorCam2; |
| 509 |
} |
| 510 |
|
| 511 |
static void reorientCamera(SoCamera* cam, const SbRotation& rot) |
| 512 |
{ |
| 513 |
if (!cam) { |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
// Find global coordinates of focal point. |
| 518 |
SbVec3f direction; |
| 519 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 520 |
SbVec3f focalpoint = cam->position.getValue() + cam->focalDistance.getValue() * direction; |
| 521 |
|
| 522 |
// Set new orientation value by accumulating the new rotation. |
| 523 |
cam->orientation = rot * cam->orientation.getValue(); |
| 524 |
|
| 525 |
// Reposition camera so we are still pointing at the same old focal point. |
| 526 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 527 |
cam->position = focalpoint - cam->focalDistance.getValue() * direction; |
| 528 |
} |
| 529 |
|
| 530 |
static void copyCameraSettings( |
| 531 |
SoCamera* cam1, |
| 532 |
SbRotation& rot_cam1, |
| 533 |
SbVec3f& pos_cam1, |
| 534 |
SoCamera* cam2, |
| 535 |
SbRotation& rot_cam2, |
| 536 |
SbVec3f& pos_cam2 |
| 537 |
) |
| 538 |
{ |
| 539 |
Q_UNUSED(pos_cam2); |
| 540 |
|
| 541 |
// recompute the diff we have applied to the camera's orientation |
| 542 |
SbRotation rot = cam1->orientation.getValue(); |
| 543 |
SbRotation dif = rot * rot_cam1.inverse(); |
| 544 |
rot_cam1 = rot; |
| 545 |
|
| 546 |
// copy the values |
| 547 |
cam2->enableNotify(false); |
| 548 |
cam2->nearDistance = cam1->nearDistance; |
| 549 |
cam2->farDistance = cam1->farDistance; |
| 550 |
cam2->focalDistance = cam1->focalDistance; |
| 551 |
reorientCamera(cam2, dif); |
| 552 |
rot_cam2 = cam2->orientation.getValue(); |
| 553 |
|
| 554 |
// reverse engineer the translation part in wc |
| 555 |
SbVec3f pos = cam1->position.getValue(); |
| 556 |
SbVec3f difpos = pos - pos_cam1; |
| 557 |
pos_cam1 = pos; |
| 558 |
// the translation in pixel coords |
| 559 |
cam1->orientation.getValue().inverse().multVec(difpos, difpos); |
| 560 |
// the translation again in wc for the second camera |
| 561 |
cam2->orientation.getValue().multVec(difpos, difpos); |
| 562 |
cam2->position.setValue(cam2->position.getValue() + difpos); |
| 563 |
|
| 564 |
if (cam1->getTypeId() == cam2->getTypeId()) { |
| 565 |
if (cam1->getTypeId() == SoOrthographicCamera::getClassTypeId()) { |
| 566 |
static_cast<SoOrthographicCamera*>(cam2)->height |
| 567 |
= static_cast<SoOrthographicCamera*>(cam1)->height; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
cam2->enableNotify(true); |
| 572 |
} |
| 573 |
static void syncCameraCB(void* data, SoSensor* s) |
| 574 |
{ |
| 575 |
auto self = static_cast<ManualAlignment*>(data); |
| 576 |
if (!self->myViewer) { |
| 577 |
return; // already destroyed |
| 578 |
} |
| 579 |
SoCamera* cam1 = self->myViewer->getViewer(0)->getSoRenderManager()->getCamera(); |
| 580 |
SoCamera* cam2 = self->myViewer->getViewer(1)->getSoRenderManager()->getCamera(); |
| 581 |
if (!cam1 || !cam2) { |
| 582 |
return; // missing camera |
| 583 |
} |
| 584 |
auto sensor = static_cast<SoNodeSensor*>(s); |
| 585 |
SoNode* node = sensor->getAttachedNode(); |
| 586 |
if (node && node->getTypeId().isDerivedFrom(SoCamera::getClassTypeId())) { |
| 587 |
if (node == cam1) { |
| 588 |
Private::copyCameraSettings( |
| 589 |
cam1, |
| 590 |
self->d->rot_cam1, |
| 591 |
self->d->pos_cam1, |
| 592 |
cam2, |
| 593 |
self->d->rot_cam2, |
| 594 |
self->d->pos_cam2 |
| 595 |
); |
| 596 |
self->myViewer->getViewer(1)->redraw(); |
| 597 |
} |
| 598 |
else if (node == cam2) { |
| 599 |
Private::copyCameraSettings( |
| 600 |
cam2, |
| 601 |
self->d->rot_cam2, |
| 602 |
self->d->pos_cam2, |
| 603 |
cam1, |
| 604 |
self->d->rot_cam1, |
| 605 |
self->d->pos_cam1 |
| 606 |
); |
| 607 |
self->myViewer->getViewer(0)->redraw(); |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
static Base::Placement transformation2x2( |
| 613 |
const Base::Vector3d& plane1_base, |
| 614 |
const Base::Vector3d& plane1_xaxis, |
| 615 |
const Base::Vector3d& plane2_base, |
| 616 |
const Base::Vector3d& plane2_xaxis |
| 617 |
) |
| 618 |
{ |
| 619 |
// the transformation is: |
| 620 |
// * move from plane1_base to plane2_base |
| 621 |
// * rotate from plane1_zaxis to plane2_zaxis around plane2_base as center point |
| 622 |
Base::Rotation rot(plane1_xaxis, plane2_xaxis); |
| 623 |
|
| 624 |
Base::Vector3d pln_base; |
| 625 |
rot.multVec(plane1_base, pln_base); |
| 626 |
Base::Vector3d dif = plane2_base - pln_base; |
| 627 |
return {dif, rot}; |
| 628 |
} |
| 629 |
|
| 630 |
static Base::Placement transformation3x3( |
| 631 |
const Base::Vector3d& plane1_base, |
| 632 |
const Base::Vector3d& plane1_zaxis, |
| 633 |
const Base::Vector3d& plane1_xaxis, |
| 634 |
const Base::Vector3d& plane2_base, |
| 635 |
const Base::Vector3d& plane2_zaxis, |
| 636 |
const Base::Vector3d& plane2_xaxis |
| 637 |
) |
| 638 |
{ |
| 639 |
// the transformation is: |
| 640 |
// * move from plane1_base to plane2_base |
| 641 |
// * rotate from plane1_zaxis to plane2_zaxis around plane2_base as center point |
| 642 |
Base::Rotation rot(plane1_zaxis, plane2_zaxis); |
| 643 |
|
| 644 |
// first transformation to align the plane normals and base points |
| 645 |
Base::Vector3d dif1 = plane1_base; |
| 646 |
rot.multVec(dif1, dif1); |
| 647 |
dif1 = plane2_base - dif1; |
| 648 |
Base::Placement plm1(dif1, rot); |
| 649 |
|
| 650 |
// second transformation to align the planes' x axes |
| 651 |
Base::Vector3d pln_xaxis; |
| 652 |
rot.multVec(plane1_xaxis, pln_xaxis); |
| 653 |
Base::Rotation rot2(pln_xaxis, plane2_xaxis); |
| 654 |
Base::Vector3d dif2 = plane2_base; |
| 655 |
rot2.multVec(dif2, dif2); |
| 656 |
dif2 = plane2_base - dif2; |
| 657 |
Base::Placement plm2(dif2, rot2); |
| 658 |
plm2 = plm2 * plm1; |
| 659 |
return plm2; |
| 660 |
} |
| 661 |
}; |
| 662 |
|
| 663 |
/* TRANSLATOR Gui::ManualAlignment */ |
| 664 |
|
| 665 |
ManualAlignment* ManualAlignment::_instance = nullptr; |
| 666 |
|
| 667 |
/** |
| 668 |
* Construction. |
| 669 |
*/ |
| 670 |
ManualAlignment::ManualAlignment() |
| 671 |
: myViewer(nullptr) |
| 672 |
, myDocument(nullptr) |
| 673 |
, myPickPoints(3) |
| 674 |
, d(new Private) |
| 675 |
{ |
| 676 |
// NOLINTBEGIN |
| 677 |
// connect with the application's signal for deletion of documents |
| 678 |
this->connectApplicationDeletedDocument = Gui::Application::Instance->signalDeleteDocument.connect( |
| 679 |
std::bind(&ManualAlignment::slotDeletedDocument, this, sp::_1) |
| 680 |
); |
| 681 |
// NOLINTEND |
| 682 |
|
| 683 |
// setup sensor connection |
| 684 |
d->sensorCam1 = new SoNodeSensor(Private::syncCameraCB, this); |
| 685 |
d->sensorCam2 = new SoNodeSensor(Private::syncCameraCB, this); |
| 686 |
} |
| 687 |
|
| 688 |
/** |
| 689 |
* Destruction. |
| 690 |
*/ |
| 691 |
ManualAlignment::~ManualAlignment() |
| 692 |
{ |
| 693 |
this->connectDocumentDeletedObject.disconnect(); |
| 694 |
this->connectApplicationDeletedDocument.disconnect(); |
| 695 |
closeViewer(); |
| 696 |
delete d; |
| 697 |
_instance = nullptr; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* Creates the one and only instance of this class. |
| 702 |
*/ |
| 703 |
ManualAlignment* ManualAlignment::instance() |
| 704 |
{ |
| 705 |
// not initialized? |
| 706 |
if (!_instance) { |
| 707 |
_instance = new ManualAlignment(); |
| 708 |
} |
| 709 |
return _instance; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Destructs the one and only instance of this class. |
| 714 |
*/ |
| 715 |
void ManualAlignment::destruct() |
| 716 |
{ |
| 717 |
if (_instance) { |
| 718 |
ManualAlignment* tmp = _instance; |
| 719 |
_instance = nullptr; |
| 720 |
delete tmp; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Checks whether the one instance exists. |
| 726 |
*/ |
| 727 |
bool ManualAlignment::hasInstance() |
| 728 |
{ |
| 729 |
return _instance != nullptr; |
| 730 |
} |
| 731 |
|
| 732 |
void ManualAlignment::setMinPoints(int minPoints) |
| 733 |
{ |
| 734 |
if ((minPoints > 0) && (minPoints <= 3)) { |
| 735 |
myPickPoints = minPoints; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
void ManualAlignment::setFixedGroup(const FixedGroup& fixed) |
| 740 |
{ |
| 741 |
this->myFixedGroup = fixed; |
| 742 |
this->myDocument = fixed.getDocument(); |
| 743 |
} |
| 744 |
|
| 745 |
void ManualAlignment::setModel(const MovableGroupModel& model) |
| 746 |
{ |
| 747 |
this->myAlignModel = model; |
| 748 |
} |
| 749 |
|
| 750 |
void ManualAlignment::clearAll() |
| 751 |
{ |
| 752 |
myFixedGroup.clear(); |
| 753 |
myAlignModel.clear(); |
| 754 |
myDocument = nullptr; |
| 755 |
} |
| 756 |
|
| 757 |
void ManualAlignment::setViewingDirections( |
| 758 |
const Base::Vector3d& view1, |
| 759 |
const Base::Vector3d& up1, |
| 760 |
const Base::Vector3d& view2, |
| 761 |
const Base::Vector3d& up2 |
| 762 |
) |
| 763 |
{ |
| 764 |
if (myViewer.isNull()) { |
| 765 |
return; |
| 766 |
} |
| 767 |
|
| 768 |
{ |
| 769 |
SbVec3f vz(-view1.x, -view1.y, -view1.z); |
| 770 |
vz.normalize(); |
| 771 |
SbVec3f vy(up1.x, up1.y, up1.z); |
| 772 |
vy.normalize(); |
| 773 |
SbVec3f vx = vy.cross(vz); |
| 774 |
vy = vz.cross(vx); |
| 775 |
|
| 776 |
SbMatrix rot = SbMatrix::identity(); |
| 777 |
rot[0][0] = vx[0]; |
| 778 |
rot[0][1] = vx[1]; |
| 779 |
rot[0][2] = vx[2]; |
| 780 |
|
| 781 |
rot[1][0] = vy[0]; |
| 782 |
rot[1][1] = vy[1]; |
| 783 |
rot[1][2] = vy[2]; |
| 784 |
|
| 785 |
rot[2][0] = vz[0]; |
| 786 |
rot[2][1] = vz[1]; |
| 787 |
rot[2][2] = vz[2]; |
| 788 |
|
| 789 |
SbRotation total(rot); |
| 790 |
myViewer->getViewer(0)->getSoRenderManager()->getCamera()->orientation.setValue(total); |
| 791 |
myViewer->getViewer(0)->viewAll(); |
| 792 |
} |
| 793 |
|
| 794 |
{ |
| 795 |
SbVec3f vz(-view2.x, -view2.y, -view2.z); |
| 796 |
vz.normalize(); |
| 797 |
SbVec3f vy(up2.x, up2.y, up2.z); |
| 798 |
vy.normalize(); |
| 799 |
SbVec3f vx = vy.cross(vz); |
| 800 |
vy = vz.cross(vx); |
| 801 |
|
| 802 |
SbMatrix rot = SbMatrix::identity(); |
| 803 |
rot[0][0] = vx[0]; |
| 804 |
rot[0][1] = vx[1]; |
| 805 |
rot[0][2] = vx[2]; |
| 806 |
|
| 807 |
rot[1][0] = vy[0]; |
| 808 |
rot[1][1] = vy[1]; |
| 809 |
rot[1][2] = vy[2]; |
| 810 |
|
| 811 |
rot[2][0] = vz[0]; |
| 812 |
rot[2][1] = vz[1]; |
| 813 |
rot[2][2] = vz[2]; |
| 814 |
|
| 815 |
SbRotation total(rot); |
| 816 |
myViewer->getViewer(1)->getSoRenderManager()->getCamera()->orientation.setValue(total); |
| 817 |
myViewer->getViewer(1)->viewAll(); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Performs the alignment for the specified aligned and non-aligned views specified by setModel() |
| 823 |
* and setFixedGroup(). |
| 824 |
*/ |
| 825 |
void ManualAlignment::startAlignment(Base::Type mousemodel) |
| 826 |
{ |
| 827 |
// allow only one alignment at a time |
| 828 |
if (!myViewer.isNull()) { |
| 829 |
QMessageBox::warning( |
| 830 |
qApp->activeWindow(), |
| 831 |
tr("Manual alignment"), |
| 832 |
tr("The alignment is already in progress.") |
| 833 |
); |
| 834 |
return; |
| 835 |
} |
| 836 |
|
| 837 |
myTransform = Base::Placement(); |
| 838 |
|
| 839 |
if (myFixedGroup.isEmpty()) { |
| 840 |
return; |
| 841 |
} |
| 842 |
if (myAlignModel.isEmpty()) { |
| 843 |
return; |
| 844 |
} |
| 845 |
|
| 846 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 847 |
// save the current window state before opening the alignment viewer |
| 848 |
previousWindowState = Qt::WindowNoState; |
| 849 |
if (auto* activeDoc = Gui::Application::Instance->activeDocument()) { |
| 850 |
if (auto* activeView = activeDoc->getActiveView()) { |
| 851 |
if (auto* subWindow = qobject_cast<QMdiSubWindow*>(activeView->parentWidget())) { |
| 852 |
previousWindowState = subWindow->windowState(); |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
#endif |
| 857 |
|
| 858 |
// create a split window for picking the points |
| 859 |
myViewer = new AlignmentView(myDocument, Gui::getMainWindow()); |
| 860 |
myViewer->setWindowTitle(tr("Alignment[*]")); |
| 861 |
myViewer->setWindowIcon(QApplication::windowIcon()); |
| 862 |
myViewer->resize(400, 300); |
| 863 |
Gui::getMainWindow()->addWindow(myViewer); |
| 864 |
myViewer->showMaximized(); |
| 865 |
int n = this->myPickPoints; |
| 866 |
QString msg = n == 1 ? tr("Select at least 1 point in the left and the right view") |
| 867 |
: tr("Select at least %1 points in the left and the right view").arg(n); |
| 868 |
myViewer->myLabel->setText(msg); |
| 869 |
|
| 870 |
connect(myViewer, &QObject::destroyed, this, &ManualAlignment::reset); |
| 871 |
|
| 872 |
// show all aligned views in the 2nd view |
| 873 |
myFixedGroup.addToViewer(myViewer->getViewer(1)); |
| 874 |
myFixedGroup.setAlignable(true); |
| 875 |
|
| 876 |
// set picked points root |
| 877 |
SoNode* node1 = myViewer->getViewer(0)->getSceneGraph(); |
| 878 |
if (node1->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) { |
| 879 |
((SoGroup*)node1)->addChild(d->picksepLeft); |
| 880 |
} |
| 881 |
SoNode* node2 = myViewer->getViewer(1)->getSceneGraph(); |
| 882 |
if (node2->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) { |
| 883 |
((SoGroup*)node2)->addChild(d->picksepRight); |
| 884 |
} |
| 885 |
|
| 886 |
myViewer->getViewer(0)->setEditing(true); |
| 887 |
myViewer->getViewer(0)->addEventCallback( |
| 888 |
SoMouseButtonEvent::getClassTypeId(), |
| 889 |
ManualAlignment::probePickedCallback |
| 890 |
); |
| 891 |
myViewer->getViewer(1)->setEditing(true); |
| 892 |
myViewer->getViewer(1)->addEventCallback( |
| 893 |
SoMouseButtonEvent::getClassTypeId(), |
| 894 |
ManualAlignment::probePickedCallback |
| 895 |
); |
| 896 |
// apply the mouse model |
| 897 |
myViewer->getViewer(0)->setNavigationType(mousemodel); |
| 898 |
myViewer->getViewer(1)->setNavigationType(mousemodel); |
| 899 |
|
| 900 |
// Connect to the document's signal as we want to be notified when something happens |
| 901 |
if (this->connectDocumentDeletedObject.connected()) { |
| 902 |
this->connectDocumentDeletedObject.disconnect(); |
| 903 |
} |
| 904 |
// NOLINTBEGIN |
| 905 |
this->connectDocumentDeletedObject = myDocument->signalDeletedObject.connect( |
| 906 |
std::bind(&ManualAlignment::slotDeletedObject, this, sp::_1) |
| 907 |
); |
| 908 |
// NOLINTEND |
| 909 |
|
| 910 |
continueAlignment(); |
| 911 |
} |
| 912 |
|
| 913 |
/** |
| 914 |
* If still one view needs to be aligned then it is shown in the first window. If all views are |
| 915 |
* aligned the process will be terminated. |
| 916 |
*/ |
| 917 |
void ManualAlignment::continueAlignment() |
| 918 |
{ |
| 919 |
myFixedGroup.clearPoints(); |
| 920 |
coinRemoveAllChildren(d->picksepLeft); |
| 921 |
coinRemoveAllChildren(d->picksepRight); |
| 922 |
|
| 923 |
if (!myAlignModel.isEmpty()) { |
| 924 |
AlignmentGroup& grp = myAlignModel.activeGroup(); |
| 925 |
grp.clearPoints(); |
| 926 |
grp.addToViewer(myViewer->getViewer(0)); |
| 927 |
grp.setAlignable(true); |
| 928 |
|
| 929 |
Gui::getMainWindow()->showMessage(tr("Select points in the left and right view")); |
| 930 |
|
| 931 |
myViewer->getViewer(0)->setEditingCursor(QCursor(Qt::PointingHandCursor)); |
| 932 |
myViewer->getViewer(1)->setEditingCursor(QCursor(Qt::PointingHandCursor)); |
| 933 |
} |
| 934 |
else { |
| 935 |
finish(); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
void ManualAlignment::closeViewer() |
| 940 |
{ |
| 941 |
if (!myViewer) { |
| 942 |
return; |
| 943 |
} |
| 944 |
// Close the viewer |
| 945 |
if (myViewer->parentWidget()) { |
| 946 |
myViewer->parentWidget()->deleteLater(); |
| 947 |
} |
| 948 |
myViewer = nullptr; |
| 949 |
|
| 950 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 951 |
QTimer::singleShot(0, [this]() { |
| 952 |
auto* activeDoc = Gui::Application::Instance->activeDocument(); |
| 953 |
if (!activeDoc) { |
| 954 |
return; |
| 955 |
} |
| 956 |
|
| 957 |
auto* activeView = activeDoc->getActiveView(); |
| 958 |
if (!activeView) { |
| 959 |
return; |
| 960 |
} |
| 961 |
|
| 962 |
Gui::getMainWindow()->setActiveWindow(activeView); |
| 963 |
if (auto* subWindow = qobject_cast<QMdiSubWindow*>(activeView->parentWidget())) { |
| 964 |
// restore the previous window state |
| 965 |
if (previousWindowState & Qt::WindowMaximized) { |
| 966 |
subWindow->showMaximized(); |
| 967 |
} |
| 968 |
else if (previousWindowState & Qt::WindowMinimized) { |
| 969 |
subWindow->showMinimized(); |
| 970 |
} |
| 971 |
else { |
| 972 |
subWindow->showNormal(); |
| 973 |
} |
| 974 |
} |
| 975 |
}); |
| 976 |
#endif |
| 977 |
} |
| 978 |
|
| 979 |
/** |
| 980 |
* Make all views unpickable and resets internal data. |
| 981 |
*/ |
| 982 |
void ManualAlignment::reset() |
| 983 |
{ |
| 984 |
if (!myAlignModel.isEmpty()) { |
| 985 |
myAlignModel.activeGroup().setAlignable(false); |
| 986 |
myAlignModel.activeGroup().clear(); |
| 987 |
myAlignModel.clear(); |
| 988 |
} |
| 989 |
|
| 990 |
myFixedGroup.setAlignable(false); |
| 991 |
myFixedGroup.clear(); |
| 992 |
|
| 993 |
coinRemoveAllChildren(d->picksepLeft); |
| 994 |
coinRemoveAllChildren(d->picksepRight); |
| 995 |
|
| 996 |
if (myDocument) { |
| 997 |
this->connectDocumentDeletedObject.disconnect(); |
| 998 |
myDocument = nullptr; |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
/** |
| 1003 |
* Terminates the process and closes the windows. |
| 1004 |
*/ |
| 1005 |
void ManualAlignment::finish() |
| 1006 |
{ |
| 1007 |
if (myViewer.isNull()) { |
| 1008 |
return; |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (myDocument) { |
| 1012 |
myDocument->getDocument()->recompute(); |
| 1013 |
} |
| 1014 |
closeViewer(); |
| 1015 |
reset(); |
| 1016 |
|
| 1017 |
Gui::getMainWindow()->showMessage(tr("The alignment has finished")); |
| 1018 |
|
| 1019 |
// If an event receiver has been defined send the manual alignment finished event to it |
| 1020 |
Q_EMIT emitFinished(); |
| 1021 |
} |
| 1022 |
|
| 1023 |
/** |
| 1024 |
* Cancels the process and closes the windows without performing an alignment. |
| 1025 |
*/ |
| 1026 |
void ManualAlignment::cancel() |
| 1027 |
{ |
| 1028 |
if (myViewer.isNull()) { |
| 1029 |
return; |
| 1030 |
} |
| 1031 |
|
| 1032 |
closeViewer(); |
| 1033 |
myTransform = Base::Placement(); |
| 1034 |
reset(); |
| 1035 |
|
| 1036 |
Gui::getMainWindow()->showMessage(tr("The alignment has been canceled")); |
| 1037 |
|
| 1038 |
// If an event receiver has been defined send the manual alignment cancelled event to it |
| 1039 |
Q_EMIT emitCanceled(); |
| 1040 |
} |
| 1041 |
|
| 1042 |
void ManualAlignment::align() |
| 1043 |
{ |
| 1044 |
// Now we can start the actual alignment |
| 1045 |
if (myAlignModel.activeGroup().countPoints() < myPickPoints) { |
| 1046 |
QMessageBox::warning( |
| 1047 |
myViewer, |
| 1048 |
tr("Manual alignment"), |
| 1049 |
tr("Too few points picked in the left view." |
| 1050 |
" At least %1 points are needed.") |
| 1051 |
.arg(myPickPoints) |
| 1052 |
); |
| 1053 |
} |
| 1054 |
else if (myFixedGroup.countPoints() < myPickPoints) { |
| 1055 |
QMessageBox::warning( |
| 1056 |
myViewer, |
| 1057 |
tr("Manual alignment"), |
| 1058 |
tr("Too few points picked in the right view." |
| 1059 |
" At least %1 points are needed.") |
| 1060 |
.arg(myPickPoints) |
| 1061 |
); |
| 1062 |
} |
| 1063 |
else if (myAlignModel.activeGroup().countPoints() != myFixedGroup.countPoints()) { |
| 1064 |
QMessageBox::warning( |
| 1065 |
myViewer, |
| 1066 |
tr("Manual alignment"), |
| 1067 |
tr("Different number of points picked in left and right view.\n" |
| 1068 |
"On the left view %1 points are picked,\n" |
| 1069 |
"on the right view %2 points are picked.") |
| 1070 |
.arg(myAlignModel.activeGroup().countPoints()) |
| 1071 |
.arg(myFixedGroup.countPoints()) |
| 1072 |
); |
| 1073 |
} |
| 1074 |
else { |
| 1075 |
// do not allow one to pick further points |
| 1076 |
myAlignModel.activeGroup().removeFromViewer(myViewer->getViewer(0)); |
| 1077 |
myAlignModel.activeGroup().setAlignable(false); |
| 1078 |
std::vector<App::DocumentObject*> pViews = myAlignModel.activeGroup().getViews(); |
| 1079 |
Gui::getMainWindow()->showMessage(tr("Try to align group of views")); |
| 1080 |
|
| 1081 |
// Compute alignment |
| 1082 |
bool ok = computeAlignment(myAlignModel.activeGroup().getPoints(), myFixedGroup.getPoints()); |
| 1083 |
if (ok && myDocument) { |
| 1084 |
// Align views |
| 1085 |
myDocument->openCommand(QT_TRANSLATE_NOOP("Command", "Align")); |
| 1086 |
for (const auto& pView : pViews) { |
| 1087 |
alignObject(pView); |
| 1088 |
} |
| 1089 |
myDocument->commitCommand(); |
| 1090 |
|
| 1091 |
// the alignment was successful so show it in the right view now |
| 1092 |
// myAlignModel.activeGroup().setRandomColor(); |
| 1093 |
myAlignModel.activeGroup().setAlignable(true); |
| 1094 |
myAlignModel.activeGroup().addToViewer(myViewer->getViewer(1)); |
| 1095 |
myAlignModel.activeGroup().moveTo(myFixedGroup); |
| 1096 |
myAlignModel.continueAlignment(); |
| 1097 |
} |
| 1098 |
else { |
| 1099 |
// Inform user that alignment failed |
| 1100 |
auto ret = QMessageBox::critical( |
| 1101 |
myViewer, |
| 1102 |
tr("Manual alignment"), |
| 1103 |
tr("The alignment failed.\nHow do you want to proceed?"), |
| 1104 |
QMessageBox::Retry | QMessageBox::Ignore | QMessageBox::Abort |
| 1105 |
); |
| 1106 |
if (ret == QMessageBox::Ignore) { |
| 1107 |
myAlignModel.continueAlignment(); |
| 1108 |
} |
| 1109 |
else if (ret == QMessageBox::Abort) { |
| 1110 |
finish(); |
| 1111 |
return; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
continueAlignment(); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
void ManualAlignment::showInstructions() |
| 1120 |
{ |
| 1121 |
// Now we can start the actual alignment |
| 1122 |
if (myAlignModel.activeGroup().countPoints() < myPickPoints) { |
| 1123 |
Gui::getMainWindow()->showMessage(tr("Too few points picked in the left view." |
| 1124 |
" At least %1 points are needed.") |
| 1125 |
.arg(myPickPoints)); |
| 1126 |
} |
| 1127 |
else if (myFixedGroup.countPoints() < myPickPoints) { |
| 1128 |
Gui::getMainWindow()->showMessage(tr("Too few points picked in the right view." |
| 1129 |
" At least %1 points are needed.") |
| 1130 |
.arg(myPickPoints)); |
| 1131 |
} |
| 1132 |
else if (myAlignModel.activeGroup().countPoints() != myFixedGroup.countPoints()) { |
| 1133 |
Gui::getMainWindow()->showMessage( |
| 1134 |
tr("Different number of points picked in left and right view. " |
| 1135 |
"On the left view %1 points are picked, " |
| 1136 |
"on the right view %2 points are picked.") |
| 1137 |
.arg(myAlignModel.activeGroup().countPoints()) |
| 1138 |
.arg(myFixedGroup.countPoints()) |
| 1139 |
); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
bool ManualAlignment::canAlign() const |
| 1144 |
{ |
| 1145 |
if (myAlignModel.activeGroup().countPoints() == myFixedGroup.countPoints()) { |
| 1146 |
if (myFixedGroup.countPoints() >= myPickPoints) { |
| 1147 |
return true; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
return false; |
| 1152 |
} |
| 1153 |
|
| 1154 |
/** |
| 1155 |
* This method computes the alignment. For the calculation of the alignment the picked points of |
| 1156 |
* both views are taken. If the alignment fails false is returned, true otherwise. |
| 1157 |
*/ |
| 1158 |
bool ManualAlignment::computeAlignment( |
| 1159 |
const std::vector<PickedPoint>& movPts, |
| 1160 |
const std::vector<PickedPoint>& fixPts |
| 1161 |
) |
| 1162 |
{ |
| 1163 |
assert((int)movPts.size() >= myPickPoints); |
| 1164 |
assert((int)fixPts.size() >= myPickPoints); |
| 1165 |
assert((int)movPts.size() == (int)fixPts.size()); |
| 1166 |
myTransform = Base::Placement(); |
| 1167 |
|
| 1168 |
if (movPts.size() == 1) { |
| 1169 |
// 1 point partial solution: Simple translation only |
| 1170 |
myTransform.setPosition(fixPts[0].point - movPts[0].point); |
| 1171 |
} |
| 1172 |
else if (movPts.size() == 2) { |
| 1173 |
const Base::Vector3d& p1 = movPts[0].point; |
| 1174 |
const Base::Vector3d& p2 = movPts[1].point; |
| 1175 |
Base::Vector3d d1 = p2 - p1; |
| 1176 |
d1.Normalize(); |
| 1177 |
|
| 1178 |
const Base::Vector3d& q1 = fixPts[0].point; |
| 1179 |
const Base::Vector3d& q2 = fixPts[1].point; |
| 1180 |
Base::Vector3d d2 = q2 - q1; |
| 1181 |
d2.Normalize(); |
| 1182 |
|
| 1183 |
myTransform = Private::transformation2x2(p1, d1, q1, d2); |
| 1184 |
} |
| 1185 |
else if (movPts.size() >= 3) { |
| 1186 |
const Base::Vector3d& p1 = movPts[0].point; |
| 1187 |
const Base::Vector3d& p2 = movPts[1].point; |
| 1188 |
const Base::Vector3d& p3 = movPts[2].point; |
| 1189 |
Base::Vector3d d1 = p2 - p1; |
| 1190 |
d1.Normalize(); |
| 1191 |
Base::Vector3d n1 = (p2 - p1) % (p3 - p1); |
| 1192 |
n1.Normalize(); |
| 1193 |
|
| 1194 |
const Base::Vector3d& q1 = fixPts[0].point; |
| 1195 |
const Base::Vector3d& q2 = fixPts[1].point; |
| 1196 |
const Base::Vector3d& q3 = fixPts[2].point; |
| 1197 |
Base::Vector3d d2 = q2 - q1; |
| 1198 |
d2.Normalize(); |
| 1199 |
Base::Vector3d n2 = (q2 - q1) % (q3 - q1); |
| 1200 |
n2.Normalize(); |
| 1201 |
|
| 1202 |
myTransform = Private::transformation3x3(p1, d1, n1, q1, d2, n2); |
| 1203 |
} |
| 1204 |
|
| 1205 |
return true; |
| 1206 |
} |
| 1207 |
|
| 1208 |
/** |
| 1209 |
* This method performs the actual alignment of view \a pView. |
| 1210 |
*/ |
| 1211 |
void ManualAlignment::alignObject(App::DocumentObject* obj) |
| 1212 |
{ |
| 1213 |
if (obj->isDerivedFrom<App::GeoFeature>()) { |
| 1214 |
auto geom = static_cast<App::GeoFeature*>(obj); |
| 1215 |
geom->transformPlacement(this->myTransform); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
/** |
| 1220 |
* Creates a point element as visible feedback for the user. |
| 1221 |
*/ |
| 1222 |
SoNode* ManualAlignment::pickedPointsSubGraph(const SbVec3f& p, const SbVec3f& n, int id) |
| 1223 |
{ |
| 1224 |
static const float color_table[10][3] = { |
| 1225 |
{1.0f, 0.0f, 0.0f}, // red |
| 1226 |
{0.0f, 1.0f, 0.0f}, // green |
| 1227 |
{0.0f, 0.0f, 1.0f}, // blue |
| 1228 |
{1.0f, 1.0f, 0.0f}, // yellow |
| 1229 |
{0.0f, 1.0f, 1.0f}, // cyan |
| 1230 |
{0.7f, 0.0f, 0.0f}, |
| 1231 |
{0.0f, 0.7f, 0.0f}, |
| 1232 |
{0.7f, 0.7f, 0.0f}, |
| 1233 |
{0.7f, 0.0f, 0.5f}, |
| 1234 |
{1.0f, 0.7f, 0.0f} |
| 1235 |
}; |
| 1236 |
|
| 1237 |
int index = (id - 1) % 10; |
| 1238 |
|
| 1239 |
auto probe = new SoRegPoint(); |
| 1240 |
probe->base.setValue(p); |
| 1241 |
probe->normal.setValue(n); |
| 1242 |
probe->color.setValue(color_table[index][0], color_table[index][1], color_table[index][2]); |
| 1243 |
SbString s(tr("Point_%1").arg(id).toStdString().c_str()); |
| 1244 |
probe->text.setValue(s); |
| 1245 |
return probe; |
| 1246 |
} |
| 1247 |
|
| 1248 |
/** |
| 1249 |
* Handle if the current document is about to being closed. |
| 1250 |
*/ |
| 1251 |
void ManualAlignment::slotDeletedDocument(const Gui::Document& Doc) |
| 1252 |
{ |
| 1253 |
if (&Doc == this->myDocument) { |
| 1254 |
reset(); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
/** |
| 1259 |
* Handle if the a view provider is about to being destroyed. |
| 1260 |
*/ |
| 1261 |
void ManualAlignment::slotDeletedObject(const Gui::ViewProvider& Obj) |
| 1262 |
{ |
| 1263 |
// remove the view provider either from the left or the right view |
| 1264 |
if (Obj.isDerivedFrom<Gui::ViewProviderDocumentObject>()) { |
| 1265 |
// remove the view provider immediately from the split window |
| 1266 |
bool found = false; |
| 1267 |
auto vp = const_cast<Gui::ViewProviderDocumentObject*>( |
| 1268 |
static_cast<const Gui::ViewProviderDocumentObject*>(&Obj) |
| 1269 |
); |
| 1270 |
if (myAlignModel.activeGroup().hasView(vp)) { |
| 1271 |
myViewer->getViewer(0)->removeViewProvider(vp); |
| 1272 |
found = true; |
| 1273 |
} |
| 1274 |
if (myFixedGroup.hasView(vp)) { |
| 1275 |
myViewer->getViewer(1)->removeViewProvider(vp); |
| 1276 |
found = true; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (found) { |
| 1280 |
cancel(); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
void ManualAlignment::onAlign() |
| 1286 |
{ |
| 1287 |
align(); |
| 1288 |
} |
| 1289 |
|
| 1290 |
void ManualAlignment::onRemoveLastPointMoveable() |
| 1291 |
{ |
| 1292 |
int nPoints = myAlignModel.activeGroup().countPoints(); |
| 1293 |
if (nPoints > 0) { |
| 1294 |
myAlignModel.activeGroup().removeLastPoint(); |
| 1295 |
d->picksepLeft->removeChild(nPoints - 1); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
void ManualAlignment::onRemoveLastPointFixed() |
| 1300 |
{ |
| 1301 |
int nPoints = myFixedGroup.countPoints(); |
| 1302 |
if (nPoints > 0) { |
| 1303 |
myFixedGroup.removeLastPoint(); |
| 1304 |
d->picksepRight->removeChild(nPoints - 1); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
void ManualAlignment::onClear() |
| 1309 |
{ |
| 1310 |
myAlignModel.activeGroup().clear(); |
| 1311 |
myFixedGroup.clear(); |
| 1312 |
|
| 1313 |
coinRemoveAllChildren(d->picksepLeft); |
| 1314 |
coinRemoveAllChildren(d->picksepRight); |
| 1315 |
} |
| 1316 |
|
| 1317 |
void ManualAlignment::onCancel() |
| 1318 |
{ |
| 1319 |
cancel(); |
| 1320 |
} |
| 1321 |
|
| 1322 |
void ManualAlignment::probePickedCallback(void* ud, SoEventCallback* n) |
| 1323 |
{ |
| 1324 |
Q_UNUSED(ud); |
| 1325 |
|
| 1326 |
auto view = static_cast<Gui::View3DInventorViewer*>(n->getUserData()); |
| 1327 |
const SoEvent* ev = n->getEvent(); |
| 1328 |
if (ev->getTypeId() == SoMouseButtonEvent::getClassTypeId()) { |
| 1329 |
// set as handled |
| 1330 |
n->getAction()->setHandled(); |
| 1331 |
n->setHandled(); |
| 1332 |
|
| 1333 |
auto mbe = static_cast<const SoMouseButtonEvent*>(ev); |
| 1334 |
if (mbe->getButton() == SoMouseButtonEvent::BUTTON1 |
| 1335 |
&& mbe->getState() == SoButtonEvent::DOWN) { |
| 1336 |
// if we are in 'align' mode then handle the click event |
| 1337 |
ManualAlignment* self = ManualAlignment::instance(); |
| 1338 |
// Get the closest point to the camera of the whole scene. |
| 1339 |
// This point doesn't need to be part of this view provider. |
| 1340 |
Gui::WaitCursor wc; |
| 1341 |
const SoPickedPoint* point = view->getPickedPoint(n); |
| 1342 |
if (point) { |
| 1343 |
auto vp = static_cast<Gui::ViewProvider*>( |
| 1344 |
view->getViewProviderByPath(point->getPath()) |
| 1345 |
); |
| 1346 |
if (vp && vp->isDerivedFrom<Gui::ViewProviderDocumentObject>()) { |
| 1347 |
auto that = static_cast<Gui::ViewProviderDocumentObject*>(vp); |
| 1348 |
if (self->applyPickedProbe(that, point)) { |
| 1349 |
const SbVec3f& vec = point->getPoint(); |
| 1350 |
Gui::getMainWindow()->showMessage( |
| 1351 |
tr("Point picked at (%1,%2,%3)").arg(vec[0]).arg(vec[1]).arg(vec[2]) |
| 1352 |
); |
| 1353 |
} |
| 1354 |
else { |
| 1355 |
Gui::getMainWindow()->showMessage(tr("No point was found on model")); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
} |
| 1359 |
else { |
| 1360 |
Gui::getMainWindow()->showMessage(tr("No point was picked")); |
| 1361 |
} |
| 1362 |
} |
| 1363 |
else if (mbe->getButton() == SoMouseButtonEvent::BUTTON2 |
| 1364 |
&& mbe->getState() == SoButtonEvent::UP) { |
| 1365 |
ManualAlignment* self = ManualAlignment::instance(); |
| 1366 |
if (self->myAlignModel.isEmpty() || self->myFixedGroup.isEmpty()) { |
| 1367 |
return; |
| 1368 |
} |
| 1369 |
self->showInstructions(); |
| 1370 |
int nPoints; |
| 1371 |
if (view == self->myViewer->getViewer(0)) { |
| 1372 |
nPoints = self->myAlignModel.activeGroup().countPoints(); |
| 1373 |
} |
| 1374 |
else { |
| 1375 |
nPoints = self->myFixedGroup.countPoints(); |
| 1376 |
} |
| 1377 |
QMenu menu; |
| 1378 |
QAction* fi = menu.addAction(tr("&Align")); |
| 1379 |
QAction* rem = menu.addAction(tr("&Remove Last Point")); |
| 1380 |
// QAction* cl = menu.addAction("C&lear"); |
| 1381 |
QAction* ca = menu.addAction(tr("&Cancel")); |
| 1382 |
fi->setEnabled(self->canAlign()); |
| 1383 |
rem->setEnabled(nPoints > 0); |
| 1384 |
menu.addSeparator(); |
| 1385 |
QAction* sync = menu.addAction(tr("&Synchronize Views")); |
| 1386 |
sync->setCheckable(true); |
| 1387 |
if (self->d->sensorCam1->getAttachedNode()) { |
| 1388 |
sync->setChecked(true); |
| 1389 |
} |
| 1390 |
QAction* id = menu.exec(QCursor::pos()); |
| 1391 |
if (id == fi) { |
| 1392 |
// call align->align(); |
| 1393 |
QTimer::singleShot(300, self, &ManualAlignment::onAlign); |
| 1394 |
} |
| 1395 |
else if ((id == rem) && (view == self->myViewer->getViewer(0))) { |
| 1396 |
QTimer::singleShot(300, self, &ManualAlignment::onRemoveLastPointMoveable); |
| 1397 |
} |
| 1398 |
else if ((id == rem) && (view == self->myViewer->getViewer(1))) { |
| 1399 |
QTimer::singleShot(300, self, &ManualAlignment::onRemoveLastPointFixed); |
| 1400 |
} |
| 1401 |
// else if (id == cl) { |
| 1402 |
// // call align->clear(); |
| 1403 |
// QTimer::singleShot(300, self, &ManualAlignment::onClear); |
| 1404 |
// } |
| 1405 |
else if (id == ca) { |
| 1406 |
// call align->cancel(); |
| 1407 |
QTimer::singleShot(300, self, &ManualAlignment::onCancel); |
| 1408 |
} |
| 1409 |
else if (id == sync) { |
| 1410 |
// setup sensor connection |
| 1411 |
if (sync->isChecked()) { |
| 1412 |
SoCamera* cam1 = self->myViewer->getViewer(0)->getSoRenderManager()->getCamera(); |
| 1413 |
SoCamera* cam2 = self->myViewer->getViewer(1)->getSoRenderManager()->getCamera(); |
| 1414 |
if (cam1 && cam2) { |
| 1415 |
self->d->sensorCam1->attach(cam1); |
| 1416 |
self->d->rot_cam1 = cam1->orientation.getValue(); |
| 1417 |
self->d->pos_cam1 = cam1->position.getValue(); |
| 1418 |
self->d->sensorCam2->attach(cam2); |
| 1419 |
self->d->rot_cam2 = cam2->orientation.getValue(); |
| 1420 |
self->d->pos_cam2 = cam2->position.getValue(); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
else { |
| 1424 |
self->d->sensorCam1->detach(); |
| 1425 |
self->d->sensorCam2->detach(); |
| 1426 |
} |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
/** |
| 1433 |
* This method stores the picked point \a pnt from the view provider \a prov. If enough points in |
| 1434 |
* both windows have been picked the alignment gets invoked. |
| 1435 |
*/ |
| 1436 |
bool ManualAlignment::applyPickedProbe(Gui::ViewProviderDocumentObject* prov, const SoPickedPoint* pnt) |
| 1437 |
{ |
| 1438 |
const SbVec3f& vec = pnt->getPoint(); |
| 1439 |
const SbVec3f& nor = pnt->getNormal(); |
| 1440 |
|
| 1441 |
// add to the list for the non-aligned view in the left view |
| 1442 |
if (myAlignModel.activeGroup().hasView(prov)) { |
| 1443 |
std::vector<Base::Vector3d> pts = prov->getModelPoints(pnt); |
| 1444 |
if (pts.empty()) { |
| 1445 |
return false; |
| 1446 |
} |
| 1447 |
PickedPoint pp; |
| 1448 |
pp.point = pts.front(); |
| 1449 |
myAlignModel.activeGroup().addPoint(pp); |
| 1450 |
// Adds a point marker for the picked point. |
| 1451 |
d->picksepLeft->addChild( |
| 1452 |
pickedPointsSubGraph(vec, nor, myAlignModel.activeGroup().countPoints()) |
| 1453 |
); |
| 1454 |
return true; |
| 1455 |
} |
| 1456 |
else if (myFixedGroup.hasView(prov)) { |
| 1457 |
std::vector<Base::Vector3d> pts = prov->getModelPoints(pnt); |
| 1458 |
if (pts.empty()) { |
| 1459 |
return false; |
| 1460 |
} |
| 1461 |
PickedPoint pp; |
| 1462 |
pp.point = pts.front(); |
| 1463 |
myFixedGroup.addPoint(pp); |
| 1464 |
// Adds a point marker for the picked point. |
| 1465 |
d->picksepRight->addChild(pickedPointsSubGraph(vec, nor, myFixedGroup.countPoints())); |
| 1466 |
return true; |
| 1467 |
} |
| 1468 |
|
| 1469 |
return false; |
| 1470 |
} |
| 1471 |
|
| 1472 |
#include "moc_ManualAlignment.cpp" |