Path: blob/master/src/sage/plot/plot3d/implicit_plot3d.py
8815 views
"""1Implicit Plots2"""34from implicit_surface import ImplicitSurface56def implicit_plot3d(f, xrange, yrange, zrange, **kwds):7r"""8Plots an isosurface of a function.910INPUT:1112- ``f`` - function1314- ``xrange`` - a 2-tuple (x_min, x_max) or a 3-tuple (x, x_min, x_max)1516- ``yrange`` - a 2-tuple (y_min, y_may) or a 3-tuple (y, y_min, y_may)1718- ``zrange`` - a 2-tuple (z_min, z_maz) or a 3-tuple (z, z_min, z_maz)1920- ``plot_points`` - (default: "automatic", which is 50) the number of21function evaluations in each direction. (The number of cubes in the22marching cubes algorithm will be one less than this). Can be a triple of23integers, to specify a different resolution in each of x,y,z.2425- ``contour`` - (default: 0) plot the isosurface f(x,y,z)==contour. Can be a26list, in which case multiple contours are plotted.2728- ``region`` - (default: None) If region is given, it must be a Python29callable. Only segments of the surface where region(x,y,z) returns a30number >0 will be included in the plot. (Note that returning a Python31boolean is acceptable, since True == 1 and False == 0).3233EXAMPLES::3435sage: var('x,y,z')36(x, y, z)3738A simple sphere::3940sage: implicit_plot3d(x^2+y^2+z^2==4, (x, -3, 3), (y, -3,3), (z, -3,3))4142A nested set of spheres with a hole cut out::4344sage: implicit_plot3d((x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=[1,3,5], \45... region=lambda x,y,z: x<=0.2 or y>=0.2 or z<=0.2).show(viewer='tachyon')4647A very pretty example, attributed to Douglas Summers-Stay (`archived page48<http://web.archive.org/web/20080529033738/http://iat.ubalt.edu/summers/math/platsol.htm>`_)::4950sage: T = RDF(golden_ratio)51sage: p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))52sage: r = 4.7753sage: implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=40).show(viewer='tachyon')5455As I write this (but probably not as you read it), it's almost Valentine's56day, so let's try a heart (from http://mathworld.wolfram.com/HeartSurface.html)5758::5960sage: p = (x^2+9/4*y^2+z^2-1)^3-x^2*z^3-9/(80)*y^2*z^361sage: r = 1.562sage: implicit_plot3d(p, (x, -r,r), (y, -r,r), (z, -r,r), plot_points=80, color='red', smooth=False).show(viewer='tachyon')6364The same examples also work with the default Jmol viewer; for example::6566sage: T = RDF(golden_ratio)67sage: p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))68sage: r = 4.7769sage: implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=40).show()7071Here we use smooth=True with a Tachyon graph::7273sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, smooth=True)7475We explicitly specify a gradient function (in conjunction with smooth=True)76and invert the normals::7778sage: gx = lambda x, y, z: -(2*x + y^2 + z^2)79sage: gy = lambda x, y, z: -(x^2 + 2*y + z^2)80sage: gz = lambda x, y, z: -(x^2 + y^2 + 2*z)81sage: implicit_plot3d(x^2+y^2+z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, \82... plot_points=40, smooth=True, gradient=(gx, gy, gz)).show(viewer='tachyon')8384A graph of two metaballs interacting with each other::8586sage: def metaball(x0, y0, z0): return 1 / ((x-x0)^2 + (y-y0)^2 + (z-z0)^2)87sage: implicit_plot3d(metaball(-0.6, 0, 0) + metaball(0.6, 0, 0), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=2)8889MANY MORE EXAMPLES:9091A kind of saddle::9293sage: implicit_plot3d(x^3 + y^2 - z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=0)9495A smooth surface with six radial openings::9697sage: implicit_plot3d(-(cos(x) + cos(y) + cos(z)), (x, -4, 4), (y, -4, 4), (z, -4, 4))9899A cube composed of eight conjoined blobs::100101sage: implicit_plot3d(x^2 + y ^2 + z^2 +cos(4*x)+cos(4*y)+cos(4*z)-0.2, (x, -2, 2), (y, -2, 2), (z, -2, 2))102103A variation of the blob cube featuring heterogeneously sized blobs::104105sage: implicit_plot3d(x^2 + y ^2 + z^2 +sin(4*x) + sin(4*y) + sin(4*z) -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))106107A klein bottle::108109sage: implicit_plot3d((x^2+y^2+z^2+2*y-1)*((x^2+y^2+z^2-2*y-1)^2-8*z^2)+16*x*z*(x^2+y^2+z^2-2*y-1), (x, -3, 3), (y, -3.1, 3.1), (z, -4, 4))110111A lemniscate::112113sage: implicit_plot3d(4*x^2*(x^2+y^2+z^2+z)+y^2*(y^2+z^2-1), (x, -0.5, 0.5), (y, -1, 1), (z, -1, 1))114115Drope::116117sage: implicit_plot3d(z - 4*x*exp(-x^2-y^2), (x, -2, 2), (y, -2, 2), (z, -1.7, 1.7))118119A cube with a circular aperture on each face::120121sage: implicit_plot3d(((1/2.3)^2 *(x^2 + y^2 + z^2))^-6 + ( (1/2)^8 * (x^8 + y^8 + z^8) )^6 -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))122123A simple hyperbolic surface::124125sage: implicit_plot3d(x*x + y - z*z, (x, -1, 1), (y, -1, 1), (z, -1, 1))126127A hyperboloid::128129sage: implicit_plot3d(x^2 + y^2 - z^2 -0.3, (x, -2, 2), (y, -2, 2), (z, -1.8, 1.8))130131Duplin cycloid::132133sage: implicit_plot3d((2^2 - 0^2 - (2 + 2.1)^2) * (2^2 - 0^2 - (2 - 2.1)^2)*(x^4+y^4+z^4)+ 2*((2^2 - 0^2 - (2 + 2.1)^2 )*(2^2 - 0^2 - (2 - 2.1)^2)* (x^2 * y^2+x^2 * z^2+y^2 * z^2))+2* 2^2 *((-0^2-2^2+2^2+2.1^2)* (2 *x *2+2* y* 0-2^2)-4*0 *2.1^2 *y)*(x^2+y^2+z^2)+ 4 * 2^4 * (2 *x+0 *y)* (-2^2+0 * y+2 * x)+4* 2^4 * 2.1^2 * y^2+2^8, (x, -2, 2.2), (y, -2, 2), (z, -1.3, 1.3))134135Sinus::136137sage: implicit_plot3d(sin(pi*((x)^2+(y)^2))/2 +z, (x, -1, 1), (y, -1, 1), (z, -1, 1))138139A torus::140141sage: implicit_plot3d((sqrt(x*x+y*y)-3)^2 + z*z - 1, (x, -4, 4), (y, -4, 4), (z, -1, 1))142143An octahedron::144145sage: implicit_plot3d(abs(x)+abs(y)+abs(z) - 1, (x, -1, 1), (y, -1, 1), (z, -1, 1))146147A cube::148149sage: implicit_plot3d(x^100 + y^100 + z^100 -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))150151Toupie::152153sage: implicit_plot3d((sqrt(x*x+y*y)-3)^3 + z*z - 1, (x, -4, 4), (y, -4, 4), (z, -6, 6))154155A cube with rounded edges::156157sage: implicit_plot3d(x^4 + y^4 + z^4 - (x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2))158159Chmutov::160161sage: implicit_plot3d(x^4 + y^4 + z^4 - (x^2 + y^2 + z^2-0.3), (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1.5, 1.5))162163Further Chutmov::164165sage: implicit_plot3d(2*(x^2*(3-4*x^2)^2+y^2*(3-4*y^2)^2+z^2*(3-4*z^2)^2) -3, (x, -1.3, 1.3), (y, -1.3, 1.3), (z, -1.3, 1.3))166167Clebsch::168169sage: implicit_plot3d(81*(x^3+y^3+z^3)-189*(x^2*y+x^2*z+y^2*x+y^2*z+z^2*x+z^2*y) +54*x*y*z+126*(x*y+x*z+y*z)-9*(x^2+y^2+z^2)-9*(x+y+z)+1, (x, -1, 1), (y, -1, 1), (z, -1, 1))170171Looks like a water droplet::172173sage: implicit_plot3d(x^2 +y^2 -(1-z)*z^2, (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1, 1))174175Sphere in a cage::176177sage: implicit_plot3d((x^8 + z^30 + y^8 - (x^4 + z^50 + y^4 -0.3))*(x^2 + y^2 + z^2 -0.5), (x, -1.2, 1.2), (y, -1.3, 1.3), (z, -1.5, 1.5))178179Ortho circle::180181sage: implicit_plot3d(((x^2 + y^2 - 1)^2 + z^2)* ((y^2 + z^2 - 1)^2 + x^2)* ((z^2 + x^2 - 1)^2 + y^2) - 0.075^2 *(1 + 3* (x^2 + y^2 + z^2)), (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1.5, 1.5))182183Cube sphere::184185sage: implicit_plot3d(12 - ((1/2.3)^2 *(x^2 + y^2 + z^2))^-6 - ( (1/2)^8 * (x^8 + y^8 + z^8) )^6, (x, -2, 2), (y, -2, 2), (z, -2, 2))186187Two cylinders intersect to make a cross::188189sage: implicit_plot3d((x^2 + y^2 - 1) * ( x^2 + z^2 - 1) - 1, (x, -3, 3), (y, -3, 3), (z, -3, 3))190191Three cylinders intersect in a similar fashion::192193sage: implicit_plot3d((x^2 + y^2 - 1) * ( x^2 + z^2 - 1)* ( y^2 + z^2 - 1) - 1, (x, -3, 3), (y, -3, 3), (z, -3, 3))194195A sphere-ish object with twelve holes, four on each XYZ plane::196197sage: implicit_plot3d(3*(cos(x) + cos(y) + cos(z)) + 4* cos(x) * cos(y) * cos(z), (x, -3, 3), (y, -3, 3), (z, -3, 3))198199A gyroid::200201sage: implicit_plot3d(cos(x) * sin(y) + cos(y) * sin(z) + cos(z) * sin(x), (x, -4, 4), (y, -4, 4), (z, -4, 4))202203Tetrahedra::204205sage: implicit_plot3d((x^2 + y^2 + z^2)^2 + 8*x*y*z - 10*(x^2 + y^2 + z^2) + 25, (x, -4, 4), (y, -4, 4), (z, -4, 4))206207TESTS:208209Test a separate resolution in the X direction; this should look like a210regular sphere::211212sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=(10, 40, 40), contour=4)213214Test using different plot ranges in the different directions; each215of these should generate half of a sphere. Note that we need to use216the ``aspect_ratio`` keyword to make it look right with the unequal217plot ranges::218219sage: implicit_plot3d(x^2 + y^2 + z^2, (x, 0, 2), (y, -2, 2), (z, -2, 2), contour=4, aspect_ratio=1)220221sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, 0, 2), (z, -2, 2), contour=4, aspect_ratio=1)222223sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, 0, 2), contour=4, aspect_ratio=1)224225Extra keyword arguments will be passed to show()::226227sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, viewer='tachyon')228229An implicit plot that doesn't include any surface in the view volume230produces an empty plot::231232sage: implicit_plot3d(x^2 + y^2 + z^2 - 5000, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)233234Make sure that implicit_plot3d doesn't error if the function cannot235be symbolically differentiated::236237sage: implicit_plot3d(max_symbolic(x, y^2) - z, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)238"""239240# These options aren't fully implemented yet:241# vertex_color: Either a single callable taking (x,y,z) and returning242# (r,g,b), or a triple of three callables. Not used for jmol. Note that243# Tachyon only lets you specify a single color for its triangles; this will244# be the mean of the three vertex colors of the triangle. If this is None245# (the default), we don't provide separate triangle colors to Tachyon.246247# These options, related to rendering with smooth shading, are irrelevant248# since IndexFaceSet does not support surface normals:249# smooth: (default: False) Whether to use vertex normals to produce a250# smooth-looking surface. False is slightly faster.251# gradient: (default: None) If smooth is True (the default), then252# Tachyon rendering needs vertex normals. In that case, if gradient is None253# (the default), then we try to differentiate the function to get the254# gradient. If that fails, then we use central differencing on the scalar255# field. But it's also possible to specify the gradient; this must be either256# a single python callable that takes (x,y,z) and returns a tuple (dx,dy,dz)257# or a tuple of three callables that each take (x,y,z) and return dx, dy, dz258# respectively.259260261G = ImplicitSurface(f, xrange, yrange, zrange, **kwds)262G._set_extra_kwds(kwds)263return G264265266267