Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change camera API to be more explicit #1820

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions application/F3DStarter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
8 changes: 4 additions & 4 deletions library/public/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.;
};

/**
Expand Down
16 changes: 8 additions & 8 deletions library/src/camera_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ PYBIND11_MODULE(pyf3d, module)
.def(py::init<>())
.def(py::init<const f3d::point3_t&, const f3d::point3_t&, const f3d::vector3_t&,
const f3d::angle_deg_t&>())
.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_<f3d::window, std::unique_ptr<f3d::window, py::nodelete>> window(module, "Window");
Expand Down
32 changes: 16 additions & 16 deletions python/testing/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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")
Expand Down Expand Up @@ -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():
Expand Down
Loading