Code_TYMPAN  4.4.0
Industrial site acoustic simulation
UniformSphericSampler2.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 // Rajout de libraires pour creation fichier externe de sortie
17 #include <limits>
18 #include <iostream>
19 #include <fstream>
20 #include <sstream>
21 #include <deque>
22 #include <stack>
23 #include <string>
24 #include <math.h>
25 #include "Sampler.h"
26 #include "Tools/UnitConverter.h"
27 
28 #ifndef UNIFORM_SPHERIC_SAMPLER_2
29  #define UNIFORM_SPHERIC_SAMPLER_2
30 
31 using namespace std;
36 {
37 public:
39  UniformSphericSampler2(const unsigned int& nbRays = 0, const decimal& Theta = (decimal)M_PIDIV2,
40  const decimal& Phi = (decimal)M_2PI)
41  : Sampler(nbRays, Theta, Phi), _real_nb_rays(0), _n1(1), _n2(1), _dr(0.), _phi0(0.), _d_theta(0.),
42  _d_phi(0.), _thetaCalcul(0.), _i(1), _j(1)
43  {
44  init();
45  }
46 
48  {
49  _real_nb_rays = 0;
50  _n1 = 1;
51  _n2 = 1;
52  _dr = 0.;
53  _phi0 = 0.;
54  _d_theta = 0.;
55  _d_phi = 0.;
56  _i = 1;
57  _j = 1;
58  _thetaCalcul = 0.;
59  init();
60  }
61 
63  {
64  _real_nb_rays = 0;
65  _n1 = 1;
66  _n2 = 1;
67  _dr = 0.;
68  _phi0 = 0.;
69  _d_theta = 0.;
70  _d_phi = 0.;
71  _i = 1;
72  _j = 1;
73  _thetaCalcul = 0.;
74  init();
75  }
76 
77  virtual Sampler* Clone()
78  {
79  Sampler* sampler = new UniformSphericSampler2(this);
80  return sampler;
81  }
84 
85  virtual vec3 getSample()
86  {
87  vec3 res(0., 0., 0.);
88  if (_i > _n1)
89  {
90  return res;
91  }
92 
93  if (_j > _n2)
94  {
95  computeThetaCalcul(++_i);
96  _phi0 = _d_phi / 2.f;
97  computeN2();
98  _j = 1;
99  }
100 
101  decimal phiCalcul = _j * _d_phi + _phi0;
102 
103  _j++;
104 
105  Tools::fromRadianToCarthesien(_thetaCalcul, phiCalcul, res);
106  res.normalize();
107 
108  return res;
109  }
110 
111  virtual bool isAcceptableSample(vec3 v)
112  {
113  return true;
114  }
115 
116  virtual void init()
117  {
118  _dr = sqrt(4.f / static_cast<decimal>(_nb_rays));
119 
120  computeN1();
121 
122  computeTrueNbRays();
123 
124  computeThetaCalcul(_i);
125 
126  computeN2();
127  }
129  unsigned int getRealNbRays() const
130  {
131  return _real_nb_rays;
132  }
133 
134  virtual unsigned int computeDiffractionNbr(const decimal& thetaCalcul)
135  {
136  return static_cast<unsigned int>(floor(M_2PI * cos(thetaCalcul) / (2. * _dr) + 0.5));
137  }
138 
139 private:
140  inline void computeN1()
141  {
142  decimal dh = 2.f * _dr * static_cast<float>(sin(M_PI / 3.)); // height of a slice on equator
143  _d_theta = atan(dh);
144  _n1 = static_cast<unsigned int>(floor(M_PI / _d_theta));
145  }
146 
147  inline void computeThetaCalcul(unsigned int i)
148  {
149  _thetaCalcul = static_cast<decimal>(M_PIDIV2 - i * _d_theta);
150  }
151 
152  inline void computeN2()
153  {
154  _n2 = static_cast<unsigned int>(floor(M_2PI * cos(_thetaCalcul) / (2. * _dr)));
155  _d_phi = _phi / _n2;
156  }
157 
158  inline void computeTrueNbRays()
159  {
160  for (unsigned int i = 1; i <= _n1; i++)
161  {
162  computeThetaCalcul(i);
163  computeN2();
164  _real_nb_rays += _n2;
165  }
166  }
167 
168 private:
169  unsigned int _real_nb_rays;
170  unsigned int _n1;
171  unsigned int _n2;
177  unsigned int _i, _j;
178 };
179 
180 #endif
#define M_2PI
2Pi.
Definition: 3d.h:55
Sampler class and its sub-classes describe ray generators used in AcousticRayTracer....
Definition: Sampler.h:30
Another sampler class for uniform spherical sampling.
UniformSphericSampler2(UniformSphericSampler2 *sampler)
void computeThetaCalcul(unsigned int i)
decimal _thetaCalcul
Current angle along a longitude.
virtual vec3 getSample()
Return the sample.
decimal _d_phi
Angle step along given slice.
decimal _d_theta
Angle step along longitude.
virtual void init()
Initialize the sample.
UniformSphericSampler2(const unsigned int &nbRays=0, const decimal &Theta=(decimal) M_PIDIV2, const decimal &Phi=(decimal) M_2PI)
Constructors.
unsigned int getRealNbRays() const
Return the launched rays real number.
decimal _dr
Radius of elementary element of sphere.
virtual bool isAcceptableSample(vec3 v)
Return true for an acceptable sample.
decimal _phi0
Angle offset between a slice and the previous on.
virtual ~UniformSphericSampler2()
Destructor.
UniformSphericSampler2(const UniformSphericSampler2 &other)
virtual Sampler * Clone()
Clone a sample.
virtual unsigned int computeDiffractionNbr(const decimal &thetaCalcul)
Return the number of rays to launch after a diffraction event.
unsigned int _n1
Number of slices.
unsigned int _real_nb_rays
Real number of rays launched.
unsigned int _n2
Number of rays along a specific slice.
#define M_PI
Pi.
Definition: color.cpp:25
#define M_PIDIV2
Definition: mathlib.h:68
void fromRadianToCarthesien(decimal theta, decimal phi, vec3 &result)
Convert spherical coordinates to cartesian coordinates In this function :
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:381