]> Dogcows Code - chaz/yoink/blob - src/Moof/Plane.hh
sockets documentation and cleanup
[chaz/yoink] / src / Moof / Plane.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_PLANE_HH_
13 #define _MOOF_PLANE_HH_
14
15 #include <Moof/Math.hh>
16 #include <Moof/Shape.hh>
17
18
19 namespace Mf {
20
21
22 template <int D> class Aabb;
23 template <int D> class Sphere;
24
25
26 /*
27 * A plane in 3-space defined by the equation Ax + By + Cz = D, where [A,
28 * B, C] is normal to the plane.
29 */
30
31 struct Plane : public Shape<3>
32 {
33 Vector3 normal;
34 Scalar d;
35
36 typedef enum
37 {
38 NEGATIVE = -1,
39 INTERSECT = 0,
40 POSITIVE = 1
41 } Halfspace;
42
43 Plane() {}
44 Plane(const Vector3& vector, Scalar scalar) :
45 normal(vector),
46 d(scalar) {}
47 Plane(Scalar a, Scalar b, Scalar c, Scalar scalar) :
48 normal(a, b, c),
49 d(scalar) {}
50
51
52 bool intersectRay(const Ray<3>& ray, Ray<3>::Contact& hit)
53 {
54 // solve: [(ray.point + t*ray.direction) dot normal] + d = 0
55
56 Scalar denom = cml::dot(ray.direction, normal);
57
58 // check for parallel condition
59 if (denom == SCALAR(0.0))
60 {
61 if (isEqual(cml::dot(ray.point, normal), -d))
62 {
63 // the ray lies on the plane
64 hit.distance = SCALAR(0.0);
65 hit.normal.set(0.0, 0.0, 0.0);
66 return true;
67 }
68
69 // no solution
70 return false;
71 }
72
73 Scalar numer = cml::dot(ray.point, normal) + d;
74 hit.distance = -numer / denom;
75 if (hit.distance < SCALAR(0.0)) return false;
76
77 if (numer >= 0.0) hit.normal = normal;
78 else hit.normal = -normal;
79 return true;
80 }
81
82
83 /* Causes the normal of the plane to become normalized. The scalar may
84 * also be changed to keep the equation true. Word to the wise: don't
85 * normalize a plane if the normal is the zero vector.
86 */
87 void normalize()
88 {
89 Scalar mag = normal.length();
90
91 normal /= mag;
92 d /= mag;
93 }
94
95 /**
96 * Determine the shortest distance between a point and the plane.
97 */
98 Scalar getDistanceToPoint(const Vector3& point) const
99 {
100 return cml::dot(point, normal) + d;
101 }
102
103 Halfspace intersects(const Vector3& point) const
104 {
105 Scalar distance = getDistanceToPoint(point);
106
107 if (isEqual(distance, 0.0)) return INTERSECT;
108 else if (distance < 0.0) return NEGATIVE;
109 else return POSITIVE;
110 }
111
112 Halfspace intersects(const Aabb<3>& aabb) const;
113 Halfspace intersects(const Sphere<3>& sphere) const;
114 };
115
116
117 } // namespace Mf
118
119 #endif // _MOOF_PLANE_HH_
120
This page took 0.033246 seconds and 4 git commands to generate.