]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeFilter.cxx
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuTubeFilter.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 //===== 
27 // 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)
28 //===== 
29 #include "bbcreaMaracasVisuTubeFilter.h"
30 #include "bbcreaMaracasVisuPackage.h"
31
32
33 #include <vtkPolyData.h>
34 #include <vtkPoints.h>
35 #include <vtkCellArray.h>
36 #include <vtkDoubleArray.h>
37 #include <vtkPolyData.h>
38 #include <vtkPointData.h>
39
40 #include <vtkCell.h>
41 #include <vtkCellData.h>
42 #include <vtkDataSet.h>
43 #include <vtkDataSetAttributes.h>
44 #include <vtkProperty.h>
45 #include <vtkTubeFilter.h>
46
47 #include <vtkDataSetMapper.h>
48 #include <vtkPolyDataMapper.h>
49
50
51 namespace bbcreaMaracasVisu
52 {
53
54         MaracasTubeFilter::MaracasTubeFilter()
55         {
56         }
57
58         MaracasTubeFilter::~MaracasTubeFilter()
59         {
60                 // Interface Update
61                 if (renderer!=NULL )
62                 {
63                         renderer->RemoveActor(actor);
64                 }
65                 
66         }
67         
68
69         void MaracasTubeFilter::SetvtkRenderer(vtkRenderer *render)
70         {
71                 this->renderer = render;
72         }
73         
74
75         
76         void MaracasTubeFilter::SetlstPoints( std::vector<double> lstPointX , std::vector<double> lstPointY , std::vector<double> lstPointZ )
77         {
78                 this->lstPointX = lstPointX;
79                 this->lstPointY = lstPointY;
80                 this->lstPointZ = lstPointZ;
81         }
82         
83         
84         void MaracasTubeFilter::SetlstColour( std::vector<double> lstColour  )
85         {
86                 this->lstColour=lstColour;
87         }
88
89         
90         void MaracasTubeFilter::SetlstRadius( std::vector<double> lstRadius  )
91         {
92                 this->lstRadius = lstRadius;
93         }
94         
95         void MaracasTubeFilter::SetOpacity(double opacity)
96         {
97                 this->opacity = opacity;
98         }
99         
100         void MaracasTubeFilter::SetTransform( vtkLinearTransform* transform  )
101         {
102                 this->transform = transform;
103         }
104
105         vtkActor *MaracasTubeFilter::GetActor()
106         {
107                 return actor;
108         }
109         
110         void MaracasTubeFilter::Run()
111         {
112                 unsigned int i;
113                 unsigned int nTv = 8;       // No. of surface elements for each tube vertex             
114                 
115                 // Create points and cells 
116                 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
117                 for(i = 0; i < lstPointX.size(); i++)
118                 {
119                         points->InsertPoint(i, lstPointX[i],  lstPointY[i],  lstPointZ[i]);
120                 }
121                 
122                 vtkSmartPointer<vtkCellArray> lines =   vtkSmartPointer<vtkCellArray>::New();
123                 lines->InsertNextCell( lstPointX.size() );
124                 for (i = 0; i < lstPointX.size(); i++)
125                 {
126                         lines->InsertCellPoint(i);
127                 }
128                 
129                 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
130                 polyData->SetPoints(points);
131                 polyData->SetLines(lines);
132                 
133                 double radio;
134                 // Varying tube radius using sine-function
135                 vtkSmartPointer<vtkDoubleArray> tubeRadius = vtkSmartPointer<vtkDoubleArray>::New();
136                 tubeRadius->SetName("TubeRadius");
137                 tubeRadius->SetNumberOfTuples( lstPointX.size() );
138                 for (i=0 ;i<lstPointX.size() ; i++)
139                 {
140                         if (lstRadius.size()==0)
141                         { 
142                            radio=1;
143                         } else if (i<lstRadius.size()) {
144                           radio = lstRadius[i];
145                         } else if (i>=lstRadius.size()) {
146                           radio= lstRadius[ lstRadius.size()-1 ];
147                         }   
148
149                         tubeRadius->SetTuple1(i, radio );
150                 }
151                 polyData->GetPointData()->AddArray(tubeRadius);
152                 polyData->GetPointData()->SetActiveScalars("TubeRadius");
153                 
154                 // RBG array (could add Alpha channel too I guess...)
155                 // Varying from blue to red
156                 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
157                 colors->SetName("Colors");
158                 colors->SetNumberOfComponents(3);
159                 colors->SetNumberOfTuples( lstPointX.size() );
160                 int numberOfColours = lstColour.size()/3;
161                 int indexcolour;
162                 for (i = 0; i < lstPointX.size() ;i++)
163                 {                       
164                         if (numberOfColours==0){
165                                 colors->InsertTuple3(i, 1 ,  1 ,  1 );
166                         } else {
167                                 if (i<numberOfColours){
168                                         indexcolour=i*3;
169                                 } else {
170                                         indexcolour=(numberOfColours-1)*3;
171                                 }
172                                 colors->InsertTuple3(i, 255*lstColour[indexcolour+0] ,  255*lstColour[indexcolour+1] ,  255*lstColour[indexcolour+2] );
173                         }
174                 } // for
175                 polyData->GetPointData()->AddArray(colors);
176                 
177                 vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
178                 tube->CappingOn();
179
180 //EED 2017-01-01 Migration VTK7
181 #if VTK_MAJOR_VERSION <= 5
182                 tube->SetInput(polyData);
183 #else
184                 tube->SetInputData(polyData);
185 #endif
186
187                 tube->SetNumberOfSides(nTv);
188                 tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
189                 
190                 vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
191                 mapper->SetInputConnection(tube->GetOutputPort());
192 //              mapper->ScalarVisibilityOn();
193 //              mapper->SetScalarModeToUsePointFieldData();
194                 
195                 mapper->ScalarVisibilityOn();
196                 
197 //              mapper->SetScalarModeToUseCellData();
198 //              mapper->SetScalarModeToUseCellFieldData();
199 //              mapper->SetScalarModeToUseFieldData();
200 //              mapper->SetScalarModeToUsePointData();
201                 mapper->SetScalarModeToUsePointFieldData();
202                 
203                 mapper->SetColorModeToDefault();
204 //              mapper->SetColorModeToMapScalars();             
205                 
206                 
207                 mapper->SelectColorArray("Colors");
208                 
209                 //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
210                 actor = vtkSmartPointer<vtkActor>::New();
211                 actor->SetMapper(mapper);
212                 actor->GetProperty()->SetOpacity( opacity );
213
214                 if ( transform!=NULL )
215                 {
216                         actor->SetUserTransform( transform );
217                 }
218                 
219                 // Interface Update
220                 if (renderer!=NULL )
221                 {
222                         renderer->AddActor(actor);
223                 }
224                                 
225         }
226         
227         
228         
229         
230 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TubeFilter)
231 BBTK_BLACK_BOX_IMPLEMENTATION(TubeFilter,bbtk::AtomicBlackBox);
232 //===== 
233 // 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)
234 //===== 
235 void TubeFilter::Process()
236 {
237
238 // THE MAIN PROCESSING METHOD BODY
239 //   Here we simply set the input 'In' value to the output 'Out'
240 //   And print out the output value
241 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
242 //    void bbSet{Input|Output}NAME(const TYPE&)
243 //    const TYPE& bbGet{Input|Output}NAME() const 
244 //    Where :
245 //    * NAME is the name of the input/output
246 //      (the one provided in the attribute 'name' of the tag 'input')
247 //    * TYPE is the C++ type of the input/output
248 //      (the one provided in the attribute 'type' of the tag 'input')
249         
250         if (tubefilter!=NULL) 
251         { 
252                 delete tubefilter;
253         }
254         
255         tubefilter = new MaracasTubeFilter();
256         tubefilter->SetvtkRenderer( bbGetInputRenderer() );     
257         tubefilter->SetlstPoints( bbGetInputlstPointX(), bbGetInputlstPointY(), bbGetInputlstPointZ() );
258
259         // Sets the default value for radius to 1 everywhere, if the radius input vevctor is empty.
260         if (bbGetInputlstRadio().size() == 0)
261           {
262             std::vector<double> radio;
263             radio.resize(bbGetInputlstPointX().size(), 1.);
264             bbSetInputlstRadio(radio);
265           }
266         tubefilter->SetlstRadius( bbGetInputlstRadio() );
267         tubefilter->SetOpacity( bbGetInputOpacity() );
268         tubefilter->SetTransform( bbGetInputTransform() );      
269         tubefilter->SetlstColour( bbGetInputColour() ); 
270         tubefilter->Run();
271     bbSetOutputOutAxis( tubefilter->GetActor() );       
272 }
273         
274 //===== 
275 // 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)
276 //===== 
277 void TubeFilter::bbUserSetDefaultValues()
278 {
279 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
280 //    Here we initialize the input 'In' to 0
281         bbSetInputOpacity(1);
282         bbSetInputTransform(NULL);
283         
284         std::vector<double> colour;
285         // 0- gray
286         colour.push_back(1.0);
287         colour.push_back(0.0);
288         colour.push_back(0.0);
289         bbSetInputColour(colour);
290
291         std::vector<double> lstRadius;
292         lstRadius.push_back(1);
293         bbSetInputColour(lstRadius);
294 }
295         
296 //===== 
297 // 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)
298 //===== 
299 void TubeFilter::bbUserInitializeProcessing()
300 {
301 //  THE INITIALIZATION METHOD BODY :
302 //    Here does nothing 
303 //    but this is where you should allocate the internal/output pointers 
304 //    if any    
305         tubefilter = NULL; 
306 }
307         
308 //===== 
309 // 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)
310 //===== 
311 void TubeFilter::bbUserFinalizeProcessing()
312 {
313 //  THE FINALIZATION METHOD BODY :
314 //    Here does nothing 
315 //    but this is where you should desallocate the internal/output pointers 
316 //    if any
317 }
318         
319 }
320 // EO namespace bbcreaMaracasVisu
321
322