]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSphereList.cxx
#3059 BBTK Bug New Normal - Update of SphereList box in vtk package
[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         {
52             radio = bbGetInputlstRadio()[ bbGetInputlstRadio().size() - 1 ];
53         }  
54
55        return radio;
56 }
57
58
59 void SphereList::GetPoint(int i, double &px,double &py,double &pz)        
60  {
61       double spc[3];
62       if (bbGetInputSpacing().size()==3)
63       {
64                 spc[0]=bbGetInputSpacing()[0];
65                 spc[1]=bbGetInputSpacing()[1];
66                 spc[2]=bbGetInputSpacing()[2];
67       } else {
68                 spc[0]=spc[1]=spc[2]=1;
69        }
70
71        px = bbGetInputlstPointX()[i]*spc[0];
72        if (bbGetInputlstPointY().size() == bbGetInputlstPointX().size() )
73        {
74                 py = bbGetInputlstPointY()[i]*spc[1];
75         }
76                 if (bbGetInputlstPointZ().size() == bbGetInputlstPointX().size() )
77         {                       
78                 pz = bbGetInputlstPointZ()[i]*spc[2];
79         }
80 printf("EED SphereList::GetPoint() %f %f %f\n",px,py,pz);
81 }  
82
83                         
84 void SphereList::GetColor(int i, double &cr,double &cg,double &cb)        
85  {
86         unsigned int j = 3*i +2;
87         if (j < bbGetInputColour().size() )
88         {
89                 cr=bbGetInputColour()[3*i+0];
90                 cg=bbGetInputColour()[3*i+1];
91                 cb=bbGetInputColour()[3*i+2];
92                 
93         } else {
94                 int lastValidColorIndex = (bbGetInputColour().size()-1)/3;
95                 lastValidColorIndex     = lastValidColorIndex*3;
96                 cr=bbGetInputColour()[ lastValidColorIndex+0 ];
97                 cg=bbGetInputColour()[ lastValidColorIndex+1 ];
98                 cb=bbGetInputColour()[ lastValidColorIndex+2 ];
99         }
100 }  
101
102
103
104 void SphereList::Process()
105 {
106 printf ("EED SphereList::Process Start\n");
107         int iMin, iMax;
108         double px = 0.0;
109         double py = 0.0;
110         double pz = 0.0;
111
112         double cr = 0.0;
113         double cg = 0.0;
114         double cb = 0.0;
115         
116 /*
117 == igualar la lista de objetos 
118      si la lista es igual nada que hacer
119      si la nueva lista es las corta que la lista existente borrar los actores
120      si la lista es mas larga que la lista existente crear los actores
121 == acualizar la lista de objetos
122 */
123
124
125         if (bbGetInputRenderer()!=NULL)
126         {
127 // If it is not the first time Process is executed, the actors have already been created. The only thing to do is to update the opacity. 
128                 if (sphereActor.size() != 0)
129         {
130                 for (int i = 0; i < sphereActor.size(); i++)
131                 {
132                                 GetPoint(i, px,py,pz);
133                                 GetColor(i, cr,cg,cb);
134                                 sphereActor[i] ->SetPosition( px,py,pz );
135 printf("EED SphereList::Process() %f %f %f\n",px,py,pz);
136                 sphereActor[i] -> GetProperty() -> SetOpacity( bbGetInputOpacity() );
137                 sphereActor[i] -> GetProperty() -> SetColor( cr,cg,cb );
138                 vtkspheresourceLst[i] -> SetRadius( GetRadio(i) ); 
139                 } // for i
140                 } else {
141                 iMin=0;
142             iMax=bbGetInputlstPointX().size();
143 //                    printf("EED SphereList::Process   iMax=%d \n", iMax);
144 //                    for (int i = 0; i < iMax; i++)
145 //                      {
146 //                        std::cout << "CM SphereList::Process i=" <<  i  << " bbGetInputlstPointX()[i]=" << bbGetInputlstPointX()[i] << std::endl;
147 //                        std::cout << "CM SphereList::Process i=" <<  i  << " bbGetInputlstPointY()[i]=" << bbGetInputlstPointY()[i] << std::endl;
148 //                        std::cout << "CM SphereList::Process i=" <<  i  << " bbGetInputlstPointZ()[i]=" << bbGetInputlstPointZ()[i] << std::endl;
149 //                      } // for i
150  
151                    // If the vector Y or respectively the vector Z has a different size from the vector X, the position value py or respectively pz is set to 0.
152                 for ( int i=iMin ; i<iMax; i=i+1 )
153                 {
154                 GetPoint(i, px,py,pz);
155                                 GetColor(i, cr,cg,cb);
156                                 // Sphere
157                                 vtkSphereSource * newSphere = vtkSphereSource::New();
158                                 vtkspheresourceLst.push_back(newSphere);
159
160                                 newSphere -> SetThetaResolution(20);
161                                 newSphere -> SetPhiResolution(20);
162                                 newSphere -> SetRadius( GetRadio(i) ); 
163
164                                 vtkPolyDataMapper * newMapper = vtkPolyDataMapper::New();
165                                 newMapper -> SetInput( newSphere -> GetOutput() );
166         
167                                 vtkActor * newActor = vtkActor::New();
168                                 sphereActor.push_back(newActor);
169                                 newActor -> SetMapper(newMapper);
170                                 newActor -> SetOrigin(0, 0, 0);                 
171                                 newActor -> GetProperty() -> SetColor(cr,cg,cb);
172                                 newActor -> GetProperty() -> SetOpacity( bbGetInputOpacity() );
173                                 newActor -> SetPosition( px,py,pz );
174         
175                                 if ( bbGetInputTransform()!=NULL )
176                                 {
177                                         newActor->SetUserTransform( bbGetInputTransform() );
178                                 }
179         
180                                 if (bbGetInputRenderer()!=NULL)
181                                 {
182                                     bbGetInputRenderer() -> AddActor( newActor );
183                                 }
184
185                         } // for
186
187             if (sphereActor.size() != 0)
188             {
189                 // Sets the output.
190                 bbSetOutputActorList(sphereActor);
191                 }
192                     
193                 } // if (sphereActor.size() != 0)
194         } // if (bbGetInputRenderer()!=NULL)
195
196 printf ("EED SphereList::Process END\n");
197
198 }
199
200 void SphereList::bbUserSetDefaultValues()
201 {
202  
203   bbSetInputRenderer(NULL);
204   bbSetInputTransform(NULL);
205  
206   // Sets default radio to 1.
207   std::vector<double> radio;
208   radio.push_back(1.0);
209   bbSetInputlstRadio(radio);
210
211   // Sets default colour to red.
212   std::vector<double> colour;
213   colour.push_back(1.0);
214   colour.push_back(0.0);
215   colour.push_back(0.0);
216   bbSetInputColour(colour);
217         
218         // Sets default spacing.
219         std::vector<double> spacing;
220         spacing.push_back(1.0);
221         spacing.push_back(1.0);
222         spacing.push_back(1.0);
223         bbSetInputSpacing(spacing);
224         
225   bbSetInputOpacity(1.0);
226
227 }
228
229 void SphereList::bbUserInitializeProcessing()
230 {
231  
232 }
233
234 void SphereList::bbUserFinalizeProcessing()
235 {
236   
237 }
238 }
239 // EO namespace bbvtk
240
241