src/vertex.cpp

changeset 3
55a55a9ec2c2
child 5
593a658cba8e
equal deleted inserted replaced
2:2bdc3ac5e77c 3:55a55a9ec2c2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright(C) 2013 - 2018 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 "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
34 double& Vertex::operator[](Axis axis)
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
49 double Vertex::operator[](Axis axis) const
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:
60 return 0.0;
61 }
62 }
63
64 void Vertex::setCoordinate(Axis axis, qreal value)
65 {
66 (*this)[axis] = value;
67 }
68
69 Vertex VertexFromVector(const QVector3D& vector)
70 {
71 return {vector.x(), vector.y(), vector.z()};
72 }
73
74 Vertex Vertex::operator*(qreal scalar) const
75 {
76 return {this->x * scalar, this->y * scalar, this->z * scalar};
77 }
78
79 Vertex& Vertex::operator+=(const QVector3D& other)
80 {
81 this->x += other.x();
82 this->y += other.y();
83 this->z += other.z();
84 return *this;
85 }
86
87 Vertex Vertex::operator+(const QVector3D& other) const
88 {
89 Vertex result(*this);
90 result += other;
91 return result;
92 }
93
94
95 QVector3D vertexToVector(const Vertex& vertex)
96 {
97 return {
98 static_cast<float>(vertex.x),
99 static_cast<float>(vertex.y),
100 static_cast<float>(vertex.z)
101 };
102 }
103
104 Vertex Vertex::operator-(const QVector3D& vector) const
105 {
106 Vertex result = *this;
107 result -= vector;
108 return result;
109 }
110
111 Vertex& Vertex::operator-=(const QVector3D& vector)
112 {
113 this->x -= vector.x();
114 this->y -= vector.y();
115 this->z -= vector.z();
116 return *this;
117 }
118
119 QVector3D Vertex::operator-(const Vertex& other) const
120 {
121 return {
122 static_cast<float>(this->x - other.x),
123 static_cast<float>(this->y - other.y),
124 static_cast<float>(this->z - other.z)
125 };
126 }
127
128 Vertex& Vertex::operator*=(qreal scalar)
129 {
130 x *= scalar;
131 y *= scalar;
132 z *= scalar;
133 return *this;
134 }
135
136 bool Vertex::operator==(const Vertex& other) const
137 {
138 return this->x == other.x and this->y == other.y and this->z == other.z;
139 }
140
141 bool Vertex::operator!=(const Vertex& other) const
142 {
143 return not(*this == other);
144 }
145
146 Vertex::operator QVariant() const
147 {
148 return QVariant::fromValue<Vertex>(*this);
149 }
150
151 bool Vertex::operator<(const Vertex& other) const
152 {
153 if (not qFuzzyCompare(this->x, other.x))
154 return this->x < other.x;
155 else if (not qFuzzyCompare(this->y, other.y))
156 return this->y < other.y;
157 else
158 return this->z < other.z;
159 }
160
161 /*
162 * Transforms this vertex with a tranformation matrix and returns the result.
163 */
164 Vertex Vertex::transformed(const GLRotationMatrix& matrix) const
165 {
166 return {
167 matrix(0, 0) * this->x
168 + matrix(0, 1) * this->y
169 + matrix(0, 2) * this->z,
170 matrix(1, 0) * this->x
171 + matrix(1, 1) * this->y
172 + matrix(1, 2) * this->z,
173 matrix(2, 0) * this->x
174 + matrix(2, 1) * this->y
175 + matrix(2, 2) * this->z,
176 };
177 }
178
179 /*
180 * Returns the distance from one vertex to another.
181 */
182 qreal distance(const Vertex& one, const Vertex& other)
183 {
184 return (one - other).length();
185 }
186
187 /*
188 * Returns a vertex with all coordinates inverted.
189 */
190 Vertex operator-(const Vertex& vertex)
191 {
192 return {-vertex.x, -vertex.y, -vertex.z};
193 }
194
195 /*
196 * Inserts this vertex into a data stream. This is needed for vertices to be
197 * stored in QSettings.
198 */
199 QDataStream& operator<<(QDataStream& out, const Vertex& vertex)
200 {
201 return out << vertex.x << vertex.y << vertex.z;
202 }
203
204 /*
205 * Takes a vertex from a data stream.
206 */
207 QDataStream& operator>>(QDataStream& in, Vertex& vertex)
208 {
209 return in >> vertex.x >> vertex.y >> vertex.z;
210 }
211
212 unsigned int qHash(const Vertex& key)
213 {
214 return qHash(key.x) ^ rotl10(qHash(key.y)) ^ rotl20(qHash(key.z));
215 }

mercurial