Following this question, I want to create a sphere from a tetrahedron using this Wolfram support. But so far the resulted sphere is bigger than it should be and center seems to have an offset.

Sphere Sphere::FromTetrahedron(const glm::vec3& p1, const glm::vec3& p2,
                                 const glm::vec3& p3, const glm::vec3& p4)
    assert(AreEqual(p1, p2) == false);
    assert(AreEqual(p1, p3) == false);
    assert(AreEqual(p1, p4) == false);
    assert(AreEqual(p2, p3) == false);
    assert(AreEqual(p2, p4) == false);
    assert(AreEqual(p3, p4) == false);

    glm::mat4 matrix(1.0f);
    matrix = glm::row(matrix, 0, glm::vec4(p1.x, p2.x, p3.x, p4.x));
    matrix = glm::row(matrix, 1, glm::vec4(p1.y, p2.y, p3.y, p4.y));
    matrix = glm::row(matrix, 2, glm::vec4(p1.z, p2.z, p3.z, p4.z));
    matrix = glm::row(matrix, 3, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));

    // Function that assert if determinant is 0
    const float a = Determinant(matrix);

    glm::mat4 D = matrix;
    glm::vec3 center(0.0f);

    const glm::vec4 squares = {

    D        = glm::row(D, 0, squares);
    center.x = glm::determinant(D);

    D        = glm::row(D, 1, glm::row(matrix, 0));
    center.y = - glm::determinant(D);

    D        = glm::row(D, 2, glm::row(matrix, 1));
    center.z = glm::determinant(D);

    center /= (2.0f * a);

    return Sphere(center, glm::distance(p1, center));
  • 1
    \$\begingroup\$ Can you try forming the triple-scalar product Dot(p4 - p1, Cross(p3 - p1, p2 - p1)) to check if your inputs might be coplanar (triple-scalar product = 0)? That would indicate a problem elsewhere in your algorithm. If that doesn't catch anything, can you try running this with four distinct non-coplanar vectors like (0, 0, 0), (3, 0, 0), (0, 5, 0), (0, 0, 7) and printing the matrix you get at each step? \$\endgroup\$ – DMGregory Aug 16 '18 at 15:07
  • \$\begingroup\$ Actually this implementation is correct. I think I was too tired last day and din't noticed a mistake somewhere else. However the triple scalar product helped me for my other question. \$\endgroup\$ – FloFu Aug 18 '18 at 13:25
  • \$\begingroup\$ Glad you got it solved, then! In that case, would it be appropriate to delete this question? Or is there still something we can help with here? \$\endgroup\$ – DMGregory Aug 18 '18 at 13:26

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.