Sat, 29 Aug 2015 16:30:56 +0300
Closed old branch
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 |
927
409b82a4765e
- bump copyright year
Teemu Piippo <crimsondusk64@gmail.com>
parents:
920
diff
changeset
|
3 | * Copyright (C) 2013 - 2015 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> |
941 | 24 | #include <QVector> |
733
cc39df788660
- reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents:
655
diff
changeset
|
25 | #include <QVector3D> |
768
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
26 | #include <QSharedPointer> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | #include "macros.h" |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | class LDObject; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | class QFile; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | class QTextStream; |
768
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
32 | 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
|
33 | class LDDocument; |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | using int8 = qint8; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | using int16 = qint16; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | using int32 = qint32; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | using int64 = qint64; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | using uint8 = quint8; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | using uint16 = quint16; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | using uint32 = quint32; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | using uint64 = quint64; |
768
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
43 | using LDObjectPtr = QSharedPointer<LDObject>; |
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
44 | using LDObjectList = QList<LDObjectPtr>; |
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
45 | using LDObjectWeakPtr = QWeakPointer<LDObject>; |
29e6c5552388
- initial overhaul with smart pointers
Santeri Piippo <crimsondusk64@gmail.com>
parents:
739
diff
changeset
|
46 | 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
|
47 | 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
|
48 | using LDDocumentWeakPtr = QWeakPointer<LDDocument>; |
941 | 49 | using real = double; |
655
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 | template<typename T, typename R> |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | using Pair = std::pair<T, R>; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | |
794 | 54 | // |
55 | // A mathematical 3 x 3 matrix | |
56 | // | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | class Matrix |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | { |
794 | 59 | public: |
60 | Matrix() {} | |
61 | Matrix (const std::initializer_list<double>& vals); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | |
794 | 63 | // Constructs a matrix all 9 elements initialized to the same value. |
64 | Matrix (double fillval); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | |
794 | 66 | // Constructs a matrix with a C-array. |
67 | // note: @vals is expected to have exactly 9 elements. | |
68 | Matrix (double vals[]); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | |
794 | 70 | // Calculates the matrix's determinant. |
71 | double getDeterminant() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
794 | 73 | // Multiplies this matrix with @other |
74 | // note: a.mult(b) is not equivalent to b.mult(a)! | |
75 | Matrix mult (const Matrix& other) const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | |
794 | 77 | // Prints the matrix to stdout. |
78 | void dump() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | |
794 | 80 | // Yields a string representation of the matrix. |
81 | QString toString() const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | |
794 | 83 | // Zeroes the matrix out. |
84 | void zero(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | |
794 | 86 | // Assigns the matrix values to the values of @other. |
87 | Matrix& operator= (const Matrix& other); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | |
794 | 89 | // Returns a mutable reference to a value by @idx |
90 | inline double& value (int idx) | |
91 | { | |
92 | return m_vals[idx]; | |
93 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | |
794 | 95 | // An overload of value() for const matrices. |
96 | inline const double& value (int idx) const | |
97 | { | |
98 | return m_vals[idx]; | |
99 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | |
794 | 101 | // An operator overload for mult(). |
102 | inline Matrix operator* (const Matrix& other) const | |
103 | { | |
104 | return mult (other); | |
105 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
794 | 107 | // An operator overload for value(). |
108 | inline double& operator[] (int idx) | |
109 | { | |
110 | return value (idx); | |
111 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | |
794 | 113 | // An operator overload for value() const. |
114 | inline const double& operator[] (int idx) const | |
115 | { | |
116 | return value (idx); | |
117 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | |
794 | 119 | // Checks whether the two matrices have the same values. |
120 | bool operator== (const Matrix& other) const; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | |
794 | 122 | private: |
123 | double m_vals[9]; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | }; |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
126 | extern const Matrix IdentityMatrix; |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
128 | static const double Pi = 3.14159265358979323846; |
816 | 129 | |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
130 | template<typename T> |
941 | 131 | T clamp (T a, T min, T max) |
816 | 132 | { |
133 | return (a > max) ? max : (a < min) ? min : a; | |
134 | } | |
135 | ||
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
136 | template<typename T> |
941 | 137 | T min (T a, T b) |
816 | 138 | { |
139 | return (a < b) ? a : b; | |
140 | } | |
141 | ||
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
142 | template<typename T> |
941 | 143 | T max (T a, T b) |
816 | 144 | { |
145 | return (a > b) ? a : b; | |
146 | } | |
147 | ||
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
148 | template<typename T> |
941 | 149 | T abs (T a) |
816 | 150 | { |
151 | return (a >= 0) ? a : -a; | |
152 | } | |
153 | ||
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
154 | template<typename T> |
941 | 155 | bool isZero (T a) |
816 | 156 | { |
941 | 157 | return abs<T> (a) < 0.00001; |
816 | 158 | } |
159 | ||
818
748ba8818af8
- added the magic wand tool
Santeri Piippo <crimsondusk64@gmail.com>
parents:
816
diff
changeset
|
160 | template<typename T> |
941 | 161 | const char* plural (T n) |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
162 | { |
941 | 163 | return (not isZero (n - 1)) ? "s" : ""; |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
164 | } |
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
165 | |
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
166 | template<typename T> |
941 | 167 | bool isInteger (T a) |
168 | { | |
169 | return (abs (a - floor(a)) < 0.00001) or (abs (a - ceil(a)) < 0.00001); | |
170 | } | |
171 | ||
172 | template<typename T> | |
173 | bool isInRange (T a, T b, T c) | |
847 | 174 | { |
175 | return a >= b and a <= c; | |
176 | } | |
177 | ||
178 | template<typename T> | |
941 | 179 | void removeDuplicates (T& a) |
816 | 180 | { |
181 | std::sort (a.begin(), a.end()); | |
182 | a.erase (std::unique (a.begin(), a.end()), a.end()); | |
183 | } | |
184 | ||
185 | // | |
186 | // Returns true if first arg is equal to any of the other args | |
187 | // | |
188 | template<typename T, typename Arg, typename... Args> | |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
189 | bool Eq (T const& a, Arg const& arg, Args const&... args) |
816 | 190 | { |
191 | if (a == arg) | |
192 | return true; | |
193 | ||
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
194 | return Eq (a, args...); |
816 | 195 | } |
196 | ||
197 | template<typename T> | |
861
83426c5fa732
- major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents:
847
diff
changeset
|
198 | bool Eq (T const&) |
816 | 199 | { |
200 | return false; | |
201 | } |