Wed, 22 Jan 2020 01:17:11 +0200
commit work done on plugging vao to the gl renderer, renders nonsense for now
3 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
24 | 3 | * Copyright (C) 2013 - 2020 Teemu Piippo |
3 | 4 | * |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
24 | 8 | * (at your option) any later version. |
3 | 9 | * |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include "main.h" | |
20 | #include "vertex.h" | |
21 | ||
22 | /* | |
23 | void Vertex::transform(const Matrix& matrix, const Vertex& pos) | |
24 | { | |
25 | double x2 = (matrix(0, 0) * x) + (matrix(0, 1) * y) + (matrix(0, 2) * z) + pos.x; | |
26 | double y2 = (matrix(1, 0) * x) + (matrix(1, 1) * y) + (matrix(1, 2) * z) + pos.y; | |
27 | double z2 = (matrix(2, 0) * x) + (matrix(2, 1) * y) + (matrix(2, 2) * z) + pos.z; | |
28 | this->x = x2; | |
29 | this->y = y2; | |
30 | this->z = z2; | |
31 | } | |
32 | */ | |
33 | ||
18 | 34 | Point3D::CoordinateType& Point3D::get(Axis axis) |
3 | 35 | { |
36 | switch (axis) | |
37 | { | |
38 | case X: | |
39 | return this->x; | |
40 | case Y: | |
41 | return this->y; | |
42 | case Z: | |
43 | return this->z; | |
44 | default: | |
45 | throw std::runtime_error("Non-axis given to Vertex::operator[]"); | |
46 | } | |
47 | } | |
48 | ||
18 | 49 | Point3D::CoordinateType Point3D::get(Axis axis) const |
3 | 50 | { |
51 | switch (axis) | |
52 | { | |
53 | case X: | |
54 | return this->x; | |
55 | case Y: | |
56 | return this->y; | |
57 | case Z: | |
58 | return this->z; | |
59 | default: | |
5 | 60 | return 0; |
3 | 61 | } |
62 | } | |
63 | ||
20 | 64 | Point3D::operator QVariant() const |
65 | { | |
66 | return QVariant::fromValue(*this); | |
67 | } | |
68 | ||
18 | 69 | void Point3D::assign(Axis axis, CoordinateType value) |
3 | 70 | { |
18 | 71 | this->get(axis) = value; |
3 | 72 | } |
73 | ||
18 | 74 | Point3D VertexFromVector(const QVector3D& vector) |
3 | 75 | { |
76 | return {vector.x(), vector.y(), vector.z()}; | |
77 | } | |
78 | ||
18 | 79 | Point3D operator*(const Point3D& point, Point3D::CoordinateType scalar) |
3 | 80 | { |
18 | 81 | return {point.x * scalar, point.y * scalar, point.z * scalar}; |
3 | 82 | } |
83 | ||
18 | 84 | Point3D& operator+=(Point3D& point, const QVector3D& other) |
3 | 85 | { |
18 | 86 | point.x += other.x(); |
87 | point.y += other.y(); | |
88 | point.z += other.z(); | |
89 | return point; | |
3 | 90 | } |
91 | ||
18 | 92 | Point3D operator+(Point3D point, const QVector3D& other) |
3 | 93 | { |
18 | 94 | point += other; |
95 | return point; | |
3 | 96 | } |
97 | ||
98 | ||
18 | 99 | QVector3D vertexToVector(const Point3D& vertex) |
3 | 100 | { |
101 | return { | |
102 | static_cast<float>(vertex.x), | |
103 | static_cast<float>(vertex.y), | |
104 | static_cast<float>(vertex.z) | |
105 | }; | |
106 | } | |
107 | ||
18 | 108 | Point3D operator-(Point3D point, const QVector3D& vector) |
3 | 109 | { |
18 | 110 | point -= vector; |
111 | return point; | |
3 | 112 | } |
113 | ||
18 | 114 | Point3D& operator-=(Point3D& point, const QVector3D& vector) |
3 | 115 | { |
18 | 116 | point.x -= vector.x(); |
117 | point.y -= vector.y(); | |
118 | point.z -= vector.z(); | |
119 | return point; | |
3 | 120 | } |
121 | ||
18 | 122 | QVector3D operator-(const Point3D& point, const Point3D& other) |
3 | 123 | { |
124 | return { | |
18 | 125 | static_cast<float>(point.x - other.x), |
126 | static_cast<float>(point.y - other.y), | |
127 | static_cast<float>(point.z - other.z) | |
3 | 128 | }; |
129 | } | |
130 | ||
18 | 131 | Point3D& operator*=(Point3D& point, Point3D::CoordinateType scalar) |
3 | 132 | { |
18 | 133 | point.x *= scalar; |
134 | point.y *= scalar; | |
135 | point.z *= scalar; | |
136 | return point; | |
3 | 137 | } |
138 | ||
18 | 139 | bool operator==(const Point3D& point, const Point3D& other) |
3 | 140 | { |
18 | 141 | return point.x == other.x and point.y == other.y and point.z == other.z; |
3 | 142 | } |
143 | ||
18 | 144 | bool operator!=(const Point3D& point, const Point3D& other) |
3 | 145 | { |
18 | 146 | return not (point == other); |
3 | 147 | } |
148 | ||
18 | 149 | bool operator<(const Point3D& point, const Point3D& other) |
3 | 150 | { |
18 | 151 | if (not qFuzzyCompare(point.x, other.x)) |
152 | return point.x < other.x; | |
153 | else if (not qFuzzyCompare(point.y, other.y)) | |
154 | return point.y < other.y; | |
3 | 155 | else |
18 | 156 | return point.z < other.z; |
3 | 157 | } |
158 | ||
159 | /* | |
21 | 160 | * Transforms the specified vertex with a transformation matrix |
3 | 161 | */ |
21 | 162 | Point3D math::transform(const Point3D& point, const Matrix4x4& matrix) |
3 | 163 | { |
164 | return { | |
18 | 165 | matrix(0, 0) * point.x |
166 | + matrix(0, 1) * point.y | |
21 | 167 | + matrix(0, 2) * point.z |
168 | + matrix(0, 3), | |
18 | 169 | matrix(1, 0) * point.x |
170 | + matrix(1, 1) * point.y | |
21 | 171 | + matrix(1, 2) * point.z |
172 | + matrix(1, 3), | |
18 | 173 | matrix(2, 0) * point.x |
174 | + matrix(2, 1) * point.y | |
21 | 175 | + matrix(2, 2) * point.z |
176 | + matrix(2, 3), | |
3 | 177 | }; |
178 | } | |
179 | ||
180 | /* | |
181 | * Returns the distance from one vertex to another. | |
182 | */ | |
18 | 183 | qreal math::distance(const Point3D& one, const Point3D& other) |
3 | 184 | { |
185 | return (one - other).length(); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Returns a vertex with all coordinates inverted. | |
190 | */ | |
18 | 191 | Point3D operator-(const Point3D& vertex) |
3 | 192 | { |
193 | return {-vertex.x, -vertex.y, -vertex.z}; | |
194 | } | |
195 | ||
196 | /* | |
197 | * Inserts this vertex into a data stream. This is needed for vertices to be | |
198 | * stored in QSettings. | |
199 | */ | |
18 | 200 | QDataStream& operator<<(QDataStream& out, const Point3D& vertex) |
3 | 201 | { |
202 | return out << vertex.x << vertex.y << vertex.z; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Takes a vertex from a data stream. | |
207 | */ | |
18 | 208 | QDataStream& operator>>(QDataStream& in, Point3D& vertex) |
3 | 209 | { |
210 | return in >> vertex.x >> vertex.y >> vertex.z; | |
211 | } | |
212 | ||
18 | 213 | unsigned int qHash(const Point3D& key) |
3 | 214 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
215 | return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z)); |
3 | 216 | } |
6 | 217 | |
18 | 218 | QString vertexToStringParens(const Point3D& vertex) |
6 | 219 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
220 | return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z); |
6 | 221 | } |