]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeTreeFilter.cxx
8f2a3ce5b96a44cd26d1a401feb3b34d085db377
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuTubeTreeFilter.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 "bbcreaMaracasVisuTubeTreeFilter.h"
5 #include "bbcreaMaracasVisuPackage.h"
6
7 #include <vtkSmartPointer.h>
8 #include <vtkDoubleArray.h>
9 #include <vtkPolyData.h>
10 #include <vtkPointData.h>
11 #include <vtkTubeFilter.h>
12 #include <vtkSphereSource.h>
13
14
15 namespace bbcreaMaracasVisu
16 {
17
18 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TubeTreeFilter)
19 BBTK_BLACK_BOX_IMPLEMENTATION(TubeTreeFilter,bbtk::AtomicBlackBox);
20
21
22
23 void TubeTreeFilter::DrawOneTube(int iGeneral,int numPoints, int iTube)
24 {
25         int i;
26         unsigned int nTv = 8;       // No. of surface elements for each tube vertex
27
28         double spc[3];
29         //      img->GetSpacing(spc);
30         spc[0]=1;
31         spc[1]=1;
32         spc[2]=1;
33
34         int size=iGeneral+numPoints;
35
36         // Create points and cells
37         vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
38 //EED   vtkPoints *points = vtkPoints::New();
39         vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
40 //EED   vtkCellArray *lines = vtkCellArray::New();
41
42         lines->InsertNextCell( numPoints );
43
44         for (i=iGeneral;i<size;i++)
45         {
46                 points->InsertPoint(i-iGeneral, bbGetInputlstPointX()[i]*spc[0],
47                                                                 bbGetInputlstPointY()[i]*spc[1],
48                                                                 bbGetInputlstPointZ()[i]*spc[2] );
49                 lines->InsertCellPoint(i-iGeneral);
50         }
51
52         vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
53 //EED   vtkPolyData *polyData = vtkPolyData::New();
54         polyData->SetPoints(points);
55         polyData->SetLines(lines);
56
57
58         // Varying tube radius using sine-function
59         vtkSmartPointer<vtkDoubleArray> tubeRadius = vtkSmartPointer<vtkDoubleArray>::New();
60 //EED   vtkDoubleArray *tubeRadius = vtkDoubleArray::New();
61         tubeRadius->SetName("TubeRadius");
62         tubeRadius->SetNumberOfTuples( numPoints );
63         for (i=iGeneral;i<size;i++)
64         {
65                 tubeRadius->SetTuple1(i-iGeneral, bbGetInputlstRadio()[i] );
66         }
67         polyData->GetPointData()->AddArray(tubeRadius);
68         polyData->GetPointData()->SetActiveScalars("TubeRadius");
69
70
71
72 // Adding spheres at the start and end of the tube
73
74         // Sphere start
75         i=iGeneral;
76         vtkSphereSource *vtksphereStart         = vtkSphereSource::New();
77         vtksphereStart->SetThetaResolution (10);
78         vtksphereStart->SetPhiResolution (10);
79         vtksphereStart->SetRadius( bbGetInputlstRadio()[i]  );
80         vtkPolyDataMapper *sphereMapperStart    = vtkPolyDataMapper::New();
81         sphereMapperStart->SetInput( vtksphereStart->GetOutput() );
82         vtkActor *sphereActorStart      = vtkActor::New();
83         sphereActorStart->SetMapper(sphereMapperStart);
84         sphereActorStart->SetOrigin( 0,0,0 );
85         sphereActorStart->SetPosition(bbGetInputlstPointX()[i]*spc[0], bbGetInputlstPointY()[i]*spc[1], bbGetInputlstPointZ()[i]*spc[2] );
86
87         // Sphere end
88         i=size-1;
89         vtkSphereSource *vtksphereEnd           = vtkSphereSource::New();
90         vtksphereEnd->SetThetaResolution (10);
91         vtksphereEnd->SetPhiResolution (10);
92         vtksphereEnd->SetRadius( bbGetInputlstRadio()[i]  );
93         vtkPolyDataMapper *sphereMapperEnd      = vtkPolyDataMapper::New();
94         sphereMapperEnd->SetInput( vtksphereEnd->GetOutput() );
95         vtkActor *sphereActorEnd        = vtkActor::New();
96         sphereActorEnd->SetMapper(sphereMapperEnd);
97         sphereActorEnd->SetOrigin( 0,0,0 );
98         sphereActorEnd->SetPosition(bbGetInputlstPointX()[i]*spc[0], bbGetInputlstPointY()[i]*spc[1], bbGetInputlstPointZ()[i]*spc[2] );
99
100         vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
101 //EED   vtkTubeFilter *tube = vtkTubeFilter::New();
102         tube->SetInput(polyData);
103         tube->SetNumberOfSides(nTv);
104         tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
105
106         vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
107 //EED   vtkPolyDataMapper *mapper =     vtkPolyDataMapper::New();
108         mapper->SetInputConnection(tube->GetOutputPort());
109         mapper->ScalarVisibilityOn();
110         mapper->SetScalarModeToUsePointFieldData();
111
112         //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
113     vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
114         actor->SetMapper(mapper);
115
116     vecVtkActors.push_back( actor );
117     vecVtkActors.push_back( sphereActorEnd );
118     vecVtkActors.push_back( sphereActorStart );
119
120         // Interface Update    // Interface Update
121      if  (bbGetInputRenderer()!=NULL )
122      {
123             bbGetInputRenderer()->AddActor( actor );
124             bbGetInputRenderer()->AddActor( sphereActorStart );
125             bbGetInputRenderer()->AddActor( sphereActorEnd );
126      }
127 }
128
129
130 void TubeTreeFilter::SetGraphicProperties()
131 {
132         int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
133
134     vtkActor *actorTube;
135     vtkActor *actorSpherEnd;
136     vtkActor *actorSpherStart;
137
138
139     double r,g,b;
140     r = bbGetInputColour()[0];
141     g = bbGetInputColour()[1];
142     b = bbGetInputColour()[2];
143     for (iTube=0 ; iTube<sizeLstAxis; iTube++)
144     {
145         // Get Actor
146         actorTube=vecVtkActors[0+iTube*3];
147         actorSpherEnd=vecVtkActors[1+iTube*3];
148         actorSpherStart=vecVtkActors[2+iTube*3];
149
150         // Set Opacity
151         actorTube->GetProperty()->SetOpacity( bbGetInputOpacity() );
152         actorSpherEnd->GetProperty()->SetOpacity( bbGetInputOpacity() );
153         actorSpherStart->GetProperty()->SetOpacity( bbGetInputOpacity() );
154
155         //Set Colour
156         if ( bbGetInputTransform()!=NULL )
157         {
158             actorTube->SetUserTransform( bbGetInputTransform() );
159             actorSpherEnd->SetUserTransform( bbGetInputTransform() );
160             actorSpherStart->SetUserTransform( bbGetInputTransform() );
161         }
162
163         if (bbGetInputColourLaw()==1)  // One solide colour
164         {
165             //
166         }
167
168         if (bbGetInputColourLaw()==2)  // color for differents tubes
169         {
170             if ( (iTube*3+1) < (int)(bbGetInputColour().size()) )
171             {
172                 r = bbGetInputColour()[0+iTube*3];
173                 g = bbGetInputColour()[1+iTube*3];
174                 b = bbGetInputColour()[2+iTube*3];
175             } else {
176                 r = (rand() % 100) / 100.0;
177                 g = (rand() % 100) / 100.0;
178                 b = (rand() % 100) / 100.0;
179             }
180         }
181
182         actorTube->GetProperty()->SetColor( r,g,b );
183         actorSpherEnd->GetProperty()->SetColor( r,g,b );
184         actorSpherStart->GetProperty()->SetColor( r,g,b );
185     } // for
186 }
187
188 //=====
189 // 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)
190 //=====
191 void TubeTreeFilter::Process()
192 {
193
194         printf("EED TubeTreeFilter::Process start \n");
195
196         int iGeneral=0;
197         int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
198         int iActor,sizeActors = vecVtkActors.size();
199
200         int numPoints;
201     if (oldLstSize!=sizeLstAxis)
202     {
203         oldLstSize=sizeLstAxis;
204         for (iActor=0 ; iActor<sizeActors; iActor++)
205         {
206             if (bbGetInputRenderer()!=NULL )
207             {
208                 bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
209     //EED                       vecVtkPolyData[iActor]->Delete();
210     //EED                       vecVtkPolyDataMaper[iActor]->Delete();
211     //EED                       vecVtkActors[iActor]->Delete();
212             }
213         }
214
215     //EED       vecVtkPolyData.clear();
216     //EED       vecVtkPolyDataMaper.clear();
217         vecVtkActors.clear();
218
219         for ( iTube=0 ; iTube<sizeLstAxis ; iTube++)
220         {
221             numPoints = bbGetInputlstIndexs()[iTube];
222             DrawOneTube(iGeneral,numPoints,iTube);
223             iGeneral = iGeneral+numPoints;
224         }
225
226         if ( bbGetInputiTube()*3 <= (int)(vecVtkActors.size()-1) )
227         {
228             bbSetOutputOutTube( vecVtkActors[ bbGetInputiTube()*3 ] );
229         } else  {
230                 printf("TubeTreeFilter .ERROR. missing index vector...\n");
231         }// vector Actor size
232     }
233     SetGraphicProperties();
234
235         printf("EED TubeTreeFilter::Process end \n");
236 }
237 //=====
238 // 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)
239 //=====
240 void TubeTreeFilter::bbUserSetDefaultValues()
241 {
242     oldLstSize=-1;
243
244 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
245 //    Here we initialize the input 'In' to 0
246         bbSetInputiTube(0);
247         std::vector<double> colour;
248         // 0- gray
249         colour.push_back(0.5);
250         colour.push_back(0.5);
251         colour.push_back(0.5);
252         // 1- red
253         colour.push_back(1.0);
254         colour.push_back(0.0);
255         colour.push_back(0.0);
256         // 2- blue
257         colour.push_back(0.0);
258         colour.push_back(0.0);
259         colour.push_back(1.0);
260         // 3- yellow
261         colour.push_back(1.0);
262         colour.push_back(1.0);
263         colour.push_back(0.0);
264         // 4- green
265         colour.push_back(0.0);
266         colour.push_back(1.0);
267         colour.push_back(0.0);
268         // 5-
269         colour.push_back(0.0);
270         colour.push_back(1.0);
271         colour.push_back(1.0);
272
273         // 6-
274         colour.push_back(0.5);
275         colour.push_back(0.5);
276         colour.push_back(0.5);
277
278         bbSetInputColour(colour);
279         bbSetInputTransform(NULL);
280         bbSetInputRenderer(NULL);
281     bbSetInputColourLaw(1);
282     bbSetInputOpacity(1);
283
284 }
285 //=====
286 // 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)
287 //=====
288 void TubeTreeFilter::bbUserInitializeProcessing()
289 {
290
291 //  THE INITIALIZATION METHOD BODY :
292 //    Here does nothing
293 //    but this is where you should allocate the internal/output pointers
294 //    if any
295
296
297 }
298 //=====
299 // 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)
300 //=====
301 void TubeTreeFilter::bbUserFinalizeProcessing()
302 {
303
304 //  THE FINALIZATION METHOD BODY :
305 //    Here does nothing
306 //    but this is where you should desallocate the internal/output pointers
307 //    if any
308
309 }
310 }
311 // EO namespace bbcreaMaracasVisu
312
313