]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeTreeFilter.cxx
2107 Bug Color Tube Axes visu in 3D
[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 void TubeTreeFilter::SetRadioTube(int iGeneral,int numPoints, int iTube)
219 {
220         int i;
221         double radiotmp=1;
222         int size=iGeneral+numPoints;
223         vtkDoubleArray *tubeRadius = vecTubeRadiosArray[iTube];
224
225         if (bbGetInputlstRadio().size()>=1) 
226         {
227            if (bbGetInputlstRadioLaw()==0)  // for All
228            {
229                 radiotmp=bbGetInputlstRadio()[0];
230            }  // Law 0  for All
231
232            if (bbGetInputlstRadioLaw()==1)  // by segment
233            {
234                if (bbGetInputlstRadio().size()>=iTube) 
235                {
236                    radiotmp=bbGetInputlstRadio()[iTube];
237                } else {
238                    radiotmp=bbGetInputlstRadio()[ bbGetInputlstRadio().size()-1 ];
239                }
240            }  // Law 1  by segment
241
242            for (i=iGeneral;i<size;i++)
243            {
244                 if (bbGetInputlstRadioLaw()==2)  // by point
245                 {
246                     if (bbGetInputlstRadio().size()>=i) 
247                     {
248                         radiotmp=bbGetInputlstRadio()[i];
249                     } else {
250                         radiotmp=bbGetInputlstRadio()[ bbGetInputlstRadio().size()-1 ]; 
251                     } 
252                 } // Law 2  by points
253                 tubeRadius->SetTuple1(i-iGeneral, radiotmp );
254            } // for
255         tubeRadius->Modified();
256          }  // bbGetInputlstRadio().size()>=1
257
258
259 }
260
261
262 void TubeTreeFilter::DrawOneTube(int iGeneral,int numPoints, int iTube)
263 {
264         int i;
265         unsigned int nTv = 8;       // No. of surface elements for each tube vertex
266
267         double spc[3];
268         //      img->GetSpacing(spc);
269         spc[0]=1;
270         spc[1]=1;
271         spc[2]=1;
272
273         int size=iGeneral+numPoints;
274
275         // Create points and cells
276         vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
277 //EED   vtkPoints *points = vtkPoints::New();
278         vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
279 //EED   vtkCellArray *lines = vtkCellArray::New();
280
281         lines->InsertNextCell( numPoints );
282
283         for (i=iGeneral;i<size;i++)
284         {
285                 points->InsertPoint(i-iGeneral, bbGetInputlstPointX()[i]*spc[0],
286                                                                 bbGetInputlstPointY()[i]*spc[1],
287                                                                 bbGetInputlstPointZ()[i]*spc[2] );
288                 lines->InsertCellPoint(i-iGeneral);
289         }
290
291         vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
292 //EED   vtkPolyData *polyData = vtkPolyData::New();
293         polyData->SetPoints(points);
294         polyData->SetLines(lines);
295
296
297         // Varying tube radius using sine-function
298         vtkSmartPointer<vtkDoubleArray> tubeRadius = vtkSmartPointer<vtkDoubleArray>::New();
299 //EED   vtkDoubleArray *tubeRadius = vtkDoubleArray::New();
300         tubeRadius->SetName("TubeRadius");
301         tubeRadius->SetNumberOfTuples( numPoints );
302         vecTubeRadiosArray.push_back( tubeRadius );
303
304
305         polyData->GetPointData()->AddArray(tubeRadius);
306         polyData->GetPointData()->SetActiveScalars("TubeRadius");
307
308
309
310 // Adding spheres at the start and end of the tube
311
312         // Sphere start
313         i=iGeneral;
314         vtkSphereSource *vtksphereStart         = vtkSphereSource::New();
315         vtksphereStart->SetThetaResolution (10);
316         vtksphereStart->SetPhiResolution (10);
317         vtksphereStart->SetRadius( bbGetInputlstRadio()[i]  );
318         vtkPolyDataMapper *sphereMapperStart    = vtkPolyDataMapper::New();
319         sphereMapperStart->SetInput( vtksphereStart->GetOutput() );
320         vtkActor *sphereActorStart      = vtkActor::New();
321         sphereActorStart->SetMapper(sphereMapperStart);
322         sphereActorStart->SetOrigin( 0,0,0 );
323         sphereActorStart->SetPosition(bbGetInputlstPointX()[i]*spc[0], bbGetInputlstPointY()[i]*spc[1], bbGetInputlstPointZ()[i]*spc[2] );
324
325         // Sphere end
326         i=size-1;
327         vtkSphereSource *vtksphereEnd           = vtkSphereSource::New();
328         vtksphereEnd->SetThetaResolution (10);
329         vtksphereEnd->SetPhiResolution (10);
330         vtksphereEnd->SetRadius( bbGetInputlstRadio()[i]  );
331         vtkPolyDataMapper *sphereMapperEnd      = vtkPolyDataMapper::New();
332         sphereMapperEnd->SetInput( vtksphereEnd->GetOutput() );
333         vtkActor *sphereActorEnd        = vtkActor::New();
334         sphereActorEnd->SetMapper(sphereMapperEnd);
335         sphereActorEnd->SetOrigin( 0,0,0 );
336         sphereActorEnd->SetPosition(bbGetInputlstPointX()[i]*spc[0], bbGetInputlstPointY()[i]*spc[1], bbGetInputlstPointZ()[i]*spc[2] );
337
338         vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
339         tube->SetInput(polyData);
340         tube->SetNumberOfSides(nTv);
341         tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
342         
343         vtkLookupTableEED* vLutEED                               = vtkLookupTableEED::New();
344
345         vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
346         mapper->SetInputConnection(tube->GetOutputPort());
347         mapper->ScalarVisibilityOn();
348         
349 // EED fev 02/ 20112    
350         mapper->SetScalarModeToUsePointFieldData();     
351         mapper->SetLookupTable( vLutEED );
352         mapper->SelectColorArray( "points_axe" );
353         
354         //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
355     vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
356         actor->SetMapper(mapper);
357
358     vecVtkActors.push_back( actor );
359     vecVtkActors.push_back( sphereActorEnd );
360     vecVtkActors.push_back( sphereActorStart );
361
362         // Interface Update    // Interface Update
363      if  (bbGetInputRenderer()!=NULL )
364      {
365             bbGetInputRenderer()->AddActor( actor );
366             bbGetInputRenderer()->AddActor( sphereActorStart );
367             bbGetInputRenderer()->AddActor( sphereActorEnd );
368      }
369 }
370
371
372 void TubeTreeFilter::SetGraphicProperties()
373 {
374     int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
375
376     int iGeneral=0;
377     int numPoints;
378
379     vtkActor *actorTube;
380     vtkActor *actorSpherEnd;
381     vtkActor *actorSpherStart;
382
383
384     double r,g,b;
385     r = bbGetInputColour()[0];
386     g = bbGetInputColour()[1];
387     b = bbGetInputColour()[2];
388     for (iTube=0 ; iTube<sizeLstAxis; iTube++)
389     {
390         // Get Actor
391         actorTube=vecVtkActors[0+iTube*3];
392         actorSpherEnd=vecVtkActors[1+iTube*3];
393         actorSpherStart=vecVtkActors[2+iTube*3];
394
395         // Set Opacity
396         actorTube->GetProperty()->SetOpacity( bbGetInputOpacity() );
397         actorSpherEnd->GetProperty()->SetOpacity( bbGetInputOpacity() );
398         actorSpherStart->GetProperty()->SetOpacity( bbGetInputOpacity() );
399
400         // Set Transform
401         if ( bbGetInputTransform()!=NULL )
402         {
403             actorTube->SetUserTransform( bbGetInputTransform() );
404             actorSpherEnd->SetUserTransform( bbGetInputTransform() );
405             actorSpherStart->SetUserTransform( bbGetInputTransform() );
406         }
407
408         //Set Colour
409         if (bbGetInputColourLaw()==1)  // One solide colour
410         {
411             //
412         }
413
414         if (bbGetInputColourLaw()==2)  // color for differents tubes
415         {
416             if ( (iTube*3+1) < (int)(bbGetInputColour().size()) )
417             {
418                 r = bbGetInputColour()[0+iTube*3];
419                 g = bbGetInputColour()[1+iTube*3];
420                 b = bbGetInputColour()[2+iTube*3];
421             } else {
422                 r = (rand() % 100) / 100.0;
423                 g = (rand() % 100) / 100.0;
424                 b = (rand() % 100) / 100.0;
425             }
426         }
427
428         actorTube->GetProperty()->SetColor( r,g,b );
429         actorSpherEnd->GetProperty()->SetColor( r,g,b );
430         actorSpherStart->GetProperty()->SetColor( r,g,b );
431
432
433 // Setting radio
434          numPoints = bbGetInputlstIndexs()[iTube];
435          SetRadioTube(iGeneral,numPoints,iTube);
436          iGeneral = iGeneral+numPoints;
437
438     } // for
439 }
440
441 //=====
442 // 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)
443 //=====
444 void TubeTreeFilter::Process()
445 {
446
447         printf("EED TubeTreeFilter::Process start \n");
448
449         int iGeneral=0;
450         int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
451         int iActor,sizeActors = vecVtkActors.size();
452
453         int numPoints;
454     if (oldLstSize!=sizeLstAxis)
455     {
456         oldLstSize=sizeLstAxis;
457         for (iActor=0 ; iActor<sizeActors; iActor++)
458         {
459             if (bbGetInputRenderer()!=NULL )
460             {
461                 bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
462     //EED                       vecVtkPolyData[iActor]->Delete();
463     //EED                       vecVtkPolyDataMaper[iActor]->Delete();
464     //EED                       vecVtkActors[iActor]->Delete();
465             }
466         }
467
468     //EED       vecVtkPolyData.clear();
469     //EED       vecVtkPolyDataMaper.clear();
470         vecVtkActors.clear();
471         vecTubeRadiosArray.clear();
472
473         for ( iTube=0 ; iTube<sizeLstAxis ; iTube++)
474         {
475             numPoints = bbGetInputlstIndexs()[iTube];
476             DrawOneTube(iGeneral,numPoints,iTube);
477             iGeneral = iGeneral+numPoints;
478         }
479
480         if ( bbGetInputiTube()*3 <= (int)(vecVtkActors.size()-1) )
481         {
482             bbSetOutputOutTube( vecVtkActors[ bbGetInputiTube()*3 ] );
483         } else  {
484                 printf("TubeTreeFilter .ERROR. missing index vector...\n");
485         }// vector Actor size
486     } // oldLstSize
487     SetGraphicProperties();
488
489         printf("EED TubeTreeFilter::Process end \n");
490 }
491 //=====
492 // 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)
493 //=====
494 void TubeTreeFilter::bbUserSetDefaultValues()
495 {
496     oldLstSize=-1;
497
498 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
499 //    Here we initialize the input 'In' to 0
500         bbSetInputiTube(0);
501         std::vector<double> colour;
502         // 0- gray
503         colour.push_back(0.5);
504         colour.push_back(0.5);
505         colour.push_back(0.5);
506         // 1- red
507         colour.push_back(1.0);
508         colour.push_back(0.0);
509         colour.push_back(0.0);
510         // 2- blue
511         colour.push_back(0.0);
512         colour.push_back(0.0);
513         colour.push_back(1.0);
514         // 3- yellow
515         colour.push_back(1.0);
516         colour.push_back(1.0);
517         colour.push_back(0.0);
518         // 4- green
519         colour.push_back(0.0);
520         colour.push_back(1.0);
521         colour.push_back(0.0);
522         // 5-
523         colour.push_back(0.0);
524         colour.push_back(1.0);
525         colour.push_back(1.0);
526
527         // 6-
528         colour.push_back(0.5);
529         colour.push_back(0.5);
530         colour.push_back(0.5);
531
532         bbSetInputColour(colour);
533         bbSetInputTransform(NULL);
534         bbSetInputRenderer(NULL);
535     bbSetInputColourLaw(1);
536     bbSetInputOpacity(1);
537
538    std::vector<double> radio;
539         radio.push_back(1);
540         bbSetInputlstRadio( radio );
541         bbSetInputlstRadioLaw( 0 );  // for all
542
543 }
544 //=====
545 // 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)
546 //=====
547 void TubeTreeFilter::bbUserInitializeProcessing()
548 {
549
550 //  THE INITIALIZATION METHOD BODY :
551 //    Here does nothing
552 //    but this is where you should allocate the internal/output pointers
553 //    if any
554
555
556 }
557 //=====
558 // 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)
559 //=====
560 void TubeTreeFilter::bbUserFinalizeProcessing()
561 {
562
563 //  THE FINALIZATION METHOD BODY :
564 //    Here does nothing
565 //    but this is where you should desallocate the internal/output pointers
566 //    if any
567
568 }
569 }
570 // EO namespace bbcreaMaracasVisu
571
572