nannou_core/geom/
mod.rs

1//! Types, functions and other items related to geometry. This module is the source of all graphics
2//! and lazer primitives and aids work in 2D and 3D space.
3//!
4//! Each module provides a set of general tools for working with the named geometry including:
5//!
6//! - A typed, object representation.
7//! - Functions for producing vertices, triangles and triangulation indices.
8//! - Functions for checking whether or not the geometry contains a point.
9//! - Functions for determining the bounding rectangle or cuboid.
10//! - A function for finding the centroid.
11
12pub mod cuboid;
13pub mod ellipse;
14pub mod point;
15pub mod polygon;
16pub mod quad;
17pub mod range;
18pub mod rect;
19pub mod scalar;
20pub mod tri;
21pub mod vector;
22pub mod vertex;
23
24pub use self::cuboid::Cuboid;
25pub use self::ellipse::Ellipse;
26pub use self::point::{pt2, pt3, pt4, Point2, Point3, Point4};
27pub use self::polygon::Polygon;
28pub use self::quad::Quad;
29pub use self::range::{Align, Edge, Range};
30pub use self::rect::{Corner, Padding, Rect};
31pub use self::scalar::Scalar;
32pub use self::tri::Tri;
33#[allow(deprecated)]
34pub use self::vector::{Vector2, Vector3, Vector4};
35pub use self::vertex::{Vertex, Vertex2d, Vertex3d};
36pub use glam::{
37    dvec2, dvec3, dvec4, ivec2, ivec3, ivec4, vec2, vec3, vec4, DVec2, DVec3, DVec4, IVec2, IVec3,
38    IVec4, Vec2, Vec3, Vec4,
39};
40
41// General geometry utility functions
42
43/// The `Rect` that bounds the given sequence of vertices.
44///
45/// Returns `None` if the given iterator is empty.
46pub fn bounding_rect<I>(vertices: I) -> Option<Rect<<I::Item as Vertex>::Scalar>>
47where
48    I: IntoIterator,
49    I::Item: Vertex2d,
50{
51    let mut vertices = vertices.into_iter();
52    vertices.next().map(|first| {
53        let [x, y] = first.point2();
54        let bounds = Rect {
55            x: Range::new(x, x),
56            y: Range::new(y, y),
57        };
58        vertices.fold(bounds, |b, v| b.stretch_to_point(v.point2()))
59    })
60}
61
62/// The `Cuboid` that bounds the given sequence of vertices.
63///
64/// Returns `None` if the given iterator is empty.
65pub fn bounding_cuboid<I>(vertices: I) -> Option<Cuboid<<I::Item as Vertex>::Scalar>>
66where
67    I: IntoIterator,
68    I::Item: Vertex3d,
69{
70    let mut vertices = vertices.into_iter();
71    vertices.next().map(|first| {
72        let [x, y, z] = first.point3();
73        let bounds = Cuboid {
74            x: Range::new(x, x),
75            y: Range::new(y, y),
76            z: Range::new(z, z),
77        };
78        vertices.fold(bounds, |b, v| b.stretch_to_point(v.point3()))
79    })
80}
81
82/// The `centroid` (average position) of all vertices in the given iterator.
83///
84/// Returns `None` if the given iterator contains no vertices.
85pub fn centroid<I>(vertices: I) -> Option<I::Item>
86where
87    I: IntoIterator,
88    I::Item: vertex::Average,
89{
90    <I::Item as vertex::Average>::average(vertices)
91}