Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Scene.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 #include <deque>
17 #include <string>
18 #include <iostream>
19 #include <fstream>
20 
26 
27 #include "Triangle.h"
28 #include "Scene.h"
29 
30 #include <math.h>
31 
33 {
34  for (unsigned int i = 0; i < shapes.size(); i++)
35  {
36  delete shapes.at(i);
37  }
38  shapes.clear();
39  registeredVertices.clear();
40  vertices.clear();
41  globalBox.isNull = true;
43  compteurFace = 0;
44  if (accelerator)
45  {
46  delete accelerator;
47  }
48 }
49 
50 bool Scene::finish(int accelerator_id /* = 3*/,
51  leafTreatment::treatment _intersectionChoice /* = leafTreatment::FIRST*/)
52 {
53  switch (accelerator_id)
54  {
55  case 0:
57  break;
58  case 1:
60  break;
61  case 2:
63  AcousticRaytracerConfiguration::get()->MaxTreeDepth, "middle");
64  break;
65  case 3:
67  break;
68  default:
70  break;
71  }
72 
73  accelerator->setIntersectionChoice(_intersectionChoice);
74 
75  return accelerator->build();
76 }
77 
78 bool Scene::getIndex(vec3& search, unsigned int& result)
79 {
80  std::map<vec3, unsigned int, compVec>::iterator it = registeredVertices.find(search);
81  if (it != registeredVertices.end())
82  {
83  result = it->second;
84  return true;
85  }
86 
87  return false;
88 }
89 
90 bool Scene::getVertex(unsigned int& search, vec3& result)
91 {
92  if (search < vertices.size())
93  {
94  result = vertices[search];
95  return true;
96  }
97 
98  return false;
99 }
100 
101 bool Scene::addVertex(const vec3& newVertex, unsigned int& index)
102 {
103  std::map<vec3, unsigned int, compVec>::iterator it = registeredVertices.find(newVertex);
104  if (it != registeredVertices.end())
105  {
106  index = it->second;
107  return false;
108  }
109  else
110  {
111  index = static_cast<unsigned int>(vertices.size());
112  vertices.push_back(newVertex);
113  registeredVertices.insert(std::pair<vec3, unsigned int>(newVertex, index));
114  return true;
115  }
116 }
117 
118 Shape* Scene::addTriangle(unsigned int i1, unsigned int i2, unsigned int i3, Material* m, const bool& isSol)
119 {
120  Triangle* triangle = new Triangle(i1, i2, i3, &vertices, m, isSol);
122 
123  return triangle;
124 }
125 
126 void Scene::addBuilding(vec3 origine, vec3 dimension, Material* m)
127 {
128 
129  unsigned int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0, p8 = 0;
130 
131  addVertex(origine, p1);
132  addVertex(vec3(origine.x + dimension.x, origine.y, origine.z), p2);
133  addVertex(vec3(origine.x + dimension.x, origine.y + dimension.y, origine.z), p3);
134  addVertex(vec3(origine.x, origine.y + dimension.y, origine.z), p4);
135  addVertex(vec3(origine.x, origine.y, origine.z + dimension.z), p5);
136  addVertex(vec3(origine.x + dimension.x, origine.y, origine.z + dimension.z), p6);
137  addVertex(vec3(origine.x + dimension.x, origine.y + dimension.y, origine.z + dimension.z), p7);
138  addVertex(vec3(origine.x, origine.y + dimension.y, origine.z + dimension.z), p8);
139 
140  addTriangle(p1, p2, p6, m);
141  addTriangle(p6, p5, p1, m);
142 
143  addTriangle(p3, p4, p8, m);
144  addTriangle(p8, p7, p3, m);
145 
146  addTriangle(p2, p3, p7, m);
147  addTriangle(p7, p6, p2, m);
148 
149  addTriangle(p8, p4, p1, m);
150  addTriangle(p1, p5, p8, m);
151 
152  addTriangle(p5, p6, p7, m);
153  addTriangle(p7, p8, p5, m);
154 }
155 
156 std::vector<Shape*> Scene::getShapes(int shape_type)
157 {
158  std::vector<Shape*> vec;
159  for (unsigned int i = 0; i < shapes.size(); i++)
160  {
161  if (shapes.at(i)->form() == shape_type)
162  {
163  vec.push_back(shapes.at(i));
164  }
165  }
166  return vec;
167 }
168 
169 void Scene::export_to_ply(std::string fileName)
170 {
171  // using long long cause of a limitation of MS visual studio 10 in std::to_string
172  long long nb_vertex = vertices.size();
173  long long nb_faces = shapes.size();
174 
175  // Create the file content as a vector of string
176  std::deque<std::string> file_content;
177 
178  // Create header
179  file_content.push_back(std::string("ply\n"));
180  file_content.push_back(std::string("format ascii 1.0\n"));
181  file_content.push_back(std::string("element vertex ") + std::to_string(nb_vertex) + std::string("\n"));
182  file_content.push_back(std::string("property float x\n"));
183  file_content.push_back(std::string("property float y\n"));
184  file_content.push_back(std::string("property float z\n"));
185  file_content.push_back(std::string("element face ") + std::to_string(nb_faces) + std::string("\n"));
186  file_content.push_back(std::string("property list uchar int vertex_indices\n"));
187  file_content.push_back(std::string("property uchar red\n"));
188  file_content.push_back(std::string("property uchar green\n"));
189  file_content.push_back(std::string("property uchar blue\n"));
190  file_content.push_back(std::string("end_header\n"));
191  // Creation of vertex list
192  std::string line;
193  for (unsigned int i = 0; i < vertices.size(); i++)
194  {
195  line = std::to_string(static_cast<long double>(vertices.at(i).x)) + " " +
196  std::to_string(static_cast<long double>(vertices.at(i).y)) + " " +
197  std::to_string(static_cast<long double>(vertices.at(i).z)) + "\n";
198  file_content.push_back(line);
199  }
200 
201  // Creation of faces list
202  std::vector<unsigned int>* vertice_index = nullptr;
203  for (unsigned int i = 0; i < shapes.size(); i++)
204  {
205  if (shapes.at(i)->form() != TRIANGLE)
206  {
207  continue;
208  }
209 
210  vertice_index = shapes.at(i)->getLocalVertices();
211  file_content.push_back(std::to_string(static_cast<long long>(3)) + " " +
212  std::to_string(static_cast<long long>(vertice_index->at(0))) + " " +
213  std::to_string(static_cast<long long>(vertice_index->at(1))) + " " +
214  std::to_string(static_cast<long long>(vertice_index->at(2))) + " " +
215  std::string("0 255 0\n"));
216  }
217 
218  // Create the file
219  std::ofstream out(fileName, std::ios::out);
220  for (unsigned int i = 0; i < file_content.size(); i++)
221  {
222  out << file_content.at(i);
223  }
224 
225  out.close();
226 }
227 
228 void Scene::import_from_ply(std::string fileName)
229 {
230  std::ifstream in(fileName.c_str());
231  std::string keyword;
232  int nb_vertex = 0, nb_faces = 0;
233  in >> keyword; // ply
234  if (keyword != "ply")
235  {
236  std::cerr << "Read: " + keyword + "\n" + fileName + " is not a ply file !" << std::endl;
237  exit(-1);
238  }
239  in >> keyword >> keyword >> keyword; // format ascii 1.0
240  in >> keyword >> keyword >> nb_vertex; // element vertex 119
241  in >> keyword >> keyword >> keyword; // property float x
242  in >> keyword >> keyword >> keyword; // property float y
243  in >> keyword >> keyword >> keyword; // property float z
244  in >> keyword >> keyword >> nb_faces; // element face 198
245  in >> keyword >> keyword >> keyword >> keyword >> keyword; // property list uchar int vertex_indices
246  in >> keyword >> keyword >> keyword; // property int material_index ?
247  bool material = false;
248  if (keyword == "material_index")
249  {
250  material = true;
251  in >> keyword >> keyword >> keyword; // property uchar red
252  }
253  in >> keyword >> keyword >> keyword; // property uchar green
254  in >> keyword >> keyword >> keyword; // property uchar blue
255  if (material)
256  {
257  in >> keyword >> keyword >> keyword; // element material 5
258  in >> keyword >> keyword >> keyword >> keyword >> keyword; // property list uchar uchar id
259  }
260  in >> keyword; // end_header
261  float x = NAN, y = NAN, z = NAN;
262  // Read vertex:
263  for (int i = 0; i < nb_vertex; i++)
264  {
265  unsigned int p = 0;
266  in >> x >> y >> z; // x y z
267  addVertex(vec3(x, y, z), p);
268  }
269  int i1 = 0, i2 = 0, i3 = 0;
270  Material* m = new Material();
271  // Read faces:
272  int n = 0;
273  for (int i = 0; i < nb_faces; i++)
274  {
275  in >> n >> i1 >> i2 >> i3; // vertices
276  if (material)
277  in >> keyword; // material
278  in >> keyword >> keyword >> keyword; // color
279  if (n != 3)
280  {
281  std::cerr << "The shape " << i << " is not a Triangle !" << std::endl;
282  exit(-1);
283  }
284  addTriangle(i1, i2, i3, m, false);
285  }
286  in.close();
287 }
@ 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:228
void clean()
Clear all arrays.
Definition: Scene.cpp:32
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:78
void export_to_ply(std::string fileName)
Export the Scene to a PLY format file named fileName.
Definition: Scene.cpp:169
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:118
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:126
bool addVertex(const vec3 &newVertex, unsigned int &index)
Add a vertex to the vertices array.
Definition: Scene.cpp:101
bool getVertex(unsigned int &search, vec3 &result)
Get a vertex from the vertices array with its index.
Definition: Scene.cpp:90
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:50
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:387