18 |
18 |
19 #include "miscallenous.h" |
19 #include "miscallenous.h" |
20 #include "linetypes/modelobject.h" |
20 #include "linetypes/modelobject.h" |
21 #include "lddocument.h" |
21 #include "lddocument.h" |
22 |
22 |
23 Vertex::Vertex() : |
23 void Vertex::transform(const Matrix& matrix, const Vertex& pos) |
24 QVector3D() {} |
24 { |
25 |
25 double x2 = (matrix(0, 0) * x) + (matrix(0, 1) * y) + (matrix(0, 2) * z) + pos.x; |
26 Vertex::Vertex (const QVector3D& a) : |
26 double y2 = (matrix(1, 0) * x) + (matrix(1, 1) * y) + (matrix(1, 2) * z) + pos.y; |
27 QVector3D (a) {} |
27 double z2 = (matrix(2, 0) * x) + (matrix(2, 1) * y) + (matrix(2, 2) * z) + pos.z; |
28 |
28 this->x = x2; |
29 Vertex::Vertex (qreal xpos, qreal ypos, qreal zpos) : |
29 this->y = y2; |
30 QVector3D(xpos, ypos, zpos) {} |
30 this->z = z2; |
31 |
31 } |
32 |
32 |
33 void Vertex::transform (const Matrix& matrix, const Vertex& pos) |
33 void Vertex::apply(ApplyFunction func) |
34 { |
34 { |
35 double x2 = (matrix(0, 0) * x()) + (matrix(0, 1) * y()) + (matrix(0, 2) * z()) + pos.x(); |
35 func(X, this->x); |
36 double y2 = (matrix(1, 0) * x()) + (matrix(1, 1) * y()) + (matrix(1, 2) * z()) + pos.y(); |
36 func(Y, this->y); |
37 double z2 = (matrix(2, 0) * x()) + (matrix(2, 1) * y()) + (matrix(2, 2) * z()) + pos.z(); |
37 func(Z, this->z); |
38 setX (x2); |
38 } |
39 setY (y2); |
39 |
40 setZ (z2); |
40 void Vertex::apply(ApplyConstFunction func) const |
41 } |
41 { |
42 |
42 func(X, this->x); |
43 void Vertex::apply (ApplyFunction func) |
43 func(Y, this->y); |
44 { |
44 func(Z, this->z); |
45 double newX = x(), newY = y(), newZ = z(); |
45 } |
46 func (X, newX); |
46 |
47 func (Y, newY); |
47 double& Vertex::operator[](Axis axis) |
48 func (Z, newZ); |
48 { |
49 *this = Vertex (newX, newY, newZ); |
49 switch (axis) |
50 } |
50 { |
51 |
51 case X: |
52 void Vertex::apply (ApplyConstFunction func) const |
52 return this->x; |
53 { |
53 |
54 func (X, x()); |
54 case Y: |
55 func (Y, y()); |
55 return this->y; |
56 func (Z, z()); |
56 |
57 } |
57 case Z: |
58 |
58 return this->z; |
59 double Vertex::operator[] (Axis ax) const |
59 |
60 { |
60 default: |
61 switch (ax) |
61 return ::sink<double>(); |
62 { |
62 } |
63 case X: return x(); |
63 } |
64 case Y: return y(); |
64 |
65 case Z: return z(); |
65 double Vertex::operator[] (Axis axis) const |
66 } |
66 { |
67 |
67 switch (axis) |
68 return 0.0; |
68 { |
69 } |
69 case X: |
70 |
70 return this->x; |
71 void Vertex::setCoordinate (Axis ax, qreal value) |
71 |
72 { |
72 case Y: |
73 switch (ax) |
73 return this->y; |
74 { |
74 |
75 case X: setX (value); break; |
75 case Z: |
76 case Y: setY (value); break; |
76 return this->z; |
77 case Z: setZ (value); break; |
77 |
78 } |
78 default: |
|
79 return 0.0; |
|
80 } |
|
81 } |
|
82 |
|
83 void Vertex::setCoordinate (Axis axis, qreal value) |
|
84 { |
|
85 (*this)[axis] = value; |
79 } |
86 } |
80 |
87 |
81 QString Vertex::toString (bool mangled) const |
88 QString Vertex::toString (bool mangled) const |
82 { |
89 { |
83 if (mangled) |
90 if (mangled) |
84 return format ("(%1, %2, %3)", x(), y(), z()); |
91 return ::format("(%1, %2, %3)", this->x, this->y, this->z); |
85 |
92 else |
86 return format ("%1 %2 %3", x(), y(), z()); |
93 return ::format("%1 %2 %3", this->x, this->y, this->z); |
87 } |
94 } |
88 |
95 |
89 Vertex Vertex::operator* (qreal scalar) const |
96 Vertex Vertex::operator* (qreal scalar) const |
90 { |
97 { |
91 return Vertex (x() * scalar, y() * scalar, z() * scalar); |
98 return {this->x * scalar, this->y * scalar, this->z * scalar}; |
92 } |
99 } |
93 |
100 |
94 Vertex& Vertex::operator+= (const Vertex& other) |
101 Vertex& Vertex::operator+=(const QVector3D& other) |
95 { |
102 { |
96 setX (x() + other.x()); |
103 this->x += other.x(); |
97 setY (y() + other.y()); |
104 this->y += other.y(); |
98 setZ (z() + other.z()); |
105 this->z += other.z(); |
99 return *this; |
106 return *this; |
100 } |
107 } |
101 |
108 |
102 Vertex Vertex::operator+ (const Vertex& other) const |
109 Vertex Vertex::operator+(const QVector3D& other) const |
103 { |
110 { |
104 Vertex result (*this); |
111 Vertex result (*this); |
105 result += other; |
112 result += other; |
106 return result; |
113 return result; |
107 } |
114 } |
108 |
115 |
|
116 QVector3D Vertex::toVector() const |
|
117 { |
|
118 return { |
|
119 static_cast<float>(this->x), |
|
120 static_cast<float>(this->y), |
|
121 static_cast<float>(this->z) |
|
122 }; |
|
123 } |
|
124 |
|
125 Vertex Vertex::operator- (const QVector3D& vector) const |
|
126 { |
|
127 Vertex result = *this; |
|
128 result -= vector; |
|
129 return result; |
|
130 } |
|
131 |
|
132 Vertex& Vertex::operator-= (const QVector3D& vector) |
|
133 { |
|
134 this->x -= vector.x(); |
|
135 this->y -= vector.y(); |
|
136 this->z -= vector.z(); |
|
137 return *this; |
|
138 } |
|
139 |
|
140 QVector3D Vertex::operator- (const Vertex& other) const |
|
141 { |
|
142 return { |
|
143 static_cast<float>(this->x - other.x), |
|
144 static_cast<float>(this->y - other.y), |
|
145 static_cast<float>(this->z - other.z) |
|
146 }; |
|
147 } |
|
148 |
109 Vertex& Vertex::operator*= (qreal scalar) |
149 Vertex& Vertex::operator*= (qreal scalar) |
110 { |
150 { |
111 setX (x() * scalar); |
151 x *= scalar; |
112 setY (y() * scalar); |
152 y *= scalar; |
113 setZ (z() * scalar); |
153 z *= scalar; |
114 return *this; |
154 return *this; |
115 } |
155 } |
116 |
156 |
|
157 bool Vertex::operator==(const Vertex& other) const |
|
158 { |
|
159 return this->x == other.x and this->y == other.y and this->z == other.z; |
|
160 } |
|
161 |
|
162 bool Vertex::operator!=(const Vertex& other) const |
|
163 { |
|
164 return not (*this == other); |
|
165 } |
|
166 |
117 bool Vertex::operator< (const Vertex& other) const |
167 bool Vertex::operator< (const Vertex& other) const |
118 { |
168 { |
119 if (x() != other.x()) return x() < other.x(); |
169 if (not qFuzzyCompare(this->x, other.x)) |
120 if (y() != other.y()) return y() < other.y(); |
170 return this->x < other.x; |
121 if (z() != other.z()) return z() < other.z(); |
171 else if (not qFuzzyCompare(this->y, other.y)) |
122 return false; |
172 return this->y < other.y; |
|
173 else |
|
174 return this->z < other.z; |
123 } |
175 } |
124 |
176 |
125 /* |
177 /* |
126 * Transforms this vertex with a tranformation matrix and returns the result. |
178 * Transforms this vertex with a tranformation matrix and returns the result. |
127 */ |
179 */ |
128 Vertex Vertex::transformed(const GLRotationMatrix& matrix) const |
180 Vertex Vertex::transformed(const GLRotationMatrix& matrix) const |
129 { |
181 { |
130 return { |
182 return { |
131 matrix(0, 0) * x() + matrix(0, 1) * y() + matrix(0, 2) * z(), |
183 matrix(0, 0) * this->x |
132 matrix(1, 0) * x() + matrix(1, 1) * y() + matrix(1, 2) * z(), |
184 + matrix(0, 1) * this->y |
133 matrix(2, 0) * x() + matrix(2, 1) * y() + matrix(2, 2) * z(), |
185 + matrix(0, 2) * this->z, |
|
186 matrix(1, 0) * this->x |
|
187 + matrix(1, 1) * this->y |
|
188 + matrix(1, 2) * this->z, |
|
189 matrix(2, 0) * this->x |
|
190 + matrix(2, 1) * this->y |
|
191 + matrix(2, 2) * this->z, |
134 }; |
192 }; |
135 } |
193 } |
136 |
194 |
137 // ============================================================================= |
195 // ============================================================================= |
138 // |
196 // |
151 |
209 |
152 // ============================================================================= |
210 // ============================================================================= |
153 // |
211 // |
154 void BoundingBox::calcVertex (const Vertex& vertex) |
212 void BoundingBox::calcVertex (const Vertex& vertex) |
155 { |
213 { |
156 m_vertex0.setX (qMin (vertex.x(), m_vertex0.x())); |
214 m_vertex0.x = qMin(vertex.x, m_vertex0.x); |
157 m_vertex0.setY (qMin (vertex.y(), m_vertex0.y())); |
215 m_vertex0.y = qMin(vertex.y, m_vertex0.y); |
158 m_vertex0.setZ (qMin (vertex.z(), m_vertex0.z())); |
216 m_vertex0.z = qMin(vertex.z, m_vertex0.z); |
159 m_vertex1.setX (qMax (vertex.x(), m_vertex1.x())); |
217 m_vertex1.x = qMax(vertex.x, m_vertex1.x); |
160 m_vertex1.setY (qMax (vertex.y(), m_vertex1.y())); |
218 m_vertex1.y = qMax(vertex.y, m_vertex1.y); |
161 m_vertex1.setZ (qMax (vertex.z(), m_vertex1.z())); |
219 m_vertex1.z = qMax(vertex.z, m_vertex1.z); |
162 m_isEmpty = false; |
220 m_isEmpty = false; |
163 } |
221 } |
164 |
222 |
165 // ============================================================================= |
223 // ============================================================================= |
166 // |
224 // |
167 // Clears the bounding box |
225 // Clears the bounding box |
168 // |
226 // |
169 void BoundingBox::reset() |
227 void BoundingBox::reset() |
170 { |
228 { |
171 m_vertex0 = Vertex (10000.0, 10000.0, 10000.0); |
229 m_vertex0 = {10000.0, 10000.0, 10000.0}; |
172 m_vertex1 = Vertex (-10000.0, -10000.0, -10000.0); |
230 m_vertex1 = {-10000.0, -10000.0, -10000.0}; |
173 m_isEmpty = true; |
231 m_isEmpty = true; |
174 } |
232 } |
175 |
233 |
176 // ============================================================================= |
234 // ============================================================================= |
177 // |
235 // |
178 // Returns the length of the bounding box on the longest measure. |
236 // Returns the length of the bounding box on the longest measure. |
179 // |
237 // |
180 double BoundingBox::longestMeasurement() const |
238 double BoundingBox::longestMeasurement() const |
181 { |
239 { |
182 double xscale = (m_vertex0.x() - m_vertex1.x()); |
240 double xscale = m_vertex0.x - m_vertex1.x; |
183 double yscale = (m_vertex0.y() - m_vertex1.y()); |
241 double yscale = m_vertex0.y - m_vertex1.y; |
184 double zscale = (m_vertex0.z() - m_vertex1.z()); |
242 double zscale = m_vertex0.z - m_vertex1.z; |
185 double size = zscale; |
243 double size = qMax(xscale, qMax(yscale, zscale)); |
186 |
244 return qMax(qAbs(size / 2.0), 1.0); |
187 if (xscale > yscale) |
|
188 { |
|
189 if (xscale > zscale) |
|
190 size = xscale; |
|
191 } |
|
192 else if (yscale > zscale) |
|
193 size = yscale; |
|
194 |
|
195 if (qAbs (size) >= 2.0) |
|
196 return qAbs (size / 2); |
|
197 |
|
198 return 1.0; |
|
199 } |
245 } |
200 |
246 |
201 // ============================================================================= |
247 // ============================================================================= |
202 // |
248 // |
203 // Yields the center of the bounding box. |
249 // Yields the center of the bounding box. |
204 // |
250 // |
205 Vertex BoundingBox::center() const |
251 Vertex BoundingBox::center() const |
206 { |
252 { |
207 return Vertex ( |
253 return { |
208 (m_vertex0.x() + m_vertex1.x()) / 2, |
254 (m_vertex0.x + m_vertex1.x) / 2, |
209 (m_vertex0.y() + m_vertex1.y()) / 2, |
255 (m_vertex0.y + m_vertex1.y) / 2, |
210 (m_vertex0.z() + m_vertex1.z()) / 2); |
256 (m_vertex0.z + m_vertex1.z) / 2 |
|
257 }; |
211 } |
258 } |
212 |
259 |
213 bool BoundingBox::isEmpty() const |
260 bool BoundingBox::isEmpty() const |
214 { |
261 { |
215 return m_isEmpty; |
262 return m_isEmpty; |