tests/auto/qpathclipper/pathcompare.h
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #ifndef PATHCOMPARE_H
       
    42 #define PATHCOMPARE_H
       
    43 
       
    44 #include <qmath.h>
       
    45 
       
    46 namespace QPathCompare {
       
    47 
       
    48 static const int precision = 8;
       
    49 static const qreal epsilon = qPow(0.1, precision);
       
    50 
       
    51 static inline bool fuzzyIsZero(qreal x, qreal relative)
       
    52 {
       
    53     if (qAbs(relative) < epsilon)
       
    54         return qAbs(x) < epsilon;
       
    55     else
       
    56         return qAbs(x / relative) < epsilon;
       
    57 }
       
    58 
       
    59 static bool fuzzyCompare(const QPointF &a, const QPointF &b)
       
    60 {
       
    61     const QPointF delta = a - b;
       
    62 
       
    63     const qreal x = qMax(qAbs(a.x()), qAbs(b.x()));
       
    64     const qreal y = qMax(qAbs(a.y()), qAbs(b.y()));
       
    65 
       
    66     return fuzzyIsZero(delta.x(), x) && fuzzyIsZero(delta.y(), y);
       
    67 }
       
    68 
       
    69 static bool isClosed(const QPainterPath &path)
       
    70 {
       
    71     if (path.elementCount() == 0)
       
    72         return false;
       
    73 
       
    74     QPointF first = path.elementAt(0);
       
    75     QPointF last = path.elementAt(path.elementCount() - 1);
       
    76 
       
    77     return fuzzyCompare(first, last);
       
    78 }
       
    79 
       
    80 // rotation and direction independent path comparison
       
    81 // allows paths to be shifted or reversed relative to each other
       
    82 static bool comparePaths(const QPainterPath &actual, const QPainterPath &expected)
       
    83 {
       
    84     const int endActual = isClosed(actual) ? actual.elementCount() - 1 : actual.elementCount();
       
    85     const int endExpected = isClosed(expected) ? expected.elementCount() - 1 : expected.elementCount();
       
    86 
       
    87     if (endActual != endExpected)
       
    88         return false;
       
    89 
       
    90     for (int i = 0; i < endActual; ++i) {
       
    91         int k = 0;
       
    92         for (k = 0; k < endActual; ++k) {
       
    93             int i1 = k;
       
    94             int i2 = (i + k) % endActual;
       
    95 
       
    96             QPointF a = actual.elementAt(i1);
       
    97             QPointF b = expected.elementAt(i2);
       
    98 
       
    99             if (!fuzzyCompare(a, b))
       
   100                 break;
       
   101         }
       
   102 
       
   103         if (k == endActual)
       
   104             return true;
       
   105 
       
   106         for (k = 0; k < endActual; ++k) {
       
   107             int i1 = k;
       
   108             int i2 = (i + endActual - k) % endActual;
       
   109 
       
   110             QPointF a = actual.elementAt(i1);
       
   111             QPointF b = expected.elementAt(i2);
       
   112 
       
   113             if (!fuzzyCompare(a, b))
       
   114                 break;
       
   115         }
       
   116 
       
   117         if (k == endActual)
       
   118             return true;
       
   119     }
       
   120 
       
   121     return false;
       
   122 }
       
   123 
       
   124 }
       
   125 
       
   126 #endif