#
# HELP: SYNOPSIS:
# HELP:     sonoctux/user/UwFastPolys
# HELP:
# HELP: DESCRIPTION:
# HELP:     Prepare an acquired volume for display, including removal of
# HELP:     contrast agent, denoising and autodetection of polyps.
#

! generic/menu/confirm_longrun
IF ${!$result} QUIT

# Check if already processed
VOL_ALLOC_DEPTH
VOL_ALLOC_CLASS
{ CSR ${$root.rc==-1}
	! sonoctux/helper/ComplainScriptRerun
	QUIT
} CSR 0 0

# Normalize the Volume first.
! sonoctux/helper/NormalizeVolume

# Shut down noisy progress bars.
WAITBARTITLE 0 100 FastPolys

VIEW_SET Main
VOL_ALLOC_DEPTH
VOL_ALLOC_CLASS

! sonoctux/helper/FindBody

! sonoctux/helper/Filtering

# ok - denoising is completed. We will now try to steepen the surface
# between air and contrast agent. This is only needed, if you want to
# include the contrast agent in the polyp detection. 
# However this doesn't play well with polyp detection.
# FILTSEP_STEEPEN 1 3 ${$root.func.eccet.fromHU(-750)} ${$root.func.eccet.fromHU(156)}

# Now mark the air.
MARKCG 0 ${$root.func.eccet.fromHU(-750)} 0 3

# enable this to also mark the contrast agent. 
# This doesn't play too well with the polyp detection, as the border
# will look like lots of polyps.
# Thus automatic polyp detection will only run in the air filled space.
# mark ${$root.func.eccet.fromHU(156)} 65535 0 3

MARKCC_SIZE 1000 3 0 2
MARKCG 65535 65535 0 4
MARKCC_SIZE 10 0 2 3
MARKCC 3,4 0
MARKCC_KILL_FACE z e 2 3
MARKCC_DILATE 1 2,3 32,36
norm2 32,36 2,3 2 3
! sonoctux/helper/FindPolyps
VIEW_DISP 0
#
# O.K. - we are ready - now convert to colotux format
#
! sonoctux/helper/SaveC2D

! sonoctux/helper/MergeAndFilter

# Now we re-harden to do away with the surface
FILTSEP_STEEPEN 1 3 ${$root.func.eccet.fromHU(-750)} ${$root.func.eccet.fromHU(156)}

# Push the greyscale stuff into the classes
# Erase the target field
WAITBARTITLE 0 20 Generating 3D data  
GREYTOCLASS 0 65535 0 0
# Let the air/tissue level flip at around -750 HU
WAITBARTITLE 20 40 Generating 3D data
GREYTOCLASS ${$root.func.eccet.fromHU(-1000)} ${$root.func.eccet.fromHU(-500)} 0 255
# Everything up to 156 (Threshold for contrast agent) - 250 is solid.
WAITBARTITLE 40 60 Generating 3D data
GREYTOCLASS ${$root.func.eccet.fromHU(-500)} ${$root.func.eccet.fromHU(-94)} 255 255
# Let the contrast agent level flip at 156
WAITBARTITLE 60 80 Generating 3D data
GREYTOCLASS ${$root.func.eccet.fromHU(-94)} ${$root.func.eccet.fromHU(406)} 255 0
# Everything denser is also transparent.
WAITBARTITLE 80 100 Generating 3D data
GREYTOCLASS ${$root.func.eccet.fromHU(406)} 65535 0 0
# Re-enable noisy progress bars.
WAITBARTITLE -1

! sonoctux/helper/MergeOriginal
! sonoctux/helper/PolyRGtoRYG

! sonoctux/helper/Prepare3dClass
