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