]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeFilter.cxx
c25e3e5bb8411b9c2b0be0849a7609223cf93bf2
[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                 
113                 
114                 unsigned int i;
115                 unsigned int nTv = 8;       // No. of surface elements for each tube vertex             
116                 
117                 // Create points and cells 
118                 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
119                 for(i = 0; i < lstPointX.size(); i++)
120                 {
121                         points->InsertPoint(i, lstPointX[i],  lstPointY[i],  lstPointZ[i]);
122                 }
123                 
124                 vtkSmartPointer<vtkCellArray> lines =   vtkSmartPointer<vtkCellArray>::New();
125                 lines->InsertNextCell( lstPointX.size() );
126                 for (i = 0; i < lstPointX.size(); i++)
127                 {
128                         lines->InsertCellPoint(i);
129                 }
130                 
131                 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
132                 polyData->SetPoints(points);
133                 polyData->SetLines(lines);
134                 
135                 double radio;
136                 // Varying tube radius using sine-function
137                 vtkSmartPointer<vtkDoubleArray> tubeRadius = vtkSmartPointer<vtkDoubleArray>::New();
138                 tubeRadius->SetName("TubeRadius");
139                 tubeRadius->SetNumberOfTuples( lstPointX.size() );
140                 for (i=0 ;i<lstPointX.size() ; i++)
141                 {
142                         if (lstRadius.size()==0)
143                         { 
144                            radio=1;
145                         } else if (i<lstRadius.size()) {
146                           radio = lstRadius[i];
147                         } else if (i>=lstRadius.size()) {
148                           radio= lstRadius[ lstRadius.size()-1 ];
149                         }   
150
151                         tubeRadius->SetTuple1(i, radio );
152                 }
153                 polyData->GetPointData()->AddArray(tubeRadius);
154                 polyData->GetPointData()->SetActiveScalars("TubeRadius");
155                 
156                 // RBG array (could add Alpha channel too I guess...)
157                 // Varying from blue to red
158                 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
159                 colors->SetName("Colors");
160                 colors->SetNumberOfComponents(3);
161                 colors->SetNumberOfTuples( lstPointX.size() );
162                 int numberOfColours = lstColour.size()/3;
163                 int indexcolour;
164                 for (i = 0; i < lstPointX.size() ;i++)
165                 {                       
166                         if (numberOfColours==0){
167                                 colors->InsertTuple3(i, 1 ,  1 ,  1 );
168                         } else {
169                                 if (i<numberOfColours){
170                                         indexcolour=i*3;
171                                 } else {
172                                         indexcolour=(numberOfColours-1)*3;
173                                 }
174                                 colors->InsertTuple3(i, 255*lstColour[indexcolour+0] ,  255*lstColour[indexcolour+1] ,  255*lstColour[indexcolour+2] );
175                         }
176                 } // for
177                 polyData->GetPointData()->AddArray(colors);
178                 
179                 vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
180                 tube->CappingOn();
181                 tube->SetInput(polyData);
182                 tube->SetNumberOfSides(nTv);
183                 tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
184                 
185                 vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
186                 mapper->SetInputConnection(tube->GetOutputPort());
187 //              mapper->ScalarVisibilityOn();
188 //              mapper->SetScalarModeToUsePointFieldData();
189                 
190                 mapper->ScalarVisibilityOn();
191                 
192 //              mapper->SetScalarModeToUseCellData();
193 //              mapper->SetScalarModeToUseCellFieldData();
194 //              mapper->SetScalarModeToUseFieldData();
195 //              mapper->SetScalarModeToUsePointData();
196                 mapper->SetScalarModeToUsePointFieldData();
197                 
198                 mapper->SetColorModeToDefault();
199 //              mapper->SetColorModeToMapScalars();             
200                 
201                 
202                 mapper->SelectColorArray("Colors");
203                 
204                 //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
205                 actor = vtkSmartPointer<vtkActor>::New();
206                 actor->SetMapper(mapper);
207                 actor->GetProperty()->SetOpacity( opacity );
208
209                 if ( transform!=NULL )
210                 {
211                         actor->SetUserTransform( transform );
212                 }
213                 
214                 // Interface Update
215                 if (renderer!=NULL )
216                 {
217                         renderer->AddActor(actor);
218                 }
219                                 
220         }
221         
222         
223         
224         
225 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TubeFilter)
226 BBTK_BLACK_BOX_IMPLEMENTATION(TubeFilter,bbtk::AtomicBlackBox);
227 //===== 
228 // 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)
229 //===== 
230 void TubeFilter::Process()
231 {
232
233 // THE MAIN PROCESSING METHOD BODY
234 //   Here we simply set the input 'In' value to the output 'Out'
235 //   And print out the output value
236 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
237 //    void bbSet{Input|Output}NAME(const TYPE&)
238 //    const TYPE& bbGet{Input|Output}NAME() const 
239 //    Where :
240 //    * NAME is the name of the input/output
241 //      (the one provided in the attribute 'name' of the tag 'input')
242 //    * TYPE is the C++ type of the input/output
243 //      (the one provided in the attribute 'type' of the tag 'input')
244         
245         printf("EED TubeFilter::Process start \n");
246
247         if (tubefilter!=NULL) 
248         { 
249                 delete tubefilter;
250         }
251         
252         tubefilter = new MaracasTubeFilter();
253         tubefilter->SetvtkRenderer( bbGetInputRenderer() );     
254         tubefilter->SetlstPoints( bbGetInputlstPointX(), bbGetInputlstPointY(), bbGetInputlstPointZ() );
255
256         // Sets the default value for radius to 1 everywhere, if the radius input vevctor is empty.
257         if (bbGetInputlstRadio().size() == 0)
258           {
259             std::vector<double> radio;
260             radio.resize(bbGetInputlstPointX().size(), 1.);
261             bbSetInputlstRadio(radio);
262           }
263         tubefilter->SetlstRadius( bbGetInputlstRadio() );
264         tubefilter->SetOpacity( bbGetInputOpacity() );
265         tubefilter->SetTransform( bbGetInputTransform() );      
266         tubefilter->SetlstColour( bbGetInputColour() ); 
267         tubefilter->Run();
268     bbSetOutputOutAxis( tubefilter->GetActor() );       
269         printf("EED TubeFilter::Process end \n");
270 }
271         
272 //===== 
273 // 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)
274 //===== 
275 void TubeFilter::bbUserSetDefaultValues()
276 {
277 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
278 //    Here we initialize the input 'In' to 0
279         bbSetInputOpacity(1);
280         bbSetInputTransform(NULL);
281         
282         std::vector<double> colour;
283         // 0- gray
284         colour.push_back(1.0);
285         colour.push_back(0.0);
286         colour.push_back(0.0);
287         bbSetInputColour(colour);
288
289         std::vector<double> lstRadius;
290         lstRadius.push_back(1);
291         bbSetInputColour(lstRadius);
292 }
293         
294 //===== 
295 // 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)
296 //===== 
297 void TubeFilter::bbUserInitializeProcessing()
298 {
299 //  THE INITIALIZATION METHOD BODY :
300 //    Here does nothing 
301 //    but this is where you should allocate the internal/output pointers 
302 //    if any    
303         tubefilter = NULL; 
304 }
305         
306 //===== 
307 // 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)
308 //===== 
309 void TubeFilter::bbUserFinalizeProcessing()
310 {
311 //  THE FINALIZATION METHOD BODY :
312 //    Here does nothing 
313 //    but this is where you should desallocate the internal/output pointers 
314 //    if any
315 }
316         
317 }
318 // EO namespace bbcreaMaracasVisu
319
320