Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qclipperutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <clip2tri.h>
6
8
15
16static const double kClipperScaleFactor = 281474976710656.0; // 48 bits of precision
17static const double kClipperScaleFactorInv = 1.0 / kClipperScaleFactor;
18
19static IntPoint toIntPoint(const QDoubleVector2D &p)
20{
21 return IntPoint(cInt(p.x() * kClipperScaleFactor), cInt(p.y() * kClipperScaleFactor));
22}
23
24static QDoubleVector2D toVector2D(const IntPoint &p)
25{
26 return QDoubleVector2D(double(p.X) * kClipperScaleFactorInv, double(p.Y) * kClipperScaleFactorInv);
27}
28
29static QList<QDoubleVector2D> pathToQList(const Path &path)
30{
31 QList<QDoubleVector2D> res;
32 res.reserve(int(path.size()));
33 for (const IntPoint &ip: path)
34 res.append(toVector2D(ip));
35 return res;
36}
37
38static QList<QList<QDoubleVector2D>> pathsToQList(const Paths &paths)
39{
40 QList<QList<QDoubleVector2D> > res;
41 res.reserve(int(paths.size()));
42 for (const Path &p: paths) {
43 res.append(pathToQList(p));
44 }
45 return res;
46}
47
48static Path qListToPath(const QList<QDoubleVector2D> &list)
49{
50 Path res;
51 res.reserve(list.size());
52 for (const QDoubleVector2D &p: list)
53 res.push_back(toIntPoint(p));
54 return res;
55}
56
57QClipperUtils::QClipperUtils() : d_ptr(new QClipperUtilsPrivate)
58{
59}
60
61QClipperUtils::QClipperUtils(const QClipperUtils &other) : d_ptr(new QClipperUtilsPrivate)
62{
63 d_ptr->m_cachedPolygon = other.d_ptr->m_cachedPolygon;
64}
65
66QClipperUtils::~QClipperUtils()
67{
68 delete d_ptr;
69}
70
71double QClipperUtils::clipperScaleFactor()
72{
73 return kClipperScaleFactor;
74}
75
76int QClipperUtils::pointInPolygon(const QDoubleVector2D &point, const QList<QDoubleVector2D> &polygon)
77{
78 if (polygon.isEmpty())
79 qWarning("No vertices are specified for the polygon!");
80 return c2t::clip2tri::pointInPolygon(toIntPoint(point), qListToPath(polygon));
81}
82
83void QClipperUtils::clearClipper()
84{
85 d_ptr->m_clipper.clearClipper();
86}
87
88void QClipperUtils::addSubjectPath(const QList<QDoubleVector2D> &path, bool closed)
89{
90 d_ptr->m_clipper.addSubjectPath(qListToPath(path), closed);
91}
92
93void QClipperUtils::addClipPolygon(const QList<QDoubleVector2D> &path)
94{
95 d_ptr->m_clipper.addClipPolygon(qListToPath(path));
96}
97
98QList<QList<QDoubleVector2D>> QClipperUtils::execute(QClipperUtils::Operation op,
99 QClipperUtils::PolyFillType subjFillType,
100 QClipperUtils::PolyFillType clipFillType)
101{
102 auto result = d_ptr->m_clipper.execute(static_cast<c2t::clip2tri::Operation>(op),
103 static_cast<QtClipperLib::PolyFillType>(subjFillType),
104 static_cast<QtClipperLib::PolyFillType>(clipFillType));
105 return pathsToQList(result);
106}
107
108void QClipperUtils::setPolygon(const QList<QDoubleVector2D> &polygon)
109{
110 d_ptr->m_cachedPolygon = qListToPath(polygon);
111}
112
113int QClipperUtils::pointInPolygon(const QDoubleVector2D &point) const
114{
115 if (d_ptr->m_cachedPolygon.empty())
116 qWarning("No vertices are specified for the polygon!");
117 return c2t::clip2tri::pointInPolygon(toIntPoint(point), d_ptr->m_cachedPolygon);
118}
119
120QT_END_NAMESPACE
c2t::clip2tri m_clipper
Combined button and popup list for selecting options.
static IntPoint toIntPoint(const QDoubleVector2D &p)
static const double kClipperScaleFactor
static QDoubleVector2D toVector2D(const IntPoint &p)
static const double kClipperScaleFactorInv
static QList< QList< QDoubleVector2D > > pathsToQList(const Paths &paths)
static QList< QDoubleVector2D > pathToQList(const Path &path)
static Path qListToPath(const QList< QDoubleVector2D > &list)