]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuTubeTreeFilter.cxx
2116 creaMaracasVisu Bug New Normal Erase Start End Sphere of the tubes
[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         tube->SetInput(polyData);
311         tube->SetNumberOfSides(nTv);
312         tube->SetVaryRadiusToVaryRadiusByAbsoluteScalar();
313         tube->CappingOn();
314         
315         vtkLookupTableEED* vLutEED                               = vtkLookupTableEED::New();
316
317         vtkSmartPointer<vtkPolyDataMapper> mapper =     vtkSmartPointer<vtkPolyDataMapper>::New();
318         mapper->SetInputConnection(tube->GetOutputPort());
319         mapper->ScalarVisibilityOn();
320         
321 // EED fev 02/ 20112    
322         mapper->SetScalarModeToUsePointFieldData();     
323         mapper->SetLookupTable( vLutEED );
324         mapper->SelectColorArray( "points_axe" );
325         
326         //              vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();
327     vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
328         actor->SetMapper(mapper);
329
330     vecVtkActors.push_back( actor );
331
332         // Interface Update    // Interface Update
333      if  (bbGetInputRenderer()!=NULL )
334      {
335             bbGetInputRenderer()->AddActor( actor );
336      }
337 }
338
339
340 void TubeTreeFilter::SetGraphicProperties()
341 {
342     int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
343
344     int iGeneral=0;
345     int numPoints;
346
347     vtkActor *actorTube;
348
349     double r,g,b;
350     r = bbGetInputColour()[0];
351     g = bbGetInputColour()[1];
352     b = bbGetInputColour()[2];
353     for (iTube=0 ; iTube<sizeLstAxis; iTube++)
354     {
355         // Get Actor
356
357         actorTube=vecVtkActors[iTube];
358
359         // Set Opacity
360         actorTube->GetProperty()->SetOpacity( bbGetInputOpacity() );
361
362         // Set Transform
363         if ( bbGetInputTransform()!=NULL )
364         {
365             actorTube->SetUserTransform( bbGetInputTransform() );
366         }
367
368         //Set Colour
369         if (bbGetInputColourLaw()==1)  // One solide colour
370         {
371             //
372         }
373
374         if (bbGetInputColourLaw()==2)  // color for differents tubes
375         {
376             if ( (iTube*3+1) < (int)(bbGetInputColour().size()) )
377             {
378                 r = bbGetInputColour()[0+iTube*3];
379                 g = bbGetInputColour()[1+iTube*3];
380                 b = bbGetInputColour()[2+iTube*3];
381             } else {
382                 r = (rand() % 100) / 100.0;
383                 g = (rand() % 100) / 100.0;
384                 b = (rand() % 100) / 100.0;
385             }
386         }
387
388         actorTube->GetProperty()->SetColor( r,g,b );
389
390
391 // Setting radio
392          numPoints = bbGetInputlstIndexs()[iTube];
393          SetRadioTube(iGeneral,numPoints,iTube);
394          iGeneral = iGeneral+numPoints;
395
396     } // for
397 }
398
399 //=====
400 // 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)
401 //=====
402 void TubeTreeFilter::Process()
403 {
404
405         printf("EED TubeTreeFilter::Process start \n");
406
407         int iGeneral=0;
408         int iTube,sizeLstAxis=bbGetInputlstIndexs().size();
409         int iActor,sizeActors = vecVtkActors.size();
410
411         int numPoints;
412     if (oldLstSize!=sizeLstAxis)
413     {
414         oldLstSize=sizeLstAxis;
415         for (iActor=0 ; iActor<sizeActors; iActor++)
416         {
417             if (bbGetInputRenderer()!=NULL )
418             {
419                 bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
420     //EED                       vecVtkPolyData[iActor]->Delete();
421     //EED                       vecVtkPolyDataMaper[iActor]->Delete();
422     //EED                       vecVtkActors[iActor]->Delete();
423             }
424         }
425
426     //EED       vecVtkPolyData.clear();
427     //EED       vecVtkPolyDataMaper.clear();
428         vecVtkActors.clear();
429         vecTubeRadiosArray.clear();
430
431         for ( iTube=0 ; iTube<sizeLstAxis ; iTube++)
432         {
433             numPoints = bbGetInputlstIndexs()[iTube];
434             DrawOneTube(iGeneral,numPoints,iTube);
435             iGeneral = iGeneral+numPoints;
436         }
437
438         if ( bbGetInputiTube() <= (int)(vecVtkActors.size()-1) )
439         {
440             bbSetOutputOutTube( vecVtkActors[ bbGetInputiTube() ] );
441         } else  {
442                 printf("TubeTreeFilter .ERROR. missing index vector...\n");
443         }// vector Actor size
444
445     } // oldLstSize
446     SetGraphicProperties();
447
448         printf("EED TubeTreeFilter::Process end \n");
449 }
450 //=====
451 // 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)
452 //=====
453 void TubeTreeFilter::bbUserSetDefaultValues()
454 {
455     oldLstSize=-1;
456
457 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
458 //    Here we initialize the input 'In' to 0
459         bbSetInputiTube(0);
460         std::vector<double> colour;
461         // 0- gray
462         colour.push_back(0.5);
463         colour.push_back(0.5);
464         colour.push_back(0.5);
465         // 1- red
466         colour.push_back(1.0);
467         colour.push_back(0.0);
468         colour.push_back(0.0);
469         // 2- blue
470         colour.push_back(0.0);
471         colour.push_back(0.0);
472         colour.push_back(1.0);
473         // 3- yellow
474         colour.push_back(1.0);
475         colour.push_back(1.0);
476         colour.push_back(0.0);
477         // 4- green
478         colour.push_back(0.0);
479         colour.push_back(1.0);
480         colour.push_back(0.0);
481         // 5-
482         colour.push_back(0.0);
483         colour.push_back(1.0);
484         colour.push_back(1.0);
485
486         // 6-
487         colour.push_back(0.5);
488         colour.push_back(0.5);
489         colour.push_back(0.5);
490
491         bbSetInputColour(colour);
492         bbSetInputTransform(NULL);
493         bbSetInputRenderer(NULL);
494     bbSetInputColourLaw(1);
495     bbSetInputOpacity(1);
496
497    std::vector<double> radio;
498         radio.push_back(1);
499         bbSetInputlstRadio( radio );
500         bbSetInputlstRadioLaw( 0 );  // for all
501
502 }
503 //=====
504 // 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)
505 //=====
506 void TubeTreeFilter::bbUserInitializeProcessing()
507 {
508
509 //  THE INITIALIZATION METHOD BODY :
510 //    Here does nothing
511 //    but this is where you should allocate the internal/output pointers
512 //    if any
513
514
515 }
516 //=====
517 // 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)
518 //=====
519 void TubeTreeFilter::bbUserFinalizeProcessing()
520 {
521
522 //  THE FINALIZATION METHOD BODY :
523 //    Here does nothing
524 //    but this is where you should desallocate the internal/output pointers
525 //    if any
526
527 }
528 }
529 // EO namespace bbcreaMaracasVisu
530
531