Box.cpp 1.5 KB

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