| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#include <Inventor/SbViewportRegion.h> |
| 26 |
#include <Inventor/SoPickedPoint.h> |
| 27 |
#include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 28 |
#include <Inventor/draggers/SoDragger.h> |
| 29 |
#include <Inventor/errors/SoDebugError.h> |
| 30 |
#include <Inventor/nodes/SoSeparator.h> |
| 31 |
#include <Inventor/nodes/SoCamera.h> |
| 32 |
#include <Inventor/nodes/SoOrthographicCamera.h> |
| 33 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 34 |
#include <Inventor/projectors/SbSphereSheetProjector.h> |
| 35 |
#include <QAction> |
| 36 |
#include <QActionGroup> |
| 37 |
#include <QApplication> |
| 38 |
#include <QByteArray> |
| 39 |
#include <QCursor> |
| 40 |
#include <QMenu> |
| 41 |
|
| 42 |
|
| 43 |
#include <cmath> |
| 44 |
#include <limits> |
| 45 |
|
| 46 |
#include <Base/Interpreter.h> |
| 47 |
#include <App/Application.h> |
| 48 |
|
| 49 |
#include "Navigation/NavigationStyle.h" |
| 50 |
#include "Navigation/NavigationStylePy.h" |
| 51 |
#include "Application.h" |
| 52 |
#include "Command.h" |
| 53 |
#include "Action.h" |
| 54 |
#include "Inventor/SoMouseWheelEvent.h" |
| 55 |
#include "MenuManager.h" |
| 56 |
#include "MouseSelection.h" |
| 57 |
#include "Navigation/NavigationAnimator.h" |
| 58 |
#include "Navigation/NavigationAnimation.h" |
| 59 |
#include "View3DInventorViewer.h" |
| 60 |
|
| 61 |
using namespace Gui; |
| 62 |
|
| 63 |
class FCSphereSheetProjector: public SbSphereSheetProjector |
| 64 |
{ |
| 65 |
using inherited = SbSphereSheetProjector; |
| 66 |
|
| 67 |
public: |
| 68 |
enum OrbitStyle |
| 69 |
{ |
| 70 |
Turntable, |
| 71 |
Trackball, |
| 72 |
FreeTurntable, |
| 73 |
TrackballClassic, |
| 74 |
RoundedArcball |
| 75 |
}; |
| 76 |
|
| 77 |
static constexpr float defaultSphereRadius = 0.8F; |
| 78 |
|
| 79 |
FCSphereSheetProjector(const SbSphere& sph, const SbBool orienttoeye = true) |
| 80 |
: SbSphereSheetProjector(sph, orienttoeye) |
| 81 |
{} |
| 82 |
|
| 83 |
void setViewVolume(const SbViewVolume& vol) override |
| 84 |
{ |
| 85 |
inherited::setViewVolume(vol); |
| 86 |
} |
| 87 |
|
| 88 |
void setWorkingSpace(const SbMatrix& space) override |
| 89 |
{ |
| 90 |
// inherited::setWorkingSpace(space); |
| 91 |
this->worldToScreen = space.inverse(); |
| 92 |
} |
| 93 |
|
| 94 |
SbVec3f project(const SbVec2f& point) override |
| 95 |
{ |
| 96 |
if (orbit != RoundedArcball) { |
| 97 |
return inherited::project(point); |
| 98 |
} |
| 99 |
|
| 100 |
// Rounded Arcball implementation |
| 101 |
// based on SbSphereSheetProjector in Open Inventor |
| 102 |
SbVec3f result; |
| 103 |
SbLine workingLine = getWorkingLine(point); |
| 104 |
|
| 105 |
if (needSetup) { |
| 106 |
setupPlane(); |
| 107 |
} |
| 108 |
|
| 109 |
SbVec3f planeIntersection; |
| 110 |
|
| 111 |
SbVec3f sphereIntersection, dontCare; |
| 112 |
SbBool hitSphere; |
| 113 |
if (intersectFront == TRUE) { |
| 114 |
hitSphere = sphere.intersect(workingLine, sphereIntersection, dontCare); |
| 115 |
} |
| 116 |
else { |
| 117 |
hitSphere = sphere.intersect(workingLine, dontCare, sphereIntersection); |
| 118 |
} |
| 119 |
|
| 120 |
if (hitSphere) { |
| 121 |
// drop the sphere intersection onto the tolerance plane |
| 122 |
|
| 123 |
SbLine projectLine(sphereIntersection, sphereIntersection + planeDir); |
| 124 |
if (!tolPlane.intersect(projectLine, planeIntersection)) { |
| 125 |
#ifdef FC_DEBUG |
| 126 |
SoDebugError::post( |
| 127 |
"SbSphereSheetProjector::project", |
| 128 |
"Could not intersect working line with plane" |
| 129 |
); |
| 130 |
#endif |
| 131 |
} |
| 132 |
} |
| 133 |
else if (!tolPlane.intersect(workingLine, planeIntersection)) { |
| 134 |
#ifdef FC_DEBUG |
| 135 |
SoDebugError::post("SbSphereSheetProjector::project", "Could not intersect with plane"); |
| 136 |
#endif |
| 137 |
} |
| 138 |
|
| 139 |
// Three possibilities: |
| 140 |
// (1) Intersection is on the sphere inside where the fillet |
| 141 |
// hits it |
| 142 |
// (2) Intersection is on the fillet |
| 143 |
// (3) Intersection is on the plane |
| 144 |
float distance = (planeIntersection - planePoint).length(); |
| 145 |
|
| 146 |
// Amount of filleting |
| 147 |
// 0 = no fillet (just sphere and plane) |
| 148 |
// infinity = only fillet |
| 149 |
float border = 0.5; |
| 150 |
|
| 151 |
// Radius where the fillet meets the plane in "squished space" |
| 152 |
float r_a = 1.0F + border; |
| 153 |
// Radius where the sphere meets the fillet in "squished space" |
| 154 |
float r_i = 2.0 / (r_a + 1.0 / r_a); |
| 155 |
|
| 156 |
// Distance squared in "squished space" |
| 157 |
float d_2 = (distance * distance) * r_a * r_a; |
| 158 |
// Distance in "squished space" |
| 159 |
float d = std::sqrt(d_2); |
| 160 |
|
| 161 |
// Compute how far off the plane we are |
| 162 |
float offsetDist = 0.0; |
| 163 |
|
| 164 |
if (d > r_a) { |
| 165 |
// On the plane |
| 166 |
offsetDist = 0.0; |
| 167 |
} |
| 168 |
else if (d < r_i) { |
| 169 |
// On the sphere inside the fillet |
| 170 |
offsetDist = std::sqrt(1.0 - d_2); |
| 171 |
} |
| 172 |
else { |
| 173 |
// On the fillet |
| 174 |
float d_r = r_a - d; |
| 175 |
float a = border * (1.0 + border / 2.0); |
| 176 |
offsetDist = a - std::sqrt((a + d_r) * (a - d_r)); |
| 177 |
} |
| 178 |
|
| 179 |
SbVec3f offset; |
| 180 |
if (orientToEye) { |
| 181 |
if (viewVol.getProjectionType() == SbViewVolume::PERSPECTIVE) { |
| 182 |
offset = workingProjPoint - planeIntersection; |
| 183 |
} |
| 184 |
else { |
| 185 |
worldToWorking.multDirMatrix(viewVol.zVector(), offset); |
| 186 |
} |
| 187 |
|
| 188 |
offset.normalize(); |
| 189 |
} |
| 190 |
else { |
| 191 |
offset.setValue(0, 0, 1); |
| 192 |
} |
| 193 |
if (intersectFront == FALSE) { |
| 194 |
offset *= -1.0; |
| 195 |
} |
| 196 |
|
| 197 |
offset *= offsetDist; |
| 198 |
result = planeIntersection + offset; |
| 199 |
|
| 200 |
lastPoint = result; |
| 201 |
return result; |
| 202 |
} |
| 203 |
|
| 204 |
SbRotation getRotation(const SbVec3f& point1, const SbVec3f& point2) override |
| 205 |
{ |
| 206 |
SbRotation rot = inherited::getRotation(point1, point2); |
| 207 |
if (orbit == Turntable || orbit == FreeTurntable) { |
| 208 |
return getTurntable(rot, point1, point2); |
| 209 |
} |
| 210 |
if (orbit == TrackballClassic) { |
| 211 |
return getTrackballClassic(point1, point2); |
| 212 |
} |
| 213 |
|
| 214 |
return rot; |
| 215 |
} |
| 216 |
|
| 217 |
void setOrbitStyle(OrbitStyle style) |
| 218 |
{ |
| 219 |
this->orbit = style; |
| 220 |
} |
| 221 |
|
| 222 |
OrbitStyle getOrbitStyle() const |
| 223 |
{ |
| 224 |
return this->orbit; |
| 225 |
} |
| 226 |
|
| 227 |
private: |
| 228 |
SbRotation getTurntable(SbRotation rot, const SbVec3f& point1, const SbVec3f& point2) const |
| 229 |
{ |
| 230 |
// 0000333: Turntable camera rotation |
| 231 |
SbVec3f axis; |
| 232 |
float angle {}; |
| 233 |
rot.getValue(axis, angle); |
| 234 |
SbVec3f dif = point1 - point2; |
| 235 |
if (fabs(dif[1]) > fabs(dif[0])) { |
| 236 |
SbVec3f xaxis(1, 0, 0); |
| 237 |
if (dif[1] < 0) { |
| 238 |
angle = -angle; |
| 239 |
} |
| 240 |
rot.setValue(xaxis, angle); |
| 241 |
} |
| 242 |
else { |
| 243 |
SbVec3f zaxis(0, 0, 1); |
| 244 |
this->worldToScreen.multDirMatrix(zaxis, zaxis); |
| 245 |
if (zaxis[1] < 0) { |
| 246 |
if (dif[0] < 0) { |
| 247 |
angle = -angle; |
| 248 |
} |
| 249 |
} |
| 250 |
else { |
| 251 |
if (dif[0] > 0) { |
| 252 |
angle = -angle; |
| 253 |
} |
| 254 |
} |
| 255 |
rot.setValue(zaxis, angle); |
| 256 |
} |
| 257 |
|
| 258 |
return rot; |
| 259 |
} |
| 260 |
|
| 261 |
SbRotation getTrackballClassic(const SbVec3f& point1, const SbVec3f& point2) const |
| 262 |
{ |
| 263 |
// Classic trackball |
| 264 |
SbRotation zrot; |
| 265 |
SbRotation yrot; |
| 266 |
SbVec3f dif = point1 - point2; |
| 267 |
|
| 268 |
SbVec3f zaxis(1, 0, 0); |
| 269 |
zrot.setValue(zaxis, dif[1]); |
| 270 |
|
| 271 |
SbVec3f yaxis(0, 1, 0); |
| 272 |
yrot.setValue(yaxis, -dif[0]); |
| 273 |
|
| 274 |
return zrot * yrot; |
| 275 |
} |
| 276 |
|
| 277 |
private: |
| 278 |
SbMatrix worldToScreen; |
| 279 |
OrbitStyle orbit {RoundedArcball}; |
| 280 |
}; |
| 281 |
|
| 282 |
NavigationStyleEvent::NavigationStyleEvent(const Base::Type& s) |
| 283 |
: QEvent(QEvent::User) |
| 284 |
, t(s) |
| 285 |
{} |
| 286 |
|
| 287 |
NavigationStyleEvent::~NavigationStyleEvent() = default; |
| 288 |
|
| 289 |
const Base::Type& NavigationStyleEvent::style() const |
| 290 |
{ |
| 291 |
return t; |
| 292 |
} |
| 293 |
|
| 294 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::NavigationStyle, Base::BaseClass) |
| 295 |
|
| 296 |
NavigationStyle::NavigationStyle() |
| 297 |
: viewer(nullptr) |
| 298 |
, mouseSelection(nullptr) |
| 299 |
, pythonObject(nullptr) |
| 300 |
{ |
| 301 |
this->rotationCenterMode = NavigationStyle::RotationCenterMode::ScenePointAtCursor |
| 302 |
| NavigationStyle::RotationCenterMode::FocalPointAtCursor; |
| 303 |
initialize(); |
| 304 |
} |
| 305 |
|
| 306 |
NavigationStyle::~NavigationStyle() |
| 307 |
{ |
| 308 |
finalize(); |
| 309 |
delete this->animator; |
| 310 |
|
| 311 |
if (!pythonObject.is(nullptr)) { |
| 312 |
Base::PyGILStateLocker lock; |
| 313 |
Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(pythonObject.ptr()); |
| 314 |
obj->setInvalid(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
NavigationStyle& NavigationStyle::operator=(const NavigationStyle& ns) |
| 319 |
{ |
| 320 |
this->panningplane = ns.panningplane; |
| 321 |
this->menuenabled = ns.menuenabled; |
| 322 |
this->animationEnabled = ns.animationEnabled; |
| 323 |
this->spinningAnimationEnabled = ns.spinningAnimationEnabled; |
| 324 |
static_cast<FCSphereSheetProjector*>(this->spinprojector) |
| 325 |
->setOrbitStyle(static_cast<FCSphereSheetProjector*>(ns.spinprojector)->getOrbitStyle()); |
| 326 |
return *this; |
| 327 |
} |
| 328 |
|
| 329 |
void NavigationStyle::setViewer(View3DInventorViewer* view) |
| 330 |
{ |
| 331 |
this->viewer = view; |
| 332 |
} |
| 333 |
|
| 334 |
void NavigationStyle::initialize() |
| 335 |
{ |
| 336 |
this->animator = new NavigationAnimator(); |
| 337 |
|
| 338 |
this->sensitivity = 2.0f; |
| 339 |
this->resetcursorpos = false; |
| 340 |
this->currentmode = NavigationStyle::IDLE; |
| 341 |
this->animationEnabled = true; |
| 342 |
this->spinningAnimationEnabled = false; |
| 343 |
this->spinsamplecounter = 0; |
| 344 |
this->spinincrement = SbRotation::identity(); |
| 345 |
this->rotationCenterFound = false; |
| 346 |
this->rotationCenterIsScenePointAtCursor = false; |
| 347 |
|
| 348 |
// FIXME: use a smaller sphere than the default one to have a larger |
| 349 |
// area close to the borders that gives us "z-axis rotation"? |
| 350 |
// 19990425 mortene. |
| 351 |
this->spinprojector = new FCSphereSheetProjector( |
| 352 |
SbSphere(SbVec3f(0, 0, 0), FCSphereSheetProjector::defaultSphereRadius) |
| 353 |
); |
| 354 |
SbViewVolume volume; |
| 355 |
volume.ortho(-1, 1, -1, 1, -1, 1); |
| 356 |
this->spinprojector->setViewVolume(volume); |
| 357 |
|
| 358 |
this->log.size = 16; |
| 359 |
this->log.position = new SbVec2s[16]; |
| 360 |
this->log.time = new SbTime[16]; |
| 361 |
this->log.historysize = 0; |
| 362 |
|
| 363 |
this->menuenabled = true; |
| 364 |
this->button1down = false; |
| 365 |
this->button2down = false; |
| 366 |
this->button3down = false; |
| 367 |
this->ctrldown = false; |
| 368 |
this->shiftdown = false; |
| 369 |
this->altdown = false; |
| 370 |
this->invertZoom = App::GetApplication() |
| 371 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View") |
| 372 |
->GetBool("InvertZoom", true); |
| 373 |
this->zoomAtCursor = App::GetApplication() |
| 374 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View") |
| 375 |
->GetBool("ZoomAtCursor", true); |
| 376 |
this->zoomStep = App::GetApplication() |
| 377 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View") |
| 378 |
->GetFloat("ZoomStep", 0.2f); |
| 379 |
long mode = App::GetApplication() |
| 380 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View") |
| 381 |
->GetInt("RotationMode", 0); |
| 382 |
if (mode == 0) { |
| 383 |
setRotationCenterMode(NavigationStyle::RotationCenterMode::WindowCenter); |
| 384 |
} |
| 385 |
else if (mode == 1) { |
| 386 |
setRotationCenterMode( |
| 387 |
NavigationStyle::RotationCenterMode::ScenePointAtCursor |
| 388 |
| NavigationStyle::RotationCenterMode::FocalPointAtCursor |
| 389 |
); |
| 390 |
} |
| 391 |
else if (mode == 2) { |
| 392 |
setRotationCenterMode( |
| 393 |
NavigationStyle::RotationCenterMode::ScenePointAtCursor |
| 394 |
| NavigationStyle::RotationCenterMode::BoundingBoxCenter |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
this->hasDragged = false; |
| 399 |
this->hasPanned = false; |
| 400 |
this->hasZoomed = false; |
| 401 |
} |
| 402 |
|
| 403 |
void NavigationStyle::finalize() |
| 404 |
{ |
| 405 |
delete this->spinprojector; |
| 406 |
delete[] this->log.position; |
| 407 |
delete[] this->log.time; |
| 408 |
} |
| 409 |
|
| 410 |
void NavigationStyle::interactiveCountInc() |
| 411 |
{ |
| 412 |
viewer->interactiveCountInc(); |
| 413 |
} |
| 414 |
|
| 415 |
void NavigationStyle::interactiveCountDec() |
| 416 |
{ |
| 417 |
viewer->interactiveCountDec(); |
| 418 |
} |
| 419 |
|
| 420 |
int NavigationStyle::getInteractiveCount() const |
| 421 |
{ |
| 422 |
return viewer->getInteractiveCount(); |
| 423 |
} |
| 424 |
|
| 425 |
void NavigationStyle::setOrbitStyle(NavigationStyle::OrbitStyle style) |
| 426 |
{ |
| 427 |
auto projector = static_cast<FCSphereSheetProjector*>(this->spinprojector); |
| 428 |
projector->setOrbitStyle(FCSphereSheetProjector::OrbitStyle(style)); |
| 429 |
} |
| 430 |
|
| 431 |
NavigationStyle::OrbitStyle NavigationStyle::getOrbitStyle() const |
| 432 |
{ |
| 433 |
auto projector = static_cast<FCSphereSheetProjector*>(this->spinprojector); |
| 434 |
return NavigationStyle::OrbitStyle(projector->getOrbitStyle()); |
| 435 |
} |
| 436 |
|
| 437 |
SbBool NavigationStyle::isViewing() const |
| 438 |
{ |
| 439 |
return viewer->isViewing(); |
| 440 |
} |
| 441 |
|
| 442 |
void NavigationStyle::setViewing(SbBool enable) |
| 443 |
{ |
| 444 |
viewer->setViewing(enable); |
| 445 |
} |
| 446 |
|
| 447 |
SbBool NavigationStyle::isSeekMode() const |
| 448 |
{ |
| 449 |
return viewer->isSeekMode(); |
| 450 |
} |
| 451 |
|
| 452 |
void NavigationStyle::setSeekMode(SbBool enable) |
| 453 |
{ |
| 454 |
viewer->setSeekMode(enable); |
| 455 |
} |
| 456 |
|
| 457 |
SbBool NavigationStyle::seekToPoint(const SbVec2s screenpos) |
| 458 |
{ |
| 459 |
return viewer->seekToPoint(screenpos); |
| 460 |
} |
| 461 |
|
| 462 |
void NavigationStyle::seekToPoint(const SbVec3f& scenepos) |
| 463 |
{ |
| 464 |
viewer->seekToPoint(scenepos); |
| 465 |
} |
| 466 |
|
| 467 |
void NavigationStyle::lookAtPoint(const SbVec2s screenpos) |
| 468 |
{ |
| 469 |
const SoCamera* camera = viewer->getCamera(); |
| 470 |
if (!camera) { |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
SoRayPickAction rpaction(viewer->getViewportRegion()); |
| 475 |
rpaction.setPoint(screenpos); |
| 476 |
rpaction.setRadius(viewer->getPickRadius()); |
| 477 |
rpaction.apply(viewer->getSoRenderManager()->getSceneGraph()); |
| 478 |
|
| 479 |
const SoPickedPoint* picked = rpaction.getPickedPoint(); |
| 480 |
|
| 481 |
// Point is either the hitpoint or the projected point on the panning plane |
| 482 |
SbVec3f point; |
| 483 |
if (picked) { |
| 484 |
point = picked->getPoint(); |
| 485 |
} |
| 486 |
else { |
| 487 |
const SbViewportRegion& vp = viewer->getViewportRegion(); |
| 488 |
const float aspectratio = vp.getViewportAspectRatio(); |
| 489 |
SbViewVolume vv = camera->getViewVolume(aspectratio); |
| 490 |
|
| 491 |
// See note in Coin docs for SoCamera::getViewVolume re:viewport mapping |
| 492 |
if (aspectratio < 1.0) { |
| 493 |
vv.scale(1.0 / aspectratio); |
| 494 |
} |
| 495 |
|
| 496 |
SbLine line; |
| 497 |
vv.projectPointToLine(normalizePixelPos(screenpos), line); |
| 498 |
panningplane.intersect(line, point); |
| 499 |
} |
| 500 |
|
| 501 |
lookAtPoint(point); |
| 502 |
} |
| 503 |
|
| 504 |
void NavigationStyle::lookAtPoint(const SbVec3f& position) |
| 505 |
{ |
| 506 |
this->rotationCenterFound = false; |
| 507 |
translateCamera(position - getFocalPoint()); |
| 508 |
} |
| 509 |
|
| 510 |
SoCamera* NavigationStyle::getCamera() const |
| 511 |
{ |
| 512 |
return this->viewer->getCamera(); |
| 513 |
} |
| 514 |
|
| 515 |
std::shared_ptr<NavigationAnimation> NavigationStyle::setCameraOrientation( |
| 516 |
const SbRotation& orientation, |
| 517 |
const SbBool moveToCenter |
| 518 |
) const |
| 519 |
{ |
| 520 |
SoCamera* camera = getCamera(); |
| 521 |
if (!camera) { |
| 522 |
return {}; |
| 523 |
} |
| 524 |
|
| 525 |
animator->stop(); |
| 526 |
|
| 527 |
const SbVec3f focalPoint = getFocalPoint(); |
| 528 |
SbVec3f translation(0, 0, 0); |
| 529 |
|
| 530 |
if (moveToCenter) { |
| 531 |
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion()); |
| 532 |
action.apply(viewer->getSceneGraph()); |
| 533 |
SbBox3f box = action.getBoundingBox(); |
| 534 |
if (!box.isEmpty()) { |
| 535 |
translation = box.getCenter() - focalPoint; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
// Start an animation and return it |
| 540 |
if (isAnimationEnabled()) { |
| 541 |
return viewer->startAnimation(orientation, focalPoint, translation); |
| 542 |
} |
| 543 |
|
| 544 |
// or set the pose directly |
| 545 |
|
| 546 |
// Distance from rotation center to camera position in camera coordinate system |
| 547 |
const SbVec3f rotationCenterDistanceCam = camera->focalDistance.getValue() * SbVec3f(0, 0, 1); |
| 548 |
|
| 549 |
// Set to the given orientation |
| 550 |
camera->orientation = orientation; |
| 551 |
|
| 552 |
// Distance from rotation center to new camera position in global coordinate system |
| 553 |
SbVec3f newRotationCenterDistance; |
| 554 |
camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance); |
| 555 |
|
| 556 |
// Reposition camera so the rotation center stays in the same place |
| 557 |
// Optionally add translation to move to center |
| 558 |
camera->position = focalPoint + newRotationCenterDistance + translation; |
| 559 |
|
| 560 |
return {}; |
| 561 |
} |
| 562 |
|
| 563 |
std::shared_ptr<NavigationAnimation> NavigationStyle::translateCamera(const SbVec3f& translation) const |
| 564 |
{ |
| 565 |
SoCamera* camera = getCamera(); |
| 566 |
if (!camera) { |
| 567 |
return {}; |
| 568 |
} |
| 569 |
|
| 570 |
animator->stop(); |
| 571 |
|
| 572 |
// Start an animation and return it |
| 573 |
if (isAnimationEnabled()) { |
| 574 |
return viewer->startAnimation(camera->orientation.getValue(), SbVec3f(0, 0, 0), translation); |
| 575 |
} |
| 576 |
|
| 577 |
// or set the pose directly |
| 578 |
|
| 579 |
camera->position = camera->position.getValue() + translation; |
| 580 |
|
| 581 |
return {}; |
| 582 |
} |
| 583 |
|
| 584 |
void NavigationStyle::boxZoom(const SbBox2s& box) |
| 585 |
{ |
| 586 |
SoCamera* cam = viewer->getSoRenderManager()->getCamera(); |
| 587 |
if (!cam) { // no camera |
| 588 |
return; |
| 589 |
} |
| 590 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 591 |
SbViewVolume vv = cam->getViewVolume(vp.getViewportAspectRatio()); |
| 592 |
|
| 593 |
short sizeX {}, sizeY {}; |
| 594 |
box.getSize(sizeX, sizeY); |
| 595 |
SbVec2s size = vp.getViewportSizePixels(); |
| 596 |
|
| 597 |
// The bbox must not be empty i.e. width and length is zero, but it is possible that |
| 598 |
// either width or length is zero |
| 599 |
if (sizeX == 0 && sizeY == 0) { |
| 600 |
return; |
| 601 |
} |
| 602 |
|
| 603 |
// Get the new center in normalized pixel coordinates |
| 604 |
short xmin {}, xmax {}, ymin {}, ymax {}; |
| 605 |
box.getBounds(xmin, ymin, xmax, ymax); |
| 606 |
const SbVec2f center( |
| 607 |
(float)((xmin + xmax) / 2) / (float)std::max((int)(size[0] - 1), 1), |
| 608 |
(float)(size[1] - (ymin + ymax) / 2) / (float)std::max((int)(size[1] - 1), 1) |
| 609 |
); |
| 610 |
|
| 611 |
SbPlane plane = vv.getPlane(cam->focalDistance.getValue()); |
| 612 |
panCamera(cam, vp.getViewportAspectRatio(), plane, SbVec2f(0.5, 0.5), center); |
| 613 |
|
| 614 |
// Set height or height angle of the camera |
| 615 |
float scaleX = (float)sizeX / (float)size[0]; |
| 616 |
float scaleY = (float)sizeY / (float)size[1]; |
| 617 |
float scaleFactor = std::max<float>(scaleX, scaleY); |
| 618 |
|
| 619 |
doScale(cam, scaleFactor); |
| 620 |
} |
| 621 |
void NavigationStyle::scale(float factor) |
| 622 |
{ |
| 623 |
SoCamera* cam = viewer->getSoRenderManager()->getCamera(); |
| 624 |
if (!cam) { // no camera |
| 625 |
return; |
| 626 |
} |
| 627 |
|
| 628 |
// Find the current center of the screen |
| 629 |
SbVec3f direction; |
| 630 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 631 |
SbVec3f initCenter = cam->position.getValue() + cam->focalDistance.getValue() * direction; |
| 632 |
|
| 633 |
// Move the camera to the origin for scaling |
| 634 |
cam->position = cam->position.getValue() - initCenter; |
| 635 |
|
| 636 |
// Scale the view |
| 637 |
doScale(cam, factor); |
| 638 |
|
| 639 |
// Move the camera back to it's initial position scaled |
| 640 |
cam->position = cam->position.getValue() + initCenter * factor; |
| 641 |
} |
| 642 |
|
| 643 |
void NavigationStyle::viewAll() |
| 644 |
{ |
| 645 |
// Get the bounding box of the scene |
| 646 |
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion()); |
| 647 |
action.apply(viewer->getSceneGraph()); |
| 648 |
SbBox3f box = action.getBoundingBox(); |
| 649 |
if (box.isEmpty()) { |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
SoCamera* cam = viewer->getSoRenderManager()->getCamera(); |
| 655 |
if (!cam) { |
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
SbViewVolume vol = cam->getViewVolume(); |
| 660 |
if (vol.ulf == vol.llf) { |
| 661 |
return; // empty frustum (no view up vector defined) |
| 662 |
} |
| 663 |
SbVec2f s = vol.projectBox(box); |
| 664 |
SbVec2s size = viewer->getSoRenderManager()->getSize(); |
| 665 |
|
| 666 |
SbVec3f pt1, pt2, pt3, tmp; |
| 667 |
vol.projectPointToLine(SbVec2f(0.0f, 0.0f), pt1, tmp); |
| 668 |
vol.projectPointToLine(SbVec2f(s[0], 0.0f), pt2, tmp); |
| 669 |
vol.projectPointToLine(SbVec2f(0.0f, s[1]), pt3, tmp); |
| 670 |
|
| 671 |
float cam_width = (pt2 - pt1).length(); |
| 672 |
float cam_height = (pt3 - pt1).length(); |
| 673 |
|
| 674 |
// add a small border |
| 675 |
cam_height = 1.08f * std::max<float>((cam_width * (float)size[1]) / (float)size[0], cam_height); |
| 676 |
|
| 677 |
float aspect = cam->aspectRatio.getValue(); |
| 678 |
|
| 679 |
if (cam->getTypeId() == SoOrthographicCamera::getClassTypeId()) { |
| 680 |
auto ocam = static_cast<SoOrthographicCamera*>(cam); |
| 681 |
if (aspect < 1.0f) { |
| 682 |
ocam->height = cam_height / aspect; |
| 683 |
} |
| 684 |
else { |
| 685 |
ocam->height = cam_height; |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403) |
| 691 |
void NavigationStyle::findBoundingSphere() |
| 692 |
{ |
| 693 |
// Find a bounding sphere for the scene |
| 694 |
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion()); |
| 695 |
action.apply(viewer->getSceneGraph()); |
| 696 |
boundingSphere.circumscribe(action.getBoundingBox()); |
| 697 |
} |
| 698 |
#endif |
| 699 |
|
| 700 |
/** Rotate the camera by the given amount, then reposition it so we're still pointing at the same |
| 701 |
* focal point |
| 702 |
*/ |
| 703 |
void NavigationStyle::reorientCamera(SoCamera* camera, const SbRotation& rotation) |
| 704 |
{ |
| 705 |
reorientCamera(camera, rotation, getFocalPoint()); |
| 706 |
} |
| 707 |
|
| 708 |
/** Rotate the camera by the given amount, then reposition it so the rotation center stays in the |
| 709 |
* same place |
| 710 |
*/ |
| 711 |
void NavigationStyle::reorientCamera( |
| 712 |
SoCamera* camera, |
| 713 |
const SbRotation& rotation, |
| 714 |
const SbVec3f& rotationCenter |
| 715 |
) |
| 716 |
{ |
| 717 |
if (!camera) { |
| 718 |
return; |
| 719 |
} |
| 720 |
|
| 721 |
// Distance from rotation center to camera position in camera coordinate system |
| 722 |
SbVec3f rotationCenterDistanceCam; |
| 723 |
camera->orientation.getValue().inverse().multVec( |
| 724 |
camera->position.getValue() - rotationCenter, |
| 725 |
rotationCenterDistanceCam |
| 726 |
); |
| 727 |
|
| 728 |
// Set new orientation value by accumulating the new rotation |
| 729 |
camera->orientation = rotation * camera->orientation.getValue(); |
| 730 |
|
| 731 |
// Distance from rotation center to new camera position in global coordinate system |
| 732 |
SbVec3f newRotationCenterDistance; |
| 733 |
camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance); |
| 734 |
|
| 735 |
// Reposition camera so the rotation center stays in the same place |
| 736 |
camera->position = rotationCenter + newRotationCenterDistance; |
| 737 |
|
| 738 |
if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) { |
| 739 |
// Adjust the camera position to keep the focal point in the same place while making sure |
| 740 |
// the focal distance stays small. This increases rotation stability for small and large |
| 741 |
// scenes |
| 742 |
|
| 743 |
SbVec3f direction; |
| 744 |
camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 745 |
|
| 746 |
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 405) |
| 747 |
// Large focal distance puts the camera far away which causes Coin's auto clipping |
| 748 |
// calculations to add more slack (more space between near and far plane) and thus reduces |
| 749 |
// chances or hidden geometry. |
| 750 |
constexpr float orthographicFocalDistance = 250; |
| 751 |
#else |
| 752 |
constexpr float orthographicFocalDistance = 1; |
| 753 |
#endif |
| 754 |
camera->position = getFocalPoint() - orthographicFocalDistance * direction; |
| 755 |
camera->focalDistance = orthographicFocalDistance; |
| 756 |
} |
| 757 |
|
| 758 |
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403) |
| 759 |
// Fix issue with near clipping in orthogonal view |
| 760 |
if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) { |
| 761 |
|
| 762 |
// The center of the bounding sphere in camera coordinate system |
| 763 |
SbVec3f center; |
| 764 |
camera->orientation.getValue().inverse().multVec( |
| 765 |
boundingSphere.getCenter() - camera->position.getValue(), |
| 766 |
center |
| 767 |
); |
| 768 |
|
| 769 |
SbVec3f dir; |
| 770 |
camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), dir); |
| 771 |
|
| 772 |
// Reposition the camera but keep the focal point the same |
| 773 |
// nearDistance is 0 and farDistance is the diameter of the bounding sphere |
| 774 |
float repositionDistance = -center.getValue()[2] - boundingSphere.getRadius(); |
| 775 |
camera->position = camera->position.getValue() + repositionDistance * dir; |
| 776 |
camera->nearDistance = 0; |
| 777 |
camera->farDistance = 2 * boundingSphere.getRadius() + 1; |
| 778 |
camera->focalDistance = camera->focalDistance.getValue() - repositionDistance; |
| 779 |
} |
| 780 |
#endif |
| 781 |
} |
| 782 |
|
| 783 |
void NavigationStyle::panCamera( |
| 784 |
SoCamera* cam, |
| 785 |
float aspectratio, |
| 786 |
const SbPlane& panplane, |
| 787 |
const SbVec2f& currpos, |
| 788 |
const SbVec2f& prevpos |
| 789 |
) |
| 790 |
{ |
| 791 |
if (!cam) { // can happen for empty scenegraph |
| 792 |
return; |
| 793 |
} |
| 794 |
if (currpos == prevpos) { // useless invocation |
| 795 |
return; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
// Find projection points for the last and current mouse coordinates. |
| 800 |
SbViewVolume vv = cam->getViewVolume(aspectratio); |
| 801 |
|
| 802 |
// See note in Coin docs for SoCamera::getViewVolume re:viewport mapping |
| 803 |
if (aspectratio < 1.0) { |
| 804 |
vv.scale(1.0 / aspectratio); |
| 805 |
} |
| 806 |
|
| 807 |
SbLine line; |
| 808 |
vv.projectPointToLine(currpos, line); |
| 809 |
SbVec3f current_planept; |
| 810 |
panplane.intersect(line, current_planept); |
| 811 |
vv.projectPointToLine(prevpos, line); |
| 812 |
SbVec3f old_planept; |
| 813 |
panplane.intersect(line, old_planept); |
| 814 |
|
| 815 |
// Reposition camera according to the vector difference between the |
| 816 |
// projected points. |
| 817 |
cam->position = cam->position.getValue() - (current_planept - old_planept); |
| 818 |
|
| 819 |
if (this->currentmode != NavigationStyle::IDLE) { |
| 820 |
hasPanned = true; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
void NavigationStyle::setupPanningPlane(const SoCamera* camera) |
| 825 |
{ |
| 826 |
// The plane we're projecting the mouse coordinates to get 3D |
| 827 |
// coordinates should stay the same during the whole pan |
| 828 |
// operation, so we should calculate this value here. |
| 829 |
if (!camera) { // can happen for empty scenegraph |
| 830 |
this->panningplane = SbPlane(SbVec3f(0, 0, 1), 0); |
| 831 |
} |
| 832 |
else { |
| 833 |
const SbViewportRegion& vp = viewer->getViewportRegion(); |
| 834 |
const float aspectratio = vp.getViewportAspectRatio(); |
| 835 |
SbViewVolume vv = camera->getViewVolume(aspectratio); |
| 836 |
|
| 837 |
// See note in Coin docs for SoCamera::getViewVolume re:viewport mapping |
| 838 |
if (aspectratio < 1.0) { |
| 839 |
vv.scale(1.0 / aspectratio); |
| 840 |
} |
| 841 |
|
| 842 |
this->panningplane = vv.getPlane(camera->focalDistance.getValue()); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
/** Dependent on the camera type this will either shrink or expand the |
| 847 |
* height of the viewport (orthogonal camera) or move the camera |
| 848 |
* closer or further away from the focal point in the scene. |
| 849 |
*/ |
| 850 |
void NavigationStyle::zoom(SoCamera* cam, float diffvalue) |
| 851 |
{ |
| 852 |
if (!cam) { // can happen for empty scenegraph |
| 853 |
return; |
| 854 |
} |
| 855 |
|
| 856 |
animator->stop(); |
| 857 |
|
| 858 |
SoType t = cam->getTypeId(); |
| 859 |
SbName tname = t.getName(); |
| 860 |
|
| 861 |
// This will be in the range of <0, ->>. |
| 862 |
auto multiplicator = float(exp(diffvalue)); |
| 863 |
|
| 864 |
if (t.isDerivedFrom(SoOrthographicCamera::getClassTypeId())) { |
| 865 |
|
| 866 |
// Since there's no perspective, "zooming" in the original sense |
| 867 |
// of the word won't have any visible effect. So we just increase |
| 868 |
// or decrease the field-of-view values of the camera instead, to |
| 869 |
// "shrink" the projection size of the model / scene. |
| 870 |
|
| 871 |
auto oc = static_cast<SoOrthographicCamera*>(cam); |
| 872 |
oc->height = oc->height.getValue() * multiplicator; |
| 873 |
} |
| 874 |
else { |
| 875 |
// FrustumCamera can be found in the SmallChange CVS module (it's |
| 876 |
// a camera that lets you specify (for instance) an off-center |
| 877 |
// frustum (similar to glFrustum()) |
| 878 |
if (!t.isDerivedFrom(SoPerspectiveCamera::getClassTypeId()) && tname != "FrustumCamera") { |
| 879 |
#ifdef FC_DEBUG |
| 880 |
static SbBool first = true; |
| 881 |
if (first) { |
| 882 |
SoDebugError::postWarning( |
| 883 |
"NavigationStyle::zoom", |
| 884 |
"Unknown camera type, " |
| 885 |
"will zoom by moving position, " |
| 886 |
"but this might not be correct." |
| 887 |
); |
| 888 |
first = false; |
| 889 |
} |
| 890 |
#endif |
| 891 |
} |
| 892 |
|
| 893 |
const float oldfocaldist = cam->focalDistance.getValue(); |
| 894 |
const float newfocaldist = oldfocaldist * multiplicator; |
| 895 |
|
| 896 |
SbVec3f direction; |
| 897 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 898 |
|
| 899 |
const SbVec3f oldpos = cam->position.getValue(); |
| 900 |
const SbVec3f newpos = oldpos + (newfocaldist - oldfocaldist) * -direction; |
| 901 |
|
| 902 |
// This catches a rather common user interface "buglet": if the |
| 903 |
// user zooms the camera out to a distance from origo larger than |
| 904 |
// what we still can safely do floating point calculations on |
| 905 |
// (i.e. without getting NaN or Inf values), the faulty floating |
| 906 |
// point values will propagate until we start to get debug error |
| 907 |
// messages and eventually an assert failure from core Coin code. |
| 908 |
// |
| 909 |
// With the below bounds check, this problem is avoided. |
| 910 |
// |
| 911 |
// (But note that we depend on the input argument ''diffvalue'' to |
| 912 |
// be small enough that zooming happens gradually. Ideally, we |
| 913 |
// should also check distorigo with isinf() and isnan() (or |
| 914 |
// inversely; isinfite()), but those only became standardized with |
| 915 |
// C99.) |
| 916 |
const float distorigo = newpos.length(); |
| 917 |
// sqrt(FLT_MAX) == ~ 1e+19, which should be both safe for further |
| 918 |
// calculations and ok for the end-user and app-programmer. |
| 919 |
float maxDistance = std::sqrt(std::numeric_limits<float>::max()); |
| 920 |
if (distorigo > maxDistance) { |
| 921 |
// do nothing here |
| 922 |
} |
| 923 |
else { |
| 924 |
cam->position = newpos; |
| 925 |
cam->focalDistance = newfocaldist; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
if (this->currentmode != NavigationStyle::IDLE) { |
| 930 |
hasZoomed = true; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
// Calculate a zoom/dolly factor from the difference of the current |
| 935 |
// cursor position and the last. |
| 936 |
void NavigationStyle::zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos) |
| 937 |
{ |
| 938 |
// There is no "geometrically correct" value, 20 just seems to give |
| 939 |
// about the right "feel". |
| 940 |
float value = (thispos[1] - prevpos[1]) * 10.0f /*20.0f*/; |
| 941 |
if (this->invertZoom) { |
| 942 |
value = -value; |
| 943 |
} |
| 944 |
zoom(viewer->getSoRenderManager()->getCamera(), value); |
| 945 |
} |
| 946 |
|
| 947 |
void NavigationStyle::zoomIn() |
| 948 |
{ |
| 949 |
zoom(viewer->getSoRenderManager()->getCamera(), -this->zoomStep); |
| 950 |
} |
| 951 |
|
| 952 |
void NavigationStyle::zoomOut() |
| 953 |
{ |
| 954 |
zoom(viewer->getSoRenderManager()->getCamera(), this->zoomStep); |
| 955 |
} |
| 956 |
|
| 957 |
/*! |
| 958 |
* Returns the steps if the mouse wheel is rotated |
| 959 |
*/ |
| 960 |
int NavigationStyle::getDelta() const |
| 961 |
{ |
| 962 |
return 120; |
| 963 |
} |
| 964 |
|
| 965 |
void NavigationStyle::doZoom(SoCamera* camera, int wheeldelta, const SbVec2f& pos) |
| 966 |
{ |
| 967 |
float value = this->zoomStep * wheeldelta / float(getDelta()); |
| 968 |
if (this->invertZoom) { |
| 969 |
value = -value; |
| 970 |
} |
| 971 |
doZoom(camera, value, pos); |
| 972 |
} |
| 973 |
|
| 974 |
/*! |
| 975 |
*\brief NavigationStyle::doZoom Zooms in or out by specified factor, keeping the point on screen |
| 976 |
* specified by parameter pos fixed or not according to user preference |
| 977 |
* (NavigationStyle::zoomAtCursor). Ignores invertZoom user preference. |
| 978 |
*/ |
| 979 |
void NavigationStyle::doZoom(SoCamera* camera, float logfactor, const SbVec2f& pos) |
| 980 |
{ |
| 981 |
// something is asking for big zoom factor. This func is made for interactive zooming, |
| 982 |
// where the changes are per mouse move and thus are small. |
| 983 |
if (fabs(logfactor) > 4.0) { |
| 984 |
return; |
| 985 |
} |
| 986 |
SbBool zoomAtCur = this->zoomAtCursor; |
| 987 |
if (zoomAtCur) { |
| 988 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 989 |
float ratio = vp.getViewportAspectRatio(); |
| 990 |
SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio()); |
| 991 |
SbPlane panplane = vv.getPlane(camera->focalDistance.getValue()); |
| 992 |
panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, SbVec2f(0.5, 0.5), pos); |
| 993 |
} |
| 994 |
|
| 995 |
zoom(camera, logfactor); |
| 996 |
|
| 997 |
if (zoomAtCur) { |
| 998 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 999 |
float ratio = vp.getViewportAspectRatio(); |
| 1000 |
SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio()); |
| 1001 |
SbPlane panplane = vv.getPlane(camera->focalDistance.getValue()); |
| 1002 |
panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, pos, SbVec2f(0.5, 0.5)); |
| 1003 |
|
| 1004 |
// Change the position of the rotation center indicator after zooming at cursor |
| 1005 |
// Rotation mode is WindowCenter |
| 1006 |
if (!rotationCenterMode) { |
| 1007 |
viewer->changeRotationCenterPosition(getFocalPoint()); |
| 1008 |
|
| 1009 |
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403) |
| 1010 |
findBoundingSphere(); |
| 1011 |
#endif |
| 1012 |
} |
| 1013 |
} |
| 1014 |
} |
| 1015 |
void NavigationStyle::doScale(SoCamera* cam, float factor) |
| 1016 |
{ |
| 1017 |
if (cam->getTypeId() == SoOrthographicCamera::getClassTypeId()) { |
| 1018 |
float height = static_cast<SoOrthographicCamera*>(cam)->height.getValue() * factor; |
| 1019 |
static_cast<SoOrthographicCamera*>(cam)->height = height; |
| 1020 |
} |
| 1021 |
else if (cam->getTypeId() == SoPerspectiveCamera::getClassTypeId()) { |
| 1022 |
float height = static_cast<SoPerspectiveCamera*>(cam)->heightAngle.getValue() / 2.0f; |
| 1023 |
height = 2.0f * atan(tan(height) * factor); |
| 1024 |
static_cast<SoPerspectiveCamera*>(cam)->heightAngle = height; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
void NavigationStyle::doRotate(SoCamera* camera, float angle, const SbVec2f& pos) |
| 1028 |
{ |
| 1029 |
SbBool zoomAtCur = this->zoomAtCursor; |
| 1030 |
if (zoomAtCur) { |
| 1031 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1032 |
float ratio = vp.getViewportAspectRatio(); |
| 1033 |
SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio()); |
| 1034 |
SbPlane panplane = vv.getPlane(camera->focalDistance.getValue()); |
| 1035 |
panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, SbVec2f(0.5, 0.5), pos); |
| 1036 |
} |
| 1037 |
|
| 1038 |
SbRotation rotcam = camera->orientation.getValue(); |
| 1039 |
// get view direction |
| 1040 |
SbVec3f vdir; |
| 1041 |
rotcam.multVec(SbVec3f(0, 0, -1), vdir); |
| 1042 |
// rotate |
| 1043 |
SbRotation drot(vdir, angle); |
| 1044 |
camera->orientation.setValue(rotcam * drot); |
| 1045 |
|
| 1046 |
if (zoomAtCur) { |
| 1047 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1048 |
float ratio = vp.getViewportAspectRatio(); |
| 1049 |
SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio()); |
| 1050 |
SbPlane panplane = vv.getPlane(camera->focalDistance.getValue()); |
| 1051 |
panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, pos, SbVec2f(0.5, 0.5)); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
SbVec3f NavigationStyle::getRotationCenter(SbBool& found) const |
| 1056 |
{ |
| 1057 |
found = this->rotationCenterFound; |
| 1058 |
return this->rotationCenter; |
| 1059 |
} |
| 1060 |
|
| 1061 |
std::optional<SbVec2s>& NavigationStyle::getRightClickPosition() |
| 1062 |
{ |
| 1063 |
return rightClickPosition; |
| 1064 |
} |
| 1065 |
|
| 1066 |
void NavigationStyle::setRotationCenter(const SbVec3f& cnt) |
| 1067 |
{ |
| 1068 |
this->rotationCenter = cnt; |
| 1069 |
this->rotationCenterFound = true; |
| 1070 |
|
| 1071 |
const auto camera = getCamera(); |
| 1072 |
if (camera->isOfType(SoPerspectiveCamera::getClassTypeId())) { |
| 1073 |
SbVec3f direction; |
| 1074 |
camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 1075 |
|
| 1076 |
// Calculate distance from camera to rotation center |
| 1077 |
const auto rotationCenterDistance = rotationCenter - camera->position.getValue(); |
| 1078 |
const auto rotationCenterDepth = rotationCenterDistance.dot(direction); |
| 1079 |
|
| 1080 |
// Set focal distance to match rotation center depth so we can zoom at the new rotation |
| 1081 |
// center with a perspective camera |
| 1082 |
camera->focalDistance.setValue(rotationCenterDepth); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
SbVec3f NavigationStyle::getFocalPoint() const |
| 1087 |
{ |
| 1088 |
SoCamera* cam = viewer->getSoRenderManager()->getCamera(); |
| 1089 |
if (!cam) { |
| 1090 |
return {0, 0, 0}; |
| 1091 |
} |
| 1092 |
|
| 1093 |
// Find global coordinates of focal point. |
| 1094 |
SbVec3f direction; |
| 1095 |
cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction); |
| 1096 |
SbVec3f focal = cam->position.getValue() + cam->focalDistance.getValue() * direction; |
| 1097 |
return focal; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** Uses the sphere sheet projector to map the mouse position onto |
| 1101 |
* a 3D point and find a rotation from this and the last calculated point. |
| 1102 |
*/ |
| 1103 |
void NavigationStyle::spin(const SbVec2f& pointerpos) |
| 1104 |
{ |
| 1105 |
if (this->log.historysize < 2) { |
| 1106 |
return; |
| 1107 |
} |
| 1108 |
assert(this->spinprojector); |
| 1109 |
|
| 1110 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1111 |
SbVec2s glsize(vp.getViewportSizePixels()); |
| 1112 |
SbVec2f lastpos; |
| 1113 |
lastpos[0] = float(this->log.position[1][0]) / float(std::max((int)(glsize[0] - 1), 1)); |
| 1114 |
lastpos[1] = float(this->log.position[1][1]) / float(std::max((int)(glsize[1] - 1), 1)); |
| 1115 |
|
| 1116 |
if (getOrbitStyle() == FreeTurntable) { |
| 1117 |
SbVec2f midpos(lastpos[0], pointerpos[1]); |
| 1118 |
spinInternal(pointerpos, midpos); |
| 1119 |
spinInternal(midpos, lastpos); |
| 1120 |
} |
| 1121 |
else { |
| 1122 |
spinInternal(pointerpos, lastpos); |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (this->currentmode != NavigationStyle::IDLE) { |
| 1126 |
hasDragged = true; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
void NavigationStyle::spinInternal(const SbVec2f& pointerpos, const SbVec2f& lastpos) |
| 1131 |
{ |
| 1132 |
float sensitivity = getSensitivity(); |
| 1133 |
|
| 1134 |
// Adjust the spin projector sphere to the screen position of the rotation center when the mouse |
| 1135 |
// intersects an object |
| 1136 |
if ((getOrbitStyle() == Trackball || getOrbitStyle() == TrackballClassic |
| 1137 |
|| getOrbitStyle() == RoundedArcball) |
| 1138 |
&& rotationCenterMode & RotationCenterMode::ScenePointAtCursor && rotationCenterFound |
| 1139 |
&& rotationCenterIsScenePointAtCursor) { |
| 1140 |
const auto pointOnScreen = viewer->getPointOnViewport(rotationCenter); |
| 1141 |
const auto sphereCenter = 2 * normalizePixelPos(pointOnScreen) - SbVec2f {1, 1}; |
| 1142 |
|
| 1143 |
float x, y; |
| 1144 |
sphereCenter.getValue(x, y); |
| 1145 |
|
| 1146 |
const float sphereScale = 1 + sphereCenter.length(); |
| 1147 |
const float radius = FCSphereSheetProjector::defaultSphereRadius * sphereScale; |
| 1148 |
sensitivity *= sphereScale; |
| 1149 |
|
| 1150 |
spinprojector->setSphere(SbSphere {SbVec3f {x, y, 0}, radius}); |
| 1151 |
} |
| 1152 |
else { |
| 1153 |
spinprojector->setSphere( |
| 1154 |
SbSphere {SbVec3f {0, 0, 0}, FCSphereSheetProjector::defaultSphereRadius} |
| 1155 |
); |
| 1156 |
} |
| 1157 |
|
| 1158 |
// 0000333: Turntable camera rotation |
| 1159 |
SbMatrix mat; |
| 1160 |
viewer->getSoRenderManager()->getCamera()->orientation.getValue().getValue(mat); |
| 1161 |
this->spinprojector->setWorkingSpace(mat); |
| 1162 |
|
| 1163 |
this->spinprojector->project(lastpos); |
| 1164 |
SbRotation r; |
| 1165 |
this->spinprojector->projectAndGetRotation(pointerpos, r); |
| 1166 |
if (sensitivity > 1.0f) { |
| 1167 |
SbVec3f axis; |
| 1168 |
float radians {}; |
| 1169 |
r.getValue(axis, radians); |
| 1170 |
radians = sensitivity * radians; |
| 1171 |
r.setValue(axis, radians); |
| 1172 |
} |
| 1173 |
r.invert(); |
| 1174 |
|
| 1175 |
if (this->rotationCenterMode && this->rotationCenterFound) { |
| 1176 |
this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r, rotationCenter); |
| 1177 |
} |
| 1178 |
else { |
| 1179 |
this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r); |
| 1180 |
} |
| 1181 |
|
| 1182 |
// Calculate an average angle magnitude value to make the transition |
| 1183 |
// to a possible spin animation mode appear smooth. |
| 1184 |
|
| 1185 |
SbVec3f dummy_axis, newaxis; |
| 1186 |
float acc_angle {}, newangle {}; |
| 1187 |
this->spinincrement.getValue(dummy_axis, acc_angle); |
| 1188 |
acc_angle *= this->spinsamplecounter; // weight |
| 1189 |
r.getValue(newaxis, newangle); |
| 1190 |
acc_angle += newangle; |
| 1191 |
|
| 1192 |
this->spinsamplecounter++; |
| 1193 |
acc_angle /= this->spinsamplecounter; |
| 1194 |
// FIXME: accumulate and average axis vectors as well? 19990501 mortene. |
| 1195 |
this->spinincrement.setValue(newaxis, acc_angle); |
| 1196 |
|
| 1197 |
// Don't carry too much baggage, as that'll give unwanted results |
| 1198 |
// when the user quickly trigger (as in "click-drag-release") a spin |
| 1199 |
// animation. |
| 1200 |
if (this->spinsamplecounter > 3) { |
| 1201 |
this->spinsamplecounter = 3; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
/*! |
| 1206 |
* \brief NavigationStyle::spin_simplified is a simplified version of |
| 1207 |
* NavigationStyle::spin(..), which uses less global variables. Doesn't support |
| 1208 |
* starting an animated spinning. |
| 1209 |
* |
| 1210 |
* \param cam the camera to affect. The rotation amount is determined by delta |
| 1211 |
* (curpos-prevpos), and rotation axis is also affected by average pos. |
| 1212 |
* \param curpos current normalized position or mouse pointer |
| 1213 |
* \param prevpos previous normalized position of mouse pointer |
| 1214 |
*/ |
| 1215 |
void NavigationStyle::spin_simplified(SbVec2f curpos, SbVec2f prevpos) |
| 1216 |
{ |
| 1217 |
assert(this->spinprojector); |
| 1218 |
|
| 1219 |
if (getOrbitStyle() == FreeTurntable) { |
| 1220 |
SbVec2f midpos(prevpos[0], curpos[1]); |
| 1221 |
spinSimplifiedInternal(curpos, midpos); |
| 1222 |
spinSimplifiedInternal(midpos, prevpos); |
| 1223 |
} |
| 1224 |
else { |
| 1225 |
spinSimplifiedInternal(curpos, prevpos); |
| 1226 |
} |
| 1227 |
|
| 1228 |
hasDragged = true; |
| 1229 |
} |
| 1230 |
|
| 1231 |
void NavigationStyle::spinSimplifiedInternal(SbVec2f curpos, SbVec2f prevpos) |
| 1232 |
{ |
| 1233 |
// 0000333: Turntable camera rotation |
| 1234 |
SbMatrix mat; |
| 1235 |
viewer->getSoRenderManager()->getCamera()->orientation.getValue().getValue(mat); |
| 1236 |
this->spinprojector->setWorkingSpace(mat); |
| 1237 |
|
| 1238 |
this->spinprojector->project(prevpos); |
| 1239 |
SbRotation r; |
| 1240 |
this->spinprojector->projectAndGetRotation(curpos, r); |
| 1241 |
float sensitivity = getSensitivity(); |
| 1242 |
if (sensitivity > 1.0f) { |
| 1243 |
SbVec3f axis; |
| 1244 |
float radians {}; |
| 1245 |
r.getValue(axis, radians); |
| 1246 |
radians = sensitivity * radians; |
| 1247 |
r.setValue(axis, radians); |
| 1248 |
} |
| 1249 |
r.invert(); |
| 1250 |
|
| 1251 |
if (this->rotationCenterMode && this->rotationCenterFound) { |
| 1252 |
this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r, rotationCenter); |
| 1253 |
} |
| 1254 |
else { |
| 1255 |
this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
SbBool NavigationStyle::doSpin() |
| 1260 |
{ |
| 1261 |
if (this->log.historysize >= 3) { |
| 1262 |
SbTime stoptime = (SbTime::getTimeOfDay() - this->log.time[0]); |
| 1263 |
if (isSpinningAnimationEnabled() && stoptime.getValue() < 0.100) { |
| 1264 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1265 |
const SbVec2s glsize(vp.getViewportSizePixels()); |
| 1266 |
SbVec3f from = this->spinprojector->project(SbVec2f( |
| 1267 |
float(this->log.position[2][0]) / float(std::max(glsize[0] - 1, 1)), |
| 1268 |
float(this->log.position[2][1]) / float(std::max(glsize[1] - 1, 1)) |
| 1269 |
)); |
| 1270 |
SbVec3f to = this->spinprojector->project(this->lastmouseposition); |
| 1271 |
SbRotation rot = this->spinprojector->getRotation(from, to); |
| 1272 |
|
| 1273 |
SbTime delta = (this->log.time[0] - this->log.time[2]); |
| 1274 |
double deltatime = delta.getValue(); |
| 1275 |
rot.invert(); |
| 1276 |
rot.scaleAngle(float(0.200 / deltatime)); |
| 1277 |
|
| 1278 |
SbVec3f axis; |
| 1279 |
float radians {}; |
| 1280 |
rot.getValue(axis, radians); |
| 1281 |
if ((radians > 0.01f) && (deltatime < 0.300)) { |
| 1282 |
viewer->startSpinningAnimation(axis, radians * 5); |
| 1283 |
return true; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
return false; |
| 1289 |
} |
| 1290 |
|
| 1291 |
void NavigationStyle::saveCursorPosition(const SoEvent* const ev) |
| 1292 |
{ |
| 1293 |
this->globalPos.setValue(QCursor::pos().x(), QCursor::pos().y()); |
| 1294 |
this->localPos = ev->getPosition(); |
| 1295 |
rotationCenterIsScenePointAtCursor = false; |
| 1296 |
|
| 1297 |
// mode is WindowCenter |
| 1298 |
if (!this->rotationCenterMode) { |
| 1299 |
setRotationCenter(getFocalPoint()); |
| 1300 |
} |
| 1301 |
|
| 1302 |
// Option to get point on model (slow) or always on focal plane (fast) |
| 1303 |
// |
| 1304 |
// mode is ScenePointAtCursor to get exact point if possible |
| 1305 |
if (this->rotationCenterMode & NavigationStyle::RotationCenterMode::ScenePointAtCursor) { |
| 1306 |
SoRayPickAction rpaction(viewer->getSoRenderManager()->getViewportRegion()); |
| 1307 |
rpaction.setPoint(this->localPos); |
| 1308 |
rpaction.setRadius(viewer->getPickRadius()); |
| 1309 |
rpaction.apply(viewer->getSoRenderManager()->getSceneGraph()); |
| 1310 |
|
| 1311 |
SoPickedPoint* picked = rpaction.getPickedPoint(); |
| 1312 |
if (picked) { |
| 1313 |
setRotationCenter(picked->getPoint()); |
| 1314 |
rotationCenterIsScenePointAtCursor = true; |
| 1315 |
return; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
// mode is FocalPointAtCursor or a ScenePointAtCursor failed |
| 1320 |
if (this->rotationCenterMode & NavigationStyle::RotationCenterMode::FocalPointAtCursor) { |
| 1321 |
// get the intersection point of the ray and the focal plane |
| 1322 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1323 |
float ratio = vp.getViewportAspectRatio(); |
| 1324 |
|
| 1325 |
SoCamera* cam = viewer->getSoRenderManager()->getCamera(); |
| 1326 |
if (!cam) { // no camera |
| 1327 |
return; |
| 1328 |
} |
| 1329 |
SbViewVolume vv = cam->getViewVolume(ratio); |
| 1330 |
|
| 1331 |
SbLine line; |
| 1332 |
SbVec2f currpos = ev->getNormalizedPosition(vp); |
| 1333 |
vv.projectPointToLine(currpos, line); |
| 1334 |
SbVec3f current_planept; |
| 1335 |
SbPlane panplane = vv.getPlane(cam->focalDistance.getValue()); |
| 1336 |
panplane.intersect(line, current_planept); |
| 1337 |
|
| 1338 |
setRotationCenter(current_planept); |
| 1339 |
} |
| 1340 |
|
| 1341 |
// mode is BoundingBoxCenter or a ScenePointAtCursor failed |
| 1342 |
if (this->rotationCenterMode & NavigationStyle::RotationCenterMode::BoundingBoxCenter) { |
| 1343 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1344 |
float ratio = vp.getViewportAspectRatio(); |
| 1345 |
|
| 1346 |
SoCamera* cam = viewer->getSoRenderManager()->getCamera(); |
| 1347 |
if (!cam) { // no camera |
| 1348 |
return; |
| 1349 |
} |
| 1350 |
|
| 1351 |
// Get the bounding box center of the physical object group |
| 1352 |
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion()); |
| 1353 |
action.apply(viewer->objectGroup); |
| 1354 |
SbBox3f boundingBox = action.getBoundingBox(); |
| 1355 |
SbVec3f boundingBoxCenter = boundingBox.getCenter(); |
| 1356 |
setRotationCenter(boundingBoxCenter); |
| 1357 |
|
| 1358 |
// To drag around the center point of the bbox we have to determine |
| 1359 |
// its projection on the screen because this information is used in |
| 1360 |
// NavigationStyle::spin() for the panning |
| 1361 |
SbViewVolume vv = cam->getViewVolume(ratio); |
| 1362 |
vv.projectToScreen(boundingBoxCenter, boundingBoxCenter); |
| 1363 |
SbVec2s size = vp.getViewportSizePixels(); |
| 1364 |
auto tox = static_cast<short>(boundingBoxCenter[0] * size[0]); |
| 1365 |
auto toy = static_cast<short>(boundingBoxCenter[1] * size[1]); |
| 1366 |
this->localPos.setValue(tox, toy); |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
SbVec2f NavigationStyle::normalizePixelPos(SbVec2s pixpos) |
| 1371 |
{ |
| 1372 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1373 |
const SbVec2s size(vp.getViewportSizePixels()); |
| 1374 |
return { |
| 1375 |
(float)pixpos[0] / (float)std::max((int)(size[0] - 1), 1), |
| 1376 |
(float)pixpos[1] / (float)std::max((int)(size[1] - 1), 1) |
| 1377 |
}; |
| 1378 |
} |
| 1379 |
|
| 1380 |
SbVec2f NavigationStyle::normalizePixelPos(SbVec2f pixpos) |
| 1381 |
{ |
| 1382 |
const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion(); |
| 1383 |
const SbVec2s size(vp.getViewportSizePixels()); |
| 1384 |
return { |
| 1385 |
pixpos[0] / (float)std::max((int)(size[0] - 1), 1), |
| 1386 |
pixpos[1] / (float)std::max((int)(size[1] - 1), 1) |
| 1387 |
}; |
| 1388 |
} |
| 1389 |
|
| 1390 |
void NavigationStyle::moveCursorPosition() |
| 1391 |
{ |
| 1392 |
if (!isResetCursorPosition()) { |
| 1393 |
return; |
| 1394 |
} |
| 1395 |
|
| 1396 |
QPoint cpos = QCursor::pos(); |
| 1397 |
if (abs(cpos.x() - globalPos[0]) > 10 || abs(cpos.y() - globalPos[1]) > 10) { |
| 1398 |
QCursor::setPos(globalPos[0], globalPos[1] - 1); |
| 1399 |
this->log.position[0] = localPos; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
SbBool NavigationStyle::handleEventInForeground(const SoEvent* const e) |
| 1405 |
{ |
| 1406 |
SoHandleEventAction action(viewer->getSoRenderManager()->getViewportRegion()); |
| 1407 |
action.setEvent(e); |
| 1408 |
action.setPickRadius(viewer->getPickRadius()); |
| 1409 |
action.apply(viewer->foregroundroot); |
| 1410 |
return action.isHandled(); |
| 1411 |
} |
| 1412 |
|
| 1413 |
/** |
| 1414 |
* @brief Decide if it should be possible to start any animation |
| 1415 |
* |
| 1416 |
* If the enable flag is false and we're currently animating, the animation will be stopped |
| 1417 |
*/ |
| 1418 |
void NavigationStyle::setAnimationEnabled(const SbBool enable) |
| 1419 |
{ |
| 1420 |
animationEnabled = enable; |
| 1421 |
if (!enable && isAnimating()) { |
| 1422 |
animator->stop(); |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
/** |
| 1427 |
* @brief Decide if it should be possible to start a spin animation of the model in the viewer by |
| 1428 |
* releasing the mouse button while dragging |
| 1429 |
* |
| 1430 |
* If the enable flag is false and we're currently animating, the spin animation will be stopped |
| 1431 |
*/ |
| 1432 |
void NavigationStyle::setSpinningAnimationEnabled(const SbBool enable) |
| 1433 |
{ |
| 1434 |
spinningAnimationEnabled = enable; |
| 1435 |
if (!enable && isSpinning()) { |
| 1436 |
animator->stop(); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
/** |
| 1441 |
* @return Whether or not it is possible to start any animation |
| 1442 |
*/ |
| 1443 |
SbBool NavigationStyle::isAnimationEnabled() const |
| 1444 |
{ |
| 1445 |
return animationEnabled; |
| 1446 |
} |
| 1447 |
|
| 1448 |
/** |
| 1449 |
* @return Whether or not it is possible to start a spinning animation e.g. after dragging |
| 1450 |
*/ |
| 1451 |
SbBool NavigationStyle::isSpinningAnimationEnabled() const |
| 1452 |
{ |
| 1453 |
return animationEnabled && spinningAnimationEnabled; |
| 1454 |
} |
| 1455 |
|
| 1456 |
/** |
| 1457 |
* @return Whether or not any animation is currently active |
| 1458 |
*/ |
| 1459 |
SbBool NavigationStyle::isAnimating() const |
| 1460 |
{ |
| 1461 |
return animator->isAnimating(); |
| 1462 |
} |
| 1463 |
|
| 1464 |
/** |
| 1465 |
* @return Whether or not a spinning animation is currently active e.g. after a user drag |
| 1466 |
*/ |
| 1467 |
SbBool NavigationStyle::isSpinning() const |
| 1468 |
{ |
| 1469 |
return currentmode == NavigationStyle::SPINNING; |
| 1470 |
} |
| 1471 |
|
| 1472 |
void NavigationStyle::startAnimating(const std::shared_ptr<NavigationAnimation>& animation, bool wait) const |
| 1473 |
{ |
| 1474 |
if (wait) { |
| 1475 |
animator->startAndWait(animation); |
| 1476 |
} |
| 1477 |
else { |
| 1478 |
animator->start(animation); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
void NavigationStyle::stopAnimating() const |
| 1483 |
{ |
| 1484 |
animator->stop(); |
| 1485 |
} |
| 1486 |
|
| 1487 |
void NavigationStyle::setSensitivity(float val) |
| 1488 |
{ |
| 1489 |
this->sensitivity = val; |
| 1490 |
} |
| 1491 |
|
| 1492 |
float NavigationStyle::getSensitivity() const |
| 1493 |
{ |
| 1494 |
return this->sensitivity; |
| 1495 |
} |
| 1496 |
|
| 1497 |
void NavigationStyle::setResetCursorPosition(SbBool on) |
| 1498 |
{ |
| 1499 |
this->resetcursorpos = on; |
| 1500 |
} |
| 1501 |
|
| 1502 |
SbBool NavigationStyle::isResetCursorPosition() const |
| 1503 |
{ |
| 1504 |
return this->resetcursorpos; |
| 1505 |
} |
| 1506 |
|
| 1507 |
void NavigationStyle::setZoomInverted(SbBool on) |
| 1508 |
{ |
| 1509 |
this->invertZoom = on; |
| 1510 |
} |
| 1511 |
|
| 1512 |
SbBool NavigationStyle::isZoomInverted() const |
| 1513 |
{ |
| 1514 |
return this->invertZoom; |
| 1515 |
} |
| 1516 |
|
| 1517 |
void NavigationStyle::setZoomStep(float val) |
| 1518 |
{ |
| 1519 |
this->zoomStep = val; |
| 1520 |
} |
| 1521 |
|
| 1522 |
void NavigationStyle::setZoomAtCursor(SbBool on) |
| 1523 |
{ |
| 1524 |
this->zoomAtCursor = on; |
| 1525 |
} |
| 1526 |
|
| 1527 |
SbBool NavigationStyle::isZoomAtCursor() const |
| 1528 |
{ |
| 1529 |
return this->zoomAtCursor; |
| 1530 |
} |
| 1531 |
|
| 1532 |
void NavigationStyle::setRotationCenterMode(NavigationStyle::RotationCenterModes mode) |
| 1533 |
{ |
| 1534 |
this->rotationCenterMode = mode; |
| 1535 |
} |
| 1536 |
|
| 1537 |
NavigationStyle::RotationCenterModes NavigationStyle::getRotationCenterMode() const |
| 1538 |
{ |
| 1539 |
return this->rotationCenterMode; |
| 1540 |
} |
| 1541 |
|
| 1542 |
void NavigationStyle::startSelection(AbstractMouseSelection* mouse) |
| 1543 |
{ |
| 1544 |
if (!mouse) { |
| 1545 |
return; |
| 1546 |
} |
| 1547 |
|
| 1548 |
if (mouseSelection) { |
| 1549 |
SoDebugError::postWarning( |
| 1550 |
"NavigationStyle::startSelection", |
| 1551 |
"Set new mouse selection while an old is still active." |
| 1552 |
); |
| 1553 |
} |
| 1554 |
|
| 1555 |
mouseSelection = mouse; |
| 1556 |
mouseSelection->grabMouseModel(viewer); |
| 1557 |
} |
| 1558 |
|
| 1559 |
void NavigationStyle::startSelection(NavigationStyle::SelectionMode mode) |
| 1560 |
{ |
| 1561 |
if (mouseSelection) { |
| 1562 |
return; |
| 1563 |
} |
| 1564 |
if (isSelecting()) { |
| 1565 |
stopSelection(); |
| 1566 |
} |
| 1567 |
|
| 1568 |
switch (mode) { |
| 1569 |
case Lasso: |
| 1570 |
mouseSelection = new PolyPickerSelection(); |
| 1571 |
break; |
| 1572 |
case Rectangle: |
| 1573 |
mouseSelection = new RectangleSelection(); |
| 1574 |
break; |
| 1575 |
case Rubberband: |
| 1576 |
mouseSelection = new RubberbandSelection(); |
| 1577 |
break; |
| 1578 |
case BoxZoom: |
| 1579 |
mouseSelection = new BoxZoomSelection(); |
| 1580 |
break; |
| 1581 |
case Clip: |
| 1582 |
mouseSelection = new PolyClipSelection(); |
| 1583 |
break; |
| 1584 |
default: |
| 1585 |
break; |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (mouseSelection) { |
| 1589 |
mouseSelection->grabMouseModel(viewer); |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
void NavigationStyle::abortSelection() |
| 1594 |
{ |
| 1595 |
pcPolygon.clear(); |
| 1596 |
if (mouseSelection) { |
| 1597 |
mouseSelection->releaseMouseModel(true); |
| 1598 |
delete mouseSelection; |
| 1599 |
mouseSelection = nullptr; |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
void NavigationStyle::stopSelection() |
| 1604 |
{ |
| 1605 |
pcPolygon.clear(); |
| 1606 |
if (mouseSelection) { |
| 1607 |
mouseSelection->releaseMouseModel(); |
| 1608 |
delete mouseSelection; |
| 1609 |
mouseSelection = nullptr; |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
void NavigationStyle::resetButtonState() |
| 1614 |
{ |
| 1615 |
button1down = button2down = button3down = false; |
| 1616 |
setViewingMode(IDLE); |
| 1617 |
} |
| 1618 |
|
| 1619 |
SbBool NavigationStyle::isSelecting() const |
| 1620 |
{ |
| 1621 |
return (mouseSelection ? true : false); |
| 1622 |
} |
| 1623 |
|
| 1624 |
const std::vector<SbVec2s>& NavigationStyle::getPolygon(SelectionRole* role) const |
| 1625 |
{ |
| 1626 |
if (role) { |
| 1627 |
*role = this->selectedRole; |
| 1628 |
} |
| 1629 |
return pcPolygon; |
| 1630 |
} |
| 1631 |
|
| 1632 |
bool NavigationStyle::isDraggerUnderCursor(const SbVec2s pos) const |
| 1633 |
{ |
| 1634 |
SoRayPickAction rp(this->viewer->getSoRenderManager()->getViewportRegion()); |
| 1635 |
rp.setRadius(viewer->getPickRadius()); |
| 1636 |
rp.setPoint(pos); |
| 1637 |
rp.apply(this->viewer->getSoRenderManager()->getSceneGraph()); |
| 1638 |
SoPickedPoint* pick = rp.getPickedPoint(); |
| 1639 |
if (pick) { |
| 1640 |
const auto fullpath = static_cast<const SoFullPath*>(pick->getPath()); |
| 1641 |
for (int i = 0; i < fullpath->getLength(); ++i) { |
| 1642 |
if (fullpath->getNode(i)->isOfType(SoDragger::getClassTypeId())) { |
| 1643 |
return true; |
| 1644 |
} |
| 1645 |
} |
| 1646 |
return false; |
| 1647 |
} |
| 1648 |
return false; |
| 1649 |
} |
| 1650 |
|
| 1651 |
// This method adds another point to the mouse location log, used for spin |
| 1652 |
// animation calculations. |
| 1653 |
void NavigationStyle::addToLog(const SbVec2s pos, const SbTime time) |
| 1654 |
{ |
| 1655 |
// In case someone changes the const size setting at the top of this |
| 1656 |
// file too small. |
| 1657 |
assert(this->log.size > 2 && "mouse log too small!"); |
| 1658 |
|
| 1659 |
if (this->log.historysize > 0 && pos == this->log.position[0]) { |
| 1660 |
return; |
| 1661 |
} |
| 1662 |
|
| 1663 |
int lastidx = this->log.historysize; |
| 1664 |
// If we've filled up the log, we should throw away the last item: |
| 1665 |
if (lastidx == this->log.size) { |
| 1666 |
lastidx--; |
| 1667 |
} |
| 1668 |
|
| 1669 |
assert(lastidx < this->log.size); |
| 1670 |
for (int i = lastidx; i > 0; i--) { |
| 1671 |
this->log.position[i] = this->log.position[i - 1]; |
| 1672 |
this->log.time[i] = this->log.time[i - 1]; |
| 1673 |
} |
| 1674 |
|
| 1675 |
this->log.position[0] = pos; |
| 1676 |
this->log.time[0] = time; |
| 1677 |
if (this->log.historysize < this->log.size) { |
| 1678 |
this->log.historysize += 1; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
// This method "clears" the mouse location log, used for spin |
| 1683 |
// animation calculations. |
| 1684 |
void NavigationStyle::clearLog() |
| 1685 |
{ |
| 1686 |
this->log.historysize = 0; |
| 1687 |
} |
| 1688 |
|
| 1689 |
void NavigationStyle::syncModifierKeys(const SoEvent* const ev) |
| 1690 |
{ |
| 1691 |
// Mismatches in state of the modifier keys happens if the user |
| 1692 |
// presses or releases them outside the viewer window. |
| 1693 |
if (this->ctrldown != ev->wasCtrlDown()) { |
| 1694 |
this->ctrldown = ev->wasCtrlDown(); |
| 1695 |
} |
| 1696 |
if (this->shiftdown != ev->wasShiftDown()) { |
| 1697 |
this->shiftdown = ev->wasShiftDown(); |
| 1698 |
} |
| 1699 |
if (this->altdown != ev->wasAltDown()) { |
| 1700 |
this->altdown = ev->wasAltDown(); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
// The viewer is a state machine, and all changes to the current state |
| 1705 |
// are made through this call. |
| 1706 |
void NavigationStyle::setViewingMode(const ViewerMode newmode) |
| 1707 |
{ |
| 1708 |
const ViewerMode oldmode = this->currentmode; |
| 1709 |
|
| 1710 |
// Reset flags when changing from IDLE to another mode or if the mode is IDLE and the buttons |
| 1711 |
// are released |
| 1712 |
if ((oldmode == IDLE && newmode != IDLE) |
| 1713 |
|| (newmode == IDLE && !button1down && !button2down && !button3down)) { |
| 1714 |
hasPanned = false; |
| 1715 |
hasDragged = false; |
| 1716 |
hasZoomed = false; |
| 1717 |
} |
| 1718 |
|
| 1719 |
if (newmode == oldmode) { |
| 1720 |
|
| 1721 |
// The rotation center could have been changed even if the mode has not changed |
| 1722 |
if (newmode == NavigationStyle::DRAGGING && rotationCenterFound) { |
| 1723 |
viewer->changeRotationCenterPosition(rotationCenter); |
| 1724 |
} |
| 1725 |
|
| 1726 |
return; |
| 1727 |
} |
| 1728 |
|
| 1729 |
switch (newmode) { |
| 1730 |
case DRAGGING: |
| 1731 |
// Set up initial projection point for the projector object when |
| 1732 |
// first starting a drag operation. |
| 1733 |
animator->stop(); |
| 1734 |
viewer->showRotationCenter(true); |
| 1735 |
|
| 1736 |
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403) |
| 1737 |
findBoundingSphere(); |
| 1738 |
#endif |
| 1739 |
|
| 1740 |
this->spinprojector->project(this->lastmouseposition); |
| 1741 |
this->interactiveCountInc(); |
| 1742 |
this->clearLog(); |
| 1743 |
break; |
| 1744 |
|
| 1745 |
case SPINNING: |
| 1746 |
this->interactiveCountInc(); |
| 1747 |
viewer->getSoRenderManager()->scheduleRedraw(); |
| 1748 |
break; |
| 1749 |
|
| 1750 |
case PANNING: |
| 1751 |
animator->stop(); |
| 1752 |
setupPanningPlane(viewer->getSoRenderManager()->getCamera()); |
| 1753 |
this->interactiveCountInc(); |
| 1754 |
break; |
| 1755 |
|
| 1756 |
case ZOOMING: |
| 1757 |
animator->stop(); |
| 1758 |
this->interactiveCountInc(); |
| 1759 |
break; |
| 1760 |
|
| 1761 |
case BOXZOOM: |
| 1762 |
animator->stop(); |
| 1763 |
this->interactiveCountInc(); |
| 1764 |
break; |
| 1765 |
|
| 1766 |
default: // include default to avoid compiler warnings. |
| 1767 |
break; |
| 1768 |
} |
| 1769 |
|
| 1770 |
switch (oldmode) { |
| 1771 |
case SPINNING: |
| 1772 |
case DRAGGING: |
| 1773 |
viewer->showRotationCenter(false); |
| 1774 |
[[fallthrough]]; |
| 1775 |
case PANNING: |
| 1776 |
case ZOOMING: |
| 1777 |
case BOXZOOM: |
| 1778 |
this->interactiveCountDec(); |
| 1779 |
break; |
| 1780 |
|
| 1781 |
default: |
| 1782 |
break; |
| 1783 |
} |
| 1784 |
|
| 1785 |
viewer->setCursorRepresentation(newmode); |
| 1786 |
this->currentmode = newmode; |
| 1787 |
} |
| 1788 |
|
| 1789 |
int NavigationStyle::getViewingMode() const |
| 1790 |
{ |
| 1791 |
return (int)this->currentmode; |
| 1792 |
} |
| 1793 |
|
| 1794 |
SbBool NavigationStyle::processEvent(const SoEvent* const ev) |
| 1795 |
{ |
| 1796 |
// If we're in picking mode then all events must be redirected to the |
| 1797 |
// appropriate mouse model. |
| 1798 |
if (mouseSelection) { |
| 1799 |
int hd = mouseSelection->handleEvent(ev, viewer->getSoRenderManager()->getViewportRegion()); |
| 1800 |
if (hd == AbstractMouseSelection::Continue || hd == AbstractMouseSelection::Restart) { |
| 1801 |
return true; |
| 1802 |
} |
| 1803 |
else if (hd == AbstractMouseSelection::Finish) { |
| 1804 |
pcPolygon = mouseSelection->getPositions(); |
| 1805 |
selectedRole = mouseSelection->selectedRole(); |
| 1806 |
delete mouseSelection; |
| 1807 |
mouseSelection = nullptr; |
| 1808 |
syncWithEvent(ev); |
| 1809 |
return NavigationStyle::processSoEvent(ev); |
| 1810 |
} |
| 1811 |
else if (hd == AbstractMouseSelection::Cancel) { |
| 1812 |
pcPolygon.clear(); |
| 1813 |
delete mouseSelection; |
| 1814 |
mouseSelection = nullptr; |
| 1815 |
syncWithEvent(ev); |
| 1816 |
return NavigationStyle::processSoEvent(ev); |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
const ViewerMode curmode = this->currentmode; |
| 1821 |
|
| 1822 |
SbBool processed = false; |
| 1823 |
processed = this->processSoEvent(ev); |
| 1824 |
|
| 1825 |
// check for left click without selecting something |
| 1826 |
if ((curmode == NavigationStyle::SELECTION || curmode == NavigationStyle::IDLE) && !processed) { |
| 1827 |
if (SoMouseButtonEvent::isButtonReleaseEvent(ev, SoMouseButtonEvent::BUTTON1)) { |
| 1828 |
if (!ev->wasCtrlDown()) { |
| 1829 |
Gui::Selection().clearSelection(); |
| 1830 |
} |
| 1831 |
} |
| 1832 |
} |
| 1833 |
|
| 1834 |
return processed; |
| 1835 |
} |
| 1836 |
|
| 1837 |
SbBool NavigationStyle::processSoEvent(const SoEvent* const ev) |
| 1838 |
{ |
| 1839 |
bool processed = false; |
| 1840 |
bool offeredtoViewerEventBase = false; |
| 1841 |
|
| 1842 |
// handle mouse wheel zoom |
| 1843 |
if (ev->isOfType(SoMouseWheelEvent::getClassTypeId())) { |
| 1844 |
auto const event = static_cast<const SoMouseWheelEvent*>(ev); |
| 1845 |
processed = processWheelEvent(event); |
| 1846 |
viewer->processSoEventBase(ev); |
| 1847 |
offeredtoViewerEventBase = true; |
| 1848 |
} |
| 1849 |
|
| 1850 |
if (!processed && !offeredtoViewerEventBase) { |
| 1851 |
processed = viewer->processSoEventBase(ev); |
| 1852 |
} |
| 1853 |
|
| 1854 |
return processed; |
| 1855 |
} |
| 1856 |
|
| 1857 |
void NavigationStyle::syncWithEvent(const SoEvent* const ev) |
| 1858 |
{ |
| 1859 |
// Events when in "ready-to-seek" mode are ignored, except those |
| 1860 |
// which influence the seek mode itself -- these are handled further |
| 1861 |
// up the inheritance hierarchy. |
| 1862 |
if (this->isSeekMode()) { |
| 1863 |
return; |
| 1864 |
} |
| 1865 |
|
| 1866 |
const SoType type(ev->getTypeId()); |
| 1867 |
|
| 1868 |
// Mismatches in state of the modifier keys happens if the user |
| 1869 |
// presses or releases them outside the viewer window. |
| 1870 |
syncModifierKeys(ev); |
| 1871 |
|
| 1872 |
// Keyboard handling |
| 1873 |
if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) { |
| 1874 |
auto const event = static_cast<const SoKeyboardEvent*>(ev); |
| 1875 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 1876 |
switch (event->getKey()) { |
| 1877 |
case SoKeyboardEvent::LEFT_CONTROL: |
| 1878 |
case SoKeyboardEvent::RIGHT_CONTROL: |
| 1879 |
this->ctrldown = press; |
| 1880 |
break; |
| 1881 |
case SoKeyboardEvent::LEFT_SHIFT: |
| 1882 |
case SoKeyboardEvent::RIGHT_SHIFT: |
| 1883 |
this->shiftdown = press; |
| 1884 |
break; |
| 1885 |
case SoKeyboardEvent::LEFT_ALT: |
| 1886 |
case SoKeyboardEvent::RIGHT_ALT: |
| 1887 |
this->altdown = press; |
| 1888 |
break; |
| 1889 |
default: |
| 1890 |
break; |
| 1891 |
} |
| 1892 |
} |
| 1893 |
|
| 1894 |
// Mouse Button / Spaceball Button handling |
| 1895 |
if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) { |
| 1896 |
auto const event = static_cast<const SoMouseButtonEvent*>(ev); |
| 1897 |
const int button = event->getButton(); |
| 1898 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 1899 |
#ifdef FC_DEBUG |
| 1900 |
SoDebugError::postInfo("processSoEvent", "button = %d", button); |
| 1901 |
#endif |
| 1902 |
switch (button) { |
| 1903 |
case SoMouseButtonEvent::BUTTON1: |
| 1904 |
this->button1down = press; |
| 1905 |
break; |
| 1906 |
case SoMouseButtonEvent::BUTTON2: |
| 1907 |
this->button2down = press; |
| 1908 |
break; |
| 1909 |
case SoMouseButtonEvent::BUTTON3: |
| 1910 |
this->button3down = press; |
| 1911 |
break; |
| 1912 |
default: |
| 1913 |
break; |
| 1914 |
} |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
SbBool NavigationStyle::processMotionEvent(const SoMotion3Event* const ev) |
| 1919 |
{ |
| 1920 |
SoCamera* const camera = viewer->getSoRenderManager()->getCamera(); |
| 1921 |
if (!camera) { |
| 1922 |
return false; |
| 1923 |
} |
| 1924 |
|
| 1925 |
SbViewVolume volume(camera->getViewVolume()); |
| 1926 |
SbVec3f center(volume.getSightPoint(camera->focalDistance.getValue())); |
| 1927 |
float scale(volume.getWorldToScreenScale(center, 1.0)); |
| 1928 |
float translationFactor = scale * .0001; |
| 1929 |
|
| 1930 |
SbVec3f dir = ev->getTranslation(); |
| 1931 |
|
| 1932 |
if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) { |
| 1933 |
auto oCam = static_cast<SoOrthographicCamera*>(camera); |
| 1934 |
oCam->scaleHeight(1.0 + (dir[2] * 0.0001)); |
| 1935 |
dir[2] = 0.0; // don't move the cam for z translation. |
| 1936 |
} |
| 1937 |
|
| 1938 |
SbRotation newRotation(ev->getRotation() * camera->orientation.getValue()); |
| 1939 |
SbVec3f newPosition, newDirection; |
| 1940 |
newRotation.multVec(SbVec3f(0.0, 0.0, -1.0), newDirection); |
| 1941 |
newPosition = center - (newDirection * camera->focalDistance.getValue()); |
| 1942 |
|
| 1943 |
camera->orientation.setValue(newRotation); |
| 1944 |
camera->orientation.getValue().multVec(dir, dir); |
| 1945 |
camera->position = newPosition + (dir * translationFactor); |
| 1946 |
|
| 1947 |
return true; |
| 1948 |
} |
| 1949 |
|
| 1950 |
SbBool NavigationStyle::processKeyboardEvent(const SoKeyboardEvent* const event) |
| 1951 |
{ |
| 1952 |
SbBool processed = false; |
| 1953 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 1954 |
switch (event->getKey()) { |
| 1955 |
case SoKeyboardEvent::LEFT_CONTROL: |
| 1956 |
case SoKeyboardEvent::RIGHT_CONTROL: |
| 1957 |
this->ctrldown = press; |
| 1958 |
break; |
| 1959 |
case SoKeyboardEvent::LEFT_SHIFT: |
| 1960 |
case SoKeyboardEvent::RIGHT_SHIFT: |
| 1961 |
this->shiftdown = press; |
| 1962 |
break; |
| 1963 |
case SoKeyboardEvent::LEFT_ALT: |
| 1964 |
case SoKeyboardEvent::RIGHT_ALT: |
| 1965 |
this->altdown = press; |
| 1966 |
break; |
| 1967 |
case SoKeyboardEvent::S: |
| 1968 |
case SoKeyboardEvent::HOME: |
| 1969 |
case SoKeyboardEvent::LEFT_ARROW: |
| 1970 |
case SoKeyboardEvent::UP_ARROW: |
| 1971 |
case SoKeyboardEvent::RIGHT_ARROW: |
| 1972 |
case SoKeyboardEvent::DOWN_ARROW: |
| 1973 |
if (!this->isViewing()) { |
| 1974 |
this->setViewing(true); |
| 1975 |
} |
| 1976 |
break; |
| 1977 |
case SoKeyboardEvent::PAGE_UP: { |
| 1978 |
processed = true; |
| 1979 |
const SbVec2f posn = normalizePixelPos(event->getPosition()); |
| 1980 |
doZoom(viewer->getSoRenderManager()->getCamera(), getDelta(), posn); |
| 1981 |
break; |
| 1982 |
} |
| 1983 |
case SoKeyboardEvent::PAGE_DOWN: { |
| 1984 |
processed = true; |
| 1985 |
const SbVec2f posn = normalizePixelPos(event->getPosition()); |
| 1986 |
doZoom(viewer->getSoRenderManager()->getCamera(), -getDelta(), posn); |
| 1987 |
break; |
| 1988 |
} |
| 1989 |
default: |
| 1990 |
break; |
| 1991 |
} |
| 1992 |
|
| 1993 |
return processed; |
| 1994 |
} |
| 1995 |
|
| 1996 |
SbBool NavigationStyle::processClickEvent(const SoMouseButtonEvent* const event) |
| 1997 |
{ |
| 1998 |
// issue #0002433: avoid to swallow the UP event if down the |
| 1999 |
// scene graph somewhere a dialog gets opened |
| 2000 |
SbBool processed = false; |
| 2001 |
const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false; |
| 2002 |
if (press) { |
| 2003 |
SbTime tmp = (event->getTime() - mouseDownConsumedEvent.getTime()); |
| 2004 |
float dci = (float)QApplication::doubleClickInterval() / 1000.0f; |
| 2005 |
// a double-click? |
| 2006 |
if (tmp.getValue() < dci) { |
| 2007 |
mouseDownConsumedEvent = *event; |
| 2008 |
mouseDownConsumedEvent.setTime(event->getTime()); |
| 2009 |
processed = true; |
| 2010 |
} |
| 2011 |
else { |
| 2012 |
mouseDownConsumedEvent.setTime(event->getTime()); |
| 2013 |
// 'ANY' is used to mark that we don't know yet if it will |
| 2014 |
// be a double-click event. |
| 2015 |
mouseDownConsumedEvent.setButton(SoMouseButtonEvent::ANY); |
| 2016 |
} |
| 2017 |
} |
| 2018 |
else if (!press) { |
| 2019 |
if (mouseDownConsumedEvent.getButton() == SoMouseButtonEvent::BUTTON1) { |
| 2020 |
// now handle the postponed event |
| 2021 |
NavigationStyle::processSoEvent(&mouseDownConsumedEvent); |
| 2022 |
mouseDownConsumedEvent.setButton(SoMouseButtonEvent::ANY); |
| 2023 |
} |
| 2024 |
} |
| 2025 |
|
| 2026 |
return processed; |
| 2027 |
} |
| 2028 |
|
| 2029 |
SbBool NavigationStyle::processWheelEvent(const SoMouseWheelEvent* const event) |
| 2030 |
{ |
| 2031 |
const SbVec2s pos(event->getPosition()); |
| 2032 |
const SbVec2f posn = normalizePixelPos(pos); |
| 2033 |
|
| 2034 |
// handle mouse wheel zoom |
| 2035 |
doZoom(viewer->getSoRenderManager()->getCamera(), event->getDelta(), posn); |
| 2036 |
return true; |
| 2037 |
} |
| 2038 |
|
| 2039 |
void NavigationStyle::setPopupMenuEnabled(const SbBool on) |
| 2040 |
{ |
| 2041 |
this->menuenabled = on; |
| 2042 |
} |
| 2043 |
|
| 2044 |
SbBool NavigationStyle::isPopupMenuEnabled() const |
| 2045 |
{ |
| 2046 |
return this->menuenabled; |
| 2047 |
} |
| 2048 |
|
| 2049 |
bool NavigationStyle::isNavigationStyleAction(QAction* action, QActionGroup* navMenuGroup) const |
| 2050 |
{ |
| 2051 |
return action && navMenuGroup->actions().indexOf(action) >= 0 && action->isChecked(); |
| 2052 |
} |
| 2053 |
|
| 2054 |
QWidget* NavigationStyle::findView3DInventorWidget() const |
| 2055 |
{ |
| 2056 |
QWidget* widget = viewer->getWidget(); |
| 2057 |
while (widget && !widget->inherits("Gui::View3DInventor")) { |
| 2058 |
widget = widget->parentWidget(); |
| 2059 |
} |
| 2060 |
return widget; |
| 2061 |
} |
| 2062 |
|
| 2063 |
void NavigationStyle::applyNavigationStyleChange(QAction* selectedAction) |
| 2064 |
{ |
| 2065 |
QByteArray navigationStyleTypeName = selectedAction->data().toByteArray(); |
| 2066 |
QWidget* view3DWidget = findView3DInventorWidget(); |
| 2067 |
|
| 2068 |
if (view3DWidget) { |
| 2069 |
Base::Type newNavigationStyle = Base::Type::fromName(navigationStyleTypeName.constData()); |
| 2070 |
if (newNavigationStyle != this->getTypeId()) { |
| 2071 |
QEvent* navigationChangeEvent = new NavigationStyleEvent(newNavigationStyle); |
| 2072 |
QApplication::postEvent(view3DWidget, navigationChangeEvent); |
| 2073 |
} |
| 2074 |
} |
| 2075 |
} |
| 2076 |
|
| 2077 |
void NavigationStyle::openPopupMenu(const SbVec2s& position) |
| 2078 |
{ |
| 2079 |
// store the right-click position for potential use by Clarify Selection |
| 2080 |
rightClickPosition = position; |
| 2081 |
|
| 2082 |
// ask workbenches and view provider, ... |
| 2083 |
MenuItem view; |
| 2084 |
Gui::Application::Instance->setupContextMenu("View", &view); |
| 2085 |
|
| 2086 |
auto contextMenu = new QMenu(viewer->getGLWidget()); |
| 2087 |
MenuManager::getInstance()->setupContextMenu(&view, *contextMenu); |
| 2088 |
contextMenu->setAttribute(Qt::WA_DeleteOnClose); |
| 2089 |
|
| 2090 |
auto navMenu = contextMenu->addMenu(QObject::tr("Navigation styles")); |
| 2091 |
auto navMenuGroup = new QActionGroup(navMenu); |
| 2092 |
|
| 2093 |
// add submenu at the end to select navigation style |
| 2094 |
const std::map<Base::Type, std::string> styles = UserNavigationStyle::getUserFriendlyNames(); |
| 2095 |
for (const auto& style : styles) { |
| 2096 |
const QString name = QApplication::translate(style.first.getName(), style.second.c_str()); |
| 2097 |
QAction* item = navMenuGroup->addAction(name); |
| 2098 |
navMenu->addAction(item); |
| 2099 |
item->setCheckable(true); |
| 2100 |
item->setData(QByteArray(style.first.getName())); |
| 2101 |
|
| 2102 |
if (const Base::Type item_style = style.first; item_style != this->getTypeId()) { |
| 2103 |
auto triggeredFun = [this, item_style]() { |
| 2104 |
QWidget* widget = viewer->getWidget(); |
| 2105 |
while (widget && !widget->inherits("Gui::View3DInventor")) { |
| 2106 |
widget = widget->parentWidget(); |
| 2107 |
} |
| 2108 |
if (widget) { |
| 2109 |
// this is the widget where the viewer is embedded |
| 2110 |
QEvent* ns_event = new NavigationStyleEvent(item_style); |
| 2111 |
QApplication::postEvent(widget, ns_event); |
| 2112 |
} |
| 2113 |
}; |
| 2114 |
item->connect(item, &QAction::triggered, triggeredFun); |
| 2115 |
} |
| 2116 |
else { |
| 2117 |
item->setChecked(true); |
| 2118 |
} |
| 2119 |
} |
| 2120 |
|
| 2121 |
// Add Clarify Selection option if there are objects under cursor |
| 2122 |
bool separator = false; |
| 2123 |
auto posAction = !contextMenu->actions().empty() ? contextMenu->actions().front() : nullptr; |
| 2124 |
|
| 2125 |
// Get picked objects at position |
| 2126 |
SoRayPickAction rp(viewer->getSoRenderManager()->getViewportRegion()); |
| 2127 |
rp.setPoint(position); |
| 2128 |
rp.setRadius(viewer->getPickRadius()); |
| 2129 |
rp.setPickAll(true); |
| 2130 |
rp.apply(viewer->getSoRenderManager()->getSceneGraph()); |
| 2131 |
|
| 2132 |
const SoPickedPointList& pplist = rp.getPickedPointList(); |
| 2133 |
QAction* pickAction = nullptr; |
| 2134 |
|
| 2135 |
if (pplist.getLength() > 0) { |
| 2136 |
separator = true; |
| 2137 |
if (auto cmd |
| 2138 |
= Application::Instance->commandManager().getCommandByName("Std_ClarifySelection")) { |
| 2139 |
pickAction = new QAction(cmd->getAction()->text(), contextMenu); |
| 2140 |
pickAction->setShortcut(cmd->getAction()->shortcut()); |
| 2141 |
} |
| 2142 |
else { |
| 2143 |
pickAction = new QAction(QObject::tr("Clarify Selection"), contextMenu); |
| 2144 |
} |
| 2145 |
if (posAction) { |
| 2146 |
contextMenu->insertAction(posAction, pickAction); |
| 2147 |
contextMenu->insertSeparator(posAction); |
| 2148 |
} |
| 2149 |
else { |
| 2150 |
contextMenu->addAction(pickAction); |
| 2151 |
} |
| 2152 |
} |
| 2153 |
|
| 2154 |
if (separator && posAction) { |
| 2155 |
contextMenu->insertSeparator(posAction); |
| 2156 |
} |
| 2157 |
|
| 2158 |
auto navigationFunction = [this, navMenuGroup](QAction* selectedAction) { |
| 2159 |
// handle navigation style change if user selected a navigation style option |
| 2160 |
if (isNavigationStyleAction(selectedAction, navMenuGroup)) { |
| 2161 |
applyNavigationStyleChange(selectedAction); |
| 2162 |
rightClickPosition.reset(); |
| 2163 |
} |
| 2164 |
}; |
| 2165 |
|
| 2166 |
auto clarifyFunction = [pickAction](QAction* selectedAction) { |
| 2167 |
if (selectedAction == pickAction) { |
| 2168 |
// Execute the Clarify Selection command at this position |
| 2169 |
auto cmd = Application::Instance->commandManager().getCommandByName("Std_ClarifySelection"); |
| 2170 |
if (cmd && cmd->isActive()) { |
| 2171 |
cmd->invoke(0); // required placeholder value - we don't use group command |
| 2172 |
} |
| 2173 |
} |
| 2174 |
}; |
| 2175 |
|
| 2176 |
QObject::connect(contextMenu, &QMenu::triggered, navigationFunction); |
| 2177 |
QObject::connect(contextMenu, &QMenu::triggered, clarifyFunction); |
| 2178 |
|
| 2179 |
contextMenu->popup(QCursor::pos()); |
| 2180 |
|
| 2181 |
rightClickPosition.reset(); |
| 2182 |
} |
| 2183 |
|
| 2184 |
PyObject* NavigationStyle::getPyObject() |
| 2185 |
{ |
| 2186 |
if (pythonObject.is(nullptr)) { |
| 2187 |
// ref counter is set to 1 |
| 2188 |
pythonObject = Py::asObject(new NavigationStylePy(this)); |
| 2189 |
} |
| 2190 |
return Py::new_reference_to(pythonObject); |
| 2191 |
} |
| 2192 |
|
| 2193 |
// ---------------------------------------------------------------------------------- |
| 2194 |
|
| 2195 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::UserNavigationStyle, Gui::NavigationStyle) |
| 2196 |
|
| 2197 |
std::string UserNavigationStyle::userFriendlyName() const |
| 2198 |
{ |
| 2199 |
std::string name = this->getTypeId().getName(); |
| 2200 |
// remove namespaces |
| 2201 |
std::size_t pos = name.rfind("::"); |
| 2202 |
if (pos != std::string::npos) { |
| 2203 |
name = name.substr(pos + 2); |
| 2204 |
} |
| 2205 |
|
| 2206 |
// remove 'NavigationStyle' |
| 2207 |
pos = name.find("NavigationStyle"); |
| 2208 |
if (pos != std::string::npos) { |
| 2209 |
name = name.substr(0, pos); |
| 2210 |
} |
| 2211 |
return name; |
| 2212 |
} |
| 2213 |
|
| 2214 |
std::map<Base::Type, std::string> UserNavigationStyle::getUserFriendlyNames() |
| 2215 |
{ |
| 2216 |
std::map<Base::Type, std::string> names; |
| 2217 |
std::vector<Base::Type> types; |
| 2218 |
Base::Type::getAllDerivedFrom(UserNavigationStyle::getClassTypeId(), types); |
| 2219 |
|
| 2220 |
for (auto& type : types) { |
| 2221 |
if (type != UserNavigationStyle::getClassTypeId()) { |
| 2222 |
std::unique_ptr<UserNavigationStyle> inst( |
| 2223 |
static_cast<UserNavigationStyle*>(type.createInstance()) |
| 2224 |
); |
| 2225 |
if (inst) { |
| 2226 |
names[type] = inst->userFriendlyName(); |
| 2227 |
} |
| 2228 |
} |
| 2229 |
} |
| 2230 |
return names; |
| 2231 |
} |