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