Crates.io | cuv |
lib.rs | cuv |
version | 0.1.1 |
source | src |
created_at | 2023-10-20 09:42:27.116699 |
updated_at | 2024-07-25 20:12:38.17851 |
description | Compressed unit vectors that can be used to store things like normal vectors efficiently. |
homepage | |
repository | https://github.com/cry-inc/cuv |
max_upload_size | |
id | 1008842 |
size | 17,379 |
A Rust library for compressed unit vectors. It is a simple Rust port of some C/C++ code.
It can be used to efficiently store and transfer things like normal vectors used in computer graphics. You can use it lower the memory footsprint or reduce the size of files on disk. Intead of three 32 bit floats you can represent the unit vector with a single 16 bit unsigned integer.
The compression itself is lossy, so most input values will be mapped to something slighty different when being unpacked. For many use cases this loss is acceptable. Please make sure this applies to your case as well.
The library provides low level functions to pack and unpack the values and a high level "rustified" interface.
The low level interface requires manual creation of a lookup-table:
let packed: u16 = cuv::pack(1.0, 0.0, 0.0);
let lut = cuv::create_lut();
let unpacked = cuv::unpack(packed, &lut);
assert_eq!(unpacked, [1.0, 0.0, 0.0]);
The high level interface will take care of the lookup-table for you and is a bit more convinient to use:
let input = [1.0, 0.0, 0.0];
let packed = cuv::CompUnitVec::from_slice(&input);
let unpacked = packed.get();
assert_eq!(unpacked, [1.0, 0.0, 0.0]);
The original C/C++ code comes from Rafael Baptista who published the idea in a long lost post. The code was originally described as "unit vector to 16-bit word conversion algorithm".
In 2013 Rafael reposted the description and code again on his blog at https://oroboro.com/compressed-unit-vectors/.
This repo includes some benchmarks implemented with Criterion.rs to be able to detect performance regressions during development.
You can run the benchmarks using the command cargo bench
.