bbox.cpp

changeset 183
f1b8cb53d2a2
parent 182
9374fea8f77f
child 184
fae3bc9ce319
equal deleted inserted replaced
182:9374fea8f77f 183:f1b8cb53d2a2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri 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 "common.h"
20 #include "bbox.h"
21 #include "ldtypes.h"
22 #include "file.h"
23
24 // =============================================================================
25 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
26 // =============================================================================
27 bbox::bbox () {
28 reset ();
29 }
30
31 // =============================================================================
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
33 // =============================================================================
34 void bbox::calculate () {
35 reset ();
36
37 if (!g_curfile)
38 return;
39
40 for (LDObject* obj : g_curfile->m_objs)
41 calcObject (obj);
42 }
43
44 // =============================================================================
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
46 // =============================================================================
47 void bbox::calcObject (LDObject* obj) {
48 switch (obj->getType ()) {
49 case LDObject::Line:
50 {
51 LDLine* line = static_cast<LDLine*> (obj);
52 for (short i = 0; i < 2; ++i)
53 calcVertex (line->vaCoords[i]);
54 }
55 break;
56
57 case LDObject::Triangle:
58 {
59 LDTriangle* tri = static_cast<LDTriangle*> (obj);
60 for (short i = 0; i < 3; ++i)
61 calcVertex (tri->vaCoords[i]);
62 }
63 break;
64
65 case LDObject::Quad:
66 {
67 LDQuad* quad = static_cast<LDQuad*> (obj);
68 for (short i = 0; i < 4; ++i)
69 calcVertex (quad->vaCoords[i]);
70 }
71 break;
72
73 case LDObject::CondLine:
74 {
75 LDCondLine* line = static_cast<LDCondLine*> (obj);
76 for (short i = 0; i < 4; ++i)
77 calcVertex (line->vaCoords[i]);
78 }
79 break;
80
81 case LDObject::Subfile:
82 {
83 LDSubfile* ref = static_cast<LDSubfile*> (obj);
84 vector<LDObject*> objs = ref->inlineContents (true, true);
85
86 for (LDObject* obj : objs) {
87 calcObject (obj);
88 delete obj;
89 }
90 }
91 break;
92
93 case LDObject::Radial:
94 {
95 LDRadial* rad = static_cast<LDRadial*> (obj);
96 vector<LDObject*> objs = rad->decompose (true);
97
98 for (LDObject* obj : objs) {
99 calcObject (obj);
100 delete obj;
101 }
102 }
103 break;
104
105 default:
106 break;
107 }
108 }
109
110 // =============================================================================
111 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
112 // =============================================================================
113 void bbox::calcVertex (vertex v) {
114 for (const Axis ax : g_Axes) {
115 if (v[ax] < m_v0[ax])
116 m_v0[ax] = v[ax];
117
118 if (v[ax] > m_v1[ax])
119 m_v1[ax] = v[ax];
120 }
121
122 m_empty = false;
123 }
124
125 // =============================================================================
126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
127 // =============================================================================
128 void bbox::reset () {
129 m_v0[X] = m_v0[Y] = m_v0[Z] = +0x7FFFFFFF;
130 m_v1[X] = m_v1[Y] = m_v1[Z] = -0x7FFFFFFF;
131
132 m_empty = true;
133 }
134
135 // =============================================================================
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
137 // =============================================================================
138 double bbox::size () const {
139 double fXScale = (m_v0[X] - m_v1[X]);
140 double fYScale = (m_v0[Y] - m_v1[Y]);
141 double fZScale = (m_v0[Z] - m_v1[Z]);
142 double fSize = fZScale;
143
144 if (fXScale > fYScale) {
145 if (fXScale > fZScale)
146 fSize = fXScale;
147 } else if (fYScale > fZScale)
148 fSize = fYScale;
149
150 if (abs (fSize) >= 2.0f)
151 return abs (fSize / 2);
152
153 return 1.0f;
154 }
155
156 // =============================================================================
157 vertex bbox::center () const {
158 return vertex (
159 (m_v0[X] + m_v1[X]) / 2,
160 (m_v0[Y] + m_v1[Y]) / 2,
161 (m_v0[Z] + m_v1[Z]) / 2);
162 }
163
164 // =============================================================================
165 bool bbox::empty() const {
166 return m_empty;
167 }

mercurial