diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/box.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/box.js | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/box.js b/js/helper-classes/RDGE/src/core/script/box.js new file mode 100644 index 00000000..8272d952 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/box.js | |||
@@ -0,0 +1,143 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | MAX_VAL = 1e+38; | ||
8 | |||
9 | function box() | ||
10 | { | ||
11 | this.min = [MAX_VAL, MAX_VAL, MAX_VAL]; | ||
12 | this.max = [-MAX_VAL, -MAX_VAL, -MAX_VAL]; | ||
13 | } | ||
14 | |||
15 | box.prototype.addBox = function(a) | ||
16 | { | ||
17 | this.min = vec3.min( this.min, a.min ); | ||
18 | this.max = vec3.max( this.max, a.max ); | ||
19 | // this.min = vec3.min( this.min, a.min ); | ||
20 | // this.max = vec3.max( this.max, a.max ); | ||
21 | } | ||
22 | |||
23 | box.prototype.addVec3 = function(a) | ||
24 | { | ||
25 | this.min = vec3.min( this.min, a ); | ||
26 | this.max = vec3.max( this.max, a ); | ||
27 | } | ||
28 | |||
29 | box.prototype.set = function(min, max) | ||
30 | { | ||
31 | this.min[0] = min[0]; | ||
32 | this.min[1] = min[1]; | ||
33 | this.min[2] = min[2]; | ||
34 | this.max[0] = max[0]; | ||
35 | this.max[1] = max[1]; | ||
36 | this.max[2] = max[2]; | ||
37 | } | ||
38 | |||
39 | box.prototype.reset = function() | ||
40 | { | ||
41 | this.min[0] = MAX_VAL; | ||
42 | this.min[1] = MAX_VAL; | ||
43 | this.min[2] = MAX_VAL; | ||
44 | this.max[0] = -MAX_VAL; | ||
45 | this.max[1] = -MAX_VAL; | ||
46 | this.max[2] = -MAX_VAL; | ||
47 | } | ||
48 | |||
49 | box.prototype.getCenter = function() | ||
50 | { | ||
51 | return [0.5*(this.min[0]+this.max[0]), 0.5*(this.min[1]+this.max[1]), 0.5*(this.min[2]+this.max[2])]; | ||
52 | } | ||
53 | |||
54 | box.prototype.isVisible = function(frustum) | ||
55 | { | ||
56 | var center = this.getCenter(); | ||
57 | var radius = vec3.distance( this.max, center ); | ||
58 | // var diag = vec3.sub( this.max, center ); | ||
59 | |||
60 | var i = 0; | ||
61 | while(i < frustum.length) { | ||
62 | var plane = frustum[i]; | ||
63 | var dist = vec3.dot( plane, center ) + plane[3]; | ||
64 | if( dist < -radius ) { | ||
65 | return false; | ||
66 | } | ||
67 | i++; | ||
68 | } | ||
69 | |||
70 | return true; | ||
71 | } | ||
72 | |||
73 | box.prototype.isValid = function() | ||
74 | { | ||
75 | return !(this.min[0] > this.max[0] || this.min[1] > this.max[1] || this.min[2] > this.max[2]); | ||
76 | } | ||
77 | |||
78 | box.prototype.transform = function(mat) { | ||
79 | var out = new box(); | ||
80 | var pts = []; | ||
81 | pts.push( [ this.min[0], this.min[1], this.min[2] ] ); | ||
82 | pts.push( [ this.min[0], this.max[1], this.min[2] ] ); | ||
83 | pts.push( [ this.max[0], this.max[1], this.min[2] ] ); | ||
84 | pts.push( [ this.max[0], this.min[1], this.min[2] ] ); | ||
85 | pts.push( [ this.min[0], this.min[1], this.max[2] ] ); | ||
86 | pts.push( [ this.min[0], this.max[1], this.max[2] ] ); | ||
87 | pts.push( [ this.max[0], this.max[1], this.max[2] ] ); | ||
88 | pts.push( [ this.max[0], this.min[1], this.max[2] ] ); | ||
89 | |||
90 | var i = pts.length - 1; | ||
91 | do { | ||
92 | out.addVec3( mat4.transformPoint( mat, pts[i] ) ); | ||
93 | } while(i--); | ||
94 | |||
95 | return out; | ||
96 | } | ||
97 | /* | ||
98 | box.prototype.transform = function(mat) { | ||
99 | var newBox = new box(); | ||
100 | var e, f; | ||
101 | |||
102 | newBox.b[0] = mat[12]; newBox.b[1] = mat[13]; newBox.b[2] = mat[14]; | ||
103 | newBox.t[0] = mat[12]; newBox.t[1] = mat[13]; newBox.t[2] = mat[14]; | ||
104 | |||
105 | e = mat[0] * this.min[0]; f = mat[0] * this.max[0]; | ||
106 | newBox.b[0] += (e < f) ? e : f; | ||
107 | newBox.t[0] += (e < f) ? f : e; | ||
108 | |||
109 | e = mat[4] * this.min[1]; f = mat[4] * this.max[1]; | ||
110 | newBox.b[0] += (e < f) ? e : f; | ||
111 | newBox.t[0] += (e < f) ? f : e; | ||
112 | |||
113 | e = mat[8] * this.min[2]; f = mat[8] * this.max[2]; | ||
114 | newBox.b[0] += (e < f) ? e : f; | ||
115 | newBox.t[0] += (e < f) ? f : e; | ||
116 | |||
117 | e = mat[1] * this.min[0]; f = mat[1] * this.max[0]; | ||
118 | newBox.b[1] += (e < f) ? e : f; | ||
119 | newBox.t[1] += (e < f) ? f : e; | ||
120 | |||
121 | e = mat[5] * this.min[1]; f = mat[5] * this.max[1]; | ||
122 | newBox.b[1] += (e < f) ? e : f; | ||
123 | newBox.t[1] += (e < f) ? f : e; | ||
124 | |||
125 | e = mat[9] * this.min[2]; f = mat[9] * this.max[2]; | ||
126 | newBox.b[1] += (e < f) ? e : f; | ||
127 | newBox.t[1] += (e < f) ? f : e; | ||
128 | |||
129 | e = mat[2] * this.min[0]; f = mat[2] * this.max[0]; | ||
130 | newBox.b[2] += (e < f) ? e : f; | ||
131 | newBox.t[2] += (e < f) ? f : e; | ||
132 | |||
133 | e = mat[6] * this.min[1]; f = mat[6] * this.max[1]; | ||
134 | newBox.b[2] += (e < f) ? e : f; | ||
135 | newBox.t[2] += (e < f) ? f : e; | ||
136 | |||
137 | e = mat[10] * this.min[2]; f = mat[10] * this.max[2]; | ||
138 | newBox.b[2] += (e < f) ? e : f; | ||
139 | newBox.t[2] += (e < f) ? f : e; | ||
140 | |||
141 | return newBox; | ||
142 | } | ||
143 | */ \ No newline at end of file | ||