|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright(C) 2013 - 2017 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 void Vertex::transform(const Matrix& matrix, const Vertex& pos) |
|
23 { |
|
24 double x2 = (matrix(0, 0) * x) + (matrix(0, 1) * y) + (matrix(0, 2) * z) + pos.x; |
|
25 double y2 = (matrix(1, 0) * x) + (matrix(1, 1) * y) + (matrix(1, 2) * z) + pos.y; |
|
26 double z2 = (matrix(2, 0) * x) + (matrix(2, 1) * y) + (matrix(2, 2) * z) + pos.z; |
|
27 this->x = x2; |
|
28 this->y = y2; |
|
29 this->z = z2; |
|
30 } |
|
31 |
|
32 void Vertex::apply(ApplyFunction func) |
|
33 { |
|
34 func(X, this->x); |
|
35 func(Y, this->y); |
|
36 func(Z, this->z); |
|
37 } |
|
38 |
|
39 void Vertex::apply(ApplyConstFunction func) const |
|
40 { |
|
41 func(X, this->x); |
|
42 func(Y, this->y); |
|
43 func(Z, this->z); |
|
44 } |
|
45 |
|
46 double& Vertex::operator[](Axis axis) |
|
47 { |
|
48 switch (axis) |
|
49 { |
|
50 case X: |
|
51 return this->x; |
|
52 |
|
53 case Y: |
|
54 return this->y; |
|
55 |
|
56 case Z: |
|
57 return this->z; |
|
58 |
|
59 default: |
|
60 return ::sink<double>(); |
|
61 } |
|
62 } |
|
63 |
|
64 double Vertex::operator[](Axis axis) const |
|
65 { |
|
66 switch (axis) |
|
67 { |
|
68 case X: |
|
69 return this->x; |
|
70 |
|
71 case Y: |
|
72 return this->y; |
|
73 |
|
74 case Z: |
|
75 return this->z; |
|
76 |
|
77 default: |
|
78 return 0.0; |
|
79 } |
|
80 } |
|
81 |
|
82 void Vertex::setCoordinate(Axis axis, qreal value) |
|
83 { |
|
84 (*this)[axis] = value; |
|
85 } |
|
86 |
|
87 QString Vertex::toString(bool mangled) const |
|
88 { |
|
89 if (mangled) |
|
90 return ::format("(%1, %2, %3)", this->x, this->y, this->z); |
|
91 else |
|
92 return ::format("%1 %2 %3", this->x, this->y, this->z); |
|
93 } |
|
94 |
|
95 Vertex Vertex::operator*(qreal scalar) const |
|
96 { |
|
97 return {this->x * scalar, this->y * scalar, this->z * scalar}; |
|
98 } |
|
99 |
|
100 Vertex& Vertex::operator+= (const QVector3D& other) |
|
101 { |
|
102 this->x += other.x(); |
|
103 this->y += other.y(); |
|
104 this->z += other.z(); |
|
105 return *this; |
|
106 } |
|
107 |
|
108 Vertex Vertex::operator+ (const QVector3D& other) const |
|
109 { |
|
110 Vertex result(*this); |
|
111 result += other; |
|
112 return result; |
|
113 } |
|
114 |
|
115 QVector3D Vertex::toVector() const |
|
116 { |
|
117 return { |
|
118 static_cast<float>(this->x), |
|
119 static_cast<float>(this->y), |
|
120 static_cast<float>(this->z) |
|
121 }; |
|
122 } |
|
123 |
|
124 Vertex Vertex::operator-(const QVector3D& vector) const |
|
125 { |
|
126 Vertex result = *this; |
|
127 result -= vector; |
|
128 return result; |
|
129 } |
|
130 |
|
131 Vertex& Vertex::operator-= (const QVector3D& vector) |
|
132 { |
|
133 this->x -= vector.x(); |
|
134 this->y -= vector.y(); |
|
135 this->z -= vector.z(); |
|
136 return *this; |
|
137 } |
|
138 |
|
139 QVector3D Vertex::operator-(const Vertex& other) const |
|
140 { |
|
141 return { |
|
142 static_cast<float>(this->x - other.x), |
|
143 static_cast<float>(this->y - other.y), |
|
144 static_cast<float>(this->z - other.z) |
|
145 }; |
|
146 } |
|
147 |
|
148 Vertex& Vertex::operator*= (qreal scalar) |
|
149 { |
|
150 x *= scalar; |
|
151 y *= scalar; |
|
152 z *= scalar; |
|
153 return *this; |
|
154 } |
|
155 |
|
156 bool Vertex::operator== (const Vertex& other) const |
|
157 { |
|
158 return this->x == other.x and this->y == other.y and this->z == other.z; |
|
159 } |
|
160 |
|
161 bool Vertex::operator!= (const Vertex& other) const |
|
162 { |
|
163 return not(*this == other); |
|
164 } |
|
165 |
|
166 bool Vertex::operator<(const Vertex& other) const |
|
167 { |
|
168 if (not qFuzzyCompare(this->x, other.x)) |
|
169 return this->x < other.x; |
|
170 else if (not qFuzzyCompare(this->y, other.y)) |
|
171 return this->y < other.y; |
|
172 else |
|
173 return this->z < other.z; |
|
174 } |
|
175 |
|
176 /* |
|
177 * Transforms this vertex with a tranformation matrix and returns the result. |
|
178 */ |
|
179 Vertex Vertex::transformed(const GLRotationMatrix& matrix) const |
|
180 { |
|
181 return { |
|
182 matrix(0, 0) * this->x |
|
183 + matrix(0, 1) * this->y |
|
184 + matrix(0, 2) * this->z, |
|
185 matrix(1, 0) * this->x |
|
186 + matrix(1, 1) * this->y |
|
187 + matrix(1, 2) * this->z, |
|
188 matrix(2, 0) * this->x |
|
189 + matrix(2, 1) * this->y |
|
190 + matrix(2, 2) * this->z, |
|
191 }; |
|
192 } |
|
193 |
|
194 QDataStream& operator<<(QDataStream& out, const Vertex& vertex) |
|
195 { |
|
196 return out << vertex.x << vertex.y << vertex.z; |
|
197 } |
|
198 |
|
199 QDataStream& operator>>(QDataStream& in, Vertex& vertex) |
|
200 { |
|
201 return in >> vertex.x >> vertex.y >> vertex.z; |
|
202 } |