Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Triangle.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 "Triangle.h"
17 
18 #include <math.h>
19 
20 Triangle::Triangle(const vec3& p1, const vec3& p2, const vec3& p3, Material* m, const bool& isSol)
21 {
22  name = "unknown triangle";
24  material = m;
25  p = p1;
26  u = p2 - p1;
27  v = p3 - p1;
28  vertices = NULL;
29 
30  normal.cross(u, v);
31  normal.normalize();
32 
33  updateBBox();
34 }
35 
36 Triangle::Triangle(unsigned int p1, unsigned int p2, unsigned int p3, std::vector<vec3>* _vertices,
37  Material* _m, const bool& isSol)
38 {
40  vertices = _vertices;
41  material = _m;
42 
43  localVertices.push_back(p1);
44  localVertices.push_back(p2);
45  localVertices.push_back(p3);
46 
47  vec3 v1 = vertices->at(p1);
48  vec3 v2 = vertices->at(p2);
49  vec3 v3 = vertices->at(p3);
50 
51  p = v1;
52  u = v2 - v1;
53  v = v3 - v1;
54 
55  normal.cross(u, v);
56  normal.normalize();
57 
58  updateBBox();
59 }
60 
62 {
63  box = BBox(p, p);
64  box = box.Union(p + u);
65  box = box.Union(p + v);
66  box.pMin = vec3((decimal)(box.pMin.x - 0.01), (decimal)(box.pMin.y - 0.01), (decimal)(box.pMin.z - 0.01));
67  box.pMax = vec3((decimal)(box.pMax.x + 0.01), (decimal)(box.pMax.y + 0.01), (decimal)(box.pMax.z + 0.01));
68 };
69 
71 {
72  vec3 directeur = ray.getDirection();
73  directeur.normalize();
74  if (directeur.dot(this->normal) > 0.)
75  {
76  return false;
77  }
78 
79  vec3 n;
80  n.cross(u, v);
81  vec3 otr = Vector_r(ray.getPosition(), p);
82  double ir = NAN, iu = NAN, iv = NAN;
83  vec3 temp;
84 
85  ir = -(n.dot(otr)) / (n.dot(directeur));
86  temp.cross(otr, v);
87  iu = (temp.dot(directeur)) / (n.dot(directeur));
88  temp.cross(u, otr);
89  iv = (temp.dot(directeur)) / (n.dot(directeur));
90 
91  if (iu >= -0.00001 && iu <= 1.00001 && iv >= -0.00001 && iv <= 1.00001 && ir >= -0.00001 &&
92  iu + iv <= 1.00001) // Detection du point d'intersection
93  {
94  inter.t = (decimal)ir;
95  inter.p = this;
96  inter.forme = TRIANGLE;
97  return true;
98  }
99  return false;
100 }
101 
102 bool Triangle::sample(decimal density, std::vector<vec3>& samples)
103 {
104 
105  vec3 proj = vertices->at(localVertices.at(0))
106  .closestPointOnLine(vertices->at(localVertices.at(1)), vertices->at(localVertices.at(2)));
107 
108  decimal areaTriangle =
109  (decimal)(vertices->at(localVertices.at(1)).distance(vertices->at(localVertices.at(2))) *
110  proj.distance(vertices->at(localVertices.at(0))) / 2.);
111 
112  unsigned int nbSamples = (unsigned int)(areaTriangle * density) + 1;
113 
114  for (unsigned int i = 0; i < nbSamples; i++)
115  {
116  decimal randU = 1.;
117  decimal randV = 1.;
118  while (randU + randV > 1.)
119  {
120  randU = (decimal)rand() / (decimal)RAND_MAX;
121  randV = (decimal)rand() / (decimal)RAND_MAX;
122  }
123  samples.push_back(p + u * randU + v * randV);
124  }
125 
126  return true;
127 }
@ TRIANGLE
Definition: Shape.h:36
Definition of a bounding box which is aligned along the axis (BBox AABB).
Definition: BBox.h:32
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
: 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
vec3 getPosition() const
Return starting point ray.
Definition: Ray.h:356
vec3 getDirection() const
Return direction of the ray.
Definition: Ray.h:346
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
void setIsSol(const bool &isSol)
Definition: Shape.h:198
bool isSol() const
Get/Set the flag _isSol (ground or not)
Definition: Shape.h:194
std::vector< unsigned int > localVertices
Index of the vertices used for this shape.
Definition: Shape.h:213
virtual bool sample(decimal density, std::vector< vec3 > &samples)
Uncommented method cause not used.
Definition: Triangle.cpp:102
vec3 normal
Normal to triangle.
Definition: Triangle.h:73
virtual bool getIntersection(Ray &ray, Intersection &inter)
Get the Intersection between a ray and this shape.
Definition: Triangle.cpp:70
virtual void updateBBox()
Update the bounding box:
Definition: Triangle.cpp:61
vec3 v
Vector to reach the third vertex (vertex3=p+v)
Definition: Triangle.h:71
vec3 u
Vector to reach the second vertex (vertex2=p+u)
Definition: Triangle.h:70
Triangle()
Default constructor.
Definition: Triangle.h:29
vec3 p
First vertex (vertex1=p)
Definition: Triangle.h:69
float decimal
Definition: mathlib.h:45
vec3 Vector_r(const vec3 &vp1, const vec3 &vp2)
Definition: mathlib.h:692
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