updated to modern VTK
[engrid-github.git] / src / libengrid / polyline.cpp
blobf79ee94cd273d3f721119618ae1731f42a4a84bd
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 //
22 #include "polyline.h"
24 void PolyLine::getStencil(double l, int &i1, int &i2, double &w1, double &w2)
26 l *= m_Length;
27 double L = 0;
28 w2 = 1;
29 i1 = m_Points.size() - 2;
30 i2 = m_Points.size() - 1;
31 for (int i = 1; i < m_Points.size(); ++i) {
32 double dL = (m_Points[i] - m_Points[i-1]).abs();
33 if (l >= L && l <= L + dL) {
34 i1 = i - 1;
35 i2 = i;
36 w2 = (l - L)/dL;
37 break;
39 L += dL;
41 w1 = 1 - w2;
44 vec3_t PolyLine::position(double l)
46 int i1, i2;
47 double w1, w2;
48 getStencil(l, i1, i2, w1, w2);
49 return w1*m_Points[i1] + w2*m_Points[i2];
52 vec3_t PolyLine::normal(double l)
54 int i1, i2;
55 double w1, w2;
56 getStencil(l, i1, i2, w1, w2);
57 vec3_t n = m_Points[i2] - m_Points[i1];
58 n.normalise();
59 return n;
62 vec3_t PolyLine::intersection(vec3_t x, vec3_t n)
64 double min_err = EG_LARGE_REAL;
65 vec3_t x_best = x;
66 for (int i = 1; i < m_Points.size(); ++i) {
67 double k = GeometryTools::intersection(m_Points[i-1], m_Points[i] - m_Points[i-1], x, n);
68 double err = fabs(k - 0.5);
69 if (err < min_err) {
70 min_err = err;
71 x_best = (1-k)*m_Points[i-1] + k*m_Points[i];
74 return x_best;