File indexing completed on 2024-05-12 16:30:46
0001 /* This file is part of the KDE project 0002 Copyright (C) 2001-2002 Lennart Kudling <kudling@kde.org> 0003 Copyright (C) 2002-2003 Rob Buis <buis@kde.org> 0004 Copyright (C) 2005 Thomas Zander <zander@kde.org> 0005 Copyright (C) 2006 Tim Beaulen <tbscope@gmail.com> 0006 Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net> 0007 0008 This library is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU Library General Public 0010 License as published by the Free Software Foundation; either 0011 version 2 of the License, or (at your option) any later version. 0012 0013 This library is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 Library General Public License for more details. 0017 0018 You should have received a copy of the GNU Library General Public License 0019 along with this library; see the file COPYING.LIB. If not, write to 0020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0021 * Boston, MA 02110-1301, USA. 0022 */ 0023 0024 #ifndef KARBONGLOBAL_H 0025 #define KARBONGLOBAL_H 0026 0027 #include <karboncommon_export.h> 0028 0029 #include <QPointF> 0030 0031 // define some often used mathematical constants et al: 0032 0033 // TODO: optimize those values. 0034 0035 namespace KarbonGlobal 0036 { 0037 const qreal pi = 3.14159265358979323846; // pi 0038 const qreal twopi = 6.28318530717958647692; // 2pi 0039 const qreal pi_2 = 1.57079632679489661923; // pi/2 0040 const qreal pi_180 = 0.01745329251994329576; // pi/180 0041 const qreal one_pi_180 = 57.29577951308232087684; // 180/pi 0042 const qreal sqrt2 = 1.41421356237309504880; // sqrt(2) 0043 const qreal one_3 = 0.33333333333333333333; // 1/3 0044 const qreal two_3 = 0.66666666666666666667; // 2/3 0045 const qreal one_6 = 0.16666666666666666667; // 1/6 0046 const qreal one_7 = 0.14285714285714285714; // 1/7 0047 0048 /** 0049 * Constants used to decide if a number is equal zero or nearly the same 0050 * as another number. 0051 */ 0052 const qreal veryBigNumber = 1.0e8; 0053 const qreal verySmallNumber = 1.0e-8; 0054 0055 /** 0056 * A bezier with this flatness is considered "flat". Used in subdividing. 0057 */ 0058 const qreal flatnessTolerance = 0.01; 0059 0060 /** 0061 * A tolerance used to approximate bezier lengths. If the relative difference 0062 * between chordlength and polylength (length of the controlpolygon) is smaller 0063 * than this value, the length of the bezier is 1/2 chordlength + 1/2 polylength. 0064 */ 0065 const qreal lengthTolerance = 0.005; 0066 0067 /** 0068 * A tolerance used to calculate param t on a segment at a given arc 0069 * length (counting from t=0). 0070 * If the relative difference between a length approximation and the given 0071 * length is smaller than this value, they are assumed to be identical. 0072 */ 0073 const qreal paramLengthTolerance = 0.001; 0074 0075 /** 0076 * A range for the isNear() check, to decide if a QPointF "is the same" 0077 * as another. 0078 */ 0079 const qreal isNearRange = 0.001; 0080 0081 /** 0082 * A tolerance for multiplying normalized (length=1) vectors. A result of 0083 * >= parallelTolerance indicates parallel vectors. 0084 */ 0085 const qreal parallelTolerance = 0.99; 0086 0087 /** 0088 * Returns the sign of parameter a. 0089 */ 0090 inline int sign(qreal a) 0091 { 0092 return a < 0.0 0093 ? -1 0094 : 1; 0095 } 0096 0097 /** 0098 * Calculates the binomial coefficient n! / ( k! * ( n - k)! ). 0099 */ 0100 int binomialCoeff(unsigned n, unsigned k); 0101 0102 /** 0103 * Calculates the value ln( n! ). 0104 */ 0105 qreal factorialLn(unsigned n); 0106 0107 /** 0108 * Calculates the value ln| Gamma(x) | for x > 0. 0109 */ 0110 qreal gammaLn(qreal x); 0111 0112 /// Returns scalar product of two given vectors 0113 KARBONCOMMON_EXPORT qreal scalarProduct(const QPointF &p1, const QPointF &p2); 0114 0115 bool pointsAreNear(const QPointF &p1, const QPointF &p2, qreal range); 0116 0117 /// Returns the cross product of two given vectors 0118 QPointF crossProduct(const QPointF &v1, const QPointF &v2); 0119 } 0120 0121 #endif // KARBONGLOBAL_H 0122