Code_TYMPAN  4.4.0
Industrial site acoustic simulation
DefaultEngine.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 DEFAULT_ENGINE_H
17 #define DEFAULT_ENGINE_H
18 
19 #include "Engine.h"
20 #include <stack>
21 
25 class DefaultEngine : public Engine
26 {
27 #ifdef TEST_ACCELERATION_RECEPTORS
28 public:
31  {
32  nbRayonsTraites = 0;
33  }
34 
35  DefaultEngine(Scene* _scene, std::vector<Source>* _sources, Solver* _solver, Scene* _recepteurs)
36  : Engine(_scene, _sources, _solver, _recepteurs)
37  {
38  }
41  {
42  scene = other.scene;
43  sources = other.sources;
44  solver = other.solver;
45  recepteurs = other.recepteurs;
46  }
48  virtual ~DefaultEngine() {}
49 
50  virtual bool process();
51 
52  virtual void runStructureBenchmark();
53 
54  Ray* genRay();
55 
56 protected:
64  void searchForReceptor(const decimal& tmin, Ray* r)
65  {
66  // Get the receptors' accelerating structure
67  Accelerator* accelerator = recepteurs->getAccelerator();
68  std::list<Intersection> foundPrims;
69 
70  // Find intersections with receptors
71  accelerator->traverse(r, foundPrims);
72  std::list<Intersection>::iterator iter;
73 
74  // For each found intersections
75  for (iter = foundPrims.begin(); iter != foundPrims.end(); iter++)
76  {
77 
78  Intersection its = (*iter);
79  Intersection result;
80 
81  vec3 position = r->getPosition();
82  vec3 direction = r->getDirection();
83 
84  // If the ray intersect the receptor before traveling a tmin distance
85  if (dynamic_cast<Recepteur*>(its.p)->intersectionRecepteur(position, direction, tmin, result))
86  {
87  // Create a copy of the ray
88  Ray* valide_ray = new Ray(r);
89  valide_ray->setConstructId(rayCounter);
90 
91  rayCounter++;
92 
93  // Set the ray's receptor
94  valide_ray->setRecepteur(dynamic_cast<Recepteur*>(its.p));
95 
96  // Compute the final position of the ray based on its direction and the distance traveled
97  valide_ray->setFinalPosition(valide_ray->getPosition() +
98  valide_ray->getDirection() * result.t);
99  valide_ray->computeLongueur();
100 
101  // Add the ray to the solver's list of valid rays
102  solver->valideRayon(valide_ray);
103  }
104  }
105  }
110  {
111  for (vector<Source>::iterator itsource = sources->begin(); itsource != sources->end(); itsource++)
112  {
113  std::vector<Shape*>* tabReceptors = recepteurs->getShapes();
114  for (std::vector<Shape*>::iterator itrecp = tabReceptors->begin(); itrecp != tabReceptors->end();
115  itrecp++)
116  {
117  Ray* new_ray = new Ray();
118  new_ray->setConstructId(rayCounter);
119  rayCounter++;
120  new_ray->setSource((&(*itsource)));
121  new_ray->setPosition(itsource->getPosition());
122  vec3 psource = itsource->getPosition();
123  vec3 precp = dynamic_cast<Sphere*>((*itrecp))->getPosition();
124  vec3 direction = precp - psource;
125  direction.normalize();
126  new_ray->setDirection(direction);
127  new_ray->setMint(0.);
128  new_ray->setMaxt(10000.);
129  if (itsource->getSampler()->isAcceptableSample(direction))
130  pile_traitement.push(new_ray);
131  }
132  }
133  }
134 #else
135 public:
136  DefaultEngine() : Engine()
137  {
138  nbRayonsTraites = 0;
139  }
140 
141  DefaultEngine(Scene* _scene, std::vector<Source>* _sources, Solver* _solver,
142  std::vector<Recepteur>* _recepteurs)
143  : Engine(_scene, _sources, _solver, _recepteurs)
144  {
145  }
146 
147  DefaultEngine(const DefaultEngine& other)
148  {
149  scene = other.scene;
150  sources = other.sources;
151  solver = other.solver;
152  recepteurs = other.recepteurs;
153  }
154 
155  virtual ~DefaultEngine() {}
156 
157  virtual bool process();
158 
159  virtual void runStructureBenchmark();
160 
161  Ray* genRay();
162 
163 protected:
164  void searchForReceptor(const decimal& tmin, Ray* r)
165  {
166  for (unsigned int i = 0; i < recepteurs->size(); i++)
167  {
168  Intersection result;
169  // if the ray hits a receptor before traveling tmin
170  if (recepteurs->at(i).intersectionRecepteur(r->position, r->direction, tmin, result))
171  {
172 
173  // Create a copy of the ray
174  Ray* valide_ray = new Ray(r);
175  valide_ray->setConstructId(rayCounter);
176 
177  rayCounter++;
178 
179  // Set the ray's receptor
180  valide_ray->setRecepteur(dynamic_cast<Recepteur*>(its.p));
181 
182  // Compute the final position of the ray based on its direction and the distance traveled
183  valide_ray->setFinalPosition(valide_ray->getPosition() +
184  valide_ray->getDirection() * result.t);
185  valide_ray->computeLongueur();
186 
187  // Add the ray to the solver's list of valid rays
188  solver->valideRayon(valide_ray);
189  }
190  }
191  }
192 
194  {
195  for (vector<Source>::iterator itsource = sources->begin(); itsource != sources->end(); itsource++)
196  {
197  for (vector<Recepteur>::iterator itrecp = recepteurs->begin(); itrecp != recepteurs->end();
198  itrecp++)
199  {
200  Ray* new_ray = new Ray();
201  new_ray->constructId = rayCounter;
202  rayCounter++;
203  new_ray->source = (&(*itsource));
204  new_ray->position = itsource->getPosition();
205  vec3 psource = itsource->getPosition();
206  vec3 precp = itrecp->getPosition();
207  new_ray->direction = precp - psource;
208  new_ray->direction.normalize();
209  new_ray->mint = 0.;
210  new_ray->maxt = 10000.;
211  if (itsource->getSampler()->isAcceptableSample(direction))
212  pile_traitement.push(new_ray);
213  }
214  }
215  }
216 #endif
217 protected:
224  {
225  // Copy ray
226  Ray* copie = new Ray(r);
227  copie->setConstructId(rayCounter);
228  rayCounter++;
229 
230  // Generate a response with the event and use it has the copy's direction
231  vec3 response;
232  r->getEvents()->back()->getResponse(response);
233  copie->setDirection(response);
234 
235  // Add the copy to the stack
236  pile_traitement.push(copie);
237  }
238 
245  virtual bool traitementRay(Ray* r, std::list<validRay>& result);
246 
247  std::stack<Ray*, std::deque<Ray*>> pile_traitement;
248 
249  unsigned long long int nbRayonsTraites;
250 };
251 
252 #endif
Base class for accelerators.
Definition: Accelerator.h:27
virtual decimal traverse(Ray *r, std::list< Intersection > &result) const
Run this accelerator.
Definition: Accelerator.h:68
Default Engine class.
Definition: DefaultEngine.h:26
virtual bool process()
If implemented, process and return true if success.
std::stack< Ray *, std::deque< Ray * > > pile_traitement
Treatment stack containing the rays to treat.
void searchForReceptor(const decimal &tmin, Ray *r)
Search if a ray intersects a receptor before traveling a tmin distance (NB: this is the only place in...
Definition: DefaultEngine.h:64
virtual ~DefaultEngine()
Destructor.
Definition: DefaultEngine.h:48
void copyRayAndAddToStack(Ray *r)
Copy a ray and use its last event to generate a response to use as the copy's direction (used to hand...
unsigned long long int nbRayonsTraites
Treated rays number.
void initialReceptorTargeting()
Initialize the rays treatment stack by a loop on the receptors.
DefaultEngine(Scene *_scene, std::vector< Source > *_sources, Solver *_solver, Scene *_recepteurs)
Definition: DefaultEngine.h:35
virtual bool traitementRay(Ray *r, std::list< validRay > &result)
Ray treatment method.
DefaultEngine(const DefaultEngine &other)
Copy constructor.
Definition: DefaultEngine.h:40
virtual void runStructureBenchmark()
If implemented, run a benchmark for the engine.
Ray * genRay()
Create rays from the sources.
DefaultEngine()
Constructors.
Definition: DefaultEngine.h:30
Base class for engines (DefaultEngine, ParallelDefaultEngine,...)
Definition: Engine.h:36
Scene * recepteurs
Pointer to all the sources.
Definition: Engine.h:105
Scene * scene
Pointer to the scene.
Definition: Engine.h:103
Solver * solver
Pointer to the solver.
Definition: Engine.h:106
unsigned long long int rayCounter
Ray counter.
Definition: Engine.h:108
std::vector< Source > * sources
Pointer to all the receptors.
Definition: Engine.h:104
: Describes a ray by a pair of unsigned int. The first one gives the source number (in the range 0-40...
Definition: Ray.h:38
void setDirection(vec3 _direction)
set the direction if the ray
Definition: Ray.h:376
void setPosition(vec3 _position)
set the starting point ray
Definition: Ray.h:366
vec3 direction
Direction vector for the ray at the source.
Definition: Ray.h:505
void setFinalPosition(vec3 _finalPosition)
set the ending point of the ray
Definition: Ray.h:386
void setRecepteur(void *_recepteur)
set the pointer to the receptor of the ray
Definition: Ray.h:426
Source * source
Pointer to the source of the ray.
Definition: Ray.h:509
vec3 getPosition() const
Return starting point ray.
Definition: Ray.h:356
vec3 getDirection() const
Return direction of the ray.
Definition: Ray.h:346
decimal mint
Definition: Ray.h:507
void setMaxt(decimal _maxt)
set the maxt
Definition: Ray.h:406
void setSource(Source *_source)
set the pointer to the source of the ray
Definition: Ray.h:416
unsigned long long int constructId
Ray id.
Definition: Ray.h:512
decimal maxt
Definition: Ray.h:508
void setMint(decimal _mint)
set the Mint
Definition: Ray.h:396
vec3 position
Starting point ray.
Definition: Ray.h:504
void setConstructId(unsigned long long int _constructId)
set the ray id
Definition: Ray.h:446
void computeLongueur()
Compute the distance traveled (length) by the ray and the result is set into the longueur attribute.
Definition: Ray.cpp:24
std::vector< boost::shared_ptr< Event > > * getEvents()
Return the events array encountered by the ray.
Definition: Ray.h:195
Receptor inherits from a Sphere Shape.
Definition: Recepteur.h:28
bool intersectionRecepteur(vec3 &origine, vec3 &directeur, float tmax, Intersection &result)
Get the Intersection between a ray and this shape.
Definition: Recepteur.cpp:19
This class mainly define a mesh (list of Shape) used by the Simulation object.
Definition: Scene.h:51
std::vector< Shape * > * getShapes()
Return all the shapes.
Definition: Scene.h:88
Accelerator * getAccelerator() const
Get the accelerator.
Definition: Scene.h:82
The Solver class gives an interface to the developer to add easily a new acoustic method using ray tr...
Definition: Solver.h:45
virtual bool valideRayon(Ray *r)
Ray validation. The developer may, for instance, choose a filtering on the rays and only validate sin...
Definition: Solver.cpp:48
Sphere Shape.
Definition: Sphere.h:25
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:381
Intersection struct.
Definition: Shape.h:46
decimal t
Definition: Shape.h:48
Shape * p
Definition: Shape.h:49