Code_TYMPAN  4.2.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) ) { return SELECTOR_ACCEPT; }
47 
48  vec3 beginPos = r->getSource()->getPosition();
49  vec3 currentPos, nextPos, N, N1, N2, W, From, To;
50 
51  Diffraction *pDiff = NULL;
52 
53  decimal F1 = 0., F2 = 0., T1 = 0., T2 = 0., FT = 0.;
54 
55  vector<boost::shared_ptr<Event> >::iterator iter = events->begin();
56  do
57  {
58  if ( (*iter)->getType() != DIFFRACTION )
59  {
60  beginPos = (*iter)->getPosition();
61  iter++;
62  continue;
63  }
64 
65  currentPos = (*iter)->getPosition();
66 
67  pDiff = dynamic_cast<Diffraction*>( (*iter).get() );
68 
69 
70  N1 = dynamic_cast<Cylindre*>(pDiff->getShape())->getFirstShape()->getNormal();
71  N2 = dynamic_cast<Cylindre*>(pDiff->getShape())->getSecondShape()->getNormal();
72 
73  From = (currentPos - beginPos);
74  From.normalize();
75 
76  if ( (iter+1) != events->end() )
77  {
78  nextPos = (*(iter+1)).get()->getPosition();
79  }
80  else
81  {
82  nextPos = static_cast<Recepteur*> (r->getRecepteur())->getPosition();
83  }
84 
85  To = (nextPos - currentPos);
86  To.normalize();
87 
88  FT = From * To;
89 
90  //Accept the ray if FROM and TO are colinear and point in the same direction
91  if ( ( 1. - FT ) < EPSILON_4 ) { return SELECTOR_ACCEPT; }
92 
93  //Reject the ray if FROM and TO point in opposite directions
94  if ( FT < 0. ) { return SELECTOR_REJECT; }
95 
96  F1 = From * N1;
97  F2 = From * N2;
98 
99  //Reject the ray if its incoming direction is such that there is no shadow zone
100  if ( (F1 * F2) > 0.) { return SELECTOR_REJECT; }
101 
102  T1 = To * N1;
103  T2 = To * N2;
104 
105  //Reject the ray if TO is not in the shadow zone of the obstacle
106  if ( (F1 <= 0.) && ( (T1 > EPSILON_4 ) || ( (T2 - F2) > EPSILON_4 ) ) )
107  {
108  return SELECTOR_REJECT;
109  }
110 
111  if ( (F2 <= 0.) && ( ( T2 > EPSILON_4 ) || ( (T1 - F1) > EPSILON_4 ) ) )
112  {
113  return SELECTOR_REJECT;
114  }
115 
116  beginPos = currentPos;
117  iter++;
118  }
119  while( iter != events->end() );
120 
121  return SELECTOR_ACCEPT;
122  }
124  virtual void insert(T* r, unsigned long long& replace) { return; }
125 
126  virtual bool insertWithTest(T* r)
127  {
128  vector<boost::shared_ptr<Event> >* events = r->getEvents();
129 
130  if ( (events->size() == 0) || (r->getDiff() == 0) ) { return true; }
131 
132  vec3 beginPos = r->getSource()->getPosition();
133  vec3 currentPos, nextPos, N, N1, N2, W, From, To;
134 
135  Diffraction *pDiff = NULL;
136 
137  decimal F1 = 0., F2 = 0., T1 = 0., T2 = 0., FT = 0.;
138 
139  vector<boost::shared_ptr<Event> >::iterator iter = events->begin();
140  do
141  {
142  if ( (*iter)->getType() != DIFFRACTION )
143  {
144  beginPos = (*iter)->getPosition();
145  iter++;
146  continue;
147  }
148 
149  currentPos = (*iter)->getPosition();
150 
151  pDiff = dynamic_cast<Diffraction*>( (*iter).get() );
152 
153  N1 = dynamic_cast<Cylindre*>(pDiff->getShape())->getFirstShape()->getNormal();
154  N2 = dynamic_cast<Cylindre*>(pDiff->getShape())->getSecondShape()->getNormal();
155 
156  From = (currentPos - beginPos);
157  From.normalize();
158 
159  if ( (iter+1) != events->end() )
160  {
161  nextPos = (*(iter+1)).get()->getPosition();
162  }
163  else
164  {
165  nextPos = static_cast<Recepteur*> (r->getRecepteur())->getPosition();
166  }
167 
168  To = (nextPos - currentPos);
169  To.normalize();
170 
171  FT = From * To;
172 
173  //Return true if FROM and TO are colinear and point in the same direction
174  if ( ( 1. - FT ) < EPSILON_4 ) { return true; }
175 
176  //Return false if FROM and TO point in opposite directions
177  if ( FT < 0. ) { return false; }
178 
179  F1 = From * N1;
180  F2 = From * N2;
181 
182  //Return false if its incoming direction is such that there is no shadow zone
183  if ( (F1 * F2) > 0.) { return false; }
184 
185  T1 = To * N1;
186  T2 = To * N2;
187 
188  //Return false if TO is not in the shadow zone of the obstacle
189  if ( (F1 <= 0.) && ( (T1 > EPSILON_4 ) || ( (T2 - F2) > EPSILON_4 ) ) )
190  {
191  return false;
192  }
193 
194  if ( (F2 <= 0.) && ( ( T2 > EPSILON_4 ) || ( (T1 - F1) > EPSILON_4 ) ) )
195  {
196  return false;
197  }
198 
199  beginPos = currentPos;
200  iter++;
201  }
202  while( iter != events->end() );
203 
204  return true;
205  }
206 
210  virtual const char* getSelectorName(){
211  return typeid(this).name();
212  }
213 };
214 
215 #endif //DIFFRACTION_ANGLE_SELECTOR
Shape * getShape()
Return the primitive of the impact.
Definition: Event.h:116
const char * name
Cylinder class.
Definition: Cylindre.h:26
SELECTOR_RESPOND
Definition: Selector.h:23
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 Selector< T > * Copy()
Copy Selector.
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.
Base class for Selector (used to keep or disable rays according different criterias) ...
Definition: Selector.h:77
float decimal
Definition: mathlib.h:46
#define EPSILON_4
Definition: mathlib.h:51
virtual vec3 getNormal(const vec3 pos=vec3())
Get normal.
Definition: Shape.h:115
Diffraction class Event.
Definition: Diffraction.h:30
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.
void setIsDeletable(bool _isDeletable)
Set deletable flag.
Definition: Selector.h:92
virtual void insert(T *r, unsigned long long &replace)
Select the ray.
Receptor inherits from a Sphere Shape.
Definition: Recepteur.h:27
: Select diffracted rays that are launched in the shadow zone of the obstacle (closed angle) Other ar...