#
# NEED_PARMS maxangle maxwidth minwidth
#

DECLARE int $x
DECLARE int $y
DECLARE int $z
DECLARE int $x1
DECLARE int $y1
DECLARE int $z1
DECLARE int $ctr
DECLARE int $fd
DECLARE int $fd2

DECLARE int $width
DECLARE int $angle
DECLARE int $minabst

DECLARE string $fileinp
DECLARE string $lastfileinp

CECHO ${$width=10000} ${$angle=10} ${$minabst=27}

IF ${(int)$argv[1]!=0} CECHO ${$angle=(int)$argv[1]}
IF ${(int)$argv[2]!=0} CECHO ${$width=(int)$argv[2]}
IF ${(int)$argv[3]!=0} CECHO ${$minabst=(int)$argv[3]}

# First, find a nice parcellation of the surface.

FINDVOXEL i

CECHO ${$fd=$root.func.file.open("eccet.tmp.patchmidpoints","wct")}
IF ${ $fd==-1 } QUIT

CECHO ${$ctr=0}
{ LOOP1 1

	FINDVOXEL c 32
	CECHO ${$x=$root.rc.x} ${$y=$root.rc.y} ${$z=$root.rc.z}
	IF ${$x==-1} BREAK LOOP1
	MARKCG_FILL_BOUND_NORM ${$x} ${$y} ${$z} ${$width} ${$angle} 32 241 32 ${$minabst} 1

	# fill that patch, so we don't find more dots here.
	FIND_COG ${$x} ${$y} ${$z} 243 241
	FIND_CLOSEST ${$x} ${$y} ${$z} ${$root.rc.x} ${$root.rc.y} ${$root.rc.z} 244 243
	CECHO ${$x=$root.rc.x} ${$y=$root.rc.y} ${$z=$root.rc.z}
	# take the point nearest to the COG to 246.
	CECHO ${$root.func.file.puts($fd,(string)$ctr+": "+(string)$x+" "+(string)$y+" "+(string)$z+" "+(string)$root.func.eccet.getvoxelnorm($x,$y,$z)+"\n")}
	CECHO ${$root.func.eccet.setvoxelclass($x,$y,$z,246)}
	CECHO ${$root.func.eccet.setvoxelnorm($x,$y,$z,($ctr&0xffff))}
	CECHO ${$root.func.eccet.setvoxeldepth($x,$y,$z,($ctr>>16))}
	CECHO ${$ctr+=1}

	IF ${($ctr&15)==0} VIEW_RENDER

} LOOP1 1 1

VIEW_RENDER

CECHO ${$root.func.file.close($fd)}

# Now we have small dots of class 246 marking patches of 244 and still
# the borders in 242
#
# Now extend the small dots with their DG markings

MARKCC_FLOOD_DG 10000 246 242,244

# We should now have Everything in DG marked patches at 246

# Now find the neighbouring patches.

CECHO ${$fd=$root.func.file.open("eccet.tmp.patchneighbours","wct")}
IF ${ $fd==-1 } QUIT

FIND_DG_NEIGHBOURS 246 ${$fd}

CECHO ${$root.func.file.close($fd)}

# Cosmetic only - show the chosen vertices

markcc 246 241

#CECHO ${$fd=$root.func.file.open("eccet.tmp.patchmidpoints","r")}
#IF ${ $fd==-1 } QUIT
#
#{ revert 1
#	CECHO ${$fileinp=$root.func.file.gets($fd)}
#	if ${$fileinp==""} BREAK revert
#	CECHO ${$fileinp=$root.func.strings.rightof($fileinp,": ")}
#	CECHO ${$x=(int)$fileinp}
#	CECHO ${$fileinp=$root.func.strings.rightof($fileinp," ")}
#	CECHO ${$y=(int)$fileinp}
#	CECHO ${$fileinp=$root.func.strings.rightof($fileinp," ")}
#	CECHO ${$z=(int)$fileinp}
#	ECHO ${$x} ${$y} ${$z}
#	CECHO ${$root.func.eccet.setvoxelclass($x,$y,$z,242)}
#} revert 1 1
#
#CECHO ${$root.func.file.close($fd)}
#
