| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> * |
| 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 |
#include <cassert> |
| 24 |
#include <numbers> |
| 25 |
|
| 26 |
#include <Inventor/SbRotation.h> |
| 27 |
#include <Inventor/engines/SoComposeVec3f.h> |
| 28 |
#include <Inventor/nodes/SoBaseColor.h> |
| 29 |
#include <Inventor/nodes/SoCone.h> |
| 30 |
#include <Inventor/nodes/SoAnnotation.h> |
| 31 |
#include <Inventor/engines/SoCalculator.h> |
| 32 |
#include <Inventor/nodes/SoTransform.h> |
| 33 |
|
| 34 |
#include "SoRotationDragger.h" |
| 35 |
|
| 36 |
#include <Base/Quantity.h> |
| 37 |
#include <Gui/Inventor/SoToggleSwitch.h> |
| 38 |
#include <Gui/MainWindow.h> |
| 39 |
#include <Gui/Utilities.h> |
| 40 |
|
| 41 |
#include "SoRotationDraggerGeometry.h" |
| 42 |
|
| 43 |
using namespace Gui; |
| 44 |
|
| 45 |
SO_KIT_SOURCE(SoRotationDragger) |
| 46 |
|
| 47 |
void SoRotationDragger::initClass() |
| 48 |
{ |
| 49 |
SO_KIT_INIT_CLASS(SoRotationDragger, SoDragger, "Dragger"); |
| 50 |
} |
| 51 |
|
| 52 |
SoRotationDragger::SoRotationDragger() |
| 53 |
{ |
| 54 |
SO_KIT_CONSTRUCTOR(SoRotationDragger); |
| 55 |
#if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) |
| 56 |
this->ref(); |
| 57 |
#endif |
| 58 |
|
| 59 |
SO_KIT_ADD_CATALOG_ENTRY(baseGeomSwitch, SoToggleSwitch, true, topSeparator, motionMatrix, true); |
| 60 |
SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY( |
| 61 |
baseGeom, |
| 62 |
SoRotatorGeometryBaseKit, |
| 63 |
SoRotatorBase, |
| 64 |
true, |
| 65 |
baseGeomSwitch, |
| 66 |
"", |
| 67 |
true |
| 68 |
); |
| 69 |
|
| 70 |
FC_ADD_CATALOG_ENTRY(baseColor, SoBaseColor, geomSeparator); |
| 71 |
FC_ADD_CATALOG_ENTRY(activeSwitch, SoToggleSwitch, geomSeparator); |
| 72 |
FC_ADD_CATALOG_ENTRY(secondaryColor, SoBaseColor, activeSwitch); |
| 73 |
FC_ADD_CATALOG_ENTRY(scale, SoScale, geomSeparator); |
| 74 |
SO_KIT_ADD_CATALOG_ABSTRACT_ENTRY( |
| 75 |
rotator, |
| 76 |
SoRotatorGeometryKit, |
| 77 |
SoRotatorGeometry, |
| 78 |
false, |
| 79 |
geomSeparator, |
| 80 |
"", |
| 81 |
true |
| 82 |
); |
| 83 |
|
| 84 |
SO_KIT_ADD_FIELD(rotation, (SbVec3f(0.0, 0.0, 1.0), 0.0)); |
| 85 |
SO_KIT_ADD_FIELD(rotationIncrement, (std::numbers::pi / 8.0)); |
| 86 |
SO_KIT_ADD_FIELD(rotationIncrementCount, (0)); |
| 87 |
SO_KIT_ADD_FIELD(color, (1, 0, 0)); |
| 88 |
SO_KIT_ADD_FIELD(activeColor, (1, 1, 0)); |
| 89 |
SO_KIT_ADD_FIELD(geometryScale, (1, 1, 1)); |
| 90 |
SO_KIT_ADD_FIELD(active, (false)); |
| 91 |
SO_KIT_ADD_FIELD(baseGeomVisible, (false)); |
| 92 |
|
| 93 |
SO_KIT_INIT_INSTANCE(); |
| 94 |
|
| 95 |
setPart("baseColor", buildColor()); |
| 96 |
setPart("secondaryColor", buildActiveColor()); |
| 97 |
|
| 98 |
auto scale = SO_GET_ANY_PART(this, "scale", SoScale); |
| 99 |
scale->scaleFactor.connectFrom(&geometryScale); |
| 100 |
|
| 101 |
auto rotator = SO_GET_ANY_PART(this, "rotator", SoRotatorGeometryKit); |
| 102 |
rotator->geometryScale.connectFrom(&geometryScale); |
| 103 |
|
| 104 |
this->addStartCallback(&SoRotationDragger::startCB); |
| 105 |
this->addMotionCallback(&SoRotationDragger::motionCB); |
| 106 |
this->addFinishCallback(&SoRotationDragger::finishCB); |
| 107 |
|
| 108 |
addValueChangedCallback(&SoRotationDragger::valueChangedCB); |
| 109 |
|
| 110 |
fieldSensor.setFunction(&SoRotationDragger::fieldSensorCB); |
| 111 |
fieldSensor.setData(this); |
| 112 |
fieldSensor.setPriority(0); |
| 113 |
|
| 114 |
this->setUpConnections(TRUE, TRUE); |
| 115 |
|
| 116 |
FC_SET_TOGGLE_SWITCH("activeSwitch", false); |
| 117 |
} |
| 118 |
|
| 119 |
SoRotationDragger::~SoRotationDragger() |
| 120 |
{ |
| 121 |
fieldSensor.setData(nullptr); |
| 122 |
fieldSensor.detach(); |
| 123 |
|
| 124 |
this->removeStartCallback(&SoRotationDragger::startCB); |
| 125 |
this->removeMotionCallback(&SoRotationDragger::motionCB); |
| 126 |
this->removeFinishCallback(&SoRotationDragger::finishCB); |
| 127 |
removeValueChangedCallback(&SoRotationDragger::valueChangedCB); |
| 128 |
} |
| 129 |
|
| 130 |
SoBaseColor* SoRotationDragger::buildColor() |
| 131 |
{ |
| 132 |
auto color = new SoBaseColor; |
| 133 |
color->rgb.connectFrom(&this->color); |
| 134 |
|
| 135 |
return color; |
| 136 |
} |
| 137 |
|
| 138 |
SoBaseColor* SoRotationDragger::buildActiveColor() |
| 139 |
{ |
| 140 |
auto color = new SoBaseColor; |
| 141 |
color->rgb.connectFrom(&activeColor); |
| 142 |
|
| 143 |
return color; |
| 144 |
} |
| 145 |
|
| 146 |
void SoRotationDragger::startCB(void*, SoDragger* d) |
| 147 |
{ |
| 148 |
auto sudoThis = static_cast<SoRotationDragger*>(d); |
| 149 |
assert(sudoThis); |
| 150 |
sudoThis->dragStart(); |
| 151 |
} |
| 152 |
|
| 153 |
void SoRotationDragger::motionCB(void*, SoDragger* d) |
| 154 |
{ |
| 155 |
auto sudoThis = static_cast<SoRotationDragger*>(d); |
| 156 |
assert(sudoThis); |
| 157 |
sudoThis->drag(); |
| 158 |
} |
| 159 |
|
| 160 |
void SoRotationDragger::finishCB(void*, SoDragger* d) |
| 161 |
{ |
| 162 |
auto sudoThis = static_cast<SoRotationDragger*>(d); |
| 163 |
assert(sudoThis); |
| 164 |
sudoThis->dragFinish(); |
| 165 |
} |
| 166 |
|
| 167 |
void SoRotationDragger::fieldSensorCB(void* f, SoSensor*) |
| 168 |
{ |
| 169 |
auto sudoThis = static_cast<SoRotationDragger*>(f); |
| 170 |
|
| 171 |
if (!f) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft |
| 176 |
sudoThis->workFieldsIntoTransform(matrix); |
| 177 |
sudoThis->setMotionMatrix(matrix); |
| 178 |
} |
| 179 |
|
| 180 |
void SoRotationDragger::valueChangedCB(void*, SoDragger* d) |
| 181 |
{ |
| 182 |
auto sudoThis = dynamic_cast<SoRotationDragger*>(d); |
| 183 |
assert(sudoThis); |
| 184 |
SbMatrix matrix = sudoThis->getMotionMatrix(); // clazy:exclude=rule-of-two-soft |
| 185 |
|
| 186 |
SbRotation localRotation = getMatrixTransform(matrix).rotation; |
| 187 |
|
| 188 |
sudoThis->fieldSensor.detach(); |
| 189 |
if (sudoThis->rotation.getValue() != localRotation) { |
| 190 |
sudoThis->rotation = localRotation; |
| 191 |
} |
| 192 |
sudoThis->fieldSensor.attach(&sudoThis->rotation); |
| 193 |
} |
| 194 |
|
| 195 |
void SoRotationDragger::dragStart() |
| 196 |
{ |
| 197 |
FC_SET_TOGGLE_SWITCH("activeSwitch", true); |
| 198 |
|
| 199 |
projector.setViewVolume(this->getViewVolume()); |
| 200 |
projector.setWorkingSpace(this->getLocalToWorldMatrix()); |
| 201 |
projector.setPlane(SbPlane(SbVec3f(0.0, 0.0, 1.0), 0.0)); |
| 202 |
|
| 203 |
SbVec3f hitPoint; |
| 204 |
if (!projector.tryProject(getNormalizedLocaterPosition(), 0.0, hitPoint)) { |
| 205 |
return; |
| 206 |
} |
| 207 |
hitPoint.normalize(); |
| 208 |
|
| 209 |
SbMatrix localToWorld = getLocalToWorldMatrix(); |
| 210 |
localToWorld.multVecMatrix(hitPoint, hitPoint); |
| 211 |
setStartingPoint((hitPoint)); |
| 212 |
|
| 213 |
rotationIncrementCount.setValue(0); |
| 214 |
} |
| 215 |
|
| 216 |
void SoRotationDragger::drag() |
| 217 |
{ |
| 218 |
projector.setViewVolume(this->getViewVolume()); |
| 219 |
projector.setWorkingSpace(this->getLocalToWorldMatrix()); |
| 220 |
|
| 221 |
SbVec3f hitPoint; |
| 222 |
if (!projector.tryProject(getNormalizedLocaterPosition(), 0.0, hitPoint)) { |
| 223 |
return; |
| 224 |
} |
| 225 |
hitPoint.normalize(); |
| 226 |
|
| 227 |
SbVec3f startingPoint = getLocalStartingPoint(); |
| 228 |
startingPoint.normalize(); |
| 229 |
|
| 230 |
SbRotation localRotation(startingPoint, hitPoint); |
| 231 |
// getting some slop from this. grab vector and put it absolute. |
| 232 |
SbVec3f tempVec; |
| 233 |
float tempRadians; |
| 234 |
localRotation.getValue(tempVec, tempRadians); |
| 235 |
tempVec[0] = 0.0; |
| 236 |
tempVec[1] = 0.0; |
| 237 |
tempVec.normalize(); |
| 238 |
if (tempVec[2] < 0.0) { |
| 239 |
tempRadians *= -1.0; |
| 240 |
tempVec.negate(); |
| 241 |
} |
| 242 |
int incrementCount = roundIncrement(tempRadians); |
| 243 |
rotationIncrementCount.setValue(incrementCount); |
| 244 |
localRotation |
| 245 |
= SbRotation(tempVec, incrementCount * static_cast<float>(rotationIncrement.getValue())); |
| 246 |
|
| 247 |
// same problem as described in tDragger::drag. |
| 248 |
if (localRotation.equals(SbRotation(SbVec3f(0.0, 0.0, 1.0), 0.0), 0.00001f)) { |
| 249 |
setMotionMatrix(getStartMotionMatrix()); |
| 250 |
this->valueChanged(); |
| 251 |
} |
| 252 |
else { |
| 253 |
setMotionMatrix(appendRotation(getStartMotionMatrix(), localRotation, SbVec3f(0.0, 0.0, 0.0))); |
| 254 |
} |
| 255 |
|
| 256 |
Base::Quantity quantity( |
| 257 |
static_cast<double>(rotationIncrementCount.getValue()) * (180.0 / std::numbers::pi) |
| 258 |
* rotationIncrement.getValue(), |
| 259 |
Base::Unit::Angle |
| 260 |
); |
| 261 |
|
| 262 |
QString message = QStringLiteral("%1 %2").arg( |
| 263 |
QObject::tr("Rotation:"), |
| 264 |
QString::fromStdString(quantity.getUserString()) |
| 265 |
); |
| 266 |
getMainWindow()->showMessage(message, 3000); |
| 267 |
} |
| 268 |
|
| 269 |
void SoRotationDragger::dragFinish() |
| 270 |
{ |
| 271 |
FC_SET_TOGGLE_SWITCH("activeSwitch", false); |
| 272 |
} |
| 273 |
|
| 274 |
SbBool SoRotationDragger::setUpConnections(SbBool onoff, SbBool doitalways) |
| 275 |
{ |
| 276 |
if (!doitalways && this->connectionsSetUp == onoff) { |
| 277 |
return onoff; |
| 278 |
} |
| 279 |
|
| 280 |
SbBool oldval = this->connectionsSetUp; |
| 281 |
|
| 282 |
if (onoff) { |
| 283 |
inherited::setUpConnections(onoff, doitalways); |
| 284 |
SoRotationDragger::fieldSensorCB(this, nullptr); |
| 285 |
if (this->fieldSensor.getAttachedField() != &this->rotation) { |
| 286 |
this->fieldSensor.attach(&this->rotation); |
| 287 |
} |
| 288 |
} |
| 289 |
else { |
| 290 |
if (this->fieldSensor.getAttachedField()) { |
| 291 |
this->fieldSensor.detach(); |
| 292 |
} |
| 293 |
inherited::setUpConnections(onoff, doitalways); |
| 294 |
} |
| 295 |
this->connectionsSetUp = onoff; |
| 296 |
return oldval; |
| 297 |
} |
| 298 |
|
| 299 |
int SoRotationDragger::roundIncrement(const float& radiansIn) |
| 300 |
{ |
| 301 |
int rCount = 0; |
| 302 |
|
| 303 |
auto increment = static_cast<float>(rotationIncrement.getValue()); |
| 304 |
if (fabs(radiansIn) > (increment / 2.0)) { |
| 305 |
rCount = static_cast<int>(radiansIn / increment); |
| 306 |
float remainder = fmod(radiansIn, increment); |
| 307 |
if (remainder >= (increment / 2.0)) { |
| 308 |
rCount++; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return rCount; |
| 313 |
} |
| 314 |
|
| 315 |
void SoRotationDragger::instantiateBaseGeometry() |
| 316 |
{ |
| 317 |
baseGeomVisible = true; |
| 318 |
|
| 319 |
auto baseGeom = SO_GET_ANY_PART(this, "baseGeom", SoRotatorBase); |
| 320 |
baseGeom->geometryScale.connectFrom(&geometryScale); |
| 321 |
baseGeom->rotation.connectFrom(&rotation); |
| 322 |
baseGeom->active.connectFrom(&active); |
| 323 |
} |
| 324 |
|
| 325 |
SO_KIT_SOURCE(SoRotationDraggerContainer) |
| 326 |
|
| 327 |
void SoRotationDraggerContainer::initClass() |
| 328 |
{ |
| 329 |
SoRotationDragger::initClass(); |
| 330 |
SO_KIT_INIT_CLASS(SoRotationDraggerContainer, SoInteractionKit, "InteractionKit"); |
| 331 |
} |
| 332 |
|
| 333 |
SoRotationDraggerContainer::SoRotationDraggerContainer() |
| 334 |
{ |
| 335 |
SO_KIT_CONSTRUCTOR(SoRotationDraggerContainer); |
| 336 |
|
| 337 |
#if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) |
| 338 |
this->ref(); |
| 339 |
#endif |
| 340 |
|
| 341 |
FC_ADD_CATALOG_ENTRY(draggerSwitch, SoToggleSwitch, geomSeparator); |
| 342 |
FC_ADD_CATALOG_ENTRY(transform, SoTransform, draggerSwitch); |
| 343 |
FC_ADD_CATALOG_ENTRY(dragger, SoRotationDragger, draggerSwitch); |
| 344 |
|
| 345 |
SO_KIT_ADD_FIELD(rotation, (0, 0, 0, 0)); |
| 346 |
SO_KIT_ADD_FIELD(color, (0, 0, 0)); |
| 347 |
SO_KIT_ADD_FIELD(translation, (0, 0, 0)); |
| 348 |
SO_KIT_ADD_FIELD(visible, (1)); |
| 349 |
|
| 350 |
SO_KIT_INIT_INSTANCE(); |
| 351 |
|
| 352 |
setPart("transform", buildTransform()); |
| 353 |
|
| 354 |
auto sw = SO_GET_ANY_PART(this, "draggerSwitch", SoToggleSwitch); |
| 355 |
sw->on.connectFrom(&visible); |
| 356 |
|
| 357 |
getDragger()->color.connectFrom(&color); |
| 358 |
} |
| 359 |
|
| 360 |
SoTransform* SoRotationDraggerContainer::buildTransform() |
| 361 |
{ |
| 362 |
auto transform = new SoTransform; |
| 363 |
transform->translation.connectFrom(&this->translation); |
| 364 |
transform->rotation.connectFrom(&this->rotation); |
| 365 |
|
| 366 |
return transform; |
| 367 |
} |
| 368 |
|
| 369 |
SoRotationDragger* SoRotationDraggerContainer::getDragger() |
| 370 |
{ |
| 371 |
return SO_GET_PART(this, "dragger", SoRotationDragger); |
| 372 |
} |
| 373 |
|
| 374 |
SbVec3f SoRotationDraggerContainer::getPointerDirection() |
| 375 |
{ |
| 376 |
// This is the direction along which the SoLinearDragger points in it local space |
| 377 |
SbVec3f draggerDir |
| 378 |
= SO_GET_ANY_PART(this, "rotator", SoRotatorGeometryKit)->pivotPosition.getValue(); |
| 379 |
rotation.getValue().multVec(draggerDir, draggerDir); |
| 380 |
|
| 381 |
return draggerDir; |
| 382 |
} |
| 383 |
|
| 384 |
void SoRotationDraggerContainer::setPointerDirection(const SbVec3f& dir) |
| 385 |
{ |
| 386 |
// This is the direction from the origin to the spherical pivot of the rotator |
| 387 |
SbVec3f draggerDir |
| 388 |
= SO_GET_ANY_PART(this, "rotator", SoRotatorGeometryKit)->pivotPosition.getValue(); |
| 389 |
|
| 390 |
SbRotation rot {draggerDir, dir}; |
| 391 |
rotation.setValue(rot); |
| 392 |
} |
| 393 |
|
| 394 |
void SoRotationDraggerContainer::setArcNormalDirection(const SbVec3f& dir) |
| 395 |
{ |
| 396 |
SbVec3f currentNormal = {0, 0, 1}; |
| 397 |
auto currentRot = rotation.getValue(); |
| 398 |
currentRot.multVec(currentNormal, currentNormal); |
| 399 |
|
| 400 |
SbRotation rot {currentNormal, dir}; |
| 401 |
rotation = currentRot * rot; |
| 402 |
} |