2 // 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)
8 #include <vtkDoubleArray.h>
9 #include <vtkIntArray.h>
10 #include <vtkInteractorStyleTrackballCamera.h>
11 #include <vtkKMeansStatistics.h>
12 #include <vtkMultiBlockDataSet.h>
13 #include <vtkNamedColors.h>
15 #include <vtkPointData.h>
16 #include <vtkPoints.h>
17 #include <vtkPolyData.h>
18 #include <vtkPolyDataMapper.h>
19 #include <vtkProperty.h>
20 #include <vtkRenderWindow.h>
21 #include <vtkRenderWindowInteractor.h>
22 #include <vtkRenderer.h>
24 #include <vtkVertexGlyphFilter.h>
25 #include <vtkXMLPolyDataWriter.h>
31 #include "bbcreaVtkKMeansStatistics.h"
32 #include "bbcreaVtkPackage.h"
36 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,KMeansStatistics)
37 BBTK_BLACK_BOX_IMPLEMENTATION(KMeansStatistics,bbtk::AtomicBlackBox);
39 // 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)
41 void KMeansStatistics::Process()
44 // THE MAIN PROCESSING METHOD BODY
45 // Here we simply set the input 'In' value to the output 'Out'
46 // And print out the output value
47 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
48 // void bbSet{Input|Output}NAME(const TYPE&)
49 // const TYPE& bbGet{Input|Output}NAME() const
51 // * NAME is the name of the input/output
52 // (the one provided in the attribute 'name' of the tag 'input')
53 // * TYPE is the C++ type of the input/output
54 // (the one provided in the attribute 'type' of the tag 'input')
56 // bbSetOutputOut( bbGetInputIn() );
57 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
61 printf("EED KMeansStatistics::Process Start\n");
63 vtkNew<vtkTable> inputData;
66 bbGetInputIn()->GetExtent(ext);
67 dimX = ext[1]-ext[0]+1;
68 dimY = ext[3]-ext[2]+1;
69 dimZ = ext[5]-ext[4]+1;
71 vtkImageData *resultImage = vtkImageData::New();
72 resultImage->Initialize();
73 resultImage->SetSpacing( bbGetInputIn()->GetSpacing() );
74 resultImage->SetDimensions( dimX, dimY, dimZ );
75 resultImage->AllocateScalars( VTK_SHORT ,1 );
77 long int size= dimX*dimY*dimZ;
78 vtkDoubleArray *doubleArrayX = vtkDoubleArray::New();
79 vtkDoubleArray *doubleArrayY = vtkDoubleArray::New();
80 vtkDoubleArray *doubleArrayZ = vtkDoubleArray::New();
81 vtkDoubleArray *doubleArrayValue1 = vtkDoubleArray::New();
82 vtkDoubleArray *doubleArrayValue2 = vtkDoubleArray::New();
83 vtkDoubleArray *doubleArrayValue3 = vtkDoubleArray::New();
84 doubleArrayX->SetNumberOfComponents(1);
85 doubleArrayY->SetNumberOfComponents(1);
86 doubleArrayZ->SetNumberOfComponents(1);
87 doubleArrayValue1->SetNumberOfComponents(1);
88 doubleArrayValue2->SetNumberOfComponents(1);
89 doubleArrayValue3->SetNumberOfComponents(1);
90 doubleArrayX->SetName("X");
91 doubleArrayY->SetName("Y");
92 doubleArrayZ->SetName("Z");
93 doubleArrayValue1->SetName("Value1");
94 doubleArrayValue2->SetName("Value2");
95 doubleArrayValue3->SetName("Value3");
96 doubleArrayX->SetNumberOfTuples( size );
97 doubleArrayY->SetNumberOfTuples( size );
98 doubleArrayZ->SetNumberOfTuples( size );
99 if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { doubleArrayValue1->SetNumberOfTuples( size ); }
100 if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { doubleArrayValue2->SetNumberOfTuples( size ); }
101 if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { doubleArrayValue3->SetNumberOfTuples( size ); }
105 for ( z=0 ; z<dimZ ; z++ )
107 for ( y=0 ; y<dimY ; y++ )
109 for ( x=0 ; x<dimX ; x++ )
111 doubleArrayX->SetValue(x + y*dimX + z*dimX*dimY, x );
112 doubleArrayY->SetValue(x + y*dimX + z*dimX*dimY, y );
113 doubleArrayZ->SetValue(x + y*dimX + z*dimX*dimY, z );
114 if (bbGetInputIn()->GetNumberOfScalarComponents()>=1)
116 value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,0);
117 doubleArrayValue1->SetValue( x + y*dimX + z*dimX*dimY , value );
119 if (bbGetInputIn()->GetNumberOfScalarComponents()>=2)
121 value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,1);
122 doubleArrayValue2->SetValue( x + y*dimX + z*dimX*dimY , value );
124 if (bbGetInputIn()->GetNumberOfScalarComponents()>=3)
126 value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,2);
127 doubleArrayValue3->SetValue( x + y*dimX + z*dimX*dimY , value );
133 inputData->AddColumn(doubleArrayX);
134 inputData->AddColumn(doubleArrayY);
135 inputData->AddColumn(doubleArrayZ);
136 if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { inputData->AddColumn(doubleArrayValue1); }
137 if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { inputData->AddColumn(doubleArrayValue2); }
138 if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { inputData->AddColumn(doubleArrayValue3); }
141 printf("EED KMeansStatistics::Process 1\n");
143 vtkNew<vtkKMeansStatistics> kMeansStatistics;
144 kMeansStatistics->SetInputData(vtkStatisticsAlgorithm::INPUT_DATA, inputData);
145 kMeansStatistics->SetColumnStatus(inputData->GetColumnName(0), 1);
146 kMeansStatistics->SetColumnStatus(inputData->GetColumnName(1), 1);
147 kMeansStatistics->SetColumnStatus(inputData->GetColumnName(2), 1);
148 if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { kMeansStatistics->SetColumnStatus(inputData->GetColumnName(3), 1); }
149 if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { kMeansStatistics->SetColumnStatus(inputData->GetColumnName(4), 1); }
150 if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { kMeansStatistics->SetColumnStatus(inputData->GetColumnName(5), 1); }
152 kMeansStatistics->RequestSelectedColumns();
153 kMeansStatistics->SetAssessOption(true);
154 kMeansStatistics->SetDefaultNumberOfClusters( bbGetInputNumberOfClusters() );
155 kMeansStatistics->Update();
157 // Display the results
158 // kMeansStatistics->GetOutput()->Dump();
159 for ( z=0 ; z<dimZ ; z++ )
161 for ( y=0 ; y<dimY ; y++ )
163 for ( x=0 ; x<dimX ; x++ )
165 long int r = x+y*dimX+z*dimX*dimY;
166 vtkVariant v = kMeansStatistics->GetOutput()->GetValue( r, kMeansStatistics->GetOutput()->GetNumberOfColumns() - 1);
167 resultImage->SetScalarComponentFromDouble(x,y,z,0,(double) (v.ToInt()) );
171 bbSetOutputOut( resultImage );
172 printf("EED KMeansStatistics::Process Start\n");
175 // 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)
177 void KMeansStatistics::bbUserSetDefaultValues()
180 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
181 // Here we initialize the input 'In' to 0
183 bbSetInputNumberOfClusters(3);
187 // 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)
189 void KMeansStatistics::bbUserInitializeProcessing()
192 // THE INITIALIZATION METHOD BODY :
194 // but this is where you should allocate the internal/output pointers
200 // 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)
202 void KMeansStatistics::bbUserFinalizeProcessing()
205 // THE FINALIZATION METHOD BODY :
207 // but this is where you should desallocate the internal/output pointers
212 // EO namespace bbcreaVtk