module Algebra.Geometric.Strip (
Strip (StripC, stripSet),
safePolygonToStrip, polygonToStrip)
where
import Data.Set hiding (map)
import Control.Monad
import Foreign
import Foreign.C
import Algebra.Geometric.Contour
import Algebra.Geometric.Polygon
type CContour = Ptr (Contour)
type CPolygon = Ptr (Polygon)
type CStrip = Ptr (Strip)
newtype Strip = StripC {stripSet :: Set Contour} deriving Show
instance Storable Strip where
sizeOf _ = 8
alignment _ = alignment (undefined :: Int)
peek cStrip =
(StripC . fromList) `liftM`
do
numStrips <-
fromIntegral `liftM` (\ptr -> do {peekByteOff ptr 0 ::IO CInt}) cStrip
(\ptr -> do {peekByteOff ptr 4 ::IO (CContour)}) cStrip >>= peekArray numStrips
poke cStrip (StripC strip) =
(\ptr val -> do {pokeByteOff ptr 0 (val::CInt)}) cStrip (fromIntegral $ size strip) >>
newArray (toList strip) >>= (\ptr val -> do {pokeByteOff ptr 4 (val::(CContour))}) cStrip
safePolygonToStrip :: Polygon -> IO Strip
safePolygonToStrip polygon =
alloca $
\cStrip -> alloca $
\cPolygon ->
do
poke cPolygon polygon
gpc_polygon_to_tristrip cPolygon cStrip
result <- peek cStrip
gpc_free_polygon cPolygon
gpc_free_tristrip cStrip
return result
polygonToStrip :: Polygon -> Strip
polygonToStrip = unsafePerformIO . safePolygonToStrip
foreign import ccall unsafe "Algebra/Geometric/Strip.chs.h gpc_polygon_to_tristrip"
gpc_polygon_to_tristrip :: ((CPolygon) -> ((CStrip) -> (IO ())))
foreign import ccall unsafe "Algebra/Geometric/Strip.chs.h gpc_free_polygon"
gpc_free_polygon :: ((CPolygon) -> (IO ()))
foreign import ccall unsafe "Algebra/Geometric/Strip.chs.h gpc_free_tristrip"
gpc_free_tristrip :: ((CStrip) -> (IO ()))