]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuAxeVolume.cxx
#3413 creaMaracasVisu Feature New Normal - openmp and Transparent mechanism in...
[creaMaracasVisu.git] / bbtk / src / bbmaracasvisuAxeVolume.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "bbmaracasvisuAxeVolume.h"
27 #include "bbcreaMaracasVisuPackage.h"
28
29 #include "creaVtk_MACROS.h"
30
31 namespace bbcreaMaracasVisu
32 {
33
34
35 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,AxeVolume)
36 BBTK_BLACK_BOX_IMPLEMENTATION(AxeVolume,bbtk::AtomicBlackBox);
37
38 int AxeVolume::GetTypeFormat( std::string formatStr , vtkImageData* image )
39 {
40         int outputformat = VTK_UNSIGNED_CHAR;
41         if (formatStr=="SAME")
42         {                                               
43                 if (image!=NULL) outputformat = image->GetScalarType();
44         }
45         else if (formatStr=="VTK_BIT")                          outputformat = VTK_BIT;                         // 1
46         else if (formatStr=="VTK_CHAR")                         outputformat = VTK_CHAR;                        // 2
47         else if (formatStr=="VTK_SIGNED_CHAR")          outputformat = VTK_SIGNED_CHAR;         // 15
48         else if (formatStr=="VTK_UNSIGNED_CHAR")        outputformat = VTK_UNSIGNED_CHAR;       // 3
49         else if (formatStr=="VTK_SHORT")                        outputformat = VTK_SHORT;                       // 4
50         else if (formatStr=="VTK_UNSIGNED_SHORT")       outputformat = VTK_UNSIGNED_SHORT;      // 5
51         else if (formatStr=="VTK_INT")                          outputformat = VTK_INT;                 // 6
52         else if (formatStr=="VTK_UNSIGNED_INT")         outputformat = VTK_UNSIGNED_INT;        // 7
53         else if (formatStr=="VTK_LONG")                         outputformat = VTK_LONG;                // 8  
54         else if (formatStr=="VTK_UNSIGNED_LONG")        outputformat = VTK_UNSIGNED_LONG;       // 9
55         else if (formatStr=="VTK_FLOAT")                        outputformat = VTK_FLOAT;               // 10
56         else if (formatStr=="VTK_DOUBLE")                       outputformat = VTK_DOUBLE;              // 11 
57         else if (formatStr=="MET_CHAR")                         outputformat = VTK_CHAR;                        // 2
58         else if (formatStr=="MET_UCHAR")                        outputformat = VTK_UNSIGNED_CHAR;       // 3
59         else if (formatStr=="MET_SHORT")                        outputformat = VTK_SHORT;                       // 4
60         else if (formatStr=="MET_USHORT")                       outputformat = VTK_UNSIGNED_SHORT;      // 5
61         else if (formatStr=="MET_SHORT")                        outputformat = VTK_SHORT;               // 4
62         else if (formatStr=="MET_FLOAT")                        outputformat = VTK_FLOAT;               // 10
63         else if (formatStr=="MET_DOUBLE")                       outputformat = VTK_DOUBLE;              // 11  
64
65     return outputformat;
66 }
67
68
69 void AxeVolume::Process()
70 {
71         if (bbGetInputIn()!=NULL)
72         {
73                 if ( mimage!=NULL )
74                 {
75                         mimage->Delete();
76                 }
77
78                 int ext[6];
79                 bbGetInputIn()->GetExtent(ext);
80                 int sizeX=ext[1]-ext[0]+1;
81                 int sizeY=ext[3]-ext[2]+1;
82                 int sizeZ=ext[5]-ext[4]+1;
83
84                 double spc[3];
85                 bbGetInputIn()->GetSpacing(spc);
86                 double invSpc[3];
87                 invSpc[0] = 1/spc[0];
88                 invSpc[1] = 1/spc[1];
89                 invSpc[2] = 1/spc[2];
90
91                 int outputformat = GetTypeFormat( bbGetInputOutputFormat() , bbGetInputIn() );
92
93                 mimage = vtkImageData::New();
94                 mimage->SetSpacing(bbGetInputIn()->GetSpacing());
95                 mimage->SetDimensions(bbGetInputIn()->GetDimensions());
96                 mimage->SetExtent(bbGetInputIn()->GetExtent());
97                 mimage->SetOrigin(bbGetInputIn()->GetOrigin());
98         //EED 2017-01-01 Migration VTK7
99         #if VTK_MAJOR_VERSION <= 5
100                 mimage->SetScalarType( outputformat );
101                 mimage->AllocateScalars();
102         #else
103                 mimage->AllocateScalars( outputformat , 1);
104         #endif
105
106                 int sizeLstPointR       = bbGetInputlstPointR().size();
107                 int iAxe,sizeAxe        = bbGetInputlstPointX().size();
108                 int ii;
109                 int sizeImage           = sizeX*sizeY*sizeZ;
110         //      unsigned short *p;
111
112                 // Clean image
113                 memset( (char*)mimage->GetScalarPointer(0,0,0) , 0, sizeImage*(mimage->GetScalarSize()) );
114
115         //      p = (unsigned short*)mimage->GetScalarPointer (0, 0, 0);
116         //      for ( ii=0 ; ii<sizeImage ; ii++)
117         //      {
118         //              *p = 0;
119         //              p++;
120         //      }
121
122
123         DEF_POINTER_IMAGE_VTK_CREA(vI,ssI,pI,stI,mimage)
124
125
126         int k1omp=(double)(sizeAxe-1)*0.0;
127         int k2omp=(double)(sizeAxe-1)*0.2;
128         int k3omp=(double)(sizeAxe-1)*0.4;
129         int k4omp=(double)(sizeAxe-1)*0.6;
130         int k5omp=(double)(sizeAxe-1)*0.8;
131         int k6omp=(double)(sizeAxe-1)*1.0;
132
133         printf("EED AxeVolume::Process (with openmp)\n");
134
135         #pragma omp parallel for 
136                 for (iAxe=0 ; iAxe<sizeAxe; iAxe++)
137                  {
138         if ( (k1omp==iAxe) || (k2omp==iAxe) || (k3omp==iAxe) || 
139                  (k4omp==iAxe) || (k5omp==iAxe) || (k6omp==iAxe) ) { printf("  %d%\n", (int)(((double)iAxe/(double)(sizeAxe-1))*100 )); }
140                         int i,j,k;
141                         double rx,ry,rz;
142                         double r,rr;
143                         double px,py,pz;
144                         double px1,py1,pz1;
145                         double px2,py2,pz2;
146                         double vItmpOMP;
147
148                         if (iAxe<sizeLstPointR)
149                         {
150                                 r = bbGetInputlstPointR()[ iAxe ]* invSpc[0];
151                         } else {
152                                 if (bbGetInputlstPointR().size()>=1)
153                                 {
154                                         r = bbGetInputlstPointR()[ bbGetInputlstPointR().size()-1 ]    * invSpc[0];
155                                 } else {
156                                         r = 1;
157                                 }
158                         } // if iAxe sizeLstPointR
159                         px = bbGetInputlstPointX()[iAxe] * invSpc[0];
160                         py = bbGetInputlstPointY()[iAxe] * invSpc[1];
161                         pz = bbGetInputlstPointZ()[iAxe] * invSpc[2];
162                         px1 = px - r;
163                         py1 = py - r;
164                         pz1 = pz - r;
165                         px2 = px + r;
166                         py2 = py + r;
167                         pz2 = pz + r;
168                         rr=r*r;
169
170                         long int index;
171                         for ( i=px1 ; i<=px2 ; i++ )
172                         {
173                                 rx      =       i - px;
174                                 rx      =       rx*rx;
175                                 for ( j=py1 ; j<py2 ; j++ )
176                                 {
177                                         ry      =       j - py;
178                                         ry      =       ry*ry;
179                                         for ( k=pz1 ; k<pz2 ; k++ )
180                                         {
181                                                 if ( (i>=0) && (i<sizeX) && (j>=0) && (j<sizeY) &&(k>=0) && (k<sizeZ) )
182                                                 {
183         //                                              p = (unsigned short*)mimage->GetScalarPointer (i, j, k);
184         //                                              if (*p==0)
185                                                         index=i+j*sizeX+k*sizeX*sizeY;
186                                                         GETVALUE2_VTK_CREA(vItmpOMP,pI,stI,index)
187         // EED2020-04-25                                                if ( mimage->GetScalarComponentAsDouble(i,j,k,0)==0 )
188                                                         if ( vI==0 )
189                                                         {
190                                                                 rz      =       k - pz;
191                                                                 rz      =       rz*rz;
192                                                                 if ( rx + ry + rz <= rr )
193                                                                 {
194         //                                                              *p=255;
195                                                                         vItmpOMP=bbGetInputValue();
196                                                                         SETVALUE2_VTK_CREA(vItmpOMP,pI,stI,index)
197         // EED2020-04-25                                                                mimage->SetScalarComponentFromDouble (i,j,k,0, bbGetInputValue() );
198                                                                 }
199                                                         } // *p==0
200                                                 } // if inside point
201                                         } //for k
202                                 } //for j
203                         } //for i
204                 } // for iAxe
205                 bbSetOutputOut( mimage );
206         } else  {
207                 printf("\n");
208                 printf("EED Warnning!!  AxeVolume::Process  The Input In is not set.\n");
209                 printf("\n");
210         }// if bbGetInputIn()
211 }
212
213
214         //-----------------------------------------------------------------
215         void AxeVolume::bbUserSetDefaultValues()
216         {
217                 mimage=NULL;
218                 bbSetInputOutputFormat("SAME");
219                 bbSetInputValue(255);
220         }
221
222         //-----------------------------------------------------------------
223         void AxeVolume::bbUserInitializeProcessing()
224         {
225         }
226
227         //-----------------------------------------------------------------
228         void AxeVolume::bbUserFinalizeProcessing()
229         {
230         }
231
232         //-----------------------------------------------------------------
233
234 }
235 // EO namespace bbcreaMaracasVisu
236
237