LibrePCB Developers Documentation
tangentpathjoiner.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_CORE_TANGENTPATHJOINER_H
21#define LIBREPCB_CORE_TANGENTPATHJOINER_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../geometry/path.h"
27
28#include <optional/tl/optional.hpp>
29
30#include <QtCore>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37/*******************************************************************************
38 * Class TangentPathJoiner
39 ******************************************************************************/
40
58 Q_DECLARE_TR_FUNCTIONS(TangentPathJoiner)
59
60public:
61 // Constructors / Destructor
63 TangentPathJoiner(const TangentPathJoiner& other) = delete;
65
66 // General Methods
67 static QVector<Path> join(QVector<Path> paths, qint64 timeoutMs = -1,
68 bool* timedOut = nullptr) noexcept;
69
70 // Operator Overloadings
71 TangentPathJoiner& operator=(const TangentPathJoiner& rhs) = delete;
72
73private:
74 struct Segment {
75 int index;
76 bool reverse;
77 };
78
79 struct Result {
80 QVector<Segment> segments;
81 QSet<int> indices;
82 QSet<Point> junctions;
85 mutable qreal lengthAreaCache;
86
88 : segments(),
89 indices(),
90 junctions(),
91 startPos(),
92 endPos(),
94
95 bool isClosed() const noexcept {
96 return (!segments.isEmpty()) && (startPos == endPos);
97 }
98
99 qreal calcLengthOrArea(const QVector<Path>& paths) const noexcept {
100 if (lengthAreaCache == 0) {
101 const Path path = buildPath(paths);
104 : path.getTotalStraightLength()->toMm();
105 }
106 return lengthAreaCache;
107 }
108
109 Result sub(int index, bool reverse, const Point& start,
110 const Point& end) const {
111 Result r(*this);
112 r.segments.append(Segment{index, reverse});
113 r.indices.insert(index);
114 r.junctions.insert(end);
115 if (segments.isEmpty()) {
116 r.startPos = start;
117 }
118 r.endPos = end;
119 return r;
120 }
121
122 Path buildPath(const QVector<Path>& paths) const {
123 QVector<Vertex> vertices;
124 foreach (const Segment& segment, segments) {
125 if (!vertices.isEmpty()) {
126 vertices.takeLast();
127 }
128 Path p = paths.at(segment.index);
129 if (segment.reverse) {
130 p.reverse();
131 }
132 vertices.append(p.getVertices());
133 }
134 return Path(vertices);
135 }
136 };
137
138 static void findAllPaths(QVector<Result>& result, const QVector<Path>& paths,
139 const QElapsedTimer& timer, qint64 timeoutMs,
140 const Result& prefix = Result(),
141 bool* timedOut = nullptr) noexcept;
142
143 static tl::optional<Result> join(const QVector<Path>& paths,
144 const Result& prefix, int index,
145 bool reverse) noexcept;
146};
147
148/*******************************************************************************
149 * End of File
150 ******************************************************************************/
151
152} // namespace librepcb
153
154#endif
The Path class represents a list of vertices connected by straight lines or circular arc segments.
Definition: path.h:58
Path & reverse() noexcept
Definition: path.cpp:253
QVector< Vertex > & getVertices() noexcept
Definition: path.h:74
qreal calcAreaOfStraightSegments() const noexcept
Definition: path.cpp:99
UnsignedLength getTotalStraightLength() const noexcept
Definition: path.cpp:86
bool isClosed() const noexcept
Definition: path.cpp:55
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition: point.h:79
Helper class to join tangent paths (polylines) together.
Definition: tangentpathjoiner.h:57
static QVector< Path > join(QVector< Path > paths, qint64 timeoutMs=-1, bool *timedOut=nullptr) noexcept
Definition: tangentpathjoiner.cpp:36
TangentPathJoiner(const TangentPathJoiner &other)=delete
static void findAllPaths(QVector< Result > &result, const QVector< Path > &paths, const QElapsedTimer &timer, qint64 timeoutMs, const Result &prefix=Result(), bool *timedOut=nullptr) noexcept
Definition: tangentpathjoiner.cpp:173
Definition: occmodel.cpp:77
Definition: uuid.h:183
Definition: tangentpathjoiner.h:79
qreal calcLengthOrArea(const QVector< Path > &paths) const noexcept
Definition: tangentpathjoiner.h:99
Point startPos
Definition: tangentpathjoiner.h:83
QSet< Point > junctions
Definition: tangentpathjoiner.h:82
Path buildPath(const QVector< Path > &paths) const
Definition: tangentpathjoiner.h:122
Result()
Definition: tangentpathjoiner.h:87
qreal lengthAreaCache
Definition: tangentpathjoiner.h:85
QVector< Segment > segments
Definition: tangentpathjoiner.h:80
Result sub(int index, bool reverse, const Point &start, const Point &end) const
Definition: tangentpathjoiner.h:109
QSet< int > indices
Definition: tangentpathjoiner.h:81
Point endPos
Definition: tangentpathjoiner.h:84
bool isClosed() const noexcept
Definition: tangentpathjoiner.h:95
Definition: tangentpathjoiner.h:74
bool reverse
Definition: tangentpathjoiner.h:76
int index
Definition: tangentpathjoiner.h:75