improved performance for face search -- some debugging required
[engrid-github.git] / src / libengrid / tricoord.cpp
blobf57307c36f128f7a390df03b0155628f1d629688
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2013 enGits GmbH +
7 // + +
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. +
12 // + +
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. +
17 // + +
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/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 #include "tricoord.h"
25 TriCoord::TriCoord(vtkUnstructuredGrid* surf_grid, vtkIdType id_tri, vec3_t x)
27 m_SurfGrid = surf_grid;
28 setPosition(x);
31 void TriCoord::setPosition(vec3_t x)
34 bool intersects_face = GeometryTools::intersectEdgeAndTriangle(T.a, T.b, T.c, x1, x2, xi, ri);
35 if (!intersects_face) {
36 double kab = GeometryTools::intersection(T.a, T.b - T.a, xp, T.b - T.a);
37 double kac = GeometryTools::intersection(T.a, T.c - T.a, xp, T.c - T.a);
38 double kbc = GeometryTools::intersection(T.b, T.c - T.b, xp, T.c - T.b);
39 double dab = (T.a + kab*(T.b-T.a) - xp).abs();
40 double dac = (T.a + kac*(T.c-T.a) - xp).abs();
41 double dbc = (T.b + kbc*(T.c-T.b) - xp).abs();
42 bool set = false;
43 if ((kab >= 0) && (kab <= 1)) {
44 if (dab < d) {
45 xi = T.a + kab*(T.b-T.a);
46 d = dab;
47 set = true;
50 if ((kac >= 0) && (kac <= 1)) {
51 if (dac < d) {
52 xi = T.a + kac*(T.c-T.a);
53 d = dac;
54 set = true;
57 if ((kbc >= 0) && (kbc <= 1)) {
58 if (dbc < d) {
59 xi = T.b + kbc*(T.c-T.b);
60 d = dbc;
61 set = true;
64 double da = (T.a - xp).abs();
65 double db = (T.b - xp).abs();
66 double dc = (T.c - xp).abs();
67 if (da < d) {
68 xi = T.a;
69 d = da;
70 set = true;
72 if (db < d) {
73 xi = T.b;
74 d = db;
76 if (dc < d) {
77 xi = T.c;
78 d = dc;
79 set = true;
81 if (!set) {
82 EG_BUG;