cuv/
lib.rs

1//! A Rust library for compressed unit vectors.
2//!
3//! It can be used to efficiently store and transfer things like normal vectors used in computer graphics.
4//! You can use it lower the memory footsprint or reduce the size of files on disk.
5//! Intead of three 32 bit floats you can represent the unit vector with a single 16 bit unsigned integer.
6//!
7//! The compression itself is **lossy**, so most input values will be mapped to something slighty different when being unpacked.
8//! For many use cases this loss is acceptable. Please make sure this applies to your case as well.
9//!
10//! ### Getting Started
11//!
12//! The following example shows how to create and read a compressed unit vector:
13//!
14//! ```
15//! use cuv::CompUnitVec;
16//!
17//! let input = [1.0, 0.0, 0.0];
18//! let packed = CompUnitVec::from_slice(&input);
19//! let unpacked = packed.get();
20//! assert_eq!(unpacked, input);
21//! ```
22//! There is also a low level interface which requires the manual creation of a lookup-table for unpacking:
23//!
24//! ```
25//! let packed: u16 = cuv::pack(1.0, 0.0, 0.0);
26//! let lut = cuv::create_lut();
27//! let unpacked = cuv::unpack(packed, &lut);
28//! assert_eq!(unpacked, [1.0, 0.0, 0.0]);
29//! ```
30//!
31//! ### Invalid Input
32//!
33//! If the input is not a unit vector, it will still work because the input is internally normalized.
34//! The unpacked output vectors will be always normalized and true unit vectors.
35//!
36//! The algorithm can handle invalid inputs and will map them to a valid compressed unit vector.
37//! For example, using `[0, 0, 0]` as input will unpack as `[0, 0, 1]`.
38//! The same applies for `NaN` or infinity values.
39
40#![forbid(unsafe_code)]
41#![deny(
42    clippy::unwrap_used,
43    clippy::expect_used,
44    clippy::panic,
45    clippy::large_stack_arrays,
46    clippy::large_types_passed_by_value
47)]
48
49mod comp_unit_vec;
50mod core;
51
52// Low level interface
53pub use core::create_lut;
54pub use core::pack;
55pub use core::unpack;
56
57// High level rustified interface
58pub use comp_unit_vec::CompUnitVec;