Skip to main content

bevy_shape/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(
3    any(docsrs, docsrs_dep),
4    expect(
5        internal_features,
6        reason = "rustdoc_internals is needed for fake_variadic"
7    )
8)]
9#![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))]
10#![cfg_attr(docsrs, feature(doc_cfg))]
11#![doc(
12    html_logo_url = "https://bevy.org/assets/icon.png",
13    html_favicon_url = "https://bevy.org/assets/icon.png"
14)]
15#![no_std]
16
17//! This module defines primitive shapes.
18//! The origin is (0, 0) for 2D primitives and (0, 0, 0) for 3D primitives,
19//! unless stated otherwise.
20
21#[cfg(feature = "std")]
22extern crate std;
23
24#[cfg(feature = "alloc")]
25extern crate alloc;
26
27mod dim2;
28pub use dim2::*;
29mod dim3;
30pub use dim3::*;
31mod half_space;
32pub use half_space::*;
33mod view_frustum;
34pub use view_frustum::*;
35mod inset;
36pub use inset::*;
37mod ray;
38pub use ray::{Ray2d, Ray3d};
39mod bounding;
40pub use bounding::*;
41
42#[cfg(feature = "rand")]
43pub mod sampling;
44#[cfg(feature = "rand")]
45pub use sampling::ShapeSample;
46
47#[cfg(feature = "alloc")]
48mod polygon;
49#[cfg(feature = "alloc")]
50pub use polygon::*;
51
52/// A marker trait for 2D primitives
53pub trait Primitive2d {}
54
55/// A marker trait for 3D primitives
56pub trait Primitive3d {}
57
58impl Primitive2d for bevy_math::Dir2 {}
59impl Primitive3d for bevy_math::Dir3 {}
60impl Primitive3d for bevy_math::Dir3A {}
61
62/// The winding order for a set of points
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64#[doc(alias = "Orientation")]
65pub enum WindingOrder {
66    /// A clockwise winding order
67    Clockwise,
68    /// A counterclockwise winding order
69    #[doc(alias = "AntiClockwise")]
70    CounterClockwise,
71    /// An invalid winding order indicating that it could not be computed reliably.
72    /// This often happens in *degenerate cases* where the points lie on the same line
73    #[doc(alias("Degenerate", "Collinear"))]
74    Invalid,
75}
76
77/// A trait for getting measurements of 2D shapes
78pub trait Measured2d {
79    /// Get the perimeter of the shape
80    fn perimeter(&self) -> f32;
81
82    /// Get the area of the shape
83    fn area(&self) -> f32;
84}
85
86/// A trait for getting measurements of 3D shapes
87pub trait Measured3d {
88    /// Get the surface area of the shape
89    fn area(&self) -> f32;
90
91    /// Get the volume of the shape
92    fn volume(&self) -> f32;
93}
94
95/// The shape prelude.
96///
97/// This includes all primitive shape types in this crate, re-exported for your convenience.
98pub mod prelude {
99    // just re-export everything, it's just shape definitions anyways
100    #[doc(hidden)]
101    pub use crate::{Measured2d, Measured3d, Primitive2d, Primitive3d, WindingOrder};
102
103    #[doc(hidden)]
104    pub use crate::bounding::*;
105
106    #[doc(hidden)]
107    pub use crate::dim2::*;
108
109    #[doc(hidden)]
110    pub use crate::dim3::*;
111
112    #[doc(hidden)]
113    pub use crate::half_space::*;
114
115    #[doc(hidden)]
116    pub use crate::inset::*;
117
118    #[doc(hidden)]
119    pub use crate::ray::{Ray2d, Ray3d};
120
121    #[doc(hidden)]
122    pub use crate::view_frustum::*;
123
124    #[doc(hidden)]
125    #[cfg(feature = "rand")]
126    pub use crate::sampling::ShapeSample;
127
128    #[doc(hidden)]
129    #[cfg(feature = "alloc")]
130    pub use crate::polygon::*;
131}