Skip to main content

physdes/
lib.rs

1//! # physdes-rs
2//!
3//! A library for Physical Design in Rust with geometric operations and algorithms.
4//!
5//! ## Overview
6//!
7//! ```svgbob
8//!    Point (x, y)
9//!         *
10//!        /|\
11//!       / | \
12//!      /  |  \
13//!     /   |   \
14//!    *----*----*
15//! Interval [lb, ub]
16//!
17//!  Vector2 (x, y)
18//!      -->
19//!     (dx, dy)
20//! ```
21//!
22//! ## Main Components
23//!
24//! The library provides several geometric structures:
25//!
26//! - `Point<T1, T2>`: A 2D point with x and y coordinates
27//! - `Vector2<T1, T2>`: A 2D vector with x and y components
28//! - `Interval<T>`: A range with lower and upper bounds
29//! - `Polygon<T>`: An arbitrary polygon
30//! - `RPolygon<T>`: A rectilinear polygon
31//! - `GeomError`: Error types for geometric operations
32//! - `vlsi_ops`: VLSI-specific geometric operations
33//! - `algorithms`: Additional geometric algorithms
34//!
35//! # Examples
36//!
37//! ```
38//! use physdes::{Point, Vector2};
39//! use physdes::interval::Interval;
40//! use physdes::polygon::Polygon as Poly;
41//!
42//! // Create a point
43//! let p = Point::new(3, 4);
44//! assert_eq!(p.xcoord, 3);
45//! assert_eq!(p.ycoord, 4);
46//!
47//! // Create a vector
48//! let v = Vector2::new(1, 2);
49//! assert_eq!(v.x_, 1);
50//! assert_eq!(v.y_, 2);
51//!
52//! // Create an interval
53//! let interval = Interval::new(1, 5);
54//! assert_eq!(interval.lb(), 1);
55//! assert_eq!(interval.ub(), 5);
56//!
57//! // Create a polygon from points
58//! let points = vec![Point::new(0, 0), Point::new(1, 0), Point::new(1, 1), Point::new(0, 1)];
59//! let polygon = Poly::new(&points);
60//! assert_eq!(polygon.origin, Point::new(0, 0));
61//! ```
62//!
63/// Geometric algorithms module
64pub mod algorithms;
65/// Error types for geometric operations
66pub mod error;
67/// Generic traits for geometric operations
68pub mod generic;
69/// Interval operations and types
70pub mod interval;
71/// Merge object for combining geometric objects
72pub mod merge_obj;
73/// Point types and operations
74pub mod point;
75/// Polygon types and operations
76pub mod polygon;
77/// Rectilinear polygon types and operations
78pub mod rpolygon;
79/// Vector2 types and operations
80pub mod vector2;
81/// VLSI-specific geometric operations
82pub mod vlsi_ops;
83
84/// Logging module - available when `std` feature is enabled.
85#[cfg(feature = "std")]
86pub mod logging;
87
88pub use crate::point::Point;
89pub use crate::polygon::Polygon;
90pub use crate::rpolygon::RPolygon;
91pub use crate::vector2::Vector2;
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use quickcheck_macros::quickcheck;
97
98    #[test]
99    pub fn it_works() {
100        let pt_a = Point::<i32, i32>::new(12, 23);
101        let vec_b = Vector2::<i32, i32>::new(34, 45);
102        println!("{:?}", pt_a + vec_b);
103        println!("{:?}", pt_a - vec_b);
104
105        let mut pt_a = Point::<i32, i32>::new(42, 53);
106        pt_a += vec_b;
107        pt_a -= vec_b;
108        println!("{:?}", -pt_a);
109
110        let pt_nested = Point::<Point<i32, i32>, Point<i32, i32>>::new(pt_a, pt_a);
111        println!("{:?}", pt_nested);
112
113        let interval_x = interval::Interval::<i32>::new(12, 23);
114        // let interval_y = interval::Interval::<i32>::new(42, 53);
115        println!("{:?}", interval_x);
116    }
117
118    #[quickcheck]
119    fn check_point(ax: u16, bx: u16) -> bool {
120        let pt_a = Point::<i32, i32>::new(ax as i32, 23);
121        let vec_b = Vector2::<i32, i32>::new(bx as i32, 45);
122        pt_a == (pt_a - vec_b) + vec_b
123    }
124
125    // Additional quickcheck tests to verify build configuration
126    #[quickcheck]
127    fn check_point_arithmetic_properties(x: i16, y: i16, dx: i16, dy: i16) -> bool {
128        let pt = Point::<i32, i32>::new(x as i32, y as i32);
129        let vec = Vector2::<i32, i32>::new(dx as i32, dy as i32);
130
131        // Test associative property: (pt + vec) - vec == pt
132        let result = (pt + vec) - vec;
133        pt == result
134    }
135
136    #[quickcheck]
137    fn check_interval_properties(a: i32, b: i32) -> bool {
138        let lower = a.min(b);
139        let upper = a.max(b);
140        let interval = interval::Interval::<i32>::new(lower, upper);
141
142        // Test that the interval has correct bounds
143        interval.lb() <= interval.ub()
144    }
145
146    #[test]
147    fn test_const_functions() {
148        // Test that the const functions we added actually work in const contexts
149        const _P1: Point<i32, i32> = Point::new(1, 2);
150        const _I1: interval::Interval<i32> = interval::Interval::new(1, 5);
151        const _LB: i32 = _I1.lb();
152        const _UB: i32 = _I1.ub();
153        const _M1: merge_obj::MergeObj<i32, i32> = merge_obj::MergeObj::new(1, 2);
154    }
155}