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