]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSegmentationConnectivity.cxx
Clean code
[bbtk.git] / packages / vtk / src / bbvtkSegmentationConnectivity.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbvtkSegmentationConnectivity.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:58 $
33   Version:   $Revision: 1.8 $
34 =========================================================================*/
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40
41 #ifdef _USE_VTK_
42
43 #include "bbvtkSegmentationConnectivity.h"
44 #include "bbvtkPackage.h"
45
46 #include "bbtkStaticLecture.h"
47
48 namespace bbvtk
49 {
50   BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,SegmentationConnectivity);
51   BBTK_BLACK_BOX_IMPLEMENTATION(SegmentationConnectivity,
52                                 bbtk::AtomicBlackBox);
53
54 // --------------------------------------------------------------
55
56   void SegmentationConnectivity::bbUserSetDefaultValues() 
57   { 
58           thresh2       = NULL;
59 //        cast2         = NULL;
60           connect2      = NULL;
61           cast4         = NULL;
62           bbSetInputIn(NULL);
63           std::vector<int> position;
64           position.push_back(0);
65           position.push_back(0);
66           position.push_back(0);
67           bbSetInputPositionXYZ(position);
68           std::vector<int> threshold;
69           threshold.push_back(0);
70           threshold.push_back(1);
71           bbSetInputThresholdMinMax(threshold);
72           bbSetOutputOut(NULL);
73           bbSetInputOutputFormat("SAME");
74           bbSetInputValue(255);
75   }
76
77 // --------------------------------------------------------------       
78         void SegmentationConnectivity::bbUserInitializeProcessing()
79         {
80                 thresh2 = vtkImageThreshold::New();
81                 thresh2->SetInValue(255);
82                 thresh2->SetOutValue(0);
83                 thresh2->SetOutputScalarTypeToUnsignedChar();
84                 connect2 = vtkImageSeedConnectivity::New();
85 //EED 2017-01-01 Migration VTK7
86 #if VTK_MAJOR_VERSION <= 5
87                 connect2->SetInput(thresh2->GetOutput());
88 #else
89                 connect2->SetInputData(thresh2->GetOutput());
90 #endif
91                 connect2->SetInputConnectValue(255);
92                 connect2->SetOutputConnectedValue( 255 );
93                 connect2->SetOutputUnconnectedValue(0);
94                 cast4 = vtkImageCast::New();
95 //EED 2017-01-01 Migration VTK7
96 #if VTK_MAJOR_VERSION <= 5
97                 cast4->SetInput(connect2->GetOutput());
98 #else
99                 cast4->SetInputData(connect2->GetOutput());
100 #endif
101         }
102
103 // --------------------------------------------------------------               
104   void SegmentationConnectivity::bbUserFinalizeProcessing() 
105   { 
106           if (thresh2!=NULL)
107           {
108                   thresh2->Delete();
109                   thresh2=NULL;
110           }
111 //        if (cast2!=NULL)
112 //        {
113 //                cast2->Delete();
114 //                cast2=NULL;
115 //        }
116           if (connect2!=NULL)
117           {
118                   connect2->Delete();
119                   connect2=NULL;
120           }
121           if (cast4!=NULL)
122           {
123                   cast4->Delete();
124                   cast4=NULL;
125           }  
126   }
127
128 // --------------------------------------------------------------
129   void SegmentationConnectivity::DoProcess()
130   {
131       
132       printf("EED (%s) SegmentationConnectivity::DoProcess Start \n", bbGetFullName().c_str() );
133         int x,y,z;
134         int i,numPoints = bbGetInputPositionXYZ().size()/3;
135         int verif = (bbGetInputPositionXYZ().size()-1) % 3;
136         if (( numPoints>=1) && (verif==2) && (bbGetInputIn()!=NULL)) 
137         {
138         printf("EED  SegmentationConnectivity::DoProcess 1 format=%s %p\n", bbGetInputOutputFormat().c_str() , bbGetInputIn()  );
139         printf("EED  SegmentationConnectivity::DoProcess 1.1 \n" );
140
141 //EED 2020-01-18
142                 int outputformat = bbtkStaticLecture::GetTypeFormat( bbGetInputOutputFormat() , bbGetInputIn() );
143 //        int outputformat=4;
144         printf("EED  SegmentationConnectivity::DoProcess 2 \n" );
145                 vtkImageData *imagedata = bbGetInputIn();
146 //EED 2017-01-01 Migration VTK7
147 #if VTK_MAJOR_VERSION <= 5
148                 imagedata->UpdateInformation();
149                 imagedata->SetUpdateExtent(imagedata->GetWholeExtent());
150                 imagedata->Update();
151                 thresh2->SetInput(imagedata);
152 #else
153 //              imagedata->SetExtent(imagedata->GetExtent());
154                 thresh2->SetInputData(imagedata);
155 #endif
156         printf("EED SegmentationConnectivity::DoProcess 3  Threshold= %f %f\n", bbGetInputThresholdMinMax()[0], bbGetInputThresholdMinMax()[1] );
157                 thresh2->ThresholdBetween(bbGetInputThresholdMinMax()[0], bbGetInputThresholdMinMax()[1]);
158                 thresh2->Update();
159                 connect2->RemoveAllSeeds ();
160                 int ext[6];
161 //EED 2017-01-01 Migration VTK7
162 #if VTK_MAJOR_VERSION <= 5
163                 imagedata->GetWholeExtent(ext);
164 #else
165                 imagedata->GetExtent(ext);
166 #endif
167                 int maxX = ext[1]-ext[0]+1;
168                 int maxY = ext[3]-ext[2]+1;
169                 int maxZ = ext[5]-ext[4]+1;
170         printf("EED SegmentationConnectivity::DoProcess 4 numPoints=%d\n",numPoints );
171                 for (i=0;i<numPoints;i++)
172                 {
173                         x = bbGetInputPositionXYZ()[i*3+0];
174                         y = bbGetInputPositionXYZ()[i*3+1];
175                         z = bbGetInputPositionXYZ()[i*3+2];
176                         if ( (x>=0) || (x<maxX) || (y>=0) || (y<maxY) || (z>=0) || (z<maxZ) )
177                         {
178                 printf("EED  SegmentationConnectivity::DoProcess 4.1 xyz=%d %d %d\n",x,y,z );
179                                 connect2->AddSeed( x,y,z );
180                         } else {
181                                 printf("EED WARNNIG! In box: SegmentationConnectivity points list out of range \n");
182                         } // if point inside image 
183                 } // for i
184         printf("EED  SegmentationConnectivity::DoProcess 4.2 \n" );
185                 connect2->SetOutputConnectedValue( bbGetInputValue() );
186         printf("EED  SegmentationConnectivity::DoProcess 4.3 \n" );
187                 cast4->SetOutputScalarType( outputformat );
188         printf("EED  SegmentationConnectivity::DoProcess 4.5 \n" );
189                 connect2->Modified();
190         printf("EED  SegmentationConnectivity::DoProcess 5 \n" );
191                 connect2->Update();
192                 cast4->Update();                        
193         printf("EED SegmentationConnectivity::DoProcess 6 \n" );
194                 bbSetOutputOut(cast4->GetOutput() );
195         }  else {
196                 printf("EED WARNNIG! In box: SegmentationConnectivity List of points are not coherent. \n");
197                 bbSetOutputOut( NULL );
198         }// if numPoints verif
199       
200       printf("EED SegmentationConnectivity::DoProcess End \n" );
201
202 }
203   
204 }// EO namespace bbvtk
205
206 #endif //_USE_VTK_
207
208
209
210
211