Plane.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Matrix3x4.h"
  5. namespace Urho3D
  6. {
  7. /// Surface in three-dimensional space.
  8. /// @allfloats
  9. class URHO3D_API Plane
  10. {
  11. public:
  12. /// Construct a degenerate plane with zero normal and parameter.
  13. Plane() noexcept :
  14. d_(0.0f)
  15. {
  16. }
  17. /// Copy-construct from another plane.
  18. Plane(const Plane& plane) noexcept = default;
  19. /// Construct from 3 vertices.
  20. Plane(const Vector3& v0, const Vector3& v1, const Vector3& v2) noexcept
  21. {
  22. Define(v0, v1, v2);
  23. }
  24. /// Construct from a normal vector and a point on the plane.
  25. Plane(const Vector3& normal, const Vector3& point) noexcept
  26. {
  27. Define(normal, point);
  28. }
  29. /// Construct from a 4-dimensional vector, where the w coordinate is the plane parameter.
  30. explicit Plane(const Vector4& plane) noexcept
  31. {
  32. Define(plane);
  33. }
  34. /// Assign from another plane.
  35. Plane& operator =(const Plane& rhs) noexcept = default;
  36. /// Define from 3 vertices.
  37. void Define(const Vector3& v0, const Vector3& v1, const Vector3& v2)
  38. {
  39. Vector3 dist1 = v1 - v0;
  40. Vector3 dist2 = v2 - v0;
  41. Define(dist1.CrossProduct(dist2), v0);
  42. }
  43. /// Define from a normal vector and a point on the plane.
  44. void Define(const Vector3& normal, const Vector3& point)
  45. {
  46. normal_ = normal.Normalized();
  47. absNormal_ = normal_.Abs();
  48. d_ = -normal_.DotProduct(point);
  49. }
  50. /// Define from a 4-dimensional vector, where the w coordinate is the plane parameter.
  51. void Define(const Vector4& plane)
  52. {
  53. normal_ = Vector3(plane.x_, plane.y_, plane.z_);
  54. absNormal_ = normal_.Abs();
  55. d_ = plane.w_;
  56. }
  57. /// Transform with a 3x3 matrix.
  58. void Transform(const Matrix3& transform);
  59. /// Transform with a 3x4 matrix.
  60. void Transform(const Matrix3x4& transform);
  61. /// Transform with a 4x4 matrix.
  62. void Transform(const Matrix4& transform);
  63. /// Project a point on the plane.
  64. Vector3 Project(const Vector3& point) const { return point - normal_ * (normal_.DotProduct(point) + d_); }
  65. /// Return signed distance to a point.
  66. float Distance(const Vector3& point) const { return normal_.DotProduct(point) + d_; }
  67. /// Reflect a normalized direction vector.
  68. Vector3 Reflect(const Vector3& direction) const { return direction - (2.0f * normal_.DotProduct(direction) * normal_); }
  69. /// Return a reflection matrix.
  70. /// @property
  71. Matrix3x4 ReflectionMatrix() const;
  72. /// Return transformed by a 3x3 matrix.
  73. Plane Transformed(const Matrix3& transform) const;
  74. /// Return transformed by a 3x4 matrix.
  75. Plane Transformed(const Matrix3x4& transform) const;
  76. /// Return transformed by a 4x4 matrix.
  77. Plane Transformed(const Matrix4& transform) const;
  78. /// Return as a vector.
  79. Vector4 ToVector4() const { return Vector4(normal_, d_); }
  80. /// Plane normal.
  81. Vector3 normal_;
  82. /// Plane absolute normal.
  83. Vector3 absNormal_;
  84. /// Plane constant.
  85. float d_{};
  86. /// Plane at origin with normal pointing up.
  87. static const Plane UP;
  88. };
  89. }