Performance tricks
nalgebra defines a few types that may save you valuable computation times. Those types have strong restrictions in their use and have a quite narrow semantic. However, they can sometimes save you a few square roots, corner-cases checking, or even avoid costly matrix multiplications.
Unit
wrapper#
The Many geometrical algorithms require some of its inputs to have a unit norm.
For example the normal of a triangle or a quaternion that represents a 3D
rotation should have a magnitude equal to 1. That's why the Unit
wrapper type
is here to ensure that the underlying value has a unit norm. For example
Unit<Vector3<f32>>
is a normalized 3D vector, i.e., it lies on the unit
3-dimensional sphere . Also note that the UnitQuaternion<T>
representing a 3D rotation is actually a type alias for Unit<Quaternion<T>>
.
In general, the Unit
wrapper should be used whenever you write an algorithm
that expects a normalized direction as an input. Doing so, you avoid the need
to normalize the input vector yourself and don't have to deal with special
cases where the given direction is zero. Here is a simple example that computes
the length of one vector along a given direction: