limited volume meshing to boundary layer only
[engrid-github.git] / src / libengrid / polyline.h
blob3363aecf38a6d6300efd5d5dd84b8eaf4f1e3541
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 #ifndef POLYLINE_H
23 #define POLYLINE_H
25 class PolyLine;
27 #include "curve.h"
29 #include <QVector>
30 #include <QFile>
31 #include <QTextStream>
33 class PolyLine : public Curve
36 protected: // attributes
38 QVector<vec3_t> m_Points;
39 double m_Length;
42 protected: // methods
44 template <typename Q_CONTAINER1, typename Q_CONTAINER2>
45 int findClosest(vec3_t x, const Q_CONTAINER1& points, const Q_CONTAINER2& exclude);
47 void getStencil(double l, int &i1, int &i2, double &w1, double &w2);
49 public:
51 template <typename Q_CONTAINER>
52 void fromPointsFile(QString points_file, const Q_CONTAINER& start_points);
54 virtual vec3_t position(double l);
55 virtual vec3_t normal(double l);
56 virtual vec3_t intersection(vec3_t x, vec3_t n);
60 template <typename Q_CONTAINER>
61 void PolyLine::fromPointsFile(QString points_file, const Q_CONTAINER& start_points)
63 // read raw point data from file
64 QFile file(points_file);
65 file.open(QIODevice::ReadOnly);
66 QTextStream f(&file);
67 QList<vec3_t> raw_points;
68 while (!f.atEnd()) {
69 QString line = f.readLine();
70 QStringList parts = line.split(",");
71 if (parts.size() >= 3) {
72 vec3_t x;
73 x[0] = parts[0].trimmed().toDouble();
74 x[1] = parts[1].trimmed().toDouble();
75 x[2] = parts[2].trimmed().toDouble();
76 raw_points << x;
80 // find start point of poly-line
81 QList<int> exclude;
82 int i_start = 0;
83 double min_dist = EG_LARGE_REAL;
84 foreach (vec3_t x, start_points) {
85 int i_best = findClosest(x, raw_points, exclude);
86 double dist = (x - raw_points[i_best]).abs();
87 if (dist < min_dist) {
88 min_dist = dist;
89 i_start = i_best;
93 // sort points in such a way that they form a curve ...
94 m_Points.resize(raw_points.size());
95 m_Points[0] = raw_points[i_start];
96 exclude << i_start;
97 for (int i = 1; i < raw_points.size(); ++i) {
98 int i_next = findClosest(m_Points[i-1], raw_points, exclude);
99 m_Points[i] = raw_points[i_next];
100 exclude << i_next;
103 // compute the total length of the curve
104 m_Length = 0;
105 for (int i = 1; i < m_Points.size(); ++i) {
106 m_Length += (m_Points[i] - m_Points[i-1]).abs();
110 template <typename Q_CONTAINER1, typename Q_CONTAINER2>
111 int PolyLine::findClosest(vec3_t x, const Q_CONTAINER1& points, const Q_CONTAINER2& exclude)
113 int i_best = -1;
114 double min_dist = EG_LARGE_REAL;
115 for (int i = 0; i < points.size(); ++i) {
116 if (!exclude.contains(i)) {
117 double dist = (points[i] - x).abs();
118 if (dist < min_dist) {
119 min_dist = dist;
120 i_best = i;
124 return i_best;
127 #endif // POLYLINE_H