Box.cpp 930 B

123456789101112131415161718192021222324252627282930313233
  1. #include "common/Box.h"
  2. Box::Box(const Vector3& min, const Vector3& max) : min(min), max(max) {
  3. }
  4. Box::Box(const Vector3& size) : max(size) {
  5. }
  6. Box Box::offset(const Vector3& offset) const {
  7. return Box(offset + min, offset + max);
  8. }
  9. bool Box::collidesWith(const Box& other) const {
  10. return max[0] > other.min[0] && min[0] < other.max[0] &&
  11. max[1] > other.min[1] && min[1] < other.max[1] &&
  12. max[2] > other.min[2] && min[2] < other.max[2];
  13. }
  14. Box Box::expand(const Vector3& offset) const {
  15. Vector3 add(offset[0] * (offset[0] >= 0.0), offset[1] * (offset[1] >= 0.0),
  16. offset[2] * (offset[2] >= 0.0));
  17. Vector3 sub(offset[0] * (offset[0] <= 0.0), offset[1] * (offset[1] <= 0.0),
  18. offset[2] * (offset[2] <= 0.0));
  19. return Box(min + sub, max + add);
  20. }
  21. const Vector3& Box::getMin() const {
  22. return min;
  23. }
  24. const Vector3& Box::getMax() const {
  25. return max;
  26. }