{-- 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)
--}

{- | A module for supporting the functions of TerraLib TeGeometryAlgorithms

More information - <http://www.terralib.org>
-}
module TerraHS.TerraLib.TeTopologyOps {--
	(
		 distance, linelength, area, Centroid (..), terelation, Egenhofer9 (..), 
		TeRelations (..), TeSpatialRelation (..), BinaryPredicates(..)
	) --}
	 where


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

--locais
import TerraHS.Algebras.Base.Object
import TerraHS.Misc.GenericFunctions
import TerraHS.TerraLib.TeGeometry
import TerraHS.TerraLib.TePoint
import TerraHS.TerraLib.TeLine2D
import TerraHS.TerraLib.TeCell
import TerraHS.TerraLib.TeBox
import TerraHS.TerraLib.TePolygon

import TerraHS.Algebras.Base.Operations



-- semanticamente
toTE tx = TerraHS.Algebras.Base.Object.new tx

-- | Operators that test topologival relation between two objects.

	

instance BinaryPredicates TeGeometry TeGeometry where
	
	--------------------------------------
	-- teequals ------------------------------
	--------------------------------------
	-- TePoint -> TePoint --------
	equals (GPg g1) (GPg g2)  = equals g1 g2

	-- TeLine2D -> TeLine2D --------		
	equals (GCl g1) (GCl g2)  = equals g1 g2

	-- TePolygon -> TePolygon --------				
	equals (GPg g1) (GPg g2)  = equals g1 g2

	-- TeCell -> TeCell --------				
	equals (GCl g1) (GCl g2)  = equals g1 g2

	--------------------------------------
	-- tewithin ----------------------------
	--------------------------------------

	-- TePoint -> TeLine2D 
	within (GPt g1) (GLn g2)  = within g1 g2

	-- TePoint -> TePoint 
	within (GPt g1) (GPt g2)  = within g1 g2

	-- TePoint -> TePolygon
	within (GPt g1) (GPg g2)  = within g1 g2
	
	-- TePoint -> TePolygon
	within (GPt g1) (GPg g2)  = within g1 g2

	-- TeLine2D -> TeLine2D
	within (GLn g1) (GLn g2)  = within g1 g2

	-- TeLine2D -> TePolygon
	within (GLn g1) (GPg g2)  = within g1 g2

	-- TeLine2D -> TePolygon
	within (GLn g1) (GPg g2)  = within g1 g2

	-- TeCell -> TeCell
	within (GCl g1) (GCl g2)  = within g1 g2
		
	-- TeLIne2D -> TeCell
	within (GLn g1) (GCl g2)  = within g1 g2

	-- TeCell -> TePolygon
	within (GCl g1) (GPg g2)  = within g1 g2
	
	within (GPg g1) (GPg g2)  = within g1 g2

	-- TePoint -> TeCell
	within (GPt g1) (GCl g2)  = within g1 g2
	
	--------------------------------------
	-- tedisjoint ----------------------------
	--------------------------------------

	disjoint (GPg g1) (GPg g2) = disjoint g1 g2
		
	disjoint (GLn g1) (GPg g2) = disjoint g1 g2

	disjoint (GPt g1) (GPt g2)  = disjoint g1 g2

	disjoint (GLn g1) (GLn g2)  = disjoint g1 g2

	disjoint (GPt g1) (GLn g2)  = disjoint g1 g2

	disjoint (GPt g1) (GPg g2)  = disjoint g1 g2

	disjoint (GCl g1) (GCl g2)  = disjoint g1 g2

	disjoint (GCl g1) (GLn g2)  = disjoint g1 g2
				
	disjoint (GCl g1) (GPg g2)  = disjoint g1 g2
		
	disjoint (GCl g1) (GPt g2)  = disjoint g1 g2

	--------------------------------------
	-- tecrosses ---------------------------
	--------------------------------------

	crosses (GLn g1) (GPg g2) = crosses g1 g2

	crosses (GLn g1) (GLn g2) = crosses g1 g2
		
	crosses (GLn g1) (GCl g2)  = crosses g1 g2
		
	--------------------------------------
	-- tetouches ---------------------------
	--------------------------------------
		
	touches (GLn g1) (GLn g2) = touches g1 g2
		
	touches (GPt g1) (GLn g2) = touches g1 g2

	touches (GPt g1) (GPg g2)  = touches g1 g2
		
	touches (GLn g1) (GPg g2)  = touches g1 g2

	touches (GPg g1) (GPg g2)  = touches g1 g2
		
	touches (GCl g1) (GCl g2)  = touches g1 g2

	touches (GLn g1) (GCl g2)  = touches g1 g2

	touches (GCl g1) (GPg g2)  = touches g1 g2

	touches (GPt g1) (GCl g2)  = touches g1 g2

	--------------------------------------
	-- teoverlaps ---------------------------
	--------------------------------------
	overlaps (GLn g1) (GLn g2)  = overlaps g1 g2

	overlaps (GPg g1) (GPg g2)  = overlaps g1 g2

	overlaps (GCl g1) (GCl g2)  = overlaps g1 g2

	overlaps (GCl g1) (GPg g2)  = overlaps g1 g2
						
	--------------------------------------
	-- tecoveredby ---------------------------
	--------------------------------------
	coveredby (GLn g1) (GLn g2)  = coveredby g1 g2

	coveredby (GPg g1) (GPg g2)  = coveredby g1 g2

	coveredby (GCl g1) (GCl g2)  = coveredby g1 g2
		
	coveredby (GPg g1) (GCl g2)  = coveredby g1 g2

	coveredby (GLn g1) (GPg g2)  = coveredby g1 g2
				
	coveredby (GLn g1) (GCl g2)  = coveredby g1 g2


