aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Plane.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/Math/Plane.hs')
-rw-r--r--Spear/Math/Plane.hs78
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 @@
1module Spear.Math.Plane 1module Spear.Math.Plane
2( 2(
3 Plane 3 Plane
4, plane 4, plane
5, classify 5, classify
6) 6)
7where 7where
8 8
9import Spear.Math.Vector 9import Spear.Math.Vector
10 10
11data PointPlanePos = Front | Back | Contained deriving (Eq, Show) 11data PointPlanePos = Front | Back | Contained deriving (Eq, Show)
12 12
13data Plane = Plane 13data 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.
20plane :: Vector3 -> Float -> Plane 20plane :: Vector3 -> Float -> Plane
21plane n d = Plane (normalise n) d 21plane 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.
26fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane 26fromPoints :: Vector3 -> Vector3 -> Vector3 -> Plane
27fromPoints p0 p1 p2 = Plane n d 27fromPoints 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.
34classify :: Plane -> Vector3 -> PointPlanePos 34classify :: Plane -> Vector3 -> PointPlanePos
35classify (Plane n d) pt = 35classify (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