]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSimpleMathematics.cxx
765d06fbdde80d22b3e50ef723dd6774853a9398
[bbtk.git] / packages / vtk / src / bbvtkSimpleMathematics.cxx
1 #include "bbvtkSimpleMathematics.h"
2 #include "bbvtkPackage.h"
3 namespace bbvtk
4 {
5
6         void StaticLecture::setPixelValue(int i, int j, int k, vtkImageData* img, double value)
7         {
8                 //double rta;
9                 int scalar_type = img->GetScalarType();
10                 int* ext = img->GetExtent();
11                 if (i < ext[0])
12                         return;
13                 if (i > ext[1])
14                         return;
15
16                 if (j < ext[2])
17                         return;
18                 if (j > ext[3])
19                         return;
20
21                 if (k < ext[4])
22                         return;
23                 if (k > ext[5])
24                         return;
25
26                 switch (scalar_type)
27                 {
28                         case VTK_CHAR:
29                                 char * ap2;
30                                 ap2 = (char *) img->GetScalarPointer(i,j,k);
31                                 *ap2 = (char) value;
32                         break;
33                         case VTK_UNSIGNED_CHAR:
34                                 unsigned char * ap3;
35                                 ap3 = (unsigned char *) img->GetScalarPointer(i,j,k);
36                                 *ap3 = (unsigned char) value;
37                         break;
38                         case VTK_SHORT:
39                                 short * ap4;
40                                 ap4 = (short *) img->GetScalarPointer(i,j,k);
41                                 *ap4 = (short) value;
42                         break;
43                         case VTK_UNSIGNED_SHORT:
44                                 unsigned short * ap5;
45                                 ap5 = (unsigned short *) img->GetScalarPointer(i,j,k);
46                                 *ap5 = (unsigned short) value;
47                         break;
48                         case VTK_INT:
49                                 int * ap6;
50                                 ap6 = (int *) img->GetScalarPointer(i,j,k);
51                                 *ap6 = (int) value;
52                         break;
53                         case VTK_UNSIGNED_INT:
54                                 unsigned int * ap7;
55                                 ap7 = (unsigned int *) img->GetScalarPointer(i,j,k);
56                                 *ap7 = (unsigned int) value;
57                         break;
58                         case VTK_LONG:
59                                 long * ap8;
60                                 ap8 = (long *) img->GetScalarPointer(i,j,k);
61                                 *ap8 = (long) value;
62                         break;
63                         case VTK_UNSIGNED_LONG:
64                                 unsigned long * ap9;
65                                 ap9 = (unsigned long *) img->GetScalarPointer(i,j,k);
66                                 *ap9 = (unsigned long) value;
67                         break;
68                         case VTK_FLOAT:
69                                 float * ap10;
70                                 ap10 = (float *) img->GetScalarPointer(i,j,k);
71                                 *ap10 = (float) value;
72                         break;
73                         case VTK_DOUBLE:
74                                 double * ap11;
75                                 ap11 = (double *) img->GetScalarPointer(i,j,k);
76                                 *ap11 = (double) value;
77                         break;
78                 }
79         }
80
81         double StaticLecture::getPixelValue(int i, int j, int k, vtkImageData* img)
82         {
83                 double rta;
84                 int scalar_type = img->GetScalarType();
85                 int* ext = img->GetExtent();
86                 if (i < ext[0])
87                         i=ext[0];
88                 if (i > ext[1])
89                         i=ext[1];
90
91                 if (j < ext[2])
92                         j=ext[2];
93                 if (j > ext[3])
94                         j=ext[3];
95
96                 if (k < ext[4])
97                         k=ext[4];
98                 if (k > ext[5])
99                         k=ext[5];
100
101                 switch (scalar_type)
102                 {
103                         case VTK_CHAR:
104                                 char * ap2;
105                                 ap2 = (char *) img->GetScalarPointer(i,j,k);
106                                 rta = (double) *ap2;
107                         break;
108                         case VTK_UNSIGNED_CHAR:
109                                 unsigned char * ap3;
110                                 ap3 = (unsigned char *) img->GetScalarPointer(i,j,k);
111                                 rta = (double) *ap3;
112                         break;
113                         case VTK_SHORT:
114                                 short * ap4;
115                                 ap4 = (short *) img->GetScalarPointer(i,j,k);
116                                 rta = (double) *ap4;
117                         break;
118                         case VTK_UNSIGNED_SHORT:
119                                 unsigned short * ap5;
120                                 ap5 = (unsigned short *) img->GetScalarPointer(i,j,k);
121                                 rta = (double) *ap5;
122                         break;
123                         case VTK_INT:
124                                 int * ap6;
125                                 ap6 = (int *) img->GetScalarPointer(i,j,k);
126                                 rta = (double) *ap6;
127                         break;
128                         case VTK_UNSIGNED_INT:
129                                 unsigned int * ap7;
130                                 ap7 = (unsigned int *) img->GetScalarPointer(i,j,k);
131                                 rta = (double) *ap7;
132                         break;
133                         case VTK_LONG:
134                                 long * ap8;
135                                 ap8 = (long *) img->GetScalarPointer(i,j,k);
136                                 rta = (double) *ap8;
137                         break;
138                         case VTK_UNSIGNED_LONG:
139                                 unsigned long * ap9;
140                                 ap9 = (unsigned long *) img->GetScalarPointer(i,j,k);
141                                 rta = (double) *ap9;
142                         break;
143                         case VTK_FLOAT:
144                                 float * ap10;
145                                 ap10 = (float *) img->GetScalarPointer(i,j,k);
146                                 rta = (double) *ap10;
147                         break;
148                         case VTK_DOUBLE:
149                                 double * ap11;
150                                 ap11 = (double *) img->GetScalarPointer(i,j,k);
151                                 rta = (double) *ap11;
152                         break;
153                 }
154                 return rta;
155         }
156
157 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,SimpleMathematics)
158 BBTK_BLACK_BOX_IMPLEMENTATION(SimpleMathematics,bbtk::AtomicBlackBox);
159 void SimpleMathematics::Process()
160 {
161
162 // THE MAIN PROCESSING METHOD BODY
163 //   Here we simply set the input 'In' value to the output 'Out'
164 //   And print out the output value
165 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
166 //    void bbSet{Input|Output}NAME(const TYPE&)
167 //    const TYPE& bbGet{Input|Output}NAME() const
168 //    Where :
169 //    * NAME is the name of the input/output
170 //      (the one provided in the attribute 'name' of the tag 'input')
171 //    * TYPE is the C++ type of the input/output
172 //      (the one provided in the attribute 'type' of the tag 'input')
173
174         if (bbGetInputIn1() == NULL)
175         {
176                 std::cout << "Set In1 at least" << std::endl;
177                 return;
178         }
179
180         ope->SetInput1((vtkDataObject*)bbGetInputIn1());
181         bool onlyone = true;
182
183         if (bbGetInputIn2() != NULL)
184         {
185                 ope->SetInput2((vtkDataObject*)bbGetInputIn2());
186                 onlyone = false;
187         }
188
189         ope->SetConstantK(bbGetInputInConstant());
190         ope->SetConstantC(bbGetInputInConstant());
191
192         switch (bbGetInputOperation())
193         {
194                 case 0:
195                         if (onlyone)
196                                 ope->SetOperationToAddConstant();
197                         else
198                                 ope->SetOperationToAdd();
199                 break;
200                 case 1:
201                         if (onlyone)
202                         {
203                                 crearPredeterminado();
204                                 ope->SetInput2((vtkDataSet*) constante);
205                                 ope->SetOperationToSubtract();
206                         }
207                         else
208                         {
209                                 ope->SetOperationToSubtract();
210                         }
211                 break;
212                 case 2:
213                         if (onlyone)
214                                 ope->SetOperationToMultiply();
215                         else
216                                 ope->SetOperationToMultiplyByK();
217                 break;
218                 case 3:
219                         if (onlyone)
220                         {
221                                 std::cout << "Set In2" << std::endl;
222                                 return;
223                         }
224                         else
225                         {
226                                 ope->SetOperationToDivide();
227                         }
228                 break;
229                 case 4:
230                         ope->SetOperationToInvert();
231                 break;
232                 case 9:
233                         ope->SetOperationToAbsoluteValue();
234                 break;
235                 case 5:
236                         if (onlyone)
237                         {
238                                 std::cout << "Set In2" << std::endl;
239                                 return;
240                         }
241                         else
242                         {
243                                 ope->SetOperationToSin();
244                         }
245                 break;
246                 case 6:
247                         if (onlyone)
248                         {
249                                 std::cout << "Set In2" << std::endl;
250                                 return;
251                         }
252                         else
253                         {
254                                 ope->SetOperationToCos();
255                         }
256                 break;
257                 case 7:
258                         if (onlyone)
259                         {
260                                 std::cout << "Set In2" << std::endl;
261                                 return;
262                         }
263                         else
264                         {
265                                 ope->SetOperationToExp();
266                         }
267                 break;
268                 case 8:
269                         if (onlyone)
270                         {
271                                 std::cout << "Set In2" << std::endl;
272                                 return;
273                         }
274                         else
275                         {
276                                 ope->SetOperationToLog();
277                         }
278                 break;
279                 case 10:
280                         substractWithMinimum();
281                         bbSetOutputOut(constante);
282                         return;
283                 break;
284                 case 11:
285                         MultiplyBy();
286                         bbSetOutputOut(constante);
287                         return;
288                 break;
289                 default:
290                         std::cout << "Invalid Operation" << std::endl;
291                         return;
292                 break;
293         }
294
295         ope->Update();
296         vtkImageData* salida = ope->GetOutput();
297
298         bbSetOutputOut(salida);
299         bbSignalOutputModification();
300
301 }
302 void SimpleMathematics::crearPredeterminado()
303 {
304         int ext[6];
305         vtkImageData* inicial = bbGetInputIn1();
306
307
308         if (constante == NULL)
309         {
310                 inicial->GetExtent(ext);
311                 constante = vtkImageData::New();
312                 constante->SetExtent(ext);
313                 constante->SetScalarType(inicial->GetScalarType());
314                 constante->AllocateScalars();
315                 constante->Update();
316         }
317
318         for (int i=ext[0]; i<=ext[1]; i++)
319         {
320                 for (int j=ext[2]; j<=ext[3]; j++)
321                 {
322                         for (int k=ext[4]; k<=ext[5]; k++)
323                         {
324                                 unsigned short* value = (unsigned short*)constante->GetScalarPointer(i,j,k);
325                                 *value = bbGetInputInConstant();
326                         }
327                 }
328         }
329 }
330 void SimpleMathematics::substractWithMinimum()
331 {
332         int ext[6];
333         double spc[3];
334         vtkImageData* inicial = bbGetInputIn1();
335
336
337         if (constante == NULL)
338         {
339                 inicial->GetExtent(ext);
340                 inicial->GetSpacing(spc);
341                 constante = vtkImageData::New();
342                 constante->SetExtent(ext);
343                 constante->SetSpacing(spc);
344                 constante->SetOrigin(inicial->GetOrigin());
345                 constante->SetScalarType(inicial->GetScalarType());
346                 constante->AllocateScalars();
347                 constante->Update();
348         }
349
350         for (int i=ext[0]; i<=ext[1]; i++)
351         {
352                 for (int j=ext[2]; j<=ext[3]; j++)
353                 {
354                         for (int k=ext[4]; k<=ext[5]; k++)
355                         {
356                                 double temp1 = lector.getPixelValue(i,j,k,inicial);
357                                 double temp = temp1 - bbGetInputInConstant();
358                                 if (temp < 0)
359                                         temp = 0;
360                                 lector.setPixelValue(i,j,k,constante,temp);
361                         }
362                 }
363         }
364 }
365
366 void SimpleMathematics::MultiplyBy()
367 {
368         int ext[6];
369         double spc[3];
370         vtkImageData* inicial = bbGetInputIn1();
371
372
373         if (constante == NULL)
374         {
375                 inicial->GetExtent(ext);
376                 inicial->GetSpacing(spc);
377                 constante = vtkImageData::New();
378                 constante->SetExtent(ext);
379                 constante->SetSpacing(spc);
380                 constante->SetOrigin(inicial->GetOrigin());
381                 constante->SetScalarType(inicial->GetScalarType());
382                 constante->AllocateScalars();
383                 constante->Update();
384         }
385
386         for (int i=ext[0]; i<=ext[1]; i++)
387         {
388                 for (int j=ext[2]; j<=ext[3]; j++)
389                 {
390                         for (int k=ext[4]; k<=ext[5]; k++)
391                         {
392                                 double temp1 = lector.getPixelValue(i,j,k,inicial);
393                                 double temp = temp1 * bbGetInputInConstant();
394                                 if (temp < 0)
395                                         temp = 0;
396                                 lector.setPixelValue(i,j,k,constante,temp);
397                         }
398                 }
399         }
400 }
401
402 void SimpleMathematics::bbUserSetDefaultValues()
403 {
404
405 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
406 //    Here we initialize the input 'In' to 0
407    bbSetInputIn1(NULL);
408    bbSetInputIn2(NULL);
409    bbSetOutputOut(NULL);
410    bbSetInputInConstant(0);
411    ope = vtkImageMathematics::New();
412    constante = NULL;
413 }
414 void SimpleMathematics::bbUserInitializeProcessing()
415 {
416
417 //  THE INITIALIZATION METHOD BODY :
418 //    Here does nothing
419 //    but this is where you should allocate the internal/output pointers
420 //    if any
421
422
423 }
424 void SimpleMathematics::bbUserFinalizeProcessing()
425 {
426
427 //  THE FINALIZATION METHOD BODY :
428 //    Here does nothing
429 //    but this is where you should desallocate the internal/output pointers
430 //    if any
431
432 }
433 }
434 // EO namespace bbvtk
435
436