]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeTreeFilter.cxx
f431595c9a0219e8dfaa7f353ec610c69b953359
[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 #include "vtkObjectFactory.h"
14
15
16         
17         vtkStandardNewMacro(vtkLookupTableEED);
18         
19         // Construct with range=(0,1); and hsv ranges set up for rainbow color table 
20         // (from red to blue).
21         vtkLookupTableEED::vtkLookupTableEED(int sze, int ext)
22         {
23                 this->TableRange[0] = 0.0;
24                 this->TableRange[1] = 1.0;
25         }
26         
27         //----------------------------------------------------------------------------
28         vtkLookupTableEED::~vtkLookupTableEED()
29         {
30         }
31         
32         
33         
34         unsigned char *vtkLookupTableEED::MapValue(double v)
35         {
36                 
37                 //int idx = this->GetIndex(v);
38                 //return (this->Table->GetPointer(0) + 4*idx);
39                 
40                 return 0;
41         }
42         
43         void vtkLookupTableEED::GetColor(double v, double rgb[3])
44         {
45                 //      unsigned char *rgb8 = this->MapValue(v);
46                 //      rgb[0] = rgb8[0]/255.0;
47                 //      rgb[1] = rgb8[1]/255.0;
48                 //      rgb[2] = rgb8[2]/255.0;
49                 
50                 rgb[0] = 1;
51                 rgb[1] = 1;
52                 rgb[2] = 0;
53         }
54         
55         
56         void vtkLookupTableEED::SetTableRange(double r[2])
57         {
58                 this->SetTableRange(r[0],r[1]);
59         }
60         
61         //----------------------------------------------------------------------------
62         // Set the minimum/maximum scalar values for scalar mapping. Scalar values
63         // less than minimum range value are clamped to minimum range value.
64         // Scalar values greater than maximum range value are clamped to maximum
65         // range value.
66         void vtkLookupTableEED::SetTableRange(double rmin, double rmax)
67         {
68                 if (rmax < rmin)
69                 {
70                         vtkErrorMacro("Bad table range: ["<<rmin<<", "<<rmax<<"]");
71                         return;
72                 }
73                 
74                 if (this->TableRange[0] == rmin && this->TableRange[1] == rmax)
75                 {
76                         return;
77                 }
78                 
79                 this->TableRange[0] = rmin;
80                 this->TableRange[1] = rmax;
81                 
82                 this->Modified();
83         }
84         
85         
86         
87         //----------------------------------------------------------------------------
88         // Although this is a relatively expensive calculation,
89         // it is only done on the first render. Colors are cached
90         // for subsequent renders.
91         template<class T>
92         void vtkLookupTableMapDirVectorEED(vtkLookupTableEED *self, T *input, 
93                                                                            unsigned char *output, int length, 
94                                                                            int inIncr, int outFormat)
95         {
96                 double tmp,sum;
97                 //      double *mag;
98                 int i, j;
99                 double dirx,diry,dirz;
100                 
101                 
102                 printf("EED length %d  %p\n", length,input);
103                 //      mag = new double[length];
104                 for (i = 0; i < length; ++i)
105                 {
106                         dirx    = 0;
107                         diry    = 0;
108                         dirz    = 0;
109                         sum             = 0;
110                         for (j = 0; j < inIncr; ++j)
111                         {
112                                 if (j==0) dirx= static_cast<double>(*input);  
113                                 if (j==1) diry= static_cast<double>(*input);  
114                                 if (j==2) dirz= static_cast<double>(*input);  
115                                 tmp = static_cast<double>(*input);  
116                                 sum += (tmp * tmp);
117                                 ++input;
118                         }
119                         sum=sqrt(sum);
120                         *output++ = (unsigned char) abs( (255*dirx/sum) );
121                         *output++ = (unsigned char) abs( (255*diry/sum) );
122                         *output++ = (unsigned char) abs( (255*dirz/sum) );
123                         *output++ = 255;
124                         //              printf("%d %d %d   ",(int)(255*dirx/sum),(int)(255*diry/sum),(int)(255*dirz/sum));
125                 }
126                 
127                 //      vtkLookupTableMapData(self, mag, output, length, 1, outFormat);
128                 
129                 //      delete [] mag;
130         }
131         
132         
133         
134         //----------------------------------------------------------------------------
135         void vtkLookupTableEED::MapScalarsThroughTable2(void *input, 
136                                                                                                         unsigned char *output,
137                                                                                                         int inputDataType, 
138                                                                                                         int numberOfValues,
139                                                                                                         int inputIncrement,
140                                                                                                         int outputFormat)
141         {
142                 
143                 printf("vtkLookupTableEED::MapScalarsThroughTable2 inputIncrement=%d   inputDataType=%d\n",inputIncrement,inputDataType);
144                 
145                 
146                 //      if (this->UseMagnitude && inputIncrement > 1)
147                 //  {
148                 switch (inputDataType)
149                 {
150                                 vtkTemplateMacro(
151                                                                  vtkLookupTableMapDirVectorEED(this,static_cast<VTK_TT*>(input),output,
152                                                                                                                            numberOfValues,inputIncrement,outputFormat);
153                                                                  return
154                                                                  );                             
155                         case VTK_BIT:
156                                 vtkErrorMacro("Cannot comput magnitude of bit array.");
157                                 break;
158                         default:
159                                 vtkErrorMacro(<< "MapImageThroughTable: Unknown input ScalarType");
160                 }
161                 //  }
162                 
163         }  
164         
165         
166         //----------------------------------------------------------------------------
167         void vtkLookupTableEED::PrintSelf(ostream& os, vtkIndent indent)
168         {
169                 this->Superclass::PrintSelf(os,indent); 
170         }
171         
172         
173         
174         
175         //----------------------------------
176         //----------------------------------
177         //----------------------------------
178         //----------------------------------
179         //----------------------------------
180         
181         
182         
183 namespace bbcreaMaracasVisu
184 {
185         
186         
187
188         
189 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,TubeTreeFilter)
190 BBTK_BLACK_BOX_IMPLEMENTATION(TubeTreeFilter,bbtk::AtomicBlackBox);
191
192
193
194 void TubeTreeFilter::DrawOneTube(int iGeneral,int numPoints, int iTube)
195 {
196         int i;
197         unsigned int nTv = 8;       // No. of surface elements for each tube vertex
198
199         double spc[3];
200         //      img->GetSpacing(spc);
201         spc[0]=1;
202         spc[1]=1;
203         spc[2]=1;
204
205         int size=iGeneral+numPoints;
206
207         // Create points and cells
208         vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
209 //EED   vtkPoints *points = vtkPoints::New();
210         vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
211 //EED   vtkCellArray *lines = vtkCellArray::New();
212
213         lines->InsertNextCell( numPoints );
214
215         for (i=iGeneral;i<size;i++)
216         {
217                 points->InsertPoint(i-iGeneral, bbGetInputlstPointX()[i]*spc[0],
218                                                                 bbGetInputlstPointY()[i]*spc[1],
219                                                                 bbGetInputlstPointZ()[i]*spc[2] );
220                 lines->InsertCellPoint(i-iGeneral);
221         }
222
223         vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
224 //EED   vtkPolyData *polyData = vtkPolyData::New();
225         polyData->SetPoints(points);
226         polyData->SetLines(lines);
227
228
229         // Varying tube radius using sine-function
230         vtkSmartPointer<vtkDoubleArray> tubeRadius = vtkSmartPointer<vtkDoubleArray>::New();
231 //EED   vtkDoubleArray *tubeRadius = vtkDoubleArray::New();
232         tubeRadius->SetName("TubeRadius");
233         tubeRadius->SetNumberOfTuples( numPoints );
234         for (i=iGeneral;i<size;i++)
235         {
236                 tubeRadius->SetTuple1(i-iGeneral, bbGetInputlstRadio()[i] );
237         }
238         polyData->GetPointData()->AddArray(tubeRadius);
239         polyData->GetPointData()->SetActiveScalars("TubeRadius");
240
241
242
243 // Adding spheres at the start and end of the tube
244
245         // Sphere start
246         i=iGeneral;
247         vtkSphereSource *vtksphereStart         = vtkSphereSource::New();
248         vtksphereStart->SetThetaResolution (10);
249         vtksphereStart->SetPhiResolution (10);
250         vtksphereStart->SetRadius( bbGetInputlstRadio()[i]  );
251         vtkPolyDataMapper *sphereMapperStart    = vtkPolyDataMapper::New();
252         sphereMapperStart->SetInput( vtksphereStart->GetOutput() );
253         vtkActor *sphereActorStart      = vtkActor::New();
254         sphereActorStart->SetMapper(sphereMapperStart);
255         sphereActorStart->SetOrigin( 0,0,0 );
256         sphereActorStart->SetPosition(bbGetInputlstPointX()[i]*spc[0], bbGetInputlstPointY()[i]*spc[1], bbGetInputlstPointZ()[i]*spc[2] );
257
258         // Sphere end
259         i=size-1;
260         vtkSphereSource *vtksphereEnd           = vtkSphereSource::New();
261         vtksphereEnd->SetThetaResolution (10);
262         vtksphereEnd->SetPhiResolution (10);
263         vtksphereEnd->SetRadius( bbGetInputlstRadio()[i]  );
264         vtkPolyDataMapper *sphereMapperEnd      = vtkPolyDataMapper::New();
265         sphereMapperEnd->SetInput( vtksphereEnd->GetOutput() );
266         vtkActor *sphereActorEnd        = vtkActor::New();
267         sphereActorEnd->SetMapper(sphereMapperEnd);
268         sphereActorEnd->SetOrigin( 0,0,0 );
269         sphereActorEnd->SetPosition(bbGetInputlstPointX()[i]*spc[0], bbGetInputlstPointY()[i]*spc[1], bbGetInputlstPointZ()[i]*spc[2] );
270
271         vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
272         tube->SetInput(polyData);
273         tube->SetNumberOfSides(nTv);
274         tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
275         
276         vtkLookupTableEED* vLutEED                               = vtkLookupTableEED::New();
277
278         vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
279         mapper->SetInputConnection(tube->GetOutputPort());
280         mapper->ScalarVisibilityOn();
281         
282 // EED fev 02/ 20112    
283         mapper->SetScalarModeToUsePointFieldData();     
284         mapper->SetLookupTable( vLutEED );
285         mapper->SelectColorArray( "points_axe" );
286         
287         //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
288     vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
289         actor->SetMapper(mapper);
290
291     vecVtkActors.push_back( actor );
292     vecVtkActors.push_back( sphereActorEnd );
293     vecVtkActors.push_back( sphereActorStart );
294
295         // Interface Update    // Interface Update
296      if  (bbGetInputRenderer()!=NULL )
297      {
298             bbGetInputRenderer()->AddActor( actor );
299             bbGetInputRenderer()->AddActor( sphereActorStart );
300             bbGetInputRenderer()->AddActor( sphereActorEnd );
301      }
302 }
303
304
305 void TubeTreeFilter::SetGraphicProperties()
306 {
307         int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
308
309     vtkActor *actorTube;
310     vtkActor *actorSpherEnd;
311     vtkActor *actorSpherStart;
312
313
314     double r,g,b;
315     r = bbGetInputColour()[0];
316     g = bbGetInputColour()[1];
317     b = bbGetInputColour()[2];
318     for (iTube=0 ; iTube<sizeLstAxis; iTube++)
319     {
320         // Get Actor
321         actorTube=vecVtkActors[0+iTube*3];
322         actorSpherEnd=vecVtkActors[1+iTube*3];
323         actorSpherStart=vecVtkActors[2+iTube*3];
324
325         // Set Opacity
326         actorTube->GetProperty()->SetOpacity( bbGetInputOpacity() );
327         actorSpherEnd->GetProperty()->SetOpacity( bbGetInputOpacity() );
328         actorSpherStart->GetProperty()->SetOpacity( bbGetInputOpacity() );
329
330         //Set Colour
331         if ( bbGetInputTransform()!=NULL )
332         {
333             actorTube->SetUserTransform( bbGetInputTransform() );
334             actorSpherEnd->SetUserTransform( bbGetInputTransform() );
335             actorSpherStart->SetUserTransform( bbGetInputTransform() );
336         }
337
338         if (bbGetInputColourLaw()==1)  // One solide colour
339         {
340             //
341         }
342
343         if (bbGetInputColourLaw()==2)  // color for differents tubes
344         {
345             if ( (iTube*3+1) < (int)(bbGetInputColour().size()) )
346             {
347                 r = bbGetInputColour()[0+iTube*3];
348                 g = bbGetInputColour()[1+iTube*3];
349                 b = bbGetInputColour()[2+iTube*3];
350             } else {
351                 r = (rand() % 100) / 100.0;
352                 g = (rand() % 100) / 100.0;
353                 b = (rand() % 100) / 100.0;
354             }
355         }
356
357         actorTube->GetProperty()->SetColor( r,g,b );
358         actorSpherEnd->GetProperty()->SetColor( r,g,b );
359         actorSpherStart->GetProperty()->SetColor( r,g,b );
360     } // for
361 }
362
363 //=====
364 // 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)
365 //=====
366 void TubeTreeFilter::Process()
367 {
368
369         printf("EED TubeTreeFilter::Process start \n");
370
371         int iGeneral=0;
372         int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
373         int iActor,sizeActors = vecVtkActors.size();
374
375         int numPoints;
376     if (oldLstSize!=sizeLstAxis)
377     {
378         oldLstSize=sizeLstAxis;
379         for (iActor=0 ; iActor<sizeActors; iActor++)
380         {
381             if (bbGetInputRenderer()!=NULL )
382             {
383                 bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
384     //EED                       vecVtkPolyData[iActor]->Delete();
385     //EED                       vecVtkPolyDataMaper[iActor]->Delete();
386     //EED                       vecVtkActors[iActor]->Delete();
387             }
388         }
389
390     //EED       vecVtkPolyData.clear();
391     //EED       vecVtkPolyDataMaper.clear();
392         vecVtkActors.clear();
393
394         for ( iTube=0 ; iTube<sizeLstAxis ; iTube++)
395         {
396             numPoints = bbGetInputlstIndexs()[iTube];
397             DrawOneTube(iGeneral,numPoints,iTube);
398             iGeneral = iGeneral+numPoints;
399         }
400
401         if ( bbGetInputiTube()*3 <= (int)(vecVtkActors.size()-1) )
402         {
403             bbSetOutputOutTube( vecVtkActors[ bbGetInputiTube()*3 ] );
404         } else  {
405                 printf("TubeTreeFilter .ERROR. missing index vector...\n");
406         }// vector Actor size
407     }
408     SetGraphicProperties();
409
410         printf("EED TubeTreeFilter::Process end \n");
411 }
412 //=====
413 // 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)
414 //=====
415 void TubeTreeFilter::bbUserSetDefaultValues()
416 {
417     oldLstSize=-1;
418
419 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
420 //    Here we initialize the input 'In' to 0
421         bbSetInputiTube(0);
422         std::vector<double> colour;
423         // 0- gray
424         colour.push_back(0.5);
425         colour.push_back(0.5);
426         colour.push_back(0.5);
427         // 1- red
428         colour.push_back(1.0);
429         colour.push_back(0.0);
430         colour.push_back(0.0);
431         // 2- blue
432         colour.push_back(0.0);
433         colour.push_back(0.0);
434         colour.push_back(1.0);
435         // 3- yellow
436         colour.push_back(1.0);
437         colour.push_back(1.0);
438         colour.push_back(0.0);
439         // 4- green
440         colour.push_back(0.0);
441         colour.push_back(1.0);
442         colour.push_back(0.0);
443         // 5-
444         colour.push_back(0.0);
445         colour.push_back(1.0);
446         colour.push_back(1.0);
447
448         // 6-
449         colour.push_back(0.5);
450         colour.push_back(0.5);
451         colour.push_back(0.5);
452
453         bbSetInputColour(colour);
454         bbSetInputTransform(NULL);
455         bbSetInputRenderer(NULL);
456     bbSetInputColourLaw(1);
457     bbSetInputOpacity(1);
458
459 }
460 //=====
461 // 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)
462 //=====
463 void TubeTreeFilter::bbUserInitializeProcessing()
464 {
465
466 //  THE INITIALIZATION METHOD BODY :
467 //    Here does nothing
468 //    but this is where you should allocate the internal/output pointers
469 //    if any
470
471
472 }
473 //=====
474 // 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)
475 //=====
476 void TubeTreeFilter::bbUserFinalizeProcessing()
477 {
478
479 //  THE FINALIZATION METHOD BODY :
480 //    Here does nothing
481 //    but this is where you should desallocate the internal/output pointers
482 //    if any
483
484 }
485 }
486 // EO namespace bbcreaMaracasVisu
487
488