Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Source.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 SOURCE_H
17 #define SOURCE_H
18 
19 #include <string>
20 #include <iostream>
21 
22 #include "Geometry/Sampler.h"
23 #include "Base.h"
24 #ifdef _ALLOW_TARGETING_
25  #include "Tools/TargetManager.h"
26 #endif //_ALLOW_TARGETING_
27 #include "Spectre.h"
28 
32 class Source : public Base
33 {
34 public:
35 #ifdef _ALLOW_TARGETING_
36  Source(std::string _name = "unknown source") : Base(), sampler(NULL), targetManager(NULL)
37  {
38  name = _name;
39  }
40  Source(const Source& other) : Base(other)
41  {
42  name = std::string(other.name);
43  pos = vec3(other.pos);
45  if (other.sampler)
46  {
47  sampler = other.sampler->Clone();
48  }
49  else
50  {
51  sampler = NULL;
52  }
53  nbRayLeft = other.nbRayLeft;
55  targetManager = other.targetManager;
56  for (unsigned int i = 0; i < other.directions.size(); i++)
57  {
58  directions.push_back(other.directions.at(i));
59  }
60  }
61 #else
63  Source(std::string _name = "unknown source") : Base(), sampler(NULL)
64  {
65  name = _name;
66  nbRayLeft = 0;
67  initialRayCount = 0;
68  id = -1;
69  indice = -1;
70  }
72  Source(const Source& other) : Base(other)
73  {
74  name = std::string(other.name);
75  pos = vec3(other.pos);
77  if (other.sampler)
78  {
79  sampler = other.sampler->Clone();
80  }
81  else
82  {
83  sampler = NULL;
84  }
85  nbRayLeft = other.nbRayLeft;
87  id = other.id;
88  indice = other.indice;
89  }
90 #endif
92  virtual ~Source() {}
93 
95  std::string getName()
96  {
97  return name;
98  }
100  void setName(const std::string& _name)
101  {
102  name = std::string(_name);
103  }
106  {
107  return vec3(pos);
108  }
110  void setPosition(const vec3 _pos)
111  {
112  pos = vec3(_pos);
113  }
116  {
117  return spectrePuissance;
118  }
120  void setSpectre(const Spectre& spectre)
121  {
122  spectrePuissance = Spectre(spectre);
123  }
124 
127  {
128  return nbRayLeft;
129  }
131  void setNbRayLeft(int nb)
132  {
133  nbRayLeft = nb;
135  {
137  }
138  }
141  {
142  return initialRayCount;
143  }
145  void setInitialRayCount(int nb)
146  {
147  initialRayCount = nb;
148  nbRayLeft = nb;
149  }
152  {
153  return sampler;
154  }
156  void setSampler(Sampler* _sampler)
157  {
158  sampler = _sampler;
159  }
161  unsigned int getId()
162  {
163  return id;
164  }
166  void setId(unsigned int _id)
167  {
168  id = _id;
169  }
172  {
173 #ifdef _ALLOW_TARGETING_
174  if (nbRayLeft <= 0)
175  {
176  return false;
177  }
178  nbRayLeft--;
179  if (!directions.empty())
180  {
181  r = vec3(directions.back());
182  directions.pop_back();
183  }
184  // else if(targetCount != 0){
185  // targetCount--;
186  // std::vector<vec3>& targets = targetManager->getTargets();
187  // unsigned int indexTarget = rand() % targets.size();
188  // r = vec3(targets.at(indexTarget) - pos);
189  // r.normalize();
190  //}
191  else
192  {
193  r = vec3(sampler->getSample());
194  }
195 #else
196  if (nbRayLeft <= 0)
197  {
198  return false;
199  }
200 
201  nbRayLeft--;
202 
203  r = vec3(sampler->getSample());
204 #endif // _ALLOW_TARGETING_
205 
206  // std::cout<<"Renvoie d'une nouvelle direction."<<std::endl;
207  return true;
208  }
209 
210 protected:
211  std::string name;
215  int nbRayLeft;
217  unsigned int id;
218  int indice;
219 
220 #ifdef _ALLOW_TARGETING_
221 public:
222  void setTargetManager(TargetManager* _targetManager)
223  {
224  targetManager = _targetManager;
225  }
226 
227  void setInitialTargetCount(unsigned int nb)
228  {
229  initialTargetCount = nb;
230  targetCount = nb;
231  }
232  unsigned int getInitialTargetCount()
233  {
234  return initialTargetCount;
235  }
236  void addDirection(vec3 dir)
237  {
238  directions.push_back(dir);
239  }
240 
241  std::vector<vec3>& getPrecomputedDirections()
242  {
243  return directions;
244  }
245 
246 protected:
247  unsigned int initialTargetCount;
248  unsigned int targetCount;
249  std::vector<vec3> directions;
250  TargetManager* targetManager;
251 #endif //_ALLOW_TARGETING_
252 };
253 #endif
Base class of Event, Material, PostFilter, Ray, Repere, Scene, Shape, Simulation, Source.
Definition: Base.h:25
Base()
Default constructor.
Definition: Base.h:29
Sampler class and its sub-classes describe ray generators used in AcousticRayTracer....
Definition: Sampler.h:30
virtual Sampler * Clone()
Clone a sample.
Definition: Sampler.h:55
virtual vec3 getSample()
Return the sample.
Definition: Sampler.h:64
Acoustic source class.
Definition: Source.h:33
int getInitialRayCount()
Get the initial rays counter.
Definition: Source.h:140
void setSampler(Sampler *_sampler)
Set the Sampler for this Source.
Definition: Source.h:156
void setId(unsigned int _id)
Set the Source id.
Definition: Source.h:166
vec3 getPosition()
Get the position of the Source.
Definition: Source.h:105
void setPosition(const vec3 _pos)
Set the position of the Source.
Definition: Source.h:110
int nbRayLeft
Number of remaining rays to generate.
Definition: Source.h:215
void setInitialRayCount(int nb)
Set the initial rays counter.
Definition: Source.h:145
int getNbRayLeft()
Get the number of remaining rays to generate.
Definition: Source.h:126
int indice
Index to know which ray we are working on.
Definition: Source.h:218
std::string name
Name.
Definition: Source.h:211
Spectre spectrePuissance
Spectrum.
Definition: Source.h:213
bool getDirection(vec3 &r)
Get a direction (return false if all rays has been launched)
Definition: Source.h:171
Sampler * getSampler()
Get the Sampler of this Source.
Definition: Source.h:151
int initialRayCount
Initial rays counter.
Definition: Source.h:216
Spectre getSpectre()
Get the spectrum.
Definition: Source.h:115
void setSpectre(const Spectre &spectre)
Set the spectrum.
Definition: Source.h:120
unsigned int getId()
Get the Source id.
Definition: Source.h:161
Source(std::string _name="unknown source")
Base constructor.
Definition: Source.h:63
void setNbRayLeft(int nb)
Set the number of remaining rays to generate.
Definition: Source.h:131
unsigned int id
Source identification.
Definition: Source.h:217
Sampler * sampler
A pointer to a Sampler.
Definition: Source.h:214
void setName(const std::string &_name)
Set the name of the Source.
Definition: Source.h:100
std::string getName()
Get the name of the Source.
Definition: Source.h:95
Source(const Source &other)
Copy constructor.
Definition: Source.h:72
vec3 pos
Position.
Definition: Source.h:212
virtual ~Source()
Destructor.
Definition: Source.h:92
Spectrum class.
Definition: Spectre.h:25
Class to manage targets.
Definition: TargetManager.h:27
base_vec3< decimal > vec3
Definition: mathlib.h:381