# Test: is the sum of insphere radii independent of the tetrahedralization of a convex polytope inscribed in a sphere?
# Exact arithmetic with sympy on a triangular bipyramid inscribed in the unit sphere.
import sympy as sp
def P(*c): return sp.Matrix(c)
def vol(a,b,c,d): return sp.Abs((b-a).dot((c-a).cross(d-a)))/6
def area(a,b,c): return sp.sqrt(((b-a).cross(c-a)).dot((b-a).cross(c-a)))/2
def inr(a,b,c,d): return 3*vol(a,b,c,d)/(area(a,b,c)+area(a,b,d)+area(a,c,d)+area(b,c,d))
# equator triangle (not equilateral) and poles: all on unit sphere
h=sp.Rational(3,5)  # z of equatorial plane; radius sqrt(1-h^2)=4/5
r=sp.Rational(4,5)
A=P(r,0,h); B=P(-r*sp.Rational(3,5),r*sp.Rational(4,5),h); C=P(-r*sp.Rational(3,5),-r*sp.Rational(4,5),h)
N=P(0,0,1); S=P(0,0,-1)
for X in (A,B,C,N,S): assert sp.simplify(X.dot(X)-1)==0
two=inr(A,B,C,N)+inr(A,B,C,S)                    # split by triangle ABC
three=inr(N,S,A,B)+inr(N,S,B,C)+inr(N,S,C,A)     # split around axis NS (segment NS crosses plane z=h inside ABC since (0,0,h) is interior)
# check (0,0,h) inside triangle ABC (convexity condition for the 3-split)
print("two-split  :",sp.N(two,20)); print("three-split:",sp.N(three,20)); print("difference :",sp.N(two-three,20))
# sanity check of the inradius formula on a regular tetrahedron with edge 2*sqrt(2): r = a/(2*sqrt(6)) = 1/sqrt(3)
T=[P(1,1,1),P(1,-1,-1),P(-1,1,-1),P(-1,-1,1)]
print("regular tet check:",sp.simplify(inr(*T)-1/sp.sqrt(3))==0)
# volumes agree (both splits tile the same bipyramid)
print("volumes equal:",sp.simplify(vol(A,B,C,N)+vol(A,B,C,S)-(vol(N,S,A,B)+vol(N,S,B,C)+vol(N,S,C,A)))==0)
