DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SphericalAccumulator.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
44 template <typename T>
45 inline
47 {
48  myTotal = 0;
49  myNphi = aP;
50  myNtheta = 2*myNphi;
51  myAccumulator.resize(myNphi*myNtheta,0);
52  myAccumulatorDir.resize(myNphi*myNtheta, Vector::zero);
53  myBinNumber = 0;
54 
55  myMaxBinTheta = 0;
56  myMaxBinPhi= 0;
57 
58 
59  for(Size posPhi=0; posPhi < myNphi; posPhi++)
60  for(Size posTheta=0; posTheta < myNtheta; posTheta++)
61  {
62  double dphi = M_PI/((double)myNphi-1);
63  double Ntheta_i = floor(2.0*((double)myNphi)*sin((double)posPhi*dphi)) ;
64 
65  if (((posPhi == 0) || (posPhi == (myNphi-1))) && (posTheta==0))
66  myBinNumber++;
67  else
68  if ((posPhi < myNphi) && (posTheta<Ntheta_i) && (posTheta< myNtheta))
69  myBinNumber ++;
70  }
71 }
75 template <typename T>
76 inline
78 {
79 }
80 // --------------------------------------------------------
81 template <typename T>
82 inline
84  Size &posPhi,
85  Size &posTheta) const
86 {
87  double theta,phi;
88  double norm = aDir.norm();
89 
90  ASSERT(norm != 0);
91 
92  phi = acos(NumberTraits<typename T::Component>::castToDouble(aDir[2])/norm);
93 
94  double dphi = M_PI/(double)(myNphi-1);
95  double Nthetai;
96  posPhi = static_cast<Size>(floor( (phi+dphi/2.) *(myNphi-1)/ M_PI));
97  if(posPhi == 0 || posPhi== (myNphi-1))
98  {
99  posTheta =0;
100  }
101  else
102  {
105 
106  if(aDir[1]<0)
107  theta += 2.0*M_PI;
108 
109  Nthetai = floor(2.0*(myNphi)*sin(posPhi*dphi));
110  double dtheta = 2.0*M_PI/(Nthetai);
111  posTheta = static_cast<Size>(floor( (theta+dtheta/2.0)/dtheta));
112 
113  if (posTheta >= Nthetai)
114  posTheta -= Nthetai;
115  }
116 
117  ASSERT(posPhi < myNphi);
118  ASSERT( isValidBin(posPhi,posTheta) );
119 }
120 // --------------------------------------------------------
121 template <typename T>
122 inline
124 {
125  Size posPhi,posTheta;
126 
127  binCoordinates(aDir , posPhi, posTheta);
128  myAccumulator[posTheta + posPhi*myNtheta] += 1;
129  myAccumulatorDir[posTheta + posPhi*myNtheta] += aDir;
130  myTotal ++;
131 
132  //Max bin update
133  if ( myAccumulator[posTheta + posPhi*myNtheta] >
134  myAccumulator[ myMaxBinTheta + myMaxBinPhi*myNtheta])
135  {
136  myMaxBinTheta = posTheta;
137  myMaxBinPhi = posPhi;
138  }
139 }
140 // --------------------------------------------------------
141 template <typename T>
142 inline
145 {
146  return myTotal;
147 }
148 // --------------------------------------------------------
149 template <typename T>
150 inline
151 void
153 {
154  phi = myMaxBinPhi;
155  theta = myMaxBinTheta;
156 }
157 // --------------------------------------------------------
158 template <typename T>
159 inline
162  const Size &posTheta) const
163 {
164  ASSERT( isValidBin(posPhi,posTheta) );
165  return myAccumulator[posTheta + posPhi*myNtheta];
166 }
167 // --------------------------------------------------------
168 template <typename T>
169 inline
170 T
172  const Size &posTheta) const
173 {
174  ASSERT( isValidBin(posPhi,posTheta) );
175  Vector p=myAccumulatorDir[posTheta + posPhi*myNtheta] ;
176  return p;
177 }
178 // --------------------------------------------------------
179 template <typename T>
180 inline
181 T
183 {
184  Size dist = it - myAccumulator.begin();
185  Vector p = *(myAccumulatorDir.begin() + dist);
186  return p;
187 }
188 // --------------------------------------------------------
189 // --------------------------------------------------------
190 template <typename T>
191 inline
192 void
194  Size &posPhi,
195  Size &posTheta) const
196 {
197  Size dist = it - myAccumulator.begin();
198  posPhi = dist/ myNtheta;
199  posTheta = dist % myNtheta;
200 }
201 // --------------------------------------------------------
202 template <typename T>
203 inline
204 bool
206  const Size &posTheta) const
207 {
208  double dphi = M_PI/((double)myNphi-1.0);
209  double Ntheta_i = floor(2.0*((double)myNphi)*sin((double)posPhi*dphi));
210 
211  if ((posPhi == 0) || (posPhi == (myNphi-1)))
212  return (posTheta==0);
213  else
214  return (posPhi < myNphi) && (posTheta<Ntheta_i) && (posTheta< myNtheta);
215 }
216 // --------------------------------------------------------
217 template <typename T>
218 inline
219 void
221  const Size &posTheta,
222  RealVector &a,
223  RealVector &b,
224  RealVector &c,
225  RealVector &d) const
226 {
227  ASSERT( isValidBin(posPhi,posTheta) );
228  double dphi = M_PI/(double)((double)myNphi-1);
229  double phi= (double)posPhi*dphi;
230  double dtheta;
231  double Nthetai = floor(2.0*(myNphi)*sin(phi) );
232 
233  dtheta = 2.0*M_PI/(Nthetai);
234 
235  double theta=posTheta*dtheta;
236 
237  a[0]=cos(theta-dtheta/2.0)*sin(phi -dphi/2.0);
238  a[1]=sin(theta-dtheta/2.0)*sin(phi -dphi/2.0);
239  a[2]=cos(phi -dphi/2.0);
240 
241 
242  if ((double)posTheta -1 >= Nthetai)
243  {
244  b[0]=cos(-dtheta/2.0)*sin(phi -dphi/2.0);
245  b[1]=sin(-dtheta/2.0)*sin(phi -dphi/2.0);
246  b[2]=cos(phi -dphi/2.0);
247 
248  c[0]=cos(- dtheta/2.0)*sin(phi+dphi/2.0);
249  c[1]=sin( - dtheta/2.0)*sin(phi+dphi/2.0);
250  c[2]=cos(phi+dphi/2.0);
251  }
252  else
253  {
254  b[0]=cos(theta+dtheta/2.0)*sin(phi -dphi/2.0);
255  b[1]=sin(theta+dtheta/2.0)*sin(phi -dphi/2.0);
256  b[2]=cos(phi -dphi/2.0);
257 
258  c[0]=cos(theta+dtheta/2.0)*sin(phi+dphi/2.0);
259  c[1]=sin(theta+dtheta/2.0)*sin(phi+dphi/2.0);
260  c[2]=cos(phi+dphi/2.0);
261  }
262 
263  d[0]=cos(theta-dtheta/2.0)*sin(phi+dphi/2.0);
264  d[1]=sin(theta-dtheta/2.0)*sin(phi+dphi/2.0);
265  d[2]=cos(phi+dphi/2.0);
266 }
267 // --------------------------------------------------------
268 template <typename T>
269 inline
270 void
272 {
273  myTotal = 0;
274  for(Size i=0; i < myNphi; i++)
275  for(Size j=0; j < myNtheta; j++)
276  if (isValidBin(i,j))
277  myAccumulator[j + i*myNtheta]=0;
278  else
279  myAccumulator[j+ i*myNtheta]=-1;
280 
281  std::fill(myAccumulatorDir.begin(), myAccumulatorDir.end(), Vector::zero);
282 }
283 
284 
286 // Interface - public :
287 
292 template <typename T>
293 inline
294 void
296 {
297  out << "[SphericalAccumulator] Nphi="<<myNphi<<" Ntheta=" <<myNtheta
298  <<" Number of samples="<<myTotal
299  <<" Number of bins="<<myBinNumber;
300 }
301 
306 template <typename T>
307 inline
308 bool
310 {
311  return true;
312 }
313 
314 
315 
317 // Implementation of inline functions //
318 
319 template <typename T>
320 inline
321 std::ostream&
322 DGtal::operator<< ( std::ostream & out,
323  const SphericalAccumulator<T> & object )
324 {
325  object.selfDisplay( out );
326  return out;
327 }
328 
329 // //
331 
332