BALL 1.5.0
simpleBox3.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4// $Id: simpleBox3.h,v 1.9 2004/05/27 19:49:42 oliver Exp $
5//
6
7#ifndef BALL_MATHS_SIMPLEBOX3_H
8#define BALL_MATHS_SIMPLEBOX3_H
9
10#ifndef BALL_COMMON_H
11# include <BALL/common.h>
12#endif
13
14#ifndef BALL_MATHS_VECTOR3_H
15# include <BALL/MATHS/vector3.h>
16#endif
17
18namespace BALL
19{
24
30 template <typename T>
32 {
33 public:
34
36
37
40
41
46 ;
47
53
59 TSimpleBox3(const TVector3<T>& a, const TVector3<T>& b);
60
70 TSimpleBox3(const T& ax, const T& ay, const T& az, const T& bx, const T& by, const T& bz);
71
76 virtual ~TSimpleBox3()
77
78 {
79 }
80
84 virtual void clear();
85
87
90
94 void set(const TSimpleBox3& box);
95
100 void set(const TVector3<T>& lower, const TVector3<T>& upper);
101
110 void set(const T& ax, const T& ay, const T& az, const T& bx, const T& by, const T& bz);
111
117
122 void get(TSimpleBox3& box) const;
123
128 void get(TVector3<T>& lower, TVector3<T>& upper) const;
129
138 void get(T& ax, T& ay, T& az, T& bx, T& by, T& bz) const;
139
143 void swap(TSimpleBox3& box);
144
146
147
154 T getSurface() const;
155
159 T getVolume() const;
160
164 T getWidth() const;
165
169 T getHeight() const;
170
174 T getDepth() const;
175
181 void join(const TSimpleBox3& box);
182
184
187
191 bool operator == (const TSimpleBox3& box) const;
192
196 bool operator != (const TSimpleBox3& box) const;
197
204 bool has(const TVector3<T>& point, bool on_surface = false) const;
205
210 bool isIntersecting(const TSimpleBox3& box) const;
212
216
221 bool isValid() const;
222
229 void dump(std::ostream& s = std::cout, Size depth = 0) const;
231
232
236
240
245 };
247
248 template <typename T>
250 : a(),
251 b()
252 {
253 }
254
255 template <typename T>
257 : a(box.a),
258 b(box.b)
259 {
260 }
261
262 template <typename T>
264
265 : a(lower),
266 b(upper)
267 {
268 }
269
270 template <typename T>
271 TSimpleBox3<T>::TSimpleBox3(const T& ax, const T& ay, const T& az,
272 const T& bx, const T& by, const T& bz)
273
274 : a(ax, ay, az),
275 b(bx, by, bz)
276 {
277 }
278
279 template <typename T>
282
283 {
284 a.set(box.a);
285 b.set(box.b);
286 }
287
288 template <typename T>
290 void TSimpleBox3<T>::set(const TVector3<T>& lower, const TVector3<T>& upper)
291
292 {
293 a.set(lower);
294 b.set(upper);
295 }
296
297 template <typename T>
299 void TSimpleBox3<T>::set(const T& ax, const T& ay, const T& az,
300 const T& bx, const T& by, const T& bz)
301
302 {
303 a.set(ax, ay, az);
304 b.set(bx, by, bz);
305 }
306
307 template <typename T>
310
311 {
312 set(box);
313 return *this;
314 }
315
316 template <typename T>
319
320 {
321 box.set(*this);
322 }
323
324 template <typename T>
326 void TSimpleBox3<T>::get(TVector3<T>& lower, TVector3<T>& upper) const
327
328 {
329 lower.set(a);
330 upper.set(b);
331 }
332
333 template <typename T>
335 void TSimpleBox3<T>::get(T& ax, T& ay, T& az, T& bx, T& by, T& bz) const
336
337 {
338 a.get(ax, ay, az);
339 b.get(bx, by, bz);
340 }
341
342 template <typename T>
345
346 {
347 a.swap(box.a);
348 b.swap(box.b);
349 }
350
351 template <typename T>
354
355 {
356 a.set((T)0, (T)0, (T)0);
357 b.set((T)0, (T)0, (T)0);
358 }
359
360 template <typename T>
363
364 {
365 T width = Maths::abs(b.x - a.x);
366 T height = Maths::abs(b.y - a.y);
367 T depth = Maths::abs(b.z - a.z);
368
369 return ((width * height + width * depth + height * depth) * 2);
370 }
371
372 template <typename T>
375
376 {
377 T width = Maths::abs(b.x - a.x);
378 T height = Maths::abs(b.y - a.y);
379 T depth = Maths::abs(b.z - a.z);
380
381 return (width * height * depth);
382 }
383
384 template <typename T>
387
388 {
389 return Maths::abs(b.x - a.x);
390 }
391
392 template <typename T>
395
396 {
397 return Maths::abs(b.y - a.y);
398 }
399
400 template <typename T>
403
404 {
405 return Maths::abs(b.z - a.z);
406 }
407
408 template <typename T>
410
411 {
412 T minimum = a.x;
413 minimum = Maths::min(minimum, b.x);
414 minimum = Maths::min(minimum, box.a.x);
415 minimum = Maths::min(minimum, box.b.x);
416
417 T maximum = a.x;
418 maximum = Maths::max(maximum, b.x);
419 maximum = Maths::max(maximum, box.a.x);
420 maximum = Maths::max(maximum, box.b.x);
421
422 a.x = minimum;
423 b.x = maximum;
424
425 minimum = a.y;
426 minimum = Maths::min(minimum, b.y);
427 minimum = Maths::min(minimum, box.a.y);
428 minimum = Maths::min(minimum, box.b.y);
429
430 maximum = a.y;
431 maximum = Maths::max(maximum, b.y);
432 maximum = Maths::max(maximum, box.a.y);
433 maximum = Maths::max(maximum, box.b.y);
434
435 a.y = minimum;
436 b.y = maximum;
437
438 minimum = a.z;
439 minimum = Maths::min(minimum, b.z);
440 minimum = Maths::min(minimum, box.a.z);
441 minimum = Maths::min(minimum, box.b.z);
442
443 maximum = a.z;
444 maximum = Maths::max(maximum, b.z);
445 maximum = Maths::max(maximum, box.a.z);
446 maximum = Maths::max(maximum, box.b.z);
447
448 a.z = minimum;
449 b.z = maximum;
450 }
451
452 template <typename T>
455
456 {
457 return (a == box.a && b == box.b);
458 }
459
460 template <typename T>
463
464 {
465 return !(*this == box);
466 }
467
468 template <typename T>
471
472 {
473 return (a.isValid() && b.isValid());
474 }
475
476 template <typename T>
477 bool TSimpleBox3<T>::has(const TVector3<T>& point, bool on_surface) const
478
479 {
480 if (!on_surface)
481 {
482 if (Maths::isLess(b[0],a[0]) || Maths::isLess(b[1],a[1]) || Maths::isLess(b[2],a[2]))
483 {
484 for (int i = 0; i < 3; i++)
485 {
486 if (Maths::isLess(point[i],b[i]) || Maths::isLess(a[i],point[i]))
487 {
488 return false;
489 }
490 }
491 }
492 else
493 {
494 for (int i = 0; i < 3; i++)
495 {
496 if (Maths::isLess(point[i],a[i]) || Maths::isLess(b[i],point[i]))
497 {
498 return false;
499 }
500 }
501 }
502 return true;
503 }
504
505 bool temp = false;
506 for (int i = 0; i < 3; i++)
507 {
508 if (Maths::isEqual(point[i],a[i]) || Maths::isEqual(point[i],b[i]))
509 {
510 temp = true;
511 break;
512 }
513 }
514 return (temp && has(point, false));
515 }
516
517 template <typename T>
519
520 {
521 const TVector3<T>* lower;
522 const TVector3<T>* higher;
523 const TVector3<T>* box_lower;
524 const TVector3<T>* box_higher;
525
526 if (Maths::isLess(b[0],a[0]) ||
527 Maths::isLess(b[1],a[1]) ||
528 Maths::isLess(b[2],a[2]))
529 {
530 lower = &b;
531 higher = &a;
532 }
533 else
534 {
535 lower = &a;
536 higher = &b;
537 }
538
539 if (Maths::isLess(box.b[0],box.a[0]) ||
540 Maths::isLess(box.b[1],box.a[1]) ||
541 Maths::isLess(box.b[2],box.a[2]))
542 {
543 box_lower = &box.b;
544 box_higher = &box.a;
545 }
546 else
547 {
548 box_lower = &box.a;
549 box_higher = &box.b;;
550 }
551
552 for (int i = 0; i < 3; i++)
553 {
554 if (Maths::isLess((*box_higher)[i],(*lower)[i]) || Maths::isLess((*higher)[i],(*box_lower)[i]))
555 {
556 return false;
557 }
558 }
559
560 return true;
561 }
562
563 template <typename T>
564 void TSimpleBox3<T>::dump(std::ostream& s, Size depth) const
565
566 {
568
569 BALL_DUMP_HEADER(s, this, this);
570
571 BALL_DUMP_DEPTH(s, depth);
572 s << " a: " << a << std::endl;
573
574 BALL_DUMP_DEPTH(s, depth);
575 s << " b: " << b << std::endl;
576
578 }
579
583
588 template <typename T>
589 std::istream& operator >> (std::istream& s, TSimpleBox3<T>& box)
590
591 {
592 char c;
593 s >> c >> box.a >> box.b >> c;
594 return s;
595 }
596
605 template <typename T>
606 std::ostream& operator << (std::ostream& s, const TSimpleBox3<T>& box)
607
608 {
609 return s << "(" << box.a << ' ' << box.b << ')';
610 }
611
616
617} // namespace BALL
618
619#endif // BALL_MATHS_SimpleBox3_H
#define BALL_CREATE(name)
Definition: create.h:62
#define BALL_INLINE
Definition: debug.h:15
#define BALL_DUMP_STREAM_PREFIX(os)
Definition: macros.h:391
#define BALL_DUMP_STREAM_SUFFIX(os)
Definition: macros.h:395
#define BALL_DUMP_DEPTH(os, depth)
Definition: macros.h:390
#define BALL_DUMP_HEADER(os, cl, ob)
Definition: macros.h:393
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
TSimpleBox3< float > SimpleBox3
Definition: simpleBox3.h:615
Definition: constants.h:13
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
T max(const T &a, const T &b)
Definition: MATHS/common.h:75
bool isLess(const T1 &a, const T2 &b)
Definition: MATHS/common.h:236
T min(const T &a, const T &b)
Definition: MATHS/common.h:102
T abs(const T &t)
Definition: MATHS/common.h:50
bool isEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:212
void get(TVector3< T > &lower, TVector3< T > &upper) const
Definition: simpleBox3.h:326
T getHeight() const
Definition: simpleBox3.h:394
void dump(std::ostream &s=std::cout, Size depth=0) const
Definition: simpleBox3.h:564
TVector3< T > a
Definition: simpleBox3.h:239
bool has(const TVector3< T > &point, bool on_surface=false) const
Definition: simpleBox3.h:477
void set(const T &ax, const T &ay, const T &az, const T &bx, const T &by, const T &bz)
Definition: simpleBox3.h:299
T getSurface() const
Definition: simpleBox3.h:362
void set(const TVector3< T > &lower, const TVector3< T > &upper)
Definition: simpleBox3.h:290
T getDepth() const
Definition: simpleBox3.h:402
void swap(TSimpleBox3 &box)
Definition: simpleBox3.h:344
T getVolume() const
Definition: simpleBox3.h:374
bool isValid() const
Definition: simpleBox3.h:470
bool isIntersecting(const TSimpleBox3 &box) const
Definition: simpleBox3.h:518
virtual void clear()
Definition: simpleBox3.h:353
bool operator==(const TSimpleBox3 &box) const
Definition: simpleBox3.h:454
const TSimpleBox3 & operator=(const TSimpleBox3 &box)
Definition: simpleBox3.h:309
void get(T &ax, T &ay, T &az, T &bx, T &by, T &bz) const
Definition: simpleBox3.h:335
T getWidth() const
Definition: simpleBox3.h:386
bool operator!=(const TSimpleBox3 &box) const
Definition: simpleBox3.h:462
void set(const TSimpleBox3 &box)
Definition: simpleBox3.h:281
TVector3< T > b
Definition: simpleBox3.h:243
void get(TSimpleBox3 &box) const
Definition: simpleBox3.h:318
void join(const TSimpleBox3 &box)
Definition: simpleBox3.h:409
void set(const T *ptr)
Definition: vector3.h:615