{-- TerraHS - Interface between TerraLib and Haskell

    (c) Sergio Costa (INPE) - Setembro, 2005

	This program is free software; you can redistribute it 
    and/or modify it under the terms of the GNU General 
    Public License 2.1 as published by the Free Software Foundation
    (http://www.opensource.org/licenses/gpl-license.php)
--}

{--  --}

module Algebras.Base.Operations 
	(
		Set (..) , TopologyOps (..), Relations (..), centroid,  distance , llength , area 
	)
	 where

import System.IO.Unsafe 


import Foreign
import Foreign.C.String
import qualified Foreign.Ptr (Ptr)

import TerraHS.TerraLib.TePoint
import TerraHS.TerraLib.TeLine2D
import TerraHS.TerraLib.TePolygon
import TerraHS.TerraLib.TeGeometry
import TerraHS.TerraLib.TeCell
import TerraHS.TerraLib.TeGeometryAlgorithms
import TerraHS.TerraLib.TeOverlay

import TerraHS.TerraLib.TeTopologyOps


import TerraHS.Misc.Object
import TerraHS.Misc.Generic

import Algebras.Base.Points
import Algebras.Base.Lines
import Algebras.Base.Polygons 
import Algebras.Base.Geometries


------------------------------------	
--- topologics operations ----------
------------------------------------	

class (Topology a b) =>	 TopologyOps a b where
	intersects, contains, coveredby, overlaps, equals, within, disjoint, crosses, touches  :: a -> b -> Bool
	containedBy  :: b -> a -> Bool

	equals g1 g2  =  teequals g1 g2
	within g1 g2  = tewithin g1 g2 
	disjoint g1 g2  = tedisjoint g1 g2
	touches g1 g2 =   tetouches g1 g2
	overlaps g1 g2 =  teoverlaps g1 g2
	coveredby g1 g2 = tecoveredby g1 g2
	crosses g1 g2 =  tecrosses g1 g2
	contains g1 g2 = tecontains g1 g2


-- instance to several combination of types
instance TopologyOps TePoint TePoint where
instance TopologyOps TeLine2D TeLine2D where
instance TopologyOps TePolygon TePolygon where
instance TopologyOps TeCell TeCell where
instance TopologyOps TePoint TePolygon where	
instance TopologyOps TeLine2D TePolygon where	
instance TopologyOps TePoint TeLine2D where	
instance TopologyOps TeLine2D TeCell where	
instance TopologyOps TePoint TeCell where	
instance TopologyOps TePolygon TeCell where	
instance TopologyOps TeCell TePolygon where	
instance TopologyOps TeCell TeLine2D where	
instance TopologyOps TeCell TePoint where	
instance TopologyOps TeGeometry TeGeometry where	


class (TeRelations a b) => Relations a b where
	relation :: a -> b -> TeSpatialRelation
	relation g1 g2 = terelation g1 g2
	
instance Relations TePolygon TePolygon where
	
instance Relations TePoint TePolygon where
	
instance Relations TeLine2D TePolygon where

instance Relations TePoint TeLine2D where
	

------------------------------------	
--- set operations -------------
------------------------------------	
	
class Set a where
	union			:: [a] -> [a] -> [a]
	intersection 		:: [a] -> [a] -> [a]
	difference 		:: [a] -> [a] -> [a]
	
	
instance Set TePolygon where

	union p1 p2 = map toPolygon geos
		where
		geos =  teunion (TePolygonSet p1) (TePolygonSet p2)
		
	intersection a b = map toPolygon geos
		where
		geos = teintersection (TePolygonSet a) (TePolygonSet b)
		
	difference a b = map toPolygon geos
		where
		geos = tedifference (TePolygonSet a) (TePolygonSet b)
	
	
------------------------------------	
--- metrics operations -------------
------------------------------------	


-- distancia entre dois pontos	
distance :: TePoint -> TePoint -> Double
distance pt1 pt2 = tedistance pt1 pt2

-- | Returns the length of a Line 2D.
llength :: TeLine2D -> Double
llength l = telength l

-- | Returns the area of a TePolygon 
area :: TePolygon ->  Double
area p = tearea p



------------------------------------	
--- centroid operations ------------
------------------------------------

class (TeCentroid a) => Centroid a where
	centroid :: a -> TePoint
	centroid g = tecentroid g
	
instance Centroid TePointSet where

instance Centroid TeLineSet where

instance Centroid TePolygonSet where

instance Centroid TePolygon where