1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31 #include <QTextStream>
33 class PolyLine
: public Curve
36 protected: // attributes
38 QVector
<vec3_t
> m_Points
;
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
);
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
);
67 QList
<vec3_t
> raw_points
;
69 QString line
= f
.readLine();
70 QStringList parts
= line
.split(",");
71 if (parts
.size() >= 3) {
73 x
[0] = parts
[0].trimmed().toDouble();
74 x
[1] = parts
[1].trimmed().toDouble();
75 x
[2] = parts
[2].trimmed().toDouble();
80 // find start point of poly-line
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
) {
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
];
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
];
103 // compute the total length of the curve
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
)
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
) {