]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuDrawAxisTree3D.cxx
#3476 Clean code
[creaMaracasVisu.git] / bbtk / src / bbmaracasvisuDrawAxisTree3D.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 #include "bbmaracasvisuDrawAxisTree3D.h"
27 #include "bbcreaMaracasVisuPackage.h"
28 #include "vtkLinearTransform.h"
29
30 #include "vtkUnsignedCharArray.h"
31 #include "vtkCellData.h"
32 #include "vtkPolyLine.h"
33 #include "vtkLine.h"
34
35
36
37 namespace bbcreaMaracasVisu
38 {
39
40 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,DrawAxisTree3D)
41 BBTK_BLACK_BOX_IMPLEMENTATION(DrawAxisTree3D,bbtk::AtomicBlackBox);
42
43
44
45 void DrawAxisTree3D::DrawOneAxis(int iGeneral,int numPoints, int iAxis)
46 {
47 //EED printf("8.1 ");
48         vtkPolyData                     *polydata               = vtkPolyData::New( );
49         vtkPolyDataMapper       *polydatamapper = vtkPolyDataMapper::New();
50         vtkActor                        *vtkactor               = vtkActor::New();
51
52 //      polydatamapper->ScalarVisibilityOff();
53 //EED 2017-01-01 Migration VTK7
54 #if VTK_MAJOR_VERSION <= 5
55         polydatamapper->SetInput(polydata);
56 #else
57         polydatamapper->SetInputData(polydata);
58         polydatamapper->Update();
59 #endif
60         vtkactor->SetMapper(polydatamapper);
61
62 //EED printf("8.2 ");
63
64         vecVtkPolyData.push_back( polydata );
65         vecVtkPolyDataMaper.push_back( polydatamapper );
66         vecVtkActors.push_back( vtkactor );
67
68 //EED printf("8.3 ");
69
70 //      vtkImageData* img = bbGetInputImage();
71         unsigned int i,size;
72         double spc[3];
73 //      img->GetSpacing(spc);
74         spc[0]=1;
75         spc[1]=1;
76         spc[2]=1;
77
78         int lstpointsXsize =bbGetInputlstPointX().size();
79
80 //EED printf("8.4 ");
81
82         if( lstpointsXsize>0 )
83         {
84 //EED printf("8.5 ");
85                 vtkPoints       *allPoints              = vtkPoints::New( );
86                 vtkPolyLine *polyline           = vtkPolyLine::New();
87                 vtkCellArray* allTopology       = vtkCellArray::New( );
88 //              allTopology->InsertNextCell( numPoints );
89
90
91                 size=iGeneral+numPoints;
92 //EED printf("8.6a %p %d %d ",allPoints, iGeneral,size);
93 //EED printf("8.6b %d %d %d ",bbGetInputlstPointX().size(), bbGetInputlstPointY().size(), bbGetInputlstPointZ().size() );
94
95                 for (i=iGeneral;i<size;i++)
96                 {
97 //EED printf("8.7(%d) ",i);
98                         //multiplicar ver parametros spacing, en maracas cuando se toca la imagen y se ve dycom
99                         //hay par·metro dycom, vtkImagedata valor spacing y esos datos hay que multiplicar al polydata
100                         allPoints->InsertNextPoint( bbGetInputlstPointX()[i]*spc[0],
101                                                                                 bbGetInputlstPointY()[i]*spc[1],
102                                                                                 bbGetInputlstPointZ()[i]*spc[2] );
103 //                      printf("DrawAxisTree3D::DrawOneAxis point  %d -> %f, %f, %f \n", i,  bbGetInputlstPointX()[i], bbGetInputlstPointY()[i], bbGetInputlstPointZ()[i] );
104
105 polyline->GetPointIds()->InsertNextId(i-iGeneral);
106
107 //                      allTopology->InsertCellPoint( i-iGeneral );
108                 } // rof
109 //EED printf("8.8 ");
110                 
111   allTopology->InsertNextCell(polyline);
112
113  // create cell array for edges
114   vtkCellArray *edges = vtkCellArray::New();
115 //EED printf("8.9 ");
116
117 // connect edge's and vertexes point ids
118   for(vtkIdType i = iGeneral; i < size-1; i++)
119   {
120 //EED printf("8.10(%d) ",i);
121     vtkLine *edge = vtkLine::New();
122     edge->GetPointIds()->SetId(0, i-iGeneral);
123     edge->GetPointIds()->SetId(1, (i-iGeneral+1) );
124     edges->InsertNextCell(edge);
125     edge->Delete();
126 //    vtkSmartPointer<vtkVertex> vertex = vtkSmartPointer<vtkVertex>::New();
127 //    vertex->GetPointIds()->SetId(0, i);
128 //    vertexes->InsertNextCell(vertex);
129   }
130
131 //EED printf("8.11 ");
132                 polydata->SetPoints( allPoints );
133  polydata->SetLines(edges);
134 //              polydata->SetLines( allTopology );
135
136
137  // polydata->SetPolys( allTopology );
138
139                 edges->Delete();
140                 allPoints->Delete();
141                 allTopology->Delete();
142 //EED printf("8.12 ");
143                 
144         }
145
146         // color
147         double r,g,b;
148
149         if (bbGetInputColourLaw()==1)  // the same color for all 
150         {
151 //EED printf("8.13(%d) ",bbGetInputColour().size() );
152
153                 r = bbGetInputColour()[0];
154                 g = bbGetInputColour()[1];
155                 b = bbGetInputColour()[2];
156                 vtkactor->GetProperty()->SetColor( r,g,b );
157         } // Law 1  color for all
158
159         if (bbGetInputColourLaw()==2) // different colors for each segment
160         {
161 //EED printf("8.14 ");
162                 if ( (iAxis*3+1) < (int)(bbGetInputColour().size()) )   
163                 {
164                         r = bbGetInputColour()[0+iAxis*3];
165                         g = bbGetInputColour()[1+iAxis*3];
166                         b = bbGetInputColour()[2+iAxis*3];
167                 } else {
168                         r = (rand() % 100) / 100.0;
169                         g = (rand() % 100) / 100.0;
170                         b = (rand() % 100) / 100.0;
171                 }
172 //EED printf("8.15 ");
173                 vtkactor->GetProperty()->SetColor( r,g,b );
174         }  // Law 2  color for each segment
175
176         if (bbGetInputColourLaw()==3)  // color for each point 
177         {
178 //EED printf("8.16 ");
179                 // Setup the colors array
180                 vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
181                 colors->SetNumberOfComponents(3);
182                 colors->SetName("Colors");
183 //              colors->SetName("vertex color");
184                 int iPoint,pointSize=iGeneral+numPoints;
185
186
187                 for (iPoint=iGeneral; iPoint<pointSize; iPoint++)
188                 {
189                         r = bbGetInputColour()[0+iPoint*3]*255.0;
190                         g = bbGetInputColour()[1+iPoint*3]*255.0;
191                         b = bbGetInputColour()[2+iPoint*3]*255.0;
192                         //colors->SetTuple3(iPoint,r,g,b);
193                         colors->InsertNextTuple3(r,g,b);
194                 }
195                 polydata->GetCellData()->SetScalars(colors);
196                 polydata->Modified();
197  
198         }  // Law 3 color for each point
199
200 //EED printf("8.17 ");
201         vtkactor->GetProperty()->SetLineWidth( bbGetInputLineWidth() );
202         vtkactor->GetProperty()->SetOpacity( bbGetInputOpacity() );
203
204         if ( bbGetInputTransform()!=NULL )
205         {
206 //EED printf("8.18 ");
207                 vtkactor->SetUserTransform( bbGetInputTransform() );
208         }
209
210      // Interface Update
211      if  (bbGetInputRenderer()!=NULL )
212      {
213 //EED printf("8.19 ");
214             bbGetInputRenderer()->AddActor( vtkactor );
215      }
216 //EED printf("8.20 ");
217
218 }
219
220
221 void DrawAxisTree3D::Process()
222 {
223 printf("DrawAxisTree3D::Process Start >>   %s\n", bbGetFullName().c_str() );
224
225    if ( (bbGetInputlstPointX().size()==bbGetInputlstPointY().size()) && 
226                 (bbGetInputlstPointX().size()==bbGetInputlstPointZ().size())      )
227         {
228                 int iGeneral=0;
229                 std::vector<int> lstIndexs = bbGetInputlstIndexs();
230                 if ( bbGetInputlstIndexs().size()==0 )
231                 {
232         //EED printf("2 ");
233                         lstIndexs.push_back( bbGetInputlstPointX().size() );
234                 } // if
235                 int iAxis,sizeLstAxis=lstIndexs.size();
236                 int iActor,sizeActors = vecVtkActors.size();
237                 int numPoints;
238
239         //EED printf("3 ");
240
241         //    if (oldLstSize!=sizeLstAxis)
242         //       {
243         //        oldLstSize=sizeLstAxis;
244                      for (iActor=0 ; iActor<sizeActors; iActor++)
245                      {
246         //EED printf("4-%d ",iActor);
247                         if (bbGetInputRenderer()!=NULL )
248                         { 
249         //EED printf("5 ");
250                             bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
251                             vecVtkPolyData[iActor]->Delete();
252                             vecVtkPolyDataMaper[iActor]->Delete();
253                             vecVtkActors[iActor]->Delete();
254         //EED printf("6 ");
255                         } // if
256                      } // for iActor
257                      vecVtkPolyData.clear();
258                      vecVtkPolyDataMaper.clear();
259                      vecVtkActors.clear();
260         //EED printf("7 ");
261                      for ( iAxis=0 ; iAxis<sizeLstAxis ; iAxis++)
262                      {
263         //EED printf("8(%d) ",iAxis);
264                         numPoints = lstIndexs[iAxis];
265         //EED printf("9(%d) ",iAxis);
266                         DrawOneAxis(iGeneral,numPoints,iAxis);
267         //EED printf("10 ");   
268                         iGeneral = iGeneral+numPoints;
269                 //              if ((iAxis % 1)==0)
270                 //              {
271                 //                      printf("EED  DrawAxisTree3D::Process  %d/%d\n", iAxis,sizeLstAxis );
272                 //              }
273                      } // for iAxis
274
275         //EED printf("11 ");
276
277                     if ( bbGetInputiAxis() < (int)(vecVtkActors.size() ) )
278                     {
279                         bbSetOutputOutAxis( vecVtkActors[ bbGetInputiAxis() ] );            
280                     } else      {
281                         printf("DrawAxisTree3D .ERROR.  iAxis for exist in actors vector ...\n");
282                     } // if 
283          //   } else {// if oldLstSize
284         //    } // if oldLstSize
285         } else {
286                 printf("\nDrawAxisTree3D .ERROR.  size of vectors lstPointX.size=%d lstPointY.size=%d lstPointZ.size=%d are not coherent \n\n",bbGetInputlstPointX().size(), bbGetInputlstPointY().size(), bbGetInputlstPointZ().size() );
287         }// size X Y Z
288 printf("DrawAxisTree3D::Process End \n");
289
290 }
291
292 void DrawAxisTree3D::bbUserSetDefaultValues()
293 {
294     oldLstSize=-1;
295          bbSetInputiAxis(0);
296      std::vector<double> colour;
297          // 1- red
298      colour.push_back(1.0);
299      colour.push_back(0.0);
300      colour.push_back(0.0);
301          // 2- blue
302      colour.push_back(0.0);
303      colour.push_back(0.0);
304      colour.push_back(1.0);
305          // 3- yellow
306      colour.push_back(1.0);
307      colour.push_back(1.0);
308      colour.push_back(0.0);
309          // 4- green
310      colour.push_back(0.0);
311      colour.push_back(1.0);
312      colour.push_back(0.0);
313          // 5-
314      colour.push_back(0.0);
315      colour.push_back(1.0);
316      colour.push_back(1.0);
317
318          // 6-
319      colour.push_back(0.5);
320      colour.push_back(0.5);
321      colour.push_back(0.5);
322
323      bbSetInputColour(colour);
324      bbSetInputOpacity(1);
325      bbSetInputLineWidth(0.5);
326      bbSetInputColourLaw(1);
327      bbSetInputTransform(NULL);
328      bbSetOutputOutAxis(NULL);
329
330 }
331
332
333         //-----------------------------------------------------------------
334         void DrawAxisTree3D::bbUserInitializeProcessing()
335         {
336         }
337
338         //-----------------------------------------------------------------
339         void DrawAxisTree3D::bbUserFinalizeProcessing()
340         {
341         }
342
343         //-----------------------------------------------------------------
344
345 }
346 // EO namespace bbcreaMaracasVisu
347
348