{-# LINE 1 "src/Chiphunk/Low/BB.chs" #-}
module Chiphunk.Low.BB
( BB (..)
, bbNew
, bbNewForExtents
, bbNewForCircle
, bbIntersects
, bbContainsBB
, bbContainsVect
, bbMerge
, bbExpand
, bbCenter
, bbArea
, bbMergedArea
, bbSegmentQuery
, bbIntersectsSegment
, bbClampVect
, bbWrapVect
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified System.IO.Unsafe as C2HSImp
import Data.Fixed
import Foreign
import Chiphunk.Low.Math
import Chiphunk.Low.Types
{-# LINE 27 "src/Chiphunk/Low/BB.chs" #-}
bbNew :: Double -> Double -> Double -> Double -> BB
bbNew = BB
bbNewForExtents
:: Vect
-> Double
-> Double
-> BB
bbNewForExtents (Vect x y) hw hh = BB (x - hw) (y - hh) (x + hw) (y + hh)
bbNewForCircle
:: Vect
-> Double
-> BB
bbNewForCircle v r = bbNewForExtents v r r
bbIntersects :: BB -> BB -> Bool
BB l1 b1 r1 t1 `bbIntersects` BB l2 b2 r2 t2 = r1 >= l1 && r2 >= l2 && t1 >= b1 && t2 >= b2
bbContainsBB
:: BB
-> BB
-> Bool
BB l1 b1 r1 t1 `bbContainsBB` BB l2 b2 r2 t2 = l1 <= l2 && r1 >= r2 && t1 >= t2 && b1 <= b2
bbContainsVect
:: BB
-> Vect
-> Bool
BB l b r t `bbContainsVect` Vect x y = l <= x && r >= x && b <= y && t >= y
bbMerge
:: BB
-> BB
-> BB
BB l1 b1 r1 t1 `bbMerge` BB l2 b2 r2 t2 = BB (min l1 l2) (min b1 b2) (max r1 r2) (max t1 t2)
bbExpand
:: BB
-> Vect
-> BB
BB l b r t `bbExpand` Vect x y = BB (min l x) (min b y) (max r x) (max t y)
bbCenter
:: BB
-> Vect
bbCenter (BB l b r t) = Vect ((l + r) / 2) ((b + t) / 2)
bbArea
:: BB
-> Double
bbArea (BB l b r t) = (r - l) * (t - b)
bbMergedArea
:: BB
-> BB
-> Double
BB l1 b1 r1 t1 `bbMergedArea` BB l2 b2 r2 t2 = (max r1 r2 - min l1 l2) * (max t1 t2 - min b1 b2)
bbSegmentQuery :: (BB)
-> (Vect)
-> (Vect)
-> (Double)
bbSegmentQuery a1 a2 a3 =
C2HSImp.unsafePerformIO $
with a1 $ \a1' ->
with a2 $ \a2' ->
with a3 $ \a3' ->
bbSegmentQuery'_ a1' a2' a3' >>= \res ->
let {res' = realToFrac res} in
return (res')
{-# LINE 107 "src/Chiphunk/Low/BB.chs" #-}
bbIntersectsSegment :: (BB)
-> (Vect)
-> (Vect)
-> (Bool)
bbIntersectsSegment a1 a2 a3 =
C2HSImp.unsafePerformIO $
with a1 $ \a1' ->
with a2 $ \a2' ->
with a3 $ \a3' ->
bbIntersectsSegment'_ a1' a2' a3' >>= \res ->
let {res' = C2HSImp.toBool res} in
return (res')
{-# LINE 114 "src/Chiphunk/Low/BB.chs" #-}
bbClampVect
:: BB
-> Vect
-> Vect
BB l b r t `bbClampVect` Vect x y = Vect (fClamp x l r) (fClamp y b t)
bbWrapVect
:: BB
-> Vect
-> Vect
BB l b r t `bbWrapVect` Vect x y = Vect (l + ((x - l) `mod'` abs (r - l))) (b + ((y - b) `mod'` abs (t - b)))
foreign import ccall unsafe "Chiphunk/Low/BB.chs.h __c2hs_wrapped__w_cpBBSegmentQuery"
bbSegmentQuery'_ :: ((BBPtr) -> ((VectPtr) -> ((VectPtr) -> (IO C2HSImp.CDouble))))
foreign import ccall unsafe "Chiphunk/Low/BB.chs.h __c2hs_wrapped__w_cpBBIntersectsSegment"
bbIntersectsSegment'_ :: ((BBPtr) -> ((VectPtr) -> ((VectPtr) -> (IO C2HSImp.CUChar))))