servo / euclid

Geometry primitives (basic linear algebra) for Rust
Other
458 stars 102 forks source link

Unable to normalize Rotation3D #398

Closed ghost closed 4 years ago

ghost commented 4 years ago

I am having trouble normalizing a Rotation3D. here is my code:

let rotation = Rotation3D::around_axis(rotation_axis, angle).normalize();
let prev_point_on_shape = rotation.transform_vector3d(current_point_on_shape);

when I run this, it panics complaining that the assertion is_normalized() fails for rotation. I tried calling is_normalized() on rotation myself and it returned false. I have no idea what is going wrong please help.

nical commented 4 years ago

It probably means that the epsilon threshold used in is_normalized is a bit too strict for the actual precision of normalize.

Are you using it with f32 values? I guess the right thing to do would be to brute-force random parameters of around_axis for a while and pick the smallest epsilon that doesn't break.

nical commented 4 years ago

So I wrote quick and dirty program that tested epsilon values continuously, let it run for a while and for f32 rotations the best is_normalized threshold it could find was 0.00000040000000001150227 which is quite a bit smaller than the currently used 1.0e-5, so the current threshold should be pretty safe.

Could you print the value of rotations that cause this issue? It could be containing NaNs.

ghost commented 4 years ago

I will try that tomorrow. If the threshold isn't the problem, it is likely that my rotation contains NaNs.

ghost commented 4 years ago

Actually, I just took a quick look and it turns out I'm really stupid. I named a method the wrong thing to work properly with another crate and this all culminated in 2 vectors that I was taking the cross product of being the same. Closing.