Code_TYMPAN  4.4.0
Industrial site acoustic simulation
triangle.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2014> <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 <cmath>
17 #include <limits>
18 #include <cassert>
19 #include <sstream>
20 
21 #include "triangle.h"
22 
25 
27 {
28  assert(i < 3 && "Triangle vertex indices are 0, 1 or 2");
29  return this->*vertices_m_ptr[i];
30 }
31 
32 const OPoint3D& OTriangle::vertex(unsigned i) const
33 {
34  assert(i < 3 && "Triangle vertex indices are 0, 1 or 2");
35  return this->*vertices_m_ptr[i];
36 }
37 
38 int OTriangle::index(unsigned i) const
39 {
40  assert(i < 3 && "Triangle vertex indices are 0, 1 or 2");
41  return this->*indices_m_ptr[i];
42 }
43 
44 int& OTriangle::index(unsigned i)
45 {
46  assert(i < 3 && "Triangle vertex indices are 0, 1 or 2");
47  return this->*indices_m_ptr[i];
48 }
49 
50 OTriangle::OTriangle(int p1, int p2, int p3)
51 {
52  _p1 = p1;
53  _p2 = p2;
54  _p3 = p3;
55  // Insert signaling NaN to protect against used of uninitialized coordinates
56  const double NaN = std::numeric_limits<double>::signaling_NaN();
57  const OPoint3D notAPoint(NaN, NaN, NaN);
58  _A = notAPoint;
59  _B = notAPoint;
60  _C = notAPoint;
61 }
62 
64 {
65  _A = A;
66  _B = B;
67  _C = C;
68  // Hope this segfault if used for indexing an array (i.e. should not create
69  // a \c OTriangle from OPoint3D without setting _p1, _p2 & _p3.
70  _p1 = _p2 = _p3 = std::numeric_limits<unsigned>::max();
71 }
72 
73 bool OTriangle::operator==(const OTriangle& other) const
74 {
75  // XXX This test for representation equlity NOT triangle equality
76  // We should try any direct permutation of the vertices
77  for (unsigned i = 0; i < 3; ++i)
78  {
79  if ((this->vertex(i) != other.vertex(i)) || (this->index(i) != other.index(i)))
80  return false;
81  }
82  return true;
83 }
84 
86 
88 {
89  /*
90  // TODO : recuperation des sommets
91 
92  OPoint3D A, B, C; //sommets A, B, C du triangle
93  double xA, yA, zA, xB, yB, zB, xC, yC, zC;
94 
95  xA = _A._x; yA = _A._y; zA = _A._z;
96  xB = _B._x; yB = _B._y; zB = _B._z;
97  xC = _C._x; yC = _C._y; zC = _C._z;
98 
99  A = OPoint3D(xA, yA, zA);
100  B = OPoint3D(xB, yB, zB);
101  C = OPoint3D(xC, yC, zC);
102  */
103  // calcul des cotes :
104  double c1 = _A.distFrom(_B);
105  double c2 = _B.distFrom(_C);
106  double c3 = _C.distFrom(_A);
107  // demie somme des cotes du triangle
108  double s = (c1 + c2 + c3) * 0.5;
109  // air d'un triangle de cotes a, b , c (formule de Heron)
110  return sqrt(s * (s - c1) * (s - c2) * (s - c3));
111 }
112 
114 {
115  OPoint3D G;
116  G._x = (_A._x + _B._x + _C._x) / 3;
117  G._y = (_A._y + _B._y + _C._y) / 3;
118  G._z = (_A._z + _B._z + _C._z) / 3;
119  return G;
120 }
121 
122 bool OTriangle::checkConsistencyWrtPointsTab(const std::deque<OPoint3D>& points) const
123 {
124  return (points[_p1].distFrom(_A) < EPSILON_5 && points[_p2].distFrom(_B) < EPSILON_5 &&
125  points[_p3].distFrom(_C) < EPSILON_5);
126 }
127 
128 std::string OTriangle::reportInconsistencyWrtPointsTab(const std::deque<OPoint3D>& points)
129 {
130  using std::endl;
131  using std::stringstream;
132  if (checkConsistencyWrtPointsTab(points))
133  return std::string();
134  stringstream ss;
135  ss << "A " << _A << " ?= P(p1) " << points[_p1] << endl;
136  ss << "B " << _B << " ?= P(p2) " << points[_p2] << endl;
137  ss << "C " << _C << " ?= P(p3) " << points[_p3] << endl;
138  return ss.str();
139 }
140 
141 ::std::ostream& operator<<(::std::ostream& os, const OTriangle& t)
142 {
143  os << "OTriangle(";
144  for (unsigned i = 0; i < 3; i++)
145  os << t.index(i) << ", " << t.vertex(i) << ", ";
146  os << ")";
147  return os;
148 }
#define EPSILON_5
Definition: 3d.h:38
std::stringstream ss
Definition: Logger.cpp:21
NxReal s
Definition: NxVec3.cpp:317
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
The 3D point class.
Definition: 3d.h:487
double distFrom(const OPoint3D &pt) const
Computes the distance from this point to another.
Definition: 3d.cpp:371
Triangle class.
Definition: triangle.h:28
OPoint3D _A
First OPoint3D.
Definition: triangle.h:53
double getSurface()
Compute the triangle surface.
Definition: triangle.cpp:87
bool operator==(const OTriangle &other) const
Definition: triangle.cpp:73
static int OTriangle::* indices_m_ptr[3]
Definition: triangle.h:78
int _p1
Index of the first OPoint3D _A.
Definition: triangle.h:49
int _p3
Index of the third OPoint3D _C.
Definition: triangle.h:51
std::string reportInconsistencyWrtPointsTab(const std::deque< OPoint3D > &points)
If inconsistency found between indexes and OPoint3D points, prints it.
Definition: triangle.cpp:128
bool checkConsistencyWrtPointsTab(const std::deque< OPoint3D > &points) const
Check the consistency between indexes of points and the points of this OTriangle.
Definition: triangle.cpp:122
OPoint3D getCentre()
Gives back the triangle center.
Definition: triangle.cpp:113
OTriangle(int p1, int p2, int p3)
Constructor b.
Definition: triangle.cpp:50
int & index(unsigned i)
Definition: triangle.cpp:44
OPoint3D _C
Third OPoint3D.
Definition: triangle.h:55
OPoint3D _B
Second OPoint3D.
Definition: triangle.h:54
static OPoint3D OTriangle::* vertices_m_ptr[3]
< A static table of OPoint3D.
Definition: triangle.h:76
~OTriangle()
Destructor.
Definition: triangle.cpp:85
int _p2
Index of the second OPoint3D _B.
Definition: triangle.h:50
OPoint3D & vertex(unsigned i)
Get the OPoint3D from the specific index.
Definition: triangle.cpp:26
::std::ostream & operator<<(::std::ostream &os, const OTriangle &t)
Definition: triangle.cpp:141