{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE MultiParamTypeClasses     #-}
{-# LANGUAGE StandaloneDeriving        #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
-- |
-- Module      :  Data.Point3
-- Copyright   :  (c) Antony Courtney and Henrik Nilsson, Yale University, 2003
-- License     :  BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer  :  ivan.perez@keera.co.uk
-- Stability   :  provisional
-- Portability :  non-portable (GHC extensions)
--
-- 3D point abstraction (R^3).
module Data.Point3
    ( Point3(..) -- Non-abstract, instance of AffineSpace
    , point3X    -- :: RealFloat a => Point3 a -> a
    , point3Y    -- :: RealFloat a => Point3 a -> a
    , point3Z    -- :: RealFloat a => Point3 a -> a
    )
  where

-- External imports
import Control.DeepSeq (NFData(..))

-- Internal imports
import Data.AffineSpace
import Data.Vector3
import Data.VectorSpace ()

-- * 3D point, constructors and selectors

-- | 3D point.
data Point3 a = RealFloat a => Point3 !a !a !a

deriving instance Eq a => Eq (Point3 a)

deriving instance Show a => Show (Point3 a)

instance NFData a => NFData (Point3 a) where
  rnf :: Point3 a -> ()
rnf (Point3 a
x a
y a
z) = forall a. NFData a => a -> ()
rnf a
x seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf a
y seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf a
z seq :: forall a b. a -> b -> b
`seq` ()

-- | X coordinate of a 3D point.
point3X :: RealFloat a => Point3 a -> a
point3X :: forall a. RealFloat a => Point3 a -> a
point3X (Point3 a
x a
_ a
_) = a
x

-- | Y coordinate of a 3D point.
point3Y :: RealFloat a => Point3 a -> a
point3Y :: forall a. RealFloat a => Point3 a -> a
point3Y (Point3 a
_ a
y a
_) = a
y

-- | Z coordinate of a 3D point.
point3Z :: RealFloat a => Point3 a -> a
point3Z :: forall a. RealFloat a => Point3 a -> a
point3Z (Point3 a
_ a
_ a
z) = a
z

-- * Affine space instance

instance RealFloat a => AffineSpace (Point3 a) (Vector3 a) a where
  origin :: Point3 a
origin = forall a. RealFloat a => a -> a -> a -> Point3 a
Point3 a
0 a
0 a
0

  (Point3 a
x a
y a
z) .+^ :: Point3 a -> Vector3 a -> Point3 a
.+^ Vector3 a
v =
    forall a. RealFloat a => a -> a -> a -> Point3 a
Point3 (a
x forall a. Num a => a -> a -> a
+ forall a. RealFloat a => Vector3 a -> a
vector3X Vector3 a
v) (a
y forall a. Num a => a -> a -> a
+ forall a. RealFloat a => Vector3 a -> a
vector3Y Vector3 a
v) (a
z forall a. Num a => a -> a -> a
+ forall a. RealFloat a => Vector3 a -> a
vector3Z Vector3 a
v)

  (Point3 a
x a
y a
z) .-^ :: Point3 a -> Vector3 a -> Point3 a
.-^ Vector3 a
v =
    forall a. RealFloat a => a -> a -> a -> Point3 a
Point3 (a
x forall a. Num a => a -> a -> a
- forall a. RealFloat a => Vector3 a -> a
vector3X Vector3 a
v) (a
y forall a. Num a => a -> a -> a
- forall a. RealFloat a => Vector3 a -> a
vector3Y Vector3 a
v) (a
z forall a. Num a => a -> a -> a
- forall a. RealFloat a => Vector3 a -> a
vector3Z Vector3 a
v)

  (Point3 a
x1 a
y1 a
z1) .-. :: Point3 a -> Point3 a -> Vector3 a
.-. (Point3 a
x2 a
y2 a
z2) =
    forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3 (a
x1 forall a. Num a => a -> a -> a
- a
x2) (a
y1 forall a. Num a => a -> a -> a
- a
y2) (a
z1 forall a. Num a => a -> a -> a
- a
z2)