From 64ff7396c21a637b781c6bc9e640a51cdca9d12c Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Tue, 3 Mar 2026 16:09:38 -0700 Subject: [PATCH] refactor: Use `gp_Ax2` for camera plane orientation Co-authored-by: aider (gemini/gemini-2.5-pro) --- src/Camera.cpp | 42 +++++++++++++++++++++++------------------- src/Camera.h | 4 +++- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/Camera.cpp b/src/Camera.cpp index 90443a4..2e14ab4 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -6,7 +6,8 @@ // - use a right-handed, Z-up coordinate system to match Open CASCADE #include "Camera.h" -#include "ViewportWidget.h" +#include +#include #include #include #include @@ -202,25 +203,28 @@ void Camera::restoreState() setZoom(m_savedZoom); } -void Camera::animateToPlaneView(int plane) +void Camera::animateToPlaneView(const gp_Ax2& plane) { - float targetXRot = xRotation(); - float targetYRot = yRotation(); - switch (static_cast(plane)) { - case ViewportWidget::SketchPlane::XY: // Top view - targetXRot = 90 * 16; - targetYRot = 0; - break; - case ViewportWidget::SketchPlane::XZ: // Front view - targetXRot = 0; - targetYRot = 0; - break; - case ViewportWidget::SketchPlane::YZ: // Right view - targetXRot = 0; - targetYRot = -90 * 16; - break; - case ViewportWidget::SketchPlane::NONE: - break; + const auto& normal = plane.Direction(); + QVector3D n(normal.X(), normal.Y(), normal.Z()); + QVector3D d; + + // This logic preserves the quirky view directions of the original implementation. + // For XZ-like planes (normal is mostly along Y), the view is aligned WITH the plane normal. + // For other planes, it's aligned AGAINST the normal. + if (qAbs(n.y()) > 0.99) { + d = n; + } else { + d = -n; + } + + float targetXRot = qRadiansToDegrees(asin(-d.z())) * 16.0f; + float targetYRot; + + if (qAbs(d.z()) > 0.9999) { // Top/bottom-like view, Y rotation is arbitrary + targetYRot = 0; // Set to 0 for stability + } else { + targetYRot = qRadiansToDegrees(atan2(d.x(), d.y())) * 16.0f; } auto* animGroup = new QParallelAnimationGroup(this); diff --git a/src/Camera.h b/src/Camera.h index 6c55b43..dd658b0 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -14,6 +14,8 @@ #include #include +#include + class Camera : public QObject { Q_OBJECT @@ -46,7 +48,7 @@ public: void saveState(); void restoreState(); - void animateToPlaneView(int plane); + void animateToPlaneView(const gp_Ax2& plane); void animateRestoreState(); void animateToHomeView();