Simplify put_object

This commit is contained in:
numzero 2024-09-14 19:24:27 +03:00
parent 0efa513130
commit f06e502e26

View File

@ -148,15 +148,10 @@ fn rel_to_abs(space: &impl Metric, base: &Location, rel: Vec2, steps: usize) ->
} }
/// Converts a position and a rotation to a [Location]. Only the X direction is preserved from `rot` to ensure the resulting Location describes an orthonormal coordinate system. /// Converts a position and a rotation to a [Location]. Only the X direction is preserved from `rot` to ensure the resulting Location describes an orthonormal coordinate system.
///
/// Mathematically, returned `rot` is `M^-1 * orthonormalize(M * rot)` where `M^2` is the metric tensor.
fn put_object(space: &impl Metric, pos: Vec2, rot: Mat2) -> Location { fn put_object(space: &impl Metric, pos: Vec2, rot: Mat2) -> Location {
let x_dir = rot.x_axis;
let metric_sqrt = Mat2::from(space.sqrt_at(pos)); let metric_sqrt = Mat2::from(space.sqrt_at(pos));
let metric_inv_sqrt = Mat2::from(space.sqrt_at(pos).inverse()); let metric_inv_sqrt = Mat2::from(space.sqrt_at(pos).inverse());
let fx = (metric_sqrt * x_dir).normalize(); let rot = metric_inv_sqrt * (metric_sqrt * rot).orthonormalize();
let fy = vec2(-fx.y, fx.x); // TODO: respect orientation of `rot`
let rot = metric_inv_sqrt * mat2(fx, fy);
Location { pos, rot } Location { pos, rot }
} }