Sat, 26 Jul 2014 03:43:37 +0300
- refactor
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
844
11587d419d2f
- changed copyright lines to use my legal name instead of my nickname
Teemu Piippo <crimsondusk64@gmail.com>
parents:
818
diff
changeset
|
3 | * Copyright (C) 2013, 2014 Teemu Piippo |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | */ |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | #pragma once |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include <QString> |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | #include <QObject> |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | #include <QStringList> |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #include <QMetaType> |
733
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
24 | #include <QVector3D> |
768
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
25 | #include <QSharedPointer> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | #include "macros.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | class LDObject; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | class QFile; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | class QTextStream; |
768
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
31 | class Matrix; |
784
f82ab4d3c7b4
- made LDDocument use shared pointers, this eliminates a lot of document-related crashes
Santeri Piippo <crimsondusk64@gmail.com>
parents:
768
diff
changeset
|
32 | class LDDocument; |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | using int8 = qint8; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | using int16 = qint16; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | using int32 = qint32; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | using int64 = qint64; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | using uint8 = quint8; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | using uint16 = quint16; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | using uint32 = quint32; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | using uint64 = quint64; |
768
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
42 | using LDObjectPtr = QSharedPointer<LDObject>; |
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
43 | using LDObjectList = QList<LDObjectPtr>; |
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
44 | using LDObjectWeakPtr = QWeakPointer<LDObject>; |
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
45 | using LDObjectWeakList = QList<LDObjectWeakPtr>; |
784
f82ab4d3c7b4
- made LDDocument use shared pointers, this eliminates a lot of document-related crashes
Santeri Piippo <crimsondusk64@gmail.com>
parents:
768
diff
changeset
|
46 | using LDDocumentPtr = QSharedPointer<LDDocument>; |
f82ab4d3c7b4
- made LDDocument use shared pointers, this eliminates a lot of document-related crashes
Santeri Piippo <crimsondusk64@gmail.com>
parents:
768
diff
changeset
|
47 | using LDDocumentWeakPtr = QWeakPointer<LDDocument>; |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | template<typename T, typename R> |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | using Pair = std::pair<T, R>; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | enum Axis |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | { |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | X, |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | Y, |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | Z |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | }; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | |
794 | 59 | // |
60 | // Derivative of QVector3D: this class is used for the vertices. | |
61 | // | |
733
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
62 | class Vertex : public QVector3D |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
63 | { |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
64 | public: |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
65 | using ApplyFunction = std::function<void (Axis, double&)>; |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
66 | using ApplyConstFunction = std::function<void (Axis, double)>; |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
67 | |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
68 | Vertex(); |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
69 | Vertex (const QVector3D& a); |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
70 | Vertex (qreal xpos, qreal ypos, qreal zpos); |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
71 | |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
72 | void apply (ApplyFunction func); |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
73 | void apply (ApplyConstFunction func) const; |
789
4b7306f52bb5
- String -> QString
Santeri Piippo <crimsondusk64@gmail.com>
parents:
784
diff
changeset
|
74 | QString toString (bool mangled = false) const; |
733
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
75 | void transform (const Matrix& matr, const Vertex& pos); |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
76 | void setCoordinate (Axis ax, qreal value); |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
77 | |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
78 | bool operator< (const Vertex& other) const; |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
79 | double operator[] (Axis ax) const; |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
80 | }; |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
81 | |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
82 | Q_DECLARE_METATYPE (Vertex) |
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
83 | |
794 | 84 | // |
85 | // A mathematical 3 x 3 matrix | |
86 | // | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | class Matrix |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | { |
794 | 89 | public: |
90 | Matrix() {} | |
91 | Matrix (const std::initializer_list<double>& vals); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | |
794 | 93 | // Constructs a matrix all 9 elements initialized to the same value. |
94 | Matrix (double fillval); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | |
794 | 96 | // Constructs a matrix with a C-array. |
97 | // note: @vals is expected to have exactly 9 elements. | |
98 | Matrix (double vals[]); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | |
794 | 100 | // Calculates the matrix's determinant. |
101 | double getDeterminant() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | |
794 | 103 | // Multiplies this matrix with @other |
104 | // note: a.mult(b) is not equivalent to b.mult(a)! | |
105 | Matrix mult (const Matrix& other) const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
794 | 107 | // Prints the matrix to stdout. |
108 | void dump() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | |
794 | 110 | // Yields a string representation of the matrix. |
111 | QString toString() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | |
794 | 113 | // Zeroes the matrix out. |
114 | void zero(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | |
794 | 116 | // Assigns the matrix values to the values of @other. |
117 | Matrix& operator= (const Matrix& other); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | |
794 | 119 | // Returns a mutable reference to a value by @idx |
120 | inline double& value (int idx) | |
121 | { | |
122 | return m_vals[idx]; | |
123 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | |
794 | 125 | // An overload of value() for const matrices. |
126 | inline const double& value (int idx) const | |
127 | { | |
128 | return m_vals[idx]; | |
129 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | |
794 | 131 | // An operator overload for mult(). |
132 | inline Matrix operator* (const Matrix& other) const | |
133 | { | |
134 | return mult (other); | |
135 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | |
794 | 137 | // An operator overload for value(). |
138 | inline double& operator[] (int idx) | |
139 | { | |
140 | return value (idx); | |
141 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | |
794 | 143 | // An operator overload for value() const. |
144 | inline const double& operator[] (int idx) const | |
145 | { | |
146 | return value (idx); | |
147 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | |
794 | 149 | // Checks whether the two matrices have the same values. |
150 | bool operator== (const Matrix& other) const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | |
794 | 152 | private: |
153 | double m_vals[9]; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | }; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | |
794 | 156 | // |
157 | // Defines a bounding box that encompasses a given set of objects. | |
158 | // vertex0 is the minimum vertex, vertex1 is the maximum vertex. | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | // |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | class LDBoundingBox |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | { |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | PROPERTY (private, bool, isEmpty, setEmpty, STOCK_WRITE) |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | PROPERTY (private, Vertex, vertex0, setVertex0, STOCK_WRITE) |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | PROPERTY (private, Vertex, vertex1, setVertex1, STOCK_WRITE) |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | |
794 | 166 | public: |
167 | // Constructs an empty bounding box. | |
168 | LDBoundingBox(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | |
794 | 170 | // Clears the bounding box |
171 | void reset(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | |
794 | 173 | // Calculates the bounding box's values from the objects in the current |
174 | // document. | |
175 | void calculateFromCurrentDocument(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
176 | |
794 | 177 | // Returns the length of the bounding box on the longest measure. |
178 | double longestMeasurement() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
179 | |
794 | 180 | // Calculates the given \c obj to the bounding box, adjusting |
181 | // extremas if necessary. | |
182 | void calcObject (LDObjectPtr obj); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | |
794 | 184 | // Calculates the given \c vertex to the bounding box, adjusting |
185 | // extremas if necessary. | |
186 | void calcVertex (const Vertex& vertex); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | |
794 | 188 | // Yields the center of the bounding box. |
189 | Vertex center() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | |
794 | 191 | // An operator overload for calcObject() |
192 | LDBoundingBox& operator<< (LDObjectPtr obj); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | |
794 | 194 | // An operator overload for calcVertex() |
195 | LDBoundingBox& operator<< (const Vertex& v); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | }; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | extern const Vertex g_origin; // Vertex at (0, 0, 0) |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | extern const Matrix g_identity; // Identity matrix |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
201 | static const double pi = 3.14159265358979323846; |
816 | 202 | |
203 | ||
204 | // ============================================================================= | |
205 | // Plural expression | |
206 | template<class T> static inline const char* plural (T n) | |
207 | { | |
208 | return (n != 1) ? "s" : ""; | |
209 | } | |
210 | ||
211 | // ============================================================================= | |
212 | // Templated clamp | |
213 | template<class T> static inline T clamp (T a, T min, T max) | |
214 | { | |
215 | return (a > max) ? max : (a < min) ? min : a; | |
216 | } | |
217 | ||
218 | // Templated minimum | |
219 | template<class T> static inline T min (T a, T b) | |
220 | { | |
221 | return (a < b) ? a : b; | |
222 | } | |
223 | ||
224 | // Templated maximum | |
225 | template<class T> static inline T max (T a, T b) | |
226 | { | |
227 | return (a > b) ? a : b; | |
228 | } | |
229 | ||
230 | // Templated absolute value | |
231 | template<class T> static inline T abs (T a) | |
232 | { | |
233 | return (a >= 0) ? a : -a; | |
234 | } | |
235 | ||
236 | template<class T> inline bool isZero (T a) | |
237 | { | |
238 | return abs<T> (a) < 0.0001; | |
239 | } | |
240 | ||
241 | template<class T> inline bool isInteger (T a) | |
242 | { | |
243 | return isZero (a - (int) a); | |
244 | } | |
245 | ||
818
748ba8818af8
- added the magic wand tool
Santeri Piippo <crimsondusk64@gmail.com>
parents:
816
diff
changeset
|
246 | template<typename T> |
847 | 247 | inline bool within (T a, T b, T c) |
248 | { | |
249 | return a >= b and a <= c; | |
250 | } | |
251 | ||
252 | template<typename T> | |
818
748ba8818af8
- added the magic wand tool
Santeri Piippo <crimsondusk64@gmail.com>
parents:
816
diff
changeset
|
253 | void removeDuplicates (T& a) |
816 | 254 | { |
255 | std::sort (a.begin(), a.end()); | |
256 | a.erase (std::unique (a.begin(), a.end()), a.end()); | |
257 | } | |
258 | ||
259 | inline QString utf16 (const char16_t* a) | |
260 | { | |
261 | if (Q_LIKELY (sizeof(char16_t) == sizeof(unsigned short))) | |
262 | return QString::fromUtf16 (reinterpret_cast<const unsigned short*> (a)); | |
263 | ||
264 | QVector<unsigned short> data; | |
265 | ||
266 | for (const char16_t* ap = &a[0]; *ap != '\u0000'; ++ap) | |
267 | data << *ap; | |
268 | ||
269 | data << '\u0000'; | |
270 | return QString::fromUtf16 (data.constData()); | |
271 | } | |
272 | ||
273 | // | |
274 | // Returns true if first arg is equal to any of the other args | |
275 | // | |
276 | template<typename T, typename Arg, typename... Args> | |
277 | bool eq (T const& a, Arg const& arg, Args const&... args) | |
278 | { | |
279 | if (a == arg) | |
280 | return true; | |
281 | ||
282 | return eq (a, args...); | |
283 | } | |
284 | ||
285 | template<typename T> | |
286 | bool eq (T const&) | |
287 | { | |
288 | return false; | |
289 | } |