Skip to main content

polyscope_rs/
curve_network.rs

1//! Curve network registration and manipulation.
2//!
3//! Curve networks represent graphs, paths, or lines as nodes connected by edges.
4//! They can be rendered as lines or tubes, with quantities (scalar, vector, color)
5//! defined on nodes or edges.
6//!
7//! # Example
8//!
9//! ```no_run
10//! use polyscope_rs::*;
11//!
12//! fn main() -> Result<()> {
13//!     init()?;
14//!
15//!     // Create a simple path
16//!     let nodes = vec![
17//!         Vec3::new(0.0, 0.0, 0.0),
18//!         Vec3::new(1.0, 0.5, 0.0),
19//!         Vec3::new(2.0, 0.0, 0.0),
20//!     ];
21//!     let cn = register_curve_network_line("my path", nodes);
22//!     cn.set_radius(0.02, false); // absolute radius
23//!     cn.set_color(Vec3::new(1.0, 0.5, 0.0));
24//!
25//!     show();
26//!     Ok(())
27//! }
28//! ```
29
30use crate::{CurveNetwork, Vec3, with_context_mut};
31
32/// Registers a curve network with explicit edges.
33pub fn register_curve_network(
34    name: impl Into<String>,
35    nodes: Vec<Vec3>,
36    edges: Vec<[u32; 2]>,
37) -> CurveNetworkHandle {
38    let name = name.into();
39    let cn = CurveNetwork::new(name.clone(), nodes, edges);
40
41    with_context_mut(|ctx| {
42        ctx.registry
43            .register(Box::new(cn))
44            .expect("failed to register curve network");
45        ctx.update_extents();
46    });
47
48    CurveNetworkHandle { name }
49}
50
51/// Registers a curve network as a connected line (0-1-2-3-...).
52pub fn register_curve_network_line(
53    name: impl Into<String>,
54    nodes: Vec<Vec3>,
55) -> CurveNetworkHandle {
56    let name = name.into();
57    let cn = CurveNetwork::new_line(name.clone(), nodes);
58
59    with_context_mut(|ctx| {
60        ctx.registry
61            .register(Box::new(cn))
62            .expect("failed to register curve network");
63        ctx.update_extents();
64    });
65
66    CurveNetworkHandle { name }
67}
68
69/// Registers a curve network as a closed loop (0-1-2-...-n-0).
70pub fn register_curve_network_loop(
71    name: impl Into<String>,
72    nodes: Vec<Vec3>,
73) -> CurveNetworkHandle {
74    let name = name.into();
75    let cn = CurveNetwork::new_loop(name.clone(), nodes);
76
77    with_context_mut(|ctx| {
78        ctx.registry
79            .register(Box::new(cn))
80            .expect("failed to register curve network");
81        ctx.update_extents();
82    });
83
84    CurveNetworkHandle { name }
85}
86
87/// Registers a curve network as separate segments (0-1, 2-3, 4-5, ...).
88pub fn register_curve_network_segments(
89    name: impl Into<String>,
90    nodes: Vec<Vec3>,
91) -> CurveNetworkHandle {
92    let name = name.into();
93    let cn = CurveNetwork::new_segments(name.clone(), nodes);
94
95    with_context_mut(|ctx| {
96        ctx.registry
97            .register(Box::new(cn))
98            .expect("failed to register curve network");
99        ctx.update_extents();
100    });
101
102    CurveNetworkHandle { name }
103}
104
105impl_structure_accessors! {
106    get_fn = get_curve_network,
107    with_fn = with_curve_network,
108    with_ref_fn = with_curve_network_ref,
109    handle = CurveNetworkHandle,
110    type_name = "CurveNetwork",
111    rust_type = CurveNetwork,
112    doc_name = "curve network"
113}
114
115/// Handle for a registered curve network.
116#[derive(Clone)]
117pub struct CurveNetworkHandle {
118    name: String,
119}
120
121impl CurveNetworkHandle {
122    /// Returns the name of this curve network.
123    #[must_use]
124    pub fn name(&self) -> &str {
125        &self.name
126    }
127
128    /// Sets the base color.
129    pub fn set_color(&self, color: Vec3) -> &Self {
130        with_curve_network(&self.name, |cn| {
131            cn.set_color(color);
132        });
133        self
134    }
135
136    /// Sets the radius.
137    pub fn set_radius(&self, radius: f32, is_relative: bool) -> &Self {
138        with_curve_network(&self.name, |cn| {
139            cn.set_radius(radius, is_relative);
140        });
141        self
142    }
143
144    /// Sets the material.
145    pub fn set_material(&self, material: &str) -> &Self {
146        with_curve_network(&self.name, |cn| {
147            cn.set_material(material);
148        });
149        self
150    }
151}