Code covered by the BSD License  

Highlights from
Equally spaced structured Point Cloud Cuboid generation

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 1.32 KB File ID: #23551

Equally spaced structured Point Cloud Cuboid generation

by Jveer

 

04 Apr 2009

Simple function to generate equally spaced point cloud forming a cuboid

| Watch this File

File Information
Description

Inputs are vectors giving the x, y and z range. e.g x=1:100;y=40:70;z=80:250;

Apologies for lack of description but this submission is for a specific request. It is an extract from a much larger program.

% - B.I SOLID POINT CLOUD CUBOID
function [P]=Cuboid(x,y,z)
% - Generates coordinates for a solid cuboid composed of points

a=length(x);b=length(y);c=length(z); % assigning for coding simplicity
% B.I.1. Finding all x coordinates
xp=zeros(1,a*b*c); % preallocating
for nx=1:a
    if nx==1,xp(1:c*b)=(repmat(x(nx),1,c*b));
    else xp(((nx-1)*c*b)+1:nx*c*b)=repmat(x(nx),1,c*b);end
end
% B.I.2. Finding all y coordinates
yp=zeros(1,b*c); % preallocating
for ny=1:b
    if ny==1,yp(1:c)=repmat(y(ny),1,c);
    else yp(((ny-1)*c)+1:ny*c)=repmat(y(ny),1,c);
    end
end
yp=repmat(yp,1,a);
% B.I.3. Finding all z coordinates
zp=repmat(z,1,b*a);
% B.I.4. Coordinates of the cuboid generated.
P=[xp;yp;zp];
end % To Optimize

Required Products Communications Blockset
MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
07 Apr 2009 Jveer

Communications blockset not required! i don't remember putting that there!

if anyone manages to speed up this function, please let me know.

20 Jul 2010 Tilak Rajesh

Here is a faster version compared with the Cuboid()

%%
clear
for i=1:100
tic
[P]=Cuboid([-1:0.1:1],[-2:0.1:2],[-3:0.1:3]);
%plot3(P(1,:),P(2,:),P(3,:),'*')
toc
end
%Approx worst case: Elapsed time is 0.006052 seconds.
%%
clear
for i=1:100
tic
[x,y,z]=meshgrid([-1:0.1:1],[-2:0.1:2],[-3:0.1:3]);
x=reshape(x,[],1);y=reshape(y,[],1);z=reshape(z,[],1);
%plot3(x,y,z,'*')
toc
end
%Approx worst case: Elapsed time is 0.001026 seconds.

Both code produce the same result but using meshgird is much faster. I find it interesting as to how gather lots of points in the cloud, especially when there is OUT OF MEMORY issues.
for example: [x,y,z]=meshgrid([-1:0.001:1],[-1:0.001:1],[-1:0.001:1]); gives OUT OF MEMORY.
I am thinking of taking only X and Y using meshgrid and using this to create slices of the cube in the x-y plane. Any better ways to overcome the OUT OF MEMORY problems ?

13 Oct 2010 Jveer

@ Tilak

First, apologies for the late reply.

I disagree with your above comparison with meshgrid.

Consider the following:

------> Your Method
function [P]=Test

% Loading Pre-Point Generation Data
load Test.mat;

tic

% % Meshgrid
[xp,yp,zp]=meshgrid(xp,yp,zp);
xp=reshape(xp,[],1);yp=reshape(yp,[],1);zp=reshape(zp,[],1);
P=[xp(:),yp(:),zp(:)];

toc

end

----> Proof that the results are disimilar
>> P=Test;
Elapsed time is 0.247434 seconds.
>> load Test.mat;
>> tic;P1=Cuboid(xp,yp,zp);toc
Elapsed time is 0.769585 seconds.
>> isequal(P1',P)

ans =

     0

----> For similar results
function [P]=Test

% Loading Pre-Point Generation Data
load Test.mat;

tic

% % Meshgrid
[xp,yp,zp]=meshgrid(xp,yp,zp);
P=[xp(:),yp(:),zp(:)];
P=unique(P,'rows');

toc

end

---> Proof that Cuboid is faster
>> P=Test;
Elapsed time is 4.109277 seconds.
>> load Test.mat;
>> tic;P1=Cuboid(xp,yp,zp);toc
Elapsed time is 0.755397 seconds.
>> isequal(P1',P)

ans =

     1

----> Cuboid=mesgrid+unique ==> to generate a structured point cloud Cuboid is still way faster.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
point cloud Jveer 06 Apr 2009 10:08:42

Contact us at files@mathworks.com