You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.0 KiB

use nalgebra::{Vector4};
pub type HypVec3 = Vector4<f64>;
/// Lorentz inner product of two vectors
pub fn lorentz(a: HypVec3, b: HypVec3) -> f64 {
a[1] * b[1] + a[2] * b[2] + a[3] * b[3] - a[0] * b[0]
}
pub fn lorentz_norm(a: HypVec3) -> f64 {
lorentz(a, a)
}
pub fn normalize_timelike(a: HypVec3) -> Option<HypVec3> {
let n = -lorentz_norm(a);
if !(n > 0.0) {
None
} else {
Some(a * n.sqrt().recip())
}
}
pub fn normalize_spacelike(a: HypVec3) -> Option<HypVec3> {
let n = lorentz_norm(a);
if !(n > 0.0) {
None
} else {
Some(a * n.sqrt().recip())
}
}
/// Represents a line (or a ray) in hyperbolic space.
pub struct Line {
/// A point that the line passes through.
timelike: HypVec3,
/// A spacelike unit representing the direction of the line or ray.
direction: HypVec3,
}
/// Represents a surface with constant curvature. In the hyperboloid representation of the space, the surface represents the points whose Lorentz inner product with `direction` is equal to `offset`.
pub struct Surface {
/// The vector whose Lorentz inner product with each point on the surface is constant. If this is timelike, the surface is a sphere, and this is its center. If this is spacelike, the surface is hypercyclic, and this is its plane. Otherwise, the surface is horocyclic, and this represents the convergence point of its perpendiculars.
direction: HypVec3,
/// If `direction` is a timelike unit, this is the negation of the hyperbolic cosine of the radius of the sphere around the corresponding point; if `direction` is a spacelike unit, this is the hyperbolic sine of the distance from the corresponding plane.
offset: f64,
}
impl Line {
/// Create a ray from timelike unit `from` to `to`.
fn new_ray(from: HypVec3, to: HypVec3) -> Option<Line> {
let timelike = from;
let direction = normalize_spacelike((to - from) + timelike * lorentz(timelike, (to - from)))?;
Some(Line { timelike, direction })
}
}