Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/Core/Math/BoundingVolumes/Headers/BoundingBox.h

Diff revisions: vs.
  @@ -32,7 +32,8 @@
32 32 public:
33 33 BoundingBox() : GUIDWrapper(),
34 34 _computed(false),
35 - _visibility(false)
35 + _visibility(false),
36 + _pointsDirty(true)
36 37 {
37 38 _min.set(100000.0f, 100000.0f, 100000.0f);
38 39 _max.set(-100000.0f, -100000.0f, -100000.0f);
  @@ -42,6 +43,7 @@
42 43 BoundingBox(const vec3<F32>& min, const vec3<F32>& max) : GUIDWrapper(),
43 44 _computed(false),
44 45 _visibility(false),
46 + _pointsDirty(true),
45 47 _min(min),
46 48 _max(max)
47 49 {
  @@ -58,6 +60,7 @@
58 60 this->_extent = b._extent;
59 61 this->_oldMatrix = b._oldMatrix;
60 62 this->_points.clear();
63 + this->_pointsDirty = true;
61 64 for(U8 i = 0; i < 8; i++){
62 65 this->_points.push_back(b._points[i]);
63 66 }
  @@ -73,6 +76,7 @@
73 76 this->_extent = b._extent;
74 77 this->_oldMatrix = b._oldMatrix;
75 78 this->_points.clear();
79 + this->_pointsDirty = true;
76 80 for(U8 i = 0; i < 8; i++){
77 81 this->_points.push_back(b._points[i]);
78 82 }
  @@ -80,7 +84,9 @@
80 84
81 85 inline bool ContainsPoint(const vec3<F32>& point) const {
82 86 //const ReadLock r_lock(_lock);
83 - return (point.x>=_min.x && point.y>=_min.y && point.z>=_min.z && point.x<=_max.x && point.y<=_max.y && point.z<=_max.z);
87 + return (point.x>=_min.x && point.y>=_min.y &&
88 + point.z>=_min.z && point.x<=_max.x &&
89 + point.y<=_max.y && point.z<=_max.z);
84 90 };
85 91
86 92 inline bool Collision(const BoundingBox& AABB2) {
  @@ -97,18 +103,20 @@
97 103 return true;
98 104 }
99 105
100 - inline bool Compare(const BoundingBox& bb) const {/*ReadLock r_lock(_lock);*/ return _GUID == bb._GUID;}
106 + inline bool Compare(const BoundingBox& bb) const {
107 + /*ReadLock r_lock(_lock);*/
108 + return _GUID == bb._GUID;
109 + }
101 110
102 - bool operator == (BoundingBox& B){return Compare(B);}
103 - bool operator != (BoundingBox& B){return !Compare(B);}
111 + bool operator == (const BoundingBox& B) const { return Compare(B); }
112 + bool operator != (const BoundingBox& B) const { return !Compare(B); }
104 113
105 114 /// Optimized method
106 115 inline bool Intersect(const Ray &r, F32 t0, F32 t1) const {
107 116 //ReadLock r_lock(_lock);
108 117
109 118 F32 t_min, t_max, ty_min, ty_max, tz_min, tz_max;
110 - vec3<F32> bounds[2];
111 - bounds[0] = _min; bounds[1] = _max;
119 + vec3<F32> bounds[] = {_min, _max};
112 120
113 121 t_min = (bounds[r.sign[0]].x - r.origin.x) * r.inv_direction.x;
114 122 t_max = (bounds[1-r.sign[0]].x - r.origin.x) * r.inv_direction.x;
  @@ -122,6 +130,7 @@
122 130 t_min = ty_min;
123 131 if (ty_max < t_max)
124 132 t_max = ty_max;
133 +
125 134 tz_min = (bounds[r.sign[2]].z - r.origin.z) * r.inv_direction.z;
126 135 tz_max = (bounds[1-r.sign[2]].z - r.origin.z) * r.inv_direction.z;
127 136
  @@ -144,6 +153,7 @@
144 153 if(v.y < _min.y) _min.y = v.y;
145 154 if(v.z > _max.z) _max.z = v.z;
146 155 if(v.z < _min.z) _min.z = v.z;
156 + _pointsDirty = true;
147 157 };
148 158
149 159 inline void Add(const BoundingBox& bb) {
  @@ -154,18 +164,21 @@
154 164 if(bb._min.y < _min.y) _min.y = bb._min.y;
155 165 if(bb._max.z > _max.z) _max.z = bb._max.z;
156 166 if(bb._min.z < _min.z) _min.z = bb._min.z;
167 + _pointsDirty = true;
157 168 }
158 169
159 170 inline void Translate(const vec3<F32>& v) {
160 171 //WriteLock w_lock(_lock);
161 172 _min += v;
162 173 _max += v;
174 + _pointsDirty = true;
163 175 }
164 176
165 177 inline void Multiply(F32 factor){
166 178 //WriteLock w_lock(_lock);
167 179 _min *= factor;
168 180 _max *= factor;
181 + _pointsDirty = true;
169 182 }
170 183
171 184 inline void Multiply(const vec3<F32>& v){
  @@ -176,6 +189,7 @@
176 189 _max.x *= v.x;
177 190 _max.y *= v.y;
178 191 _max.z *= v.z;
192 + _pointsDirty = true;
179 193 }
180 194
181 195 inline void MultiplyMax(const vec3<F32>& v){
  @@ -183,6 +197,7 @@
183 197 _max.x *= v.x;
184 198 _max.y *= v.y;
185 199 _max.z *= v.z;
200 + _pointsDirty = true;
186 201 }
187 202
188 203 inline void MultiplyMin(const vec3<F32>& v){
  @@ -190,6 +205,7 @@
190 205 _min.x *= v.x;
191 206 _min.y *= v.y;
192 207 _min.z *= v.z;
208 + _pointsDirty = true;
193 209 }
194 210
195 211 void Transform(const BoundingBox& initialBoundingBox, const mat4<F32>& mat){
  @@ -221,46 +237,114 @@
221 237 }
222 238 }
223 239 }
224 - ComputePoints();
240 + _pointsDirty = true;
241 + }
242 +
243 + inline void setComputed(bool state) {
244 + _computed = state;
245 + }
246 +
247 + inline bool isComputed() const {
248 + /*ReadLock r_lock(_lock);*/
249 + return _computed;
250 + }
251 +
252 + inline bool getVisibility() const {
253 + /*ReadLock r_lock(_lock);*/
254 + return _visibility;
255 + }
256 +
257 + inline const vec3<F32>& getMin() const {
258 + /*ReadLock r_lock(_lock);*/
259 + return _min;
260 + }
261 +
262 + inline const vec3<F32>& getMax() const {
263 + /*ReadLock r_lock(_lock);*/
264 + return _max;
265 + }
266 +
267 + inline vec3<F32> getCenter() const {
268 + /*ReadLock r_lock(_lock);*/
269 + return (_max + _min)*0.5f;
270 + }
271 +
272 + inline vec3<F32> getExtent() const {
273 + /*ReadLock r_lock(_lock);*/
274 + return _max -_min;
275 + }
276 +
277 + inline vec3<F32> getHalfExtent() const {
278 + /*ReadLock r_lock(_lock);*/
279 + return (_max -_min) * 0.5f;
280 + }
281 +
282 + inline F32 getWidth() const {
283 + /*ReadLock r_lock(_lock);*/
284 + return _max.x - _min.x;
285 + }
286 +
287 + inline F32 getHeight() const {
288 + /*ReadLock r_lock(_lock);*/
289 + return _max.y - _min.y;
290 + }
291 +
292 + inline F32 getDepth() const {
293 + /*ReadLock r_lock(_lock);*/
294 + return _max.z - _min.z;
295 + }
296 +
297 + inline void setVisibility(bool visibility) {
298 + /*WriteLock w_lock(_lock);*/
299 + _visibility = visibility;
300 + }
301 +
302 + inline void setMin(const vec3<F32>& min) {
303 + /*WriteLock w_lock(_lock);*/
304 + _min = min;
305 + _pointsDirty = true;
306 + }
307 +
308 + inline void setMax(const vec3<F32>& max) {
309 + /*WriteLock w_lock(_lock);*/
310 + _max = max;
311 + _pointsDirty = true;
312 + }
313 +
314 + inline void set(const vec3<F32>& min, const vec3<F32>& max) {
315 + /*WriteLock w_lock(_lock);*/
316 + _min = min;
317 + _max = max;
318 + _pointsDirty = true;
319 + }
320 +
321 + inline void reset() {
322 + /*WriteLock w_lock(_lock);*/
323 + _min.set(100000.0f, 100000.0f, 100000.0f);
324 + _max.set(-100000.0f, -100000.0f, -100000.0f);
325 + _pointsDirty = true;
225 326 }
226 327
227 - inline void setComputed(bool state) {_computed = state;}
228 - inline bool isComputed() const {/*ReadLock r_lock(_lock);*/ return _computed;}
229 - inline bool getVisibility() const {/*ReadLock r_lock(_lock);*/ return _visibility;}
230 -
231 - inline const vec3<F32>& getMin() const {/*ReadLock r_lock(_lock);*/ return _min;}
232 - inline const vec3<F32>& getMax() const {/*ReadLock r_lock(_lock);*/ return _max;}
233 -
234 - inline vec3<F32> getCenter() const {/*ReadLock r_lock(_lock);*/ return (_max+_min)*0.5f;}
235 - inline vec3<F32> getExtent() const {/*ReadLock r_lock(_lock);*/ return _max -_min;}
236 - inline vec3<F32> getHalfExtent() const {return (_max -_min) * 0.5f;}
237 -
238 - inline F32 getWidth() const {/*ReadLock r_lock(_lock);*/ return _max.x - _min.x;}
239 - inline F32 getHeight() const {/*ReadLock r_lock(_lock);*/ return _max.y - _min.y;}
240 - inline F32 getDepth() const {/*ReadLock r_lock(_lock);*/ return _max.z - _min.z;}
241 -
242 - inline void setVisibility(bool visibility) {/*WriteLock w_lock(_lock);*/ _visibility = visibility;}
243 - inline void setMin(const vec3<F32>& min) {/*WriteLock w_lock(_lock);*/ _min = min;}
244 - inline void setMax(const vec3<F32>& max) {/*WriteLock w_lock(_lock);*/ _max = max;}
245 - inline void set(const vec3<F32>& min, const vec3<F32>& max) {/*WriteLock w_lock(_lock);*/ _min = min; _max = max;}
246 - inline void reset(){_min.set(100000.0f, 100000.0f, 100000.0f); _max.set(-100000.0f, -100000.0f, -100000.0f);}
247 - inline const vectorImpl<vec3<F32> >& getPoints() const {return _points;}
328 + inline const vectorImpl<vec3<F32> >& getPoints() const {
329 + ComputePoints();
330 + return _points;
331 + }
248 332
249 - inline F32 nearestDistanceFromPoint( const vec3<F32> &pos){
333 + inline F32 nearestDistanceFromPoint( const vec3<F32> &pos) const {
250 334 const vec3<F32>& center = getCenter();
251 335 const vec3<F32>& hextent = getHalfExtent();
252 336
253 - vec3<F32> nearestVec;
254 - nearestVec.x = Util::max( 0, fabsf( pos.x - center.x ) - hextent.x );
255 - nearestVec.y = Util::max( 0, fabsf( pos.y - center.y ) - hextent.y );
256 - nearestVec.z = Util::max( 0, fabsf( pos.z - center.z ) - hextent.z );
257 -
258 - return nearestVec.length();
337 + return vec3<F32>(Util::max( 0.0f, fabsf( pos.x - center.x ) - hextent.x ),
338 + Util::max( 0.0f, fabsf( pos.y - center.y ) - hextent.y ),
339 + Util::max( 0.0f, fabsf( pos.z - center.z ) - hextent.z )).length();
259 340 }
260 341
261 342 protected:
262 343
263 - void ComputePoints() {
344 + void ComputePoints() const {
345 + if(!_pointsDirty)
346 + return;
347 + /*WriteLock w_lock(_lock);*/
264 348 _points[0].set(_min.x, _min.y, _min.z);
265 349 _points[1].set(_min.x, _min.y, _max.z);
266 350 _points[2].set(_min.x, _max.y, _min.z);
  @@ -269,6 +353,7 @@
269 353 _points[5].set(_max.x, _min.y, _max.z);
270 354 _points[6].set(_max.x, _max.y, _min.z);
271 355 _points[7].set(_max.x, _max.y, _max.z);
356 + _pointsDirty = false;
272 357 }
273 358
274 359 private:
  @@ -276,7 +361,10 @@
276 361 vec3<F32> _min, _max;
277 362 vec3<F32> _center, _extent;
278 363 mat4<F32> _oldMatrix;
279 - vectorImpl<vec3<F32> > _points;
364 +
365 + // This is is very limited in scope so mutable should be ok
366 + mutable bool _pointsDirty;
367 + mutable vectorImpl<vec3<F32> > _points;
280 368 //mutable SharedLock _lock;
281 369 };
282 370