Wed, 17 Feb 2021 16:49:35 +0200
stuff
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 | ||
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
21 | BoundingBox& BoundingBox::operator<<(const glm::vec3& vertex) |
19 | 22 | { |
23 | this->consider(vertex); | |
24 | return *this; | |
25 | } | |
26 | ||
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
27 | void BoundingBox::consider(const glm::vec3& vertex) |
19 | 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 | */ | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
40 | float longestMeasure(const BoundingBox& box) |
19 | 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 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
44 | const float dx = std::abs(box.minimum.x - box.maximum.x); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
45 | const float dy = std::abs(box.minimum.y - box.maximum.y); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
46 | const float dz = std::abs(box.minimum.z - box.maximum.z); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
47 | const float size = std::max(std::max(dx, dy), dz); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
48 | return std::max(size / 2.0f, 1.0f); |
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 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
52 | return 0.0f; |
22
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 | */ | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
60 | glm::vec3 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 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
72 | return glm::vec3{0, 0, 0}; |
22
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 | */ | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
79 | float spaceDiagonal(const BoundingBox& box) |
19 | 80 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
23
diff
changeset
|
81 | return glm::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 | } |