src/vertex.cpp

changeset 33
4c41bfe2ec6e
parent 32
767592024ec5
child 34
1de2b8d64e9f
equal deleted inserted replaced
32:767592024ec5 33:4c41bfe2ec6e
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2020 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 Point3D::CoordinateType& Point3D::get(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 Point3D::CoordinateType Point3D::get(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;
61 }
62 }
63
64 Point3D::operator QVariant() const
65 {
66 return QVariant::fromValue(*this);
67 }
68
69 void Point3D::assign(Axis axis, CoordinateType value)
70 {
71 this->get(axis) = value;
72 }
73
74 Point3D VertexFromVector(const QVector3D& vector)
75 {
76 return {vector.x(), vector.y(), vector.z()};
77 }
78
79 Point3D operator*(const Point3D& point, Point3D::CoordinateType scalar)
80 {
81 return {point.x * scalar, point.y * scalar, point.z * scalar};
82 }
83
84 Point3D& operator+=(Point3D& point, const QVector3D& other)
85 {
86 point.x += other.x();
87 point.y += other.y();
88 point.z += other.z();
89 return point;
90 }
91
92 Point3D operator+(Point3D point, const QVector3D& other)
93 {
94 point += other;
95 return point;
96 }
97
98
99 QVector3D vertexToVector(const Point3D& vertex)
100 {
101 return {
102 static_cast<float>(vertex.x),
103 static_cast<float>(vertex.y),
104 static_cast<float>(vertex.z)
105 };
106 }
107
108 Point3D operator-(Point3D point, const QVector3D& vector)
109 {
110 point -= vector;
111 return point;
112 }
113
114 Point3D& operator-=(Point3D& point, const QVector3D& vector)
115 {
116 point.x -= vector.x();
117 point.y -= vector.y();
118 point.z -= vector.z();
119 return point;
120 }
121
122 QVector3D operator-(const Point3D& point, const Point3D& other)
123 {
124 return {
125 static_cast<float>(point.x - other.x),
126 static_cast<float>(point.y - other.y),
127 static_cast<float>(point.z - other.z)
128 };
129 }
130
131 Point3D& operator*=(Point3D& point, Point3D::CoordinateType scalar)
132 {
133 point.x *= scalar;
134 point.y *= scalar;
135 point.z *= scalar;
136 return point;
137 }
138
139 bool operator==(const Point3D& point, const Point3D& other)
140 {
141 return point.x == other.x and point.y == other.y and point.z == other.z;
142 }
143
144 bool operator!=(const Point3D& point, const Point3D& other)
145 {
146 return not (point == other);
147 }
148
149 bool operator<(const Point3D& point, const Point3D& other)
150 {
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;
155 else
156 return point.z < other.z;
157 }
158
159 /*
160 * Transforms the specified vertex with a transformation matrix
161 */
162 Point3D math::transform(const Point3D& point, const Matrix4x4& matrix)
163 {
164 return {
165 matrix(0, 0) * point.x
166 + matrix(0, 1) * point.y
167 + matrix(0, 2) * point.z
168 + matrix(0, 3),
169 matrix(1, 0) * point.x
170 + matrix(1, 1) * point.y
171 + matrix(1, 2) * point.z
172 + matrix(1, 3),
173 matrix(2, 0) * point.x
174 + matrix(2, 1) * point.y
175 + matrix(2, 2) * point.z
176 + matrix(2, 3),
177 };
178 }
179
180 /*
181 * Returns the distance from one vertex to another.
182 */
183 qreal math::distance(const Point3D& one, const Point3D& other)
184 {
185 return (one - other).length();
186 }
187
188 /*
189 * Returns a vertex with all coordinates inverted.
190 */
191 Point3D operator-(const Point3D& vertex)
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 */
200 QDataStream& operator<<(QDataStream& out, const Point3D& vertex)
201 {
202 return out << vertex.x << vertex.y << vertex.z;
203 }
204
205 /*
206 * Takes a vertex from a data stream.
207 */
208 QDataStream& operator>>(QDataStream& in, Point3D& vertex)
209 {
210 return in >> vertex.x >> vertex.y >> vertex.z;
211 }
212
213 unsigned int qHash(const Point3D& key)
214 {
215 return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z));
216 }
217
218 QString vertexToStringParens(const Point3D& vertex)
219 {
220 return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z);
221 }

mercurial