src/types/boundingbox.cpp

Sat, 04 Aug 2018 21:46:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 04 Aug 2018 21:46:58 +0300
changeset 1428
ece049033adc
parent 1370
c6d5ba08c62c
permissions
-rw-r--r--

fixed a crash when trying to open a document for the 3rd time after closing it 2 times

1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "boundingbox.h"
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
21 BoundingBox::BoundingBox() {}
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
22
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
23 BoundingBox& BoundingBox::operator<<(const Vertex& vertex)
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
25 consider(vertex);
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 return *this;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
29 void BoundingBox::consider(const Vertex& vertex)
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
31 this->minimum.x = min(vertex.x, this->minimum.x);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
32 this->minimum.y = min(vertex.y, this->minimum.y);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
33 this->minimum.z = min(vertex.z, this->minimum.z);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
34 this->maximum.x = max(vertex.x, this->maximum.x);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
35 this->maximum.y = max(vertex.y, this->maximum.y);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
36 this->maximum.z = max(vertex.z, this->maximum.z);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
37 this->storedIsEmpty = false;
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
40 /*
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
41 * Clears the bounding box
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
42 */
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
43 void BoundingBox::clear()
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
45 (*this) = {};
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
48 /*
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
49 * Returns the length of the bounding box on the longest measure.
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
50 */
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
51 double BoundingBox::longestMeasure() const
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
53 double dx = this->minimum.x - this->maximum.x;
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
54 double dy = this->minimum.y - this->maximum.y;
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
55 double dz = this->minimum.z - this->maximum.z;
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
56 double size = max(dx, dy, dz);
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
57 return max(abs(size / 2.0), 1.0);
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
60
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
61 /*
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
62 * Yields the center of the bounding box.
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
63 */
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 Vertex BoundingBox::center() const
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 return {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
67 (this->minimum.x + this->maximum.x) / 2,
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
68 (this->minimum.y + this->maximum.y) / 2,
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
69 (this->minimum.z + this->maximum.z) / 2
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 };
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 bool BoundingBox::isEmpty() const
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
75 return this->storedIsEmpty;
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
76 }
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
77
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
78 /*
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
79 * Returns the minimum vertex, the -X, -Y, -Z corner.
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
80 */
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
81 const Vertex& BoundingBox::minimumVertex() const
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
82 {
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
83 return this->minimum;
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
86 /*
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
87 * Returns the maximum vertex, the +X, +Y, +Z corner.
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
88 */
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
89 const Vertex& BoundingBox::maximumVertex() const
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
91 return this->maximum;
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
94 /*
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
95 * Returns the length of the bounding box's space diagonal.
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
96 */
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
97 double BoundingBox::spaceDiagonal() const
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 {
1370
c6d5ba08c62c reworked bounding box
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
99 return distance(this->minimumVertex(), this->maximumVertex());
1315
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 }

mercurial