AABB.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*************************************************************************/
  2. /* AABB.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef AABB_H
  31. #define AABB_H
  32. #include "Vector3.hpp"
  33. #include "Plane.hpp"
  34. #include <cstdlib>
  35. namespace godot {
  36. class AABB {
  37. public:
  38. Vector3 position;
  39. Vector3 size;
  40. real_t get_area() const; /// get area
  41. inline bool has_no_area() const {
  42. return (size.x <= CMP_EPSILON || size.y <= CMP_EPSILON || size.z <= CMP_EPSILON);
  43. }
  44. inline bool has_no_surface() const {
  45. return (size.x <= CMP_EPSILON && size.y <= CMP_EPSILON && size.z <= CMP_EPSILON);
  46. }
  47. inline const Vector3 &get_position() const { return position; }
  48. inline void set_position(const Vector3 &p_position) { position = p_position; }
  49. inline const Vector3 &get_size() const { return size; }
  50. inline void set_size(const Vector3 &p_size) { size = p_size; }
  51. bool operator==(const AABB &p_rval) const;
  52. bool operator!=(const AABB &p_rval) const;
  53. bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
  54. bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
  55. bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
  56. AABB merge(const AABB &p_with) const;
  57. void merge_with(const AABB &p_aabb); ///merge with another AABB
  58. AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
  59. bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
  60. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
  61. bool smits_intersect_ray(const Vector3 &from, const Vector3 &p_dir, real_t t0, real_t t1) const;
  62. bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
  63. bool intersects_plane(const Plane &p_plane) const;
  64. bool has_point(const Vector3 &p_point) const;
  65. Vector3 get_support(const Vector3 &p_normal) const;
  66. Vector3 get_longest_axis() const;
  67. int get_longest_axis_index() const;
  68. real_t get_longest_axis_size() const;
  69. Vector3 get_shortest_axis() const;
  70. int get_shortest_axis_index() const;
  71. real_t get_shortest_axis_size() const;
  72. AABB grow(real_t p_by) const;
  73. void grow_by(real_t p_amount);
  74. void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
  75. Vector3 get_endpoint(int p_point) const;
  76. AABB expand(const Vector3 &p_vector) const;
  77. void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
  78. void expand_to(const Vector3 &p_vector); /** expand to contain a point if necesary */
  79. operator String() const;
  80. inline AABB() {}
  81. inline AABB(const Vector3 &p_pos, const Vector3 &p_size) {
  82. position = p_pos;
  83. size = p_size;
  84. }
  85. };
  86. } // namespace godot
  87. #endif // RECT3_H