Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Cylindre.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 "Cylindre.h"
17 #include "Triangle.h"
18 
19 Cylindre::Cylindre(Shape* _p1, Shape* _p2, std::vector<vec3>* _vertices, unsigned int v1, unsigned int v2,
20  decimal _epaisseur)
21  : Shape(std::string("unknown cylindre"))
22 {
23  vertices = _vertices;
24  p1 = _p1;
25  p2 = _p2;
26  epaisseur = _epaisseur;
27  localVertices.push_back(v1);
28  localVertices.push_back(v2);
29 
30  // Skip instructions dependant on v1 and v2 if at least one of them does not exist
31  if (vertices && vertices->size() > max(v1, v2))
32  {
33 
34  vec3 X = vertices->at(localVertices.at(1)) - vertices->at(localVertices.at(0));
35  X.normalize();
36  vec3 Y = p1->getNormal() + p2->getNormal();
37  Y.normalize();
38  vec3 Z;
39  Z.cross(X, Y);
40 
41  vec3 segment = vertices->at(localVertices.at(1)) - vertices->at(localVertices.at(0));
42  hauteur = segment.length();
43 
44  localRepere = Repere(X, Y, Z, vertices->at(localVertices.at(0)));
45  angleOuverture = static_cast<decimal>(2 * M_PI);
46 
47  material = p1->getMaterial();
48 
49  computeMesh();
50  }
51 }
52 
54 {
55  vertices = other.vertices;
56  p1 = other.p1;
57  p2 = other.p2;
58  name = other.name;
59  epaisseur = other.epaisseur;
60  hauteur = other.hauteur;
61  for (unsigned int i = 0; i < other.localVertices.size(); i++)
62  {
63  localVertices.push_back(other.localVertices.at(i));
64  }
65 
68  material = other.material;
69 
70  computeMesh();
71  box = other.box;
72 }
73 
74 /*bool Cylindre::getIntersection(Ray &ray, Intersection &inter){
75 
76  vec3 localPos = localRepere.positionFromGlobalToLocal(ray.position);
77  vec3 localDir = localRepere.vectorFromGlobalToLocal(ray.direction);
78 
79  decimal A = localDir.y * localDir.y + localDir.z * localDir.z;
80  decimal B = 2 * (localDir.y * localPos.y + localDir.z * localPos.z);
81  decimal C = localPos.y * localPos.y + localPos.z * localPos.z - epaisseur * epaisseur;
82 
83  decimal delta = B *B - 4. * A * C;
84 
85  if(delta <= 0.)
86  return false;
87 
88  return true;
89 
90  decimal t1 = (-B - sqrt(delta)) / (2. * A);
91  decimal t2 = (-B + sqrt(delta)) / (2. * A);
92 
93  decimal hauteur1 = localPos.x + t1 * localDir.x;
94  decimal hauteur2 = localPos.x + t2 * localDir.x;
95 
96  if(hauteur1 > 0. && hauteur1 < hauteur){
97  inter.p = this;
98  inter.t = t1;
99  inter.forme = CYLINDRE;
100  return true;
101  }
102 
103  return false;
104 }*/
105 
107 {
108 
109  bool findIntersection = false;
110  Intersection currentInter;
111  decimal tmin = -1.;
112  for (unsigned int i = 0; i < mesh.size(); i++)
113  {
114  if (mesh.at(i)->getIntersection(ray, currentInter))
115  {
116  if (tmin < 0 || currentInter.t < tmin)
117  {
118  findIntersection = true;
119  inter.forme = CYLINDRE;
120  inter.p = this;
121  inter.t = currentInter.t;
122  tmin = inter.t;
123  }
124  }
125  }
126  return findIntersection;
127 }
128 
130 {
131 
132  vec3 O = localRepere.getO();
133  vec3 X = localRepere.getU();
134  vec3 Y = localRepere.getV();
135  vec3 Z = localRepere.getW();
136 
137  X.normalize();
138  Y.normalize();
139  Z.normalize();
140 
141  X *= hauteur;
142  Y *= epaisseur;
143  Z *= epaisseur;
144 
145  points.reserve(8);
146  points.push_back(O - Y - Z);
147  points.push_back(O + Y - Z);
148  points.push_back(O + Y + Z);
149  points.push_back(O - Y + Z);
150 
151  points.push_back(O + X - Y - Z);
152  points.push_back(O + X + Y - Z);
153  points.push_back(O + X + Y + Z);
154  points.push_back(O + X - Y + Z);
155 
156  mesh.push_back(new Triangle(0, 1, 5, &points, material));
157  mesh.push_back(new Triangle(5, 4, 0, &points, material));
158  mesh.push_back(new Triangle(2, 3, 7, &points, material));
159  mesh.push_back(new Triangle(7, 6, 2, &points, material));
160  mesh.push_back(new Triangle(1, 2, 6, &points, material));
161  mesh.push_back(new Triangle(6, 5, 1, &points, material));
162  mesh.push_back(new Triangle(7, 3, 0, &points, material));
163  mesh.push_back(new Triangle(0, 4, 7, &points, material));
164  mesh.push_back(new Triangle(4, 5, 6, &points, material));
165  mesh.push_back(new Triangle(6, 7, 4, &points, material));
166 
167  box = mesh.at(0)->getBBox();
168  for (unsigned int i = 1; i < mesh.size(); i++)
169  {
170  box = box.Union(mesh.at(i)->getBBox());
171  }
172 
173  box.pMin = vec3(box.pMin.x - (decimal)0.01, box.pMin.y - (decimal)0.01, box.pMin.z - (decimal)0.01);
174  box.pMax = vec3(box.pMax.x + (decimal)0.01, box.pMax.y + (decimal)0.01, box.pMax.z + (decimal)0.01);
175 }
176 
177 bool Cylindre::sample(decimal density, std::vector<vec3>& samples)
178 {
179  vec3 v1 = vertices->at(localVertices.at(0));
180  vec3 v2 = vertices->at(localVertices.at(1));
181  vec3 v1v2 = v2 - v1;
182 
183  vec3 normale = p1->getNormal() + p2->getNormal();
184  normale.normalize();
185 
186  decimal lenght = v1.distance(v2);
187 
188  unsigned int nbSamples = (unsigned int)(lenght * density) + 1;
189 
190  for (unsigned int i = 0; i < nbSamples; i++)
191  {
192  decimal choice = (decimal)rand() / (decimal)RAND_MAX;
193  vec3 newSample =
194  v1 + v1v2 * choice + normale * decimal(0.001); // On releve legerement le point pour etre sur que
195  // le point soit visible de l'exterieur du batiment
196  samples.push_back(newSample);
197  }
198 
199  return true;
200 }
std::pair< unsigned int, unsigned int > segment
@ CYLINDRE
Definition: Shape.h:37
vec3 pMax
Upper point of the BBox.
Definition: BBox.h:36
BBox Union(const BBox &b, const vec3 &p)
Union of a point and a BBox. A new BBox is created.
Definition: BBox.h:110
vec3 pMin
Lower point of the BBox.
Definition: BBox.h:35
std::string name
Each instantiated object may be named.
Definition: Base.h:52
Cylinder class.
Definition: Cylindre.h:27
vector< vec3 > points
Array of points defining the mesh.
Definition: Cylindre.h:99
vector< Shape * > mesh
Cylinder mesh (array of triangles)
Definition: Cylindre.h:100
virtual bool sample(decimal density, std::vector< vec3 > &samples)
Uncommented cause not used:
Definition: Cylindre.cpp:177
decimal hauteur
Height.
Definition: Cylindre.h:94
decimal epaisseur
Width.
Definition: Cylindre.h:93
Cylindre(Shape *_p1=NULL, Shape *_p2=NULL, std::vector< vec3 > *_vertices=NULL, unsigned int v1=0, unsigned int v2=1, decimal _epaisseur=0.2)
Constructors.
Definition: Cylindre.cpp:19
decimal angleOuverture
Opening angle.
Definition: Cylindre.h:95
Shape * p1
First shape of the diffraction edge represented by the Cylinder.
Definition: Cylindre.h:90
virtual bool getIntersection(Ray &ray, Intersection &inter)
Get the Intersection between a ray and this shape.
Definition: Cylindre.cpp:106
Shape * p2
Second shape of the diffraction edge represented by the Cylinder.
Definition: Cylindre.h:91
Repere localRepere
Local frame.
Definition: Cylindre.h:97
void computeMesh()
Definition: Cylindre.cpp:129
: 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
Frame class.
Definition: Repere.h:26
vec3 getU() const
Get global coordinates of local axis U,V,W and origin O.
Definition: Repere.h:71
vec3 getV() const
Definition: Repere.h:75
vec3 getO() const
Definition: Repere.h:83
vec3 getW() const
Definition: Repere.h:79
base class for shapes (Cylindre, Mesh, Sphere, Triangle,...)
Definition: Shape.h:57
BBox box
Bounding box of the shape.
Definition: Shape.h:210
std::vector< vec3 > * vertices
GlobalVertices of the scene.
Definition: Shape.h:212
Material * material
Pointer to material.
Definition: Shape.h:211
Material * getMaterial()
Return the pointed material.
Definition: Shape.h:99
virtual vec3 getNormal(const vec3 pos=vec3())
Get normal.
Definition: Shape.h:145
std::vector< unsigned int > localVertices
Index of the vertices used for this shape.
Definition: Shape.h:213
Triangle class.
Definition: Triangle.h:25
#define M_PI
Pi.
Definition: color.cpp: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
FORM forme
Definition: Shape.h:50
Shape * p
Definition: Shape.h:49