Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TargetManager.cpp
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 #include "TargetManager.h"
17 
18 bool TargetManager::registerTarget(const vec3 newTarget)
19 {
20 
21  std::pair<std::set<vec3>::iterator, bool> ret;
22  ret = uniqueTargets.insert(newTarget);
23  if (ret.second)
24  {
25  targets.push_back(newTarget);
26  return true;
27  }
28 
29  return false;
30 }
31 
32 bool TargetManager::registerTargets(std::vector<vec3>& newTargets)
33 {
34  bool result = true;
35  for (unsigned int i = 0; i < newTargets.size(); i++)
36  {
37  result &= registerTarget(newTargets.at(i));
38  }
39 
40  return result;
41 }
43 {
44  uniqueTargets.clear();
45 }
46 
47 unsigned int TargetManager::getTargetsAround(const vec3 center, unsigned int nbTargets, decimal distance,
48  std::vector<vec3>& result)
49 {
50  std::vector<unsigned int> globalTargets;
51  for (unsigned int i = 0; i < targets.size(); i++)
52  {
53  if (center.distance(targets.at(i)) < distance)
54  {
55  globalTargets.push_back(i);
56  }
57  }
58 
59  if (globalTargets.size() < nbTargets)
60  {
61  for (unsigned int i = 0; i < globalTargets.size(); i++)
62  {
63  result.push_back(targets.at(globalTargets.at(i)));
64  }
65 
66  return static_cast<unsigned int>(globalTargets.size());
67  }
68  else
69  {
70  for (unsigned int i = 0; i < nbTargets; i++)
71  {
72  unsigned int choice = rand() % globalTargets.size();
73  result.push_back(targets.at(globalTargets.at(choice)));
74  }
75 
76  return nbTargets;
77  }
78 }
bool registerTargets(std::vector< vec3 > &newTargets)
Register a vector of targets.
unsigned int getTargetsAround(const vec3 center, unsigned int nbTargets, decimal distance, std::vector< vec3 > &result)
Get the targets in a distance range from a center.
void finish()
Delete the uniqueTargets array.
bool registerTarget(const vec3 newTarget)
Register a new target.
std::set< vec3 > uniqueTargets
Set of single targets.
Definition: TargetManager.h:49
std::vector< vec3 > targets
Vector of targets.
Definition: TargetManager.h:48
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:381