diff options
Diffstat (limited to 'Spear/Math/Plane.hs')
-rw-r--r-- | Spear/Math/Plane.hs | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs index 08e4570..ee788b5 100644 --- a/Spear/Math/Plane.hs +++ b/Spear/Math/Plane.hs | |||
@@ -1,39 +1,39 @@ | |||
1 | module Spear.Math.Plane | 1 | module Spear.Math.Plane |
2 | ( | 2 | ( |
3 | Plane | 3 | Plane |
4 | , plane | 4 | , plane |
5 | , classify | 5 | , classify |
6 | ) | 6 | ) |
7 | where | 7 | where |
8 | 8 | ||
9 | import Spear.Math.Vector | 9 | import Spear.Math.Vector |
10 | 10 | ||
11 | data PointPlanePos = Front | Back | Contained deriving (Eq, Show) | 11 | data PointPlanePos = Front | Back | Contained deriving (Eq, Show) |
12 | 12 | ||
13 | data Plane = Plane | 13 | data Plane = Plane |
14 | { n :: {-# UNPACK #-} !Vector3, | 14 | { n :: {-# UNPACK #-} !Vector3, |
15 | d :: {-# UNPACK #-} !Float | 15 | d :: {-# UNPACK #-} !Float |
16 | } | 16 | } |
17 | deriving(Eq, Show) | 17 | deriving(Eq, Show) |
18 | 18 | ||
19 | -- | Construct a plane from a normal vector and a distance from the origin. | 19 | -- | Construct a plane from a normal vector and a distance from the origin. |
20 | plane :: Vector3 -> Float -> Plane | 20 | plane :: Vector3 -> Float -> Plane |
21 | plane n d = Plane (normalise n) d | 21 | plane n d = Plane (normalise n) d |
22 | 22 | ||
23 | -- | Construct a plane from three points. | 23 | -- | Construct a plane from three points. |
24 | -- | 24 | -- |
25 | -- Points must be given in counter-clockwise order. | 25 | -- Points must be given in counter-clockwise order. |
26 | fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane | 26 | fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane |
27 | fromPoints p0 p1 p2 = Plane n d | 27 | fromPoints p0 p1 p2 = Plane n d |
28 | where n = normalise $ v1 `cross` v2 | 28 | where n = normalise $ v1 `cross` v2 |
29 | v1 = p2 - p1 | 29 | v1 = p2 - p1 |
30 | v2 = p0 - p1 | 30 | v2 = p0 - p1 |
31 | d = p0 `dot` n | 31 | d = p0 `dot` n |
32 | 32 | ||
33 | -- | Classify the given point's relative position with respect to the plane. | 33 | -- | Classify the given point's relative position with respect to the plane. |
34 | classify :: Plane -> Vector3 -> PointPlanePos | 34 | classify :: Plane -> Vector3 -> PointPlanePos |
35 | classify (Plane n d) pt = | 35 | classify (Plane n d) pt = |
36 | case (n `dot` pt - d) `compare` 0 of | 36 | case (n `dot` pt - d) `compare` 0 of |
37 | GT -> Front | 37 | GT -> Front |
38 | LT -> Back | 38 | LT -> Back |
39 | EQ -> Contained | 39 | EQ -> Contained |