<scxml initial="Initialize" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camitk="http://camitk.imag.fr/3/asm" xsi:schemaLocation="http://camitk.imag.fr/3/smallScxml/../resources/smallScxml.xsd" xmlns="http://camitk.imag.fr/3/smallScxml">
 <state id="Initialize">
  <onentry>
   <camitk:onState>
    <camitk:description><![CDATA[This pipeline will process several actions on the input component(s): <br /> <ul><li>Open</li><li>Otsu Threshold Filter</li><li>Morphological Operators</li><li>Save As</li><li>Save As</li><li>Connected Components Classification</li><li>Save As</li><li>Manual Threshold Filter</li><li>Save As</li><li>Morphological Operators</li><li>Save As</li><li>Morphological Operators</li><li>Save As</li><li>Close</li><li>Close</li><li>Close</li><li>Close</li><li>Close</li><li>Close</li><li>Reconstruction</li><li>Save As</li><li>Save As</li><li>Close</li><li>Reconstruction</li><li>Save As</li><li>Close</li><li>Reconstruction</li><li>Save As</li><li>Save As</li><li>Clean Mesh</li><li>Save As</li>]]></camitk:description>
   </camitk:onState>
  </onentry>
  <transition target="Open Input Image" event="Next"/>
 </state>
 <state id="Open Input Image">
  <onentry>
   <camitk:onState>
    <camitk:description>Open data (component) from a file</camitk:description>
    <camitk:action>
     <camitk:name>Open File</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="File Name" value="input-1.mha" type="QString"/>
     </camitk:parameters>
     <camitk:outputs>
      <camitk:component name="input-1.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Initialize" event="Back"/>
  <transition target="Apply Otsu" event="Next"/>
 </state>
 <state id="Apply Otsu">
  <onentry>
   <camitk:onState>
    <camitk:description>&lt;p> This filter creates a binary thresholded image that separates an image into foreground and background components. &lt;br/>The filter calculates the optimum threshold separating those two classes so that their combined spread (intra-class variance) is minimal (see http://en.wikipedia.org/wiki/Otsu%27s_method).  Then the filter applies that threshold to the input image using an Itk binary fileter. The numberOfHistogram bins can be set for the Otsu Calculator. The insideValue and outsideValue can be set for the Itk binary filter.  The filter produces a labeled volume. &lt;br/>&lt;br/>The original reference is: &lt;br/>. &lt;i>Otsu, ''A threshold selection method from gray level histograms,'' IEEE Trans.Syst.ManCybern.SMC-9,62-66 1979.&lt;/i>&lt;/p></camitk:description>
    <camitk:action>
     <camitk:name>Otsu Threshold Filter</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Computed threshold:" value="46" type="double"/>
      <camitk:parameter name="Inside value" value="0" type="int"/>
      <camitk:parameter name="Outside value" value="255" type="int"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="input-1.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
         <camitk:component name="output-1.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Open Input Image" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="input-1.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Save Otsu Result as output-1.mha" event="Next"/>
 </state>
 <state id="Save Otsu Result as output-1.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-1.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Otsu" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-1.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Apply Erosion" event="Next"/>
 </state>
 <state id="Apply Erosion">
  <onentry>
   <camitk:onState>
    <camitk:description>&lt;br/>&lt;b>Mathematical morphology&lt;/b> has proved to be a powerful resource for image processing and analysis.&lt;br/>&lt;br/>This filter implements classical mathematical morphology operators:&lt;ul>&lt;li>erosion&lt;/li>&lt;li>dilation&lt;/li>&lt;li>opening&lt;/li>&lt;li>closure&lt;/li>&lt;/ul></camitk:description>
    <camitk:action>
     <camitk:name>Morphological Operators</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Structuring element size" value="3" type="int"/>
      <camitk:parameter name="Image scalar type" value="0" type="int"/>
      <camitk:parameter name="Type of operation" value="0" type="int"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="output-1.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-2.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Otsu Result as output-1.mha" event="Back"/>
  <transition target="Save Erosion Result as output-2.mha" event="Next"/>
</state>
 <state id="Save Erosion Result as output-2.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-2.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>    
   </camitk:onState>
  </onentry>
  <transition target="Apply Erosion" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-2.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>      
  <transition target="Apply Classification" event="Next"/>
</state>
 
 <state id="Apply Classification">
  <onentry>
   <camitk:onState>
    <camitk:description>	Labels connected components of a binary image and order the labes with respect to the size of the connected components (i.e. the larges connected component has the label 1, the one a little smaller has the label 2, and so on...  </camitk:description>
    <camitk:action>
     <camitk:name>Connected Components Classification</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-2.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-3.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Erosion Result as output-2.mha" event="Back"/>
  <transition target="Save Classification Result as output-3.mha" event="Next"/>
 </state>
 <state id="Save Classification Result as output-3.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-3.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Classification" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-3.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Apply Threshold" event="Next"/>
 </state>
 <state id="Apply Threshold">
  <onentry>
   <camitk:onState>
    <camitk:description>&lt;br/>Sets all the pixels / voxels scalar value according to the following rules:                   &lt;ul>                    &lt;li>low threshold &amp;lt; scalar value &amp;lt; high threshold &amp;gt; white (255) &lt;/li>                    &lt;li>scalar value &amp;lt; low threshold OR scalar value &amp;gt; high threshold &amp;gt; black (0)&lt;/li>                    &lt;/ul></camitk:description>
    <camitk:action>
     <camitk:name>Manual Threshold Filter</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Low threshold" value="1" type="int"/>
      <camitk:parameter name="High threshold" value="1" type="int"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="output-3.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-4.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Classification Result as output-3.mha" event="Back"/>
  <transition target="Save Threshold Result as output-4.mha" event="Next"/>
 </state>
 <state id="Save Threshold Result as output-4.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-4.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Threshold" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-4.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Apply Dilation" event="Next"/>
 </state>
 <state id="Apply Dilation">
  <onentry>
   <camitk:onState>
    <camitk:description>&lt;br/>&lt;b>Mathematical morphology&lt;/b> has proved to be a powerful resource for image processing and analysis.&lt;br/>&lt;br/>This filter implements classical mathematical morphology operators:&lt;ul>&lt;li>erosion&lt;/li>&lt;li>dilation&lt;/li>&lt;li>opening&lt;/li>&lt;li>closure&lt;/li>&lt;/ul></camitk:description>
    <camitk:action>
     <camitk:name>Morphological Operators</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Structuring element size" value="3" type="int"/>
      <camitk:parameter name="Image scalar type" value="0" type="int"/>
      <camitk:parameter name="Type of operation" value="1" type="int"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="output-4.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-5.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Threshold Result as output-4.mha" event="Back"/>
  <transition target="Save Dilation Result as output-5.mha" event="Next"/>
</state>
 <state id="Save Dilation Result as output-5.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-5.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Dilation" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-5.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Apply Closure" event="Next"/>
 </state>
 <state id="Apply Closure">
  <onentry>
   <camitk:onState>
    <camitk:description>&lt;br/>&lt;b>Mathematical morphology&lt;/b> has proved to be a powerful resource for image processing and analysis.&lt;br/>&lt;br/>This filter implements classical mathematical morphology operators:&lt;ul>&lt;li>erosion&lt;/li>&lt;li>dilation&lt;/li>&lt;li>opening&lt;/li>&lt;li>closure&lt;/li>&lt;/ul></camitk:description>
    <camitk:action>
     <camitk:name>Morphological Operators</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Structuring element size" value="5" type="int"/>
      <camitk:parameter name="Image scalar type" value="0" type="int"/>
      <camitk:parameter name="Type of operation" value="3" type="int"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="output-5.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-6.mha" type="ImageComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Dilation Result as output-5.mha" event="Back"/>
  <transition target="Save Closure Result as output-6.mha" event="Next"/>
 </state>
 <state id="Save Closure Result as output-6.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-6.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Closure" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-6.mha" type="ImageComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Close input-1.mha" event="Next"/>
 </state>
 <state id="Close input-1.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="input-1.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Closure Result as output-6.mha" event="Back"/>
  <transition target="Close output-1.mha" event="Next"/>
 </state>
 <state id="Close output-1.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-1.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Close input-1.mha" event="Back"/>
  <transition target="Close output-2.mha" event="Next"/>
 </state>
 <state id="Close output-2.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-2.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Close output-1.mha" event="Back"/>
  <transition target="Close output-3.mha" event="Next"/>
 </state>
 <state id="Close output-3.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-3.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Close output-2.mha" event="Back"/>
  <transition target="Close output-4.mha" event="Next"/>
 </state>
 <state id="Close output-4.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-4.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Close output-3.mha" event="Back"/>
  <transition target="Close output-5.mha" event="Next"/>
 </state>
 <state id="Close output-5.mha">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-5.mha" type="ImageComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Close output-4.mha" event="Back"/>
  <transition target="Apply Marching Cube" event="Next"/>
 </state>
 <state id="Apply Marching Cube">
  <onentry>
   <camitk:onState>
    <camitk:description>&lt;br /> Marching cubes is a simple algorithm for creating a triangle mesh from an implicit function (one of the form f(x, y, z) = 0).&lt;br /> It works by iterating ("marching") over a uniform grid of cubes superimposed over a region of the function. If all 8 vertices of the cube are positive, or all 8 vertices are negative, the cube is entirely above or entirely below the surface and no triangles are emitted. Otherwise, the cube straddles the function and some triangles and vertices are generated. Since each vertex can either be positive or negative, there are technically 28 possible configurations, but many of these are equivalent to one another. &lt;br/> &lt;br/>This action uses this algorithm to build a 3D surfacic mesh of the input image.</camitk:description>
    <camitk:action>
     <camitk:name>Reconstruction</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Threshold" value="127.5" type="double"/>
      <camitk:parameter name="Keep only largest component?" value="true" type="bool"/>
      <camitk:parameter name="Subsample original image?" value="true" type="bool"/>
      <camitk:parameter name="Subsampled image width (X)" value="16" type="int"/>
      <camitk:parameter name="Subsampled image height (Y)" value="16" type="int"/>
      <camitk:parameter name="Subsampled image depth (Z)" value="16" type="int"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="output-6.mha" type="ImageComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-7.vtk" type="MeshComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Close output-5.mha" event="Back"/>
  <transition target="Save Marching Cube Result as output-7.vtk" event="Next"/>
 </state>
 <state id="Save Marching Cube Result as output-7.vtk">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-7.vtk" type="MeshComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Marching Cube" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-7.vtk" type="MeshComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Apply Clean" event="Next"/>
 </state>

 <state id="Apply Clean">
  <onentry>
   <camitk:onState>
    <camitk:description>Merge duplicate points, and/or remove unused points and/or remove degenerate cells.</camitk:description>
    <camitk:action>
     <camitk:name>Clean Mesh</camitk:name>
     <camitk:parameters>
      <camitk:parameter name="Tolerance" value="1" type="double"/>
     </camitk:parameters>
     <camitk:inputs>
      <camitk:component name="output-7.vtk" type="MeshComponent"/>
     </camitk:inputs>
     <camitk:outputs>
      <camitk:component name="output-8.vtk" type="MeshComponent"/>
     </camitk:outputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Marching Cube Result as output-7.vtk" event="Back"/>
  <transition target="Save Clean Result as output-8.vtk" event="Next"/>
 </state>
 <state id="Save Clean Result as output-8.vtk">
  <onentry>
   <camitk:onState>
    <camitk:description>Save the currently selected data under a different filename or format</camitk:description>
    <camitk:action>
     <camitk:name>Save</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-8.vtk" type="MeshComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Apply Clean" event="Back">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-8.vtk" type="MeshComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
  <transition target="Close output-7.vtk" event="Next"/>
 </state>
 <state id="Close output-7.vtk">
  <onentry>
   <camitk:onState>
    <camitk:description>Close the currently selected components</camitk:description>
    <camitk:action>
     <camitk:name>Close</camitk:name>
     <camitk:parameters/>
     <camitk:inputs>
      <camitk:component name="output-7.vtk" type="MeshComponent"/>
     </camitk:inputs>
    </camitk:action>
   </camitk:onState>
  </onentry>
  <transition target="Save Clean Result as output-8.vtk" event="Back"/>
  <transition target="Bye" event="Next"/>
 </state>
 
 <!-- 
 At this stage :
      output-6.mha and output-8.vtk are not closed.
      as they were save, they should not be marked as "Modified" and the application should just close 
      without further ado or any kind of seg fault/exception...
 -->
 
 <state id="Bye" final="true">
  <onentry>
   <camitk:onState>
    <camitk:description><![CDATA[Thanks you for using the CamiTK Action State Machine !]]></camitk:description>
   </camitk:onState>
  </onentry>
  <transition target="Initialize" event="Back to the beginning">
   <onTransition>
    <camitk:close>
     <camitk:component name="output-5.mha" type="ImageComponent"/> <!-- already closed, but should be ok -->
     <camitk:component name="output-6.mha" type="ImageComponent"/>
     <camitk:component name="output-7.vtk" type="MeshComponent"/> <!-- already closed, but should be ok -->
     <camitk:component name="output-8.vtk" type="MeshComponent"/>
    </camitk:close>
   </onTransition>
  </transition>
 </state>
</scxml>
