Code_TYMPAN  4.4.0
Industrial site acoustic simulation
FaceSelector.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 FACE_SELECTOR
17 #define FACE_SELECTOR
18 
19 #include "Selector.h"
20 #include <map>
21 #include <vector>
22 
24 {
28 };
29 
33 template <typename T> class FaceSelector : public Selector<T>
34 {
35 public:
38  {
39  modeHistory = _modeHistory;
40  }
42  virtual ~FaceSelector() {}
43 
44  virtual Selector<T>* Copy()
45  {
46  FaceSelector* newSelector = new FaceSelector(modeHistory);
47  newSelector->setIsDeletable(this->deletable);
48  return newSelector;
49  }
50  virtual void reset()
51  {
52  selectedPath.clear();
53  return;
54  }
55 
60  {
61  return modeHistory;
62  }
63 
67  void setModeHistory(TYPEHISTORY _modeHistory)
68  {
69  if (modeHistory != _modeHistory)
70  {
71  modeHistory = _modeHistory;
72  selectedPath.clear();
73  }
74  }
75 
76  virtual SELECTOR_RESPOND canBeInserted(T* r, unsigned long long& replace)
77  {
78  typename std::map<std::vector<unsigned int>, T*, CompareToKey>::iterator it;
79  std::vector<unsigned int> path;
80 
81  // Get the history according to the TYPEHISTORY selected
82  switch (modeHistory)
83  {
84  case HISTORY_FACE:
85  path = r->getFaceHistory();
86  break;
87  case HISTORY_PRIMITIVE:
88  path = r->getPrimitiveHistory();
89  break;
90  default:
91  path = r->getFaceHistory();
92  break;
93  }
94 
95  it = selectedPath.find(
96  path); // search for an equivalent history among the histories of already selected rays
97 
98  // if there already is a ray with the same history
99  if (it != selectedPath.end())
100  {
101  r->computeLongueur();
102  double currentDistance = r->getLongueur();
103  // if current ray has a shorter length than the already selected one
104  if (currentDistance < it->second->getLongueur())
105  {
106  // replace the older ray by the new one
107  replace = it->second->getConstructId();
108  return SELECTOR_REPLACE;
109  }
110  else
111  {
112  return SELECTOR_REJECT;
113  }
114  }
115  return SELECTOR_ACCEPT;
116  }
117 
118  virtual void insert(T* r)
119  {
120  typename std::map<std::vector<unsigned int>, T*, CompareToKey>::iterator it;
121  std::vector<unsigned int> path;
122  switch (modeHistory)
123  {
124  case HISTORY_FACE:
125  path = r->getFaceHistory();
126  break;
127  case HISTORY_PRIMITIVE:
128  path = r->getPrimitiveHistory();
129  break;
130  default:
131  path = r->getFaceHistory();
132  break;
133  }
134 
135  it = selectedPath.find(
136  path); // search for an equivalent history among the histories of already selected rays
137  r->computeLongueur();
138 
139  // if there already is a ray with the same history
140  if (it != selectedPath.end())
141  {
142  it->second = r;
143 
144  return;
145  }
146  else
147  {
148  // if none of the already selected rays has the same history than r, then add the ray and its
149  // hsitory to selectedPath
150  selectedPath.insert(std::pair<std::vector<unsigned int>, T*>(path, r));
151  }
152 
153  return;
154  }
155 
156  virtual bool insertWithTest(T* r)
157  {
158  typename std::map<std::vector<unsigned int>, T*, CompareToKey>::iterator it;
159  std::vector<unsigned int> path;
160  switch (modeHistory)
161  {
162  case HISTORY_FACE:
163  path = r->getFaceHistory();
164  break;
165  case HISTORY_PRIMITIVE:
166  path = r->getPrimitiveHistory();
167  break;
168  default:
169  path = r->getFaceHistory();
170  break;
171  }
172 
173  it = selectedPath.find(
174  path); // search for an equivalent history among the histories of already selected rays
175  r->computeLongueur();
176  double currentDistance = r->getLongueur();
177 
178  // if there already is a ray with the same history
179  if (it != selectedPath.end())
180  {
181  if (currentDistance < it->second->getLongueur())
182  {
183  it->second = r;
184  return true;
185  }
186  else
187  {
188  return false;
189  }
190  }
191  else
192  {
193  // if none of the already selected rays has the same history than r, then add the ray and its
194  // hsitory to selectedPath
195  selectedPath.insert(std::pair<std::vector<unsigned int>, T*>(path, r));
196  return true;
197  }
198  }
199 
203  virtual const char* getSelectorName()
204  {
205  return typeid(this).name();
206  }
207 
208 protected:
209  std::map<std::vector<unsigned int>, T*, CompareToKey>
212 };
213 
214 #endif
TYPEHISTORY
Definition: FaceSelector.h:24
@ HISTORY_BUILDING
Definition: FaceSelector.h:27
@ HISTORY_PRIMITIVE
Definition: FaceSelector.h:26
@ HISTORY_FACE
Definition: FaceSelector.h:25
SELECTOR_RESPOND
Definition: Selector.h:24
@ SELECTOR_ACCEPT
Definition: Selector.h:25
@ SELECTOR_REJECT
Definition: Selector.h:26
@ SELECTOR_REPLACE
Definition: Selector.h:27
const char * name
: To keep only one from two or more rays which have the same history (events on the same primitive)
Definition: FaceSelector.h:34
virtual void insert(T *r)
Select the ray.
Definition: FaceSelector.h:118
std::map< std::vector< unsigned int >, T *, CompareToKey > selectedPath
Histories of all selected rays so far.
Definition: FaceSelector.h:210
virtual const char * getSelectorName()
Return the class type of the selector.
Definition: FaceSelector.h:203
virtual bool insertWithTest(T *r)
Select the ray if it respects the criteria of this Selector.
Definition: FaceSelector.h:156
TYPEHISTORY getModeHistory()
Get the TYPEHISTORY of this Selector.
Definition: FaceSelector.h:59
void setModeHistory(TYPEHISTORY _modeHistory)
Set the TYPEHISTORY of this Selector.
Definition: FaceSelector.h:67
TYPEHISTORY modeHistory
TYPEHISTORY used by this Selector (by default, HISTORY_FACE)
Definition: FaceSelector.h:211
virtual void reset()
Reset (clear the data) of this Selector.
Definition: FaceSelector.h:50
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.
Definition: FaceSelector.h:76
FaceSelector(TYPEHISTORY _modeHistory=HISTORY_FACE)
Constructor.
Definition: FaceSelector.h:37
virtual ~FaceSelector()
Destructor.
Definition: FaceSelector.h:42
virtual Selector< T > * Copy()
Copy Selector.
Definition: FaceSelector.h:44
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