Mathematica Graphics in the Internet:
Additional Lighting and Clipping in LiveGraphics3D 

Ralf Schaper
Universität Gesamthochschule Kassel

Juli 2001 (revised September 2001)

Introduction

Since 1997 Martin Kraus from Stuttgart develops LiveGraphics3D. This is a Java 1.1-Applet to display and to rotate three-dimensional graphics in HTML pages produced by Mathematica. The project is supported by Wolfram Research, especially in showing polytopes. There is also a short user manual. 
In his well written documentation Martin Kraus mentions two restrictions:
"LiveGraphics3D does not clip primitives which are outside of the PlotRange.                                               
In some situations the color of the wrong face of a polygon is used. The reason is the simple (but fast) algorithm being used to decide which face is painted." 
Two years ago I worked with LiveGraphics3D and I found some solutions. Now at last I will give a description.

Initialization

Don`t be alarmed that seven packages will be loaded now. Further on working with this notebook will become easier with these packages. Some hints about installation are given below. Only ExtendGraphics`Geometry3D` and LiveGraphics3D` are not part of the standard Mathematica packages.

Off[ General :: spell ];
Off[ General :: spell1 ];

    
Needs[ "ExtendGraphics`Geometry3D`" ];
Needs[ "Graphics`Animation`" ];
Needs[ "Graphics`Colors`" ];
Needs[ "Graphics`ParametricPlot3D`" ];
Needs[ "Graphics`Polyhedra`" ];
Needs[ "Graphics`Shapes`" ];
Needs[ "LiveGraphics3D`" ];

    
$DefaultFont  =  {"Times",  12};

Problem 1: Lighting

Take an icosahedron to get an impression of the problem of lighting "backward" faces of polygons and surfaces with LiveGraphics3D. When you have loaded the package Graphics`Polyhedra` you get an image of an icosahedron with the following expression:

gr1  =  
        Show[ Polyhedron[ Icosahedron ], 
          Boxed  ->  False ];

WriteLiveForm of LiveGraphics3D will produce the HTML-file. But you will see "shadows" on the backward faces.

WriteLiveForm[ "klafu1.m",  gr1 ];

Now you can look at the result.

You can look around the icosahedron from all directions with SpinShow from the package Graphics`Animation`:   Also the backward faces are colored:

animatgr1  =  
      SpinShow[ gr1,  
        SpinRange  ->  { 0 Degree,  
            360 Degree }, 
                                SpinDistance  ->  2, 
        SpinOrigin  ->  {0,  0,  
            0}, 
                                Frames  ->  24 ]

You may think that the wrong colors of the backward faces of the icosahedron may be due to the closed surface. But you get the same phenomenon with surfaces like the hyperbolic paraboloid with the simple equation  z = x·y .

gr2  =  
        Plot3D[ x y,  {x, -2, 2}, {y, -2, 
            2}, 
                        Axes  ->  None, PlotPoints  ->  7 ];

WriteLiveForm[ "klafu2.m",  gr2 ];

Now the LiveGraphics3D-version.

The faces from "below" are colored:

Show[ gr2,  
        ViewPoint  ->  {-0.5, 0,  -2} ];

Problem 2: Clipping

There are several possibilities in Mathematica to restrict values of functions or parts of the domain of definition, e.g. in the image gr2:

α  =  1.5;
gr3  =  
        Plot3D[ x y, {x, -2, 2}, {y, -2, 
            2}, 
                        PlotPoints  ->  7,  
          PlotRange  ->  {-α, α} ];

A little change and a restriction of the plot domain produce a more agreeable image:

Plot3D[ 
        x y,  {x, -2, 2}, {y, -2, 
          2},  
            PlotPoints  ->  
          7, 
            PlotRange  ->  {{-α, α},  {-α, α},  {-α, α}} ];

Axes -> None will suppress the ticks at the box. They are not needed for the transformation with LiveGraphics3D .

gr4  =  
        Plot3D[ x y,  {x, -2, 2}, {y, -2, 
            2}, 
                        PlotPoints  ->  7,  
          Axes  ->  
            None, 
                        PlotRange  ->  {{-α, α},  {-α, α},  {-α, α}} ];

