Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Triangle.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 "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  planeNormal.cross(u, v);
32  normal.normalize();
33 
34  updateBBox();
35 }
36 
37 Triangle::Triangle(unsigned int p1, unsigned int p2, unsigned int p3, std::vector<vec3>* _vertices,
38  Material* _m, const bool& isSol)
39 {
41  vertices = _vertices;
42  material = _m;
43 
44  localVertices.push_back(p1);
45  localVertices.push_back(p2);
46  localVertices.push_back(p3);
47 
48  vec3 v1 = vertices->at(p1);
49  vec3 v2 = vertices->at(p2);
50  vec3 v3 = vertices->at(p3);
51 
52  p = v1;
53  u = v2 - v1;
54  v = v3 - v1;
55 
56  planeNormal.cross(u, v);
58  normal.normalize();
59 
60  updateBBox();
61 }
62 
64 {
65  box = BBox(p, p);
66  box = box.Union(p + u);
67  box = box.Union(p + v);
68  box.pMin = vec3((decimal)(box.pMin.x - 0.01), (decimal)(box.pMin.y - 0.01), (decimal)(box.pMin.z - 0.01));
69  box.pMax = vec3((decimal)(box.pMax.x + 0.01), (decimal)(box.pMax.y + 0.01), (decimal)(box.pMax.z + 0.01));
70 };
71 
73 {
74  constexpr decimal EPSILON = 0.00001;
75 
76  const vec3& directeur = ray.getDirection();
77 
78  // This scalar product was previously recomputed several times.
79  const decimal denominator = planeNormal.dot(directeur);
80 
81  // Back-face culling or ray parallel with the plane containing the triangle.
82  if (denominator >= 0.)
83  {
84  return false;
85  }
86 
87  const vec3 w0 = Vector_r(ray.getPosition(), p);
88 
89  const decimal t = -planeNormal.dot(w0) / denominator;
90 
91  vec3 temporary;
92 
93  temporary.cross(w0, v);
94  const decimal iu = temporary.dot(directeur) / denominator;
95 
96  temporary.cross(u, w0);
97  const decimal iv = temporary.dot(directeur) / denominator;
98 
99  if (t >= -EPSILON && iu >= -EPSILON && iu <= 1. + EPSILON && iv >= -EPSILON && iv <= 1. + EPSILON &&
100  iu + iv <= 1. + EPSILON)
101  {
102  inter.t = t;
103  inter.p = this;
104  inter.forme = TRIANGLE;
105 
106  return true;
107  }
108 
109  return false;
110 }
111 
112 bool Triangle::sample(decimal density, std::vector<vec3>& samples)
113 {
114 
115  vec3 proj = vertices->at(localVertices.at(0))
116  .closestPointOnLine(vertices->at(localVertices.at(1)), vertices->at(localVertices.at(2)));
117 
118  decimal areaTriangle =
119  (decimal)(vertices->at(localVertices.at(1)).distance(vertices->at(localVertices.at(2))) *
120  proj.distance(vertices->at(localVertices.at(0))) / 2.);
121 
122  unsigned int nbSamples = (unsigned int)(areaTriangle * density) + 1;
123 
124  for (unsigned int i = 0; i < nbSamples; i++)
125  {
126  decimal randU = 1.;
127  decimal randV = 1.;
128  while (randU + randV > 1.)
129  {
130  randU = (decimal)rand() / (decimal)RAND_MAX;
131  randV = (decimal)rand() / (decimal)RAND_MAX;
132  }
133  samples.push_back(p + u * randU + v * randV);
134  }
135 
136  return true;
137 }
@ 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:357
vec3 getDirection() const
Return direction of the ray.
Definition: Ray.h:347
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:112
vec3 normal
Normalized normal to triangle.
Definition: Triangle.h:78
virtual bool getIntersection(Ray &ray, Intersection &inter)
Get the Intersection between a ray and this shape.
Definition: Triangle.cpp:72
virtual void updateBBox()
Update the bounding box:
Definition: Triangle.cpp:63
vec3 planeNormal
Non-normalized normal, equal to u cross v.
Definition: Triangle.h:79
vec3 v
Vector to reach the third vertex (vertex3=p+v)
Definition: Triangle.h:76
vec3 u
Vector to reach the second vertex (vertex2=p+u)
Definition: Triangle.h:75
Triangle()
Default constructor.
Definition: Triangle.h:29
vec3 p
First vertex (vertex1=p)
Definition: Triangle.h:74
float decimal
Definition: mathlib.h:45
vec3 Vector_r(const vec3 &vp1, const vec3 &vp2)
Definition: mathlib.h:698
base_vec3< decimal > vec3
Definition: mathlib.h:387
Intersection struct.
Definition: Shape.h:46
decimal t
Definition: Shape.h:48
FORM forme
Definition: Shape.h:50
Shape * p
Definition: Shape.h:49