| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
#ifndef PATHCOMPARE_H |
| 4 |
#define PATHCOMPARE_H |
| 5 |
|
| 6 |
#include <qmath.h> |
| 7 |
|
| 8 |
namespace QPathCompare { |
| 9 |
|
| 10 |
static const int precision = 8; |
| 11 |
static const qreal epsilon = qPow(0.1, precision); |
| 12 |
|
| 13 |
static inline bool fuzzyIsZero(qreal x, qreal relative) |
| 14 |
{ |
| 15 |
if (qAbs(relative) < epsilon) |
| 16 |
return qAbs(x) < epsilon; |
| 17 |
else |
| 18 |
return qAbs(x / relative) < epsilon; |
| 19 |
} |
| 20 |
|
| 21 |
static bool fuzzyCompare(const QPointF &a, const QPointF &b) |
| 22 |
{ |
| 23 |
const QPointF delta = a - b; |
| 24 |
|
| 25 |
const qreal x = qMax(qAbs(a.x()), qAbs(b.x())); |
| 26 |
const qreal y = qMax(qAbs(a.y()), qAbs(b.y())); |
| 27 |
|
| 28 |
return fuzzyIsZero(delta.x(), x) && fuzzyIsZero(delta.y(), y); |
| 29 |
} |
| 30 |
|
| 31 |
static bool isClosed(const QPainterPath &path) |
| 32 |
{ |
| 33 |
if (path.elementCount() == 0) |
| 34 |
return false; |
| 35 |
|
| 36 |
QPointF first = path.elementAt(0); |
| 37 |
QPointF last = path.elementAt(path.elementCount() - 1); |
| 38 |
|
| 39 |
return fuzzyCompare(first, last); |
| 40 |
} |
| 41 |
|
| 42 |
// rotation and direction independent path comparison |
| 43 |
// allows paths to be shifted or reversed relative to each other |
| 44 |
static bool comparePaths(const QPainterPath &actual, const QPainterPath &expected) |
| 45 |
{ |
| 46 |
const int endActual = isClosed(actual) ? actual.elementCount() - 1 : actual.elementCount(); |
| 47 |
const int endExpected = isClosed(expected) ? expected.elementCount() - 1 : expected.elementCount(); |
| 48 |
|
| 49 |
if (endActual != endExpected) |
| 50 |
return false; |
| 51 |
|
| 52 |
for (int i = 0; i < endActual; ++i) { |
| 53 |
int k = 0; |
| 54 |
for (k = 0; k < endActual; ++k) { |
| 55 |
int i1 = k; |
| 56 |
int i2 = (i + k) % endActual; |
| 57 |
|
| 58 |
QPointF a = actual.elementAt(i1); |
| 59 |
QPointF b = expected.elementAt(i2); |
| 60 |
|
| 61 |
if (!fuzzyCompare(a, b)) |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
if (k == endActual) |
| 66 |
return true; |
| 67 |
|
| 68 |
for (k = 0; k < endActual; ++k) { |
| 69 |
int i1 = k; |
| 70 |
int i2 = (i + endActual - k) % endActual; |
| 71 |
|
| 72 |
QPointF a = actual.elementAt(i1); |
| 73 |
QPointF b = expected.elementAt(i2); |
| 74 |
|
| 75 |
if (!fuzzyCompare(a, b)) |
| 76 |
break; |
| 77 |
} |
| 78 |
|
| 79 |
if (k == endActual) |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
#endif |