Skip to main content

polyscope_rs/
camera_view.rs

1//! Camera view registration and manipulation.
2//!
3//! Camera views visualize camera poses as frustum widgets in the scene.
4//! Useful for visualizing camera trajectories, multi-view setups, or
5//! debugging camera calibration.
6//!
7//! # Example
8//!
9//! ```no_run
10//! use polyscope_rs::*;
11//!
12//! fn main() -> Result<()> {
13//!     init()?;
14//!
15//!     let cam = register_camera_view_look_at(
16//!         "my camera",
17//!         Vec3::new(2.0, 1.0, 2.0),  // position
18//!         Vec3::ZERO,                 // target
19//!         Vec3::Y,                    // up
20//!         60.0,                       // fov (degrees)
21//!         1.5,                        // aspect ratio
22//!     );
23//!     cam.set_widget_focal_length(0.3, false); // absolute length
24//!
25//!     show();
26//!     Ok(())
27//! }
28//! ```
29
30use crate::{CameraParameters, CameraView, Vec3, with_context_mut};
31
32/// Registers a camera view with polyscope using camera parameters.
33pub fn register_camera_view(name: impl Into<String>, params: CameraParameters) -> CameraViewHandle {
34    let name = name.into();
35    let camera_view = CameraView::new(name.clone(), params);
36
37    with_context_mut(|ctx| {
38        ctx.registry
39            .register(Box::new(camera_view))
40            .expect("failed to register camera view");
41        ctx.update_extents();
42    });
43
44    CameraViewHandle { name }
45}
46
47/// Registers a camera view from position, target, and up direction.
48pub fn register_camera_view_look_at(
49    name: impl Into<String>,
50    position: Vec3,
51    target: Vec3,
52    up: Vec3,
53    fov_vertical_degrees: f32,
54    aspect_ratio: f32,
55) -> CameraViewHandle {
56    let params =
57        CameraParameters::look_at(position, target, up, fov_vertical_degrees, aspect_ratio);
58    register_camera_view(name, params)
59}
60
61impl_structure_accessors! {
62    get_fn = get_camera_view,
63    with_fn = with_camera_view,
64    with_ref_fn = with_camera_view_ref,
65    handle = CameraViewHandle,
66    type_name = "CameraView",
67    rust_type = CameraView,
68    doc_name = "camera view"
69}
70
71/// Handle for a registered camera view.
72#[derive(Clone)]
73pub struct CameraViewHandle {
74    name: String,
75}
76
77impl CameraViewHandle {
78    /// Returns the name of this camera view.
79    #[must_use]
80    pub fn name(&self) -> &str {
81        &self.name
82    }
83
84    /// Sets the widget color.
85    pub fn set_color(&self, color: Vec3) -> &Self {
86        with_camera_view(&self.name, |cv| {
87            cv.set_color(color);
88        });
89        self
90    }
91
92    /// Sets the widget focal length.
93    pub fn set_widget_focal_length(&self, length: f32, is_relative: bool) -> &Self {
94        with_camera_view(&self.name, |cv| {
95            cv.set_widget_focal_length(length, is_relative);
96        });
97        self
98    }
99
100    /// Sets the widget thickness.
101    pub fn set_widget_thickness(&self, thickness: f32) -> &Self {
102        with_camera_view(&self.name, |cv| {
103            cv.set_widget_thickness(thickness);
104        });
105        self
106    }
107
108    /// Updates the camera parameters.
109    pub fn set_params(&self, params: CameraParameters) -> &Self {
110        with_camera_view(&self.name, |cv| {
111            cv.set_params(params);
112        });
113        self
114    }
115}