TransWikia.com

Sphere[] mesh/wireframe using Graphics3D

Mathematica Asked by Fidel I. Schaposnik on March 4, 2021

When using ParametricPlot3D to plot a sphere, we get a nice mesh / wireframe for it:

ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 0, [Pi]}, {p, 0, 2 [Pi]}]

Sphere with mesh, using ParametricPlot3D

Is there a way to get a similar result using the (much more efficient) Sphere[] primitive? Simply doing

Graphics3D@Sphere[]

produces instead

Sphere without a mesh, using Graphics3D

5 Answers

Is there a way to get a similar result using the (much more efficient) Sphere[] primitive?

No, the internal discretization is not accessible. But you can get something similar, with a trick, and assuming that ...

I'm not picky about which mesh it chooses

DiscretizeRegion[Sphere[],
 PrecisionGoal -> 1, MaxCellMeasure -> 0.01,
 MeshCellStyle -> {{2, All} -> None, {1, All} -> Black}
]

enter image description here

Adjust the MaxCellMeasure to get a finer/coarser discretization. PrecisionGoal -> 1 is there to allow very coarse discretizations.

This is not the discretization that is used to render Sphere[], but it is efficient, and you said that is what you wanted.

Correct answer by Szabolcs on March 4, 2021

Head@ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 0, [Pi]}, {p, 0, 2 [Pi]}]

(* Graphics3D *)

makeDashed[ g_Graphics3D ] := g /. l_Line :> {Dashed, l}

  makeDashed[ ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 0, [Pi]}, {p, 0, 2 [Pi]}] ]

Dashed sphere grid

You may give the gridlines on the sphere in ParametricPlot3D the Opacity You like

Head@Sphere[]

(* Sphere *)

Nice example about the powerful ParametricPlot3D from FaceForm

ParametricPlot3D[{Cos[[Phi]] Sin[[Theta]], 
  Sin[[Phi]] Sin[[Theta]], Cos[[Theta]]}, {[Phi], 0, 
  3 Pi/2}, {[Theta], 0, Pi}, MeshStyle -> Opacity[.2], 
 PlotStyle -> FaceForm[Blue, Orange]]

Opened sphere

As long as default settings are used, no PlotPoints, MaxRecursion, WorkingPrecision, PrecisionGoal, Precision and Accuracy, Tolerance, both representations are of equal performance. ParametricPlot3D is much more versatile.

The built-in Sphere belongs to a set of Graphics3D primitives set included to compability reasons. This include Cube, Sphere, Parallelepiped, ... They are collected on SolidGeometry. Sphere is identical to Ball. All these work with Lighting

An almost white sphere with ParametricPlot can be achieved with:

ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 
   0, [Pi]}, {p, 0, 2 [Pi]}, Lighting -> {"Ambient", White}, 
  PlotStyle -> GrayLevel[.995], PlotTheme -> None]

white sphere

This can be made transparent:

ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 
  0, [Pi]}, {p, 0, 2 [Pi]}, Lighting -> {"Ambient", White}, 
 PlotStyle -> {Opacity[.5], GrayLevel[.995]}, PlotTheme -> None]

enter image description here

and colored arbitrarily:

ParametricPlot3D[{Sin[t] Cos[p], Sin[t] Sin[p], Cos[t]}, {t, 
  0, [Pi]}, {p, 0, 2 [Pi]}, Lighting -> {"Ambient", White}, 
 PlotStyle -> {Opacity[.5], Directive[Black, Glow[Red]]}, 
 PlotTheme -> None]

red sphere

This can then be shown together using Overlay, Inset, Epilog or Show together with the nice Sphere built-in.

Answered by Steffen Jaeschke on March 4, 2021

We can use RegionMember to view the structure of Sphere[]

RegionMember[Sphere[], {x, y, z}]

(x | y | z) ∈ Reals && x^2 + y^2 + z^2 == 1

and then use ContourPlot3D to make Mesh

Answered by cvgmt on March 4, 2021

You can construct 3D mesh lines using GeometricTransformation of two great circles:

ClearAll[sphereMesh]
sphereMesh[n_: 40, m_: 40, opts : OptionsPattern[]] := 
  Module[{tr1 = AffineTransform[{{{Sin @ #, 0, 0}, {0, Sin @ #, 0}, {0, 0, 1}}, 
      {0, 0, Cos @ #}}] & /@ Subdivide[0, 2 Pi, n],
    tr2 = RotationTransform[#, {0, 0, 1}] & /@ Subdivide[0, 2 Pi, m], 
    cp = Join[#, {First @ #}] &[CirclePoints[50]], cxy, cyz},
  cxy = Line[Append[0] /@ cp];
  cyz = Line[Insert[0, 1] /@ cp];
  Graphics3D[{Gray, MapThread[GeometricTransformation, {{cxy, cyz}, {tr1, tr2}}]}, 
  Boxed -> False, opts]]

Examples:

sphereMesh[]

enter image description here

Show[sphereMesh[50, 20],
  Graphics3D[{[email protected] , LightGreen, Sphere[]}], ImageSize -> Large]

enter image description here

Answered by kglr on March 4, 2021

Why not SphericalPlot3D[1, [Theta], [Phi]]enter image description here?

Answered by mjw on March 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP