]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx
#3463 KMeans box
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkKMeansStatistics.cxx
1 //===== 
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)
3 //===== 
4
5
6
7 #include <vtkActor.h>
8 #include <vtkDoubleArray.h>
9 #include <vtkIntArray.h>
10 #include <vtkInteractorStyleTrackballCamera.h>
11 #include <vtkKMeansStatistics.h>
12 #include <vtkMultiBlockDataSet.h>
13 #include <vtkNamedColors.h>
14 #include <vtkNew.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>
23 #include <vtkTable.h>
24 #include <vtkVertexGlyphFilter.h>
25 #include <vtkXMLPolyDataWriter.h>
26
27 #include <sstream>
28
29
30
31 #include "bbcreaVtkKMeansStatistics.h"
32 #include "bbcreaVtkPackage.h"
33 namespace bbcreaVtk
34 {
35
36 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,KMeansStatistics)
37 BBTK_BLACK_BOX_IMPLEMENTATION(KMeansStatistics,bbtk::AtomicBlackBox);
38 //===== 
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)
40 //===== 
41 void KMeansStatistics::Process()
42 {
43
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 
50 //    Where :
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')
55
56 //    bbSetOutputOut( bbGetInputIn() );
57 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
58   
59   
60
61   printf("EED KMeansStatistics::Process Start\n");
62
63         vtkNew<vtkTable> inputData;
64         int ext[6];
65         int dimX,dimY,dimZ;
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;
70
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 );
76   
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 ); }
102     
103         double value;
104         int x,y,z;
105         for ( z=0 ; z<dimZ ; z++ )
106         {
107                 for ( y=0 ; y<dimY ; y++ )
108                 {
109                         for ( x=0 ; x<dimX ; x++ )
110                         {
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) 
115                                 { 
116                                         value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,0);
117                                         doubleArrayValue1->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
118                                 }
119                                 if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) 
120                                 { 
121                                         value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,1);
122                                         doubleArrayValue2->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
123                                 }
124                                 if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) 
125                                 { 
126                                         value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,2);
127                                         doubleArrayValue3->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
128                                 }
129                         } // for x
130                 } // for y        
131         }// for z 
132
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); }
139
140
141   printf("EED KMeansStatistics::Process 1\n");
142
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); }
151
152   kMeansStatistics->RequestSelectedColumns();
153   kMeansStatistics->SetAssessOption(true);
154   kMeansStatistics->SetDefaultNumberOfClusters( bbGetInputNumberOfClusters() );
155   kMeansStatistics->Update();
156
157 // Display the results
158 //  kMeansStatistics->GetOutput()->Dump();
159         for ( z=0 ; z<dimZ ; z++ )
160         {
161                 for ( y=0 ; y<dimY ; y++ )
162                 {
163                         for ( x=0 ; x<dimX ; x++ )
164                         {
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()) );
168                         } // for x
169                 } // for y        
170         }// for z   
171   bbSetOutputOut( resultImage );
172   printf("EED KMeansStatistics::Process Start\n");  
173 }
174 //===== 
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)
176 //===== 
177 void KMeansStatistics::bbUserSetDefaultValues()
178 {
179
180 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
181 //    Here we initialize the input 'In' to 0
182    bbSetInputIn(NULL);
183    bbSetInputNumberOfClusters(3);
184   
185 }
186 //===== 
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)
188 //===== 
189 void KMeansStatistics::bbUserInitializeProcessing()
190 {
191
192 //  THE INITIALIZATION METHOD BODY :
193 //    Here does nothing 
194 //    but this is where you should allocate the internal/output pointers 
195 //    if any 
196
197   
198 }
199 //===== 
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)
201 //===== 
202 void KMeansStatistics::bbUserFinalizeProcessing()
203 {
204
205 //  THE FINALIZATION METHOD BODY :
206 //    Here does nothing 
207 //    but this is where you should desallocate the internal/output pointers 
208 //    if any
209   
210 }
211 }
212 // EO namespace bbcreaVtk
213
214