diff options
Diffstat (limited to 'Spear/Math/Plane.hs')
-rw-r--r-- | Spear/Math/Plane.hs | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs index 5440a43..cbf5aed 100644 --- a/Spear/Math/Plane.hs +++ b/Spear/Math/Plane.hs | |||
@@ -2,41 +2,75 @@ | |||
2 | 2 | ||
3 | module Spear.Math.Plane | 3 | module Spear.Math.Plane |
4 | ( | 4 | ( |
5 | Plane | 5 | Plane(..) |
6 | , plane | 6 | , AxisPlane(..) |
7 | , classify | 7 | , plane |
8 | , axisPlane | ||
9 | , planeClassify | ||
10 | , axisPlaneClassify | ||
8 | ) | 11 | ) |
9 | where | 12 | where |
10 | 13 | ||
11 | import Spear.Math.Vector | 14 | import Spear.Math.Vector |
12 | import Spear.Prelude | 15 | import Spear.Prelude |
13 | 16 | ||
17 | |||
18 | data Axis = X | Y | Z deriving (Eq, Show) | ||
19 | |||
20 | data AxisOrientation = PositiveAxis | NegativeAxis deriving (Eq, Show) | ||
21 | |||
14 | data PointPlanePos = Front | Back | Contained deriving (Eq, Show) | 22 | data PointPlanePos = Front | Back | Contained deriving (Eq, Show) |
15 | 23 | ||
24 | -- | A 3D plane. | ||
16 | data Plane = Plane | 25 | data Plane = Plane |
17 | { n :: {-# UNPACK #-} !Vector3, | 26 | { planeNormal :: {-# UNPACK #-} !Vector3 |
18 | d :: {-# UNPACK #-} !Float | 27 | , planeDistance :: {-# UNPACK #-} !Float |
19 | } | 28 | } |
20 | deriving(Eq, Show) | 29 | deriving(Eq, Show) |
21 | 30 | ||
31 | -- | An axis-aligned 3D plane. | ||
32 | data AxisPlane = AxisPlane | ||
33 | { axisPlaneAxis :: !Axis | ||
34 | , axisPlaneDistance :: {-# UNPACK #-} !Float | ||
35 | , axisPlaneNormal :: !AxisOrientation | ||
36 | } | ||
37 | deriving (Eq, Show) | ||
38 | |||
39 | |||
22 | -- | Construct a plane from a normal vector and a distance from the origin. | 40 | -- | Construct a plane from a normal vector and a distance from the origin. |
23 | plane :: Vector3 -> Float -> Plane | 41 | plane :: Vector3 -> Float -> Plane |
24 | plane n d = Plane (normalise n) d | 42 | plane n d = Plane (normalise n) d |
25 | 43 | ||
44 | -- | Construct an axis-aligned plane. | ||
45 | axisPlane :: Axis -> Float -> AxisOrientation -> AxisPlane | ||
46 | axisPlane = AxisPlane | ||
47 | |||
26 | -- | Construct a plane from three points. | 48 | -- | Construct a plane from three points. |
27 | -- | 49 | -- |
28 | -- Points must be given in counter-clockwise order. | 50 | -- Points must be given in counter-clockwise order. |
29 | fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane | 51 | planeFromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane |
30 | fromPoints p0 p1 p2 = Plane n d | 52 | planeFromPoints p0 p1 p2 = Plane n d |
31 | where n = normalise $ v1 `cross` v2 | 53 | where n = normalise $ v1 `cross` v2 |
32 | v1 = p2 - p1 | 54 | v1 = p2 - p1 |
33 | v2 = p0 - p1 | 55 | v2 = p0 - p1 |
34 | d = p0 `dot` n | 56 | d = p0 `dot` n |
35 | 57 | ||
36 | -- | Classify the given point's relative position with respect to the plane. | 58 | -- | Classify the given point's relative position with respect to the plane. |
37 | classify :: Plane -> Vector3 -> PointPlanePos | 59 | planeClassify :: Plane -> Vector3 -> PointPlanePos |
38 | classify (Plane n d) pt = | 60 | planeClassify (Plane n d) pt = |
39 | case (n `dot` pt - d) `compare` 0 of | 61 | case (n `dot` pt - d) `compare` 0 of |
40 | GT -> Front | 62 | GT -> Front |
41 | LT -> Back | 63 | LT -> Back |
42 | EQ -> Contained | 64 | EQ -> Contained |
65 | |||
66 | -- | Classify the given point's relative position with respect to the plane. | ||
67 | axisPlaneClassify :: AxisPlane -> Vector3 -> PointPlanePos | ||
68 | axisPlaneClassify (AxisPlane axis d _) (Vector3 x y z) = | ||
69 | let classify coord | ||
70 | | coord < d = Back | ||
71 | | coord > d = Front | ||
72 | | otherwise = Contained | ||
73 | in case axis of | ||
74 | X -> classify x | ||
75 | Y -> classify y | ||
76 | Z -> classify z | ||