]>
Dogcows Code - chaz/yoink/blob - src/moof/plane.hh
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
10 #ifndef _MOOF_PLANE_HH_
11 #define _MOOF_PLANE_HH_
13 #include <moof/math.hh>
14 #include <moof/shape.hh>
19 * Classes and functions related to planes.
25 // forward declarations
26 template <int D
> class aabb
;
27 template <int D
> class sphere
;
30 * A plane in 3-space defined by the equation Ax + By + Cz = D, where [A, B,
31 * C] is normal to the plane.
33 struct plane
: public shape
<3>
46 plane(const vector3
& vector
, scalar scalar
) :
49 plane(scalar a
, scalar b
, scalar c
, scalar scalar
) :
53 bool intersect_ray(const ray3
& ray
, ray3::contact
& hit
)
55 // solve: [(ray.point + t*ray.direction) dot normal] + d = 0
57 scalar denom
= dot(ray
.direction
, normal
);
59 // check for parallel condition
60 if (denom
== SCALAR(0.0))
62 if (is_equal(dot(ray
.point
, normal
), -d
))
64 // the ray lies on the plane
65 hit
.distance
= SCALAR(0.0);
66 hit
.normal
.set(0.0, 0.0, 0.0);
74 scalar numer
= dot(ray
.point
, normal
) + d
;
75 hit
.distance
= -numer
/ denom
;
76 if (hit
.distance
< SCALAR(0.0)) return false;
78 if (numer
>= 0.0) hit
.normal
= normal
;
79 else hit
.normal
= -normal
;
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.
89 scalar mag
= normal
.length();
95 * Determine the shortest distance between a point and the plane.
97 scalar
distance_to_point(const vector3
& point
) const
99 return dot(point
, normal
) + d
;
102 halfspace
intersects(const vector3
& point
) const
104 scalar distance
= distance_to_point(point
);
106 if (is_equal(distance
, 0.0)) return intersecting
;
107 else if (distance
< 0.0) return negative
;
111 halfspace
intersects(const aabb
<3>& aabb
) const;
112 halfspace
intersects(const sphere
<3>& sphere
) const;
118 #endif // _MOOF_PLANE_HH_
This page took 0.04316 seconds and 4 git commands to generate.