]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSphereList.cxx
4932d83cc9766d9f24ceba115775c784059dd81c
[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 }  
81
82                         
83 void SphereList::GetColor(int i, double &cr,double &cg,double &cb)        
84  {
85         unsigned int j = 3*i +2;
86         if (j < bbGetInputColour().size() )
87         {
88                 cr=bbGetInputColour()[3*i+0];
89                 cg=bbGetInputColour()[3*i+1];
90                 cb=bbGetInputColour()[3*i+2];
91                 
92         } else {
93                 int lastValidColorIndex = (bbGetInputColour().size()-1)/3;
94                 lastValidColorIndex     = lastValidColorIndex*3;
95                 cr=bbGetInputColour()[ lastValidColorIndex+0 ];
96                 cg=bbGetInputColour()[ lastValidColorIndex+1 ];
97                 cb=bbGetInputColour()[ lastValidColorIndex+2 ];
98         }
99 }  
100
101
102
103 void SphereList::Process()
104 {
105
106         int iMin, iMax;
107         double px = 0.0;
108         double py = 0.0;
109         double pz = 0.0;
110
111         double cr = 0.0;
112         double cg = 0.0;
113         double cb = 0.0;
114         
115         if (bbGetInputRenderer()!=NULL)
116         {
117                 // 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. 
118                 if (sphereActor.size() != 0)
119                 {
120                   for (int i = 0; i < sphereActor.size(); i++)
121                   {
122                         GetPoint(i, px,py,pz);
123                         GetColor(i, cr,cg,cb);
124                         sphereActor[i] ->SetPosition( px,py,pz );
125                         sphereActor[i] -> GetProperty() -> SetOpacity( bbGetInputOpacity() );
126                         sphereActor[i] -> GetProperty() -> SetColor( cr,cg,cb );
127                         vtkspheresourceLst[i] -> SetRadius( GetRadio(i) ); 
128                   } // for i
129                 } else {
130                    
131
132                     iMin=0;
133                     iMax=bbGetInputlstPointX().size();
134 //                    printf("EED SphereList::Process   iMax=%d \n", iMax);
135 //                    for (int i = 0; i < iMax; i++)
136 //                      {
137 //                        std::cout << "CM SphereList::Process i=" <<  i  << " bbGetInputlstPointX()[i]=" << bbGetInputlstPointX()[i] << std::endl;
138 //                        std::cout << "CM SphereList::Process i=" <<  i  << " bbGetInputlstPointY()[i]=" << bbGetInputlstPointY()[i] << std::endl;
139 //                        std::cout << "CM SphereList::Process i=" <<  i  << " bbGetInputlstPointZ()[i]=" << bbGetInputlstPointZ()[i] << std::endl;
140 //                      } // for i
141  
142                    // 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.
143                     for ( int i=iMin ; i<iMax; i=i+1 )
144                     {
145                          
146
147                         GetPoint(i, px,py,pz);
148                         GetColor(i, cr,cg,cb);
149                         
150
151                         // Sphere
152                         vtkSphereSource * newSphere =  vtkSphereSource::New();
153                         vtkspheresourceLst.push_back(newSphere);
154
155                         newSphere -> SetThetaResolution(20);
156                         newSphere -> SetPhiResolution(20);
157                         newSphere -> SetRadius( GetRadio(i) ); 
158
159                         vtkPolyDataMapper * newMapper = vtkPolyDataMapper::New();
160                         newMapper -> SetInput( newSphere -> GetOutput() );
161         
162                         vtkActor * newActor = vtkActor::New();
163                         sphereActor.push_back(newActor);
164                         newActor -> SetMapper(newMapper);
165                         newActor -> SetOrigin(0, 0, 0);                 
166                         newActor -> GetProperty() -> SetColor(cr,cg,cb);
167                         newActor -> GetProperty() -> SetOpacity( bbGetInputOpacity() );
168                         newActor -> SetPosition( px,py,pz );
169         
170                         if ( bbGetInputTransform()!=NULL )
171                         {
172                                 newActor->SetUserTransform( bbGetInputTransform() );
173                         }
174         
175
176                         if (bbGetInputRenderer()!=NULL)
177                         {
178                             bbGetInputRenderer() -> AddActor( newActor );
179                         }
180
181                     } // for
182
183                     if (sphereActor.size() != 0)
184                     {
185                         // Sets the output.
186                         bbSetOutputActorList(sphereActor);
187                     }
188                     
189                 } // if (bbGetInputRenderer()!=NULL)
190
191         } // if (sphereActor.size() != 0)
192 }
193
194 void SphereList::bbUserSetDefaultValues()
195 {
196  
197   bbSetInputRenderer(NULL);
198   bbSetInputTransform(NULL);
199  
200   // Sets default radio to 1.
201   std::vector<double> radio;
202   radio.push_back(1.0);
203   bbSetInputlstRadio(radio);
204
205   // Sets default colour to red.
206   std::vector<double> colour;
207   colour.push_back(1.0);
208   colour.push_back(0.0);
209   colour.push_back(0.0);
210   bbSetInputColour(colour);
211         
212         // Sets default spacing.
213         std::vector<double> spacing;
214         spacing.push_back(1.0);
215         spacing.push_back(1.0);
216         spacing.push_back(1.0);
217         bbSetInputSpacing(spacing);
218         
219   bbSetInputOpacity(1.0);
220
221 }
222
223 void SphereList::bbUserInitializeProcessing()
224 {
225  
226 }
227
228 void SphereList::bbUserFinalizeProcessing()
229 {
230   
231 }
232 }
233 // EO namespace bbvtk
234
235