instance BinaryPredicates TeBox TeBox where
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE  boxbox_tewithin)
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE  boxbox_tedisjoint)
	intersects g1 g2  = (not (disjoint g1 g2))
	
instance BinaryPredicates TePoint TePoint where
	equals g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE tepoint_teequals) 
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE  tepoint_tewithin)
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE tepoint_tedisjoint)
	
	
instance BinaryPredicates TeLine2D TeLine2D where
	equals g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE teline2d_teequals) 
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE  teline2d_tewithin)
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE  teline2d_tedisjoint)
	crosses g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE teline2d_tecrosses)
	
	
instance BinaryPredicates TePolygon TePolygon where
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE tepolygon_tewithin)	 --((terelation g1 g2) == TeWITHIN) 
	equals g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE tepolygon_teequals)	 --((terelation g1 g2) == TeWITHIN) 
	disjoint g1 g2  =  ((relation g1 g2) == TeDISJOINT) 
	touches g1 g2  =  ((relation g1 g2) == TeTOUCHES) 
	intersects g1 g2  = (not (disjoint g1 g2))
	overlaps g1 g2  =  ((relation g1 g2) == TeOVERLAPS) 
	coveredby g1 g2  =  ((relation g1 g2) == TeCOVEREDBY) 
	

f xs ys r = [r x y| x<-xs,y <-ys]
 	
instance BinaryPredicates [TePolygon] [TePolygon] where
	within g1 g2  =  or (f g1 g2 within)
	equals g1 g2  = or (f g1 g2 equals)
	disjoint g1 g2  =  or (f g1 g2 within)
	touches g1 g2  =  or (f g1 g2 disjoint)
	intersects g1 g2  = or (f g1 g2 intersects)
	overlaps g1 g2  =  or (f g1 g2 overlaps)
	coveredby g1 g2  =  or (f g1 g2 coveredby)

instance BinaryPredicates TeCell TeCell where
	equals g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE tecell_teequals) 
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE tecell_tewithin)
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE  tecell_tedisjoint)
	touches g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  tecell_tetouches)
	overlaps g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  tecell_teoverlaps)
	coveredby g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  tecell_tecoveredby)


instance BinaryPredicates TePoint TePolygon where	
	within g1 g2  =  	 ((relation g1 g2) == TeWITHIN) 
	disjoint g1 g2  =  ((relation g1 g2) == TeDISJOINT) 
	touches g1 g2  =  ((relation g1 g2) == TeTOUCHES) 
	overlaps  g1 g2  =  ((relation g1 g2) == TeOVERLAPS) 
	coveredby g1 g2  =  ((relation g1 g2) == TeCOVEREDBY) 

instance BinaryPredicates TeLine2D TePolygon where	
	within g1 g2  =  	 ((relation g1 g2) == TeWITHIN) 
	disjoint g1 g2  =  ((relation g1 g2) == TeDISJOINT) 
	touches g1 g2  =  ((relation g1 g2) == TeTOUCHES) 
	overlaps g1 g2  =  ((relation g1 g2) == TeOVERLAPS) 
	coveredby g1 g2  =  ((relation g1 g2) == TeCOVEREDBY) 
	
instance BinaryPredicates TePoint TeLine2D where	
	within g1 g2  =  	 ((relation g1 g2) == TeWITHIN) 
	disjoint g1 g2  =  ((relation g1 g2) == TeDISJOINT) 
	touches g1 g2  =  ((relation g1 g2) == TeTOUCHES) 
	overlaps g1 g2  =  ((relation g1 g2) == TeOVERLAPS) 
	coveredby g1 g2  =  ((relation g1 g2) == TeCOVEREDBY) 
		
