Sun, 19 Jan 2020 13:53:07 +0200
fixed a pile of nonsense that caused subfiles to go haywire
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2019 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 "boundingbox.h" | |
20 | ||
21 | BoundingBox& BoundingBox::operator<<(const Point3D& vertex) | |
22 | { | |
23 | this->consider(vertex); | |
24 | return *this; | |
25 | } | |
26 | ||
27 | void BoundingBox::consider(const Point3D& vertex) | |
28 | { | |
20 | 29 | this->minimum.x = math::min(vertex.x, this->minimum.x); |
30 | this->minimum.y = math::min(vertex.y, this->minimum.y); | |
31 | this->minimum.z = math::min(vertex.z, this->minimum.z); | |
32 | this->maximum.x = math::max(vertex.x, this->maximum.x); | |
33 | this->maximum.y = math::max(vertex.y, this->maximum.y); | |
34 | this->maximum.z = math::max(vertex.z, this->maximum.z); | |
19 | 35 | } |
36 | ||
37 | /* | |
38 | * Returns the length of the bounding box on the longest measure. | |
39 | */ | |
40 | double longestMeasure(const BoundingBox& box) | |
41 | { | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
42 | if (box != emptyBoundingBox) |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
43 | { |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
44 | const double dx = std::abs(box.minimum.x - box.maximum.x); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
45 | const double dy = std::abs(box.minimum.y - box.maximum.y); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
46 | const double dz = std::abs(box.minimum.z - box.maximum.z); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
47 | const double size = std::max(std::max(dx, dy), dz); |
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
22
diff
changeset
|
48 | return std::max(size / 2.0, 1.0); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
49 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
50 | else |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
51 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
52 | return 0.0; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
53 | } |
19 | 54 | } |
55 | ||
56 | ||
57 | /* | |
58 | * Yields the center of the bounding box. | |
59 | */ | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
60 | Point3D boxCenter(const BoundingBox& box) |
19 | 61 | { |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
62 | if (box != emptyBoundingBox) |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
63 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
64 | return { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
65 | (box.minimum.x + box.maximum.x) / 2, |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
66 | (box.minimum.y + box.maximum.y) / 2, |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
67 | (box.minimum.z + box.maximum.z) / 2 |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
68 | }; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
69 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
70 | else |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
71 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
72 | return origin; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
73 | } |
19 | 74 | } |
75 | ||
76 | /* | |
77 | * Returns the length of the bounding box's space diagonal. | |
78 | */ | |
79 | double spaceDiagonal(const BoundingBox& box) | |
80 | { | |
20 | 81 | return math::distance(box.minimum, box.maximum); |
19 | 82 | } |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
83 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
84 | bool operator==(const BoundingBox &box_1, const BoundingBox &box_2) |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
85 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
86 | return box_1.minimum == box_2.minimum and box_1.maximum == box_2.maximum; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
87 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
88 | |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
89 | bool operator!=(const BoundingBox &box_1, const BoundingBox &box_2) |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
90 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
91 | return not (box_1 == box_2); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
20
diff
changeset
|
92 | } |