Skip to content

Commit

Permalink
Gui: [skip ci] support of bitmasks for enum RotationCenterMode
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 29, 2020
1 parent 18ef1f7 commit 0eba78d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 79 deletions.
134 changes: 65 additions & 69 deletions src/Gui/NavigationStyle.cpp
Expand Up @@ -54,7 +54,7 @@ struct NavigationStyleP {
SbVec3f focal1, focal2;
SbVec3f rotationCenter;
SbBool rotationCenterFound;
NavigationStyle::RotationCenterMode rotationCenterMode;
NavigationStyle::RotationCenterModes rotationCenterMode;
SbRotation endRotation;
SoTimerSensor * animsensor;
float sensitivity;
Expand All @@ -68,7 +68,8 @@ struct NavigationStyleP {
this->sensitivity = 2.0f;
this->resetcursorpos = false;
this->rotationCenterFound = false;
this->rotationCenterMode = NavigationStyle::ScenePointAtCursor;
this->rotationCenterMode = NavigationStyle::RotationCenterMode::ScenePointAtCursor |
NavigationStyle::RotationCenterMode::FocalPointAtCursor;
}
static void viewAnimationCB(void * data, SoSensor * sensor);
};
Expand Down Expand Up @@ -241,13 +242,15 @@ void NavigationStyle::initialize()
long mode = App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/View")->GetInt("RotationMode", 1);
if (mode == 0) {
setRotationCenterMode(NavigationStyle::WindowCenter);
setRotationCenterMode(NavigationStyle::RotationCenterMode::WindowCenter);
}
else if (mode == 1) {
setRotationCenterMode(NavigationStyle::ScenePointAtCursor);
setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor |
NavigationStyle::RotationCenterMode::FocalPointAtCursor);
}
else if (mode == 2) {
setRotationCenterMode(NavigationStyle::BoundingBoxCenter);
setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor |
NavigationStyle::RotationCenterMode::BoundingBoxCenter);
}
}

Expand Down Expand Up @@ -892,8 +895,7 @@ void NavigationStyle::spin(const SbVec2f & pointerpos)
lastpos[0] = float(this->log.position[1][0]) / float(std::max((int)(glsize[0]-1), 1));
lastpos[1] = float(this->log.position[1][1]) / float(std::max((int)(glsize[1]-1), 1));

