{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
{-# LANGUAGE ExistentialQuantification, MultiParamTypeClasses, FlexibleInstances, StandaloneDeriving #-}
-----------------------------------------------------------------------------------------
-- |
-- Module      :  Data.Vector3
-- 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 vector abstraction (R^3).
--
-----------------------------------------------------------------------------------------

module Data.Vector3 (
    Vector3,            -- Abstract, instance of VectorSpace
    vector3,            -- :: RealFloat a => a -> a -> a -> Vector3 a
    vector3X,           -- :: RealFloat a => Vector3 a -> a
    vector3Y,           -- :: RealFloat a => Vector3 a -> a
    vector3Z,           -- :: RealFloat a => Vector3 a -> a
    vector3XYZ,         -- :: RealFloat a => Vector3 a -> (a, a, a)
    vector3Spherical,   -- :: RealFloat a => a -> a -> a -> Vector3 a
    vector3Rho,         -- :: RealFloat a => Vector3 a -> a
    vector3Theta,       -- :: RealFloat a => Vector3 a -> a
    vector3Phi,         -- :: RealFloat a => Vector3 a -> a
    vector3RhoThetaPhi, -- :: RealFloat a => Vector3 a -> (a, a, a)
    vector3Rotate       -- :: RealFloat a => a -> a -> Vector3 a -> Vector3 a
) where

import Control.DeepSeq (NFData(..))

import Data.VectorSpace

-- * 3D vector, constructors and selectors

-- | 3D Vector.

-- Restrict coefficient space to RealFloat (rather than Floating) for now.
-- While unclear if a complex coefficient space would be useful (and if the
-- result really would be a 3d vector), the only thing causing trouble is the
-- use of atan2 in vector3Theta and vector3Phi. Maybe atan2 can be generalized?

data Vector3 a = RealFloat a => Vector3 !a !a !a

deriving instance Eq a => Eq (Vector3 a)

deriving instance Show a => Show (Vector3 a)

instance NFData a => NFData (Vector3 a) where
  rnf :: Vector3 a -> ()
rnf (Vector3 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` ()

-- | Creates a 3D vector from the cartesian coordinates.
vector3 :: RealFloat a => a -> a -> a -> Vector3 a
vector3 :: forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3 = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3

-- | X cartesian coordinate.
vector3X :: RealFloat a => Vector3 a -> a
vector3X :: forall a. RealFloat a => Vector3 a -> a
vector3X (Vector3 a
x a
_ a
_) = a
x

-- | Y cartesian coordinate.
vector3Y :: RealFloat a => Vector3 a -> a
vector3Y :: forall a. RealFloat a => Vector3 a -> a
vector3Y (Vector3 a
_ a
y a
_) = a
y

-- | Z cartesian coordinate.
vector3Z :: RealFloat a => Vector3 a -> a
vector3Z :: forall a. RealFloat a => Vector3 a -> a
vector3Z (Vector3 a
_ a
_ a
z) = a
z

-- | Returns a vector's cartesian coordinates.
vector3XYZ :: RealFloat a => Vector3 a -> (a, a, a)
vector3XYZ :: forall a. RealFloat a => Vector3 a -> (a, a, a)
vector3XYZ (Vector3 a
x a
y a
z) = (a
x, a
y, a
z)

-- | Creates a 3D vector from the spherical coordinates.
vector3Spherical :: RealFloat a => a -> a -> a -> Vector3 a
vector3Spherical :: forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3Spherical a
rho a
theta a
phi =
    forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
rhoSinPhi forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos a
theta) (a
rhoSinPhi forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin a
theta) (a
rho forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos a
phi)
    where
        rhoSinPhi :: a
rhoSinPhi = a
rho forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin a
phi

-- | Calculates the vector's radial distance.
vector3Rho :: RealFloat a => Vector3 a -> a
vector3Rho :: forall a. RealFloat a => Vector3 a -> a
vector3Rho (Vector3 a
x a
y a
z) = forall a. Floating a => a -> a
sqrt (a
x forall a. Num a => a -> a -> a
* a
x forall a. Num a => a -> a -> a
+ a
y forall a. Num a => a -> a -> a
* a
y forall a. Num a => a -> a -> a
+ a
z forall a. Num a => a -> a -> a
* a
z)

-- | Calculates the vector's azimuth.
vector3Theta :: RealFloat a => Vector3 a -> a
vector3Theta :: forall a. RealFloat a => Vector3 a -> a
vector3Theta (Vector3 a
x a
y a
_) = forall a. RealFloat a => a -> a -> a
atan2 a
y a
x

-- | Calculates the vector's inclination.
vector3Phi :: RealFloat a => Vector3 a -> a
vector3Phi :: forall a. RealFloat a => Vector3 a -> a
vector3Phi v :: Vector3 a
v@(Vector3 a
_ a
_ a
z) = forall a. Floating a => a -> a
acos (a
z forall a. Fractional a => a -> a -> a
/ forall a. RealFloat a => Vector3 a -> a
vector3Rho Vector3 a
v)

-- | Spherical coordinate representation of a 3D vector.
vector3RhoThetaPhi :: RealFloat a => Vector3 a -> (a, a, a)
vector3RhoThetaPhi :: forall a. RealFloat a => Vector3 a -> (a, a, a)
vector3RhoThetaPhi (Vector3 a
x a
y a
z) = (a
rho, a
theta, a
phi)
    where
        rho :: a
rho   = forall a. Floating a => a -> a
sqrt (a
x forall a. Num a => a -> a -> a
* a
x forall a. Num a => a -> a -> a
+ a
y forall a. Num a => a -> a -> a
* a
y forall a. Num a => a -> a -> a
+ a
z forall a. Num a => a -> a -> a
* a
z)
        theta :: a
theta = forall a. RealFloat a => a -> a -> a
atan2 a
y a
x
        phi :: a
phi   = forall a. Floating a => a -> a
acos (a
z forall a. Fractional a => a -> a -> a
/ a
rho)

-- * Vector space instance

instance RealFloat a => VectorSpace (Vector3 a) a where
    zeroVector :: Vector3 a
zeroVector = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 a
0 a
0 a
0

    a
a *^ :: a -> Vector3 a -> Vector3 a
*^ (Vector3 a
x a
y a
z) = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
a forall a. Num a => a -> a -> a
* a
x) (a
a forall a. Num a => a -> a -> a
* a
y) (a
a forall a. Num a => a -> a -> a
* a
z)

    (Vector3 a
x a
y a
z) ^/ :: Vector3 a -> a -> Vector3 a
^/ a
a = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
x forall a. Fractional a => a -> a -> a
/ a
a) (a
y forall a. Fractional a => a -> a -> a
/ a
a) (a
z forall a. Fractional a => a -> a -> a
/ a
a)

    negateVector :: Vector3 a -> Vector3 a
negateVector (Vector3 a
x a
y a
z) = (forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (-a
x) (-a
y) (-a
z))

    (Vector3 a
x1 a
y1 a
z1) ^+^ :: Vector3 a -> Vector3 a -> Vector3 a
^+^ (Vector3 a
x2 a
y2 a
z2) = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
x1forall a. Num a => a -> a -> a
+a
x2) (a
y1forall a. Num a => a -> a -> a
+a
y2) (a
z1forall a. Num a => a -> a -> a
+a
z2)

    (Vector3 a
x1 a
y1 a
z1) ^-^ :: Vector3 a -> Vector3 a -> Vector3 a
^-^ (Vector3 a
x2 a
y2 a
z2) = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
x1forall a. Num a => a -> a -> a
-a
x2) (a
y1forall a. Num a => a -> a -> a
-a
y2) (a
z1forall a. Num a => a -> a -> a
-a
z2)

    (Vector3 a
x1 a
y1 a
z1) dot :: Vector3 a -> Vector3 a -> a
`dot` (Vector3 a
x2 a
y2 a
z2) = a
x1 forall a. Num a => a -> a -> a
* a
x2 forall a. Num a => a -> a -> a
+ a
y1 forall a. Num a => a -> a -> a
* a
y2 forall a. Num a => a -> a -> a
+ a
z1 forall a. Num a => a -> a -> a
* a
z2

-- * Additional operations

-- | Rotates a vector with a given polar and azimuthal angles.
vector3Rotate :: RealFloat a => a -> a -> Vector3 a -> Vector3 a
vector3Rotate :: forall a. RealFloat a => a -> a -> Vector3 a -> Vector3 a
vector3Rotate a
theta' a
phi' Vector3 a
v =
    forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3Spherical (forall a. RealFloat a => Vector3 a -> a
vector3Rho Vector3 a
v)
                     (forall a. RealFloat a => Vector3 a -> a
vector3Theta Vector3 a
v forall a. Num a => a -> a -> a
+ a
theta')
                     (forall a. RealFloat a => Vector3 a -> a
vector3Phi Vector3 a
v forall a. Num a => a -> a -> a
+ a
phi')