Rise
The Vieneo Province
Loading...
Searching...
No Matches
LampContact.h
Go to the documentation of this file.
1#pragma once
3#include <cmath>
4
5namespace LandingLights
6{
7 // Reuse the existing 15 cm housing allowance. Geometry and velocities are
8 // in exterior coordinates (the negative of network position/velocity).
9 inline constexpr float HousingKm = .00015f;
10 struct Sweep
11 {
13 float seconds = 0;
14
16 const RiseMath::Vector3& c) const
17 {
18 if (!(seconds > 0) || !std::isfinite(seconds)) return 0;
19 const auto ab = b - a, ac = c - a;
20 RiseMath::Vector3 normal;
21 RiseMath::Vec3Cross(&normal, &ab, &ac);
22 const float length = RiseMath::Vec3Length(&normal);
23 if (!(length > 1e-12f)) return 0;
24 normal /= length;
25 const auto fromFace = start - a;
26 float distance = RiseMath::Vec3Dot(&fromFace, &normal);
27 if (distance < 0) { normal = -normal; distance = -distance; }
28 const float closing = -RiseMath::Vec3Dot(&velocity, &normal);
29 if (!(closing > .00075f)) return 0;
30 const float time = (std::max)(0.f, (distance - HousingKm) / closing);
31 if (time > seconds) return 0;
32 // The contact must lie inside this finite triangle; an infinite
33 // wall plane would also damage the lamp that misses a corner.
34 const auto p = start + velocity * time - normal * (distance - closing * time) - a;
35 const double aa = RiseMath::Vec3Dot(&ab, &ab), bb = RiseMath::Vec3Dot(&ac, &ac);
36 const double cross = RiseMath::Vec3Dot(&ab, &ac);
37 const double pa = RiseMath::Vec3Dot(&p, &ab), pb = RiseMath::Vec3Dot(&p, &ac);
38 const double determinant = aa * bb - cross * cross;
39 if (!(determinant > 0)) return 0;
40 const double u = (bb * pa - cross * pb) / determinant;
41 const double v = (aa * pb - cross * pa) / determinant;
42 return u >= -1e-6 && v >= -1e-6 && u + v <= 1.000001 ? closing * 1000.f : 0.f;
43 }
44
45 float Sphere(const RiseMath::Vector3& center, float radiusKm) const
46 {
47 if (!(seconds > 0) || !std::isfinite(seconds) || !(radiusKm > 0)) return 0;
48 const auto offset = start - center;
49 const double speed2 = RiseMath::Vec3LengthSq(&velocity);
50 if (!(speed2 > 0)) return 0;
51 const double approach = RiseMath::Vec3Dot(&offset, &velocity);
52 if (approach >= 0) return 0;
53 const double radius = radiusKm + HousingKm;
54 const double clearance = RiseMath::Vec3LengthSq(&offset) - radius * radius;
55 const double discriminant = approach * approach - speed2 * clearance;
56 if (discriminant < 0) return 0;
57 const double time = clearance <= 0 ? 0 : (-approach - std::sqrt(discriminant)) / speed2;
58 if (time > seconds) return 0;
59 auto normal = offset + velocity * float(time);
60 const float length = RiseMath::Vec3Length(&normal);
61 if (!(length > 0)) return 0;
62 normal /= length;
63 return (std::max)(0.f, -RiseMath::Vec3Dot(&velocity, &normal) * 1000.f);
64 }
65 };
66}
constexpr float HousingKm
Definition LampContact.h:9
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:105
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
float Triangle(const RiseMath::Vector3 &a, const RiseMath::Vector3 &b, const RiseMath::Vector3 &c) const
Definition LampContact.h:15
float Sphere(const RiseMath::Vector3 &center, float radiusKm) const
Definition LampContact.h:45
RiseMath::Vector3 start
Definition LampContact.h:12
RiseMath::Vector3 velocity
Definition LampContact.h:12