kenes
kenes

Reputation: 133

Plotting multiple 3D rectangles using patch in Matlab

I have x, y, and z coordinates of multiple rectangles' corners. All coordinates are in one matrix; ordered as x, y, z. Every three columns contain one rectangle's four corner coordinates. I want to show all rectangles in one plot. However, it does not show any of rectangles. Here is my code:

%Coordinates(1,3*i-2:3*i) = top left corners' x y z coordinates
%Coordinates(2,3*i-2:3*i) = down left corners' x y z coordinates
%Coordinates(3,3*i-2:3*i) = down right corners' x y z coordinates
%Coordinates(4,3*i-2:3*i) = top right corners' x y z coordinates

This code works fine... [~,c] = size(coordinates); for i = 1:3:c patch(coordinates(:,i),coordinates(:,i+1),coordinates(:,i+2)) hold on end

Upvotes: 2

Views: 1576

Answers (2)

embert
embert

Reputation: 7592

If I'm right, you're looking for somthing like the following solution. The idea is taken from Amro's answer here.

Idea

The code first creates vectors with start and stop vertices. It then creates a matrix holding lines with the start vertex, stop vertex and inserts a nan line. Then it uses patch to plot a surface and makes the face invisible. Note, that for reeeaally many vertices, this "wastes" some memory for the nans but the patch command is quite fast, as it creates only one object.

Code

% Sample coordinates
coord = [ 1  -1.3  -1;...
         -1  -1.3  -1;...
         -1  -1.3   1;...
          1  -1.3   1;...
          1   1.3  -1;...
         -1   1.3  -1;...
         -1   1.3   1;...
          1   1.3   1;...
         -1.3   1  -1;...
         -1.3  -1  -1;...
         -1.3  -1   1;...
         -1.3   1   1;...
          1.3   1  -1;...
          1.3  -1  -1;...
          1.3  -1   1;...
          1.3   1   1;...
          1    -1  -1.3;...
         -1    -1  -1.3;...
         -1     1  -1.3;...
          1     1  -1.3;...
          1    -1   1.3;...
         -1    -1   1.3;...
         -1     1   1.3;...
          1     1   1.3];

nlines = size(coord, 1);
% Vectors for vertices
X = zeros(2, nlines);
Y = zeros(2, nlines);
Z = zeros(2, nlines);
C = zeros(1, nlines);
% One iteration per vertex
for ii = 1:nlines
    % Here comes the edge back to the first vertex
    if mod(ii,4) == 0
        X(1, ii) = coord(ii, 1);
        Y(1, ii) = coord(ii, 2);
        Z(1, ii) = coord(ii, 3);

        X(2, ii) = coord(ii-3, 1);
        Y(2, ii) = coord(ii-3, 2);
        Z(2, ii) = coord(ii-3, 3);

    % Here come all other edges
    else
        X(1, ii) = coord(ii, 1);
        Y(1, ii) = coord(ii, 2);
        Z(1, ii) = coord(ii, 3);

        X(2, ii) = coord(ii+1, 1);
        Y(2, ii) = coord(ii+1, 2);
        Z(2, ii) = coord(ii+1, 3);
    end
    % One color for each rectangle
    C(ii) = floor((ii-1)/4);
end
% Insert nans between lines
X(end+1, :) = nan;
Xf = X(:);
Y(end+1, :) = nan;
Yf = Y(:);
Z(end+1, :) = nan;
Zf = Z(:);
% Setup patch matrix
p = [Xf, Yf, Zf];                   
% Prepare color matrix
r = repmat(C.', 1, 3)';
clr = r(:);
% Make a figure
f = figure;
% Plot patch
surface(p(:,[1 1]), p(:,[2 2]), p(:,[3 3]), [clr clr], ...
    'EdgeColor',    'flat', ....
    'FaceColor',    'None')

grid on
view([55, 36]);

xlabel('X')
ylabel('Y')
zlabel('Z')

Plot

enter image description here

Upvotes: 2

Ander Biguri
Ander Biguri

Reputation: 35525

I am still dot understand fully how your data is saved, but let me try with what I understood:

[~,c] = size(coordinates);
hold on
for ii = 1:1:c/4
    patch(coordinates(1+(ii-1)*4:4+(ii-1)*4,1),coordinates(1+(ii-1)*4:4+(ii-1)*4,2),coordinates(1+(ii-1)*4:4+(ii-1)*4,3));
end

If this does not work, please post an example of your data so I can actually try it.

NOTE: do not use i as variable name in Matlab, as it is the imaginary unit.

Upvotes: 0

Related Questions