From 25afbaacb64d1fc02e1db260599986604982d1ba Mon Sep 17 00:00:00 2001 From: Mathieu Westphal Date: Sat, 21 Dec 2024 19:44:25 +0100 Subject: [PATCH] Change camera API to be more explicit (#1820) * Change camera API to be more explicit pos -> position up -> viewUp foc -> focalPoint angle -> viewAngle --- application/F3DStarter.cxx | 8 ++++---- library/public/camera.h | 8 ++++---- library/src/camera_impl.cxx | 16 ++++++++-------- library/src/interactor_impl.cxx | 29 ++++++++++++++--------------- python/F3DPythonBindings.cxx | 8 ++++---- python/testing/test_camera.py | 32 ++++++++++++++++---------------- 6 files changed, 50 insertions(+), 51 deletions(-) diff --git a/application/F3DStarter.cxx b/application/F3DStarter.cxx index 8397737cc7..da29e64c5e 100644 --- a/application/F3DStarter.cxx +++ b/application/F3DStarter.cxx @@ -249,10 +249,10 @@ class F3DStarter::F3DInternals return ss.str(); }; cameraMetadata << "{\n"; - cameraMetadata << " \"pos\": " << vec3toJson(state.pos) << ",\n"; - cameraMetadata << " \"foc\": " << vec3toJson(state.foc) << ",\n"; - cameraMetadata << " \"up\": " << vec3toJson(state.up) << ",\n"; - cameraMetadata << " \"angle\": " << state.angle << "\n"; + cameraMetadata << " \"position\": " << vec3toJson(state.position) << ",\n"; + cameraMetadata << " \"focalPoint\": " << vec3toJson(state.focalPoint) << ",\n"; + cameraMetadata << " \"viewUp\": " << vec3toJson(state.viewUp) << ",\n"; + cameraMetadata << " \"viewAngle\": " << state.viewAngle << "\n"; cameraMetadata << "}\n"; } diff --git a/library/public/camera.h b/library/public/camera.h index dcdf8d8230..bba321b1a1 100644 --- a/library/public/camera.h +++ b/library/public/camera.h @@ -11,10 +11,10 @@ namespace f3d { struct F3D_EXPORT camera_state_t { - point3_t pos = { 0., 0., 1. }; - point3_t foc = { 0., 0., 0. }; - vector3_t up = { 0., 1., 0. }; - angle_deg_t angle = 30.; + point3_t position = { 0., 0., 1. }; + point3_t focalPoint = { 0., 0., 0. }; + vector3_t viewUp = { 0., 1., 0. }; + angle_deg_t viewAngle = 30.; }; /** diff --git a/library/src/camera_impl.cxx b/library/src/camera_impl.cxx index a19de84de3..68e72f9635 100644 --- a/library/src/camera_impl.cxx +++ b/library/src/camera_impl.cxx @@ -128,10 +128,10 @@ void camera_impl::getViewAngle(angle_deg_t& angle) camera& camera_impl::setState(const camera_state_t& state) { vtkCamera* cam = this->GetVTKCamera(); - cam->SetPosition(state.pos.data()); - cam->SetFocalPoint(state.foc.data()); - cam->SetViewUp(state.up.data()); - cam->SetViewAngle(state.angle); + cam->SetPosition(state.position.data()); + cam->SetFocalPoint(state.focalPoint.data()); + cam->SetViewUp(state.viewUp.data()); + cam->SetViewAngle(state.viewAngle); cam->OrthogonalizeViewUp(); this->Internals->VTKRenderer->ResetCameraClippingRange(); return *this; @@ -149,10 +149,10 @@ camera_state_t camera_impl::getState() void camera_impl::getState(camera_state_t& state) { vtkCamera* cam = this->GetVTKCamera(); - cam->GetPosition(state.pos.data()); - cam->GetFocalPoint(state.foc.data()); - cam->GetViewUp(state.up.data()); - state.angle = cam->GetViewAngle(); + cam->GetPosition(state.position.data()); + cam->GetFocalPoint(state.focalPoint.data()); + cam->GetViewUp(state.viewUp.data()); + state.viewAngle = cam->GetViewAngle(); } //---------------------------------------------------------------------------- camera& camera_impl::dolly(double val) diff --git a/library/src/interactor_impl.cxx b/library/src/interactor_impl.cxx index 036574f6d5..55a0ed4ed2 100644 --- a/library/src/interactor_impl.cxx +++ b/library/src/interactor_impl.cxx @@ -326,33 +326,32 @@ class interactor_impl::internals const camera_state_t state = self->Window.getCamera().getState(); double focV[3]; - vtkMath::Subtract(picked, state.foc.data(), focV); /* foc -> picked */ + vtkMath::Subtract(picked, state.focalPoint.data(), focV); /* foc -> picked */ double posV[3]; - vtkMath::Subtract(picked, state.foc.data(), posV); /* pos -> pos1, parallel to focV */ + vtkMath::Subtract( + picked, state.focalPoint.data(), posV); /* pos -> pos1, parallel to focV */ if (!self->Style->GetInteractor()->GetShiftKey()) { double v[3]; - vtkMath::Subtract(state.foc.data(), state.pos.data(), v); /* pos -> foc */ - vtkMath::ProjectVector(focV, v, v); /* pos2 -> pos1 */ + vtkMath::Subtract(state.focalPoint.data(), state.position.data(), v); /* pos -> foc */ + vtkMath::ProjectVector(focV, v, v); /* pos2 -> pos1 */ vtkMath::Subtract(posV, v, posV); /* pos -> pos2, keeps on camera plane */ } const auto interpolateCameraState = [&state, &focV, &posV](double ratio) -> camera_state_t { - return { // + return { { + state.position[0] + posV[0] * ratio, + state.position[1] + posV[1] * ratio, + state.position[2] + posV[2] * ratio, + }, { - state.pos[0] + posV[0] * ratio, - state.pos[1] + posV[1] * ratio, - state.pos[2] + posV[2] * ratio, + state.focalPoint[0] + focV[0] * ratio, + state.focalPoint[1] + focV[1] * ratio, + state.focalPoint[2] + focV[2] * ratio, }, - { - state.foc[0] + focV[0] * ratio, - state.foc[1] + focV[1] * ratio, - state.foc[2] + focV[2] * ratio, - }, - state.up, state.angle - }; + state.viewUp, state.viewAngle }; }; self->AnimateCameraTransition(interpolateCameraState); diff --git a/python/F3DPythonBindings.cxx b/python/F3DPythonBindings.cxx index 24b5f96dd6..480335315d 100644 --- a/python/F3DPythonBindings.cxx +++ b/python/F3DPythonBindings.cxx @@ -324,10 +324,10 @@ PYBIND11_MODULE(pyf3d, module) .def(py::init<>()) .def(py::init()) - .def_readwrite("pos", &f3d::camera_state_t::pos) - .def_readwrite("foc", &f3d::camera_state_t::foc) - .def_readwrite("up", &f3d::camera_state_t::up) - .def_readwrite("angle", &f3d::camera_state_t::angle); + .def_readwrite("position", &f3d::camera_state_t::position) + .def_readwrite("focal_point", &f3d::camera_state_t::focalPoint) + .def_readwrite("view_up", &f3d::camera_state_t::viewUp) + .def_readwrite("view_angle", &f3d::camera_state_t::viewAngle); // f3d::window py::class_> window(module, "Window"); diff --git a/python/testing/test_camera.py b/python/testing/test_camera.py index c875a3ead2..1341ddb74b 100644 --- a/python/testing/test_camera.py +++ b/python/testing/test_camera.py @@ -36,10 +36,10 @@ def test_get_state(): camera.view_up = up camera.view_angle = angle - assert camera.state.pos == pos - assert camera.state.foc == foc - assert camera.state.up == up - assert camera.state.angle == angle + assert camera.state.position == pos + assert camera.state.focal_point == foc + assert camera.state.view_up == up + assert camera.state.view_angle == angle def test_set_state(): @@ -49,18 +49,18 @@ def test_set_state(): state = f3d.CameraState((1, 2, 3), (1, 22, 3), (0, 0, 1), 32) camera.state = state - assert camera.position == state.pos - assert camera.focal_point == state.foc - assert camera.view_up == state.up - assert camera.view_angle == state.angle + assert camera.position == state.position + assert camera.focal_point == state.focal_point + assert camera.view_up == state.view_up + assert camera.view_angle == state.view_angle def test_default_state(): new_state = f3d.CameraState() - assert new_state.pos == (0, 0, 1) - assert new_state.foc == (0, 0, 0) - assert new_state.up == (0, 1, 0) - assert new_state.angle == 30 + assert new_state.position == (0, 0, 1) + assert new_state.focal_point == (0, 0, 0) + assert new_state.view_up == (0, 1, 0) + assert new_state.view_angle == 30 @pytest.mark.xfail(reason="CameraState equality not implemented") @@ -92,13 +92,13 @@ def test_pan(): camera.state = f3d.CameraState((1, 2, 3), (1, 2, 13), (0, 1, 0), 40) camera.pan(1, 2) - assert camera.state.pos == (0, 4, 3) - assert camera.state.foc == (0, 4, 13) + assert camera.state.position == (0, 4, 3) + assert camera.state.focal_point == (0, 4, 13) camera.state = f3d.CameraState((1, 2, 3), (1, -2, 3), (0, 0, 1), 40) camera.pan(3, 4, 5) - assert camera.state.pos == (-2, -3, 7) - assert camera.state.foc == (-2, -7, 7) + assert camera.state.position == (-2, -3, 7) + assert camera.state.focal_point == (-2, -7, 7) def test_resets():