WriteLiveForm[ "klafu3.m",  
        gr3 ];
WriteLiveForm[ "klafu4.m",  gr4 ];

In every case LiveGraphics3D does not produce the desired result:
klafu3.html
klafu4.html

I think that the following Mathematica figure will be adequate:

gr5  =  
        Show[ Graphics3D[ gr2 ],  
          PlotRange  ->  {-α, α} ];

Also transforming this figure with LiveGraphics3D gives not the desired result:

WriteLiveForm[ "klafu5.m",  gr5 ];

klafu5.html

A solution of problem 1 : Lighting

To switch on some "additional lights" gives an obvious solution. You will find some information about the option LightSources in the Mathematica Help-Browser. This option will be used in the next expressions. More information can be found in [Smith, Blachman, p. 254], [Wickham-Jones, p. 213] and [Schaper, p. 189] .

You can get information on the default LightSources:

Options[ Plot3D,  LightSources ]

LightSources -> {direction, color} produces parallel light with the direction direction and with the color color.
Now additional lightsources are used:

lights  =  {{{  1,   0,     1}, 
            Red  }, {{  1,    1,   1}, 
            Green}, 
                {{  0,    1,    1}, 
            Blue} ,  
                  (* additional: *) 
                 {{-1,   0, -1}, Red  }, {{-1, -1, -1}, 
            Green}, 
                 {{  0, -1, -1}, 
            Blue} 
                };

SetOptions[ Plot3D,  
        LightSources  ->  lights ];

As above the hyperbolic paraboloid and the icosahedron are used to give some explanations.

gr6 =  
        Plot3D[ x  y,  {x, -2, 2}, {y, -2, 
            2}, 
                       Axes  ->  None, 
          Boxed  ->  
            True,             
                       PlotPoints  ->  
            7 ];

Using an adequate choice of  ViewPoint you can look at the surface from "below".

Show[ gr6,  
        ViewPoint  ->  {-2, 0,  -3} ];

WriteLiveForm[ "klafu6.m", gr6 ];

Here you can look at the HTML-version. And all polygons are colored!

The icosahedron will be treated in an analogous manner :

gr7  =  
        Show[ Polyhedron[ Icosahedron ],  
          LightSources  ->  
            lights, 
                  Boxed  ->  False ];

WriteLiveForm[ "klafu7.m", gr7 ];

You can see the positive effect . Compare this example with the old one .

A solution of problem 2 : Clipping

Again we will begin with the hyperbolic paraboloid. The image gr3 is reproduced as gr8α :

gr8α  =  
        Graphics3D[ 
          Plot3D[ x y, {x, -2, 2}, {y, -2, 
              2}, 
                         Axes  ->  
              True, 
                         PlotPoints  ->  7,  
            PlotRange  ->  {-α, α} ]] ;

Now the surface is clipped by Clip3D from the package ExtendGraphics`Geometry3D` of  Tom Wickham-Jones:

