Code_TYMPAN  4.4.0
Industrial site acoustic simulation
UniformPlaneSampler.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 #include "Repere.h"
28 #include "Geometry/mathlib.h"
29 
30 #ifndef UNIFORM_PLANE_SAMPLER
31  #define UNIFORM_PLANE_SAMPLER
32 
33 using namespace std;
34 
65 {
66 public:
68  UniformPlaneSampler(const vector<vec3> directions, const unsigned int& nbRays = 8)
69  : Sampler(nbRays, (decimal)M_2PI, (decimal)M_2PI), _real_nb_rays(0), _nb_rays_per_direction(0),
70  _nb_rays_per_plane(0), _nb_directions(0), _i(0), _j(0),
71  _repere(Repere(vec3(1., 0., 0.), vec3(0., 1., 0.), vec3(0., 0., 1.), vec3(0., 0., 0.))),
72  _directions(directions)
73  {
74  init();
75  }
76 
78  {
79  _real_nb_rays = other._real_nb_rays;
80  _nb_rays_per_direction = other._nb_rays_per_direction;
81  _nb_rays_per_plane = other._nb_rays_per_plane;
82  _nb_directions = other._nb_directions;
83  _i = 0;
84  _j = 0;
85  _directions = other._directions;
86  _repere = other._repere;
87  }
88 
90  {
91  _real_nb_rays = other->_real_nb_rays;
92  _nb_rays_per_direction = other->_nb_rays_per_direction;
93  _nb_rays_per_plane = other->_nb_rays_per_plane;
94  _nb_directions = other->_nb_directions;
95  _i = 0;
96  _j = 0;
97  _directions = other->_directions;
98  _repere = other->_repere;
99  }
100 
101  virtual Sampler* Clone()
102  {
103  Sampler* sampler = new UniformPlaneSampler(this);
104  return sampler;
105  }
107  virtual ~UniformPlaneSampler() {}
108 
109  virtual vec3 getSample()
110  {
111  vec3 result(1., 0., 0.);
112  if (_i >= _nb_rays_per_direction)
113  {
114  _i = 0;
115  _j++;
116  }
117 
118  if (_j < _nb_directions)
119  {
120  vec3 dir = _directions.at(_j);
121 
122  if (_i == 0)
123  {
124 
125  // Compute the axises of the global repere
126  vec3 ax2 = vec3(0., 0., 1.); // the second axis is orthogonal to the direction and th z-axis
127  ax2.cross(dir);
128  ax2.normalize();
129 
130  vec3 ax3 = dir;
131  ax3.cross(ax2); // the second axis is orthogonal to the first one and to the directivity
132  ax3.normalize();
133 
134  // Mapping from the local repere to the global one (the third axis is the directivity and
135  // corresponds to the local z_axis)
136  _repere = Repere(dir, ax2, ax3, vec3(0., 0., 0.));
137  }
138 
139  if (_i >= _nb_rays_per_plane)
140  {
141  if (_i % (_nb_rays_per_plane / 2) == 0)
142  _i++; // skip the rays already launched on the other plane
143  Tools::fromRadianToCarthesien((_i - _nb_rays_per_plane) * M_2PI / _nb_rays_per_plane, 0,
144  result);
145  }
146  else
147  {
148  Tools::fromRadianToCarthesien(0, _i * M_2PI / _nb_rays_per_plane, result);
149  }
150  _i++;
151  }
152  // return result;
153  return _repere.vectorFromLocalToGlobal(
154  result); // rotate the sample according to the rotation between the z-axis and the directivity;
155  }
156 
157  // v is acceptable if it lies in the beam's cone
158  virtual bool isAcceptableSample(vec3 v)
159  {
160  return true;
161  }
162 
163  virtual void init()
164  {
165 
166  _nb_directions = _directions.size();
167  _nb_rays_per_direction = floor(_nb_rays / 8. + 0.5) * 8;
168  _nb_rays_per_plane = _nb_rays_per_direction / 2;
169  _real_nb_rays =
170  _nb_directions *
171  (_nb_rays_per_direction -
172  2); // We remove two rays per direction because the two planes have two rays in common
173  }
174  // Return the real number of launched rays
175  unsigned int getRealNbRays() const
176  {
177  return _real_nb_rays;
178  }
179 
180 private:
181  unsigned int _real_nb_rays;
182  unsigned int _nb_rays_per_direction;
183  unsigned int _nb_rays_per_plane;
184  unsigned int _nb_directions;
185  unsigned int _i, _j;
186  vector<vec3> _directions;
188 };
189 
190 #endif
#define M_2PI
2Pi.
Definition: 3d.h:55
Frame class.
Definition: Repere.h:26
Sampler class and its sub-classes describe ray generators used in AcousticRayTracer....
Definition: Sampler.h:30
A Sampler class for sampling rays uniformely from a cone.
virtual vec3 getSample()
Return the sample.
unsigned int _nb_rays_per_plane
Number of rays launched per plane.
virtual Sampler * Clone()
Clone a sample.
unsigned int _real_nb_rays
Real number of rays launched.
unsigned int _nb_rays_per_direction
Number of rays launched per direction.
unsigned int _nb_directions
Number of directions.
UniformPlaneSampler(const vector< vec3 > directions, const unsigned int &nbRays=8)
Constructors.
unsigned int getRealNbRays() const
Repere _repere
Repere used to rotate the beam in direction of the directivity.
UniformPlaneSampler(UniformPlaneSampler *other)
UniformPlaneSampler(const UniformPlaneSampler &other)
vector< vec3 > _directions
Directions.
virtual void init()
Initialize the sample.
virtual ~UniformPlaneSampler()
Destructor.
virtual bool isAcceptableSample(vec3 v)
Return true for an acceptable sample.
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