instance BinaryPredicates TeLine2D TeCell where	
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE  linecell_tewithin)
	touches g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  linecell_tetouches)
	coveredby g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  linecell_tecoveredby)
	crosses g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  linecell_tecrosses)
	
instance BinaryPredicates TePoint TeCell where	
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE  pointcell_tewithin)
	touches g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  pointcell_tetouches)

instance BinaryPredicates TePolygon TeCell where	
	coveredby g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  polcell_tecoveredby)

instance BinaryPredicates TeCell TePolygon where	
	within g1 g2  =  unsafePerformIO (applyf g1 toTE g2 toTE  cellpol_tewithin)
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE  cellpol_tedisjoint)
	touches g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  cellpol_tetouches)
	overlaps g1 g2 = unsafePerformIO (applyf g1 toTE g2 toTE  cellpol_teoverlaps)
	

instance BinaryPredicates TeCell TeLine2D where	
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE  cellline_tedisjoint)

instance BinaryPredicates TeCell TePoint where	
	disjoint g1 g2  = unsafePerformIO (applyf g1 toTE g2 toTE  cellpoint_tedisjoint)


	
---- relation

data TeSpatialRelation = TeDISJOINT | TeTOUCHES | TeCROSSES | TeWITHIN | 
				TeOVERLAPS | TeCONTAINS | TeINTERSECTS | TeEQUALS | TeCOVERS | TeCOVEREDBY | TeUNDEFINEDREL deriving (Show, Eq, Ord)	


class TeRelations a b where
	relation :: a -> b -> TeSpatialRelation

instance TeRelations TePolygon TePolygon where
	relation g1 g2 =  ( unsafePerformIO (applyf g1 toTE g2 toTE polpol_terelation >>= \r -> return (returnTeRelation r )))
	
instance TeRelations TePoint TePolygon where
	relation g1 g2 =  ( unsafePerformIO (applyf g1 toTE g2 toTE pointpol_terelation >>= \r -> return (returnTeRelation r )))
	
instance TeRelations TeLine2D TePolygon where
	relation g1 g2 =  ( unsafePerformIO (applyf g1 toTE g2 toTE linepol_terelation >>= \r -> return (returnTeRelation r )))

instance TeRelations TePoint TeLine2D where
	relation g1 g2 =  ( unsafePerformIO (applyf g1 toTE g2 toTE pointline_terelation >>= \r -> return (returnTeRelation r )))

instance TeRelations TeGeometry TeGeometry where
	relation (GPg g1) (GPg g2) =  ( unsafePerformIO (applyf g1 toTE g2 toTE polpol_terelation >>= \r -> return (returnTeRelation r )))

	relation (GPt g1) (GPg g2) =  ( unsafePerformIO (applyf g1 toTE g2 toTE pointpol_terelation >>= \r -> return (returnTeRelation r )))
	
	relation (GLn g1) (GPg g2) =  ( unsafePerformIO (applyf g1 toTE g2 toTE linepol_terelation >>= \r -> return (returnTeRelation r )))

	relation (GPt g1) (GLn g2) =  ( unsafePerformIO (applyf g1 toTE g2 toTE pointline_terelation >>= \r -> return (returnTeRelation r )))
	
	relation _ _ = error "topoly: relation is not applicable"
	

returnTeRelation :: Int32 -> TeSpatialRelation
returnTeRelation r
	|r == 1 =TeDISJOINT 
	|r == 2 =TeTOUCHES 
	|r == 4 =TeCROSSES 
	|r == 8 =TeWITHIN 
	|r == 16 =TeOVERLAPS 
	|r == 32 =TeCONTAINS 
	|r == 64 =TeINTERSECTS 
	|r == 128 =TeEQUALS 
	|r == 256 =TeCOVERS 
	|r == 512 =TeCOVEREDBY 
	| otherwise = TeUNDEFINEDREL 

		
-- teequals -------------------------
foreign import stdcall unsafe "c_tecell_teequals" tecell_teequals :: TeCellPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepolygon_teequals" tepolygon_teequals :: TePolygonPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepoint_teequals" tepoint_teequals :: TePointPtr -> TePointPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_teline2d_teequals" teline2d_teequals :: TeLine2DPtr -> TeLine2DPtr ->  Prelude.IO Bool

--- tecrosses ------------------------
foreign import stdcall unsafe "c_teline2d_tecrosses" teline2d_tecrosses :: TeLine2DPtr -> TeLine2DPtr -> Prelude.IO Bool
foreign import stdcall unsafe "c_linepol_tecrosses" linepol_tecrosses :: TeLine2DPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linecell_tecrosses" linecell_tecrosses :: TeLine2DPtr -> TeCellPtr ->  Prelude.IO Bool

