Tue, 07 Jul 2015 21:35:20 +0300
Refactoring update.
Removed all asserts.
941 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2015 Teemu Piippo | |
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 | |
8 | * (at your option) any later version. | |
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 "vertex.h" | |
20 | #include "format.h" | |
21 | ||
22 | Vertex::Vertex() : | |
23 | QVector3D() {} | |
24 | ||
25 | Vertex::Vertex (const QVector3D& a) : | |
26 | QVector3D (a) {} | |
27 | ||
28 | Vertex::Vertex (qreal xpos, qreal ypos, qreal zpos) : | |
29 | QVector3D(xpos, ypos, zpos) {} | |
30 | ||
31 | ||
32 | void Vertex::transform (const Matrix& matr, const Vertex& pos) | |
33 | { | |
34 | double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos.x(); | |
35 | double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos.y(); | |
36 | double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos.z(); | |
37 | setX (x2); | |
38 | setY (y2); | |
39 | setZ (z2); | |
40 | } | |
41 | ||
42 | void Vertex::apply (ApplyFunction func) | |
43 | { | |
44 | double newX = x(), newY = y(), newZ = z(); | |
45 | func (X, newX); | |
46 | func (Y, newY); | |
47 | func (Z, newZ); | |
48 | *this = Vertex (newX, newY, newZ); | |
49 | } | |
50 | ||
51 | void Vertex::apply (ApplyConstFunction func) const | |
52 | { | |
53 | func (X, x()); | |
54 | func (Y, y()); | |
55 | func (Z, z()); | |
56 | } | |
57 | ||
58 | double Vertex::operator[] (Axis ax) const | |
59 | { | |
60 | switch (ax) | |
61 | { | |
62 | case X: return x(); | |
63 | case Y: return y(); | |
64 | case Z: return z(); | |
65 | } | |
66 | ||
67 | return 0.0; | |
68 | } | |
69 | ||
70 | void Vertex::setCoordinate (Axis ax, qreal value) | |
71 | { | |
72 | switch (ax) | |
73 | { | |
74 | case X: setX (value); break; | |
75 | case Y: setY (value); break; | |
76 | case Z: setZ (value); break; | |
77 | } | |
78 | } | |
79 | ||
80 | QString Vertex::toString (bool mangled) const | |
81 | { | |
82 | if (mangled) | |
83 | return format ("(%1, %2, %3)", x(), y(), z()); | |
84 | ||
85 | return format ("%1 %2 %3", x(), y(), z()); | |
86 | } | |
87 | ||
88 | bool Vertex::operator< (const Vertex& other) const | |
89 | { | |
90 | if (x() != other.x()) return x() < other.x(); | |
91 | if (y() != other.y()) return y() < other.y(); | |
92 | if (z() != other.z()) return z() < other.z(); | |
93 | return false; | |
94 | } |