Skip to content

Commit df92006

Browse files
HanaeRateauhugtalbotbakpaulepernod
authored
[Visual] Changes trackball to remove precession (#5770)
* [Visual] Modifies trackball to remove precession problem * Update Sofa/Component/Visual/src/sofa/component/visual/BaseCamera.cpp Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> * [InteractiveCamera] Changes new class variables names Following code review suggestions Co-authored-by: Paul Baksic <30337881+bakpaul@users.noreply.github.com> * Update Sofa/Component/Visual/src/sofa/component/visual/BaseCamera.cpp Co-authored-by: erik pernod <erik.pernod@gmail.com> --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> Co-authored-by: Paul Baksic <30337881+bakpaul@users.noreply.github.com> Co-authored-by: erik pernod <erik.pernod@gmail.com>
1 parent 5a2a410 commit df92006

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

Sofa/Component/Visual/src/sofa/component/visual/BaseCamera.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,21 +537,18 @@ void BaseCamera::rotateCameraAroundPoint(Quat& rotation, const type::Vec3& point
537537
updateOutputData();
538538
}
539539

540-
void BaseCamera::rotateWorldAroundPoint(Quat &rotation, const type::Vec3 &point, Quat orientationCam)
540+
void BaseCamera::rotateWorldAroundPoint(Quat &rotation, const type::Vec3 &point, Quat orientationCam, type::Vec3 positionCam)
541541
{
542542
type::Vec3 tempAxis;
543543
SReal tempAngle;
544-
//Quat orientationCam = this->getOrientation();
545-
type::Vec3& positionCam = *d_position.beginEdit();
546544

547545
rotation.quatToAxis(tempAxis, tempAngle);
548-
const Quat tempQuat (orientationCam.rotate(-tempAxis), tempAngle);
546+
const Quat tempQuat(orientationCam.rotate(-tempAxis), tempAngle);
549547

550548
const sofa::type::Transform<SReal> world_H_cam(positionCam, orientationCam);
551549
const sofa::type::Transform<SReal> world_H_pivot(point, Quat());
552550
const sofa::type::Transform<SReal> pivotBefore_R_pivotAfter(type::Vec3(0.0,0.0,0.0), tempQuat);
553551
const sofa::type::Transform<SReal> camera_H_WorldAfter = world_H_cam.inversed() * world_H_pivot * pivotBefore_R_pivotAfter * world_H_pivot.inversed();
554-
//defaulttype::SolidTypes<double>::Transform camera_H_WorldAfter = worldBefore_H_cam.inversed()*worldBefore_R_worldAfter;
555552

556553
positionCam = camera_H_WorldAfter.inversed().getOrigin();
557554
orientationCam = camera_H_WorldAfter.inversed().getOrientation();
@@ -563,13 +560,17 @@ void BaseCamera::rotateWorldAroundPoint(Quat &rotation, const type::Vec3 &point,
563560
}
564561

565562
d_orientation.setValue(orientationCam);
566-
d_position.endEdit();
563+
d_position.setValue(positionCam);
567564

568565
updateOutputData();
569566
}
570567

571568

572-
569+
void BaseCamera::rotateWorldAroundPoint(Quat& rotation, const type::Vec3& point, Quat orientationCam)
570+
{
571+
auto positionCam = sofa::helper::getReadAccessor(d_position);
572+
rotateWorldAroundPoint(rotation, point, orientationCam, positionCam);
573+
}
573574

574575

575576
type::Vec3 BaseCamera::screenToViewportPoint(const type::Vec3& p) const

Sofa/Component/Visual/src/sofa/component/visual/BaseCamera.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class SOFA_COMPONENT_VISUAL_API BaseCamera : public core::objectmodel::BaseObjec
102102
void moveCamera(const type::Vec3 &p, const Quat &q);
103103

104104
void rotateCameraAroundPoint( Quat& rotation, const type::Vec3& point);
105-
virtual void rotateWorldAroundPoint( Quat& rotation, const type::Vec3& point, Quat orientationCam);
105+
virtual void rotateWorldAroundPoint(Quat& rotation, const type::Vec3& point, Quat orientationCam);
106+
virtual void rotateWorldAroundPoint(Quat& rotation, const type::Vec3& point, Quat orientationCam, type::Vec3 positionCam);
106107

107108
type::Vec3 screenToViewportPoint(const type::Vec3& p) const;
108109
type::Vec3 screenToWorldPoint(const type::Vec3& p);

Sofa/Component/Visual/src/sofa/component/visual/InteractiveCamera.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void InteractiveCamera::moveCamera(int x, int y)
7070
currentTrackball.ComputeQuaternion(x1, y1, x2, y2);
7171
//fetch rotation
7272
newQuat = currentTrackball.GetQuaternion();
73+
7374
type::Vec3 pivot;
7475
switch (d_pivot.getValue())
7576
{
@@ -87,7 +88,7 @@ void InteractiveCamera::moveCamera(int x, int y)
8788
pivot = sceneCenter;
8889
break;
8990
}
90-
rotateWorldAroundPoint(newQuat, pivot, this->getOrientation());
91+
rotateWorldAroundPoint(newQuat, pivot, m_startingCameraOrientation, m_startingCameraPosition);
9192
}
9293
else if (currentMode == ZOOM_MODE)
9394
{
@@ -117,8 +118,11 @@ void InteractiveCamera::moveCamera(int x, int y)
117118
}
118119
//must call update afterwards
119120

120-
lastMousePosX = x;
121-
lastMousePosY = y;
121+
if (currentMode != TRACKBALL_MODE)
122+
{
123+
lastMousePosX = x;
124+
lastMousePosY = y;
125+
}
122126
}
123127
else if (currentMode == WHEEL_ZOOM_MODE)
124128
{
@@ -186,6 +190,9 @@ void InteractiveCamera::processMouseEvent(core::objectmodel::MouseEvent* me)
186190
{
187191
isMoving = true;
188192
currentMode = TRACKBALL_MODE;
193+
194+
m_startingCameraOrientation = this->getOrientation();
195+
m_startingCameraPosition = this->getPosition();
189196
lastMousePosX = posX;
190197
lastMousePosY = posY;
191198
}

Sofa/Component/Visual/src/sofa/component/visual/InteractiveCamera.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class SOFA_COMPONENT_VISUAL_API InteractiveCamera : public BaseCamera
5656
bool isMoving;
5757
int lastMousePosX, lastMousePosY;
5858
helper::visual::Trackball currentTrackball;
59+
sofa::type::Quatd m_startingCameraOrientation;
60+
sofa::type::Vec3 m_startingCameraPosition;
5961

6062
void internalUpdate() override;
6163
protected:

Sofa/framework/Helper/src/sofa/helper/visual/Trackball.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class SOFA_HELPER_API Trackball
8989
// Accessors
9090
void SetQuaternion(sofa::type::Quat<SReal> Q);
9191
sofa::type::Quat<SReal> GetQuaternion(void);
92+
9293

9394
// Pass the x and y coordinates of the last and current positions of
9495
// the mouse, scaled so they are from (-1.0 ... 1.0).
@@ -101,8 +102,9 @@ class SOFA_HELPER_API Trackball
101102
// expressed in radians. The result is put into the third argument.
102103
void AxisToQuat(double a[3], double phi, double q[4]);
103104

105+
104106
private:
105-
sofa::type::Quat<SReal> _quat;
107+
sofa::type::Quat<SReal> _quat;
106108
};
107109

108110
} // namespace sofa::helper::visual

0 commit comments

Comments
 (0)