--- tedisjoint  -----------------------
foreign import stdcall unsafe "c_tepolygon_tedisjoint" tepolygon_tedisjoint :: TePolygonPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linepol_tedisjoint" linepol_tedisjoint :: TeLine2DPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepoint_tedisjoint" tepoint_tedisjoint :: TePointPtr -> TePointPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_teline2d_tedisjoint" teline2d_tedisjoint :: TeLine2DPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_pointline_tedisjoint" pointline_tedisjoint :: TePointPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_pointpol_tedisjoint" pointpol_tedisjoint :: TePointPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tecell_tedisjoint" tecell_tedisjoint :: TeCellPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_cellline_tedisjoint" cellline_tedisjoint :: TeCellPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_cellpol_tedisjoint" cellpol_tedisjoint :: TeCellPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_cellpoint_tedisjoint" cellpoint_tedisjoint :: TeCellPtr -> TePointPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_boxbox_tedisjoint" boxbox_tedisjoint :: TeBoxPtr -> TeBoxPtr ->  Prelude.IO Bool
--- tetouches -----------
foreign import stdcall unsafe "c_teline2d_tetouches" teline2d_tetouches :: TeLine2DPtr -> TeLine2DPtr -> Prelude.IO Bool
foreign import stdcall unsafe "c_linepoint_tetouches" linepoint_tetouches :: TePointPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_pointpol_tetouches" pointpol_tetouches :: TePointPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linepol_tetouches" linepol_tetouches :: TeLine2DPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepolygon_tetouches" tepolygon_tetouches :: TePolygonPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tecell_tetouches" tecell_tetouches :: TeCellPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linecell_tetouches" linecell_tetouches :: TeLine2DPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_cellpol_tetouches" cellpol_tetouches :: TeCellPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_pointcell_tetouches" pointcell_tetouches :: TePointPtr -> TeCellPtr ->  Prelude.IO Bool

-- tewithin ----------
foreign import stdcall unsafe "c_linepoint_tewithin" linepoint_tewithin :: TePointPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepoint_tewithin" tepoint_tewithin :: TePointPtr -> TePointPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_pointpol_tewithin" pointpol_tewithin :: TePointPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_teline2d_tewithin" teline2d_tewithin :: TeLine2DPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linepol_tewithin" linepol_tewithin :: TeLine2DPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tecell_tewithin" tecell_tewithin :: TeCellPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linecell_tewithin" linecell_tewithin :: TeLine2DPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_cellpol_tewithin" cellpol_tewithin :: TeCellPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_pointcell_tewithin" pointcell_tewithin :: TePointPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepolygon_tewithin" tepolygon_tewithin :: TePolygonPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_boxbox_tewithin" boxbox_tewithin :: TeBoxPtr -> TeBoxPtr ->  Prelude.IO Bool



--- teoverlaps
foreign import stdcall unsafe "c_teline2d_teoverlaps" teline2d_teoverlaps :: TeLine2DPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepolygon_teoverlaps" tepolygon_teoverlaps :: TePolygonPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tecell_teoverlaps" tecell_teoverlaps :: TeCellPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_cellpol_teoverlaps" cellpol_teoverlaps :: TeCellPtr -> TePolygonPtr ->  Prelude.IO Bool

--- tecoveredby
foreign import stdcall unsafe "c_teline2d_tecoveredby" teline2d_tecoveredby :: TeLine2DPtr -> TeLine2DPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tepolygon_tecoveredby" tepolygon_tecoveredby :: TePolygonPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_tecell_tecoveredby" tecell_tecoveredby :: TeCellPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_polcell_tecoveredby" polcell_tecoveredby :: TePolygonPtr -> TeCellPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linepol_tecoveredby" linepol_tecoveredby :: TeLine2DPtr -> TePolygonPtr ->  Prelude.IO Bool
foreign import stdcall unsafe "c_linecell_tecoveredby" linecell_tecoveredby :: TeLine2DPtr -> TeCellPtr ->  Prelude.IO Bool

-- Relations
foreign import stdcall unsafe "c_pointline_terelation" pointline_terelation :: TePointPtr -> TeLine2DPtr ->  Prelude.IO Int32
foreign import stdcall unsafe "c_polpol_terelation" polpol_terelation :: TePolygonPtr -> TePolygonPtr -> Prelude.IO Int32
foreign import stdcall unsafe "c_pointpol_terelation" pointpol_terelation :: TePointPtr -> TePolygonPtr ->  Prelude.IO Int32
foreign import stdcall unsafe "c_linepol_terelation" linepol_terelation :: TeLine2DPtr -> TePolygonPtr ->  Prelude.IO Int32