Guest User

Sphere::FromTetrahedron

a guest
Aug 15th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1.   Sphere Sphere::FromTetrahedron(const glm::vec3& p1, const glm::vec3& p2,
  2.                                  const glm::vec3& p3, const glm::vec3& p4)
  3.   {
  4.     assert(AreEqual(p1, p2) == false);
  5.     assert(AreEqual(p1, p3) == false);
  6.     assert(AreEqual(p1, p4) == false);
  7.     assert(AreEqual(p2, p3) == false);
  8.     assert(AreEqual(p2, p4) == false);
  9.     assert(AreEqual(p3, p4) == false);
  10.  
  11.     glm::mat4 matrix(1.0f);
  12.     matrix = glm::row(matrix, 0, glm::vec4(p1.x, p2.x, p3.x, p4.x));
  13.     matrix = glm::row(matrix, 1, glm::vec4(p1.y, p2.y, p3.y, p4.y));
  14.     matrix = glm::row(matrix, 2, glm::vec4(p1.z, p2.z, p3.z, p4.z));
  15.     matrix = glm::row(matrix, 3, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
  16.  
  17.     // Function that assert if determinant is 0
  18.     const float a = Determinant(matrix);
  19.  
  20.     glm::mat4 D = matrix;
  21.     glm::vec3 center(0.0f);
  22.  
  23.     const glm::vec4 squares = {
  24.       GetSquaredLength(p1),
  25.       GetSquaredLength(p2),
  26.       GetSquaredLength(p3),
  27.       GetSquaredLength(p4),
  28.     };
  29.  
  30.     // D[0] = squares;
  31.     D        = glm::row(D, 0, squares);
  32.     center.x = glm::determinant(D);
  33.  
  34.     // D[1] = matrix[0];
  35.     D        = glm::row(D, 1, glm::row(matrix, 0));
  36.     center.y = -glm::determinant(D);
  37.  
  38.     // D[2] = matrix[1];
  39.     D        = glm::row(D, 2, glm::row(matrix, 1));
  40.     center.z = glm::determinant(D);
  41.  
  42.     center /= (2.0f * a);
  43.     return Sphere(center, glm::distance(p1, center));
Add Comment
Please, Sign In to add comment