]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkApplyMask.cxx
#3480 bug ApplyMask
[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         bbGetInputMask()->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                 double background       = bbGetInputBackground();
49                 double bgToAnalice      = bbGetInputBackgroundToAnalice();
50
51                 if (bbGetInputType()==0)
52                 {
53                         if (bbGetOutputOut()!=NULL) 
54                         {
55                                 bbGetOutputOut()->Delete();
56                         }       
57
58                         resultImage = vtkImageData::New();
59                         resultImage->Initialize();
60                         resultImage->SetSpacing( bbGetInputImage()->GetSpacing() );
61                         resultImage->SetDimensions(  dimXImage, dimYImage, dimZImage );
62                         resultImage->AllocateScalars( bbGetInputImage()->GetScalarType(),1 );
63                 } // if Type==0                 
64
65                 if (bbGetInputType()==1)
66                 {
67                         resultImage=bbGetInputImage();
68                 } // if Type==1
69
70                 if (bbGetInputType()==2)
71                 {
72                         resultImage=bbGetInputImage();
73                 } // if Type==2
74
75                 if (bbGetInputType()==3)
76                 {
77                         resultImage=bbGetInputImage();
78                 } // if Type==3
79
80
81                 if ((dimXImage==dimXMask) &&
82                         (dimYImage==dimYMask) &&
83                         (dimZImage==dimZMask)) 
84                         {
85                                 long int i, size = dimXImage * dimYImage * dimZImage;
86                                 DEF_POINTER_IMAGE_VTK_CREA(vI,ssI,pI,stI, bbGetInputImage() );
87                                 DEF_POINTER_IMAGE_VTK_CREA(vM,ssM,pM,stM, bbGetInputMask() );
88                                 DEF_POINTER_IMAGE_VTK_CREA(vO,ssO,pO,stO, resultImage );
89 long int k1omp=(double)(size-1)*0.0;
90 long int k2omp=(double)(size-1)*0.2;
91 long int k3omp=(double)(size-1)*0.4;
92 long int k4omp=(double)(size-1)*0.6;
93 long int k5omp=(double)(size-1)*0.8;
94 long int k6omp=(double)(size-1)*1.0;
95 printf("EED ApplyMask::Process (with openmp)\n");
96                                 #pragma omp parallel for 
97                                 for (i=0; i<size;i++)
98                                 {
99 if ( (k1omp==i) || (k2omp==i) || (k3omp==i) || 
100      (k4omp==i) || (k5omp==i) || (k6omp==i) ) { printf("  %d%\n", (int)(((double)i/(double)(size-1))*100 )); }
101                                         double vItmpOMP;
102                                         double vMtmpOMP;
103                                         double vOtmpOMP;
104                                         GETVALUE2_VTK_CREA(vMtmpOMP,pM,stM,i);
105                                         if (bbGetInputType()==0)  //Applay mask to new Image
106                                         {               
107                                                 GETVALUE2_VTK_CREA(vItmpOMP,pI,stI,i);
108                                                 if (vMtmpOMP!=bgToAnalice)
109                                                 {
110                                                         vOtmpOMP = vItmpOMP;
111                                                 } else {
112                                                         vOtmpOMP = background; 
113                                                 } // if vM
114                                                 SETVALUE2_VTK_CREA(vOtmpOMP,pO,stO,i);  
115                                         } // if Type==0
116                                         if (bbGetInputType()==1)  //Modifiy the Input image with the Label value in Mask place
117                                         {               
118                                                 if (vMtmpOMP!=bgToAnalice)
119                                                 {
120                                                         vOtmpOMP = bbGetInputLabel();
121                                                         SETVALUE2_VTK_CREA(vOtmpOMP,pO,stO,i);  
122                                                 }
123                                         } // if Type==1
124                                         if (bbGetInputType()==2)  //Modifiy the Input image with the Mask value
125                                         {               
126                                                 if (vMtmpOMP!=bgToAnalice)
127                                                 {
128                                                         vOtmpOMP = vMtmpOMP;
129                                                         SETVALUE2_VTK_CREA(vOtmpOMP,pO,stO,i);  
130                                                 }
131                                         } // if Type==2
132                                 } // for
133                 } //if dim
134                 resultImage->Modified();
135                 bbSetOutputOut( resultImage );
136     } else {
137         if (bbGetInputType()==2)
138         {
139             bbSetOutputOut( bbGetInputImage() );
140         }
141     }// if Image && Mask
142 }
143 //===== 
144 // 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)
145 //===== 
146 void ApplyMask::bbUserSetDefaultValues()
147 {
148
149 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
150 //    Here we initialize the input 'In' to 0
151    bbSetInputImage(NULL);
152    bbSetInputMask(NULL);
153    bbSetInputBackground(0);             // output image Type 0
154    bbSetInputBackgroundToAnalice(0);
155    bbSetInputLabel(255);
156    bbSetInputType(0);
157    bbSetOutputOut(NULL);
158   
159 }
160 //===== 
161 // 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)
162 //===== 
163 void ApplyMask::bbUserInitializeProcessing()
164 {
165
166 //  THE INITIALIZATION METHOD BODY :
167 //    Here does nothing 
168 //    but this is where you should allocate the internal/output pointers 
169 //    if any 
170
171   
172 }
173 //===== 
174 // 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)
175 //===== 
176 void ApplyMask::bbUserFinalizeProcessing()
177 {
178
179 //  THE FINALIZATION METHOD BODY :
180 //    Here does nothing 
181 //    but this is where you should desallocate the internal/output pointers 
182 //    if any
183   
184 }
185 }
186 // EO namespace bbcreaVtk
187
188