]> 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->SetInput(polyData);
147                 tube->SetNumberOfSides(nTv);
148                 tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
149                 
150                 vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
151                 mapper->SetInputConnection(tube->GetOutputPort());
152                 mapper->ScalarVisibilityOn();
153                 mapper->SetScalarModeToUsePointFieldData();
154                 mapper->SelectColorArray("Colors");
155                 
156                 //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
157                 actor = vtkSmartPointer<vtkActor>::New();
158                 actor->SetMapper(mapper);
159                 actor->GetProperty()->SetOpacity( opacity );
160
161                 if ( transform!=NULL )
162                 {
163                         actor->SetUserTransform( transform );
164                 }
165                 
166                 // Interface Update
167                 if (renderer!=NULL )
168                 {
169                         renderer->AddActor(actor);
170                 }
171                                 
172         }
173         
174         
175         
176         
177 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TubeFilter)
178 BBTK_BLACK_BOX_IMPLEMENTATION(TubeFilter,bbtk::AtomicBlackBox);
179 //===== 
180 // 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)
181 //===== 
182 void TubeFilter::Process()
183 {
184
185 // THE MAIN PROCESSING METHOD BODY
186 //   Here we simply set the input 'In' value to the output 'Out'
187 //   And print out the output value
188 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
189 //    void bbSet{Input|Output}NAME(const TYPE&)
190 //    const TYPE& bbGet{Input|Output}NAME() const 
191 //    Where :
192 //    * NAME is the name of the input/output
193 //      (the one provided in the attribute 'name' of the tag 'input')
194 //    * TYPE is the C++ type of the input/output
195 //      (the one provided in the attribute 'type' of the tag 'input')
196         
197         printf("EED TubeFilter::Process start \n");
198
199         if (tubefilter!=NULL) 
200         { 
201                 delete tubefilter;
202         }
203         
204         tubefilter = new MaracasTubeFilter();
205         tubefilter->SetvtkRenderer( bbGetInputRenderer() );     
206         tubefilter->SetlstPoints( bbGetInputlstPointX(), bbGetInputlstPointY(), bbGetInputlstPointZ() );
207         tubefilter->SetlstRadius( bbGetInputlstRadio() );
208         tubefilter->SetOpacity( bbGetInputOpacity() );
209         tubefilter->SetTransform( bbGetInputTransform() );      
210         tubefilter->SetlstColour( bbGetInputColour() ); 
211         tubefilter->Run();
212     bbSetOutputOutAxis( tubefilter->GetActor() );       
213         printf("EED TubeFilter::Process end \n");
214 }
215         
216 //===== 
217 // 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)
218 //===== 
219 void TubeFilter::bbUserSetDefaultValues()
220 {
221 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
222 //    Here we initialize the input 'In' to 0
223         bbSetInputOpacity(1);
224         bbSetInputTransform(NULL);
225         
226         std::vector<double> colour;
227         // 0- gray
228         colour.push_back(1.0);
229         colour.push_back(0.0);
230         colour.push_back(0.0);
231         bbSetInputColour(colour);
232         
233         
234 }
235         
236 //===== 
237 // 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)
238 //===== 
239 void TubeFilter::bbUserInitializeProcessing()
240 {
241 //  THE INITIALIZATION METHOD BODY :
242 //    Here does nothing 
243 //    but this is where you should allocate the internal/output pointers 
244 //    if any    
245         tubefilter = NULL; 
246 }
247         
248 //===== 
249 // 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)
250 //===== 
251 void TubeFilter::bbUserFinalizeProcessing()
252 {
253 //  THE FINALIZATION METHOD BODY :
254 //    Here does nothing 
255 //    but this is where you should desallocate the internal/output pointers 
256 //    if any
257 }
258         
259 }
260 // EO namespace bbcreaMaracasVisu
261
262