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