]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkApplyMask.cxx
#3414 creaVtk Feature New Normal - openmp
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkApplyMask.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 #include "bbcreaVtkApplyMask.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "creaVtk_MACROS.h"
8
9 namespace bbcreaVtk
10 {
11
12 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ApplyMask)
13 BBTK_BLACK_BOX_IMPLEMENTATION(ApplyMask,bbtk::AtomicBlackBox);
14 //===== 
15 // 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)
16 //===== 
17 void ApplyMask::Process()
18 {
19
20 // THE MAIN PROCESSING METHOD BODY
21 //   Here we simply set the input 'In' value to the output 'Out'
22 //   And print out the output value
23 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
24 //    void bbSet{Input|Output}NAME(const TYPE&)
25 //    const TYPE& bbGet{Input|Output}NAME() const 
26 //    Where :
27 //    * NAME is the name of the input/output
28 //      (the one provided in the attribute 'name' of the tag 'input')
29 //    * TYPE is the C++ type of the input/output
30 //      (the one provided in the attribute 'type' of the tag 'input')
31
32 //    bbSetOutputOut( bbGetInputIn() );
33 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
34
35         if ((bbGetInputImage()!=NULL) && (bbGetInputMask()!=NULL))
36         {
37                 int                             extImage[6];
38                 int                             extMask[6];
39                 vtkImageData            *resultImage=NULL;
40                 bbGetInputImage()->GetExtent( extImage );
41                 bbGetInputImage()->GetExtent( extMask );
42                 int dimXImage           = extImage[1]-extImage[0]+1;
43                 int dimYImage           = extImage[3]-extImage[2]+1;
44                 int dimZImage           = extImage[5]-extImage[4]+1;
45                 int dimXMask            = extMask[1]-extMask[0]+1;
46                 int dimYMask            = extMask[3]-extMask[2]+1;
47                 int dimZMask            = extMask[5]-extMask[4]+1;
48
49                 double background       = bbGetInputBackground();
50                 double bgToAnalice      = bbGetInputBackgroundToAnalice();
51
52                 if (bbGetInputType()==0)
53                 {
54                         if (bbGetOutputOut()!=NULL) 
55                         {
56                                 bbGetOutputOut()->Delete();
57                         }       
58
59                         resultImage = vtkImageData::New();
60                         resultImage->Initialize();
61                         resultImage->SetSpacing( bbGetInputImage()->GetSpacing() );
62                         resultImage->SetDimensions(  dimXImage, dimYImage, dimZImage );
63                         resultImage->AllocateScalars( bbGetInputImage()->GetScalarType(),1 );
64                 } // if Type==0                 
65
66                 if (bbGetInputType()==1)
67                 {
68                         resultImage=bbGetInputImage();
69                 } // if Type==1
70
71                 if (bbGetInputType()==2)
72                 {
73                         resultImage=bbGetInputImage();
74                 } // if Type==2
75
76                 if (bbGetInputType()==3)
77                 {
78                         resultImage=bbGetInputImage();
79                 } // if Type==3
80
81
82                 if ((dimXImage==dimXMask) &&
83                         (dimYImage==dimYMask) &&
84                         (dimZImage==dimZMask)) 
85                         {
86                                 long int i, size = dimXImage * dimYImage * dimZImage;
87                                 DEF_POINTER_IMAGE_VTK_CREA(vI,ssI,pI,stI, bbGetInputImage() );
88                                 DEF_POINTER_IMAGE_VTK_CREA(vM,ssM,pM,stM, bbGetInputMask() );
89                                 DEF_POINTER_IMAGE_VTK_CREA(vO,ssO,pO,stO, resultImage );
90
91 long int k1omp=(double)(size-1)*0.0;
92 long int k2omp=(double)(size-1)*0.2;
93 long int k3omp=(double)(size-1)*0.4;
94 long int k4omp=(double)(size-1)*0.6;
95 long int k5omp=(double)(size-1)*0.8;
96 long int k6omp=(double)(size-1)*1.0;
97 printf("EED ApplyMask::Process (with openmp)\n");
98                                 #pragma omp parallel for 
99                                 for (i=0; i<size;i++)
100                                 {
101 if ( (k1omp==i) || (k2omp==i) || (k3omp==i) || 
102      (k4omp==i) || (k5omp==i) || (k6omp==i) ) { printf("  %d%\n", (int)(((double)i/(double)(size-1))*100 )); }
103                                         double vItmpOMP;
104                                         double vMtmpOMP;
105                                         double vOtmpOMP;
106                                         GETVALUE2_VTK_CREA(vMtmpOMP,pM,stM,i);
107
108                                         if (bbGetInputType()==0)  //Applay mask to new Image
109                                         {               
110                                                 GETVALUE2_VTK_CREA(vItmpOMP,pI,stI,i);
111                                                 if (vMtmpOMP!=bgToAnalice)
112                                                 {
113                                                         vOtmpOMP = vItmpOMP;
114                                                 } else {
115                                                         vOtmpOMP = background; 
116                                                 } // if vM
117                                                 SETVALUE2_VTK_CREA(vOtmpOMP,pO,stO,i);  
118                                         } // if Type==0
119
120                                         if (bbGetInputType()==1)  //Modifiy the Input image with the Label value in Mask place
121                                         {               
122                                                 if (vMtmpOMP!=bgToAnalice)
123                                                 {
124                                                         vOtmpOMP = bbGetInputLabel();
125                                                         SETVALUE2_VTK_CREA(vOtmpOMP,pO,stO,i);  
126                                                 }
127                                         } // if Type==1
128
129                                         if (bbGetInputType()==2)  //Modifiy the Input image with the Mask value
130                                         {               
131                                                 if (vMtmpOMP!=bgToAnalice)
132                                                 {
133                                                         vOtmpOMP = vMtmpOMP;
134                                                         SETVALUE2_VTK_CREA(vOtmpOMP,pO,stO,i);  
135                                                 }
136                                         } // if Type==2
137
138                                 } // for
139                 } //if dim
140                 resultImage->Modified();
141                 bbSetOutputOut( resultImage );
142         } // if Image && Mask   
143 }
144 //===== 
145 // 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)
146 //===== 
147 void ApplyMask::bbUserSetDefaultValues()
148 {
149
150 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
151 //    Here we initialize the input 'In' to 0
152    bbSetInputImage(NULL);
153    bbSetInputMask(NULL);
154    bbSetInputBackground(0);             // output image Type 0
155    bbSetInputBackgroundToAnalice(0);
156    bbSetInputLabel(255);
157    bbSetInputType(0);
158    bbSetOutputOut(NULL);
159   
160 }
161 //===== 
162 // 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)
163 //===== 
164 void ApplyMask::bbUserInitializeProcessing()
165 {
166
167 //  THE INITIALIZATION METHOD BODY :
168 //    Here does nothing 
169 //    but this is where you should allocate the internal/output pointers 
170 //    if any 
171
172   
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 ApplyMask::bbUserFinalizeProcessing()
178 {
179
180 //  THE FINALIZATION METHOD BODY :
181 //    Here does nothing 
182 //    but this is where you should desallocate the internal/output pointers 
183 //    if any
184   
185 }
186 }
187 // EO namespace bbcreaVtk
188
189