Code covered by the BSD License  

Highlights from
binaryTensorVoxel

Be the first to rate this file! 14 Downloads (last 30 days) File Size: 211 KB File ID: #43481
image thumbnail

binaryTensorVoxel

by

 

Draw a 3D voxel representation of a binary tensor or 3D matrix.

| Watch this File

File Information
Description

A binary tensor may also be referred to as a 3D matrix of zeros and non-zeros or a 3D logical array. binaryTensorVoxel will draw cubes where there are non-zero entries in a tensor and leave vacancies where there are zeros. A simple use case example is

t = round(rand(3,3,3)); % zeros and ones
binaryTensorVoxel(t);

The thumbnail image for this File Exchange entry was drawn by applying binaryTensorVoxel to a 3D cellular automaton.

Required Products MATLAB
MATLAB release MATLAB 8.2 (R2013b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
13 Sep 2013 Sven

Hey Paul, GREAT idea.
You're gonna hate me for this though: it really slows down for large matrices.

The big speed hit comes from multiple calls to patch() within a loop.

I put together a more vectorised version that pre-calculates vertices/faces, then just calls patch the once. Here it is (all squashed up) below. It results in only one patch object, but it is ~1000x faster for a 20x20x20 binary matrix.

dfltfaces = [...
1 2 3 4; % front
5 6 7 8; % back
4 3 7 6; % top
1 5 8 2; % bottom
1 4 6 5; % left
2 8 7 3];
dfltvertices = [...
1 0 0;
1 1 0;
1 1 1;
1 0 1;
0 0 0;
0 0 1;
0 1 1;
0 1 0];
dfltcdata = [1 2 3 2 0 1 2 1]';

allInd = find(t);
n = numel(allInd);
[allI,allJ,allK] = ind2sub(size(t),allInd);
scaledvertices = bsxfun(@times, dfltvertices, a);
offsetamount = permute([allI,allJ,allK] - 1 - 0.5, [3 2 1]);
offsetvertices = bsxfun(@plus, scaledvertices, offsetamount);
offsetfaces = bsxfun(@plus, dfltfaces, reshape(8 * (0:n-1), 1,1,[]));
fv = struct(...
'faces', reshape(permute(offsetfaces,[1 3 2]),[],4,1),...
'vertices', reshape(permute(offsetvertices,[1 3 2]),[],3,1),...
'faceVertexCData', repmat(dfltcdata,n,1));

13 Sep 2013 Sean de Wolski

Nice job Paul, I like it.

One thing I was expecting to work that didn't though is to be able to have different side lengths of the voxel, e.g:

binaryTensorVoxel(rand(3,3,3)>0.8,[0.5 0.5 1])

This is common in three dimensional imaging where the voxels have equal side lengths in row/column but the slices are at a different depth, rendering the voxel in a brick shape.

Contact us