Code_TYMPAN  4.4.0
Industrial site acoustic simulation
DiffractionAngleSelector.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_ANGLE_SELECTOR
17 #define DIFFRACTION_ANGLE_SELECTOR
18 
19 #include "Selector.h"
20 #include "Geometry/3d.h"
21 
27 template <typename T>
28 
30 {
31 public:
34  virtual Selector<T>* Copy()
35  {
37  newSelector->setIsDeletable(this->deletable);
38  return newSelector;
39  }
40 
41  virtual SELECTOR_RESPOND canBeInserted(T* r, unsigned long long& replace)
42  {
43  vector<boost::shared_ptr<Event>>* events = r->getEvents();
44 
45  // The ray has is accepted if it has no diffraction event
46  if ((events->size() == 0) || (r->getDiff() == 0))
47  {
48  return SELECTOR_ACCEPT;
49  }
50 
51  vec3 beginPos = r->getSource()->getPosition();
52  vec3 currentPos, nextPos, N, N1, N2, W, From, To;
53 
54  Diffraction* pDiff = NULL;
55 
56  decimal F1 = 0., F2 = 0., T1 = 0., T2 = 0., FT = 0.;
57 
58  vector<boost::shared_ptr<Event>>::iterator iter = events->begin();
59  do
60  {
61  if ((*iter)->getType() != DIFFRACTION)
62  {
63  beginPos = (*iter)->getPosition();
64  iter++;
65  continue;
66  }
67 
68  currentPos = (*iter)->getPosition();
69 
70  pDiff = dynamic_cast<Diffraction*>((*iter).get());
71 
72  N1 = dynamic_cast<Cylindre*>(pDiff->getShape())->getFirstShape()->getNormal();
73  N2 = dynamic_cast<Cylindre*>(pDiff->getShape())->getSecondShape()->getNormal();
74 
75  From = (currentPos - beginPos);
76  From.normalize();
77 
78  if ((iter + 1) != events->end())
79  {
80  nextPos = (*(iter + 1)).get()->getPosition();
81  }
82  else
83  {
84  nextPos = static_cast<Recepteur*>(r->getRecepteur())->getPosition();
85  }
86 
87  To = (nextPos - currentPos);
88  To.normalize();
89 
90  FT = From * To;
91 
92  // Accept the ray if FROM and TO are colinear and point in the same direction
93  if ((1. - FT) < EPSILON_4)
94  {
95  return SELECTOR_ACCEPT;
96  }
97 
98  // Reject the ray if FROM and TO point in opposite directions
99  if (FT < 0.)
100  {
101  return SELECTOR_REJECT;
102  }
103 
104  F1 = From * N1;
105  F2 = From * N2;
106 
107  // Reject the ray if its incoming direction is such that there is no shadow zone
108  if ((F1 * F2) > 0.)
109  {
110  return SELECTOR_REJECT;
111  }
112 
113  T1 = To * N1;
114  T2 = To * N2;
115 
116  // Reject the ray if TO is not in the shadow zone of the obstacle
117  if ((F1 <= 0.) && ((T1 > EPSILON_4) || ((T2 - F2) > EPSILON_4)))
118  {
119  return SELECTOR_REJECT;
120  }
121 
122  if ((F2 <= 0.) && ((T2 > EPSILON_4) || ((T1 - F1) > EPSILON_4)))
123  {
124  return SELECTOR_REJECT;
125  }
126 
127  beginPos = currentPos;
128  iter++;
129  } while (iter != events->end());
130 
131  return SELECTOR_ACCEPT;
132  }
134  virtual void insert(T* r, unsigned long long& replace)
135  {
136  return;
137  }
138 
139  virtual bool insertWithTest(T* r)
140  {
141  vector<boost::shared_ptr<Event>>* events = r->getEvents();
142 
143  if ((events->size() == 0) || (r->getDiff() == 0))
144  {
145  return true;
146  }
147 
148  vec3 beginPos = r->getSource()->getPosition();
149  vec3 currentPos, nextPos, N, N1, N2, W, From, To;
150 
151  Diffraction* pDiff = NULL;
152 
153  decimal F1 = 0., F2 = 0., T1 = 0., T2 = 0., FT = 0.;
154 
155  vector<boost::shared_ptr<Event>>::iterator iter = events->begin();
156  do
157  {
158  if ((*iter)->getType() != DIFFRACTION)
159  {
160  beginPos = (*iter)->getPosition();
161  iter++;
162  continue;
163  }
164 
165  currentPos = (*iter)->getPosition();
166 
167  pDiff = dynamic_cast<Diffraction*>((*iter).get());
168 
169  N1 = dynamic_cast<Cylindre*>(pDiff->getShape())->getFirstShape()->getNormal();
170  N2 = dynamic_cast<Cylindre*>(pDiff->getShape())->getSecondShape()->getNormal();
171 
172  From = (currentPos - beginPos);
173  From.normalize();
174 
175  if ((iter + 1) != events->end())
176  {
177  nextPos = (*(iter + 1)).get()->getPosition();
178  }
179  else
180  {
181  nextPos = static_cast<Recepteur*>(r->getRecepteur())->getPosition();
182  }
183 
184  To = (nextPos - currentPos);
185  To.normalize();
186 
187  FT = From * To;
188 
189  // Return true if FROM and TO are colinear and point in the same direction
190  if ((1. - FT) < EPSILON_4)
191  {
192  return true;
193  }
194 
195  // Return false if FROM and TO point in opposite directions
196  if (FT < 0.)
197  {
198  return false;
199  }
200 
201  F1 = From * N1;
202  F2 = From * N2;
203 
204  // Return false if its incoming direction is such that there is no shadow zone
205  if ((F1 * F2) > 0.)
206  {
207  return false;
208  }
209 
210  T1 = To * N1;
211  T2 = To * N2;
212 
213  // Return false if TO is not in the shadow zone of the obstacle
214  if ((F1 <= 0.) && ((T1 > EPSILON_4) || ((T2 - F2) > EPSILON_4)))
215  {
216  return false;
217  }
218 
219  if ((F2 <= 0.) && ((T2 > EPSILON_4) || ((T1 - F1) > EPSILON_4)))
220  {
221  return false;
222  }
223 
224  beginPos = currentPos;
225  iter++;
226  } while (iter != events->end());
227 
228  return true;
229  }
230 
234  virtual const char* getSelectorName()
235  {
236  return typeid(this).name();
237  }
238 };
239 
240 #endif // DIFFRACTION_ANGLE_SELECTOR
@ DIFFRACTION
Definition: Event.h:27
SELECTOR_RESPOND
Definition: Selector.h:24
@ SELECTOR_ACCEPT
Definition: Selector.h:25
@ SELECTOR_REJECT
Definition: Selector.h:26
const char * name
Cylinder class.
Definition: Cylindre.h:27
: Select diffracted rays that are launched in the shadow zone of the obstacle (closed angle) Other ar...
virtual bool insertWithTest(T *r)
Select the ray if it respects the criteria of this Selector.
virtual const char * getSelectorName()
Return the class type of the selector.
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 void insert(T *r, unsigned long long &replace)
Select the ray.
virtual Selector< T > * Copy()
Copy Selector.
Diffraction class Event.
Definition: Diffraction.h:31
Shape * getShape()
Return the primitive of the impact.
Definition: Event.h:143
Receptor inherits from a Sphere Shape.
Definition: Recepteur.h:28
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
virtual vec3 getNormal(const vec3 pos=vec3())
Get normal.
Definition: Shape.h:145
#define EPSILON_4
Definition: mathlib.h:50
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:381