/* calculate sphere that goes through 4 points */ struct sphere* sphere_circum(struct sphere* rs, const struct vec4f* v0, const struct vec4f* v1, const struct vec4f* v2, const struct vec4f* v3) { struct vec4f a; vec3_sub(&a, v1, v0); struct vec4f b; vec3_sub(&b, v2, v0); struct vec4f c; vec3_sub(&c, v3, v0); struct vec4f o; struct vec4f tmp; struct mat3f m; mat3_setf(&m, a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, 0.0f, 0.0f, 0.0f); float denom = 2.0f * mat3_det(&m); vec3_muls(&o, vec3_cross(&tmp, &a, &b), vec3_dot(&c, &c)); vec3_add(&o, &o, vec3_muls(&tmp, vec3_cross(&tmp, &c, &a), vec3_dot(&b, &b))); vec3_add(&o, &o, vec3_muls(&tmp, vec3_cross(&tmp, &b, &c), vec3_dot(&a, &a))); vec3_muls(&o, &o, 1.0f/denom); return sphere_setf(rs, v0->x + o.x, v0->y + o.y, v0->z + o.z, vec3_len(&o) + EPSILON); }
float mat4_det(mat4 m) { mat3 n; int j; float t = 0.0; //m[3][0] for(j=0;j<3;j++) { n[j][0] = m[j][1]; n[j][1] = m[j][2]; n[j][2] = m[j][3]; } t -= m[3][0]*mat3_det(n); //m[3][1] for(j=0;j<3;j++) { n[j][0] = m[j][0]; n[j][1] = m[j][2]; n[j][2] = m[j][3]; } t += m[3][1]*mat3_det(n); //m[3][2] for(j=0;j<3;j++) { n[j][0] = m[j][0]; n[j][1] = m[j][1]; n[j][2] = m[j][3]; } t -= m[3][2]*mat3_det(n); //m[3][3] for(j=0;j<3;j++) { n[j][0] = m[j][0]; n[j][1] = m[j][1]; n[j][2] = m[j][2]; } t += m[3][3]*mat3_det(n); return t; }