(*This Mathematica code was written by John Zweck (zweck@utdallas.edu) and generates a mathematical curve/surface that can be printed using a 3D printer. This model can (probably) be printed by Shapeways.*)

(*If you evaluate this Mathematica notebook,the curve/surface will be rendered using computer graphics.*)

(*If you uncomment the export line at the end of the code and set the directory path appropriately for your system you will obtain an STL file that can be sent to a 3D printer.*)

(*The STL file can also be viewed using the open source software Meshlab.*)

(*You may modify this code in any way you like.*)

(*This project was supported by the National Science Foundation through a Supplement to the UTeach Dallas Robert Noyce Scholarship Program (DUE 1544159).*)


(* I learned about tbis surface from Thomas Banchoff's web site:
http://www.math.brown.edu/~banchoff/art/PAC-9603/tour/viviani/viviani.html

Also, the intersection of a sphere and an offcenter cylinder is a curve that is the shadow onto the surface of the earth of a satellite that is in perfect geosynchronous orbit about the earth. I discuss this orbit in my paper 
An Analysis of the Methods Used to Reconstruct the Flight Path of Malaysia Airlines Flight 370[to appear in Education Section of SIAM Review in mid 2016] *)

     RadiusSphere=60;
RadiusCylinder=30;
RadiusTube=1;
numPoints=100;

SurfaceThickness = 1;

Region=ImplicitRegion[(x-RadiusCylinder)^2+y^2 ≤ RadiusCylinder^2,{x,y}];

Roof=ParametricPlot3D[{u,v,(RadiusSphere^2 - (u^2+v^2))^0.5},{u,v} ∈ Region,MaxRecursion->15,Mesh->False,PlotStyle->{Thickness[SurfaceThickness]}];

BaseRegion=ImplicitRegion[(x-RadiusCylinder)^2+y^2 ≤ (RadiusCylinder+0.5*SurfaceThickness)^2,{x,y}];

Base =ParametricPlot3D[{u,v,0},{u,v} ∈ BaseRegion,
Mesh->False, MaxRecursion->4,  PlotStyle->{Thickness[SurfaceThickness+0.75*RadiusTube]}];

Sides = ParametricPlot3D[{RadiusCylinder*(1+Cos[u]),RadiusCylinder*Sin[u],v},
{u,0, 2*Pi+1},{v,0,RadiusSphere},
RegionFunction->Function[{x,y,z}, 0 ≤ z ≤ (RadiusSphere^2 - (x^2+y^2))^0.5],
Mesh->False, MaxRecursion->15,  PlotStyle->{Thickness[SurfaceThickness]}];

IntersectionCurve[t_]={RadiusCylinder*(1+Cos[t]),RadiusCylinder*Sin[t],(RadiusSphere^2 - 2*RadiusCylinder^2(1+Cos[t]))^0.5};

tubeIntersectionCurve=ParametricPlot3D[IntersectionCurve[i],{i,0,2*Pi+1},PlotStyle->Tube[RadiusTube],MaxRecursion->15,PlotPoints->numPoints,PlotRange->All];

output=Show[Roof,Base, Sides,tubeIntersectionCurve]

Export["~/Documents/TEACHING/NoyceProject/Models/STLFiles/TempleOfViviani.stl",output];