]> Creatis software - creaVtk.git/blobdiff - bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx
#3463 KMeans box
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkKMeansStatistics.cxx
diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx
new file mode 100644 (file)
index 0000000..45370ac
--- /dev/null
@@ -0,0 +1,214 @@
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+
+
+
+#include <vtkActor.h>
+#include <vtkDoubleArray.h>
+#include <vtkIntArray.h>
+#include <vtkInteractorStyleTrackballCamera.h>
+#include <vtkKMeansStatistics.h>
+#include <vtkMultiBlockDataSet.h>
+#include <vtkNamedColors.h>
+#include <vtkNew.h>
+#include <vtkPointData.h>
+#include <vtkPoints.h>
+#include <vtkPolyData.h>
+#include <vtkPolyDataMapper.h>
+#include <vtkProperty.h>
+#include <vtkRenderWindow.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkRenderer.h>
+#include <vtkTable.h>
+#include <vtkVertexGlyphFilter.h>
+#include <vtkXMLPolyDataWriter.h>
+
+#include <sstream>
+
+
+
+#include "bbcreaVtkKMeansStatistics.h"
+#include "bbcreaVtkPackage.h"
+namespace bbcreaVtk
+{
+
+BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,KMeansStatistics)
+BBTK_BLACK_BOX_IMPLEMENTATION(KMeansStatistics,bbtk::AtomicBlackBox);
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::Process()
+{
+
+// THE MAIN PROCESSING METHOD BODY
+//   Here we simply set the input 'In' value to the output 'Out'
+//   And print out the output value
+// INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
+//    void bbSet{Input|Output}NAME(const TYPE&)
+//    const TYPE& bbGet{Input|Output}NAME() const 
+//    Where :
+//    * NAME is the name of the input/output
+//      (the one provided in the attribute 'name' of the tag 'input')
+//    * TYPE is the C++ type of the input/output
+//      (the one provided in the attribute 'type' of the tag 'input')
+
+//    bbSetOutputOut( bbGetInputIn() );
+//    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
+  
+  
+
+  printf("EED KMeansStatistics::Process Start\n");
+
+       vtkNew<vtkTable> inputData;
+       int ext[6];
+       int dimX,dimY,dimZ;
+       bbGetInputIn()->GetExtent(ext);
+       dimX = ext[1]-ext[0]+1;
+       dimY = ext[3]-ext[2]+1;
+       dimZ = ext[5]-ext[4]+1;
+
+       vtkImageData *resultImage = vtkImageData::New();
+       resultImage->Initialize();
+       resultImage->SetSpacing( bbGetInputIn()->GetSpacing() );
+       resultImage->SetDimensions(  dimX, dimY, dimZ );
+       resultImage->AllocateScalars( VTK_SHORT ,1 );
+  
+       long int size= dimX*dimY*dimZ;
+    vtkDoubleArray *doubleArrayX               = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayY               = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayZ               = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayValue1  = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayValue2  = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayValue3  = vtkDoubleArray::New();
+    doubleArrayX->SetNumberOfComponents(1);
+    doubleArrayY->SetNumberOfComponents(1);
+    doubleArrayZ->SetNumberOfComponents(1);
+    doubleArrayValue1->SetNumberOfComponents(1);
+    doubleArrayValue2->SetNumberOfComponents(1);
+    doubleArrayValue3->SetNumberOfComponents(1);
+    doubleArrayX->SetName("X");
+    doubleArrayY->SetName("Y");
+    doubleArrayZ->SetName("Z");
+    doubleArrayValue1->SetName("Value1");
+    doubleArrayValue2->SetName("Value2");
+    doubleArrayValue3->SetName("Value3");
+    doubleArrayX->SetNumberOfTuples( size );
+    doubleArrayY->SetNumberOfTuples( size );
+    doubleArrayZ->SetNumberOfTuples( size );
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { doubleArrayValue1->SetNumberOfTuples( size ); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { doubleArrayValue2->SetNumberOfTuples( size ); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { doubleArrayValue3->SetNumberOfTuples( size ); }
+    
+       double value;
+       int x,y,z;
+       for ( z=0 ; z<dimZ ; z++ )
+       {
+               for ( y=0 ; y<dimY ; y++ )
+               {
+                       for ( x=0 ; x<dimX ; x++ )
+                       {
+                               doubleArrayX->SetValue(x + y*dimX + z*dimX*dimY,  x );
+                               doubleArrayY->SetValue(x + y*dimX + z*dimX*dimY,  y );
+                               doubleArrayZ->SetValue(x + y*dimX + z*dimX*dimY,  z );
+                               if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) 
+                               { 
+                                       value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,0);
+                                       doubleArrayValue1->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
+                               }
+                               if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) 
+                               { 
+                                       value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,1);
+                                       doubleArrayValue2->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
+                               }
+                               if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) 
+                               { 
+                                       value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,2);
+                                       doubleArrayValue3->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
+                               }
+                       } // for x
+               } // for y        
+       }// for z 
+
+    inputData->AddColumn(doubleArrayX);
+    inputData->AddColumn(doubleArrayY);
+    inputData->AddColumn(doubleArrayZ);
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { inputData->AddColumn(doubleArrayValue1); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { inputData->AddColumn(doubleArrayValue2); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { inputData->AddColumn(doubleArrayValue3); }
+
+
+  printf("EED KMeansStatistics::Process 1\n");
+
+  vtkNew<vtkKMeansStatistics> kMeansStatistics;
+  kMeansStatistics->SetInputData(vtkStatisticsAlgorithm::INPUT_DATA, inputData);
+  kMeansStatistics->SetColumnStatus(inputData->GetColumnName(0), 1);
+  kMeansStatistics->SetColumnStatus(inputData->GetColumnName(1), 1);
+  kMeansStatistics->SetColumnStatus(inputData->GetColumnName(2), 1);
+  if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) {   kMeansStatistics->SetColumnStatus(inputData->GetColumnName(3), 1); }
+  if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) {   kMeansStatistics->SetColumnStatus(inputData->GetColumnName(4), 1); }
+  if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) {   kMeansStatistics->SetColumnStatus(inputData->GetColumnName(5), 1); }
+
+  kMeansStatistics->RequestSelectedColumns();
+  kMeansStatistics->SetAssessOption(true);
+  kMeansStatistics->SetDefaultNumberOfClusters( bbGetInputNumberOfClusters() );
+  kMeansStatistics->Update();
+
+// Display the results
+//  kMeansStatistics->GetOutput()->Dump();
+       for ( z=0 ; z<dimZ ; z++ )
+       {
+               for ( y=0 ; y<dimY ; y++ )
+               {
+                       for ( x=0 ; x<dimX ; x++ )
+                       {
+                               long int        r = x+y*dimX+z*dimX*dimY;
+                           vtkVariant  v = kMeansStatistics->GetOutput()->GetValue( r, kMeansStatistics->GetOutput()->GetNumberOfColumns() - 1);
+                               resultImage->SetScalarComponentFromDouble(x,y,z,0,(double) (v.ToInt()) );
+                       } // for x
+               } // for y        
+       }// for z   
+  bbSetOutputOut( resultImage );
+  printf("EED KMeansStatistics::Process Start\n");  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::bbUserSetDefaultValues()
+{
+
+//  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
+//    Here we initialize the input 'In' to 0
+   bbSetInputIn(NULL);
+   bbSetInputNumberOfClusters(3);
+  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::bbUserInitializeProcessing()
+{
+
+//  THE INITIALIZATION METHOD BODY :
+//    Here does nothing 
+//    but this is where you should allocate the internal/output pointers 
+//    if any 
+
+  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::bbUserFinalizeProcessing()
+{
+
+//  THE FINALIZATION METHOD BODY :
+//    Here does nothing 
+//    but this is where you should desallocate the internal/output pointers 
+//    if any
+  
+}
+}
+// EO namespace bbcreaVtk
+
+