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