1 /*
2 Copyright (c) 2011-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Copyright: Timur Gafarov 2011-2021.
31  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
32  * Authors: Timur Gafarov
33  */
34 module dlib.geometry.ray;
35 
36 import std.math;
37 import dlib.math.vector;
38 import dlib.math.utils;
39 
40 /// Ray with starting and ending points
41 struct Ray
42 {
43     Vector3f p0;
44     Vector3f p1;
45     float t;
46 
47     this(Vector3f begin, Vector3f end)
48     {
49         p0 = begin;
50         p1 = end;
51     }
52 
53     bool intersectSphere(Vector3f position, float radius, out Vector3f intersectionPoint)
54     {
55         Vector3f dir = p1 - p0;
56         dir.normalize();
57         Vector3f dist = position - p0;
58         float B = dot(dist,dir);
59         float D = radius * radius - dot(dist,dist) + B * B;
60         if (D < 0.0f)
61         {
62             intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
63             return false;
64         }
65         float t0 = B - sqrt(D);
66         float t1 = B + sqrt(D);
67         if (t0 > 0.0f)
68         {
69             t = t0;
70             intersectionPoint = p0 + dir * t0;
71             return true;
72         }
73         if (t1 > 0.0f)
74         {
75             t = t1;
76             intersectionPoint = p0 + dir * t1;
77             return true;
78         }
79         intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
80         return false;
81     }
82 
83     bool intersectTriangle(Vector3f v0, Vector3f v1, Vector3f v2, out Vector3f intersectionPoint)
84     {
85         Vector3f u, v, n;    // triangle vectors
86         Vector3f dir, w0, w; // ray vectors
87         float r, a, b;       // params to calc ray-plane intersect
88 
89         // get triangle edge vectors and plane normal
90         u = v1 - v0;
91         v = v2 - v0;
92         n = cross(u, v); // cross product
93         if (n.isZero)    // triangle is degenerate
94         {
95             intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
96             return false;
97         }
98 
99         dir = p1 - p0; // ray direction vector
100         w0 = p0 - v0;
101         a = -dot(n, w0);
102         b = dot(n, dir);
103 
104         if (fabs(b) < EPSILON) // ray is parallel to triangle plane
105         {
106             // no intersect
107             intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
108             return false;
109         }
110 
111         // get intersect point of ray with triangle plane
112         r = a / b;
113         if (r < 0.0f) // ray goes away from triangle
114         {
115             // no intersect
116             intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
117             return false;
118         }
119 
120         Vector3f I = p0 + dir * r; // intersect point of ray and plane
121 
122         float uu, uv, vv, wu, wv, D;
123         uu = dot(u, u);
124         uv = dot(u, v);
125         vv = dot(v, v);
126         w = I - v0;
127         wu = dot(w, u);
128         wv = dot(w, v);
129         D = uv * uv - uu * vv;
130 
131         // get and test parametric coords
132         float s, t;
133         s = (uv * wv - vv * wu) / D;
134         if (s < 0.0 || s > 1.0)        // point is outside of the triangle
135         {
136             intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
137             return false;
138         }
139         t = (uv * wu - uu * wv) / D;
140         if (t < 0.0 || (s + t) > 1.0)  // point is outside of the triangle
141         {
142             intersectionPoint = Vector3f(0.0f, 0.0f, 0.0f);
143             return false;
144         }
145 
146         intersectionPoint = I; // point is inside of the triangle
147         return true;
148     }
149 }