How to create a random 3d point cloud
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
darova
2020-4-15
I think it's too complicated
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/284649/image.png)
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?
回答(1 个)
Image Analyst
2020-4-15
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/284750/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/284751/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!