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