]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbmaracasvisuDrawAxisTree3D.cxx
bae9e276b0ae2e7c62b32cad69896d698d83da76
[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         vtkPolyData                     *polydata               = vtkPolyData::New( );
48         vtkPolyDataMapper       *polydatamapper = vtkPolyDataMapper::New();
49         vtkActor                        *vtkactor               = vtkActor::New();
50
51 //      polydatamapper->ScalarVisibilityOff();
52 //EED 2017-01-01 Migration VTK7
53 #if VTK_MAJOR_VERSION <= 5
54         polydatamapper->SetInput(polydata);
55 #else
56         polydatamapper->SetInputData(polydata);
57         polydatamapper->Update();
58 #endif
59         vtkactor->SetMapper(polydatamapper);
60
61         vecVtkPolyData.push_back( polydata );
62         vecVtkPolyDataMaper.push_back( polydatamapper );
63         vecVtkActors.push_back( vtkactor );
64
65 //      vtkImageData* img = bbGetInputImage();
66         unsigned int i,size;
67         double spc[3];
68 //      img->GetSpacing(spc);
69         spc[0]=1;
70         spc[1]=1;
71         spc[2]=1;
72
73         int lstpointsXsize =bbGetInputlstPointX().size();
74         if( lstpointsXsize>0 )
75         {
76                 vtkPoints       *allPoints              = vtkPoints::New( );
77                 vtkPolyLine *polyline           = vtkPolyLine::New();
78                 vtkCellArray* allTopology       = vtkCellArray::New( );
79 //              allTopology->InsertNextCell( numPoints );
80                 size=iGeneral+numPoints;
81         std::vector<double> lstX =bbGetInputlstPointX();
82         std::vector<double> lstY =bbGetInputlstPointY();
83         std::vector<double> lstZ =bbGetInputlstPointZ();
84                 for (i=iGeneral;i<size;i++)
85                 {
86                         //multiplicar ver parametros spacing, en maracas cuando se toca la imagen y se ve dycom
87                         //hay par·metro dycom, vtkImagedata valor spacing y esos datos hay que multiplicar al polydata
88             allPoints->InsertNextPoint( lstX[i],
89                                         lstY[i],
90                                         lstZ[i] );
91
92 //                      printf("DrawAxisTree3D::DrawOneAxis point  %d -> %f, %f, %f \n", i,  bbGetInputlstPointX()[i], bbGetInputlstPointY()[i], bbGetInputlstPointZ()[i] );
93             polyline->GetPointIds()->InsertNextId(i-iGeneral);
94 //                      allTopology->InsertCellPoint( i-iGeneral );
95                 } // rof
96         allTopology->InsertNextCell(polyline);
97          // create cell array for edges
98         vtkCellArray *edges = vtkCellArray::New();
99         // connect edge's and vertexes point ids
100         for(vtkIdType i = iGeneral; i < size-1; i++)
101         {
102           vtkLine *edge = vtkLine::New();
103           edge->GetPointIds()->SetId(0, i-iGeneral);
104           edge->GetPointIds()->SetId(1, (i-iGeneral+1) );
105           edges->InsertNextCell(edge);
106           edge->Delete();
107         //      vtkSmartPointer<vtkVertex> vertex = vtkSmartPointer<vtkVertex>::New();
108         //      vertex->GetPointIds()->SetId(0, i);
109         //      vertexes->InsertNextCell(vertex);
110         } // for i
111         polydata->SetPoints( allPoints );
112         polydata->SetLines(edges);
113         //      polydata->SetLines( allTopology );
114         //   polydata->SetPolys( allTopology );
115         edges->Delete();
116         allPoints->Delete();
117         allTopology->Delete();
118         } // if lstpointsXsize
119     
120         // color
121         double r,g,b;
122     std::vector<double> lstColor = bbGetInputColour();
123         if (bbGetInputColourLaw()==1)  // the same color for all 
124         {
125                 r = lstColor[0];
126                 g = lstColor[1];
127                 b = lstColor[2];
128                 vtkactor->GetProperty()->SetColor( r,g,b );
129         } // Law 1  color for all
130         if (bbGetInputColourLaw()==2) // different colors for each segment
131         {
132                 if ( (iAxis*3+1) < (int)(bbGetInputColour().size()) )   
133                 {
134                         r = lstColor[0+iAxis*3];
135                         g = lstColor[1+iAxis*3];
136                         b = lstColor[2+iAxis*3];
137                 } else {
138                         r = (rand() % 100) / 100.0;
139                         g = (rand() % 100) / 100.0;
140                         b = (rand() % 100) / 100.0;
141                 }
142                 vtkactor->GetProperty()->SetColor( r,g,b );
143         }  // Law 2  color for each segment
144         if (bbGetInputColourLaw()==3)  // color for each point 
145         {
146                 // Setup the colors array
147                 vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
148                 colors->SetNumberOfComponents(3);
149                 colors->SetName("Colors");
150 //              colors->SetName("vertex color");
151                 int iPoint,pointSize=iGeneral+numPoints;
152                 for (iPoint=iGeneral; iPoint<pointSize; iPoint++)
153                 {
154                         r = lstColor[0+iPoint*3]*255.0;
155                         g = lstColor[1+iPoint*3]*255.0;
156                         b = lstColor[2+iPoint*3]*255.0;
157                         colors->InsertNextTuple3(r,g,b);
158                 }
159                 polydata->GetCellData()->SetScalars(colors);
160                 polydata->Modified();
161         }  // Law 3 color for each point
162
163         vtkactor->GetProperty()->SetLineWidth( bbGetInputLineWidth() );
164         vtkactor->GetProperty()->SetOpacity( bbGetInputOpacity() );
165         if ( bbGetInputTransform()!=NULL )
166         {
167                 vtkactor->SetUserTransform( bbGetInputTransform() );
168         }
169     // Interface Update
170     if  (bbGetInputRenderer()!=NULL )
171     {
172            bbGetInputRenderer()->AddActor( vtkactor );
173     }
174 }
175
176
177 void DrawAxisTree3D::Process()
178 {
179         // Removing actors
180         int iActor,sizeActors = vecVtkActors.size();
181         for (iActor=0 ; iActor<sizeActors; iActor++)
182         {
183                 if (bbGetInputRenderer()!=NULL )
184                 { 
185                         bbGetInputRenderer()->RemoveActor( vecVtkActors[iActor] );
186                         vecVtkPolyData[iActor]->Delete();
187                         vecVtkPolyDataMaper[iActor]->Delete();
188                         vecVtkActors[iActor]->Delete();
189                 } // if
190         } // for iActor
191         vecVtkPolyData.clear();
192         vecVtkPolyDataMaper.clear();
193         vecVtkActors.clear();
194         // Adding actors
195         if (bbGetInputActive()==true)
196         {
197            if ( (bbGetInputlstPointX().size()==bbGetInputlstPointY().size()) && 
198                         (bbGetInputlstPointX().size()==bbGetInputlstPointZ().size())      )
199                 {
200                         int iGeneral=0;
201                         std::vector<int> lstIndexs = bbGetInputlstIndexs();
202                         if ( bbGetInputlstIndexs().size()==0 )
203                         {
204                                 lstIndexs.push_back( bbGetInputlstPointX().size() );
205                         } // if
206                         int iAxis,sizeLstAxis=lstIndexs.size();
207                         int numPoints;
208                         for ( iAxis=0 ; iAxis<sizeLstAxis ; iAxis++)
209                         {
210                                 numPoints = lstIndexs[iAxis];
211                                 DrawOneAxis(iGeneral,numPoints,iAxis);
212                                 iGeneral = iGeneral+numPoints;
213                         } // for iAxis
214                         if ( bbGetInputiAxis() < (int)(vecVtkActors.size() ) )
215                         {
216                                 bbSetOutputOutAxis( vecVtkActors[ bbGetInputiAxis() ] );            
217                         } else  {
218                                 printf("DrawAxisTree3D .ERROR.  iAxis for exist in actors vector ...\n");
219                         } // if 
220                 } else {
221                         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() );
222                 }// size X Y Z
223         } // if Active
224 }
225
226 void DrawAxisTree3D::bbUserSetDefaultValues()
227 {
228     oldLstSize=-1;
229      std::vector<double> colour;
230          // 1- red
231      colour.push_back(1.0);
232      colour.push_back(0.0);
233      colour.push_back(0.0);
234          // 2- blue
235      colour.push_back(0.0);
236      colour.push_back(0.0);
237      colour.push_back(1.0);
238          // 3- yellow
239      colour.push_back(1.0);
240      colour.push_back(1.0);
241      colour.push_back(0.0);
242          // 4- green
243      colour.push_back(0.0);
244      colour.push_back(1.0);
245      colour.push_back(0.0);
246          // 5-
247      colour.push_back(0.0);
248      colour.push_back(1.0);
249      colour.push_back(1.0);
250
251          // 6-
252      colour.push_back(0.5);
253      colour.push_back(0.5);
254      colour.push_back(0.5);
255
256          bbSetInputActive(true);
257          bbSetInputiAxis(0);
258      bbSetInputColour(colour);
259      bbSetInputOpacity(1);
260      bbSetInputLineWidth(0.5);
261      bbSetInputColourLaw(1);
262      bbSetInputTransform(NULL);
263      bbSetOutputOutAxis(NULL);
264
265 }
266
267
268         //-----------------------------------------------------------------
269         void DrawAxisTree3D::bbUserInitializeProcessing()
270         {
271         }
272
273         //-----------------------------------------------------------------
274         void DrawAxisTree3D::bbUserFinalizeProcessing()
275         {
276         }
277
278         //-----------------------------------------------------------------
279
280 }
281 // EO namespace bbcreaMaracasVisu
282
283