MCPcopy Create free account
hub / github.com/alicevision/AliceVision / rayIntersectUnitSphere

Function rayIntersectUnitSphere

src/aliceVision/geometry/Intersection.cpp:12–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10namespace geometry {
11
12bool rayIntersectUnitSphere(Vec3& coordinates, const Vec3& start, const Vec3& direction)
13{
14 /*
15 "Which point on the sphere relates to this point ?"
16 (lambda * directionx + startx)^2
17 + (lambda * directiony + starty)^2
18 + (lambda * directionz + startz)^2=1
19
20 (lambda * directionx)^2 + startx^2 + 2.0 * lambda * directionoriginx * directionx
21 + (lambda * directiony)^2 + starty^2 + 2.0 * lambda * directionoriginy * directiony
22 + (lambda * directionz)^2 + startz^2 + 2.0 * lambda * directionoriginz * directionz
23 = 1
24
25 (lambda^2) * (directionx^2 + directiony^2 + directionz^2)
26 + lambda * (2.0 * directionoriginx * directionx + 2.0 * directionoriginy * directiony + 2.0 * directionoriginz * directionz)
27 + (startx^2 + startx^2 + startx^2) - 1 = 0
28 */
29
30 double a = direction.dot(direction);
31 double b = direction.dot(start) * 2.0;
32 double c = start.dot(start) - 1.0;
33 double det = b * b - 4.0 * a * c;
34
35 if (det < 0)
36 {
37 return false;
38 }
39
40 double x1 = (-b + sqrt(det)) / (2.0 * a);
41 double x2 = (-b - sqrt(det)) / (2.0 * a);
42 double lambda = std::min(x1, x2);
43
44 coordinates = start + lambda * direction;
45
46 return true;
47}
48
49bool rayIntersectAABB(const Eigen::Vector3d& bbMin,
50 const Eigen::Vector3d& bbMax,

Callers 1

getPointOnSphereFunction · 0.85

Calls 1

dotMethod · 0.45

Tested by 1

getPointOnSphereFunction · 0.68