src/vertex.cpp

Tue, 07 Jul 2015 21:35:20 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 07 Jul 2015 21:35:20 +0300
changeset 941
f895379d7fab
permissions
-rw-r--r--

Refactoring update.
Removed all asserts.

941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 - 2015 Teemu Piippo
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include "vertex.h"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "format.h"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 Vertex::Vertex() :
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 QVector3D() {}
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 Vertex::Vertex (const QVector3D& a) :
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 QVector3D (a) {}
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 Vertex::Vertex (qreal xpos, qreal ypos, qreal zpos) :
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 QVector3D(xpos, ypos, zpos) {}
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 void Vertex::transform (const Matrix& matr, const Vertex& pos)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos.x();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos.y();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos.z();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 setX (x2);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 setY (y2);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 setZ (z2);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 void Vertex::apply (ApplyFunction func)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 double newX = x(), newY = y(), newZ = z();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 func (X, newX);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 func (Y, newY);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 func (Z, newZ);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 *this = Vertex (newX, newY, newZ);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 void Vertex::apply (ApplyConstFunction func) const
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 func (X, x());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 func (Y, y());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 func (Z, z());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 double Vertex::operator[] (Axis ax) const
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 switch (ax)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 case X: return x();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 case Y: return y();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 case Z: return z();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 return 0.0;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 void Vertex::setCoordinate (Axis ax, qreal value)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 switch (ax)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 case X: setX (value); break;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 case Y: setY (value); break;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 case Z: setZ (value); break;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 QString Vertex::toString (bool mangled) const
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 if (mangled)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 return format ("(%1, %2, %3)", x(), y(), z());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 return format ("%1 %2 %3", x(), y(), z());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 bool Vertex::operator< (const Vertex& other) const
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 if (x() != other.x()) return x() < other.x();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 if (y() != other.y()) return y() < other.y();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 if (z() != other.z()) return z() < other.z();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 return false;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 }

mercurial