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