num_quaternion/lib.rs
1//! Quaternions for Rust.
2//!
3//! `num-quaternion` is a Rust library designed for robust, efficient and easy
4//! to use quaternion arithmetic and operations. [`Quaternion`]s and
5//! [`UnitQuaternion`]s are used extensively in computer graphics, robotics,
6//! and physics for representing rotations and orientations.
7//!
8//! # Features
9//!
10//! - **Basic Quaternion Operations**: Addition, subtraction, multiplication,
11//! division, and conjugation.
12//! - **Unit Quaternions**: Special support for unit quaternions with optimized
13//! operations.
14//! - **Conversion Functions**: Convert to/from Euler angles, rotation vectors,
15//! and more.
16//! - **Interpolation**: Spherical linear interpolation (SLERP) for smooth
17//! rotations.
18//! - **Interoperability**: Works with the `serde` and the `rand` crates.
19//! - **Comprehensive Documentation**: Detailed documentation with examples to
20//! help you get started quickly.
21//!
22//! For `#![no_std]` environments, disable the default `std` feature and enable
23//! `libm` to benefit from the advanced mathematical functions of `num-quaternion`:
24//!
25//! ```toml
26//! [dependencies]
27//! num-quaternion = { version = "1.0.6", default-features = false, features = ["libm"] }
28//! ```
29//!
30//! Then, include it in your crate:
31//!
32//! ```rust
33//! use num_quaternion::{Quaternion, UnitQuaternion, Q32, Q64, UQ32, UQ64};
34//! ```
35//!
36//! # Usage
37//!
38//! ## Creating Quaternions
39//!
40//! ```rust
41//! // Create a quaternion with explicit components
42//! # use num_quaternion::Q32;
43//! let q1 = Q32::new(1.0, 2.0, 3.0, 4.0); // = 1 + 2i + 3j + 4k
44//!
45//! // Create a quaternion using shorthand notation
46//! let q2 = 1.0 + Q32::I; // = 1 + i
47//! ```
48//!
49//! ## Basic Operations
50//!
51//! ```rust
52//! # use num_quaternion::Q32;
53//! # let q1 = Q32::ONE;
54//! # let q2 = Q32::ONE;
55//! let q3 = q1 + q2; // Quaternion addition
56//! let q4 = q1 * q2; // Quaternion multiplication
57//! let q_conj = q1.conj(); // Quaternion conjugation
58//! ```
59//!
60//! ## Unit Quaternions
61//!
62//! ```rust
63//! # #[cfg(feature = "std")]
64//! # {
65//! # use num_quaternion::{Q32, UQ32};
66//! # let q1 = Q32::ONE;
67//! let uq1 = q1.normalize().expect("Normalization failed"); // Normalize quaternion
68//! let uq2 = UQ32::I; // Unit quaternion representing the imaginary unit
69//! # }
70//! ```
71//!
72//! ## Conversion Functions
73//!
74//! ```rust
75//! # #[cfg(feature = "std")]
76//! # {
77//! # use num_quaternion::UnitQuaternion;
78//! // From Euler angles
79//! let (roll, pitch, yaw) = (1.5, 1.0, 3.0);
80//! let uq = UnitQuaternion::from_euler_angles(roll, pitch, yaw);
81//!
82//! // To Euler angles
83//! let euler_angles = uq.to_euler_angles();
84//!
85//! // From rotation vector
86//! let rotation_vector = [1.0, 0.0, 0.0]; // x axis rotation, 1 radian
87//! let uq = UnitQuaternion::from_rotation_vector(&rotation_vector);
88//!
89//! // To rotation vector
90//! let rotation_vector = uq.to_rotation_vector();
91//! # }
92//! ```
93//!
94//! ## Spherical Linear Interpolation (SLERP)
95//!
96//! ```rust
97//! # #[cfg(feature = "std")]
98//! # {
99//! # use num_quaternion::UQ32;
100//! let uq1 = UQ32::ONE; // Create a unit quaternion
101//! let uq2 = UQ32::I; // Create another unit quaternion
102//! let interpolated = uq1.slerp(&uq2, 0.3); // Perform SLERP with t=0.3
103//! # }
104//! ```
105//!
106//! # Cargo Features
107//!
108//! The crate offers the following features which can be freely enabled or
109//! disabled:
110//!
111//! - `std`: Enables the use of the Rust standard library. This feature is on
112//! by default. If disabled (`default-features = false` in `Cargo.toml`),
113//! the crate can be used in environments where the standard library is not
114//! available or desired.
115//!
116//! - `libm`: This can be used as a fallback library to provide mathematical
117//! functions which are otherwise provided by the standard library. Use
118//! this feature if you want to work without standard library, but still
119//! want features that internally require floating point functions like
120//! `sqrt` or `acos`, etc. This includes functionality like computing
121//! the norm, converting from and to Euler angles and spherical linear
122//! interpolation.
123//!
124//! - `unstable`: Enables unstable features. Items marked as `unstable` may
125//! undergo breaking changes in future releases without a major version
126//! update. Use with caution in production environments. Currently, the
127//! the `PureQuaternion` type is marked as unstable. They represent
128//! quaternions with zero real part.
129//!
130//! - `serde`: Implements the `Serialize` and `Deserialize` traits for all
131//! data structures where possible. Useful for easy integration with
132//! serialization frameworks, enabling data storage and communication
133//! functionalities.
134//!
135//! - `rand`: Implements the `Distribution` trait for `UnitQuaternion`. This
136//! feature allows you to randomly sample unit quaternions using the `rand`
137//! crate.
138//!
139//!
140//! # Design Rationale and Error Handling
141//!
142//! For detailed design principles and the error handling strategy see the
143//! [Design Rationale](DESIGN_RATIONALE.md).
144//!
145//! # Contributing
146//!
147//! Contributions are welcome! Please fork
148//! [the repository](https://github.com/ralphtandetzky/num-quaternion) and submit
149//! pull requests. By contributing, you agree that your contributions will be
150//! dual-licensed under the Apache-2.0 and MIT licenses.
151//!
152//! If you have any questions or need help, feel free to open an
153//! [issue on GitHub](https://github.com/ralphtandetzky/num-quaternion/issues).
154//!
155//! Further instructions can be found in the [CONTRIBUTING.md](CONTRIBUTING.md)
156//! guidelines.
157//!
158//! # Acknowledgements
159//!
160//! Special thanks to [@cuviper](https://github.com/cuviper) for the
161//! [`num-complex` crate](https://crates.io/crates/num-complex) which served
162//! as a model for this crate. `num-quaternion` is designed to integrate seamlessly
163//! with the [`rust-num` family](https://github.com/rust-num) of crates.
164
165#![deny(missing_docs)]
166#![no_std]
167
168#[cfg(feature = "std")]
169extern crate std;
170
171mod arithmetics;
172mod pure_quaternion;
173mod quaternion;
174mod unit_quaternion;
175
176pub use {
177 quaternion::{Quaternion, Q32, Q64},
178 unit_quaternion::{EulerAngles, ReadMat3x3, UnitQuaternion, UQ32, UQ64},
179};
180
181#[cfg(feature = "unstable")]
182pub use pure_quaternion::{PureQuaternion, PQ32, PQ64};