1 //          Copyright Ferdinand Majerech 2014.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 module gl3n_extra.plane;
7 
8 
9 public import gl3n.plane;
10 
11 
12 @safe pure nothrow @nogc:
13 
14 /** Construct a plane from a point on the plane and normal of the plane.
15  *
16  * Params:
17  *
18  * point  = A point anywhere on the plane.
19  * normal = Normal of the plane.
20  * 
21  */
22 PlaneT!F planeFromPointNormal(F)(const Vector!(F, 3) point, const Vector!(F, 3) normal)
23 {
24     return PlaneT!F(normal, -dot(point, normal));
25 }
26 
27 /** Find the intersection between a plane and a line, if any.
28  *
29  * Params:
30  *
31  * plane        = Plane to check for intersection with.
32  * lineOrigin   = Origin of the line (the 'anchor point' fom which lineVector starts)
33  * lineVector   = Vector (direction) of the line.
34  * intersection = If the plane intersects the line, the intersection point is written here.
35  *
36  * Returns: true if the plane intersects the line. false otherwise.
37  * 
38  */
39 bool intersectsLine(F)(const PlaneT!F plane, const Vector!(F, 3) lineOrigin,
40                        const Vector!(F, 3) lineVector, out Vector!(F, 3) intersection)
41 {
42     const t2 = dot(plane.normal, lineVector);
43     if(t2 == 0.0f) { return false; }
44 
45     const t = -(dot(plane.normal, lineOrigin) + plane.d) / t2;
46     intersection = lineOrigin + lineVector * t;
47     return true;
48 }