Code_TYMPAN  4.4.0
Industrial site acoustic simulation
UniformSphericSampler.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
29  #define UNIFORM_SPHERIC_SAMPLER
30 
31 using namespace std;
36 {
37 public:
39  UniformSphericSampler(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), _thetaCalcul(0.), _i(1), _j(1)
42  {
43  init();
44  }
45 
47  {
48  _real_nb_rays = 0;
49  _n1 = 1;
50  _n2 = 1;
51  _i = 1;
52  _j = 1;
53  _thetaCalcul = 0.;
54  init();
55  }
56 
58  {
59  _real_nb_rays = 0;
60  _n1 = 1;
61  _n2 = 1;
62  _i = 1;
63  _j = 1;
64  _thetaCalcul = 0.;
65  init();
66  }
67 
68  virtual Sampler* Clone()
69  {
70  Sampler* sampler = new UniformSphericSampler(this);
71  return sampler;
72  }
75 
76  virtual vec3 getSample()
77  {
78  vec3 res(0., 0., 0.);
79  if (_i > _n1)
80  {
81  return res;
82  }
83 
84  if (_j > _n2)
85  {
86  computeThetaCalcul(++_i);
87  computeN2();
88  _j = 1;
89  }
90 
91  decimal phiCalcul = _j * (_phi / _n2);
92 
93  _j++;
94 
95  Tools::fromRadianToCarthesien(_thetaCalcul, phiCalcul, res);
96  res.normalize();
97 
98  return res;
99  }
100 
101  virtual bool isAcceptableSample(vec3 v)
102  {
103  return true;
104  }
105 
106  virtual void init()
107  {
108  computeN1();
109 
110  computeTrueNbRays();
111 
112  computeThetaCalcul(_i);
113 
114  computeN2();
115  }
116  // Return the real number of launched rays
117  unsigned int getRealNbRays() const
118  {
119  return _real_nb_rays;
120  }
121 
122  virtual unsigned int computeDiffractionNbr(const decimal& thetaCalcul)
123  {
124  return static_cast<unsigned int>(
125  floor(_nb_rays * (sin(thetaCalcul + _theta / _n1) - sin(thetaCalcul - _theta / _n1)) / 2. + 0.5));
126  }
127 
128 private:
129  inline void computeN1()
130  {
131  if (_nb_rays == 1)
132  _n1 = 1;
133  else
134  _n1 =
135  2 * static_cast<unsigned int>(floor(M_PI * sqrt(static_cast<decimal>(_nb_rays)) / 8. + 0.5));
136  }
137 
138  inline void computeThetaCalcul(unsigned int i)
139  {
140  _thetaCalcul = static_cast<decimal>(static_cast<int>(_n1) - 2 * static_cast<int>(i) + 1) * _theta /
141  static_cast<decimal>(_n1);
142  }
143 
144  inline void computeN2()
145  {
146  _n2 = static_cast<unsigned int>(floor(
147  _nb_rays * (sin(_thetaCalcul + _theta / _n1) - sin(_thetaCalcul - _theta / _n1)) / 2. + 0.5));
148  }
149 
150  inline void computeTrueNbRays()
151  {
152  for (unsigned int i = 1; i <= _n1; i++)
153  {
154  computeThetaCalcul(i);
155  computeN2();
156  _real_nb_rays += _n2;
157  }
158  }
159 
160 private:
161  unsigned int _real_nb_rays;
162  unsigned int _n1;
163  unsigned int _n2;
165  unsigned int _i, _j;
166 };
167 
168 #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
A Sampler class for uniform spherical sampling.
virtual Sampler * Clone()
Clone a sample.
unsigned int _n2
Number of rays along a specific slice.
unsigned int _n1
Number of slices.
UniformSphericSampler(UniformSphericSampler *sampler)
unsigned int getRealNbRays() const
virtual void init()
Initialize the sample.
virtual unsigned int computeDiffractionNbr(const decimal &thetaCalcul)
Return the number of rays to launch after a diffraction event.
UniformSphericSampler(const unsigned int &nbRays=0, const decimal &Theta=(decimal) M_PIDIV2, const decimal &Phi=(decimal) M_2PI)
Constructors.
unsigned int _real_nb_rays
Real number of rays launched.
virtual bool isAcceptableSample(vec3 v)
Return true for an acceptable sample.
virtual vec3 getSample()
Return the sample.
void computeThetaCalcul(unsigned int i)
decimal _thetaCalcul
Current angle along a longitude.
UniformSphericSampler(const UniformSphericSampler &other)
virtual ~UniformSphericSampler()
Destructor.
#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