How to create a random 3d point cloud
Show older comments
Hello
I would like to create a random 3D pointcloud (in the pointcloud format) and then fit a 3D curve (spline) passing through it (randomly).
How is it possible ?
Please help me.
Thank You.
8 Comments
Andreas Dorner
on 8 May 2019
Probably not what you want, but random:
size = 10;
nPoints = 1000;
vars = rand(nPoints, 3)*size;
pc = pointCloud(vars)
julio cruz
on 13 Apr 2020
Is there a way to create a random 3D point cloud but have it be within some other point cloud that has the shape of a cube?
darova
on 13 Apr 2020
Can you make a sketch or drawing?
julio cruz
on 14 Apr 2020
Edited: darova
on 14 Apr 2020
Attached is the cuboid created in matlab and the data points used. Also, here is the code i used:
dataset = xlsread('data.xlsx','Sheet1','a1:c25');
x = dataset(:,1);
y = dataset(:,2);
z = dataset(:,3);
t=[x y z];
ptCloud = pointCloud(t);
pcshow(ptCloud)
darova
on 14 Apr 2020
I draw your data

So you want to draw random 3D curve. What happens if the curve hits the boundaries of this 'cube'?
julio cruz
on 15 Apr 2020
the curve cannot go past the boundaries of the cube
darova
on 15 Apr 2020
I think it's too complicated

If it was a 2D dimensional case i'd just use inpolygon to check if curve is inside the box. But it's 3D
I can't handle it
Do you have any ideas?
Image Analyst
on 15 Apr 2020
Surprised no one is trying spline(). ?
Answers (1)
Image Analyst
on 15 Apr 2020
Try using spline() on each of x, y, and z separately:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
data = xlsread('data.xlsx')
x = data(:, 1);
y = data(:, 2);
z = data(:, 3);
hFig1 = figure;
plot3(x, y, z, 'b.-', 'MarkerSize', 20, 'LineWidth', 2);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
zlabel('z', 'FontSize', fontSize);
grid on;
hFig1.WindowState = 'maximized';
% Now spline fit 1000 points in between.
hFig2 = figure;
xq = linspace(1, length(x), 1000);
xs = spline(1:length(x), x, xq);
yq = linspace(1, length(y), 1000);
ys = spline(1:length(y), y, yq);
zq = linspace(1, length(z), 1000);
zs = spline(1:length(z), z, zq);
% Plot them.
subplot(2, 1, 1);
plot(1 : length(x), x, 'r.', 'MarkerSize', 20);
hold on;
plot(1 : length(y), y, 'g.', 'MarkerSize', 20);
plot(1 : length(z), z, 'b.', 'MarkerSize', 20);
title('Original X, Y, and Z Values', 'FontSize', fontSize);
legend('x', 'y', 'z');
subplot(2, 1, 2);
plot(1 : length(xs), xs, 'r-', 'LineWidth', 2);
hold on;
plot(1 : length(ys), ys, 'g-', 'LineWidth', 2);
plot(1 : length(zs), zs, 'b-', 'LineWidth', 2);
grid on;
title('Spline Fits', 'FontSize', fontSize);
legend('x', 'y', 'z');
% Plot back on the main figure in red.
figure(hFig1);
hold on;
plot3(xs, ys, zs, 'r-', 'LineWidth', 2);
% Clip all values to no lower than 0.
xs = max(0, xs);
ys = max(0, ys);
zs = max(0, zs);
% Clip all values to no higher than 1.
xs = min(1, xs);
ys = min(1, ys);
zs = min(1, zs);
% Plot clipped range back on the main figure in magenta.
plot3(xs, ys, zs, 'm-', 'LineWidth', 2);
legend('Original Knots', 'Spline Fit', 'Clipped');
fprintf('Done running %s.m ...\n', mfilename);
In the plot below, the original data is in blue, the spline fit is in red, and the curve clipped to the small box with 0-to-1 range is in magenta:


Categories
Find more on Point Cloud Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!