module Data.Geometry.Ipe.WriteIpeGeometry( IsConvertableToIpeObject(..)
, toIpeObjects'
) where
import Data.Geometry.Point
import Data.Geometry.Geometry
import Data.Geometry.Ipe.InternalTypes
import Data.Geometry.Ipe.IpeGeometryTypes
import Data.Geometry.Ipe.IGC(IGC(IGC))
import Data.Maybe
import Data.List
import qualified Data.Map as M
toLinearPath :: [Point2' a] -> [Operation a]
toLinearPath [] = []
toLinearPath (p:pts) = MoveTo p : map LineTo pts
mkPolygonOps :: [Point2' a] -> [Operation a]
mkPolygonOps pts = toLinearPath pts ++ [ClosePath]
mkPath :: AMap -> [Operation a] -> Maybe (IpeObject a)
mkPath _ [] = Nothing
mkPath attrs ops = Just $ Path ops attrs
class IsConvertableToIpeObject g where
type PM g
toIpeObject :: g -> Maybe (IpeObject (PM g))
toIpeObjects :: [g] -> [IpeObject (PM g)]
toIpeObjects = mapMaybe toIpeObject
instance IsConvertableToIpeObject (IpePoint' a) where
type PM (IpePoint' a) = a
toIpeObject (IpePoint p attrs) = Just $ Use p attrs
instance IsConvertableToIpeObject (IpePolyline' a) where
type PM (IpePolyline' a) = a
toIpeObject (IpePolyline lines attrs) =
mkPath attrs . toLinearPath . concatMap points $ lines
instance IsConvertableToIpeObject (IpeSimplePolygon' a) where
type PM (IpeSimplePolygon' a) = a
toIpeObject (IpeSimplePolygon pts attrs) =
mkPath attrs . mkPolygonOps $ pts
instance IsConvertableToIpeObject (IpeMultiPolygon' a) where
type PM (IpeMultiPolygon' a) = a
toIpeObject (IpeMultiPolygon pls attrs) =
mkPath attrs . concatMap (mkPolygonOps . points) $ pls
instance IsConvertableToIpeObject (IGC a) where
type PM (IGC a) = a
toIpeObject gc = case obs of
[] -> Nothing
_ -> Just $ Group obs ats
where
obs = toIpeObjects' gc
ats = case obs of
[] -> M.empty
(o:_) -> attrs o
toIpeObjects' :: IGC a -> [IpeObject a]
toIpeObjects' (IGC _ pts pll sps mps) = concat [ toIpeObjects pts
, toIpeObjects pll
, toIpeObjects sps
, toIpeObjects mps ]