dgouder
dgouder

Reputation: 339

3D grid with spheres in each cell

I am attempting to create a diagram similar to this.

enter image description here

A diagram of a 3D grid with spheres in each cell. I would like to use matlab, with the slight different being that I want the spheres to have a particular image on them.

Is this possible and if so what would be a good starting point?

Upvotes: 2

Views: 369

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Here is how I would do it using bubbleplot3 from the File Exchange to plot an array of evenly spaced spheres, in addition to using warp to display an image as a texture-mapped surface for your spheres. Basically create the spheres with bubbleplot3 and then fetch each individual surface object in a for loop and call warp on them to replace their texture map with an image.

For generating the gridded cube I have no credit whatsoever and slightly modified @Raf's answer here.

clc;clear;close all

%// Read image
C = flipud(imread('peppers.png'));

%// Generate array of spheres
Radius = 1;
[x,y,z] = meshgrid(0:2:4,0:2:4,0:2:4);
r=repmat(Radius,1,numel(x));

%// Call bubbleplot3
hBubble = bubbleplot3(x,y,z,r,[],[],[],[]);

hold on

%// Get surfaces objects
SurfHandles = findobj('type','surface');

%// Use warp function to replace colordata with image
for k = 1:numel(SurfHandles)
    warp(SurfHandles(k).XData,SurfHandles(k).YData,SurfHandles(k).ZData,C)
end

%// Now cube.
%// Credit to Raf here: https://stackoverflow.com/questions/7309188/how-to-plot-3d-grid-cube-in-matlab
CubeData = -1:2:5;
[X, Y] = meshgrid(CubeData,CubeData);                         
x = [X(:) X(:)]';                                
y = [Y(:) Y(:)]';
z = [repmat(CubeData(1),1,length(x)); repmat(CubeData(end),1,length(x))];
col = 'b';

plot3(x,y,z,col,'Color','k');                                         
plot3(y,z,x,col,'Color','k');
plot3(z,x,y,col,'Color','k');

rotate3d on

And output:

enter image description here

Upvotes: 4

Related Questions