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
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#include "ccGraphicalTransformationTool.h"
#include "mainwindow.h"
#include <ccGLUtils.h>
#include <ccGLWindow.h>
//qCC_db
#include <ccLog.h>
#include <ccMesh.h>
ccGraphicalTransformationTool::ccGraphicalTransformationTool(QWidget* parent)
: ccOverlayDialog(parent)
, Ui::GraphicalTransformationDlg()
, m_toTransform("transformed")
{
setupUi(this);
connect(pauseButton, &QAbstractButton::toggled, this, &ccGraphicalTransformationTool::pause);
connect(okButton, &QAbstractButton::clicked, this, &ccGraphicalTransformationTool::apply);
connect(razButton, &QAbstractButton::clicked, this, &ccGraphicalTransformationTool::reset);
connect(cancelButton, &QAbstractButton::clicked, this, &ccGraphicalTransformationTool::cancel);
//add shortcuts
addOverridenShortcut(Qt::Key_Space); //space bar for the "pause" button
addOverridenShortcut(Qt::Key_Escape); //escape key for the "cancel" button
addOverridenShortcut(Qt::Key_Return); //return key for the "ok" button
connect(this, &ccOverlayDialog::shortcutTriggered, this, &ccGraphicalTransformationTool::onShortcutTriggered);
}
ccGraphicalTransformationTool::~ccGraphicalTransformationTool()
{
clear();
}
void ccGraphicalTransformationTool::onShortcutTriggered(int key)
{
switch(key)
{
case Qt::Key_Space:
pauseButton->toggle();
return;
case Qt::Key_Return:
okButton->click();
return;
case Qt::Key_Escape:
cancelButton->click();
return;
default:
//nothing to do
break;
}
}
void ccGraphicalTransformationTool::pause(bool state)
{
if (!m_associatedWin)
return;
if (state)
{
m_associatedWin->setInteractionMode(ccGLWindow::TRANSFORM_CAMERA());
m_associatedWin->displayNewMessage("Transformation [PAUSED]",ccGLWindow::UPPER_CENTER_MESSAGE,false,3600,ccGLWindow::MANUAL_TRANSFORMATION_MESSAGE);
m_associatedWin->displayNewMessage("Unpause to transform again",ccGLWindow::UPPER_CENTER_MESSAGE,true,3600,ccGLWindow::MANUAL_TRANSFORMATION_MESSAGE);
}
else
{
m_associatedWin->setInteractionMode(ccGLWindow::TRANSFORM_ENTITIES());
m_associatedWin->displayNewMessage("[Rotation/Translation mode]",ccGLWindow::UPPER_CENTER_MESSAGE,false,3600,ccGLWindow::MANUAL_TRANSFORMATION_MESSAGE);
}
//update mini-GUI
pauseButton->blockSignals(true);
pauseButton->setChecked(state);
pauseButton->blockSignals(false);
m_associatedWin->redraw(true, false);
}
void ccGraphicalTransformationTool::clear()
{
m_toTransform.detatchAllChildren();
m_rotation.toIdentity();
m_translation = CCVector3d(0,0,0);
m_rotationCenter = CCVector3d(0,0,0);
}
bool ccGraphicalTransformationTool::addEntity(ccHObject* entity)
{
assert(entity);
if (!entity)
return false;
//we don't handle entities associated to another context
if (entity->getDisplay() != m_associatedWin)
{
ccLog::Warning(QString("[Graphical Transformation Tool] Can't use entity '%1' cause it's not displayed in the active 3D view!").arg(entity->getName()));
return false;
}
//we can't transform locked entities
if (entity->isLocked())
{
ccLog::Warning(QString("[Graphical Transformation Tool] Can't transform entity '%1' cause it's locked!").arg(entity->getName()));
return false;
}
//we can't transform child meshes
if (entity->isA(CC_TYPES::MESH) && entity->getParent() && entity->getParent()->isKindOf(CC_TYPES::MESH))
{
ccLog::Warning(QString("[Graphical Transformation Tool] Entity '%1' can't be modified as it is part of a mesh group. You should 'clone' it first.").arg(entity->getName()));
return false;
}
//eventually, we must check that there is no "parent + sibling" in the selection!
//otherwise, the sibling will be rotated twice!
unsigned n = m_toTransform.getChildrenNumber();
for (unsigned i=0; i<n; )
{
ccHObject* previous = m_toTransform.getChild(i);
if (previous->isAncestorOf(entity))
{
//we have found a parent, we won't add this entity
return false;
}
//if the inverse is true, then we get rid of the current element!
else if (entity->isAncestorOf(previous))
{
m_toTransform.detachChild(previous);
--n;
}
else
{
//proceed
++i;
}
}
m_toTransform.addChild(entity,ccHObject::DP_NONE);
return true;
}
unsigned ccGraphicalTransformationTool::getNumberOfValidEntities() const
{
return m_toTransform.getChildrenNumber();
}
bool ccGraphicalTransformationTool::linkWith(ccGLWindow* win)
{
if (!ccOverlayDialog::linkWith(win))
{
return false;
}
assert(!win || m_toTransform.getChildrenNumber() == 0);
m_toTransform.setDisplay(win);
return true;
}
bool ccGraphicalTransformationTool::start()
{
assert(!m_processing);
assert(m_associatedWin);
if (!m_associatedWin)
return false;
unsigned childNum = m_toTransform.getChildrenNumber();
if (childNum == 0)
return false;
m_rotation.toIdentity();
m_translation = CCVector3d(0,0,0);
m_rotationCenter = CCVector3d::fromArray(m_toTransform.getBB_recursive().getCenter().u); //m_rotation center == selected entities center
//activate "moving mode" in associated GL window
m_associatedWin->setInteractionMode(ccGLWindow::TRANSFORM_ENTITIES());
m_associatedWin->setPickingMode(ccGLWindow::NO_PICKING);
//the user must not close this window!
m_associatedWin->setUnclosable(true);
connect(m_associatedWin, &ccGLWindow::rotation, this, &ccGraphicalTransformationTool::glRotate);
connect(m_associatedWin, &ccGLWindow::translation, this, &ccGraphicalTransformationTool::glTranslate);
m_associatedWin->displayNewMessage(QString(),ccGLWindow::UPPER_CENTER_MESSAGE); //clear the area
m_associatedWin->displayNewMessage("[Rotation/Translation mode]",ccGLWindow::UPPER_CENTER_MESSAGE,false,3600,ccGLWindow::MANUAL_TRANSFORMATION_MESSAGE);
m_associatedWin->redraw(true, false);
return ccOverlayDialog::start();
}
void ccGraphicalTransformationTool::stop(bool state)
{
if (m_associatedWin)
{
//deactivate "moving mode" in associated GL window
m_associatedWin->setInteractionMode(ccGLWindow::TRANSFORM_CAMERA());
m_associatedWin->setPickingMode(ccGLWindow::DEFAULT_PICKING);
m_associatedWin->setUnclosable(false);
m_associatedWin->disconnect(this);
m_associatedWin->displayNewMessage("[Rotation/Translation mode OFF]",ccGLWindow::UPPER_CENTER_MESSAGE,false,2,ccGLWindow::MANUAL_TRANSFORMATION_MESSAGE);
m_associatedWin->redraw(true, false);
}
ccOverlayDialog::stop(state);
}
void ccGraphicalTransformationTool::glTranslate(const CCVector3d& realT)
{
CCVector3d t( realT.x * (TxCheckBox->isChecked() ? 1 : 0),
realT.y * (TyCheckBox->isChecked() ? 1 : 0),
realT.z * (TzCheckBox->isChecked() ? 1 : 0));
if (t.norm2() != 0)
{
m_translation += t;
updateAllGLTransformations();
}
}
void ccGraphicalTransformationTool::glRotate(const ccGLMatrixd& rotMat)
{
switch (rotComboBox->currentIndex())
{
case 0: //XYZ
m_rotation = rotMat * m_rotation;
break;
case 1: //X
m_rotation = rotMat.xRotation() * m_rotation;
break;
case 2: //Y
m_rotation = rotMat.yRotation() * m_rotation;
break;
case 3: //Z
m_rotation = rotMat.zRotation() * m_rotation;
break;
}
updateAllGLTransformations();
}
void ccGraphicalTransformationTool::reset()
{
m_rotation.toIdentity();
m_translation = CCVector3d(0, 0, 0);
updateAllGLTransformations();
}
void ccGraphicalTransformationTool::setRotationCenter(CCVector3d& center)
{
m_translation += (m_rotationCenter-center) - m_rotation*(m_rotationCenter-center);
m_rotationCenter = center;
updateAllGLTransformations();
}
void ccGraphicalTransformationTool::updateAllGLTransformations()
{
//we recompute global GL transformation matrix
ccGLMatrixd newTrans = m_rotation;
newTrans += m_rotationCenter + m_translation - m_rotation * m_rotationCenter;
ccGLMatrix newTransf(newTrans.data());
for (unsigned i = 0; i < m_toTransform.getChildrenNumber(); ++i)
{
ccHObject* child = m_toTransform.getChild(i);
child->setGLTransformation(newTransf);
child->prepareDisplayForRefresh_recursive();
}
MainWindow::RefreshAllGLWindow(false);
}
void ccGraphicalTransformationTool::apply()
{
//we recompute global GL transformation matrix and display it in console
ccGLMatrixd finalTrans = m_rotation;
finalTrans += m_rotationCenter + m_translation - m_rotation*m_rotationCenter;
ccGLMatrixd finalTransCorrected = finalTrans;
#define NORMALIZE_TRANSFORMATION_MATRIX_WITH_EULER
#ifdef NORMALIZE_TRANSFORMATION_MATRIX_WITH_EULER
{
//convert matrix back and forth so as to be sure to get a 'true' rotation matrix
//DGM: we use Euler angles, as the axis/angle method (formerly used) is not robust
//enough! Shifts could be perceived by the user.
double phi_rad,theta_rad,psi_rad;
CCVector3d t3D;
finalTrans.getParameters(phi_rad,theta_rad,psi_rad,t3D);
finalTransCorrected.initFromParameters(phi_rad,theta_rad,psi_rad,t3D);
#ifdef QT_DEBUG
ccLog::Print("[GraphicalTransformationTool] Final transformation (before correction):");
ccLog::Print(finalTrans.toString(12,' ')); //full precision
ccLog::Print(QString("Angles(%1,%2,%3) T(%5,%6,%7)").arg(phi_rad).arg(theta_rad).arg(psi_rad).arg(t3D.x).arg(t3D.y).arg(t3D.z));
#endif
}
#endif //NORMALIZE_TRANSFORMATION_MATRIX_WITH_EULER
#ifdef QT_DEBUG
//test: compute rotation "norm" (as it may not be exactly 1 due to numerical (in)accuracy!)
{
ccGLMatrixd finalRotation = finalTransCorrected;
finalRotation.setTranslation(CCVector3(0,0,0));
ccGLMatrixd finalRotationT = finalRotation.transposed();
ccGLMatrixd idTrans = finalRotation * finalRotationT;
double norm = idTrans.data()[0] * idTrans.data()[5] * idTrans.data()[10];
ccLog::PrintDebug("[GraphicalTransformationTool] T*T-1:");
ccLog::PrintDebug(idTrans.toString(12,' ')); //full precision
ccLog::PrintDebug(QString("Rotation norm = %1").arg(norm,0,'f',12));
}
#endif
//update GL transformation for all entities
ccGLMatrix correctedFinalTrans(finalTransCorrected.data());
for (unsigned i=0; i<m_toTransform.getChildrenNumber(); ++i)
{
ccHObject* toTransform = m_toTransform.getChild(i);
toTransform->setGLTransformation(correctedFinalTrans);
//DGM: warning, applyGLTransformation may delete the associated octree!
MainWindow::ccHObjectContext objContext = MainWindow::TheInstance()->removeObjectTemporarilyFromDBTree(toTransform);
toTransform->applyGLTransformation_recursive();
toTransform->prepareDisplayForRefresh_recursive();
MainWindow::TheInstance()->putObjectBackIntoDBTree(toTransform,objContext);
//special case: if the object is a mesh vertices set, we may have to update the mesh normals!
if (toTransform->isA(CC_TYPES::POINT_CLOUD) && toTransform->getParent() && toTransform->getParent()->isKindOf(CC_TYPES::MESH))
{
ccMesh* mesh = static_cast<ccMesh*>(toTransform->getParent());
if (mesh->hasTriNormals() && !m_toTransform.isAncestorOf(mesh))
{
mesh->transformTriNormals(correctedFinalTrans);
}
}
}
stop(true);
clear();
//output resulting transformation matrix
ccLog::Print("[GraphicalTransformationTool] Applied transformation:");
ccLog::Print(correctedFinalTrans.toString(12,' ')); //full precision
#ifdef QT_DEBUG
{
float phi_rad,theta_rad,psi_rad;
CCVector3f t3D;
correctedFinalTrans.getParameters(phi_rad,theta_rad,psi_rad,t3D);
ccLog::Print(QString("Angles(%1,%2,%3) T(%5,%6,%7)").arg(phi_rad).arg(theta_rad).arg(psi_rad).arg(t3D.x).arg(t3D.y).arg(t3D.z));
}
#endif
}
void ccGraphicalTransformationTool::cancel()
{
for (unsigned i=0; i<m_toTransform.getChildrenNumber(); ++i)
{
ccHObject* child = m_toTransform.getChild(i);
child->resetGLTransformation();
child->prepareDisplayForRefresh_recursive();
}
stop(false);
clear();
//MainWindow::RefreshAllGLWindow();
}
|