Code_TYMPAN  4.2.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, std::vector<vec3>& result)
48 {
49  std::vector<unsigned int> globalTargets;
50  for (unsigned int i = 0; i < targets.size(); i++)
51  {
52  if (center.distance(targets.at(i)) < distance)
53  {
54  globalTargets.push_back(i);
55  }
56  }
57 
58  if (globalTargets.size() < nbTargets)
59  {
60  for (unsigned int i = 0; i < globalTargets.size(); i++)
61  {
62  result.push_back(targets.at(globalTargets.at(i)));
63  }
64 
65  return globalTargets.size();
66  }
67  else
68  {
69  for (unsigned int i = 0; i < nbTargets; i++)
70  {
71  unsigned int choice = rand() % globalTargets.size();
72  result.push_back(targets.at(globalTargets.at(choice)));
73  }
74 
75  return nbTargets;
76  }
77 }
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.
std::set< vec3 > uniqueTargets
Set of single targets.
Definition: TargetManager.h:45
bool registerTargets(std::vector< vec3 > &newTargets)
Register a vector of targets.
base_vec3< decimal > vec3
Definition: mathlib.h:269
float decimal
Definition: mathlib.h:46
std::vector< vec3 > targets
Vector of targets.
Definition: TargetManager.h:44
void finish()
Delete the uniqueTargets array.
bool registerTarget(const vec3 newTarget)
Register a new target.