if (PRIVATE(this)->rotationCenterMode != RotationCenterMode::WindowCenter &&
PRIVATE(this)->rotationCenterFound) {
if (PRIVATE(this)->rotationCenterMode && PRIVATE(this)->rotationCenterFound) {
SbVec3f hitpoint = PRIVATE(this)->rotationCenter;

// set to the given position
Expand Down Expand Up @@ -921,8 +923,7 @@ void NavigationStyle::spin(const SbVec2f & pointerpos)
r.invert();
this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r);

if (PRIVATE(this)->rotationCenterMode != RotationCenterMode::WindowCenter &&
PRIVATE(this)->rotationCenterFound) {
if (PRIVATE(this)->rotationCenterMode && PRIVATE(this)->rotationCenterFound) {
float ratio = vp.getViewportAspectRatio();
SbViewVolume vv = viewer->getSoRenderManager()->getCamera()->getViewVolume(vp.getViewportAspectRatio());
SbPlane panplane = vv.getPlane(viewer->getSoRenderManager()->getCamera()->focalDistance.getValue());
Expand Down Expand Up @@ -1023,69 +1024,64 @@ void NavigationStyle::saveCursorPosition(const SoEvent * const ev)
this->localPos = ev->getPosition();

//Option to get point on model (slow) or always on focal plane (fast)
switch (PRIVATE(this)->rotationCenterMode) {
case ScenePointAtCursor:
{
SoRayPickAction rpaction(viewer->getSoRenderManager()->getViewportRegion());
rpaction.setPoint(this->localPos);
rpaction.setRadius(viewer->getPickRadius());
rpaction.apply(viewer->getSoRenderManager()->getSceneGraph());

SoPickedPoint * picked = rpaction.getPickedPoint();
if (picked) {
setRotationCenter(picked->getPoint());
break;
}
//
// mode is ScenePointAtCursor to get exact point if possible
if (PRIVATE(this)->rotationCenterMode & NavigationStyle::RotationCenterMode::ScenePointAtCursor) {
SoRayPickAction rpaction(viewer->getSoRenderManager()->getViewportRegion());
rpaction.setPoint(this->localPos);
rpaction.setRadius(viewer->getPickRadius());
rpaction.apply(viewer->getSoRenderManager()->getSceneGraph());

SoPickedPoint * picked = rpaction.getPickedPoint();
if (picked) {
setRotationCenter(picked->getPoint());
return;
}
/* FALLTHRU */
}

// mode is FocalPointAtCursor or a ScenePointAtCursor failed
case FocalPointAtCursor:
{
// get the intersection point of the ray and the focal plane
const SbViewportRegion & vp = viewer->getSoRenderManager()->getViewportRegion();
float ratio = vp.getViewportAspectRatio();
if (PRIVATE(this)->rotationCenterMode & NavigationStyle::RotationCenterMode::FocalPointAtCursor) {
// get the intersection point of the ray and the focal plane
const SbViewportRegion & vp = viewer->getSoRenderManager()->getViewportRegion();
float ratio = vp.getViewportAspectRatio();

SoCamera* cam = viewer->getSoRenderManager()->getCamera();
if (!cam) return; // no camera
SbViewVolume vv = cam->getViewVolume(ratio);
SoCamera* cam = viewer->getSoRenderManager()->getCamera();
if (!cam) return; // no camera
SbViewVolume vv = cam->getViewVolume(ratio);

SbLine line;
SbVec2f currpos = ev->getNormalizedPosition(vp);
vv.projectPointToLine(currpos, line);
SbVec3f current_planept;
SbPlane panplane = vv.getPlane(cam->focalDistance.getValue());
panplane.intersect(line, current_planept);
SbLine line;
SbVec2f currpos = ev->getNormalizedPosition(vp);
vv.projectPointToLine(currpos, line);
SbVec3f current_planept;
SbPlane panplane = vv.getPlane(cam->focalDistance.getValue());
panplane.intersect(line, current_planept);

setRotationCenter(current_planept);
break;
}
case BoundingBoxCenter:
{
const SbViewportRegion & vp = viewer->getSoRenderManager()->getViewportRegion();
float ratio = vp.getViewportAspectRatio();

SoCamera* cam = viewer->getSoRenderManager()->getCamera();
if (!cam) break; // no camera

SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
action.apply(viewer->getSceneGraph());
SbBox3f boundingBox = action.getBoundingBox();
SbVec3f boundingBoxCenter = boundingBox.getCenter();
setRotationCenter(boundingBoxCenter);

// To drag around the center point of the bbox we have to determine
// its projection on the screen becaue this information is used in
// NavigationStyle::spin() for the panning
SbViewVolume vv = cam->getViewVolume(ratio);
vv.projectToScreen(boundingBoxCenter, boundingBoxCenter);
SbVec2s size = vp.getViewportSizePixels();
short tox = static_cast<short>(boundingBoxCenter[0] * size[0]);
short toy = static_cast<short>(boundingBoxCenter[1] * size[1]);
this->localPos.setValue(tox, toy);
break;
}
default:
break;
setRotationCenter(current_planept);
}

// mode is BoundingBoxCenter or a ScenePointAtCursor failed
if (PRIVATE(this)->rotationCenterMode & NavigationStyle::RotationCenterMode::BoundingBoxCenter) {
const SbViewportRegion & vp = viewer->getSoRenderManager()->getViewportRegion();
float ratio = vp.getViewportAspectRatio();

SoCamera* cam = viewer->getSoRenderManager()->getCamera();
if (!cam) return; // no camera

SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
action.apply(viewer->getSceneGraph());
SbBox3f boundingBox = action.getBoundingBox();
SbVec3f boundingBoxCenter = boundingBox.getCenter();
setRotationCenter(boundingBoxCenter);

// To drag around the center point of the bbox we have to determine
// its projection on the screen becaue this information is used in
// NavigationStyle::spin() for the panning
SbViewVolume vv = cam->getViewVolume(ratio);
vv.projectToScreen(boundingBoxCenter, boundingBoxCenter);
SbVec2s size = vp.getViewportSizePixels();
short tox = static_cast<short>(boundingBoxCenter[0] * size[0]);
short toy = static_cast<short>(boundingBoxCenter[1] * size[1]);
this->localPos.setValue(tox, toy);
}
}

Expand Down Expand Up @@ -1253,12 +1249,12 @@ SbBool NavigationStyle::isZoomAtCursor() const
return this->zoomAtCursor;
}

void NavigationStyle::setRotationCenterMode(NavigationStyle::RotationCenterMode mode)
void NavigationStyle::setRotationCenterMode(NavigationStyle::RotationCenterModes mode)
{
PRIVATE(this)->rotationCenterMode = mode;
}

NavigationStyle::RotationCenterMode NavigationStyle::getRotationCenterMode() const
NavigationStyle::RotationCenterModes NavigationStyle::getRotationCenterMode() const
{
return PRIVATE(this)->rotationCenterMode;
}
Expand Down
17 changes: 10 additions & 7 deletions src/Gui/NavigationStyle.h
Expand Up @@ -99,12 +99,13 @@ class GuiExport NavigationStyle : public Base::BaseClass
Trackball
};

enum RotationCenterMode {
WindowCenter, /**< The center of the window */
ScenePointAtCursor, /**< Find the point in the scene at the cursor position. If there is no point then the focal plane is used */
FocalPointAtCursor, /**< Find the point on the focal plane at the cursor position. */
BoundingBoxCenter /**< Find the center point of the bounding box of the scene. */
enum class RotationCenterMode {
WindowCenter = 0, /**< The center of the window */
ScenePointAtCursor = 1, /**< Find the point in the scene at the cursor position. If there is no point then the focal plane is used */
FocalPointAtCursor = 2, /**< Find the point on the focal plane at the cursor position. */
BoundingBoxCenter = 4 /**< Find the center point of the bounding box of the scene. */
};
Q_DECLARE_FLAGS(RotationCenterModes, RotationCenterMode)

public:
NavigationStyle();
Expand Down Expand Up @@ -133,8 +134,8 @@ class GuiExport NavigationStyle : public Base::BaseClass
SbBool isZoomAtCursor() const;
void zoomIn();
void zoomOut();
void setRotationCenterMode(RotationCenterMode);
RotationCenterMode getRotationCenterMode() const;
void setRotationCenterMode(RotationCenterModes);
RotationCenterModes getRotationCenterMode() const;
void setRotationCenter(const SbVec3f& cnt);
SbVec3f getFocalPoint() const;

Expand Down Expand Up @@ -410,4 +411,6 @@ class GuiExport OpenCascadeNavigationStyle : public UserNavigationStyle {

} // namespace Gui

Q_DECLARE_OPERATORS_FOR_FLAGS(Gui::NavigationStyle::RotationCenterModes)

#endif // GUI_NAVIGATIONSTYLE_H
8 changes: 5 additions & 3 deletions src/Gui/View3DInventor.cpp
Expand Up @@ -360,13 +360,15 @@ void View3DInventor::OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::M
else if (strcmp(Reason,"RotationMode") == 0) {
long mode = rGrp.GetInt("RotationMode", 1);
if (mode == 0) {
_viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::WindowCenter);
_viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::WindowCenter);
}
else if (mode == 1) {
_viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::ScenePointAtCursor);
_viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor |
NavigationStyle::RotationCenterMode::FocalPointAtCursor);
}
else if (mode == 2) {
_viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::BoundingBoxCenter);
_viewer->navigationStyle()->setRotationCenterMode(NavigationStyle::RotationCenterMode::ScenePointAtCursor |
NavigationStyle::RotationCenterMode::BoundingBoxCenter);
}
}
else if (strcmp(Reason,"EyeDistance") == 0) {
Expand Down

0 comments on commit 0eba78d

Please sign in to comment.