Plane.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "Plane.hpp"
  2. #include "Vector3.hpp"
  3. #include <cmath>
  4. namespace godot {
  5. void Plane::set_normal(const Vector3& p_normal)
  6. {
  7. this->normal = p_normal;
  8. }
  9. Vector3 Plane::project(const Vector3& p_point) const {
  10. return p_point - normal * distance_to(p_point);
  11. }
  12. void Plane::normalize() {
  13. real_t l = normal.length();
  14. if (l==0) {
  15. *this=Plane(0,0,0,0);
  16. return;
  17. }
  18. normal/=l;
  19. d/=l;
  20. }
  21. Plane Plane::normalized() const {
  22. Plane p = *this;
  23. p.normalize();
  24. return p;
  25. }
  26. Vector3 Plane::get_any_point() const {
  27. return get_normal()*d;
  28. }
  29. Vector3 Plane::get_any_perpendicular_normal() const {
  30. static const Vector3 p1 = Vector3(1,0,0);
  31. static const Vector3 p2 = Vector3(0,1,0);
  32. Vector3 p;
  33. if (::fabs(normal.dot(p1)) > 0.99) // if too similar to p1
  34. p=p2; // use p2
  35. else
  36. p=p1; // use p1
  37. p-=normal * normal.dot(p);
  38. p.normalize();
  39. return p;
  40. }
  41. /* intersections */
  42. bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
  43. const Plane &p_plane0=*this;
  44. Vector3 normal0=p_plane0.normal;
  45. Vector3 normal1=p_plane1.normal;
  46. Vector3 normal2=p_plane2.normal;
  47. real_t denom=vec3_cross(normal0,normal1).dot(normal2);
  48. if (::fabs(denom)<=CMP_EPSILON)
  49. return false;
  50. if (r_result) {
  51. *r_result = ( (vec3_cross(normal1, normal2) * p_plane0.d) +
  52. (vec3_cross(normal2, normal0) * p_plane1.d) +
  53. (vec3_cross(normal0, normal1) * p_plane2.d) )/denom;
  54. }
  55. return true;
  56. }
  57. bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3* p_intersection) const {
  58. Vector3 segment=p_dir;
  59. real_t den=normal.dot( segment );
  60. //printf("den is %i\n",den);
  61. if (::fabs(den)<=CMP_EPSILON) {
  62. return false;
  63. }
  64. real_t dist=(normal.dot( p_from ) - d) / den;
  65. //printf("dist is %i\n",dist);
  66. if (dist>CMP_EPSILON) { //this is a ray, before the emiting pos (p_from) doesnt exist
  67. return false;
  68. }
  69. dist=-dist;
  70. *p_intersection = p_from + segment * dist;
  71. return true;
  72. }
  73. bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3* p_intersection) const {
  74. Vector3 segment= p_begin - p_end;
  75. real_t den=normal.dot( segment );
  76. //printf("den is %i\n",den);
  77. if (::fabs(den)<=CMP_EPSILON) {
  78. return false;
  79. }
  80. real_t dist=(normal.dot( p_begin ) - d) / den;
  81. //printf("dist is %i\n",dist);
  82. if (dist<-CMP_EPSILON || dist > (1.0 +CMP_EPSILON)) {
  83. return false;
  84. }
  85. dist=-dist;
  86. *p_intersection = p_begin + segment * dist;
  87. return true;
  88. }
  89. /* misc */
  90. bool Plane::is_almost_like(const Plane& p_plane) const {
  91. return (normal.dot( p_plane.normal ) > _PLANE_EQ_DOT_EPSILON && ::fabs(d-p_plane.d) < _PLANE_EQ_D_EPSILON);
  92. }
  93. Plane::operator String() const {
  94. // return normal.operator String() + ", " + rtos(d);
  95. return String(); // @Todo
  96. }
  97. bool Plane::is_point_over(const Vector3 &p_point) const {
  98. return (normal.dot(p_point) > d);
  99. }
  100. real_t Plane::distance_to(const Vector3 &p_point) const {
  101. return (normal.dot(p_point)-d);
  102. }
  103. bool Plane::has_point(const Vector3 &p_point,real_t _epsilon) const {
  104. real_t dist=normal.dot(p_point) - d;
  105. dist=::fabs(dist);
  106. return ( dist <= _epsilon);
  107. }
  108. Plane::Plane(const Vector3 &p_normal, real_t p_d) {
  109. normal=p_normal;
  110. d=p_d;
  111. }
  112. Plane::Plane(const Vector3 &p_point, const Vector3& p_normal) {
  113. normal=p_normal;
  114. d=p_normal.dot(p_point);
  115. }
  116. Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3,ClockDirection p_dir) {
  117. if (p_dir == CLOCKWISE)
  118. normal=(p_point1-p_point3).cross(p_point1-p_point2);
  119. else
  120. normal=(p_point1-p_point2).cross(p_point1-p_point3);
  121. normal.normalize();
  122. d = normal.dot(p_point1);
  123. }
  124. bool Plane::operator==(const Plane& p_plane) const {
  125. return normal==p_plane.normal && d == p_plane.d;
  126. }
  127. bool Plane::operator!=(const Plane& p_plane) const {
  128. return normal!=p_plane.normal || d != p_plane.d;
  129. }
  130. }