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// Qt-Security score:significant reason:default
4
6#include <clip2tri.h>
7
9
16
17static const double kClipperScaleFactor = 281474976710656.0; // 48 bits of precision
18static const double kClipperScaleFactorInv = 1.0 / kClipperScaleFactor;
19
20static IntPoint toIntPoint(const QDoubleVector2D &p)
21{
22 return IntPoint(cInt(p.x() * kClipperScaleFactor), cInt(p.y() * kClipperScaleFactor));
23}
24
25static QDoubleVector2D toVector2D(const IntPoint &p)
26{
27 return QDoubleVector2D(double(p.X) * kClipperScaleFactorInv, double(p.Y) * kClipperScaleFactorInv);
28}
29
30static QList<QDoubleVector2D> pathToQList(const Path &path)
31{
32 QList<QDoubleVector2D> res;
33 res.reserve(int(path.size()));
34 for (const IntPoint &ip: path)
35 res.append(toVector2D(ip));
36 return res;
37}
38
39static QList<QList<QDoubleVector2D>> pathsToQList(const Paths &paths)
40{
41 QList<QList<QDoubleVector2D> > res;
42 res.reserve(int(paths.size()));
43 for (const Path &p: paths) {
44 res.append(pathToQList(p));
45 }
46 return res;
47}
48
49static Path qListToPath(const QList<QDoubleVector2D> &list)
50{
51 Path res;
52 res.reserve(list.size());
53 for (const QDoubleVector2D &p: list)
54 res.push_back(toIntPoint(p));
55 return res;
56}
57
58QClipperUtils::QClipperUtils() : d_ptr(new QClipperUtilsPrivate)
59{
60}
61
62QClipperUtils::QClipperUtils(const QClipperUtils &other) : d_ptr(new QClipperUtilsPrivate)
63{
64 d_ptr->m_cachedPolygon = other.d_ptr->m_cachedPolygon;
65}
66
67QClipperUtils::~QClipperUtils()
68{
69 delete d_ptr;
70}
71
72double QClipperUtils::clipperScaleFactor()
73{
74 return kClipperScaleFactor;
75}
76
77int QClipperUtils::pointInPolygon(const QDoubleVector2D &point, const QList<QDoubleVector2D> &polygon)
78{
79 if (polygon.isEmpty())
80 qWarning("No vertices are specified for the polygon!");
81 return c2t::clip2tri::pointInPolygon(toIntPoint(point), qListToPath(polygon));
82}
83
84void QClipperUtils::clearClipper()
85{
86 d_ptr->m_clipper.clearClipper();
87}
88
89void QClipperUtils::addSubjectPath(const QList<QDoubleVector2D> &path, bool closed)
90{
91 d_ptr->m_clipper.addSubjectPath(qListToPath(path), closed);
92}
93
94void QClipperUtils::addClipPolygon(const QList<QDoubleVector2D> &path)
95{
96 d_ptr->m_clipper.addClipPolygon(qListToPath(path));
97}
98
99QList<QList<QDoubleVector2D>> QClipperUtils::execute(QClipperUtils::Operation op,
100 QClipperUtils::PolyFillType subjFillType,
101 QClipperUtils::PolyFillType clipFillType)
102{
103 auto result = d_ptr->m_clipper.execute(static_cast<c2t::clip2tri::Operation>(op),
104 static_cast<QtClipperLib::PolyFillType>(subjFillType),
105 static_cast<QtClipperLib::PolyFillType>(clipFillType));
106 return pathsToQList(result);
107}
108
109void QClipperUtils::setPolygon(const QList<QDoubleVector2D> &polygon)
110{
111 d_ptr->m_cachedPolygon = qListToPath(polygon);
112}
113
114int QClipperUtils::pointInPolygon(const QDoubleVector2D &point) const
115{
116 if (d_ptr->m_cachedPolygon.empty())
117 qWarning("No vertices are specified for the polygon!");
118 return c2t::clip2tri::pointInPolygon(toIntPoint(point), d_ptr->m_cachedPolygon);
119}
120
121QT_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)