Plane.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "Plane.h"
  2. #include "util/Assert.h"
  3. namespace cln {
  4. //==============================================================================
  5. // setFrom3Points =
  6. //==============================================================================
  7. void Plane::setFrom3Points(const Vec3& p0, const Vec3& p1, const Vec3& p2)
  8. {
  9. // get plane vectors
  10. Vec3 u = p1 - p0;
  11. Vec3 v = p2 - p0;
  12. normal = u.cross(v);
  13. // length of normal had better not be zero
  14. ASSERT(!isZero(normal.getLengthSquared()));
  15. normal.normalize();
  16. offset = normal.dot(p0); // ToDo: correct??
  17. }
  18. //==============================================================================
  19. // setFromPlaneEquation =
  20. //==============================================================================
  21. void Plane::setFromPlaneEquation(float a, float b, float c, float d)
  22. {
  23. // normalize for cheap distance checks
  24. float lensq = a * a + b * b + c * c;
  25. // length of normal had better not be zero
  26. ASSERT(!isZero(lensq));
  27. // recover gracefully
  28. if(isZero(lensq))
  29. {
  30. normal = Vec3(1.0, 0.0, 0.0);
  31. offset = 0.0;
  32. }
  33. else
  34. {
  35. float recip = 1.0 / sqrt(lensq);
  36. normal = Vec3(a * recip, b * recip, c * recip);
  37. offset = d * recip;
  38. }
  39. }
  40. //==============================================================================
  41. // getTransformed =
  42. //==============================================================================
  43. Plane Plane::getTransformed(const Transform& trf) const
  44. {
  45. Plane plane;
  46. // the normal
  47. plane.normal = trf.getRotation()* normal;
  48. // the offset
  49. Vec3 newTrans = trf.getRotation().getTransposed() * trf.getOrigin();
  50. plane.offset = offset * trf.getScale() + newTrans.dot(normal);
  51. return plane;
  52. }
  53. } // end namespace