Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qclipperutils_p.h"
5#include <clip2tri.h>
6
8
10{
11public:
12 c2t::clip2tri m_clipper;
14};
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
60
62{
63 d_ptr->m_cachedPolygon = other.d_ptr->m_cachedPolygon;
64}
65
67{
68 delete d_ptr;
69}
70
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
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{
111}
112
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
c2t::clip2tri m_clipper
static double clipperScaleFactor()
void setPolygon(const QList< QDoubleVector2D > &polygon)
void addClipPolygon(const QList< QDoubleVector2D > &path)
static int pointInPolygon(const QDoubleVector2D &point, const QList< QDoubleVector2D > &polygon)
void addSubjectPath(const QList< QDoubleVector2D > &path, bool closed)
QList< QList< QDoubleVector2D > > execute(Operation op, PolyFillType subjFillType=pftNonZero, PolyFillType clipFillType=pftNonZero)
qsizetype size() const noexcept
Definition qlist.h:397
Combined button and popup list for selecting options.
static IntPoint toIntPoint(const QDoubleVector2D &p)
static const double kClipperScaleFactorInv
static const double kClipperScaleFactor
static QDoubleVector2D toVector2D(const IntPoint &p)
static QList< QList< QDoubleVector2D > > pathsToQList(const Paths &paths)
static QList< QDoubleVector2D > pathToQList(const Path &path)
static Path qListToPath(const QList< QDoubleVector2D > &list)
#define qWarning
Definition qlogging.h:166
GLsizei const GLuint * paths
GLuint res
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
QList< int > list
[14]
QSharedPointer< T > other(t)
[5]