plane.inl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // This code is in the public domain -- Ignacio Castaño <[email protected]>
  2. #pragma once
  3. #ifndef NV_MATH_PLANE_INL
  4. #define NV_MATH_PLANE_INL
  5. #include "plane.h"
  6. #include "vector.inl"
  7. namespace nv
  8. {
  9. inline Plane::Plane() {}
  10. inline Plane::Plane(float x, float y, float z, float w) : v(x, y, z, w) {}
  11. inline Plane::Plane(const Vector4 & v) : v(v) {}
  12. inline Plane::Plane(const Vector3 & v, float d) : v(v, d) {}
  13. inline Plane::Plane(const Vector3 & normal, const Vector3 & point) : v(normal, -dot(normal, point)) {}
  14. inline Plane::Plane(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2) {
  15. Vector3 n = cross(v1-v0, v2-v0);
  16. float d = -dot(n, v0);
  17. v = Vector4(n, d);
  18. }
  19. inline const Plane & Plane::operator=(const Plane & p) { v = p.v; return *this; }
  20. inline Vector3 Plane::vector() const { return v.xyz(); }
  21. inline float Plane::offset() const { return v.w; }
  22. // Normalize plane.
  23. inline Plane normalize(const Plane & plane, float epsilon = NV_EPSILON)
  24. {
  25. const float len = length(plane.vector());
  26. const float inv = isZero(len, epsilon) ? 0 : 1.0f / len;
  27. return Plane(plane.v * inv);
  28. }
  29. // Get the signed distance from the given point to this plane.
  30. inline float distance(const Plane & plane, const Vector3 & point)
  31. {
  32. return dot(plane.vector(), point) + plane.offset();
  33. }
  34. inline void Plane::operator*=(float s)
  35. {
  36. v *= s;
  37. }
  38. } // nv namespace
  39. #endif // NV_MATH_PLANE_H