rapier3d/
lib.rs

1//! # Rapier
2//!
3//! Rapier is a set of two Rust crates `rapier2d` and `rapier3d` for efficient cross-platform
4//! physics simulation. It target application include video games, animation, robotics, etc.
5//!
6//! Rapier has some unique features for collaborative applications:
7//! - The ability to snapshot the state of the physics engine, and restore it later.
8//! - The ability to run a perfectly deterministic simulation on different machine, as long as they
9//!   are compliant with the IEEE 754-2008 floating point standard.
10//!
11//! User documentation for Rapier is on [the official Rapier site](https://rapier.rs/docs/).
12
13#![deny(bare_trait_objects)]
14#![warn(missing_docs)]
15#![allow(clippy::too_many_arguments)]
16#![allow(clippy::needless_range_loop)] // TODO: remove this? I find that in the math code using indices adds clarity.
17#![allow(clippy::module_inception)]
18
19#[cfg(all(feature = "dim2", feature = "f32"))]
20pub extern crate parry2d as parry;
21#[cfg(all(feature = "dim2", feature = "f64"))]
22pub extern crate parry2d_f64 as parry;
23#[cfg(all(feature = "dim3", feature = "f32"))]
24pub extern crate parry3d as parry;
25#[cfg(all(feature = "dim3", feature = "f64"))]
26pub extern crate parry3d_f64 as parry;
27
28pub extern crate nalgebra as na;
29#[cfg(feature = "serde-serialize")]
30#[macro_use]
31extern crate serde;
32extern crate num_traits as num;
33
34#[cfg(feature = "parallel")]
35pub use rayon;
36
37#[cfg(all(
38    feature = "simd-is-enabled",
39    not(feature = "simd-stable"),
40    not(feature = "simd-nightly")
41))]
42std::compile_error!(
43    "The `simd-is-enabled` feature should not be enabled explicitly. Please enable the `simd-stable` or the `simd-nightly` feature instead."
44);
45#[cfg(all(feature = "simd-is-enabled", feature = "enhanced-determinism"))]
46std::compile_error!(
47    "SIMD cannot be enabled when the `enhanced-determinism` feature is also enabled."
48);
49
50macro_rules! enable_flush_to_zero(
51    () => {
52        let _flush_to_zero = crate::utils::FlushToZeroDenormalsAreZeroFlags::flush_denormal_to_zero();
53    }
54);
55
56#[cfg(feature = "simd-is-enabled")]
57macro_rules! gather(
58    ($callback: expr) => {
59        {
60            #[inline(always)]
61            #[allow(dead_code)]
62            fn create_arr<T>(mut callback: impl FnMut(usize) -> T) -> [T; SIMD_WIDTH] {
63                [callback(0usize), callback(1usize), callback(2usize), callback(3usize)]
64            }
65
66            create_arr($callback)
67        }
68    }
69);
70
71#[allow(unused_macros)]
72macro_rules! par_iter {
73    ($t: expr) => {{
74        #[cfg(not(feature = "parallel"))]
75        let it = $t.iter();
76
77        #[cfg(feature = "parallel")]
78        let it = $t.par_iter();
79        it
80    }};
81}
82
83macro_rules! par_iter_mut {
84    ($t: expr) => {{
85        #[cfg(not(feature = "parallel"))]
86        let it = $t.iter_mut();
87
88        #[cfg(feature = "parallel")]
89        let it = $t.par_iter_mut();
90        it
91    }};
92}
93
94// macro_rules! par_chunks_mut {
95//     ($t: expr, $sz: expr) => {{
96//         #[cfg(not(feature = "parallel"))]
97//         let it = $t.chunks_mut($sz);
98//
99//         #[cfg(feature = "parallel")]
100//         let it = $t.par_chunks_mut($sz);
101//         it
102//     }};
103// }
104
105#[allow(unused_macros)]
106macro_rules! try_ret {
107    ($val: expr) => {
108        try_ret!($val, ())
109    };
110    ($val: expr, $ret: expr) => {
111        if let Some(val) = $val {
112            val
113        } else {
114            return $ret;
115        }
116    };
117}
118
119// macro_rules! try_continue {
120//     ($val: expr) => {
121//         if let Some(val) = $val {
122//             val
123//         } else {
124//             continue;
125//         }
126//     };
127// }
128
129pub(crate) const INVALID_U32: u32 = u32::MAX;
130pub(crate) const INVALID_USIZE: usize = INVALID_U32 as usize;
131
132/// The string version of Rapier.
133pub const VERSION: &str = env!("CARGO_PKG_VERSION");
134
135pub mod control;
136pub mod counters;
137pub mod data;
138pub mod dynamics;
139pub mod geometry;
140pub mod pipeline;
141pub mod utils;
142
143/// Elementary mathematical entities (vectors, matrices, isometries, etc).
144pub mod math {
145    pub use parry::math::*;
146
147    /*
148     * 2D
149     */
150    /// Max number of pairs of contact points from the same
151    /// contact manifold that can be solved as part of a
152    /// single contact constraint.
153    #[cfg(feature = "dim2")]
154    pub const MAX_MANIFOLD_POINTS: usize = 2;
155
156    /// The type of a constraint Jacobian in twist coordinates.
157    #[cfg(feature = "dim2")]
158    pub type Jacobian<N> = na::Matrix3xX<N>;
159
160    /// The type of a slice of the constraint Jacobian in twist coordinates.
161    #[cfg(feature = "dim2")]
162    pub type JacobianView<'a, N> = na::MatrixView3xX<'a, N>;
163
164    /// The type of a mutable slice of the constraint Jacobian in twist coordinates.
165    #[cfg(feature = "dim2")]
166    pub type JacobianViewMut<'a, N> = na::MatrixViewMut3xX<'a, N>;
167
168    /// The type of impulse applied for friction constraints.
169    #[cfg(feature = "dim2")]
170    pub type TangentImpulse<N> = na::Vector1<N>;
171
172    /// The maximum number of possible rotations and translations of a rigid body.
173    #[cfg(feature = "dim2")]
174    pub const SPATIAL_DIM: usize = 3;
175
176    /// The maximum number of rotational degrees of freedom of a rigid-body.
177    #[cfg(feature = "dim2")]
178    pub const ANG_DIM: usize = 1;
179
180    /*
181     * 3D
182     */
183    /// Max number of pairs of contact points from the same
184    /// contact manifold that can be solved as part of a
185    /// single contact constraint.
186    #[cfg(feature = "dim3")]
187    pub const MAX_MANIFOLD_POINTS: usize = 4;
188
189    /// The type of a constraint Jacobian in twist coordinates.
190    #[cfg(feature = "dim3")]
191    pub type Jacobian<N> = na::Matrix6xX<N>;
192
193    /// The type of a slice of the constraint Jacobian in twist coordinates.
194    #[cfg(feature = "dim3")]
195    pub type JacobianView<'a, N> = na::MatrixView6xX<'a, N>;
196
197    /// The type of a mutable slice of the constraint Jacobian in twist coordinates.
198    #[cfg(feature = "dim3")]
199    pub type JacobianViewMut<'a, N> = na::MatrixViewMut6xX<'a, N>;
200
201    /// The type of impulse applied for friction constraints.
202    #[cfg(feature = "dim3")]
203    pub type TangentImpulse<N> = na::Vector2<N>;
204
205    /// The maximum number of possible rotations and translations of a rigid body.
206    #[cfg(feature = "dim3")]
207    pub const SPATIAL_DIM: usize = 6;
208
209    /// The maximum number of rotational degrees of freedom of a rigid-body.
210    #[cfg(feature = "dim3")]
211    pub const ANG_DIM: usize = 3;
212}
213
214/// Prelude containing the common types defined by Rapier.
215pub mod prelude {
216    pub use crate::dynamics::*;
217    pub use crate::geometry::*;
218    pub use crate::math::*;
219    pub use crate::pipeline::*;
220    pub use na::{DMatrix, DVector, point, vector};
221    pub extern crate nalgebra;
222}