Skip to main content

section_properties/
section_library.rs

1//! Pre-defined section library (mirrors Python sectionproperties.pre.library)
2//!
3//! Provides common hot-rolled and cold-formed steel sections, concrete sections,
4//! and primitive shapes. All dimensions in meters (SI base units).
5
6pub mod cold_formed;
7pub mod composite;
8pub mod concrete;
9pub mod primitive;
10pub mod steel;
11pub mod timber;
12
13#[allow(ambiguous_glob_reexports)]
14pub use cold_formed::*;
15#[allow(ambiguous_glob_reexports)]
16pub use composite::*;
17#[allow(ambiguous_glob_reexports)]
18pub use concrete::*;
19#[allow(ambiguous_glob_reexports)]
20pub use primitive::*;
21#[allow(ambiguous_glob_reexports)]
22pub use steel::*;
23#[allow(ambiguous_glob_reexports)]
24pub use timber::*;
25
26use crate::geometry::{Point, Polygon};
27use crate::material::Material;
28use crate::section::Section;
29
30/// Trait for all parametric sections that can build their geometry.
31pub trait ParametricSection: core::fmt::Debug {
32    /// Build the section geometry (outer boundary + holes).
33    fn build(&self) -> Section;
34
35    /// Build the section as a composite section with material groups.
36    fn build_composite(&self, material: Material) -> CompositeSection {
37        CompositeSection::single_material(self.build(), material)
38    }
39
40    /// Get the section name/designation.
41    fn designation(&self) -> String;
42
43    /// Get approximate mass per unit length [kg/m].
44    fn mass_per_length(&self, material: &Material) -> f64 {
45        let sec = self.build();
46        sec.area() * material.density
47    }
48}
49
50/// Helper: create a rectangle centered at origin.
51pub fn rectangle_polygon(width: f64, height: f64) -> Polygon {
52    let hw = width / 2.0;
53    let hh = height / 2.0;
54    Polygon::new(vec![
55        Point::new(-hw, -hh),
56        Point::new(hw, -hh),
57        Point::new(hw, hh),
58        Point::new(-hw, hh),
59    ])
60}
61
62/// Helper: create a circle approximated by n vertices, centered at origin.
63pub fn circle_polygon(radius: f64, n: usize) -> Polygon {
64    let mut vertices = Vec::with_capacity(n);
65    for i in 0..n {
66        let theta = 2.0 * std::f64::consts::PI * i as f64 / n as f64;
67        vertices.push(Point::new(radius * theta.cos(), radius * theta.sin()));
68    }
69    Polygon::new(vertices)
70}
71
72/// Helper: create a hollow circle (tube) approximated by n vertices.
73pub fn hollow_circle_polygon(outer_radius: f64, inner_radius: f64, n: usize) -> (Polygon, Polygon) {
74    let outer = circle_polygon(outer_radius, n);
75    let mut inner_vertices = Vec::with_capacity(n);
76    // CW for hole
77    for i in (0..n).rev() {
78        let theta = 2.0 * std::f64::consts::PI * i as f64 / n as f64;
79        inner_vertices.push(Point::new(
80            inner_radius * theta.cos(),
81            inner_radius * theta.sin(),
82        ));
83    }
84    let inner = Polygon::new(inner_vertices);
85    (outer, inner)
86}
87
88/// Helper: round rectangle corners with fillet radius.
89pub fn rounded_rectangle_polygon(
90    width: f64,
91    height: f64,
92    radius: f64,
93    n_per_corner: usize,
94) -> Polygon {
95    let hw = width / 2.0;
96    let hh = height / 2.0;
97    let r = radius.min(hw).min(hh);
98
99    let mut vertices = Vec::new();
100
101    // Start from bottom-left corner, going CCW
102    // Bottom edge (left to right)
103    vertices.push(Point::new(-hw + r, -hh));
104    vertices.push(Point::new(hw - r, -hh));
105
106    // Bottom-right corner (quarter circle, CW from -π/2 to 0 -> but we want CCW overall)
107    for i in 0..=n_per_corner {
108        let theta = -std::f64::consts::FRAC_PI_2
109            + std::f64::consts::FRAC_PI_2 * i as f64 / n_per_corner as f64;
110        vertices.push(Point::new(
111            hw - r + r * theta.cos(),
112            -hh + r + r * theta.sin(),
113        ));
114    }
115
116    // Right edge (bottom to top)
117    vertices.push(Point::new(hw, -hh + r));
118    vertices.push(Point::new(hw, hh - r));
119
120    // Top-right corner
121    for i in 0..=n_per_corner {
122        let theta = 0.0 + std::f64::consts::FRAC_PI_2 * i as f64 / n_per_corner as f64;
123        vertices.push(Point::new(
124            hw - r + r * theta.cos(),
125            hh - r + r * theta.sin(),
126        ));
127    }
128
129    // Top edge (right to left)
130    vertices.push(Point::new(hw - r, hh));
131    vertices.push(Point::new(-hw + r, hh));
132
133    // Top-left corner
134    for i in 0..=n_per_corner {
135        let theta = std::f64::consts::FRAC_PI_2
136            + std::f64::consts::FRAC_PI_2 * i as f64 / n_per_corner as f64;
137        vertices.push(Point::new(
138            -hw + r + r * theta.cos(),
139            hh - r + r * theta.sin(),
140        ));
141    }
142
143    // Left edge (top to bottom)
144    vertices.push(Point::new(-hw, hh - r));
145    vertices.push(Point::new(-hw, -hh + r));
146
147    // Bottom-left corner
148    for i in 0..=n_per_corner {
149        let theta =
150            std::f64::consts::PI + std::f64::consts::FRAC_PI_2 * i as f64 / n_per_corner as f64;
151        vertices.push(Point::new(
152            -hw + r + r * theta.cos(),
153            -hh + r + r * theta.sin(),
154        ));
155    }
156
157    Polygon::new(vertices)
158}
159
160/// Generate points along an arc (mirrors Python `draw_radius`).
161///
162/// Centre at `pt`, radius `r`, starting angle `theta` (radians), `n` points.
163/// `ccw` = counter-clockwise; `phi` = arc extent (radians, default π/2).
164pub fn draw_radius(pt: Point, r: f64, theta: f64, n: usize, ccw: bool, phi: f64) -> Vec<Point> {
165    if r == 0.0 {
166        return vec![pt];
167    }
168    let mult = if ccw { 1.0 } else { -1.0 };
169    let mut points = Vec::with_capacity(n);
170    for i in 0..n {
171        let t = theta + mult * i as f64 / (n.max(1) - 1).max(1) as f64 * phi;
172        points.push(Point::new(pt.x + r * t.cos(), pt.y + r * t.sin()));
173    }
174    points
175}
176
177/// Rotate a point counter-clockwise about the origin by `angle` (radians).
178pub fn rotate_point(point: Point, angle: f64) -> Point {
179    let c = angle.cos();
180    let s = angle.sin();
181    Point::new(c * point.x - s * point.y, s * point.x + c * point.y)
182}
183
184#[cfg(test)]
185mod tests {
186    use super::*;
187
188    #[test]
189    fn rectangle_polygon_area() {
190        let poly = rectangle_polygon(0.2, 0.1);
191        assert!((poly.area() - 0.02).abs() < 1e-10);
192    }
193
194    #[test]
195    fn circle_polygon_area() {
196        let poly = circle_polygon(0.1, 100);
197        assert!((poly.area() - std::f64::consts::PI * 0.01).abs() < 1e-4);
198    }
199
200    #[test]
201    fn hollow_circle_area() {
202        let (outer, inner) = hollow_circle_polygon(0.1, 0.05, 100);
203        let outer_area = outer.area();
204        let inner_area = inner.area();
205        let expected = std::f64::consts::PI * (0.1_f64.powi(2) - 0.05_f64.powi(2));
206        assert!((outer_area - inner_area - expected).abs() < 1e-4);
207    }
208
209    #[test]
210    fn rounded_rectangle_area() {
211        let poly = rounded_rectangle_polygon(0.2, 0.1, 0.01, 16);
212        let expected = 0.02 - (4.0 - std::f64::consts::PI) * 0.01_f64.powi(2);
213        assert!((poly.area() - expected).abs() < 1e-6);
214    }
215}