]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuDrawAxisTree3D.cxx
#3468 Refres for 3D+t Visu
[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         // Removing actors
226         int iActor,sizeActors = vecVtkActors.size();
227         for (iActor=0 ; iActor<sizeActors; iActor++)
228         {
229                 if (bbGetInputRenderer()!=NULL )
230                 { 
231                         bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
232                         vecVtkPolyData[iActor]->Delete();
233                         vecVtkPolyDataMaper[iActor]->Delete();
234                         vecVtkActors[iActor]->Delete();
235                 } // if
236         } // for iActor
237         vecVtkPolyData.clear();
238         vecVtkPolyDataMaper.clear();
239         vecVtkActors.clear();
240
241         // Adding actors
242         if (bbGetInputActive()==true)
243         {
244            if ( (bbGetInputlstPointX().size()==bbGetInputlstPointY().size()) && 
245                         (bbGetInputlstPointX().size()==bbGetInputlstPointZ().size())      )
246                 {
247                         int iGeneral=0;
248                         std::vector<int> lstIndexs = bbGetInputlstIndexs();
249                         if ( bbGetInputlstIndexs().size()==0 )
250                         {
251                                 lstIndexs.push_back( bbGetInputlstPointX().size() );
252                         } // if
253                         int iAxis,sizeLstAxis=lstIndexs.size();
254                         int numPoints;
255                         for ( iAxis=0 ; iAxis<sizeLstAxis ; iAxis++)
256                         {
257                                 numPoints = lstIndexs[iAxis];
258                                 DrawOneAxis(iGeneral,numPoints,iAxis);
259                                 iGeneral = iGeneral+numPoints;
260                         } // for iAxis
261                         if ( bbGetInputiAxis() < (int)(vecVtkActors.size() ) )
262                         {
263                                 bbSetOutputOutAxis( vecVtkActors[ bbGetInputiAxis() ] );            
264                         } else  {
265                                 printf("DrawAxisTree3D .ERROR.  iAxis for exist in actors vector ...\n");
266                         } // if 
267                 } else {
268                         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() );
269                 }// size X Y Z
270         } // if Active
271 printf("DrawAxisTree3D::Process End \n");
272
273 }
274
275 void DrawAxisTree3D::bbUserSetDefaultValues()
276 {
277     oldLstSize=-1;
278      std::vector<double> colour;
279          // 1- red
280      colour.push_back(1.0);
281      colour.push_back(0.0);
282      colour.push_back(0.0);
283          // 2- blue
284      colour.push_back(0.0);
285      colour.push_back(0.0);
286      colour.push_back(1.0);
287          // 3- yellow
288      colour.push_back(1.0);
289      colour.push_back(1.0);
290      colour.push_back(0.0);
291          // 4- green
292      colour.push_back(0.0);
293      colour.push_back(1.0);
294      colour.push_back(0.0);
295          // 5-
296      colour.push_back(0.0);
297      colour.push_back(1.0);
298      colour.push_back(1.0);
299
300          // 6-
301      colour.push_back(0.5);
302      colour.push_back(0.5);
303      colour.push_back(0.5);
304
305          bbSetInputActive(true);
306          bbSetInputiAxis(0);
307      bbSetInputColour(colour);
308      bbSetInputOpacity(1);
309      bbSetInputLineWidth(0.5);
310      bbSetInputColourLaw(1);
311      bbSetInputTransform(NULL);
312      bbSetOutputOutAxis(NULL);
313
314 }
315
316
317         //-----------------------------------------------------------------
318         void DrawAxisTree3D::bbUserInitializeProcessing()
319         {
320         }
321
322         //-----------------------------------------------------------------
323         void DrawAxisTree3D::bbUserFinalizeProcessing()
324         {
325         }
326
327         //-----------------------------------------------------------------
328
329 }
330 // EO namespace bbcreaMaracasVisu
331
332