       Re: Raising Contour Plot Graphics to 3D

• To: mathgroup at smc.vnet.net
• Subject: [mg37323] Re: Raising Contour Plot Graphics to 3D
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Wed, 23 Oct 2002 02:57:07 -0400 (EDT)
• References: <ap0821\$c6n\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```David,
A more considered proposal.
The idea is that when we find the graphics version of the ContourGraphics
object the lines and polygons are rendered with the later ones over the
earlier ones.
We can simulate this in the 3D version by pulling the coresponding 3D
objects towards the view point by small amount, increasing as we go along
the list.

Here is a rough implementation:

Get the contour plot:

cplot = ContourPlot[x*y, {x, -3, 3}, {y, -3, 3},
ColorFunction -> Hue];

Convert to graphics objects

grs = Graphics[cplot][];

Make the affine transformation

grs3D1 = Graphics3D[grs] /. {(x_)?NumericQ, y_} :>
{2*x + y, -x + 2*y, -1.5*x + y};

Display with Graphics3D options as required:

grs3D2 = Show[grs3D1, Lighting -> False,
ViewPoint -> {1, -2, 2}];

Find the view point used - it might be the current default value and so not
explicit in the preceding.

vp = ViewPoint /. AbsoluteOptions[grs3D2, ViewPoint]

{1.,-2.,2.}

The order that the polygons and lines appear in grs is the order in which
they will be rendered, the later ones on top of the earlier ones.
Simulate this in three dimensions: Lift the polygons and lines slightly
towards the view point the later ones more than the later ones (strictly, we
should get the viewpoint in user coordinates, but with such small
adjustments this may not be necessary)

grs3D2 = (ph = 0.; lh = 1.; grs3D1 /.
{pl_Polygon :> (pl /. {x_, y_, (z_)?NumericQ} ->
{x, y, z} + vp*(ph++/100000)),
ln_Line :> (ln /. {x_, y_, (z_)?NumericQ} ->
{x, y, z} + vp*(ph++/100000))});

Show the result

Show[grs3D2, Lighting -> False];

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"David Park" <djmp at earthlink.net> wrote in message
news:ap0821\$c6n\$1 at smc.vnet.net...
> Dear MathGroup,
>
> I would like to raise the graphics generated by a contour plot to 3D. But
> there is a problem. Here is a simple example. Here I make a simple contour
> plot. I then have two cases of converting the graphics to 3D. In the first
> case I just keep the surface in the xy-plane, simply adding a 0 z
> coordinate. In the second case I do a simple affine transformation. In
both
> cases some of the contour regions are improperly rendered. Notice that the
> ContourGraphics has to be first converted to Graphics.
>
> cplot = ContourPlot[x y, {x, -3, 3}, {y, -3, 3}, ColorFunction -> Hue];
>
> cgraphics3d =
>     First[Graphics[cplot]] /. {x_?NumericQ, y_?NumericQ} -> {x, y, 0};
>
> cgraphics3d =
>     First[Graphics[cplot]] /. {x_?NumericQ,
>           y_?NumericQ} -> {2x + y, -x + 2y, -1.5x + y};
>
> Show[Graphics3D[
>       {cgraphics3d},
>       Lighting -> False,
>       ImageSize -> 450]];
>
> The reason that this problem occurs is that Mathematica does not draw
> nonintersecting Polygons for each region, but instead will overlay smaller
> regions on top of larger regions. If we look at the Polygons that
> ContourPlot produces, after being converted to Graphics, we see that each
> one goes to a corner of plot domain.
>
> Cases[First[Graphics[cplot]], _Polygon, Infinity]
>
> The result is that when the graphics are converted to 3D, with slight
> numerical errors, perhaps in the rendering, some of the Polygons can
> interlace and produce an incorrect plot.
>
> Is there any remedy for this problem?
>
> David Park
> djmp at earthlink.net