Skip to main content

viewer/
viewer.rs

1//! Interactive viewer demo: a box fused with a cylinder.
2//!
3//! Run (needs a display server and the `window` feature):
4//!
5//! ```text
6//! cargo run -p brepkit-render --example viewer --features window
7//! ```
8//!
9//! Controls:
10//! - Left-drag: orbit
11//! - Right-drag (or Shift + left-drag): pan
12//! - Scroll: zoom
13//! - Left click on a face: highlight it (click again to clear)
14//!
15//! Each highlighted face corresponds to a kernel `FaceId`, read back from the
16//! GPU id buffer under the cursor.
17
18use brepkit_math::mat::Mat4;
19use brepkit_operations::boolean::{BooleanOp, boolean};
20use brepkit_operations::primitives::{make_box, make_cylinder};
21use brepkit_operations::transform::transform_solid;
22use brepkit_render::{ViewOpts, view_solid};
23use brepkit_topology::Topology;
24
25fn main() -> Result<(), Box<dyn std::error::Error>> {
26    // A 40x40x20 box with a cylinder rising through its top, fused into one
27    // solid so the viewer can show (and pick) the combined faces.
28    let mut topo = Topology::new();
29
30    let box_solid = make_box(&mut topo, 40.0, 40.0, 20.0)?;
31
32    // Cylinder base at z=0; lift it so it spans the box and protrudes above.
33    let cyl = make_cylinder(&mut topo, 10.0, 35.0)?;
34    transform_solid(&mut topo, cyl, &Mat4::translation(20.0, 20.0, 0.0))?;
35
36    let solid = boolean(&mut topo, BooleanOp::Fuse, box_solid, cyl)?;
37
38    let opts = ViewOpts::new("brepkit viewer — box + cylinder (click a face)");
39    view_solid(&topo, solid, &opts)?;
40    Ok(())
41}