2013-05-21 23:25:01 +08:00
|
|
|
|
/*!
|
2013-10-06 22:54:09 +08:00
|
|
|
|
# nalgebra
|
2013-05-21 23:25:01 +08:00
|
|
|
|
|
2014-10-13 02:21:06 +08:00
|
|
|
|
**nalgebra** is a low-dimensional linear algebra library written for Rust targeting:
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
2016-08-30 03:16:15 +08:00
|
|
|
|
* General-purpose linear algebra (still lacks a lot of features…)
|
|
|
|
|
* Real time computer graphics.
|
|
|
|
|
* Real time computer physics.
|
2013-10-08 08:10:35 +08:00
|
|
|
|
|
2013-10-06 22:54:09 +08:00
|
|
|
|
## Using **nalgebra**
|
2016-08-30 03:16:15 +08:00
|
|
|
|
You will need the last stable build of the [rust compiler](http://www.rust-lang.org)
|
|
|
|
|
and the official package manager: [cargo](https://github.com/rust-lang/cargo).
|
|
|
|
|
|
|
|
|
|
Simply add the following to your `Cargo.toml` file:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
[dependencies]
|
|
|
|
|
nalgebra = "0.10.*"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2016-03-25 02:04:01 +08:00
|
|
|
|
All the functionality of **nalgebra** is grouped in one place: the root module `nalgebra::`. This
|
|
|
|
|
module re-exports everything and includes free functions for all traits methods performing
|
|
|
|
|
out-of-place operations.
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
|
Thus, you can import the whole prelude using:
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
2014-02-18 19:13:40 +08:00
|
|
|
|
```.ignore
|
2014-09-29 01:20:22 +08:00
|
|
|
|
use nalgebra::*;
|
2013-10-06 22:54:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
2016-08-30 03:16:15 +08:00
|
|
|
|
However, the recommended way to use **nalgebra** is to import types and traits
|
|
|
|
|
explicitly, and call free-functions using the `na::` prefix:
|
2013-10-14 16:22:32 +08:00
|
|
|
|
|
|
|
|
|
```.rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
use na::{Vector3, Rotation3, Rotation};
|
2013-10-14 16:22:32 +08:00
|
|
|
|
|
|
|
|
|
fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
let a = Vector3::new(1.0f64, 1.0, 1.0);
|
|
|
|
|
let mut b = Rotation3::new(na::zero());
|
2013-10-14 16:22:32 +08:00
|
|
|
|
|
2015-02-02 06:23:57 +08:00
|
|
|
|
b.append_rotation_mut(&a);
|
2013-10-14 16:22:32 +08:00
|
|
|
|
|
2014-01-14 16:52:18 +08:00
|
|
|
|
assert!(na::approx_eq(&na::rotation(&b), &a));
|
2013-10-14 16:22:32 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
2016-08-30 03:16:15 +08:00
|
|
|
|
|
2013-10-06 22:54:09 +08:00
|
|
|
|
## Features
|
2014-10-13 14:46:39 +08:00
|
|
|
|
**nalgebra** is meant to be a general-purpose, low-dimensional, linear algebra library, with
|
|
|
|
|
an optimized set of tools for computer graphics and physics. Those features include:
|
2013-10-06 22:54:09 +08:00
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Vectors with predefined static sizes: `Vector1`, `Vector2`, `Vector3`, `Vector4`, `Vector5`, `Vector6`.
|
|
|
|
|
* Vector with a user-defined static size: `VectorN` (available only with the `generic_sizes` feature).
|
|
|
|
|
* Points with static sizes: `Point1`, `Point2`, `Point3`, `Point4`, `Point5`, `Point6`.
|
|
|
|
|
* Square matrices with static sizes: `Matrix1`, `Matrix2`, `Matrix3`, `Matrix4`, `Matrix5`, `Matrix6 `.
|
|
|
|
|
* Rotation matrices: `Rotation2`, `Rotation3`
|
2016-08-30 03:16:15 +08:00
|
|
|
|
* Quaternions: `Quaternion`, `Unit<Quaternion>`.
|
2016-08-21 05:13:53 +08:00
|
|
|
|
* Unit-sized values (unit vectors, unit quaternions, etc.): `Unit<T>`, e.g., `Unit<Vector3<f32>>`.
|
2016-04-18 14:29:22 +08:00
|
|
|
|
* Isometries (translation ⨯ rotation): `Isometry2`, `Isometry3`
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Similarity transformations (translation ⨯ rotation ⨯ uniform scale): `Similarity2`, `Similarity3`.
|
2016-08-30 03:16:15 +08:00
|
|
|
|
* 3D projections for computer graphics: `Persp3`, `PerspMatrix3`, `Ortho3`, `OrthoMatrix3`.
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Dynamically sized heap-allocated vector: `DVector`.
|
|
|
|
|
* Dynamically sized stack-allocated vectors with a maximum size: `DVector1` to `DVector6`.
|
|
|
|
|
* Dynamically sized heap-allocated (square or rectangular) matrix: `DMatrix`.
|
|
|
|
|
* Linear algebra and data analysis operators: `Covariance`, `Mean`, `qr`, `cholesky`.
|
2013-10-07 01:32:31 +08:00
|
|
|
|
* Almost one trait per functionality: useful for generic programming.
|
2013-05-21 23:25:01 +08:00
|
|
|
|
*/
|
2013-12-24 18:46:16 +08:00
|
|
|
|
|
2014-04-02 04:58:06 +08:00
|
|
|
|
#![deny(non_camel_case_types)]
|
2014-11-01 00:40:47 +08:00
|
|
|
|
#![deny(unused_parens)]
|
|
|
|
|
#![deny(non_upper_case_globals)]
|
|
|
|
|
#![deny(unused_qualifications)]
|
|
|
|
|
#![deny(unused_results)]
|
|
|
|
|
#![warn(missing_docs)]
|
2014-06-10 03:48:24 +08:00
|
|
|
|
#![doc(html_root_url = "http://nalgebra.org/doc")]
|
2013-05-15 05:08:29 +08:00
|
|
|
|
|
2015-03-26 05:36:19 +08:00
|
|
|
|
extern crate rustc_serialize;
|
2015-02-17 20:32:54 +08:00
|
|
|
|
extern crate rand;
|
2015-04-04 23:53:25 +08:00
|
|
|
|
extern crate num;
|
2016-03-25 02:37:56 +08:00
|
|
|
|
|
|
|
|
|
#[cfg(feature="generic_sizes")]
|
2016-03-25 02:03:29 +08:00
|
|
|
|
extern crate generic_array;
|
2013-05-15 05:08:29 +08:00
|
|
|
|
|
2015-01-10 08:36:13 +08:00
|
|
|
|
#[cfg(feature="arbitrary")]
|
|
|
|
|
extern crate quickcheck;
|
|
|
|
|
|
2016-08-21 05:13:53 +08:00
|
|
|
|
#[cfg(feature="abstract_algebra")]
|
|
|
|
|
extern crate algebra;
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
use std::cmp;
|
2015-08-21 03:41:40 +08:00
|
|
|
|
use std::ops::{Neg, Mul};
|
2015-04-18 20:19:43 +08:00
|
|
|
|
use num::{Zero, One};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub use traits::{
|
|
|
|
|
Absolute,
|
|
|
|
|
AbsoluteRotate,
|
|
|
|
|
ApproxEq,
|
2014-10-12 15:17:17 +08:00
|
|
|
|
Axpy,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Basis,
|
2014-11-15 22:47:59 +08:00
|
|
|
|
BaseFloat,
|
2014-11-16 21:04:15 +08:00
|
|
|
|
BaseNum,
|
|
|
|
|
Bounded,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Cast,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
Column,
|
|
|
|
|
ColumnSlice, RowSlice,
|
|
|
|
|
Covariance,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Cross,
|
|
|
|
|
CrossMatrix,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
Determinant,
|
|
|
|
|
Diagonal,
|
|
|
|
|
Dimension,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Dot,
|
2014-10-27 00:24:33 +08:00
|
|
|
|
EigenQR,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Eye,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
FloatPoint,
|
|
|
|
|
FloatVector,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
FromHomogeneous,
|
|
|
|
|
Indexable,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
Inverse,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Iterable,
|
|
|
|
|
IterableMut,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
Matrix,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Mean,
|
|
|
|
|
Norm,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
NumPoint,
|
|
|
|
|
NumVector,
|
|
|
|
|
Origin,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Outer,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
PartialOrder,
|
|
|
|
|
PartialOrdering,
|
|
|
|
|
PointAsVector,
|
2015-05-25 20:47:14 +08:00
|
|
|
|
Repeat,
|
2015-06-01 04:52:18 +08:00
|
|
|
|
Rotate, Rotation, RotationMatrix, RotationWithTranslation, RotationTo,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Row,
|
2014-10-26 17:46:51 +08:00
|
|
|
|
Shape,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
SquareMatrix,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
ToHomogeneous,
|
|
|
|
|
Transform, Transformation,
|
|
|
|
|
Translate, Translation,
|
|
|
|
|
Transpose,
|
2015-04-18 20:58:38 +08:00
|
|
|
|
UniformSphereSample
|
2014-09-29 01:20:22 +08:00
|
|
|
|
};
|
|
|
|
|
|
2016-03-25 02:37:56 +08:00
|
|
|
|
#[cfg(feature="generic_sizes")]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub use structs::VectorN;
|
2016-03-25 02:37:56 +08:00
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub use structs::{
|
|
|
|
|
Identity,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
DMatrix, DMatrix1, DMatrix2, DMatrix3, DMatrix4, DMatrix5, DMatrix6,
|
|
|
|
|
DVector, DVector1, DVector2, DVector3, DVector4, DVector5, DVector6,
|
|
|
|
|
Isometry2, Isometry3,
|
|
|
|
|
Similarity2, Similarity3,
|
|
|
|
|
Matrix1, Matrix2, Matrix3, Matrix4,
|
|
|
|
|
Matrix5, Matrix6,
|
|
|
|
|
Rotation2, Rotation3,
|
|
|
|
|
Vector1, Vector2, Vector3, Vector4, Vector5, Vector6,
|
|
|
|
|
Point1, Point2, Point3, Point4, Point5, Point6,
|
|
|
|
|
Perspective3, PerspectiveMatrix3,
|
|
|
|
|
Orthographic3, OrthographicMatrix3,
|
2016-08-13 03:46:40 +08:00
|
|
|
|
Quaternion, UnitQuaternion,
|
|
|
|
|
Unit
|
2014-09-29 01:20:22 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub use linalg::{
|
|
|
|
|
qr,
|
2015-08-07 20:44:25 +08:00
|
|
|
|
householder_matrix,
|
2015-09-22 06:19:45 +08:00
|
|
|
|
cholesky,
|
|
|
|
|
hessenberg
|
2014-09-29 01:20:22 +08:00
|
|
|
|
};
|
|
|
|
|
|
2014-01-14 16:40:12 +08:00
|
|
|
|
mod structs;
|
|
|
|
|
mod traits;
|
2014-05-10 04:14:37 +08:00
|
|
|
|
mod linalg;
|
2014-11-23 21:01:12 +08:00
|
|
|
|
mod macros;
|
2013-05-15 05:08:29 +08:00
|
|
|
|
|
2013-09-22 16:58:21 +08:00
|
|
|
|
// mod lower_triangular;
|
|
|
|
|
// mod chol;
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// Change the input value to ensure it is on the range `[min, max]`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-10-22 01:43:25 +08:00
|
|
|
|
pub fn clamp<T: PartialOrd>(val: T, min: T, max: T) -> T {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
if val > min {
|
|
|
|
|
if val < max {
|
|
|
|
|
val
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
max
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
min
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Same as `cmp::max`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn max<T: Ord>(a: T, b: T) -> T {
|
|
|
|
|
cmp::max(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Same as `cmp::min`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn min<T: Ord>(a: T, b: T) -> T {
|
|
|
|
|
cmp::min(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the infimum of `a` and `b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inf<T: PartialOrder>(a: &T, b: &T) -> T {
|
|
|
|
|
PartialOrder::inf(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the supremum of `a` and `b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn sup<T: PartialOrder>(a: &T, b: &T) -> T {
|
|
|
|
|
PartialOrder::sup(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Compare `a` and `b` using a partial ordering relation.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_cmp<T: PartialOrder>(a: &T, b: &T) -> PartialOrdering {
|
|
|
|
|
PartialOrder::partial_cmp(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` iff `a` and `b` are comparable and `a < b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_lt<T: PartialOrder>(a: &T, b: &T) -> bool {
|
|
|
|
|
PartialOrder::partial_lt(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` iff `a` and `b` are comparable and `a <= b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_le<T: PartialOrder>(a: &T, b: &T) -> bool {
|
|
|
|
|
PartialOrder::partial_le(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` iff `a` and `b` are comparable and `a > b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_gt<T: PartialOrder>(a: &T, b: &T) -> bool {
|
|
|
|
|
PartialOrder::partial_gt(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns `true` iff `a` and `b` are comparable and `a >= b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_ge<T: PartialOrder>(a: &T, b: &T) -> bool {
|
|
|
|
|
PartialOrder::partial_ge(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return the minimum of `a` and `b` if they are comparable.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_min<'a, T: PartialOrder>(a: &'a T, b: &'a T) -> Option<&'a T> {
|
|
|
|
|
PartialOrder::partial_min(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return the maximum of `a` and `b` if they are comparable.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_max<'a, T: PartialOrder>(a: &'a T, b: &'a T) -> Option<&'a T> {
|
|
|
|
|
PartialOrder::partial_max(a, b)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Clamp `value` between `min` and `max`. Returns `None` if `value` is not comparable to
|
|
|
|
|
/// `min` or `max`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn partial_clamp<'a, T: PartialOrder>(value: &'a T, min: &'a T, max: &'a T) -> Option<&'a T> {
|
|
|
|
|
PartialOrder::partial_clamp(value, min, max)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// Constructors
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/// Create a special identity object.
|
|
|
|
|
///
|
|
|
|
|
/// Same as `Identity::new()`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn identity() -> Identity {
|
|
|
|
|
Identity::new()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a zero-valued value.
|
|
|
|
|
///
|
|
|
|
|
/// This is the same as `std::num::zero()`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn zero<T: Zero>() -> T {
|
|
|
|
|
Zero::zero()
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-16 21:04:15 +08:00
|
|
|
|
/// Tests is a value is iqual to zero.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-11-16 21:04:15 +08:00
|
|
|
|
pub fn is_zero<T: Zero>(val: &T) -> bool {
|
|
|
|
|
val.is_zero()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// Create a one-valued value.
|
|
|
|
|
///
|
|
|
|
|
/// This is the same as `std::num::one()`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn one<T: One>() -> T {
|
|
|
|
|
One::one()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// Geometry
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
|
/// Returns the trivial origin of an affine space.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn origin<P: Origin>() -> P {
|
|
|
|
|
Origin::origin()
|
2014-10-10 17:23:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Returns the center of two points.
|
2014-10-11 03:24:18 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn center<N: BaseFloat, P: FloatPoint<N>>(a: &P, b: &P) -> P
|
2016-08-13 03:46:40 +08:00
|
|
|
|
where <P as PointAsVector>::Vector: Norm<NormType = N> {
|
2016-05-06 21:08:06 +08:00
|
|
|
|
(*a + b.to_vector()) / ::cast(2.0)
|
2014-10-11 03:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-10 17:23:52 +08:00
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* FloatPoint
|
2014-10-10 17:23:52 +08:00
|
|
|
|
*/
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Returns the distance between two points.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-08-13 03:46:40 +08:00
|
|
|
|
pub fn distance<N: BaseFloat, P: FloatPoint<N>>(a: &P, b: &P) -> N where <P as PointAsVector>::Vector: Norm<NormType = N> {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
a.distance(b)
|
2014-10-10 17:23:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Returns the squared distance between two points.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn distance_squared<N: BaseFloat, P: FloatPoint<N>>(a: &P, b: &P) -> N
|
2016-08-13 03:46:40 +08:00
|
|
|
|
where <P as PointAsVector>::Vector: Norm<NormType = N> {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
a.distance_squared(b)
|
2014-10-10 17:23:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/*
|
|
|
|
|
* Translation<V>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Gets the translation applicable by `m`.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Vector3, Isometry3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Isometry3::new(Vector3::new(1.0f64, 1.0, 1.0), na::zero());
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// let trans = na::translation(&t);
|
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(trans == Vector3::new(1.0, 1.0, 1.0));
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn translation<V, M: Translation<V>>(m: &M) -> V {
|
|
|
|
|
m.translation()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the inverse translation applicable by `m`.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Vector3, Isometry3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Isometry3::new(Vector3::new(1.0f64, 1.0, 1.0), na::zero());
|
|
|
|
|
/// let itrans = na::inverse_translation(&t);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(itrans == Vector3::new(-1.0, -1.0, -1.0));
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse_translation<V, M: Translation<V>>(m: &M) -> V {
|
|
|
|
|
m.inverse_translation()
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Applies the translation `v` to a copy of `m`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn append_translation<V, M: Translation<V>>(m: &M, v: &V) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
Translation::append_translation(m, v)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2014-10-10 17:23:52 +08:00
|
|
|
|
* Translate<P>
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Applies a translation to a point.
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Point3, Vector3, Isometry3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Isometry3::new(Vector3::new(1.0f64, 1.0, 1.0), na::zero());
|
|
|
|
|
/// let p = Point3::new(2.0, 2.0, 2.0);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2014-10-10 17:23:52 +08:00
|
|
|
|
/// let tp = na::translate(&t, &p);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(tp == Point3::new(3.0, 3.0, 3.0))
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-10-10 17:23:52 +08:00
|
|
|
|
pub fn translate<P, M: Translate<P>>(m: &M, p: &P) -> P {
|
|
|
|
|
m.translate(p)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Applies an inverse translation to a point.
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Point3, Vector3, Isometry3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Isometry3::new(Vector3::new(1.0f64, 1.0, 1.0), na::zero());
|
|
|
|
|
/// let p = Point3::new(2.0, 2.0, 2.0);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let tp = na::inverse_translate(&t, &p);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&tp, &Point3::new(1.0, 1.0, 1.0)))
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse_translate<P, M: Translate<P>>(m: &M, p: &P) -> P {
|
|
|
|
|
m.inverse_translate(p)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Rotation<V>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Gets the rotation applicable by `m`.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Vector3, Rotation3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Rotation3::new(Vector3::new(1.0f64, 1.0, 1.0));
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&na::rotation(&t), &Vector3::new(1.0, 1.0, 1.0)));
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn rotation<V, M: Rotation<V>>(m: &M) -> V {
|
|
|
|
|
m.rotation()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Gets the inverse rotation applicable by `m`.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Vector3, Rotation3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Rotation3::new(Vector3::new(1.0f64, 1.0, 1.0));
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&na::inverse_rotation(&t), &Vector3::new(-1.0, -1.0, -1.0)));
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse_rotation<V, M: Rotation<V>>(m: &M) -> V {
|
|
|
|
|
m.inverse_rotation()
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: this example is a bit shity
|
|
|
|
|
/// Applies the rotation `v` to a copy of `m`.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Vector3, Rotation3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Rotation3::new(Vector3::new(0.0f64, 0.0, 0.0));
|
|
|
|
|
/// let v = Vector3::new(1.0, 1.0, 1.0);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// let rt = na::append_rotation(&t, &v);
|
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&na::rotation(&rt), &Vector3::new(1.0, 1.0, 1.0)))
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn append_rotation<V, M: Rotation<V>>(m: &M, v: &V) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
Rotation::append_rotation(m, v)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: this example is a bit shity
|
|
|
|
|
/// Pre-applies the rotation `v` to a copy of `m`.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{Vector3, Rotation3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Rotation3::new(Vector3::new(0.0f64, 0.0, 0.0));
|
|
|
|
|
/// let v = Vector3::new(1.0, 1.0, 1.0);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// let rt = na::prepend_rotation(&t, &v);
|
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&na::rotation(&rt), &Vector3::new(1.0, 1.0, 1.0)))
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn prepend_rotation<V, M: Rotation<V>>(m: &M, v: &V) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
Rotation::prepend_rotation(m, v)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Rotate<V>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Applies a rotation to a vector.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{BaseFloat, Rotation3, Vector3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Rotation3::new(Vector3::new(0.0f64, 0.0, 0.5 * <f64 as BaseFloat>::pi()));
|
|
|
|
|
/// let v = Vector3::new(1.0, 0.0, 0.0);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// let tv = na::rotate(&t, &v);
|
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&tv, &Vector3::new(0.0, 1.0, 0.0)))
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn rotate<V, M: Rotate<V>>(m: &M, v: &V) -> V {
|
|
|
|
|
m.rotate(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Applies an inverse rotation to a vector.
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2015-03-29 19:00:09 +08:00
|
|
|
|
/// extern crate nalgebra as na;
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// use na::{BaseFloat, Rotation3, Vector3};
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
|
|
|
|
/// fn main() {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let t = Rotation3::new(Vector3::new(0.0f64, 0.0, 0.5 * <f64 as BaseFloat>::pi()));
|
|
|
|
|
/// let v = Vector3::new(1.0, 0.0, 0.0);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// let tv = na::inverse_rotate(&t, &v);
|
2014-09-29 01:20:22 +08:00
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// assert!(na::approx_eq(&tv, &Vector3::new(0.0, -1.0, 0.0)))
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse_rotate<V, M: Rotate<V>>(m: &M, v: &V) -> V {
|
|
|
|
|
m.inverse_rotate(v)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* RotationWithTranslation<LV, AV>
|
|
|
|
|
*/
|
|
|
|
|
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Rotates a copy of `m` by `amount` using `center` as the pivot point.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-01-10 06:01:46 +08:00
|
|
|
|
pub fn append_rotation_wrt_point<LV: Neg<Output = LV> + Copy,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
AV,
|
|
|
|
|
M: RotationWithTranslation<LV, AV>>(
|
|
|
|
|
m: &M,
|
|
|
|
|
amount: &AV,
|
|
|
|
|
center: &LV) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
RotationWithTranslation::append_rotation_wrt_point(m, amount, center)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// Rotates a copy of `m` by `amount` using `m.translation()` as the pivot point.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-01-05 02:03:28 +08:00
|
|
|
|
pub fn append_rotation_wrt_center<LV: Neg<Output = LV> + Copy,
|
2014-09-29 01:20:22 +08:00
|
|
|
|
AV,
|
|
|
|
|
M: RotationWithTranslation<LV, AV>>(
|
|
|
|
|
m: &M,
|
|
|
|
|
amount: &AV) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
RotationWithTranslation::append_rotation_wrt_center(m, amount)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 04:52:18 +08:00
|
|
|
|
/*
|
|
|
|
|
* RotationTo
|
|
|
|
|
*/
|
|
|
|
|
/// Computes the angle of the rotation needed to transfom `a` to `b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-06-01 04:52:18 +08:00
|
|
|
|
pub fn angle_between<V: RotationTo>(a: &V, b: &V) -> V::AngleType {
|
|
|
|
|
a.angle_to(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the rotation needed to transform `a` to `b`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-06-01 04:52:18 +08:00
|
|
|
|
pub fn rotation_between<V: RotationTo>(a: &V, b: &V) -> V::DeltaRotationType {
|
|
|
|
|
a.rotation_to(b)
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/*
|
|
|
|
|
* RotationMatrix<LV, AV, R>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Builds a rotation matrix from `r`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn to_rotation_matrix<N, LV, AV, R, M>(r: &R) -> M
|
2015-05-25 20:47:14 +08:00
|
|
|
|
where R: RotationMatrix<N, LV, AV, Output = M>,
|
2016-04-17 23:26:58 +08:00
|
|
|
|
M: SquareMatrix<N, LV> + Rotation<AV> + Copy,
|
2015-08-21 03:41:40 +08:00
|
|
|
|
LV: Mul<M, Output = LV>
|
2015-02-04 02:17:15 +08:00
|
|
|
|
{
|
|
|
|
|
// FIXME: rust-lang/rust#20413
|
2016-04-17 23:26:58 +08:00
|
|
|
|
r.to_rotation_matrix()
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* AbsoluteRotate<V>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Applies a rotation using the absolute values of its components.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn absolute_rotate<V, M: AbsoluteRotate<V>>(m: &M, v: &V) -> V {
|
|
|
|
|
m.absolute_rotate(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Transformation<T>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Gets the transformation applicable by `m`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn transformation<T, M: Transformation<T>>(m: &M) -> T {
|
|
|
|
|
m.transformation()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the inverse transformation applicable by `m`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse_transformation<T, M: Transformation<T>>(m: &M) -> T {
|
|
|
|
|
m.inverse_transformation()
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets a transformed copy of `m`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn append_transformation<T, M: Transformation<T>>(m: &M, t: &T) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
Transformation::append_transformation(m, t)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Transform<V>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Applies a transformation to a vector.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn transform<V, M: Transform<V>>(m: &M, v: &V) -> V {
|
|
|
|
|
m.transform(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Applies an inverse transformation to a vector.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse_transform<V, M: Transform<V>>(m: &M, v: &V) -> V {
|
|
|
|
|
m.inverse_transform(v)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Dot<N>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the dot product of two vectors.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn dot<V: Dot<N>, N>(a: &V, b: &V) -> N {
|
|
|
|
|
Dot::dot(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Norm<N>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the L2 norm of a vector.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-08-13 03:46:40 +08:00
|
|
|
|
pub fn norm<V: Norm>(v: &V) -> V::NormType {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Norm::norm(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the squared L2 norm of a vector.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-08-13 03:46:40 +08:00
|
|
|
|
pub fn norm_squared<V: Norm>(v: &V) -> V::NormType {
|
2016-04-17 23:26:58 +08:00
|
|
|
|
Norm::norm_squared(v)
|
2013-05-19 01:04:03 +08:00
|
|
|
|
}
|
2014-09-29 01:20:22 +08:00
|
|
|
|
|
|
|
|
|
/// Gets the normalized version of a vector.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-08-13 03:46:40 +08:00
|
|
|
|
pub fn normalize<V: Norm>(v: &V) -> V {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
Norm::normalize(v)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-13 03:46:40 +08:00
|
|
|
|
/// Gets the normalized version of a vector or `None` if its norm is smaller than `min_norm`.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn try_normalize<V: Norm>(v: &V, min_norm: V::NormType) -> Option<V> {
|
|
|
|
|
Norm::try_normalize(v, min_norm)
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Determinant<N>
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
/// Computes the determinant of a square matrix.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn determinant<M: Determinant<N>, N>(m: &M) -> N {
|
|
|
|
|
Determinant::determinant(m)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Cross<V>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the cross product of two vectors.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-05-25 20:47:14 +08:00
|
|
|
|
pub fn cross<LV: Cross>(a: &LV, b: &LV) -> LV::CrossProductType {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Cross::cross(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* CrossMatrix<M>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Given a vector, computes the matrix which, when multiplied by another vector, computes a cross
|
|
|
|
|
/// product.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn cross_matrix<V: CrossMatrix<M>, M>(v: &V) -> M {
|
|
|
|
|
CrossMatrix::cross_matrix(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* ToHomogeneous<U>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Converts a matrix or vector to homogeneous coordinates.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn to_homogeneous<M: ToHomogeneous<Res>, Res>(m: &M) -> Res {
|
|
|
|
|
ToHomogeneous::to_homogeneous(m)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* FromHomogeneous<U>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Converts a matrix or vector from homogeneous coordinates.
|
|
|
|
|
///
|
|
|
|
|
/// w-normalization is appied.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn from_homogeneous<M, Res: FromHomogeneous<M>>(m: &M) -> Res {
|
|
|
|
|
FromHomogeneous::from(m)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* UniformSphereSample
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Samples the unit sphere living on the dimension as the samples types.
|
|
|
|
|
///
|
2015-01-10 06:01:46 +08:00
|
|
|
|
/// The number of sampling point is implementation-specific. It is always uniform.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-01-08 04:16:56 +08:00
|
|
|
|
pub fn sample_sphere<V: UniformSphereSample, F: FnMut(V)>(f: F) {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
UniformSphereSample::sample(f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// Operations
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* AproxEq<N>
|
|
|
|
|
*/
|
|
|
|
|
/// Tests approximate equality.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn approx_eq<T: ApproxEq<N>, N>(a: &T, b: &T) -> bool {
|
|
|
|
|
ApproxEq::approx_eq(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tests approximate equality using a custom epsilon.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn approx_eq_eps<T: ApproxEq<N>, N>(a: &T, b: &T, eps: &N) -> bool {
|
|
|
|
|
ApproxEq::approx_eq_eps(a, b, eps)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Absolute<A>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes a component-wise absolute value.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn abs<M: Absolute<Res>, Res>(m: &M) -> Res {
|
|
|
|
|
Absolute::abs(m)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Inverse
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Gets an inverted copy of a matrix.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn inverse<M: Inverse>(m: &M) -> Option<M> {
|
|
|
|
|
Inverse::inverse(m)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Transpose
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Gets a transposed copy of a matrix.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn transpose<M: Transpose>(m: &M) -> M {
|
2015-02-02 06:23:57 +08:00
|
|
|
|
Transpose::transpose(m)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Outer<M>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the outer product of two vectors.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-05-25 20:47:14 +08:00
|
|
|
|
pub fn outer<V: Outer>(a: &V, b: &V) -> V::OuterProductType {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Outer::outer(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Covariance<M>
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the covariance of a set of observations.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn covariance<M: Covariance<Res>, Res>(observations: &M) -> Res {
|
|
|
|
|
Covariance::covariance(observations)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Mean<N>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the mean of a set of observations.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn mean<N, M: Mean<N>>(observations: &M) -> N {
|
|
|
|
|
Mean::mean(observations)
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 00:24:33 +08:00
|
|
|
|
/*
|
|
|
|
|
* EigenQR<N, V>
|
|
|
|
|
*/
|
|
|
|
|
/// Computes the eigenvalues and eigenvectors of a square matrix usin the QR algorithm.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-08-21 03:41:40 +08:00
|
|
|
|
pub fn eigen_qr<N, V, M>(m: &M, eps: &N, niter: usize) -> (M, V)
|
|
|
|
|
where V: Mul<M, Output = V>,
|
|
|
|
|
M: EigenQR<N, V> {
|
2014-10-27 00:24:33 +08:00
|
|
|
|
EigenQR::eigen_qr(m, eps, niter)
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// Structure
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Eye
|
|
|
|
|
*/
|
|
|
|
|
/// Construct the identity matrix for a given dimension
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn new_identity<M: Eye>(dimension: usize) -> M {
|
|
|
|
|
Eye::new_identity(dimension)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-25 20:47:14 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Repeat
|
|
|
|
|
*/
|
|
|
|
|
/// Create an object by repeating a value.
|
|
|
|
|
///
|
|
|
|
|
/// Same as `Identity::new()`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-05-25 20:47:14 +08:00
|
|
|
|
pub fn repeat<N, T: Repeat<N>>(val: N) -> T {
|
|
|
|
|
Repeat::repeat(val)
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/*
|
|
|
|
|
* Basis
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// Computes the canonical basis for a given dimension.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-01-08 04:16:56 +08:00
|
|
|
|
pub fn canonical_basis<V: Basis, F: FnMut(V) -> bool>(f: F) {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Basis::canonical_basis(f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the basis of the orthonormal subspace of a given vector.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-01-08 04:16:56 +08:00
|
|
|
|
pub fn orthonormal_subspace_basis<V: Basis, F: FnMut(V) -> bool>(v: &V, f: F) {
|
2014-09-29 01:20:22 +08:00
|
|
|
|
Basis::orthonormal_subspace_basis(v, f)
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 06:16:44 +08:00
|
|
|
|
/// Gets the (0-based) i-th element of the canonical basis of V.
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
|
pub fn canonical_basis_element<V: Basis>(i: usize) -> Option<V> {
|
2014-10-27 06:16:44 +08:00
|
|
|
|
Basis::canonical_basis_element(i)
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/*
|
|
|
|
|
* Row<R>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Column<C>
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Diagonal<V>
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
/// Gets the diagonal of a square matrix.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn diagonal<M: Diagonal<V>, V>(m: &M) -> V {
|
|
|
|
|
m.diagonal()
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2016-04-17 23:26:58 +08:00
|
|
|
|
* Dimension
|
2014-09-29 01:20:22 +08:00
|
|
|
|
*/
|
|
|
|
|
/// Gets the dimension an object lives in.
|
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// Same as `Dimension::dimension::(None::<V>)`.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2016-04-17 23:26:58 +08:00
|
|
|
|
pub fn dimension<V: Dimension>() -> usize {
|
|
|
|
|
Dimension::dimension(None::<V>)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-26 17:46:51 +08:00
|
|
|
|
/// Gets the indexable range of an object.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2015-01-05 02:43:04 +08:00
|
|
|
|
pub fn shape<V: Shape<I>, I>(v: &V) -> I {
|
2014-10-26 17:46:51 +08:00
|
|
|
|
v.shape()
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/*
|
|
|
|
|
* Cast<T>
|
|
|
|
|
*/
|
|
|
|
|
/// Converts an object from one type to another.
|
|
|
|
|
///
|
|
|
|
|
/// For primitive types, this is the same as the `as` keywords.
|
|
|
|
|
/// The following properties are preserved by a cast:
|
|
|
|
|
///
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// * Type-level geometric invariants cannot be broken (eg. a cast from Rotation3<f64> to Rotation3<i64> is
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// not possible)
|
2016-04-17 23:26:58 +08:00
|
|
|
|
/// * A cast to a type with more type-level invariants cannot be done (eg. a cast from Matrix<f64> to
|
|
|
|
|
/// Rotation3<f64> is not possible)
|
2014-09-29 01:20:22 +08:00
|
|
|
|
/// * For primitive types an unbounded cast is done using the `as` keyword (this is different from
|
|
|
|
|
/// the standard library which makes bound-checking to ensure eg. that a i64 is not out of the
|
|
|
|
|
/// range of an i32 when a cast from i64 to i32 is done).
|
|
|
|
|
/// * A cast does not affect the dimension of an algebraic object. Note that this prevents an
|
|
|
|
|
/// isometric transform to be cast to a raw matrix. Use `to_homogeneous` for that special purpose.
|
2016-05-06 21:08:06 +08:00
|
|
|
|
#[inline]
|
2014-09-29 01:20:22 +08:00
|
|
|
|
pub fn cast<T, U: Cast<T>>(t: T) -> U {
|
|
|
|
|
Cast::from(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Indexable
|
|
|
|
|
*/
|