Sat, 29 Aug 2015 16:30:56 +0300
Closed old branch
941 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2015 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 | #include "ldDocument.h" | |
21 | ||
22 | // ============================================================================= | |
23 | // | |
24 | BoundingBox::BoundingBox() | |
25 | { | |
26 | reset(); | |
27 | } | |
28 | ||
29 | // ============================================================================= | |
30 | // | |
31 | void BoundingBox::calculateFromCurrentDocument() | |
32 | { | |
33 | reset(); | |
34 | ||
35 | if (CurrentDocument() == nullptr) | |
36 | return; | |
37 | ||
38 | for (LDObjectPtr obj : CurrentDocument()->objects()) | |
39 | calcObject (obj); | |
40 | } | |
41 | ||
42 | // ============================================================================= | |
43 | // | |
44 | void BoundingBox::calcObject (LDObjectPtr obj) | |
45 | { | |
46 | switch (obj->type()) | |
47 | { | |
48 | case OBJ_Line: | |
49 | case OBJ_Triangle: | |
50 | case OBJ_Quad: | |
51 | case OBJ_CondLine: | |
52 | { | |
53 | for (int i = 0; i < obj->numVertices(); ++i) | |
54 | calcVertex (obj->vertex (i)); | |
55 | } break; | |
56 | ||
57 | case OBJ_Subfile: | |
58 | { | |
59 | LDSubfilePtr ref = obj.staticCast<LDSubfile>(); | |
60 | LDObjectList objs = ref->inlineContents (true, false); | |
61 | ||
62 | for (LDObjectPtr obj : objs) | |
63 | { | |
64 | calcObject (obj); | |
65 | obj->destroy(); | |
66 | } | |
67 | } | |
68 | break; | |
69 | ||
70 | default: | |
71 | break; | |
72 | } | |
73 | } | |
74 | ||
75 | // ============================================================================= | |
76 | // | |
77 | BoundingBox& BoundingBox::operator<< (const Vertex& v) | |
78 | { | |
79 | calcVertex (v); | |
80 | return *this; | |
81 | } | |
82 | ||
83 | // ============================================================================= | |
84 | // | |
85 | BoundingBox& BoundingBox::operator<< (LDObjectPtr obj) | |
86 | { | |
87 | calcObject (obj); | |
88 | return *this; | |
89 | } | |
90 | ||
91 | // ============================================================================= | |
92 | // | |
93 | void BoundingBox::calcVertex (const Vertex& vertex) | |
94 | { | |
95 | m_vertex0.setX (min (vertex.x(), m_vertex0.x())); | |
96 | m_vertex0.setY (min (vertex.y(), m_vertex0.y())); | |
97 | m_vertex0.setZ (min (vertex.z(), m_vertex0.z())); | |
98 | m_vertex1.setX (max (vertex.x(), m_vertex1.x())); | |
99 | m_vertex1.setY (max (vertex.y(), m_vertex1.y())); | |
100 | m_vertex1.setZ (max (vertex.z(), m_vertex1.z())); | |
101 | setEmpty (false); | |
102 | } | |
103 | ||
104 | // ============================================================================= | |
105 | // | |
106 | void BoundingBox::reset() | |
107 | { | |
108 | m_vertex0 = Vertex (10000.0, 10000.0, 10000.0); | |
109 | m_vertex1 = Vertex (-10000.0, -10000.0, -10000.0); | |
110 | setEmpty (true); | |
111 | } | |
112 | ||
113 | // ============================================================================= | |
114 | // | |
115 | double BoundingBox::longestMeasurement() const | |
116 | { | |
117 | double xscale = (m_vertex0.x() - m_vertex1.x()); | |
118 | double yscale = (m_vertex0.y() - m_vertex1.y()); | |
119 | double zscale = (m_vertex0.z() - m_vertex1.z()); | |
120 | double size = zscale; | |
121 | ||
122 | if (xscale > yscale) | |
123 | { | |
124 | if (xscale > zscale) | |
125 | size = xscale; | |
126 | } | |
127 | else if (yscale > zscale) | |
128 | size = yscale; | |
129 | ||
130 | if (abs (size) >= 2.0) | |
131 | return abs (size / 2); | |
132 | ||
133 | return 1.0; | |
134 | } | |
135 | ||
136 | // ============================================================================= | |
137 | // | |
138 | Vertex BoundingBox::center() const | |
139 | { | |
140 | return Vertex ( | |
141 | (m_vertex0.x() + m_vertex1.x()) / 2, | |
142 | (m_vertex0.y() + m_vertex1.y()) / 2, | |
143 | (m_vertex0.z() + m_vertex1.z()) / 2); | |
144 | } |