# visualizing the intersection of a parametric triangle vs sphere

I would like to visualize the intersection of a triangle and a sphere. I was able to figure out the parametric equation of a triangle and exclude the intersecting part from it with RegionFunction. I would like to add a wireframe sphere to the scene instead of a solid sphere. But strangely, looking at the intersection "head-on" shows lines crossing the filled part of the triangle. What am I doing wrong - hopefully not the parametric sphere equation. :P Thank you!

sphC = {0.4, 0.4, 0.4};
sphR = Sqrt[0.15];

v = {{1, 1, 1}, {0, 0, 1}, {1, 0, 0}};

splineCirclePts = {{.5, 0, 0}, {1, 0, 0}, {1, 1, 0}, {.5, 1, 0}, {0, 1, 0}, {0, 0, 0}, {.5, 0, 0}};
splineCircleW = {1, .5, .5, 1, .5, .5, 1};
splineCircleK = {0, 0, 0, 1/4, 1/2, 1/2, 3/4, 1, 1, 1};

sphereParamViz[{x_, y_, z_}, \[Rho]_] := Module[{d = 32},
{Line[#] & /@
Table[{x, y, z} + {\[Rho] Sin[\[Theta]] Sin[\[Phi]], \[Rho] Cos[\[Theta]] \
Sin[\[Phi]], \[Rho] Cos[\[Phi]]}, {\[Theta], 0, 2 \[Pi], (2 \[Pi])/
d}, {\[Phi], 0, \[Pi], \[Pi]/d}],
Line[#] & /@
Table[{x, y, z} + {\[Rho] Sin[\[Theta]] Sin[\[Phi]], \[Rho] Cos[\[Theta]] \
Sin[\[Phi]], \[Rho] Cos[\[Phi]]}, {\[Phi], 0, \[Pi], \[Pi]/
d}, {\[Theta], 0, 2 \[Pi], (2 \[Pi])/d}]}
]

triParam = ParametricPlot3D[(1 - \[Sigma]) (v[[1]] +
t (v[[2]] - v[[1]])) + \[Sigma] (v[[1]] +
t (v[[3]] - v[[1]])), {t, 0, 1}, {\[Sigma], 0, 1},
PlotStyle -> FaceForm[LightBlue, LightGreen],
BoundaryStyle -> Directive[Black, Thick], Mesh -> None,
RegionFunction ->
Function[{x, y, z, u, v},
sphR^2 < (x - sphC[[1]])^2 + (y - sphC[[2]])^2 + (z -
sphC[[3]])^2]];
sphereSolid = Graphics3D[{FaceForm[Green], Sphere[sphC, sphR]}];
sphereParam = ParametricPlot3D[
sphC + sphR { Cos[\[Theta]] Sin[\[Phi]],
Sin[\[Theta]] Sin[\[Phi]], Cos[\[Phi]]}, {\[Theta], 0,
2 \[Pi]}, {\[Phi], 0, \[Pi]}, PlotStyle -> FaceForm[None, None],
AxesOrigin -> {0, 0, 0}, Mesh -> 16,
MeshStyle -> {Directive[Red, Thick], Directive[Red, Thick]}];
sphereWire = Graphics3D[{sphereParamViz[sphC, sphR]}];
sphereSpline = Graphics3D[{GeometricTransformation[
BSplineCurve[splineCirclePts, SplineWeights -> splineCircleW,
SplineKnots -> splineCircleK],
AffineTransform[{IdentityMatrix[3],
sphC + {0, 0, #[[1]]}}].ScalingTransform[#[[2]] {1, 1,
1}].ScalingTransform[{2, 2,
2}].AffineTransform[{IdentityMatrix[3], {-0.5, -0.5,
0}}]] & /@ ({Sin[#], Cos[#]} & /@ (-\[Pi]/2 +
Range[11] \[Pi]/12) sphR)}];


Visualization:

Show[triParam, sphereSolid, sphereParam, ViewPoint -> {Infinity, Infinity, 0}, Boxed -> False, Axes -> False,  ImageSize -> {600, 400}]
Show[triParam, sphereSolid, sphereParam, ViewPoint -> {1, -1, 1}, Boxed -> False, Axes -> False, ImageSize -> {600, 400}]
Show[triParam, sphereSolid, sphereWire, ViewPoint -> {Infinity, Infinity, 0}, Boxed -> False, Axes -> False, ImageSize -> {600, 400}]
Show[triParam, sphereSolid, sphereWire, ViewPoint -> {1, -1, 1}, Boxed -> False, Axes -> False, ImageSize -> {600, 400}]
Show[triParam, sphereSolid, sphereSpline, ViewPoint -> {1, -1, 1}, Boxed -> False, Axes -> False, ImageSize -> {600, 400}]


Here's a shorter way to do it, by heavily using region functionality:

sphC = {2/5, 2/5, 2/5}; sphR = Sqrt[15]/10;
v = {{1, 1, 1}, {0, 0, 1}, {1, 0, 0}};

tri = DiscretizeRegion[RegionDifference[Polygon[v], Ball[sphC, sphR]]];
circ = DiscretizeRegion[RegionIntersection[Polygon[v], Sphere[sphC, sphR]]];

Show[Graphics3D[{{Directive[FaceForm[], EdgeForm[Black]], Triangle[v]},
MeshPrimitives[circ, 1], {EdgeForm[], MeshPrimitives[tri, 2]}}],
ParametricPlot3D[sphC + sphR {Sin[u] Cos[v], Sin[u] Sin[v], Cos[u]},
{u, π/8, π - π/8}, {v, -π, π}, BoundaryStyle -> Red,
MeshStyle -> Red, PlotStyle -> None]]


Some of the mesh lines will appear to stick out; I'm not certain those are avoidable.