gr8β =  
        Fold[  Clip3D[#1, #2] &, 
          gr8α,   
                    { Plane[{0, 0,   α}, {0,  
                0, -1}], 
                     Plane[{0, 0, -α}, {0,  
                0,   1}]
                  }];  
gr8γ  = 
        Show[ gr8β ,  Axes  ->  True ];

The option Axes will get the value None:

gr8  = 
        Show[ gr8γ ,  Axes  ->  None ];

WriteLiveForm[ "klafu8.m", gr8 ];

As you can see  LiveGraphics3D and the clipping work together. Since above the option LightSources of  Plot3D has been changed also the backward faces are colored. You get the same effect with the icosahedron. Now different colors for the lightsourses are chosen.

gr9  =  
        Show[ Polyhedron[ 
            Icosahedron ], 
                  LightSources  -> {{{1,    0,   1},  
                Red},  
                                                 {{  1,   1,    1}, 
                Green}, 
                                                 {{  0,   1,    1}, 
                Blue}, 
                                                 {{-1,   0, -1}, 
                Tomato}, 
                                                 {{  0, -1, -1}, 
                SkyBlueDeep}, 
                                                 {{-1, -1, -1}, 
                GreenDark}
                                                }, 
                  Boxed  ->  
            False ];

α  =  3/4;
gr10 =  
        Fold[ Clip3D[#1, #2] &, 
          gr9, 
                    {Plane[{0, 0,   α}, {0,  
                0, -1}], 
                    Plane[{0, 0, -α}, {0, 
                0,   1}]}];
Show[ gr10 ];

WriteLiveForm[ "klafu10.m", gr10 ];

Here you get the WWW images .

Applications

Klein Bottle

At the beginning three different forms of the Klein bottle are clipped. In the Mathematica-Book [1999] you will find the first form on page 995 resp. in the Help Browser at Light Source Variations.   [Schaper, S. 257, ff.]  gives  parametrisations of the first and second form. You can get the parametrisations of the second and third form in from this page . This page of the Help Browser of Mathematica gives supplementary information on the second form. Also see [Gray, p. 327].
(Inserted in September 2001: The parametrisation of the second form can be found in Dieudonné on page 192 resp. Seite 194. See also: The Mathematica Journal, 1, (3), 1991, p. 65.)

First form:

bx  = 6 Cos[u] (1 + Sin[u]);
by  = 16 Sin[u];
rad = 4 - 2 Cos[u];

X = If[ Pi < u <= 2 Pi, bx + rad Cos[v + Pi], 
                        bx + rad Cos[u] Cos[v] ];
Y = If[ Pi < u <= 2 Pi, by, by + rad Sin[u] Cos[v] ];
Z = rad Sin[v];

SetOptions[ ParametricPlot3D,  
        LightSources  ->  lights];

gr11 = ParametricPlot3D[ {X, Y, Z}, {u, 0, 2π}, {v, 0, 2π},
         PlotPoints -> {48, 12}, Axes -> False,
         Boxed -> False, ViewPoint-> {0,-2,-2}];

WriteLiveForm[ "klafu11.m", gr11 ];

klafu11.html

If v ∈ [0, π]  you get this "insight":

gr12  = 
        ParametricPlot3D[ {X, Y, Z}, {u, 0, 2  π}, {v, 
            0, π}, 
         PlotPoints -> {48, 12}, Axes -> False, 
          Boxed -> 
            False, 
         ViewPoint -> {0, -2, -2}];

WriteLiveForm[ "klafu12.m", gr12 ];

klafu12.html

Now another clipping:

gr13  =  
        Fold[ Clip3D[#1, #2] &, 
          gr11, 
                     {Plane[{0, -10, -1}, {4, 0, -1}]}];
Show[ gr13,  
        ViewPoint  ->  {-1.5, 0, 0} ];

WriteLiveForm[ "klafu13.m", gr13 ];

klafu13.html

Second form:

X = (2 + Cos[u/2] Sin[v] - Sin[u/2] Sin[2  v]) Cos[
            u];
Y = (2 + Cos[u/2] Sin[v] - Sin[u/2] Sin[2  v]) Sin[
            u];
Z = Sin[u/2] Sin[v] + Cos[u/2] Sin[2  v];

gr14 = 
        ParametricPlot3D[ {X, Y, Z}, {u, 0, 2  π}, {v, 0, 
            2  π}, 
             PlotPoints -> 51, Boxed -> False, 
          Axes -> None ];

WriteLiveForm[ "klafu14.m", gr14 ];

klafu14.html

Now the clipping with two planes:

gr15α  = 
        Fold[ Clip3D[#1, #2] &, 
          gr14, 
                    {Plane[{0.5, 0, 0.4}, {-0.5, 
                0.25, -1}], 
                    Plane[{    0,  0, 
                0.8}, {       0,        0, -1}]}];
gr15  = Show[ gr15α ];

WriteLiveForm[ "klafu15.m", gr15 ];

klafu15.html

Third form:

A polynomial representation of the Klein bottle due to Ian Stewart can be found on this web page . The equation looks odd":

(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) == 0

You can get the following part of the Klein bottle  with the package  ImplicitPlot3D and the following Mathematica expression. Attention please: The computation needs a lot of time!

klafu16.html

klafu18.html

Fermat

At MathSource there is a notebook of Andrew J. Hanson: FermatSolution.nb .
"Solution of Fermat's Equation ((Subscript[z, 1])^n) + ((Subscript[z, 2])^n) = 1
The notebook shows a projection from four-dimensional space of the so-called projective variety that represents all possible solutions of the equation (x^n) + (y^n) == (z^n) for varying n. What Fermat's Last Theorem states is that none of these solutions can correspond to integer values of x, y and z. "

Look also at the figures on page 39 of the second edition of the Mathematica-Book. Having done some calculations not incoorperated in this notebook, it is possible to work with the following expressions:

(*
gr19 = Show[ Graphics3D[
Table[surface[k1, k2], {k1, 1, n}, {k2, 1, n}]],
		Boxed -> False,
		LightSources-> lights ];
 *)

klafu19.html

You will get more "insight" after clipping:

klafu21.html

Outlook

Some years ago I explored the following surface. At the first glance it seems to be boaring. But there are "inner values" which can be seen when the graphic is rendered or after an adequate clipping. The changed options of lightsources are used.

SetOptions[ Graphics3D, Boxed -> False ];

gr22α = ParametricPlot3D[
           {u Cos[v] Sin[u], u Cos[u] Cos[v],-u Sin[v] }, 
           {u, 0, 3Pi, Pi/18}, {v, 0, 2Pi, Pi/18},
            Boxed -> False, Axes -> False ];

Remembering abraded shells on the shore I took the following choice of PlotRange:

gr22β  = 
        Show[ gr22α, 
          PlotRange -> {All, {-0.5, 10}, All} ];

gr22γ  =  
      Fold[ Clip3D[#1, #2] &, 
        gr22α,  
                         {Plane[{0, -0.5, 0}, {0, 1, 
              0}], 
                        Plane[{0, 1.5 π, 0}, {0, -1, 
              0}]
                       }]; gr22δ  = Show[ gr22γ ];

WriteLiveForm[ "klafu20.m", gr22γ ];

klafu20.html

gr22δ  =  
      Fold[ Clip3D[#1, #2] &, 
        gr22α,  
                         {Plane[{0, -0.7, 0.5}, {-0.2, 
              1, -1.5}], 
                         Plane[{0, -0.5,     0}, {        0, 
              1,       0}]
                       }]; gr22  = Show[ gr22δ ];

WriteLiveForm[ "klafu22.m", gr22 ];

klafu22.html

I think that Michael Trott is the author of the following Mathematica-Oneliner. 
SetOptions[ Graphics3D, Boxed -> False ] is needed:

gr23α  = 
        With[{stellDode = 
              Stellate@
                Delete[Dodecahedron[], 
                  6]},  
       Show@ Graphics3D@{EdgeForm@Thickness@0.001, 
                Stellate[
                  OpenTruncate@Geodesate[stellDode, 3, {0, 0, 0}, 1.6], 
                  1.05],  
    Stellate@OpenTruncate@
                    Geodesate[stellDode, 3]}];

Clipping and lighting is applied to this object:

gr23β  =  
        Fold[ Clip3D[#1, #2] &, 
          gr23α, 
                       {Plane[{0,   0.15, 0}, {0, -1,  
                0}], 
                       Plane[{0, -0.15, 0}, {0,   1, 0}]}];
gr23  =  
        Show[ gr23β,  
          Boxed  ->  
            False, 
           PlotRange  ->  All,  
          ViewPoint  -> {0, -3, 
              0}, 
           LightSources ->  lights ];

WriteLiveForm[ "klafu23.m", gr23 ];

klafu23.html

Now the hole is filled:

Needs[ "Graphics`Shapes`" ];
 gr24α  = 
        Graphics3D[ Sphere[0.98, 32, 16 ] ];
Show[ gr24α ];

There is a remark on "placing several lightsources at the same point" in [ Smith, Blachman, p.260]. That will produce "more intense illumination".

gr25  =  
        Show[{gr23, gr24α},  
          Boxed  ->  
            False, 
ViewPoint  ->  {0, -3, 
              0},  
 
          LightSources ->  
                  {{{   1,   0,    1}, 
                Gold}, 
                  {{   0,   1,    1}, 
                Gold}, 
                  {{   1,   1,    1}, 
                Yellow}, 
                  {{   1,   1,    1}, 
                Red}, 
                  {{   1,   0,    1}, 
                Gold}, 
                  {{   0,   1,    1}, 
                Gold}, 
                  {{   1,   1,    1}, 
                Gold}, 
                  {{ -1,   0, -1}, 
                Yellow},  
                  {{   0, -1, -1}, 
                Yellow}, 
                  {{ -1, -1, -1}, 
                Yellow},    
                  {{ -1, -1, -1}, 
                Red}   
                 }];

WriteLiveForm[ "klafu25.m", gr25 ];

klafu25.html

Let the sun shine bright over the conferences at Klagenfurt.

Hints for the Installation

You have to set the right path in the package LiveGraphics3D` to save the HTML-files.

In his book Tom Wickham-Jones describes a Mathematica-package for additional clipping routines of 3D-graphics. 
You can get the packages on MathSource with this URL:
http://www.mathsource.com/Content/Enhancements/Graphics/3D/0208-976

Our notebook needs only the packages Geometry.m, Geometry3D.m, NonConvexTriangulate.m and SimpleHull.m . Work will be more easy if the mentioned files are loaded in a directory also named ExtendGraphics in the directory Autoload in AddOns:
E.g.:  Mathematica/4.1/AddOns/Autoload/ExtendGraphics.
You can find the following example in the book of Wickham-Jones ( p. 440 ). You may proceed in the following manner after loading the mentioned packages:

Needs[ "ExtendGraphics`Geometry3D`" ]

surf = 
        Graphics3D[ 
          Plot3D[Sin[x y], 
       {x, -Pi, 
              Pi}, {y, -Pi, 
              Pi}, 
        PlotPoints -> 
              30 ]];

?Plane

gr26α  = 
        Fold[ Clip3D[#1, #2] &, 
          surf,  
            {Plane[{0, 0, 
                0.5}, {0, 
                0, -1}], 
            Plane[{0, 0, -0.5}, {0, 0, 1}]}];

Show[ gr26α ];

End of the citation.

gr26  =  
        Show[ gr26α, Boxed  ->   False,  
          Axes  ->  None,    DisplayFunction  ->  
            Identity ];

WriteLiveForm[ "klafu26.m", gr26 ];

klafu26.html

At last a variation with a "sloping" plane:

gr27α =  
      Fold[ Clip3D[#1, #2] &, 
        surf,  
           {Plane[{π/2, 0, 
              0}, {-1, 1, -1}]}]; 
    gr27  = Show[ gr27α,  Boxed  ->  False,  
        Axes  ->  None ];

WriteLiveForm[ "klafu27.m", gr27 ];

klafu27.html

Sources

Books

Jean Dieudonné
Treatise on Analysis, volume III
New York: Academic Press, 1972

Jean Dieudonné
Grundzüge der modernen Analysis, Band 3
Braunschweig: Vieweg, 1976

Alfred Gray
Modern Differential Geometry of Curves and Surfaces with Mathematica 
Boca Raton: CRC Press, 1998, 0-8493-7164-3

Ralf Schaper
Grafik mit Mathematica 
Bonn: Addison-Wesley, 1994, 3-89319-612-9

Cameron Smith, Nancy Blachman
The Mathematica Graphics Guidebook
Reading: Addison Wesley, 1995, 0-201-53280-8

Tom Wickham-Jones
Mathematica Graphics
New York: Springer, 1994, 0-387-94047-2
http://www.mathsource.com/Content/Enhancements/Graphics/3D/0208-976

Stephen Wolfram
The Mathematica Book, 2nd ed.
Redwood City: Addison-Wesley, 1991, 0-201-51502-4

Stephen Wolfram
The Mathematica Boook, 4th ed.
Champaign: Wolfram Media, 1999, 1-57955-004-5

URLs

Fermat

ImplicitPlot3D

Klein Bottle

LiveGraphics3D

HomePage                  
Documentation          
Examples 
Two Surfaces   
            
Polyhedra Interactive
Uniform Polyhedra