Plane.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Vector3.h"
  25. /// Surface in three-dimensional space.
  26. class Plane
  27. {
  28. public:
  29. /// Construct undefined.
  30. Plane()
  31. {
  32. }
  33. /// Copy-construct from another plane.
  34. Plane(const Plane& plane) :
  35. normal_(plane.normal_),
  36. intercept_(plane.intercept_)
  37. {
  38. }
  39. /// Construct from 3 vertices.
  40. Plane(const Vector3& v0, const Vector3& v1, const Vector3& v2)
  41. {
  42. Define(v0, v1, v2);
  43. }
  44. /// Construct from a normal vector and a point on the plane.
  45. Plane(const Vector3& normal, const Vector3& point)
  46. {
  47. Define(normal, point);
  48. }
  49. /// Define from 3 vertices.
  50. void Define(const Vector3& v0, const Vector3& v1, const Vector3& v2)
  51. {
  52. Vector3 dist1 = v1 - v0;
  53. Vector3 dist2 = v2 - v0;
  54. normal_ = (dist1.CrossProduct(dist2)).Normalized();
  55. absNormal_ = Vector3(fabsf(normal_.x_), fabsf(normal_.y_), fabsf(normal_.z_));
  56. intercept_ = normal_.DotProduct(v0);
  57. }
  58. /// Define from a normal and a point.
  59. void Define(const Vector3& normal, const Vector3& point)
  60. {
  61. normal_ = normal;
  62. absNormal_ = Vector3(fabsf(normal_.x_), fabsf(normal_.y_), fabsf(normal_.z_));
  63. intercept_ = normal_.DotProduct(point);
  64. }
  65. /// Return signed distance to a point.
  66. float Distance(const Vector3& point) const { return normal_.DotProduct(point) - intercept_; }
  67. /// Plane normal.
  68. Vector3 normal_;
  69. /// Plane absolute normal.
  70. Vector3 absNormal_;
  71. /// Plane intercept parameter.
  72. float intercept_;
  73. };