{-# LINE 1 "src/Chiphunk/Low/Vect.chs" #-}
module Chiphunk.Low.Vect
( Vect (..)
, cpv
, vZero
, vEql
, vAdd
, vSub
, vNeg
, vMult
, vDot
, vCross
, vPerp
, vRPerp
, vProject
, vRotate
, vUnRotate
, vLength
, vLengthSq
, vLerp
, vLerpConst
, vSLerp
, vSLerpConst
, vNormalize
, vClamp
, vDist
, vDistSq
, vNear
, vForAngle
, vToAngle
) where
import qualified Foreign.C.Types as C2HSImp
import qualified System.IO.Unsafe as C2HSImp
import Data.Cross
import Data.VectorSpace
import Foreign
import Chiphunk.Low.Types
{-# LINE 42 "src/Chiphunk/Low/Vect.chs" #-}
cpv :: Double -> Double -> Vect
cpv :: Double -> Double -> Vect
cpv = Double -> Double -> Vect
Vect
vZero :: Vect
vZero :: Vect
vZero = Vect
forall v. AdditiveGroup v => v
zeroV
vEql :: Vect -> Vect -> Bool
vEql :: Vect -> Vect -> Bool
vEql = Vect -> Vect -> Bool
forall a. Eq a => a -> a -> Bool
(==)
vAdd :: Vect -> Vect -> Vect
vAdd :: Vect -> Vect -> Vect
vAdd = Vect -> Vect -> Vect
forall v. AdditiveGroup v => v -> v -> v
(^+^)
vSub :: Vect -> Vect -> Vect
vSub :: Vect -> Vect -> Vect
vSub = Vect -> Vect -> Vect
forall v. AdditiveGroup v => v -> v -> v
(^-^)
vNeg :: Vect -> Vect
vNeg :: Vect -> Vect
vNeg = Vect -> Vect
forall v. AdditiveGroup v => v -> v
negateV
vMult :: Vect -> Double -> Vect
vMult :: Vect -> Double -> Vect
vMult = Vect -> Double -> Vect
forall v s. (VectorSpace v, s ~ Scalar v) => v -> s -> v
(^*)
vDot :: Vect -> Vect -> Double
vDot :: Vect -> Vect -> Double
vDot = Vect -> Vect -> Double
forall v. InnerSpace v => v -> v -> Scalar v
(<.>)
vCross :: Vect -> Vect -> Double
Vect Double
x1 Double
y1 vCross :: Vect -> Vect -> Double
`vCross` Vect Double
x2 Double
y2 = Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
y1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x2
vPerp :: Vect -> Vect
vPerp :: Vect -> Vect
vPerp = Vect -> Vect
forall v. HasCross2 v => v -> v
cross2
vRPerp :: Vect -> Vect
vRPerp :: Vect -> Vect
vRPerp Vect
v = Vect -> Vect
forall v. AdditiveGroup v => v -> v
negateV (Vect -> Vect) -> Vect -> Vect
forall a b. (a -> b) -> a -> b
$ Vect -> Vect
forall v. HasCross2 v => v -> v
cross2 Vect
v
vProject
:: Vect
-> Vect
-> Vect
vProject :: Vect -> Vect -> Vect
vProject = Vect -> Vect -> Vect
forall v s.
(InnerSpace v, s ~ Scalar v, Fractional s) =>
v -> v -> v
project
vRotate
:: Vect
-> Vect
-> Vect
Vect Double
x1 Double
y1 vRotate :: Vect -> Vect -> Vect
`vRotate` Vect Double
x2 Double
y2 = Double -> Double -> Vect
Vect (Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
y1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y2) (Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y1)
vUnRotate :: Vect -> Vect -> Vect
Vect Double
x1 Double
y1 vUnRotate :: Vect -> Vect -> Vect
`vUnRotate` Vect Double
x2 Double
y2 = Double -> Double -> Vect
Vect (Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
y1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y2) (Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
x1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y2)
vLength :: Vect -> Double
vLength :: Vect -> Double
vLength = Vect -> Double
forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude
vLengthSq :: Vect -> Double
vLengthSq :: Vect -> Double
vLengthSq = Vect -> Double
forall v s. (InnerSpace v, s ~ Scalar v) => v -> s
magnitudeSq
vLerp
:: Vect
-> Vect
-> Double
-> Vect
vLerp :: Vect -> Vect -> Double -> Vect
vLerp = Vect -> Vect -> Double -> Vect
forall v. VectorSpace v => v -> v -> Scalar v -> v
lerp
vLerpConst
:: Vect
-> Vect
-> Double
-> Vect
vLerpConst :: Vect -> Vect -> Double -> Vect
vLerpConst Vect
a Vect
b Double
l = Vect
a Vect -> Vect -> Vect
forall v. AdditiveGroup v => v -> v -> v
^+^ Vect -> Double -> Vect
vClamp (Vect
b Vect -> Vect -> Vect
forall v. AdditiveGroup v => v -> v -> v
^-^ Vect
a) Double
l
vSLerp :: (Vect)
-> (Vect)
-> (Double) -> (Vect)
vSLerp :: Vect -> Vect -> Double -> Vect
vSLerp Vect
a1 Vect
a2 Double
a3 =
IO Vect -> Vect
forall a. IO a -> a
C2HSImp.unsafePerformIO (IO Vect -> Vect) -> IO Vect -> Vect
forall a b. (a -> b) -> a -> b
$
Vect -> (Ptr Vect -> IO Vect) -> IO Vect
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Vect
a1 ((Ptr Vect -> IO Vect) -> IO Vect)
-> (Ptr Vect -> IO Vect) -> IO Vect
forall a b. (a -> b) -> a -> b
$ \Ptr Vect
a1' ->
Vect -> (Ptr Vect -> IO Vect) -> IO Vect
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Vect
a2 ((Ptr Vect -> IO Vect) -> IO Vect)
-> (Ptr Vect -> IO Vect) -> IO Vect
forall a b. (a -> b) -> a -> b
$ \Ptr Vect
a2' ->
let {a3' :: CDouble
a3' = Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
a3} in
(Ptr Vect -> IO Vect) -> IO Vect
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr Vect -> IO Vect) -> IO Vect)
-> (Ptr Vect -> IO Vect) -> IO Vect
forall a b. (a -> b) -> a -> b
$ \Ptr Vect
a4' ->
Ptr Vect -> Ptr Vect -> CDouble -> Ptr Vect -> IO ()
vSLerp'_ Ptr Vect
a1' Ptr Vect
a2' CDouble
a3' Ptr Vect
a4' IO () -> IO Vect -> IO Vect
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Ptr Vect -> IO Vect
forall a. Storable a => Ptr a -> IO a
peek Ptr Vect
a4'IO Vect -> (Vect -> IO Vect) -> IO Vect
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Vect
a4'' ->
Vect -> IO Vect
forall (m :: * -> *) a. Monad m => a -> m a
return (Vect
a4'')
{-# LINE 164 "src/Chiphunk/Low/Vect.chs" #-}
vSLerpConst :: (Vect)
-> (Vect)
-> (Double)
-> (Vect)
vSLerpConst a1 a2 a3 =
C2HSImp.unsafePerformIO $
with a1 $ \a1' ->
with a2 $ \a2' ->
let {a3' = realToFrac a3} in
alloca $ \a4' ->
vSLerpConst'_ a1' a2' a3' a4' >>
peek a4'>>= \a4'' ->
return (a4'')
{-# LINE 172 "src/Chiphunk/Low/Vect.chs" #-}
vNormalize :: Vect -> Vect
vNormalize = normalized
vClamp
:: Vect
-> Double
-> Vect
vClamp :: Vect -> Double -> Vect
vClamp Vect
v Double
l
| Vect -> Double
forall v s. (InnerSpace v, s ~ Scalar v) => v -> s
magnitudeSq Vect
v Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
l Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
l = Double
Scalar Vect
l Scalar Vect -> Vect -> Vect
forall v. VectorSpace v => Scalar v -> v -> v
*^ Vect -> Vect
forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> v
normalized Vect
v
| Bool
otherwise = Vect
v
vDist
:: Vect
-> Vect
-> Double
vDist :: Vect -> Vect -> Double
vDist Vect
v1 Vect
v2 = Vect -> Double
forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude (Vect -> Double) -> Vect -> Double
forall a b. (a -> b) -> a -> b
$ Vect
v1 Vect -> Vect -> Vect
forall v. AdditiveGroup v => v -> v -> v
^-^ Vect
v2
vDistSq
:: Vect
-> Vect
-> Double
vDistSq :: Vect -> Vect -> Double
vDistSq Vect
v1 Vect
v2 = Vect -> Double
forall v s. (InnerSpace v, s ~ Scalar v) => v -> s
magnitudeSq (Vect -> Double) -> Vect -> Double
forall a b. (a -> b) -> a -> b
$ Vect
v1 Vect -> Vect -> Vect
forall v. AdditiveGroup v => v -> v -> v
^-^ Vect
v2
vNear
:: Vect
-> Vect
-> Double
-> Bool
vNear :: Vect -> Vect -> Double -> Bool
vNear Vect
v1 Vect
v2 Double
d = Vect -> Vect -> Double
vDistSq Vect
v1 Vect
v2 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
d Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
d
vForAngle :: Double -> Vect
vForAngle :: Double -> Vect
vForAngle Double
alpha = Double -> Double -> Vect
Vect (Double -> Double
forall a. Floating a => a -> a
cos Double
alpha) (Double -> Double
forall a. Floating a => a -> a
sin Double
alpha)
vToAngle
:: Vect
-> Double
vToAngle :: Vect -> Double
vToAngle (Vect Double
x Double
y) = Double -> Double -> Double
forall a. RealFloat a => a -> a -> a
atan2 Double
y Double
x
foreign import ccall unsafe "Chiphunk/Low/Vect.chs.h __c2hs_wrapped__w_cpvslerp"
vSLerp'_ :: ((VectPtr) -> ((VectPtr) -> (C2HSImp.CDouble -> ((VectPtr) -> (IO ())))))
foreign import ccall unsafe "Chiphunk/Low/Vect.chs.h __c2hs_wrapped__w_cpvslerpconst"
vSLerpConst'_ :: ((VectPtr) -> ((VectPtr) -> (C2HSImp.CDouble -> ((VectPtr) -> (IO ())))))