Code_TYMPAN  4.4.0
Industrial site acoustic simulation
CoPlanaritySelector.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 COPLANARITY_SELECTOR
17 #define COPLANARITY_SELECTOR
18 
19 #include "Selector.h"
20 #include <map>
21 #include <vector>
22 
26 template <typename T> class CoPlanaritySelector : public Selector<T>
27 {
28 public:
32  virtual ~CoPlanaritySelector() {}
33 
34  virtual Selector<T>* Copy()
35  {
36  CoPlanaritySelector* newSelector = new CoPlanaritySelector();
37  newSelector->setIsDeletable(this->deletable);
38  return newSelector;
39  }
40  virtual void reset()
41  {
42  selectedRays.clear();
43  return;
44  }
45 
46  bool haveCoPlanarEvents(T* r1, T* r2)
47  {
48 
49  if (r1->getEvents()->size() == r2->getEvents()->size())
50  {
51 
52  for (unsigned int i = 0; i < r1->getEvents()->size(); i++)
53  {
54  Event* ev1 = r1->getEvents()->at(i).get();
55  Event* ev2 = r2->getEvents()->at(i).get();
56 
57  if (coPlanarityTest(ev1, ev2))
58  return true;
59  }
60  }
61  return false;
62  }
63 
64  bool areBothReflections(Event* ev1, Event* ev2)
65  {
66  return ev1->getType() == SPECULARREFLEXION && ev2->getType() == SPECULARREFLEXION;
67  }
68 
69  bool coPlanarityTest(Event* ev1, Event* ev2)
70  {
71 
72  // If any of the two events is not a reflection, events are not co-planar
73  if (areBothReflections(ev1, ev2))
74  {
75 
76  vec3 n1 = ev1->getShape()->getNormal();
77  vec3 n2 = ev2->getShape()->getNormal();
78 
79  // Check if the normals are equal
80  if (n1.compare(n2))
81  {
82  vec3 v = ev2->getPosition() - ev1->getPosition();
83  v.normalize();
84 
85  // Check if the faces are in the same plane
86  if (v * n1 < EPSILON_4 && v * n2 < EPSILON_4)
87  return true;
88  }
89  }
90 
91  return false;
92  }
93 
94  virtual SELECTOR_RESPOND canBeInserted(T* r, unsigned long long& replace)
95  {
96 
97  typename std::map<std::vector<unsigned int>, vector<T*>, CompareToKey>::iterator it;
98 
99  std::vector<unsigned int> event_signature = r->getEventSignature();
100 
101  it = selectedRays.find(event_signature);
102 
103  // if there already is a ray with the same event signatures
104  if (it != selectedRays.end())
105  {
106  r->computeLongueur();
107  // double currentDistance = r->getLongueur();
108 
109  vector<T*> rays = it->second;
110  cerr << "rays : " << rays.size() << endl;
111  /*for(unsigned int i=0;i<rays.size();i++){
112 
113 
114  Ray* r2 = rays.at(i);
115  if (currentDistance < r2->getLongueur())
116  {
117  //replace the older ray by the new one
118  replace = r2->getConstructId();
119  return SELECTOR_REPLACE;
120  }
121  else
122  {
123  return SELECTOR_REJECT;
124  }
125 
126  } */
127  return SELECTOR_REJECT;
128  }
129  return SELECTOR_ACCEPT;
130  }
131 
132  virtual void insert(T* r)
133  {
134  typename std::map<std::vector<unsigned int>, vector<T*>, CompareToKey>::iterator it;
135 
136  std::vector<unsigned int> event_signature = r->getEventSignature();
137 
138  it = selectedRays.find(event_signature);
139 
140  // if there already is a ray with the same event signatures
141  if (it != selectedRays.end())
142  {
143  r->computeLongueur();
144  // double currentDistance = r->getLongueur();
145 
146  vector<T*> rays = it->second;
147  cerr << "rays : " << rays.size() << endl;
148 
149  return;
150  }
151  else
152  {
153  // if none of the already selected rays has the same history than r, then add the ray and its
154  // hsitory to selectedPath
155  vector<T*> vec;
156  vec.push_back(r);
157  selectedRays.insert(std::pair<std::vector<unsigned int>, vector<T*>>(event_signature, vec));
158  }
159 
160  return;
161  }
162 
163  virtual bool insertWithTest(T* r)
164  {
165  typename std::map<std::vector<unsigned int>, vector<T*>, CompareToKey>::iterator it;
166 
167  std::vector<unsigned int> event_signature = r->getEventSignature();
168 
169  it = selectedRays.find(event_signature);
170 
171  // if there already is a ray with the same event signatures
172  if (it != selectedRays.end())
173  {
174  r->computeLongueur();
175  // double currentDistance = r->getLongueur();
176 
177  vector<T*> rays = it->second;
178  cerr << "rays : " << rays.size() << endl;
179 
180  return false;
181  }
182  else
183  {
184  // if none of the already selected rays has the same history than r, then add the ray and its
185  // hsitory to selectedPath
186  vector<T*> vec;
187  vec.push_back(r);
188  selectedRays.insert(std::pair<std::vector<unsigned int>, vector<T*>>(event_signature, vec));
189  return true;
190  }
191 
192  return false;
193  }
194 
198  virtual const char* getSelectorName()
199  {
200  return typeid(this).name();
201  }
202 
203 protected:
204  std::map<std::vector<unsigned int>, vector<T*>, CompareToKey>
206 };
207 
208 #endif
@ 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
: To keep only one from two or more rays which have the same history (events on the same primitive)
virtual ~CoPlanaritySelector()
Destructor.
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)
Select the ray.
bool haveCoPlanarEvents(T *r1, T *r2)
virtual Selector< T > * Copy()
Copy Selector.
CoPlanaritySelector()
Constructor.
virtual const char * getSelectorName()
Return the class type of the selector.
bool coPlanarityTest(Event *ev1, Event *ev2)
bool areBothReflections(Event *ev1, Event *ev2)
std::map< std::vector< unsigned int >, vector< T * >, CompareToKey > selectedRays
map of all event signatures with their corresponding rays
virtual void reset()
Reset (clear the data) of this Selector.
virtual bool insertWithTest(T *r)
Select the ray if it respects the criteria of this Selector.
Class describing an event (reflection, diffraction, ...)
Definition: Event.h:37
Shape * getShape()
Return the primitive of the impact.
Definition: Event.h:143
virtual int getType() const
Return the event type.
Definition: Event.h:205
const vec3 & getPosition() const
Return a reference to the event location point.
Definition: Event.h:82
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
base_vec3< decimal > vec3
Definition: mathlib.h:381