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