images on surfaces

August 17 2012 acer 10036
Maple

13

Let's see how we can display patterns, or even images, on 3D plot surfaces. Here's a simple example.

The underlying mechanism is the COLOR() component of a POLYGONS(), GRID(), or MESH() piece of a PLOT3D() data structure. (See here, here, and here for some older posts which relate to that.)

The data stored in the MESH() of a 3D plot structure can be a list-of-lists or, more efficient, an Array. The dimensions of that Array are m-by-n-by-3 where m and n are usually the size of the grid of points in the x-y plane (or of points in the two independent parameter spaces). In modern Maple quite a few kinds of 3D plots will produce a GRID() or a MESH() which represent the m-by-n independent data points that can be controlled with the usual grid=[m,n] option.

The plot,color help-page describes how colors may specified (for each x-y point pair to be plotted) using a procedure f(x,y). And that's fine for explicit plots, though there are some subtleties there. What is not documented on that help-page is the possibility of efficiently using an m-by-n-by-3 or an m*n-by-3 datatype=float[8], order=C_order Array of RGB values or am m*n float[8] Vector of hue values to specify the color data. And that's what I've been learning about, by experiment.

A (three-layer, RGB or HSV) color image used by the ImageTools package is also an m-by-n-by-3 Array. And all these Arrays under discussion have m*n*3 entries, and with either some or no manipulation they can be interchanged. I wrote earlier about converting ImageTools image structures to and from 2D density-plots. But there is also an easy way to get a 3D density-plot from an ImageTools image with a single command. That command is ImageTools:-Preview, and it even has a useful options to rescale. The rescaling is often necessary so that the dimensions of the COLOR() Array in the result match the dimensions of the grid in the MESH() Array.

For the first example, producing the banded torus above, we can get the color data directly from a densityplot, without reshaping/manipulating the color Array or using any ImageTools routines. The color data is stored in a m*n Vector of hue values.

But first a quick note: Some plots/plottools commands produce a MESH() with the data in a list-of-lists-of-lists, or a POLYGONS() call on a sequence of listlists (eg. `torus` in Maple 14). For convenience conversion of the data to a 3-dimensional Array may be done. It's handy to use `op` to see the contents of the PLOT3D() structure, but a possible catastrophe if a huge listlist gets printed in the Standard GUI.

restart:
with(ImageTools):with(plots):with(plottools):
N:=128:

d:=densityplot((x,y)->frem((x-2*y),1/2),0..1,0..1,
                      colorstyle=HUE,style=patchnogrid,grid=[N,N]):
#display(d);

c:=indets(d,specfunc(anything,COLOR))[1];

                         /     [ 1 .. 16384 Vector[column] ]\
                         |     [ Data Type: float[8]       ]|
               c := COLOR|HUE, [ Storage: rectangular      ]|
                         \     [ Order: C_order            ]/

T:=display(torus([0,0,0],1,2,grid=[N,N]),
           style=surface,scaling=constrained,axes=none,
           glossiness=0.7,lightmodel=LIGHT3):
#op(T); # Only view the operands in full with Maple 16!

# The following commands both produce the banded torus.

#op(0,T)(MESH(op([1,1..-1],T),c),op([2..-1],T)); # alternate way, M16 only

subsop([1,1]=[op([1,1],T),c][],T);

Most of the examples in this post use the command `op` or `indets` extract or replace the various parts of of the strcutures. Perhaps in future there could be an easy mechanism to pass the COLOR() Array directly to the plotting commands, using their `color` optional parameter.

In the next example we'll use an image file that is bundled with Maple as example data, and we'll use it to cover a sphere. We won't downsize the image, so that it looks sharp and clear (but note that this may make your Standard GUI session act a bit sluggish). Because we're not scaling down the image we must specify a grid=[m,n] size in the plotting command that matches the dimensions of the image. We'll use ImageTools:-Preview as a convenient mechanism to produce both the color Array as well as a 3D densityplot so that we can view the original image. Note that the data portion of the sphere plot structure is an m-by-n-by-3 Array in a MESH() which matches the dimensions of the m-by-n-by-3 Array in the COLOR() portion of the result from ImageTools:-Preview.

restart:
with(ImageTools):with(plots):with(plottools):
im:=Read(cat(kernelopts(mapledir),"/data/tree.jpg")):

p:=Preview(im):

op(1,p);

                 /                    [ 235 x 354 2-D  Array ]  
                 |                    [ Data Type: float[8]  ]  
             GRID|0 .. 266, 0 .. 400, [ Storage: rectangular ], 
                 \                    [ Order: C_order       ]  

                    /     [ 235 x 354 x 3 3-D  Array ]\\
                    |     [ Data Type: float[8]      ]||
               COLOR|RGB, [ Storage: rectangular     ]||
                    \     [ Order: C_order           ]//

q:=plot3d(1, x=0..2*Pi, y=0..Pi, coords=spherical, style=surface,
          grid=[235,354]):

display(PLOT3D(MESH(op([1,1],q), op([1,4..-1],p)), op(2..-1,q)),
           orientation=[-120,30,160]);

A fun exercise would be to get a public domain image of the earth and cover a sphere with it. (I'm not sure whether it'd be easier to use a rectilinear map projection and a cylinder which is then transformed to a sphere, or a Mercator-like projection which shows the poles if you can find such. Distortion at the poles, due to transforming, can be an issue.)

The next example covers a torus, but this time as a tiling. It might be very nice to have a `Tile` command, which  rescales an image and copies it into the color Array M times across and N times down, given a particular grid size of the plot structure. Ideally it would allow for portrait or landscape orientation.

restart:
with(ImageTools):with(plots):with(plottools):
im:=Read(cat(kernelopts(mapledir),"/data/tree.jpg")):

p:=Preview(im,40,60):

q:=display(torus([0,0,0],1,2,grid=[160,240]),
           style=surface,scaling=constrained,axes=none):

qc:=Array(1..160,1..240,1..3,datatype=float[8],order=C_order):
for i from 1 to 4 do
  for j from 1 to 4 do
    qc[(i-1)*40+1..i*40,(j-1)*60+1..j*60,1..3]:=op([1,4,2],p);
end do: end do:

final := subsop([1,1]=[op([1,1],q),COLOR(RGB,qc)][],q):
display( final, orientation=[-90,-50,-180]);

Another possibility is to take an image, apply ImageTools:-Preview to obtain a 3D densityplot (lying in the z=0 plane), and then finally apply plottools:-transform to lift that up onto what would otherwise be a function's normally plotted surface.

This is only scratching the surface of what's possible, if you'll pardon the pun. Please feel free to add to the gallery in Comments.

acer

Please Wait...