aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Spear/Math/Plane.hs60
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
3module Spear.Math.Plane 3module 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)
9where 12where
10 13
11import Spear.Math.Vector 14import Spear.Math.Vector
12import Spear.Prelude 15import Spear.Prelude
13 16
17
18data Axis = X | Y | Z deriving (Eq, Show)
19
20data AxisOrientation = PositiveAxis | NegativeAxis deriving (Eq, Show)
21
14data PointPlanePos = Front | Back | Contained deriving (Eq, Show) 22data PointPlanePos = Front | Back | Contained deriving (Eq, Show)
15 23
24-- | A 3D plane.
16data Plane = Plane 25data 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.
32data 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.
23plane :: Vector3 -> Float -> Plane 41plane :: Vector3 -> Float -> Plane
24plane n d = Plane (normalise n) d 42plane n d = Plane (normalise n) d
25 43
44-- | Construct an axis-aligned plane.
45axisPlane :: Axis -> Float -> AxisOrientation -> AxisPlane
46axisPlane = 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.
29fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane 51planeFromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane
30fromPoints p0 p1 p2 = Plane n d 52planeFromPoints 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.
37classify :: Plane -> Vector3 -> PointPlanePos 59planeClassify :: Plane -> Vector3 -> PointPlanePos
38classify (Plane n d) pt = 60planeClassify (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.
67axisPlaneClassify :: AxisPlane -> Vector3 -> PointPlanePos
68axisPlaneClassify (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