Plane.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Collision/Plane.h>
  6. namespace anki
  7. {
  8. void Plane::setFrom3Points(const Vec4& p0, const Vec4& p1, const Vec4& p2)
  9. {
  10. ANKI_ASSERT(p0.w() == 0.0f && p1.w() == 0.0f && p2.w() == 0.0f);
  11. // get plane vectors
  12. const Vec4 u = p1 - p0;
  13. const Vec4 v = p2 - p0;
  14. m_normal = u.cross(v);
  15. // length of normal had better not be zero
  16. ANKI_ASSERT(m_normal.getLengthSquared() != 0.0f);
  17. m_normal.normalize();
  18. m_offset = m_normal.dot(p0);
  19. }
  20. void Plane::setFromPlaneEquation(F32 a, F32 b, F32 c, F32 d)
  21. {
  22. m_normal = Vec4(a, b, c, 0.0f);
  23. // length of normal had better not be zero
  24. ANKI_ASSERT(isZero(m_normal.getLength() - 1.0));
  25. m_offset = d;
  26. }
  27. Plane Plane::getTransformed(const Transform& trf) const
  28. {
  29. check();
  30. Plane plane;
  31. // Rotate the normal
  32. plane.m_normal = Vec4(trf.getRotation() * m_normal, 0.0f);
  33. // the offset
  34. Mat3x4 rot = trf.getRotation();
  35. rot.transposeRotationPart();
  36. Vec4 newTrans(rot * trf.getOrigin(), 0.0f);
  37. plane.m_offset = m_offset * trf.getScale() + newTrans.dot(m_normal);
  38. return plane;
  39. }
  40. } // end namespace anki