2
0

Plane.pkg 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. $#include "Plane.h"
  2. /// Surface in three-dimensional space.
  3. class Plane
  4. {
  5. public:
  6. /// Construct undefined.
  7. Plane()
  8. {
  9. }
  10. /// Copy-construct from another plane.
  11. Plane(const Plane& plane) :
  12. normal_(plane.normal_),
  13. absNormal_(plane.absNormal_),
  14. intercept_(plane.intercept_)
  15. {
  16. }
  17. /// Construct from 3 vertices.
  18. Plane(const Vector3& v0, const Vector3& v1, const Vector3& v2)
  19. {
  20. Define(v0, v1, v2);
  21. }
  22. /// Construct from a normal vector and a point on the plane.
  23. Plane(const Vector3& normal, const Vector3& point)
  24. {
  25. Define(normal, point);
  26. }
  27. /// Define from 3 vertices.
  28. void Define(const Vector3& v0, const Vector3& v1, const Vector3& v2)
  29. {
  30. Vector3 dist1 = v1 - v0;
  31. Vector3 dist2 = v2 - v0;
  32. Define(dist1.CrossProduct(dist2).Normalized(), v0);
  33. }
  34. /// Define from a normal and a point.
  35. void Define(const Vector3& normal, const Vector3& point)
  36. {
  37. normal_ = normal;
  38. absNormal_ = normal.Abs();
  39. intercept_ = normal.DotProduct(point);
  40. }
  41. /// Return signed distance to a point.
  42. float Distance(const Vector3& point) const { return normal_.DotProduct(point) - intercept_; }
  43. /// Plane normal.
  44. Vector3 normal_ @ normal;
  45. /// Plane absolute normal.
  46. Vector3 absNormal_ @ absNormal;
  47. /// Plane intercept parameter.
  48. float intercept_ @ intercept;
  49. };