Skip to content

Commit

Permalink
Gui: Find bounding sphere before drag or animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexbas committed Nov 10, 2023
1 parent e327a3f commit a740632
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/Gui/NavigationAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ FixedTimeAnimation::FixedTimeAnimation(NavigationStyle* navigation, const SbRota

void FixedTimeAnimation::initialize()
{
navigation->findBoundingSphere();
prevAngle = 0;
prevTranslation = SbVec3f(0, 0, 0);

Expand Down Expand Up @@ -133,6 +134,7 @@ SpinningAnimation::SpinningAnimation(NavigationStyle* navigation, const SbVec3f&

void SpinningAnimation::initialize()
{
navigation->findBoundingSphere();
prevAngle = 0;

navigation->setViewing(true);
Expand Down
42 changes: 22 additions & 20 deletions src/Gui/NavigationStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ void NavigationStyle::viewAll()
}
}

void NavigationStyle::findBoundingSphere() {
// Find a bounding sphere for the scene
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
action.apply(viewer->getSceneGraph());
boundingSphere.circumscribe(action.getBoundingBox());
}

/** Rotate the camera by the given amount, then reposition it so we're still pointing at the same
* focal point
*/
Expand Down Expand Up @@ -511,29 +518,23 @@ void NavigationStyle::reorientCamera(SoCamera* camera, const SbRotation& rotatio
camera->position = rotationCenter + newRotationCenterDistance;

// Fix issue with near clipping in orthogonal view
if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {
if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {

// Find a bounding sphere for the scene
SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
action.apply(viewer->getSceneGraph());
SbSphere boundingSphere;
boundingSphere.circumscribe(action.getBoundingBox());
// The center of the bounding sphere in camera coordinate system
SbVec3f center;
camera->orientation.getValue().inverse().multVec(boundingSphere.getCenter() - camera->position.getValue(), center);

// The center of the bounding sphere in camera coordinate system
SbVec3f center;
camera->orientation.getValue().inverse().multVec(boundingSphere.getCenter() - camera->position.getValue(), center);
SbVec3f dir;
camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), dir);

SbVec3f dir;
camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), dir);

// Reposition the camera but keep the focal point the same
// nearDistance is 0 and farDistance is the diameter of the bounding sphere
float repositionDistance = -center.getValue()[2] - boundingSphere.getRadius();
camera->position = camera->position.getValue() + repositionDistance * dir;
camera->nearDistance = 0;
camera->farDistance = 2 * boundingSphere.getRadius();
camera->focalDistance = camera->focalDistance.getValue() - repositionDistance;
}
// Reposition the camera but keep the focal point the same
// nearDistance is 0 and farDistance is the diameter of the bounding sphere
float repositionDistance = -center.getValue()[2] - boundingSphere.getRadius();
camera->position = camera->position.getValue() + repositionDistance * dir;
camera->nearDistance = 0;
camera->farDistance = 2 * boundingSphere.getRadius();
camera->focalDistance = camera->focalDistance.getValue() - repositionDistance;
}
}

void NavigationStyle::panCamera(SoCamera * cam, float aspectratio, const SbPlane & panplane,
Expand Down Expand Up @@ -1299,6 +1300,7 @@ void NavigationStyle::setViewingMode(const ViewerMode newmode)
// Set up initial projection point for the projector object when
// first starting a drag operation.
animator->stop();
findBoundingSphere();
viewer->showRotationCenter(true);
this->spinprojector->project(this->lastmouseposition);
this->interactiveCountInc();
Expand Down
4 changes: 4 additions & 0 deletions src/Gui/NavigationStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <Inventor/SbBox2s.h>
#include <Inventor/SbPlane.h>
#include <Inventor/SbRotation.h>
#include <Inventor/SbSphere.h>
#include <Inventor/SbTime.h>
#include <Inventor/SbVec2f.h>
#include <Inventor/SbVec2s.h>
Expand Down Expand Up @@ -151,6 +152,7 @@ class GuiExport NavigationStyle : public Base::BaseClass
SoCamera* getCamera() const;
void setCameraOrientation(const SbRotation& orientation, SbBool moveToCenter = false);
void translateCamera(const SbVec3f& translation);
void findBoundingSphere();
void reorientCamera(SoCamera* camera, const SbRotation& rotation);
void reorientCamera(SoCamera* camera, const SbRotation& rotation, const SbVec3f& rotationCenter);

Expand Down Expand Up @@ -278,6 +280,8 @@ class GuiExport NavigationStyle : public Base::BaseClass
NavigationStyle::RotationCenterModes rotationCenterMode;
float sensitivity;
SbBool resetcursorpos;

SbSphere boundingSphere;
};

/** Sub-classes of this class appear in the preference dialog where users can
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/View3DInventorViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,8 @@ void View3DInventorViewer::setSceneGraph(SoNode* root)
static_cast<SoSeparator*>(scene)->insertChild(this->backlight, 0);
}
}

navigation->findBoundingSphere();
}

void View3DInventorViewer::savePicture(int width, int height, int sample, const QColor& bg, QImage& img) const
Expand Down

0 comments on commit a740632

Please sign in to comment.