src/types/boundingbox.cpp

Tue, 28 Sep 2021 22:14:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 22:14:00 +0300
changeset 143
7b62c52835a1
parent 33
4c41bfe2ec6e
child 189
815fbaae9cb2
permissions
-rw-r--r--

Fix memory corruption involving document tools.
I don't think that the metaobject-initialization had anything to do with this
but it is a lot simpler without it anyway.

19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2019 Teemu Piippo
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "boundingbox.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 this->consider(vertex);
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 return *this;
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {
20
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
29 this->minimum.x = math::min(vertex.x, this->minimum.x);
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
30 this->minimum.y = math::min(vertex.y, this->minimum.y);
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
31 this->minimum.z = math::min(vertex.z, this->minimum.z);
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
32 this->maximum.x = math::max(vertex.x, this->maximum.x);
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
33 this->maximum.y = math::max(vertex.y, this->maximum.y);
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
34 this->maximum.z = math::max(vertex.z, this->maximum.z);
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 * Returns the length of the bounding box on the longest measure.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 * Yields the center of the bounding box.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 * Returns the length of the bounding box's space diagonal.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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 }

mercurial