]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkInversCrop.cxx
#3467 Bug: InvertCrop Dimension of 2D or 3D
[bbtk.git] / packages / vtk / src / bbvtkInversCrop.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 "bbvtkInversCrop.h"
5 #include "bbvtkPackage.h"
6 namespace bbvtk
7 {
8
9 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,InversCrop)
10 BBTK_BLACK_BOX_IMPLEMENTATION(InversCrop,bbtk::AtomicBlackBox);
11 //===== 
12 // 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)
13 //===== 
14 void InversCrop::Process()
15 {
16
17 // THE MAIN PROCESSING METHOD BODY
18 //   Here we simply set the input 'In' value to the output 'Out'
19 //   And print out the output value
20 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
21 //    void bbSet{Input|Output}NAME(const TYPE&)
22 //    const TYPE& bbGet{Input|Output}NAME() const 
23 //    Where :
24 //    * NAME is the name of the input/output
25 //      (the one provided in the attribute 'name' of the tag 'input')
26 //    * TYPE is the C++ type of the input/output
27 //      (the one provided in the attribute 'type' of the tag 'input')
28
29         if (bbGetInputActive()==true)
30         {
31             if ( (bbGetInputImageFix()!=NULL) && (bbGetInputImageMove()!=NULL) )
32             {
33              if ( bbGetInputImageFix()->GetScalarType()==bbGetInputImageMove()->GetScalarType() ) 
34              {
35                 // Creating Image
36                 int dim[3];
37                 int ext[6];
38         
39 //EED 2017-01-01 Migration VTK7
40 #if (VTK_MAJOR_VERSION <= 5) 
41         bbGetInputImageFix()->GetWholeExtent(ext);
42 #endif
43 #if (VTK_MAJOR_VERSION >= 6) 
44         bbGetInputImageFix()->GetExtent(ext);
45 #endif
46                 dim[0]= ext[1]-ext[0]+1;
47                 dim[1]= ext[3]-ext[2]+1;
48                 dim[2] = 1;
49                 if (bbGetInputImageFix()->GetDataDimension()==3) 
50                 {
51                         dim[2] = ext[5]-ext[4]+1;
52                 } 
53                 if (bbGetInputType()==0)
54                 {
55                         _imageoutput = vtkImageData::New();
56                         _imageoutput->Initialize();
57                         _imageoutput->SetSpacing( bbGetInputImageFix()->GetSpacing() );
58                         _imageoutput->SetDimensions(  dim[0], dim[1], dim[2] );                 
59 //EED 2017-01-01 Migration VTK7
60 #if (VTK_MAJOR_VERSION <= 5) 
61                         _imageoutput->SetScalarType( bbGetInputImageFix()->GetScalarType() );
62                         _imageoutput->AllocateScalars();
63 #endif
64 #if (VTK_MAJOR_VERSION >= 6) 
65                         _imageoutput->AllocateScalars(bbGetInputImageFix()->GetScalarType() , 1);
66 #endif
67                 
68                         // Duplicating Fix Image
69                         long sizeimage = dim[0]*dim[1]*dim[2]*bbGetInputImageFix()->GetScalarSize();    
70                         memcpy( _imageoutput->GetScalarPointer() , bbGetInputImageFix()->GetScalarPointer() , sizeimage);
71                 }
72                 if (bbGetInputType()==1)
73                 {
74                         _imageoutput=bbGetInputImageFix();
75                 }
76
77                 // Copy the Move Image
78                 int j,k; 
79                 int px,py,pz;
80
81 //EED 2017-01-01 Migration VTK7
82 #if (VTK_MAJOR_VERSION <= 5) 
83                 bbGetInputImageMove()->GetWholeExtent(ext);
84 #endif
85 #if (VTK_MAJOR_VERSION >= 6) 
86                 bbGetInputImageMove()->GetExtent(ext);
87 #endif
88                 int dimMoveX    = ext[1]-ext[0]+1;
89                 int dimMoveY    = ext[3]-ext[2]+1;
90                 int dimMoveZ    = 1;
91                 if (bbGetInputImageMove()->GetDataDimension()==3) 
92                 {
93                         dimMoveZ = ext[5]-ext[4]+1;
94                 } 
95                 double org[3];
96                 org[0]=0;
97                 org[1]=0;
98                 org[2]=0;               
99                 if (bbGetInputOrigin().size()==3)
100                 {
101                         org[0]=bbGetInputOrigin()[0];
102                         org[1]=bbGetInputOrigin()[1];
103                         org[2]=bbGetInputOrigin()[2];
104                 }
105                 if (bbGetInputOrigin().size()==2)
106                 {
107                         org[0]=bbGetInputOrigin()[0];
108                         org[1]=bbGetInputOrigin()[1];
109                 }
110                 if (bbGetInputOrigin().size()==1)
111                 {
112                         org[0]=bbGetInputOrigin()[0];
113                 }
114                 int spxM                = 0;  // start px MoveImage
115                 int sizeXM              = 0;  // sizeX MoveImage
116                 px                              = org[0];
117                 spxM                    = 0;
118                 if (px<0)
119                 { 
120                    spxM = px*(-1);
121                    px   = 0;
122                 }
123                 sizeXM  = dimMoveX-spxM;
124                 if (px+sizeXM>=dim[0]) { sizeXM=dim[0]-px; }
125                 sizeXM  = sizeXM * bbGetInputImageFix()->GetScalarSize();
126                 for (k=0; k<dimMoveZ; k++)
127                 {
128                    for (j=0; j<dimMoveY; j++)
129                    {
130                         py = j+org[1];
131                                 if (bbGetInputImageMove()->GetDataDimension()==3) 
132                                 {
133                                         pz = k+org[2];
134                                 } else {
135                                         pz = k;
136                                 }// Dimension 3
137                                 if ( (py<dim[1]) && (pz<dim[2]) &&
138                                  (py>=0)    && (pz>=0)  &&
139                                          (sizeXM>0) ) 
140                                 {
141                                         memcpy( _imageoutput->GetScalarPointer(px,py,pz) , bbGetInputImageMove()->GetScalarPointer(spxM,j,k) , sizeXM );
142                                 }
143                    } // for j
144                 } // for k
145                 _imageoutput->Modified();
146         } else { // If Image Fixe Move the same GetScalarType
147                    printf ("ERROR: InversCrop  both ImageFixe and ImageMove need the same format.\n");
148                    printf ("       type ImageFix:%d   type ImageMove:%d\n", bbGetInputImageFix()->GetScalarType(), bbGetInputImageMove()->GetScalarType() );
149
150               }  
151             } else { // If Image Fixe Move != NULL
152                         printf ("ERROR: InversCrop  need ImageFixe and ImageMove to run.\n");
153            } 
154          bbSetOutputOut(_imageoutput);
155         } // if Active
156 }
157
158 //===== 
159 // 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)
160 //===== 
161 void InversCrop::bbUserSetDefaultValues()
162 {
163 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
164 //    Here we initialize the input 'In' to 0
165    bbSetInputActive(true);
166    bbSetInputType(0);
167    bbSetInputImageFix(NULL);
168    bbSetInputImageMove(NULL);
169    std::vector<int> origin;
170    origin.push_back(0);
171    origin.push_back(0);
172    origin.push_back(0);
173    bbSetInputOrigin(origin);
174    _imageoutput=NULL;
175 }
176 //===== 
177 // 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)
178 //===== 
179 void InversCrop::bbUserInitializeProcessing()
180 {
181
182 //  THE INITIALIZATION METHOD BODY :
183 //    Here does nothing 
184 //    but this is where you should allocate the internal/output pointers 
185 //    if any 
186
187   
188 }
189 //===== 
190 // 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)
191 //===== 
192 void InversCrop::bbUserFinalizeProcessing()
193 {
194
195 //  THE FINALIZATION METHOD BODY :
196 //    Here does nothing 
197 //    but this is where you should desallocate the internal/output pointers 
198 //    if any
199   
200 }
201 }
202 // EO namespace bbvtk
203
204