Box.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "math/Box.h"
  2. Box::Box(const Vector3& min_, const Vector3& max_) : min(min_), max(max_) {
  3. }
  4. Box::Box(const Vector3& size) {
  5. for(int i = 0; i < 3; i++) {
  6. if(size[i] < 0.0f) {
  7. min[i] = size[i];
  8. } else {
  9. max[i] = size[i];
  10. }
  11. }
  12. }
  13. Box Box::offset(const Vector3& offset) const {
  14. return Box(min + offset, max + offset);
  15. }
  16. bool Box::collidesWith(const Box& other) const {
  17. return max[0] > other.min[0] && min[0] < other.max[0] &&
  18. max[1] > other.min[1] && min[1] < other.max[1] &&
  19. max[2] > other.min[2] && min[2] < other.max[2];
  20. }
  21. Box Box::expand(const Vector3& offset) const {
  22. Vector3 add(offset[0] > 0.0f ? offset[0] : 0.0f,
  23. offset[1] > 0.0f ? offset[1] : 0.0f,
  24. offset[2] > 0.0f ? offset[2] : 0.0f);
  25. Vector3 sub(offset[0] < 0.0f ? offset[0] : 0.0f,
  26. offset[1] < 0.0f ? offset[1] : 0.0f,
  27. offset[2] < 0.0f ? offset[2] : 0.0f);
  28. return Box(min + sub, max + add);
  29. }
  30. Box Box::grow(const Vector3& growth) const {
  31. Vector3 half = growth * 0.5f;
  32. Vector3 nMin = min - half;
  33. Vector3 nMax = max + half;
  34. for(int i = 0; i < 3; i++) {
  35. if(nMin[i] > nMax[i]) {
  36. nMin[i] = (min[i] + max[i]) * 0.5f;
  37. nMax[i] = nMin[i];
  38. }
  39. }
  40. return Box(nMin, nMax);
  41. }
  42. const Vector3& Box::getMin() const {
  43. return min;
  44. }
  45. const Vector3& Box::getMax() const {
  46. return max;
  47. }