Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3D plot of solid object built from tiny cubes

Subject: 3D plot of solid object built from tiny cubes

From: Bas

Date: 19 Sep, 2008 11:21:52

Message: 1 of 3

Hi group,

I am simulation a solid object that is contained within a 3D matrix.
The shape of the object is defined by a logical array, while I would
typically want to plot the temparature contained in a second matrix of
the same shape.

As an example, suppose you I have the data defined by
[X,Y,Z] = meshgrid(-10:10);
R = sqrt(X.^2+Y.^2+Z.^2);
inside = R < 8;
C = rand(size(X));
In this case I would want to plot a solid sphere defined by inside,
built up of tiny cubes that each have a color given by C.

One solution might be to plotting all the surfaces with the 'slice'
function and then setting all the surfaces outside the object to
transparent. Another option might be to figure out which blocks are on
the outside of the object and only plot the visible edges using
tetramesh or something. Both solutions would be possible, but would
take quiet some time to implement. I was looking for a standard matlab
solution or something in the file exchange, but I wouldn't know for
which terms to search. Any suggestions?

Thanks,
Bas

Subject: 3D plot of solid object built from tiny cubes

From: Gwendolyn Fischer

Date: 22 Sep, 2008 06:40:08

Message: 2 of 3

Hi Bas,

> I am simulation a solid object that is contained within a 3D matrix.
> The shape of the object is defined by a logical array, while I would
> typically want to plot the temparature contained in a second matrix of
> the same shape.
>
> As an example, suppose you I have the data defined by
> [X,Y,Z] = meshgrid(-10:10);
> R = sqrt(X.^2+Y.^2+Z.^2);
> inside = R < 8;
> C = rand(size(X));
> In this case I would want to plot a solid sphere defined by inside,
> built up of tiny cubes that each have a color given by C.
>

Take a look at isosurface, it might not be exactly what you where looking for, but it comes quite close and is ready to use:

isosurface(X,Y,Z,R,8,C)

Bye

Gwen

Subject: 3D plot of solid object built from tiny cubes

From: Bas

Date: 24 Sep, 2008 13:38:22

Message: 3 of 3

> > I am simulation a solid object that is contained within a 3D matrix.
> > The shape of the object is defined by a logical array, while I would
> > typically want to plot the temparature contained in a second matrix of
> > the same shape.
>
> > As an example, suppose you I have the data defined by
> > [X,Y,Z] = meshgrid(-10:10);
> > R = sqrt(X.^2+Y.^2+Z.^2);
> > inside = R < 8;
> > C = rand(size(X));
> > In this case I would want to plot a solid sphere defined by inside,
> > built up of tiny cubes that each have a color given by C.

On Sep 22, 8:40 am, "Gwendolyn Fischer"
<gwendolyn.fisc...@nowhere.com> wrote:
> Take a look at isosurface, it might not be exactly what you where looking for, but it comes quite close and is ready to use:
> isosurface(X,Y,Z,R,8,C)

Thanks, that seems to give a result in the given example of a perfect
square. I am, however, interested in the more general case of a
complex shape described by a logical matrix as I described before and
I don't think that isosurface would work then. I ended up writing my
own function, which was easier than I thought using patch. I am
posting it below in the hope that someone finds it useful. To plot the
example given above use:
x=-10.5:1:10.5; y=x; z=x; plotCubes(x,y,z,inside,C)

Bas


function plotCubes(x,y,z,V,C)
%function plotCubes(x,y,z,V,C)
%makes 3D plot of shape defined by boolean 3d-matrix V, with the
color
%the small cubes given by the similar shaped matrix C
%x, y and z are vectors indicating the edge locations, so their
lengths
%should be one larger than the corresponding dimension of the matrices

[nx,ny,nz] = size(V);

if length(x) ~= nx + 1 | length(y) ~= ny + 1 | length(z) ~= nz + 1
    error('Length of x, y and z must be one larger than size of V and
C')
end

dc = 0.02 * (max(C(V)) - min(C(V))); %small color difference to create
3D effect

%-X
ilin = find(cat(1, V(1,:,:), ~V(1:end-1,:,:) & V(2:end,:,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = repmat(x(ix), 4, 1);
py = [y(iy); y(iy); y(iy+1); y(iy+1)];
pz = [z(iz); z(iz+1); z(iz+1); z(iz)];
c = C(ilin);
%-Y
ilin = find(cat(2, V(:,1,:), ~V(:,1:end-1,:) & V(:,2:end,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, repmat(y(iy), 4, 1)];
pz = [pz, [z(iz); z(iz+1); z(iz+1); z(iz)]];
c = [c, C(ilin) + dc];
%-Z
ilin = find(cat(3, V(:,:,1), ~V(:,:,1:end-1) & V(:,:,2:end)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, [y(iy); y(iy+1); y(iy+1); y(iy)]];
pz = [pz, repmat(z(iz), 4, 1)];
c = [c, C(ilin) - dc];
%+X
ilin = find(cat(1, V(1:end-1,:,:) & ~V(2:end,:,:), V(end,:,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, repmat(x(ix+1), 4, 1)];
py = [py, [y(iy); y(iy); y(iy+1); y(iy+1)]];
pz = [pz, [z(iz); z(iz+1); z(iz+1); z(iz)]];
c = [c, C(ilin)];
%+Y
ilin = find(cat(2, V(:,1:end-1,:) & ~V(:,2:end,:), V(:,end,:)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, repmat(y(iy+1), 4, 1)];
pz = [pz, [z(iz); z(iz+1); z(iz+1); z(iz)]];
c = [c, C(ilin) + dc];
%+Z
ilin = find(cat(3, V(:,:,1:end-1) & ~V(:,:,2:end), V(:,:,end)))';
[ix,iy,iz] = ind2sub([nx,ny,nz], ilin);
px = [px, [x(ix); x(ix); x(ix+1); x(ix+1)]];;
py = [py, [y(iy); y(iy+1); y(iy+1); y(iy)]];
pz = [pz, repmat(z(iz+1), 4, 1)];
c = [c, C(ilin) - dc];

figure;
patch(px,py,pz,c)
axis equal vis3d
shading flat; %dc can be set to zero when using faceted

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us