jalberse / shimmer

Physically based rendering in Rust
Apache License 2.0
33 stars 0 forks source link

Add NormalizedVector etc Types #4

Open jalberse opened 1 year ago

jalberse commented 1 year ago

There are certain operations, such as finding the angle between two vectors, which require that the vectors be normalized prior to the call. It would be good to capture this in our type system (you could normalize within the angle_between() call, but this is likely not optimal if the vectors ARE normalized already).

The idea is that Objects of type Vector (as opposed to a NormalizedVector) may be normalized, but are not guaranteed to be normalized. NormalizedVectors would maintain that they are normalized as an invariant, and would be returned by Vector::normalize().

Maybe a NormalizedVector has the full range of operations that a regular vector does, but if you e.g. scale it, a regular Vector is returned since it no longer has length of 1.

Or maybe there's only a smaller subset of operations that we need, such as angle_between().

jalberse commented 1 year ago

This is broadly the typestate pattern: http://cliffle.com/blog/rust-typestate/

I'm not totally sold on if this should be implemented or not.

jalberse commented 1 year ago

We could also use an octahedral encoding for this; see age 103 of PBRTv4