src/boundingbox.cpp

Sat, 29 Aug 2015 16:30:56 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 29 Aug 2015 16:30:56 +0300
changeset 951
54d90af8542d
parent 941
f895379d7fab
permissions
-rw-r--r--

Closed old branch

941
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 - 2015 Teemu Piippo
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include "boundingbox.h"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "ldDocument.h"
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 BoundingBox::BoundingBox()
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 reset();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 void BoundingBox::calculateFromCurrentDocument()
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 reset();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 if (CurrentDocument() == nullptr)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 return;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 for (LDObjectPtr obj : CurrentDocument()->objects())
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 calcObject (obj);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 void BoundingBox::calcObject (LDObjectPtr obj)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 switch (obj->type())
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 case OBJ_Line:
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 case OBJ_Triangle:
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 case OBJ_Quad:
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 case OBJ_CondLine:
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 for (int i = 0; i < obj->numVertices(); ++i)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 calcVertex (obj->vertex (i));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 } break;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 case OBJ_Subfile:
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 LDSubfilePtr ref = obj.staticCast<LDSubfile>();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 LDObjectList objs = ref->inlineContents (true, false);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 for (LDObjectPtr obj : objs)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 calcObject (obj);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 obj->destroy();
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 break;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 default:
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 break;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 BoundingBox& BoundingBox::operator<< (const Vertex& v)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 calcVertex (v);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 return *this;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 BoundingBox& BoundingBox::operator<< (LDObjectPtr obj)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 calcObject (obj);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 return *this;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 void BoundingBox::calcVertex (const Vertex& vertex)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 m_vertex0.setX (min (vertex.x(), m_vertex0.x()));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 m_vertex0.setY (min (vertex.y(), m_vertex0.y()));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 m_vertex0.setZ (min (vertex.z(), m_vertex0.z()));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 m_vertex1.setX (max (vertex.x(), m_vertex1.x()));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 m_vertex1.setY (max (vertex.y(), m_vertex1.y()));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 m_vertex1.setZ (max (vertex.z(), m_vertex1.z()));
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 setEmpty (false);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 void BoundingBox::reset()
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 m_vertex0 = Vertex (10000.0, 10000.0, 10000.0);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 m_vertex1 = Vertex (-10000.0, -10000.0, -10000.0);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 setEmpty (true);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 double BoundingBox::longestMeasurement() const
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 double xscale = (m_vertex0.x() - m_vertex1.x());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 double yscale = (m_vertex0.y() - m_vertex1.y());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 double zscale = (m_vertex0.z() - m_vertex1.z());
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 double size = zscale;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 if (xscale > yscale)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 if (xscale > zscale)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 size = xscale;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 else if (yscale > zscale)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 size = yscale;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 if (abs (size) >= 2.0)
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 return abs (size / 2);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 return 1.0;
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 }
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 // =============================================================================
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 //
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 Vertex BoundingBox::center() const
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 {
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 return Vertex (
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 (m_vertex0.x() + m_vertex1.x()) / 2,
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 (m_vertex0.y() + m_vertex1.y()) / 2,
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 (m_vertex0.z() + m_vertex1.z()) / 2);
f895379d7fab Refactoring update.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 }

mercurial