-
Notifications
You must be signed in to change notification settings - Fork 34
/
IBounds.java
336 lines (290 loc) · 9.19 KB
/
IBounds.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*---
iGeo - http://igeo.jp
Copyright (c) 2002-2013 Satoru Sugihara
This file is part of iGeo.
iGeo is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, version 3.
iGeo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with iGeo. If not, see <http://www.gnu.org/licenses/>.
---*/
package igeo;
import java.util.ArrayList;
/**
Bounding box described by minimum point and maximum point.
@author Satoru Sugihara
*/
public class IBounds{
public IVec min=null, max=null;
public IBounds(){}
public IBounds(IVec p){ init(p); }
public IBounds(IObject obj){ compare(obj); }
public IBounds(IVec min, IVec max){ this.min=min; this.max=max; }
public IBounds(IVec corner, double xwidth, double yheight, double zdepth){
init(corner);
compare(corner.add(xwidth,yheight,zdepth));
}
public IBounds(double x, double y, double z, double xwidth, double yheight, double zdepth){
IVec p = new IVec(x,y,z);
init(p);
compare(p.add(xwidth,yheight,zdepth));
}
public IBounds(IObject[] objects){
for(int i=0; i<objects.length; i++){ compare(objects[i]); }
}
public IBounds(ArrayList<IObject> objects){
for(int i=0; i<objects.size(); i++){ compare(objects.get(i)); }
}
public IBounds(IBounds b){
min = b.min.cp();
max = b.max.cp();
}
public IBounds cp(){ return new IBounds(this); }
public IVec min(){ return min; }
public IVec getMin(){ return min(); }
public IVec max(){ return max; }
public IVec getMax(){ return max(); }
public double minX(){ return min.x; }
public double minY(){ return min.y; }
public double minZ(){ return min.z; }
public double maxX(){ return max.x; }
public double maxY(){ return max.y; }
public double maxZ(){ return max.z; }
public double width(){ return max.x - min.x; }
public double height(){ return max.y - min.y; }
public double depth(){ return max.z - min.z; }
public IVec size(){
if(min==null||max==null) return null;
return max.dif(min);
}
public IVec getSize(){ return size(); }
public IVec center(){
if(min==null||max==null) return null;
return max.mid(min);
}
public IVec getCenter(){ return center(); }
public void init(IVec p){
min = new IVec(p);
max = new IVec(p);
}
public void init(){ min=max=null; }
public void compare(IVec p){
if(p==null || !p.isValid()) return;
if(min==null||max==null){ init(p); return; }
if(p.x < min.x) min.x=p.x;
else if(p.x > max.x) max.x=p.x;
if(p.y < min.y) min.y=p.y;
else if(p.y > max.y) max.y=p.y;
if(p.z < min.z) min.z=p.z;
else if(p.z > max.z) max.z=p.z;
}
public void compare(IObject e){
synchronized(e){
if(!e.visible()) return; // if e is in the middle of constructor, this should be false.
if(e instanceof IPoint){
IPoint p = (IPoint)e;
compare(p.get());
}
else if(e instanceof IPointR){
IPointR p = (IPointR)e;
compare(p.get());
}
else if(e instanceof ICurve){
ICurve c = (ICurve)e;
for(int i=0; i<c.num(); i++) compare(c.cp(i).get());
}
else if(e instanceof ICurveR){
ICurveR c = (ICurveR)e;
for(int i=0; i<c.num(); i++) compare(c.cp(i).get());
}
else if(e instanceof ISurface){
ISurface s = (ISurface)e;
for(int i=0; i<s.unum(); i++){
for(int j=0; j<s.vnum(); j++){ compare(s.cp(i,j).get()); }
}
}
else if(e instanceof ISurfaceR){
ISurfaceR s = (ISurfaceR)e;
for(int i=0; i<s.unum(); i++){
for(int j=0; j<s.vnum(); j++){ compare(s.cp(i,j).get()); }
}
}
else if(e instanceof IMesh){
IMesh m = (IMesh)e;
for(int i=0; i<m.vertexNum(); i++) compare(m.vertex(i).get());
}
else if(e instanceof IMeshR){
IMeshR m = (IMeshR)e;
for(int i=0; i<m.vertexNum(); i++){ compare(m.vertex(i).get()); }
}
else if(e instanceof IBrep){
IBrep b = (IBrep)e;
for(int i=0; i<b.surfaces.length; i++){
for(int j=0; j<b.surfaces[i].unum(); j++){
for(int k=0; k<b.surfaces[i].vnum(); k++){ compare(b.surfaces[i].cp(j,k).get()); }
}
}
}
else if(e instanceof IVectorObject){
IVectorObject vobj = (IVectorObject)e;
compare(vobj.vec.get().cp(vobj.root));
compare(vobj.root.get());
}
else if(e instanceof IText){
IText txt = (IText)e;
compare(txt.corner(0,0));
compare(txt.corner(1,0));
compare(txt.corner(0,1));
compare(txt.corner(1,1));
}
}
}
/** Calculates bounding box of all the visible objects in IServer */
public void setObjects(IServer server){ setObjects(server.getAllObjects()); }
synchronized public void setObject(IObject object){
ArrayList<IObject> objects = new ArrayList<IObject>();
objects.add(object);
setObjects(objects);
}
synchronized public void setObjects(ArrayList<IObject> objects){
//IOut.err("objects.size()="+objects.size());
if(objects.size()>1000) IOut.debug(10, "calculating bounding box of "+objects.size()+" objects"); //
//boolean first = true;
init();
synchronized(objects){
//for(IObject e:objects){
for(int n=0; n<objects.size(); n++){
IObject e = objects.get(n);
if(e.visible()){
compare(e);
/*
if(e instanceof IPoint){
IPoint p = (IPoint)e;
compare(p.get());
//if(first){ init(p.get()); first=false; }
//else compare(p.get());
}
else if(e instanceof IPointR){
IPointR p = (IPointR)e;
compare(p.get());
//if(first){ init(p.get()); first=false; }
//else compare(p.get());
}
else if(e instanceof ICurve){
ICurve c = (ICurve)e;
for(int i=0; i<c.num(); i++)
compare(c.cp(i).get());
//if(first){ init(c.cp(i).get()); first=false; }
//else compare(c.cp(i).get());
}
else if(e instanceof ICurveR){
ICurveR c = (ICurveR)e;
for(int i=0; i<c.num(); i++)
compare(c.cp(i).get());
//if(first){ init(c.cp(i).get()); first=false; }
//else compare(c.cp(i).get());
}
else if(e instanceof ISurface){
ISurface s = (ISurface)e;
for(int i=0; i<s.unum(); i++){
for(int j=0; j<s.vnum(); j++){
compare(s.cp(i,j).get());
//if(first){ init(s.cp(i,j).get()); first=false; }
//else compare(s.cp(i,j).get());
}
}
}
else if(e instanceof ISurfaceR){
ISurfaceR s = (ISurfaceR)e;
for(int i=0; i<s.unum(); i++){
for(int j=0; j<s.vnum(); j++){
compare(s.cp(i,j).get());
//if(first){ init(s.cp(i,j).get()); first=false; }
//else compare(s.cp(i,j).get());
}
}
}
else if(e instanceof IMesh){
IMesh m = (IMesh)e;
for(int i=0; i<m.vertexNum(); i++){
compare(m.vertex(i).get());
//if(first){ init(m.vertex(i).get()); first=false; }
//else compare(m.vertex(i).get());
}
}
else if(e instanceof IMeshR){
IMeshR m = (IMeshR)e;
for(int i=0; i<m.vertexNum(); i++){
compare(m.vertex(i).get());
//if(first){ init(m.vertex(i).get()); first=false; }
//else compare(m.vertex(i).get());
}
}
else if(e instanceof IBrep){
IBrep b = (IBrep)e;
for(int i=0; i<b.surfaces.length; i++){
for(int j=0; j<b.surfaces[i].unum(); j++){
for(int k=0; k<b.surfaces[i].vnum(); k++){
compare(b.surfaces[i].cp(j,k).get());
//if(first){ init(s.cp(i,j).get()); first=false; }
//else compare(s.cp(i,j).get());
}
}
}
}
else if(e instanceof IVectorObject){
IVectorObject vobj = (IVectorObject)e;
compare(vobj.vec.get());
compare(vobj.root.get());
//if(first){
// init(vobj.vec.get()); first=false;
// compare(vobj.root.get());
//}
//else{
// compare(vobj.vec.get());
// compare(vobj.root.get());
//}
}
*/
}
}
}
if(min!=null && max!=null && min.eq(max, IConfig.tolerance)){
if(min.x==max.x && min.y==max.y && min.z==max.z){ // just a point
// keep the size zero
}
else{
IOut.err("bounding box is too small. minimum size is set");
IVec sz = new IVec(IConfig.tolerance,
IConfig.tolerance,
IConfig.tolerance);
sz.div(2);
max.set(min).add(sz);
min.sub(sz);
}
}
if(objects.size()>1000) IOut.debug(10, "calculation of bounding box completed");
IOut.debug(100, this);
}
public String toString(){
//return min!=null?min.toString():"null" + "-" + max!=null?max.toString():"null";
String s1 = "null";
if(min!=null) s1=min.toString();
String s2 = "null";
if(max!=null) s2=max.toString();
return s1+"-"+s2;
}
public boolean isCloserThan(IBounds b, double thresholdDist){
if(b.min.x - max.x >= thresholdDist) return false;
if(min.x - b.max.x >= thresholdDist) return false;
if(b.min.y - max.y >= thresholdDist) return false;
if(min.y - b.max.y >= thresholdDist) return false;
if(b.min.z - max.z >= thresholdDist) return false;
if(min.z - b.max.z >= thresholdDist) return false;
return true;
}
}