Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Scene.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 <deque>
17 #include <string>
18 #include <iostream>
19 #include <fstream>
20 
26 #include "Tools/Logger.h"
27 
28 #include "Triangle.h"
29 #include "Scene.h"
30 
31 #include <math.h>
32 
34 {
35  for (unsigned int i = 0; i < shapes.size(); i++)
36  {
37  delete shapes.at(i);
38  }
39  shapes.clear();
40  registeredVertices.clear();
41  vertices.clear();
42  globalBox.isNull = true;
44  compteurFace = 0;
45  if (accelerator)
46  {
47  delete accelerator;
48  }
49 }
50 
51 bool Scene::finish(int accelerator_id /* = 3*/,
52  leafTreatment::treatment _intersectionChoice /* = leafTreatment::FIRST*/)
53 {
54 
55  ss << "La scene comporte " << vertices.size() << " vertex." << std::endl;
56  for (unsigned int i = 0; i < vertices.size(); i++)
57  {
58  ss << "(" << vertices.at(i).x << "," << vertices.at(i).y << "," << vertices.at(i).z << ")"
59  << std::endl;
60  }
61  ss << "La scene comporte " << shapes.size() << " shapes." << std::endl;
62  switch (accelerator_id)
63  {
64  case 0:
66  break;
67  case 1:
69  break;
70  case 2:
72  AcousticRaytracerConfiguration::get()->MaxTreeDepth, "middle");
73  break;
74  case 3:
76  break;
77  default:
79  break;
80  }
81 
82  accelerator->setIntersectionChoice(_intersectionChoice);
83 
84  return accelerator->build();
85 }
86 
87 bool Scene::getIndex(vec3& search, unsigned int& result)
88 {
89  std::map<vec3, unsigned int, compVec>::iterator it = registeredVertices.find(search);
90  if (it != registeredVertices.end())
91  {
92  result = it->second;
93  return true;
94  }
95 
96  return false;
97 }
98 
99 bool Scene::getVertex(unsigned int& search, vec3& result)
100 {
101  if (search < vertices.size())
102  {
103  result = vertices[search];
104  return true;
105  }
106 
107  return false;
108 }
109 
110 bool Scene::addVertex(const vec3& newVertex, unsigned int& index)
111 {
112  std::map<vec3, unsigned int, compVec>::iterator it = registeredVertices.find(newVertex);
113  if (it != registeredVertices.end())
114  {
115  index = it->second;
116  return false;
117  }
118  else
119  {
120  index = static_cast<unsigned int>(vertices.size());
121  vertices.push_back(newVertex);
122  registeredVertices.insert(std::pair<vec3, unsigned int>(newVertex, index));
123  return true;
124  }
125 }
126 
127 Shape* Scene::addTriangle(unsigned int i1, unsigned int i2, unsigned int i3, Material* m, const bool& isSol)
128 {
129  Triangle* triangle = new Triangle(i1, i2, i3, &vertices, m, isSol);
131 
132  return triangle;
133 }
134 
135 void Scene::addBuilding(vec3 origine, vec3 dimension, Material* m)
136 {
137 
138  unsigned int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0, p8 = 0;
139 
140  addVertex(origine, p1);
141  addVertex(vec3(origine.x + dimension.x, origine.y, origine.z), p2);
142  addVertex(vec3(origine.x + dimension.x, origine.y + dimension.y, origine.z), p3);
143  addVertex(vec3(origine.x, origine.y + dimension.y, origine.z), p4);
144  addVertex(vec3(origine.x, origine.y, origine.z + dimension.z), p5);
145  addVertex(vec3(origine.x + dimension.x, origine.y, origine.z + dimension.z), p6);
146  addVertex(vec3(origine.x + dimension.x, origine.y + dimension.y, origine.z + dimension.z), p7);
147  addVertex(vec3(origine.x, origine.y + dimension.y, origine.z + dimension.z), p8);
148 
149  addTriangle(p1, p2, p6, m);
150  addTriangle(p6, p5, p1, m);
151 
152  addTriangle(p3, p4, p8, m);
153  addTriangle(p8, p7, p3, m);
154 
155  addTriangle(p2, p3, p7, m);
156  addTriangle(p7, p6, p2, m);
157 
158  addTriangle(p8, p4, p1, m);
159  addTriangle(p1, p5, p8, m);
160 
161  addTriangle(p5, p6, p7, m);
162  addTriangle(p7, p8, p5, m);
163 }
164 
165 std::vector<Shape*> Scene::getShapes(int shape_type)
166 {
167  std::vector<Shape*> vec;
168  for (unsigned int i = 0; i < shapes.size(); i++)
169  {
170  if (shapes.at(i)->form() == shape_type)
171  {
172  vec.push_back(shapes.at(i));
173  }
174  }
175  return vec;
176 }
177 
178 void Scene::export_to_ply(std::string fileName)
179 {
180  // using long long cause of a limitation of MS visual studio 10 in std::to_string
181  long long nb_vertex = vertices.size();
182  long long nb_faces = shapes.size();
183 
184  // Create the file content as a vector of string
185  std::deque<std::string> file_content;
186 
187  // Create header
188  file_content.push_back(std::string("ply\n"));
189  file_content.push_back(std::string("format ascii 1.0\n"));
190  file_content.push_back(std::string("element vertex ") + std::to_string(nb_vertex) + std::string("\n"));
191  file_content.push_back(std::string("property float x\n"));
192  file_content.push_back(std::string("property float y\n"));
193  file_content.push_back(std::string("property float z\n"));
194  file_content.push_back(std::string("element face ") + std::to_string(nb_faces) + std::string("\n"));
195  file_content.push_back(std::string("property list uchar int vertex_indices\n"));
196  file_content.push_back(std::string("property uchar red\n"));
197  file_content.push_back(std::string("property uchar green\n"));
198  file_content.push_back(std::string("property uchar blue\n"));
199  file_content.push_back(std::string("end_header\n"));
200  // Creation of vertex list
201  std::string line;
202  for (unsigned int i = 0; i < vertices.size(); i++)
203  {
204  line = std::to_string(static_cast<long double>(vertices.at(i).x)) + " " +
205  std::to_string(static_cast<long double>(vertices.at(i).y)) + " " +
206  std::to_string(static_cast<long double>(vertices.at(i).z)) + "\n";
207  file_content.push_back(line);
208  }
209 
210  // Creation of faces list
211  vector<unsigned int>* vertice_index = nullptr;
212  for (unsigned int i = 0; i < shapes.size(); i++)
213  {
214  if (shapes.at(i)->form() != TRIANGLE)
215  {
216  continue;
217  }
218 
219  vertice_index = shapes.at(i)->getLocalVertices();
220  file_content.push_back(std::to_string(static_cast<long long>(3)) + " " +
221  std::to_string(static_cast<long long>(vertice_index->at(0))) + " " +
222  std::to_string(static_cast<long long>(vertice_index->at(1))) + " " +
223  std::to_string(static_cast<long long>(vertice_index->at(2))) + " " +
224  std::string("0 255 0\n"));
225  }
226 
227  // Create the file
228  ofstream out(fileName, ios::out);
229  for (unsigned int i = 0; i < file_content.size(); i++)
230  {
231  out << file_content.at(i);
232  }
233 
234  out.close();
235 }
236 
237 void Scene::import_from_ply(std::string fileName)
238 {
239  ifstream in(fileName.c_str());
240  std::string keyword;
241  int nb_vertex = 0, nb_faces = 0;
242  in >> keyword; // ply
243  if (keyword != "ply")
244  {
245  std::cerr << "Read: " + keyword + "\n" + fileName + " is not a ply file !" << std::endl;
246  exit(-1);
247  }
248  in >> keyword >> keyword >> keyword; // format ascii 1.0
249  in >> keyword >> keyword >> nb_vertex; // element vertex 119
250  in >> keyword >> keyword >> keyword; // property float x
251  in >> keyword >> keyword >> keyword; // property float y
252  in >> keyword >> keyword >> keyword; // property float z
253  in >> keyword >> keyword >> nb_faces; // element face 198
254  in >> keyword >> keyword >> keyword >> keyword >> keyword; // property list uchar int vertex_indices
255  in >> keyword >> keyword >> keyword; // property int material_index ?
256  bool material = false;
257  if (keyword == "material_index")
258  {
259  material = true;
260  in >> keyword >> keyword >> keyword; // property uchar red
261  }
262  in >> keyword >> keyword >> keyword; // property uchar green
263  in >> keyword >> keyword >> keyword; // property uchar blue
264  if (material)
265  {
266  in >> keyword >> keyword >> keyword; // element material 5
267  in >> keyword >> keyword >> keyword >> keyword >> keyword; // property list uchar uchar id
268  }
269  in >> keyword; // end_header
270  float x = NAN, y = NAN, z = NAN;
271  // Read vertex:
272  for (int i = 0; i < nb_vertex; i++)
273  {
274  unsigned int p = 0;
275  in >> x >> y >> z; // x y z
276  addVertex(vec3(x, y, z), p);
277  }
278  ss << "La scene lue comporte " << nb_vertex << " vertex." << std::endl;
279  int i1 = 0, i2 = 0, i3 = 0;
280  Material* m = new Material();
281  // Read faces:
282  int n = 0;
283  for (int i = 0; i < nb_faces; i++)
284  {
285  in >> n >> i1 >> i2 >> i3; // vertices
286  if (material)
287  in >> keyword; // material
288  in >> keyword >> keyword >> keyword; // color
289  if (n != 3)
290  {
291  std::cerr << "The shape " << i << " is not a Triangle !" << std::endl;
292  exit(-1);
293  }
294  addTriangle(i1, i2, i3, m, false);
295  }
296  ss << "La scene lue comporte " << nb_faces << " shapes." << std::endl;
297  in.close();
298 }
std::stringstream ss
Definition: Logger.cpp:21
@ TRIANGLE
Definition: Shape.h:36
virtual bool build()
Build this accelerator.
Definition: Accelerator.h:63
void setIntersectionChoice(leafTreatment::treatment _intersectionChoice=leafTreatment::FIRST)
Definition: Accelerator.h:57
static AcousticRaytracerConfiguration * get()
Get access to the configuration.
bool isNull
True if the BBox is initialized, false if not.
Definition: BBox.h:38
Brute-force algorithm.
A Bounding Volume Hierarchy (BVH) Accelerator.
Regular grid Accelerator.
K-d tree Accelerator (based on space splitting)
void import_from_ply(std::string fileName)
Import a Scene from a PLY format file named fileName.
Definition: Scene.cpp:237
void clean()
Clear all arrays.
Definition: Scene.cpp:33
std::vector< Shape * > shapes
Array of pointers to the shapes.
Definition: Scene.h:163
std::vector< Shape * > * getShapes()
Return all the shapes.
Definition: Scene.h:88
bool getIndex(vec3 &search, unsigned int &result)
Get the index of a vertex in the vertices array.
Definition: Scene.cpp:87
void export_to_ply(std::string fileName)
Export the Scene to a PLY format file named fileName.
Definition: Scene.cpp:178
unsigned int compteurFace
Faces counter.
Definition: Scene.h:172
Accelerator * accelerator
Pointer to the accelerator.
Definition: Scene.h:165
void addShape(Shape *shape)
Add a shape to the list.
Definition: Scene.h:68
Shape * addTriangle(unsigned int i1, unsigned int i2, unsigned int i3, Material *m, const bool &isSol=false)
Add a triangle to the scene built with the vertices array.
Definition: Scene.cpp:127
BBox globalBox
Bounding box of the Scene.
Definition: Scene.h:164
unsigned int compteurPrimitive
Primitives counter.
Definition: Scene.h:171
void addBuilding(vec3 origine, vec3 dimension, Material *m)
Add a building to the scene.
Definition: Scene.cpp:135
bool addVertex(const vec3 &newVertex, unsigned int &index)
Add a vertex to the vertices array.
Definition: Scene.cpp:110
bool getVertex(unsigned int &search, vec3 &result)
Get a vertex from the vertices array with its index.
Definition: Scene.cpp:99
std::vector< vec3 > vertices
All the vertices used by the different shapes.
Definition: Scene.h:167
std::map< vec3, unsigned int, compVec > registeredVertices
Association between a vertex and his index in vertices.
Definition: Scene.h:169
bool finish(int accelerator_id=1, leafTreatment::treatment _intersectionChoice=leafTreatment::FIRST)
Build the selected accelerator on the scene.
Definition: Scene.cpp:51
base class for shapes (Cylindre, Mesh, Sphere, Triangle,...)
Definition: Shape.h:57
Triangle class.
Definition: Triangle.h:25
base_vec3< decimal > vec3
Definition: mathlib.h:381