]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSphereList.cxx
#3463 Bug SphereLst box Refresh
[bbtk.git] / packages / vtk / src / bbvtkSphereList.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #include "bbvtkSphereList.h"
29 #include "bbvtkPackage.h"
30
31 #include <vtkPolyDataMapper.h>
32 #include <vtkProperty.h>
33 #include <vtkRenderWindow.h>
34
35 namespace bbvtk
36 {
37
38 BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,SphereList)
39 BBTK_BLACK_BOX_IMPLEMENTATION(SphereList,bbtk::AtomicBlackBox);
40
41 double SphereList::GetRadio(int i)
42 {
43     double radio;
44         if ( bbGetInputlstRadio().size()==0 )
45     {
46                 radio = 1.0;
47         } else if ( i<bbGetInputlstRadio().size() )
48         {
49                 radio = bbGetInputlstRadio()[i];
50         } else {
51         radio = bbGetInputlstRadio()[ bbGetInputlstRadio().size() - 1 ];
52     }  
53         return radio;
54 }
55
56
57 void SphereList::GetPoint(int i, double &px,double &py,double &pz)        
58  {
59       double spc[3];
60       if (bbGetInputSpacing().size()==3)
61       {
62                 spc[0]=bbGetInputSpacing()[0];
63                 spc[1]=bbGetInputSpacing()[1];
64                 spc[2]=bbGetInputSpacing()[2];
65       } else {
66                 spc[0]=spc[1]=spc[2]=1;
67        }
68
69        px = bbGetInputlstPointX()[i]*spc[0];
70        if (bbGetInputlstPointY().size() == bbGetInputlstPointX().size() )
71        {
72                 py = bbGetInputlstPointY()[i]*spc[1];
73         }
74                 if (bbGetInputlstPointZ().size() == bbGetInputlstPointX().size() )
75         {                       
76                 pz = bbGetInputlstPointZ()[i]*spc[2];
77         }
78 }  
79
80                         
81 void SphereList::GetColor(int i, double &cr,double &cg,double &cb)        
82  {
83         unsigned int j = 3*i +2;
84         if (j < bbGetInputColour().size() )
85         {
86                 cr=bbGetInputColour()[3*i+0];
87                 cg=bbGetInputColour()[3*i+1];
88                 cb=bbGetInputColour()[3*i+2];
89                 
90         } else {
91                 int lastValidColorIndex = (bbGetInputColour().size()-1)/3;
92                 lastValidColorIndex     = lastValidColorIndex*3;
93                 cr=bbGetInputColour()[ lastValidColorIndex+0 ];
94                 cg=bbGetInputColour()[ lastValidColorIndex+1 ];
95                 cb=bbGetInputColour()[ lastValidColorIndex+2 ];
96         }
97 }  
98
99 void SphereList::Process()
100 {
101 /*
102         if (bbGetInputRenderer()!=NULL)
103         {
104                 int sizeLstX            = bbGetInputlstPointX().size(); 
105                 int sizeLstY            = bbGetInputlstPointY().size();
106                 int sizeLstZ            = bbGetInputlstPointZ().size();
107                 int sizeLstActors       = sphereActor.size();
108                 if ((sizeLstX==sizeLstY) &&(sizeLstX==sizeLstZ))
109                 {
110                         int i,diff;
111                         if (sizeLstActors>sizeLstX)
112                         {
113                                 diff=sizeLstActors-sizeLstX;
114                                 for (i=0;i<diff;i++)
115                                 {
116                                         bbGetInputRenderer()->RemoveActor( sphereActor[0] );
117                                         vtkspheresourceLst[0]->Delete();
118                                         sphereActor[0]->Delete();
119                                         vtkspheresourceLst.erase( vtkspheresourceLst.begin() );
120                                         sphereActor.erase( sphereActor.begin() );
121                                 } // for 
122                         } // if size
123
124                         if (sizeLstActors<sizeLstX)
125                         {
126                                 diff=sizeLstX-sizeLstActors;
127                                 for (i=0;i<diff;i++)
128                                 {
129                                         // Sphere
130                                         vtkSphereSource * newSphere = vtkSphereSource::New();
131                                         vtkspheresourceLst.push_back(newSphere);
132                                         newSphere -> SetThetaResolution(20);
133                                         newSphere -> SetPhiResolution(20);
134                                         newSphere -> SetRadius( 1 ); 
135                                         vtkPolyDataMapper * newMapper = vtkPolyDataMapper::New();
136
137 //EED 2017-01-01 Migration VTK7
138 #if VTK_MAJOR_VERSION <= 5
139                                         newMapper -> SetInput( newSphere -> GetOutput() );      
140 #else
141                                         newSphere -> Update();
142                                         newMapper -> SetInputData( newSphere -> GetOutput() );  
143 #endif
144
145                                         vtkActor * newActor = vtkActor::New();
146                                         sphereActor.push_back(newActor);
147                                         newActor -> SetMapper(newMapper);
148                                         newActor -> SetOrigin(0, 0, 0);                         
149                                         if ( bbGetInputTransform()!=NULL )
150                                         {
151                                                 newActor->SetUserTransform( bbGetInputTransform() );
152                                         }
153                                         bbGetInputRenderer()->AddActor( newActor );
154                                 } // for
155                         } // if size
156
157                         // Refresh data
158                 for (i = 0; i < sphereActor.size() ; i++)
159                 {
160                                 double px = 0.0;
161                                 double py = 0.0;
162                                 double pz = 0.0;
163                                 double cr = 0.0;
164                                 double cg = 0.0;
165                                 double cb = 0.0;
166                                 GetPoint(i, px,py,pz);
167                                 GetColor(i, cr,cg,cb);
168                                 sphereActor[i] ->SetPosition( px,py,pz );
169                 sphereActor[i] -> GetProperty() -> SetOpacity( bbGetInputOpacity() );
170                 sphereActor[i] -> GetProperty() -> SetColor( cr,cg,cb );
171                 vtkspheresourceLst[i] -> SetRadius( GetRadio(i) ); 
172                         vtkspheresourceLst[i] -> Update();
173                 } // for i
174
175                 if (sphereActor.size() != 0)
176             {
177                 // Sets the output.
178                 bbSetOutputActorList(sphereActor);
179                 }
180   
181                 } else {
182                         printf("EED Warning!  SphereList::Process Lst XYZ are not of the same size.\n");
183                 }// if sizeLst
184         } else {
185                         printf("EED Warning!  SphereList::Process Render not assigned.\n");             
186         }// if Renderer
187
188 //====================================================================
189 */
190
191         if (bbGetInputRenderer()!=NULL)
192         {
193                 int sizeLstX            = bbGetInputlstPointX().size(); 
194                 int sizeLstY            = bbGetInputlstPointY().size();
195                 int sizeLstZ            = bbGetInputlstPointZ().size();
196                 int sizeLstActors       = sphereActor.size();
197                 if ((sizeLstX==sizeLstY) &&(sizeLstX==sizeLstZ))
198                 {
199                         int i;
200                         double px,py,pz;
201                         double cr,cg,cb;
202                         for ( i=0 ; i<sizeLstActors ; i++ )
203                         {
204                                 bbGetInputRenderer()->RemoveActor( sphereActor[i] );
205                                 vtkspheresourceLst[i]->Delete();
206                                 vtkpolydatamaperLst[i]->Delete();
207                                 sphereActor[i]->Delete();
208                         } // for 
209                         vtkspheresourceLst.clear();
210                         vtkpolydatamaperLst.clear();
211                         sphereActor.clear();
212                         
213                         
214                         for (i=0;i<sizeLstX;i++)
215                         {
216                                 // Sphere
217                                 vtkSphereSource * newSphere = vtkSphereSource::New();
218                                 vtkspheresourceLst.push_back(newSphere);
219                                 newSphere -> SetThetaResolution(20);
220                                 newSphere -> SetPhiResolution(20);
221                                 newSphere -> SetRadius( 1 ); 
222                                 vtkPolyDataMapper * newMapper = vtkPolyDataMapper::New();
223                                 vtkpolydatamaperLst.push_back(newMapper);
224 //EED 2017-01-01 Migration VTK7
225 #if VTK_MAJOR_VERSION <= 5
226                                 newMapper -> SetInput( newSphere -> GetOutput() );      
227 #else
228                                 newSphere -> Update();
229                                 newMapper -> SetInputData( newSphere -> GetOutput() );  
230 #endif
231
232                                 vtkActor * newActor = vtkActor::New();
233                                 sphereActor.push_back(newActor);
234                                 newActor -> SetMapper(newMapper);
235                                 newActor -> SetOrigin(0, 0, 0);                         
236                                 if ( bbGetInputTransform()!=NULL )
237                                 {
238                                         newActor->SetUserTransform( bbGetInputTransform() );
239                                 }
240                                 bbGetInputRenderer()->AddActor( newActor );
241                         } // for
242
243
244                         // Refresh data
245                 for (i = 0; i < sphereActor.size() ; i++)
246                 {
247                                 GetPoint(i, px,py,pz);
248                                 GetColor(i, cr,cg,cb);
249                                 sphereActor[i] ->SetPosition( px,py,pz );
250                 sphereActor[i] -> GetProperty() -> SetOpacity( bbGetInputOpacity() );
251                 sphereActor[i] -> GetProperty() -> SetColor( cr,cg,cb );
252                 vtkspheresourceLst[i] -> SetRadius( GetRadio(i) ); 
253                         vtkspheresourceLst[i] -> Update();
254                 } // for i
255                         
256                 if (sphereActor.size() != 0)
257             {
258                 // Sets the output.
259                 bbSetOutputActorList(sphereActor);
260                 }
261
262                 } // sizeLstX sizeLstY sizeLstZ 
263         } // if Renderer
264 }
265
266
267 void SphereList::bbUserSetDefaultValues()
268 {
269  
270   bbSetInputRenderer(NULL);
271   bbSetInputTransform(NULL);
272  
273   // Sets default radio to 1.
274   std::vector<double> radio;
275   radio.push_back(1.0);
276   bbSetInputlstRadio(radio);
277
278   // Sets default colour to red.
279   std::vector<double> colour;
280   colour.push_back(1.0);
281   colour.push_back(0.0);
282   colour.push_back(0.0);
283   bbSetInputColour(colour);
284         
285         // Sets default spacing.
286         std::vector<double> spacing;
287         spacing.push_back(1.0);
288         spacing.push_back(1.0);
289         spacing.push_back(1.0);
290         bbSetInputSpacing(spacing);
291         
292   bbSetInputOpacity(1.0);
293
294 }
295
296 void SphereList::bbUserInitializeProcessing()
297 {
298  
299 }
300
301 void SphereList::bbUserFinalizeProcessing()
302 {
303   
304 }
305 }
306 // EO namespace bbvtk
307
308