src/types/boundingbox.cpp

Thu, 29 Mar 2018 10:42:45 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 29 Mar 2018 10:42:45 +0300
changeset 1353
48374309f3d1
parent 1326
69a90bd2dba2
child 1370
c6d5ba08c62c
permissions
-rw-r--r--

refactor

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
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 // =============================================================================
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 BoundingBox::BoundingBox()
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 reset();
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
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 // =============================================================================
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 BoundingBox& BoundingBox::operator<< (const Vertex& v)
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 calcVertex (v);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 return *this;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 // =============================================================================
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 void BoundingBox::calcVertex (const Vertex& vertex)
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 m_vertex0.x = qMin(vertex.x, m_vertex0.x);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 m_vertex0.y = qMin(vertex.y, m_vertex0.y);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 m_vertex0.z = qMin(vertex.z, m_vertex0.z);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 m_vertex1.x = qMax(vertex.x, m_vertex1.x);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 m_vertex1.y = qMax(vertex.y, m_vertex1.y);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 m_vertex1.z = qMax(vertex.z, m_vertex1.z);
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 m_isEmpty = false;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 // =============================================================================
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 // Clears the bounding box
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 void BoundingBox::reset()
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 m_vertex0 = {10000.0, 10000.0, 10000.0};
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 m_vertex1 = {-10000.0, -10000.0, -10000.0};
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 m_isEmpty = true;
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
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 // =============================================================================
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 // Returns the length of the bounding box on the longest measure.
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 double BoundingBox::longestMeasurement() 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 double xscale = m_vertex0.x - m_vertex1.x;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 double yscale = m_vertex0.y - m_vertex1.y;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 double zscale = m_vertex0.z - m_vertex1.z;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 double size = qMax(xscale, qMax(yscale, zscale));
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 return qMax(qAbs(size / 2.0), 1.0);
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 // =============================================================================
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 // Yields the center of the bounding box.
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 //
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 Vertex BoundingBox::center() const
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 return {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 (m_vertex0.x + m_vertex1.x) / 2,
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 (m_vertex0.y + m_vertex1.y) / 2,
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 (m_vertex0.z + m_vertex1.z) / 2
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 };
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
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 bool BoundingBox::isEmpty() const
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 return m_isEmpty;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 const Vertex& BoundingBox::vertex0() const
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 return m_vertex0;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 }
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 const Vertex& BoundingBox::vertex1() const
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 {
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 return m_vertex1;
23d48a709ffc moved Vertex and BoundingBox into new code units
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 }

mercurial