Code_TYMPAN  4.4.0
Industrial site acoustic simulation
DiffractionPathSelector.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 #ifndef DIFFRACTION_PATH_SELECTOR
17 #define DIFFRACTION_PATH_SELECTOR
18 
19 #include "Selector.h"
20 
25 template <typename T> class DiffractionPathSelector : public Selector<T>
26 {
27 public:
29  DiffractionPathSelector(double _maximumDelta = 8) : Selector<T>()
30  {
31  maximumDelta = _maximumDelta;
32  }
33  virtual Selector<T>* Copy()
34  {
36  newSelector->setIsDeletable(this->deletable);
37  return newSelector;
38  }
39 
40  virtual SELECTOR_RESPOND canBeInserted(T* r, unsigned long long& replace)
41  {
42  vector<boost::shared_ptr<Event>>* events = r->getEvents();
43  if (events->size() == 0)
44  {
45  return SELECTOR_ACCEPT;
46  }
47 
48  decimal cumul_delta = 0.; // Cumulative distance added by the diffractions
49  decimal cumul_distance = 0.; // Cumulative distance since last reflection
50 
51  bool notLast = true;
52  Recepteur* pRecep = static_cast<Recepteur*>(r->getRecepteur());
53  Source* pSource = r->getSource();
54 
55  vec3 origin = pRecep->getPosition();
56  vec3 current_pos = origin;
57 
58  // Iterate other the list of events in REVERSE order
59  vector<boost::shared_ptr<Event>>::reverse_iterator rit = events->rbegin();
60  while (rit != events->rend())
61  {
62  cumul_distance += (*rit)->getPosition().distance(current_pos);
63  // if current event is a reflection
64  if ((*rit)->getType() == SPECULARREFLEXION)
65  {
66  // accumulate the difference between :
67  // - the cumulative distance since last reflection
68  // and
69  // - the direct distance between the current position and the last reflection
70  //(Note: the difference between cumul_distance and the direct distance is zero if no
71  // diffraction as been encountered)
72  decimal direct_distance = origin.distance((*rit)->getPosition());
73  cumul_delta += (cumul_distance - direct_distance);
74 
75  // Reset cumul_distance
76  cumul_distance = 0;
77 
78  // Save the position of the current reflection
79  origin = (*rit)->getPosition();
80  notLast = false;
81  }
82  // if the current event is a diffraction
83  else
84  {
85 
86  notLast = true;
87  }
88 
89  current_pos = (*rit)->getPosition();
90  rit++;
91  };
92 
93  // if the first event is a diffraction finish the computation with the source
94  if (notLast)
95  {
96  // Accumulate the distance from the current position to the source
97  cumul_distance += current_pos.distance(pSource->getPosition());
98  // Computer the direct distance between the source and the first reflection
99  decimal direct_distance = origin.distance(pSource->getPosition());
100  // Compute the difference between the cumulative and the direct distances
101  cumul_delta += (cumul_distance - direct_distance);
102  }
103 
104  if (cumul_delta > maximumDelta)
105  {
106  return SELECTOR_REJECT;
107  }
108 
109  return SELECTOR_ACCEPT;
110  }
112  virtual void insert(T* r, unsigned long long& replace)
113  {
114  return;
115  }
116 
117  virtual bool insertWithTest(T* r)
118  {
119  vector<boost::shared_ptr<Event>>* events = r->getEvents();
120 
121  if (events->size() == 0)
122  {
123  return true;
124  }
125 
126  decimal cumul_delta = 0.; // Cumulative distance added by the diffractions
127  decimal cumul_distance = 0.; // Cumulative distance since last reflection
128  bool notLast = true;
129 
130  Recepteur* pRecep = static_cast<Recepteur*>(r->getRecepteur());
131  Source* pSource = r->getSource();
132 
133  vec3 origin = pRecep->getPosition();
134  vec3 current_pos = origin;
135 
136  // Iterate other the list of events in REVERSE order
137  vector<boost::shared_ptr<Event>>::reverse_iterator rit = events->rbegin();
138  while (rit != events->rend())
139  {
140  cumul_distance += (*rit)->getPosition().distance(current_pos);
141  // if current event is a reflection
142  if ((*rit)->getType() == SPECULARREFLEXION)
143  {
144  // accumulate the difference between :
145  // - the cumulative distance since last reflection
146  // and
147  // - the direct distance between the current position and the last reflection
148  //(Note: the difference between cumul_distance and the direct distance is zero if no
149  // diffraction as been encountered)
150  decimal direct_distance = origin.distance((*rit)->getPosition());
151  cumul_delta += (cumul_distance - direct_distance);
152 
153  // Reset cumul_distance
154  cumul_distance = 0;
155 
156  // Save the position of the current reflection
157  origin = (*rit)->getPosition();
158  notLast = false;
159  }
160  // if the current event is a diffraction
161  else
162  {
163 
164  notLast = true;
165  }
166 
167  current_pos = (*rit)->getPosition();
168  rit++;
169  };
170 
171  // if the first event is a diffraction finish the computation with the source
172  if (notLast)
173  {
174  // Accumulate the distance from the current position to the source
175  cumul_distance += current_pos.distance(pSource->getPosition());
176  // Computer the direct distance between the source and the first reflection
177  decimal direct_distance = origin.distance(pSource->getPosition());
178  // Compute the difference between the cumulative and the direct distances
179  cumul_delta += (cumul_distance - direct_distance);
180  }
181 
182  if (cumul_delta > maximumDelta)
183  {
184  return false;
185  }
186 
187  return true;
188  }
193  {
194  return maximumDelta;
195  }
196 
200  void setMaximumDelta(double _maximumDelta)
201  {
202  this->maximumDelta = _maximumDelta;
203  }
204 
208  virtual const char* getSelectorName()
209  {
210  return typeid(this).name();
211  }
212 
213 protected:
214  double maximumDelta;
215 };
216 
217 #endif // DIFFRACTION_PATH_SELECTOR
@ SPECULARREFLEXION
Definition: Event.h:26
SELECTOR_RESPOND
Definition: Selector.h:24
@ SELECTOR_ACCEPT
Definition: Selector.h:25
@ SELECTOR_REJECT
Definition: Selector.h:26
const char * name
: Rejects rays if the cumulative length added by the diffractions events in comparison to the length ...
virtual bool insertWithTest(T *r)
Select the ray if it respects the criteria of this Selector.
void setMaximumDelta(double _maximumDelta)
Set maximumDelta.
double maximumDelta
Maximal path length difference between rays produced by diffraction.
virtual void insert(T *r, unsigned long long &replace)
Select the ray.
virtual Selector< T > * Copy()
Copy Selector.
double getMaximumDelta()
Get maximumDelta.
virtual SELECTOR_RESPOND canBeInserted(T *r, unsigned long long &replace)
Check if the ray respects the criteria of this Selector and return a SELECTOR_RESPOND.
virtual const char * getSelectorName()
Return the class type of the selector.
DiffractionPathSelector(double _maximumDelta=8)
Constructor.
Receptor inherits from a Sphere Shape.
Definition: Recepteur.h:28
vec3 getPosition()
Get the center of the bounding box.
Definition: Recepteur.h:51
Base class for Selector (used to keep or disable rays according different criterias)
Definition: Selector.h:78
void setIsDeletable(bool _isDeletable)
Set deletable flag.
Definition: Selector.h:103
bool deletable
Flag to know if the selector may be deleted or not.
Definition: Selector.h:130
Acoustic source class.
Definition: Source.h:33
vec3 getPosition()
Get the position of the Source.
Definition: Source.h:105
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:381