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  ******************************************************************************/
35 namespace librepcb {
36 
37 /*******************************************************************************
38  * Class TangentPathJoiner
39  ******************************************************************************/
40 
58  Q_DECLARE_TR_FUNCTIONS(TangentPathJoiner)
59 
60 public:
61  // Constructors / Destructor
62  TangentPathJoiner() = delete;
63  TangentPathJoiner(const TangentPathJoiner& other) = delete;
64  ~TangentPathJoiner() = delete;
65 
66  // General Methods
67  static QVector<Path> join(QVector<Path> paths,
68  qint64 timeoutMs = -1) noexcept;
69 
70  // Operator Overloadings
71  TangentPathJoiner& operator=(const TangentPathJoiner& rhs) = delete;
72 
73 private:
74  struct Segment {
75  int index;
76  bool reverse;
77  };
78 
79  struct Result {
80  QVector<Segment> segments;
81  QSet<int> indices;
85 
86  Result() : segments(), indices(), startPos(), endPos(), length(0) {}
87 
88  bool isClosed() const noexcept {
89  return (!segments.isEmpty()) && (startPos == endPos);
90  }
91 
92  Result sub(int index, bool reverse, const Point& start, const Point& end,
93  const UnsignedLength& l) const {
94  Result r(*this);
95  r.segments.append(Segment{index, reverse});
96  r.indices.insert(index);
97  if (segments.isEmpty()) {
98  r.startPos = start;
99  }
100  r.endPos = end;
101  r.length += l;
102  return r;
103  }
104 
105  Path buildPath(const QVector<Path>& paths) const {
106  QVector<Vertex> vertices;
107  foreach (const Segment& segment, segments) {
108  if (!vertices.isEmpty()) {
109  vertices.takeLast();
110  }
111  Path p = paths.at(segment.index);
112  if (segment.reverse) {
113  p.reverse();
114  }
115  vertices.append(p.getVertices());
116  }
117  return Path(vertices);
118  }
119  };
120 
121  static void findAllPaths(QVector<Result>& result, const QVector<Path>& paths,
122  const QElapsedTimer& timer, qint64 timeoutMs,
123  const Result& prefix = Result()) noexcept;
124 
125  static tl::optional<Result> join(const QVector<Path>& paths,
126  const Result& prefix, int index,
127  bool reverse) noexcept;
128 };
129 
130 /*******************************************************************************
131  * End of File
132  ******************************************************************************/
133 
134 } // namespace librepcb
135 
136 #endif
Point endPos
Definition: tangentpathjoiner.h:83
bool isClosed() const noexcept
Definition: tangentpathjoiner.h:88
Definition: occmodel.cpp:76
Definition: tangentpathjoiner.h:79
Path buildPath(const QVector< Path > &paths) const
Definition: tangentpathjoiner.h:105
Helper class to join tangent paths (polylines) together.
Definition: tangentpathjoiner.h:57
Point startPos
Definition: tangentpathjoiner.h:82
bool reverse
Definition: tangentpathjoiner.h:76
TangentPathJoiner & operator=(const TangentPathJoiner &rhs)=delete
Path & reverse() noexcept
Definition: path.cpp:226
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
Result sub(int index, bool reverse, const Point &start, const Point &end, const UnsignedLength &l) const
Definition: tangentpathjoiner.h:92
QSet< int > indices
Definition: tangentpathjoiner.h:81
Result()
Definition: tangentpathjoiner.h:86
QVector< Vertex > & getVertices() noexcept
Definition: path.h:72
The Path class represents a list of vertices connected by straight lines or circular arc segments...
Definition: path.h:57
static QVector< Path > join(QVector< Path > paths, qint64 timeoutMs=-1) noexcept
Definition: tangentpathjoiner.cpp:36
UnsignedLength length
Definition: tangentpathjoiner.h:84
Definition: tangentpathjoiner.h:74
int index
Definition: tangentpathjoiner.h:75
static void findAllPaths(QVector< Result > &result, const QVector< Path > &paths, const QElapsedTimer &timer, qint64 timeoutMs, const Result &prefix=Result()) noexcept
Definition: tangentpathjoiner.cpp:166
QVector< Segment > segments
Definition: tangentpathjoiner.h:80
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:670