#! Contains `ray`, a half line from a place in a direction, and where it first #! meets a plane, a sphere, or a box, as picking what's under a pointer, a line #! of sight, or a shot asks. import std::math; import std::serial; import malunal::linear; module malunal::geometry; #! A half line from a place in a direction. How far along it something lies is #! counted in lengths of its direction, so it's a distance where the direction #! is of length 1, as `toward` makes it. @serial pub struct ray { @serial pub var origin: vec3; @serial pub var direction: vec3; } #! The ray from a place toward another, its direction of length 1. pub def ray::toward(origin: vec3, target: vec3): ray { return ray{ origin, (target - origin).normalized() }; } pub def ray::equals(&this, other: &ray): bool { return this.origin == other.origin && this.direction == other.direction; } #! The place so far along it. pub def ray::at(&this, t: f32): vec3 { return this.origin + this.direction * t; } #! Its place nearest another, from its origin on. pub def ray::closest(&this, p: vec3): vec3 { val d := &this.direction; val toward := (p.x - this.origin.x) * d.x + (p.y - this.origin.y) * d.y + (p.z - this.origin.z) * d.z; val along := toward / dot_of(d, d); return this.at(along > 0 ? along : 0); } #! How far along it it meets a plane, from either side, or none where it runs #! beside the plane or away from it. pub def ray::hit(&this, p: plane): maybe { val toward := dot_of(&p.normal, &this.direction); return nothing {} if toward == 0; val t := p.distance_to(this.origin) / -toward; return nothing {} if !(t >= 0); return t; } #! How far along it it first meets a sphere, 0 where it begins within, or none #! where it passes by or away. pub def ray::hit(&this, s: sphere): maybe { val d := &this.direction; val mx := this.origin.x - s.center.x; val my := this.origin.y - s.center.y; val mz := this.origin.z - s.center.z; val c := mx * mx + my * my + mz * mz - s.radius * s.radius; return 0f if c <= 0; # Outside, and moving away from the center or across it, it never comes nearer. val b := mx * d.x + my * d.y + mz * d.z; return nothing {} if b >= 0; val a := dot_of(d, d); val e := b * b - a * c; return nothing {} if e < 0; return (-b - math::sqrt(e)) / a; } #! How far along it it first meets a box, 0 where it begins within, or none #! where it passes by or away: where it is between the box's faces along every #! axis at once. One along a face, in its plane, may meet the box or pass by. pub def ray::hit(&this, b: bounds): maybe { # Along an axis it doesn't move along, it's between the faces everywhere, or # nowhere, as dividing by zero gives infinities. val ix := 1 / this.direction.x; val iy := 1 / this.direction.y; val iz := 1 / this.direction.z; val x0 := (b.min.x - this.origin.x) * ix; val x1 := (b.max.x - this.origin.x) * ix; val y0 := (b.min.y - this.origin.y) * iy; val y1 := (b.max.y - this.origin.y) * iy; val z0 := (b.min.z - this.origin.z) * iz; val z1 := (b.max.z - this.origin.z) * iz; val enter := most(most(least(x0, x1), least(y0, y1)), most(least(z0, z1), 0)); val leave := least(least(most(x0, x1), most(y0, y1)), most(z0, z1)); return nothing {} if !(enter <= leave); return enter; }