diff options
Diffstat (limited to 'Spear/Math/Plane.hs')
| -rw-r--r-- | Spear/Math/Plane.hs | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs index 0f5829b..8772a42 100644 --- a/Spear/Math/Plane.hs +++ b/Spear/Math/Plane.hs | |||
| @@ -1,8 +1,8 @@ | |||
| 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 | ||
| @@ -13,21 +13,22 @@ import Spear.Math.Vector3 as Vector | |||
| 13 | data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) | 13 | data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | data Plane = Plane { | 16 | data Plane = Plane |
| 17 | n :: !Vector3, | 17 | { n :: {-# UNPACK #-} !Vector3, |
| 18 | d :: !Float | 18 | d :: {-# UNPACK #-} !Float |
| 19 | } deriving(Eq, Show) | 19 | } |
| 20 | deriving(Eq, Show) | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | -- | Create a plane given a normal vector and a distance from the origin. | 23 | -- | Create a plane given a normal vector and a distance from the origin. |
| 23 | plane :: Vector3 -> Float -> Plane | 24 | plane :: Vector3 -> Float -> Plane |
| 24 | plane n d = Plane (normalise n) d | 25 | plane n d = Plane (normalise n) d |
| 25 | 26 | ||
| 26 | 27 | ||
| 27 | -- | Classify the given point's relative position with respect to the given plane. | 28 | -- | Classify the given point's relative position with respect to the given plane. |
| 28 | classify :: Plane -> Vector3 -> PointPlanePos | 29 | classify :: Plane -> Vector3 -> PointPlanePos |
| 29 | classify (Plane n d) pt = case (n `dot` pt - d) `compare` 0 of | 30 | classify (Plane n d) pt = |
| 30 | GT -> Front | 31 | case (n `dot` pt - d) `compare` 0 of |
| 31 | LT -> Back | 32 | GT -> Front |
| 32 | EQ -> Contained | 33 | LT -> Back |
| 33 | \ No newline at end of file | 34 | EQ -> Contained |
