Plane.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2009-present, 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. void Plane::setFrom3Points(const Vec4& p0, const Vec4& p1, const Vec4& p2)
  8. {
  9. ANKI_ASSERT(p0.w == 0.0f && p1.w == 0.0f && p2.w == 0.0f);
  10. // get plane vectors
  11. const Vec4 u = p1 - p0;
  12. const Vec4 v = p2 - p0;
  13. m_normal = u.cross(v);
  14. // length of normal had better not be zero
  15. ANKI_ASSERT(m_normal.lengthSquared() != 0.0f);
  16. m_normal = m_normal.normalize();
  17. m_offset = m_normal.dot(p0);
  18. }
  19. void Plane::setFromPlaneEquation(F32 a, F32 b, F32 c, F32 d)
  20. {
  21. m_normal = Vec4(a, b, c, 0.0f);
  22. // length of normal had better not be zero
  23. ANKI_ASSERT(isZero(m_normal.length() - 1.0));
  24. m_offset = d;
  25. }
  26. Plane Plane::getTransformed(const Transform& trf) const
  27. {
  28. check();
  29. Plane plane;
  30. // Rotate the normal
  31. plane.m_normal = Vec4(trf.getRotation() * m_normal, 0.0f);
  32. // the offset
  33. Mat3x4 rot = trf.getRotation();
  34. rot.transposeRotationPart();
  35. Vec4 newTrans(rot * trf.getOrigin(), 0.0f);
  36. ANKI_ASSERT(trf.hasUniformScale());
  37. plane.m_offset = m_offset * trf.getScale().x + newTrans.dot(m_normal);
  38. return plane;
  39. }
  40. } // end namespace anki