]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkUnaryOperations.cxx
488f175881d5753877444e5a7a0915fce9c6d42b
[bbtk.git] / packages / vtk / src / bbvtkUnaryOperations.cxx
1 #include "bbvtkUnaryOperations.h"
2 #include "bbvtkPackage.h"
3 namespace bbvtk
4 {
5
6
7 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,UnaryOperations)
8 BBTK_BLACK_BOX_IMPLEMENTATION(UnaryOperations,bbtk::AtomicBlackBox);
9 void UnaryOperations::Process()
10 {
11
12 // THE MAIN PROCESSING METHOD BODY
13 //   Here we simply set the input 'In' value to the output 'Out'
14 //   And print out the output value
15 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
16 //    void bbSet{Input|Output}NAME(const TYPE&)
17 //    const TYPE& bbGet{Input|Output}NAME() const
18 //    Where :
19 //    * NAME is the name of the input/output
20 //      (the one provided in the attribute 'name' of the tag 'input')
21 //    * TYPE is the C++ type of the input/output
22 //      (the one provided in the attribute 'type' of the tag 'input')
23
24         if (bbGetInputIn1() == NULL)
25         {
26                 std::cout << "Set In1 at least" << std::endl;
27                 return;
28         }
29
30         ope->SetInput1((vtkDataObject*)bbGetInputIn1());
31
32
33         ope->SetConstantK(bbGetInputInConstant());
34         ope->SetConstantC(bbGetInputInConstant());
35
36         switch (bbGetInputOperation())
37         {
38                 case 0:
39                                 ope->SetOperationToAddConstant();
40
41                 break;
42                 case 1:
43                                 crearPredeterminado();
44                                 ope->SetInput2((vtkDataSet*) constante);
45                                 ope->SetOperationToSubtract();
46                 break;
47                 case 2:
48                 ope->SetInput2((vtkDataSet*) constante);
49                                 ope->SetOperationToMultiply();
50
51                 break;
52                 case 3:
53
54                                 ope->SetOperationToDivide();
55                 break;
56                 case 4:
57                          ope->SetOperationToInvert();
58                 break;
59                 case 9:
60                          ope->SetOperationToAbsoluteValue();
61                 break;
62                 case 5:
63
64                                 ope->SetOperationToSin();
65                 break;
66                 case 6:
67
68                         ope->SetOperationToCos();
69
70                 break;
71                 case 7:
72
73                                 ope->SetOperationToExp();
74                 break;
75                 case 8:
76
77                                 ope->SetOperationToLog();
78                 break;
79                 case 10:
80                         substractWithMinimum();
81                         bbSetOutputOut(constante);
82                         return;
83                 break;
84                 case 11:
85                         MultiplyBy();
86                         bbSetOutputOut(constante);
87                         return;
88                 break;
89                 default:
90                         std::cout << "Invalid Operation" << std::endl;
91                         return;
92                 break;
93         }
94
95         ope->Update();
96         vtkImageData* salida = ope->GetOutput();
97
98         bbSetOutputOut(salida);
99         bbSignalOutputModification();
100
101 }
102 void UnaryOperations::crearPredeterminado()
103 {
104         int ext[6];
105         vtkImageData* inicial = bbGetInputIn1();
106
107
108         if (constante == NULL)
109         {
110                 inicial->GetExtent(ext);
111                 constante = vtkImageData::New();
112                 constante->SetExtent(ext);
113                 constante->SetScalarType(inicial->GetScalarType());
114                 constante->AllocateScalars();
115                 constante->Update();
116         }
117
118         for (int i=ext[0]; i<=ext[1]; i++)
119         {
120                 for (int j=ext[2]; j<=ext[3]; j++)
121                 {
122                         for (int k=ext[4]; k<=ext[5]; k++)
123                         {
124                                 unsigned short* value = (unsigned short*)constante->GetScalarPointer(i,j,k);
125                                 *value = bbGetInputInConstant();
126                         }
127                 }
128         }
129 }
130 void UnaryOperations::substractWithMinimum()
131 {
132         int ext[6];
133         double spc[3];
134         vtkImageData* inicial = bbGetInputIn1();
135
136
137         if (constante == NULL)
138         {
139                 inicial->GetExtent(ext);
140                 inicial->GetSpacing(spc);
141                 constante = vtkImageData::New();
142                 constante->SetExtent(ext);
143                 constante->SetSpacing(spc);
144                 constante->SetOrigin(inicial->GetOrigin());
145                 constante->SetScalarType(inicial->GetScalarType());
146                 constante->AllocateScalars();
147                 constante->Update();
148         }
149
150         for (int i=ext[0]; i<=ext[1]; i++)
151         {
152                 for (int j=ext[2]; j<=ext[3]; j++)
153                 {
154                         for (int k=ext[4]; k<=ext[5]; k++)
155                         {
156                                 double temp1 = lector.getPixelValue(i,j,k,inicial);
157                                 double temp = temp1 - bbGetInputInConstant();
158                                 if (temp < 0)
159                                         temp = 0;
160                                 lector.setPixelValue(i,j,k,constante,temp);
161                         }
162                 }
163         }
164 }
165
166 void UnaryOperations::MultiplyBy()
167 {
168         int ext[6];
169         double spc[3];
170         vtkImageData* inicial = bbGetInputIn1();
171
172
173         if (constante == NULL)
174         {
175                 inicial->GetExtent(ext);
176                 inicial->GetSpacing(spc);
177                 constante = vtkImageData::New();
178                 constante->SetExtent(ext);
179                 constante->SetSpacing(spc);
180                 constante->SetOrigin(inicial->GetOrigin());
181                 constante->SetScalarType(inicial->GetScalarType());
182                 constante->AllocateScalars();
183                 constante->Update();
184         }
185
186         for (int i=ext[0]; i<=ext[1]; i++)
187         {
188                 for (int j=ext[2]; j<=ext[3]; j++)
189                 {
190                         for (int k=ext[4]; k<=ext[5]; k++)
191                         {
192                                 double temp1 = lector.getPixelValue(i,j,k,inicial);
193                                 double temp = temp1 * bbGetInputInConstant();
194                                 if (temp < 0)
195                                         temp = 0;
196                                 lector.setPixelValue(i,j,k,constante,temp);
197                         }
198                 }
199         }
200 }
201
202 void UnaryOperations::bbUserSetDefaultValues()
203 {
204
205 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
206 //    Here we initialize the input 'In' to 0
207    bbSetInputIn1(NULL);
208 //   bbSetInputIn2(NULL);
209    bbSetOutputOut(NULL);
210    bbSetInputInConstant(0);
211    ope = vtkImageMathematics::New();
212    constante = NULL;
213 }
214 void UnaryOperations::bbUserInitializeProcessing()
215 {
216
217 //  THE INITIALIZATION METHOD BODY :
218 //    Here does nothing
219 //    but this is where you should allocate the internal/output pointers
220 //    if any
221
222
223 }
224 void UnaryOperations::bbUserFinalizeProcessing()
225 {
226
227 //  THE FINALIZATION METHOD BODY :
228 //    Here does nothing
229 //    but this is where you should desallocate the internal/output pointers
230 //    if any
231
232 }
233 }
234 // EO namespace bbvtk
235
236