-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoundingBox.js
More file actions
194 lines (162 loc) · 3.94 KB
/
BoundingBox.js
File metadata and controls
194 lines (162 loc) · 3.94 KB
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
/* $RCSfile: BoundingBox.js,v $
* $Revision: 1.5 $ $Date: 2012/08/03 17:00:47 $
* Auth: Jochen Fritz (jfritz@steptools.com)
*
* Copyright (c) 1991-2012 by STEP Tools Inc.
* All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation is hereby granted, provided that this copyright
* notice and license appear on all copies of the software.
*
* STEP TOOLS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. STEP TOOLS
* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES.
*
* ----------------------------------------
*
* Bounding box class
*/
"use strict;"
function BoundingBox() {
this.empty();
}
STATIC (BoundingBox, {
fromArray : function(bb) {
var bbox = new BoundingBox();
bbox.updateX(bb[0]);
bbox.updateY(bb[1]);
bbox.updateZ(bb[2]);
bbox.updateX(bb[3]);
bbox.updateY(bb[4]);
bbox.updateZ(bb[5]);
return bbox;
},
});
METHODS (BoundingBox, {
empty : function()
{
this.minx = NaN;
this.maxx = NaN;
this.miny = NaN;
this.maxy = NaN;
this.minz = NaN;
this.maxz = NaN;
},
isEmpty : function() {
return isNaN(this.minx);
},
diagonal : function() {
if (this.isEmpty())
return 0;
var s=0;
var d;
d = this.maxx - this.minx; s += d*d;
d = this.maxy - this.miny; s += d*d;
d = this.maxz - this.minz; s += d*d;
return Math.sqrt(s);
},
center : function() {
var ret = new Array(3);
ret[0] = (this.maxx + this.minx) / 2.;
ret[1] = (this.maxy + this.miny) / 2.;
ret[2] = (this.maxz + this.minz) / 2.;
return ret;
},
updateX : function (v) {
if (isNaN(this.minx))
this.minx = this.maxx = v;
else {
if (v < this.minx)
this.minx = v;
if (v > this.maxx)
this.maxx = v;
}
},
updateY : function (v) {
if (isNaN(this.miny))
this.miny = this.maxy = v;
else {
if (v < this.miny)
this.miny = v;
if (v > this.maxy)
this.maxy = v;
}
},
updateZ : function (v) {
if (isNaN(this.minz))
this.minz = this.maxz = v;
else {
if (v < this.minz)
this.minz = v;
if (v > this.maxz)
this.maxz = v;
}
},
/*
* Update the given axis.
* i=0 for x,
* i=1 for y,
* i=2 for z
*/
updateI : function (i, v) {
switch (i) {
case 0:
this.updateX(v);
break;
case 1:
this.updateY(v);
break;
case 2:
this.updateZ(v)
break;
}
},
update : function (x, y, z, xform) {
if (typeof x != "number") {
throw new Error ("expecting a number");
}
if (xform) {
var vec=[x,y,z];
var inv = mat4.create();
mat4.inverse(xform, inv);
mat4.multiplyVec3(inv, vec);
x=vec[0];
y=vec[1];
z=vec[2];
}
this.updateX(x);
this.updateY(y);
this.updateZ(z);
},
updateVec : function (vec, xform) {
this.update(vec[0], vec[1], vec[2], xform);
},
updateFrom : function (bbox, xform) {
if (!bbox)
return;
if (!xform) {
this.update(bbox.minx, bbox.miny, bbox.minz);
this.update(bbox.maxx, bbox.maxy, bbox.maxz);
}
else {
this.update(bbox.minx, bbox.miny, bbox.minz, xform);
this.update(bbox.minx, bbox.miny, bbox.maxz, xform);
this.update(bbox.minx, bbox.maxy, bbox.minz, xform);
this.update(bbox.minx, bbox.maxy, bbox.maxz, xform);
this.update(bbox.maxx, bbox.miny, bbox.minz, xform);
this.update(bbox.maxx, bbox.miny, bbox.maxz, xform);
this.update(bbox.maxx, bbox.maxy, bbox.minz, xform);
this.update(bbox.maxx, bbox.maxy, bbox.maxz, xform);
}
},
toString : function() {
return ("(["+this.minx+ "," +this.maxx + "],["
+this.miny+ "," + this.maxy + "],["
+this.minz+"," + this.maxz + "])")
}
});