polyscope_rs/init.rs
1//! Initialization and lifecycle management for polyscope-rs.
2//!
3//! This module provides the core functions to initialize, run, and shut down
4//! the polyscope visualization system.
5
6use crate::Result;
7
8/// Initializes polyscope with default settings.
9///
10/// This must be called before any other polyscope functions. It sets up the
11/// global state required for structure registration and rendering.
12///
13/// # Errors
14///
15/// Returns an error if polyscope has already been initialized.
16///
17/// # Example
18///
19/// ```no_run
20/// use polyscope_rs::*;
21///
22/// fn main() -> Result<()> {
23/// init()?;
24/// // Now you can register structures and call show()
25/// Ok(())
26/// }
27/// ```
28pub fn init() -> Result<()> {
29 polyscope_core::state::init_context()?;
30 log::info!("polyscope-rs initialized");
31 Ok(())
32}
33
34/// Returns whether polyscope has been initialized.
35#[must_use]
36pub fn is_initialized() -> bool {
37 polyscope_core::state::is_initialized()
38}
39
40/// Shuts down polyscope and releases all resources.
41///
42/// This clears all registered structures and resets the global state.
43/// After calling this, you can call [`init()`] again to reinitialize.
44///
45/// Note: This is typically not needed as resources are cleaned up when
46/// the program exits. It's mainly useful for tests or when you need to
47/// reset the visualization state.
48pub fn shutdown() {
49 polyscope_core::state::shutdown_context();
50 log::info!("polyscope-rs shut down");
51}
52
53/// Shows the polyscope viewer window.
54///
55/// This function opens the interactive 3D viewer and blocks until the window
56/// is closed (by pressing ESC or clicking the close button).
57///
58/// Before calling `show()`, you should:
59/// 1. Call [`init()`] to initialize polyscope
60/// 2. Register structures using `register_*()` functions
61/// 3. Optionally add quantities to structures
62///
63/// # Example
64///
65/// ```no_run
66/// use polyscope_rs::*;
67///
68/// fn main() -> Result<()> {
69/// init()?;
70///
71/// // Register some geometry
72/// let points = vec![Vec3::ZERO, Vec3::X, Vec3::Y];
73/// register_point_cloud("my points", points);
74///
75/// // Open the viewer (blocks until closed)
76/// show();
77///
78/// Ok(())
79/// }
80/// ```
81pub fn show() {
82 let _ = env_logger::try_init();
83 crate::app::run_app();
84}