src/basics.h

Thu, 17 Nov 2016 13:56:13 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 17 Nov 2016 13:56:13 +0200
changeset 1056
27b7991b3bea
parent 1053
2a48c0fff107
child 1058
695edd4f0411
permissions
-rw-r--r--

Cleanup in GL renderer...

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
1014
f0a8ecb6a357 Happy new year 2016!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1013
diff changeset
3 * Copyright (C) 2013 - 2016 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
1006
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1002
diff changeset
8 * (at your option) any later version.
655
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>
920
147497785496 - fixed stability problems
Teemu Piippo <crimsondusk64@gmail.com>
parents: 890
diff changeset
25 #include <functional>
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include "macros.h"
1017
fc1c13db9618 Renamed ConfigurationValueBag to Configuration and added a pointer to it into HierarchyElement. This helps with the fight against global variables.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1015
diff changeset
27 #include "transform.h"
1037
4a9185e94d78 Moved matrix into new source/header pair in types/
Teemu Piippo <teemu@compsta2.com>
parents: 1036
diff changeset
28 #include "types/matrix.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 class LDObject;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 class QFile;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 class QTextStream;
768
29e6c5552388 - initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 739
diff changeset
33 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
34 class LDDocument;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 using int8 = qint8;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 using int16 = qint16;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 using int32 = qint32;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 using int64 = qint64;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 using uint8 = quint8;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 using uint16 = quint16;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 using uint32 = quint32;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 using uint64 = quint64;
944
1a6f1997fcbe Replace casts
Teemu Piippo <crimsondusk64@gmail.com>
parents: 920
diff changeset
44 using LDObjectList = QList<LDObject*>;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 template<typename T, typename R>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 using Pair = std::pair<T, R>;
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 enum Axis
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 X,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 Y,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 Z
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
1056
27b7991b3bea Cleanup in GL renderer...
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
56 static const Axis axes[] = {X, Y, Z};
27b7991b3bea Cleanup in GL renderer...
Teemu Piippo <teemu@hecknology.net>
parents: 1053
diff changeset
57
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
58 //
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
59 // Derivative of QVector3D: this class is used for the vertices.
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
60 //
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
61 class Vertex : public QVector3D
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
62 {
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
63 public:
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
64 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
65 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
66
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
67 Vertex();
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
68 Vertex (const QVector3D& a);
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
69 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
70
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
71 void apply (ApplyFunction func);
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
72 void apply (ApplyConstFunction func) const;
789
4b7306f52bb5 - String -> QString
Santeri Piippo <crimsondusk64@gmail.com>
parents: 784
diff changeset
73 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
74 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
75 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
76
1002
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
77 Vertex& operator+= (const Vertex& other);
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
78 Vertex operator+ (const Vertex& other) const;
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
79 Vertex& operator*= (qreal scalar);
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
80 Vertex operator* (qreal scalar) const;
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
81 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
82 double operator[] (Axis ax) const;
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
83 };
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
84
1002
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
85 inline Vertex operator* (qreal scalar, const Vertex& vertex)
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
86 {
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
87 return vertex * scalar;
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
88 }
a3ee23db8393 Bézier curves may now be serialized down using the "Demote" function. I need to rename that now.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 978
diff changeset
89
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
90 Q_DECLARE_METATYPE (Vertex)
1031
55c0d3beea0d removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1026
diff changeset
91 uint qHash(const Vertex& key);
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
92
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
93 //
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
94 // Defines a bounding box that encompasses a given set of objects.
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
95 // vertex0 is the minimum vertex, vertex1 is the maximum vertex.
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 //
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1010
diff changeset
97 class BoundingBox
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 {
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
99 public:
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1010
diff changeset
100 BoundingBox();
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
1010
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
102 void calcObject (LDObject* obj);
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
103 void calcVertex (const Vertex& vertex);
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
104 Vertex center() const;
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
105 bool isEmpty() const;
794
c254ddc6618b - source reformat
Santeri Piippo <crimsondusk64@gmail.com>
parents: 789
diff changeset
106 double longestMeasurement() const;
1010
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
107 void reset();
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
108 const Vertex& vertex0() const;
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
109 const Vertex& vertex1() const;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1010
diff changeset
111 BoundingBox& operator<< (LDObject* obj);
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1010
diff changeset
112 BoundingBox& operator<< (const Vertex& v);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
1010
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
114 private:
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
115 bool m_isEmpty;
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
116 Vertex m_vertex0;
969b48eddd6b A bit more cleanup. Finally removed the PROPERTY macro. (now let's see about making a new one sometime that isn't so terrible?)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1006
diff changeset
117 Vertex m_vertex1;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 847
diff changeset
120 extern const Vertex Origin;
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 847
diff changeset
121 extern const Matrix IdentityMatrix;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
1026
fb320996cce0 Rename Pi to lowercase pi
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1017
diff changeset
123 static const double pi = 3.14159265358979323846;
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
124
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
125
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
126 // =============================================================================
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
127 // Plural expression
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 847
diff changeset
128 template<typename T>
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
129 static inline const char* plural (T n)
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
130 {
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
131 return (n != 1) ? "s" : "";
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
132 }
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
133
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 847
diff changeset
134 template<typename T>
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
135 bool isZero (T a)
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
136 {
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
137 return qFuzzyCompare (a + 1.0, 1.0);
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
138 }
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
139
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 847
diff changeset
140 template<typename T>
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
141 bool isInteger (T a)
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 847
diff changeset
142 {
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
143 return (qAbs (a - floor(a)) < 0.00001) or (qAbs (a - ceil(a)) < 0.00001);
847
274a7fac44fc - refactor
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
144 }
274a7fac44fc - refactor
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
145
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
146 //
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
147 // Returns true if first arg is equal to any of the other args
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
148 //
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
149 template<typename T, typename Arg, typename... Args>
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
150 bool isOneOf (T const& a, Arg const& arg, Args const&... args)
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
151 {
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
152 if (a == arg)
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
153 return true;
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
154
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
155 return isOneOf (a, args...);
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
156 }
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
157
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
158 template<typename T>
966
a834e43a57da Replaced Min/Max/Clamp/Abs with use of Qt versions of them.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 946
diff changeset
159 bool isOneOf (T const&)
816
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
160 {
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
161 return false;
9adb822de7b9 - refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 794
diff changeset
162 }
1015
92c6ec099075 Added triangle count to viewport, added compile-time line length check
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
163
92c6ec099075 Added triangle count to viewport, added compile-time line length check
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
164 inline void toggle (bool& a)
92c6ec099075 Added triangle count to viewport, added compile-time line length check
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
165 {
92c6ec099075 Added triangle count to viewport, added compile-time line length check
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
166 a = not a;
92c6ec099075 Added triangle count to viewport, added compile-time line length check
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
167 }
1036
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
168
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
169 //
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
170 // Iterates an enum
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
171 //
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
172 template<typename Enum>
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
173 struct EnumIterShell
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
174 {
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
175 struct Iterator
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
176 {
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
177 Iterator(typename std::underlying_type<Enum>::type i) :
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
178 i(i) {}
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
179
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
180 Iterator& operator++() { ++i; return *this; }
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
181 bool operator==(Iterator other) { return i == other.i; }
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
182 bool operator!=(Iterator other) { return i != other.i; }
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
183 Enum operator*() const { return Enum(i); }
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
184
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
185 typename std::underlying_type<Enum>::type i;
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
186 };
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
187
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
188 Iterator begin()
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
189 {
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
190 return Iterator(EnumLimits<Enum>::First);
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
191 };
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
192
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
193 Iterator end()
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
194 {
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
195 return Iterator(EnumLimits<Enum>::End);
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
196 }
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
197 };
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
198
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
199 template<typename Enum>
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
200 EnumIterShell<Enum> iterateEnum()
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
201 {
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
202 return EnumIterShell<Enum>();
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
203 }
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
204
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
205 // Is a value inside an enum?
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
206 template<typename Enum>
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
207 bool valueInEnum(typename std::underlying_type<Enum>::type x)
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
208 {
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
209 return x >= EnumLimits<Enum>::First and x <= EnumLimits<Enum>::Last;
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
210 }
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
211
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
212 double getRadialPoint(int segment, int divisions, double(*func)(double));
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
213 QVector<QLineF> makeCircle(int segments, int divisions, double radius);

mercurial