Plane.hpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*************************************************************************/
  2. /* Plane.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 PLANE_H
  31. #define PLANE_H
  32. #include "Vector3.hpp"
  33. #include <cmath>
  34. namespace godot {
  35. enum ClockDirection {
  36. CLOCKWISE,
  37. COUNTERCLOCKWISE
  38. };
  39. class Plane {
  40. public:
  41. Vector3 normal;
  42. real_t d;
  43. void set_normal(const Vector3 &p_normal);
  44. inline Vector3 get_normal() const { return normal; } ///Point is coplanar, CMP_EPSILON for precision
  45. void normalize();
  46. Plane normalized() const;
  47. /* Plane-Point operations */
  48. inline Vector3 center() const { return normal * d; }
  49. Vector3 get_any_point() const;
  50. Vector3 get_any_perpendicular_normal() const;
  51. bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
  52. real_t distance_to(const Vector3 &p_point) const;
  53. bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;
  54. /* intersections */
  55. bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = 0) const;
  56. bool intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const;
  57. bool intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const;
  58. Vector3 project(const Vector3 &p_point) const;
  59. /* misc */
  60. inline Plane operator-() const { return Plane(-normal, -d); }
  61. bool is_almost_like(const Plane &p_plane) const;
  62. bool operator==(const Plane &p_plane) const;
  63. bool operator!=(const Plane &p_plane) const;
  64. operator String() const;
  65. inline Plane() { d = 0; }
  66. inline Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
  67. normal(p_a, p_b, p_c),
  68. d(p_d) {}
  69. Plane(const Vector3 &p_normal, real_t p_d);
  70. Plane(const Vector3 &p_point, const Vector3 &p_normal);
  71. Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
  72. };
  73. } // namespace godot
  74. #endif // PLANE_H