2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008,2009 Oliver Gloth +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 #include "geometrytools.h"
26 #include "utilities.h"
28 Triangle::Triangle() {
38 Triangle::Triangle(vec3_t a_a
, vec3_t a_b
, vec3_t a_c
) {
45 Triangle::Triangle(vtkUnstructuredGrid
* a_grid
, vtkIdType a_id_a
, vtkIdType a_id_b
, vtkIdType a_id_c
) {
49 a_grid
->GetPoints()->GetPoint(id_a
, a
.data());
50 a_grid
->GetPoints()->GetPoint(id_b
, b
.data());
51 a_grid
->GetPoints()->GetPoint(id_c
, c
.data());
55 Triangle::Triangle(vtkUnstructuredGrid
* a_grid
, vtkIdType a_id_cell
) {
57 a_grid
->GetCellPoints(a_id_cell
, Npts
, pts
);
62 a_grid
->GetPoints()->GetPoint(id_a
, a
.data());
63 a_grid
->GetPoints()->GetPoint(id_b
, b
.data());
64 a_grid
->GetPoints()->GetPoint(id_c
, c
.data());
67 EG_ERR_RETURN("only triangles allowed at the moment");
71 void Triangle::setupTriangle() {
72 m_has_neighbour
.resize(6);
73 m_has_neighbour
[0] = false;
74 m_has_neighbour
[1] = false;
75 m_has_neighbour
[2] = false;
76 m_has_neighbour
[3] = false;
77 m_has_neighbour
[4] = false;
78 m_has_neighbour
[5] = false;
92 smallest_length
= (b
- a
).abs();
93 smallest_length
= min(smallest_length
, (c
- b
).abs());
94 smallest_length
= min(smallest_length
, (a
- c
).abs());
97 vec3_t
Triangle::local3DToGlobal3D(vec3_t l_M
) {
101 vec3_t
Triangle::global3DToLocal3D(vec3_t g_M
) {
102 vec3_t tmp
= g_M
- a
;
106 vec3_t
Triangle::local2DToGlobal3D(vec2_t l_M
) {
107 return local3DToGlobal3D(vec3_t(l_M
[0], l_M
[1], 0));
110 vec2_t
Triangle::global3DToLocal2D(vec3_t g_M
) {
111 vec3_t l_M
= global3DToLocal3D(g_M
);
112 return vec2_t(l_M
[0], l_M
[1]);
115 bool Triangle::projectOnTriangle(vec3_t xp
, vec3_t
&xi
, vec3_t
&ri
, double &d
, int& side
, bool restrict_to_triangle
) {
117 double scal
= (xp
- this->a
) * this->g3
;
121 x2
= xp
- scal
* this->g3
- this->g3
;
124 x2
= xp
- scal
* this->g3
+ this->g3
;
126 // (xi,ri) gets set to the intersection of the line with the plane here!
127 bool intersects_face
= GeometryTools::intersectEdgeAndTriangle(this->a
, this->b
, this->c
, x1
, x2
, xi
, ri
);
128 if (intersects_face
|| !restrict_to_triangle
) {
129 vec3_t dx
= xp
- this->a
;
130 d
= fabs(dx
* this->g3
);
132 double kab
= GeometryTools::intersection(this->a
, this->b
- this->a
, xp
, this->b
- this->a
);
133 double kac
= GeometryTools::intersection(this->a
, this->c
- this->a
, xp
, this->c
- this->a
);
134 double kbc
= GeometryTools::intersection(this->b
, this->c
- this->b
, xp
, this->c
- this->b
);
136 double dab
= (this->a
+ kab
* (this->b
- this->a
) - xp
).abs();
137 double dac
= (this->a
+ kac
* (this->c
- this->a
) - xp
).abs();
138 double dbc
= (this->b
+ kbc
* (this->c
- this->b
) - xp
).abs();
139 double da
= (this->a
- xp
).abs();
140 double db
= (this->b
- xp
).abs();
141 double dc
= (this->c
- xp
).abs();
144 d
= 1e99
;//max(max(max(max(max(dab,dac),dbc),da),db),dc);
147 if ((kab
>= 0) && (kab
<= 1)) {
148 xi
= this->a
+ kab
* (this->b
- this->a
);
149 ri
= vec3_t(kab
, 0, 0);
156 if ((kbc
>= 0) && (kbc
<= 1)) {
157 xi
= this->b
+ kbc
* (this->c
- this->b
);
158 ri
= vec3_t(1 - kbc
, kbc
, 0);
165 if ((kac
>= 0) && (kac
<= 1)) {
166 xi
= this->a
+ kac
* (this->c
- this->a
);
167 ri
= vec3_t(0, kac
, 0);
198 if (xi
[0] > 1e98
) { // should never happen
201 /* if (not( 0<=ri[0] && ri[0]<=1 && 0<=ri[1] && ri[1]<=1 && ri[2]==0 )) {
202 qWarning()<<"ri="<<ri;
205 return intersects_face
;