fidget_core/
lib.rs

1//! Core infrastructure for evaluating complex closed-form implicit surfaces.
2//!
3//! ```
4//! use fidget_core::{
5//!     context::Context,
6//!     shape::EzShape,
7//!     vm::VmShape
8//! };
9//! let mut ctx = Context::new();
10//! let x = ctx.x();
11//! let y = ctx.y();
12//! let x_squared = ctx.mul(x, x)?;
13//! let y_squared = ctx.mul(y, y)?;
14//! let radius = ctx.add(x_squared, y_squared)?;
15//! let circle = ctx.sub(radius, 1.0)?;
16//!
17//! let shape = VmShape::new(&ctx, circle)?;
18//! let mut eval = VmShape::new_point_eval();
19//! let tape = shape.ez_point_tape();
20//!
21//! let (v, _trace) = eval.eval(&tape, 0.0, 0.0, 0.0)?;
22//! assert_eq!(v, -1.0);
23//!
24//! let (v, _trace) = eval.eval(&tape, 1.0, 0.0, 0.0)?;
25//! assert_eq!(v, 0.0);
26//!
27//! const N: usize = 15;
28//! for i in 0..N {
29//!     for j in 0..N {
30//!         let x = (i as f32 + 0.5) / (N as f32 / 2.0) - 1.0;
31//!         let y = (j as f32 + 0.5) / (N as f32 / 2.0) - 1.0;
32//!         let (v, _trace) = eval.eval(&tape, x, y, 0.0)?;
33//!         print!("{}", if v < 0.0 { "##" } else { "  " });
34//!     }
35//!     println!();
36//! }
37//!
38//! // This will print
39//! //           ##########
40//! //       ##################
41//! //     ######################
42//! //   ##########################
43//! //   ##########################
44//! // ##############################
45//! // ##############################
46//! // ##############################
47//! // ##############################
48//! // ##############################
49//! //   ##########################
50//! //   ##########################
51//! //     ######################
52//! //       ##################
53//! //           ##########
54//! # Ok::<(), fidget_core::Error>(())
55//! ```
56#![warn(missing_docs)]
57
58pub mod context;
59pub use context::Context;
60
61pub mod compiler;
62pub mod eval;
63pub mod render;
64pub mod shape;
65pub mod types;
66pub mod var;
67pub mod vm;
68
69mod error;
70pub use error::Error;
71
72#[cfg(test)]
73mod test {
74    use crate::Error;
75    use crate::context::*;
76    use crate::var::Var;
77
78    #[test]
79    fn it_works() {
80        let mut ctx = Context::new();
81        let x1 = ctx.x();
82        let x2 = ctx.x();
83        assert_eq!(x1, x2);
84
85        let a = ctx.constant(1.0);
86        let b = ctx.constant(1.0);
87        assert_eq!(a, b);
88        assert_eq!(ctx.get_const(a).unwrap(), 1.0);
89        assert!(matches!(ctx.get_const(x1), Err(Error::NotAConst)));
90
91        let c = ctx.add(a, b).unwrap();
92        assert_eq!(ctx.get_const(c).unwrap(), 2.0);
93
94        let c = ctx.neg(c).unwrap();
95        assert_eq!(ctx.get_const(c).unwrap(), -2.0);
96    }
97
98    #[test]
99    fn test_constant_folding() {
100        let mut ctx = Context::new();
101        let a = ctx.constant(1.0);
102        assert_eq!(ctx.len(), 1);
103        let b = ctx.constant(-1.0);
104        assert_eq!(ctx.len(), 2);
105        let _ = ctx.add(a, b);
106        assert_eq!(ctx.len(), 3);
107        let _ = ctx.add(a, b);
108        assert_eq!(ctx.len(), 3);
109        let _ = ctx.mul(a, b);
110        assert_eq!(ctx.len(), 3);
111    }
112
113    #[test]
114    fn test_eval() {
115        let mut ctx = Context::new();
116        let x = ctx.x();
117        let y = ctx.y();
118        let v = ctx.add(x, y).unwrap();
119
120        assert_eq!(
121            ctx.eval(v, &[(Var::X, 1.0), (Var::Y, 2.0)].into_iter().collect())
122                .unwrap(),
123            3.0
124        );
125        assert_eq!(ctx.eval_xyz(v, 2.0, 3.0, 0.0).unwrap(), 5.0);
126    }
127}