]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeFilter.cxx
#2989 creaMaracasVisu Bug New Normal - ManualContourModel duplicate last point...
[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                 tube->SetInput(polyData);
180                 tube->SetNumberOfSides(nTv);
181                 tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
182                 
183                 vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
184                 mapper->SetInputConnection(tube->GetOutputPort());
185 //              mapper->ScalarVisibilityOn();
186 //              mapper->SetScalarModeToUsePointFieldData();
187                 
188                 mapper->ScalarVisibilityOn();
189                 
190 //              mapper->SetScalarModeToUseCellData();
191 //              mapper->SetScalarModeToUseCellFieldData();
192 //              mapper->SetScalarModeToUseFieldData();
193 //              mapper->SetScalarModeToUsePointData();
194                 mapper->SetScalarModeToUsePointFieldData();
195                 
196                 mapper->SetColorModeToDefault();
197 //              mapper->SetColorModeToMapScalars();             
198                 
199                 
200                 mapper->SelectColorArray("Colors");
201                 
202                 //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
203                 actor = vtkSmartPointer<vtkActor>::New();
204                 actor->SetMapper(mapper);
205                 actor->GetProperty()->SetOpacity( opacity );
206
207                 if ( transform!=NULL )
208                 {
209                         actor->SetUserTransform( transform );
210                 }
211                 
212                 // Interface Update
213                 if (renderer!=NULL )
214                 {
215                         renderer->AddActor(actor);
216                 }
217                                 
218         }
219         
220         
221         
222         
223 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TubeFilter)
224 BBTK_BLACK_BOX_IMPLEMENTATION(TubeFilter,bbtk::AtomicBlackBox);
225 //===== 
226 // 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)
227 //===== 
228 void TubeFilter::Process()
229 {
230
231 // THE MAIN PROCESSING METHOD BODY
232 //   Here we simply set the input 'In' value to the output 'Out'
233 //   And print out the output value
234 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
235 //    void bbSet{Input|Output}NAME(const TYPE&)
236 //    const TYPE& bbGet{Input|Output}NAME() const 
237 //    Where :
238 //    * NAME is the name of the input/output
239 //      (the one provided in the attribute 'name' of the tag 'input')
240 //    * TYPE is the C++ type of the input/output
241 //      (the one provided in the attribute 'type' of the tag 'input')
242         
243         if (tubefilter!=NULL) 
244         { 
245                 delete tubefilter;
246         }
247         
248         tubefilter = new MaracasTubeFilter();
249         tubefilter->SetvtkRenderer( bbGetInputRenderer() );     
250         tubefilter->SetlstPoints( bbGetInputlstPointX(), bbGetInputlstPointY(), bbGetInputlstPointZ() );
251
252         // Sets the default value for radius to 1 everywhere, if the radius input vevctor is empty.
253         if (bbGetInputlstRadio().size() == 0)
254           {
255             std::vector<double> radio;
256             radio.resize(bbGetInputlstPointX().size(), 1.);
257             bbSetInputlstRadio(radio);
258           }
259         tubefilter->SetlstRadius( bbGetInputlstRadio() );
260         tubefilter->SetOpacity( bbGetInputOpacity() );
261         tubefilter->SetTransform( bbGetInputTransform() );      
262         tubefilter->SetlstColour( bbGetInputColour() ); 
263         tubefilter->Run();
264     bbSetOutputOutAxis( tubefilter->GetActor() );       
265 }
266         
267 //===== 
268 // 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)
269 //===== 
270 void TubeFilter::bbUserSetDefaultValues()
271 {
272 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
273 //    Here we initialize the input 'In' to 0
274         bbSetInputOpacity(1);
275         bbSetInputTransform(NULL);
276         
277         std::vector<double> colour;
278         // 0- gray
279         colour.push_back(1.0);
280         colour.push_back(0.0);
281         colour.push_back(0.0);
282         bbSetInputColour(colour);
283
284         std::vector<double> lstRadius;
285         lstRadius.push_back(1);
286         bbSetInputColour(lstRadius);
287 }
288         
289 //===== 
290 // 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)
291 //===== 
292 void TubeFilter::bbUserInitializeProcessing()
293 {
294 //  THE INITIALIZATION METHOD BODY :
295 //    Here does nothing 
296 //    but this is where you should allocate the internal/output pointers 
297 //    if any    
298         tubefilter = NULL; 
299 }
300         
301 //===== 
302 // 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)
303 //===== 
304 void TubeFilter::bbUserFinalizeProcessing()
305 {
306 //  THE FINALIZATION METHOD BODY :
307 //    Here does nothing 
308 //    but this is where you should desallocate the internal/output pointers 
309 //    if any
310 }
311         
312 }
313 // EO namespace bbcreaMaracasVisu
314
315