generating Co-ordinates from a convex hull or 3d shape

조회 수: 17 (최근 30일)
Jonathan McGuckin
Jonathan McGuckin 2020년 3월 20일
답변: Prabhan Purwar 2020년 3월 27일
I have data points x,y,z and i would like to first of all, generate a convex hull of these points, then i would like to take that convex hull and generate (x,y,z) cooridnates within the convex hull.
I have tried to use a few different methods including taking linspace and then doing inpolygon or inhull and taking only the coordinates within the shape. The problem with this method is that i need a certain amount of coordinates. I am able to generate a convex hull from the data points, but i am struggling with coordinates part.
Does anyone have any ideas how i could complete this? thanks in advance

답변 (1개)

Prabhan Purwar
Prabhan Purwar 2020년 3월 27일
Hi,
The following code illustrates the generation of required number of points inside a convex hull.
It makes use of convhull() function to find the convex hull and then generates random points that are finally verified to reside under the convexhull using the inhull() function from File Exchange.
You may adjust the "number of points" and "radius" according to your needs.
clc
close all
clear
%% Load data
[x,y,z] = meshgrid(-2:1:2,-2:1:2,-2:1:2);
x = x(:);
y = y(:);
z = z(:);
%% Findind convex hull
[k1,av1] = convhull(x,y,z);
trisurf(k1,x,y,z,'FaceColor','cyan');
hold on
scatter3(x(k1(:,1)),y(k1(:,2)),z(k1(:,3)))
%% Generation of random points
n=3; %dimension
m=1000; %number of points
r=2; %radius
u=x(k1(:,1));
v=y(k1(:,2));
w=z(k1(:,3));
c(1)=(min(u)+max(u))/2; %coordinates of the Centre of a sphere
c(2)=(min(v)+max(v))/2;
c(3)=(min(w)+max(w))/2;
X = randn(m,n);
s2 = sum(X.^2,2);
X = X.*repmat(r*(gammainc(s2/2,n/2).^(1/n))./sqrt(s2),1,n);
X(:,1:n)=X(:,1:n)+c(1,1:n);
hold on;
scatter3(X(:,1),X(:,2),X(:,3))
%% Tests for points to lie inside Convex Hull
xyz=[x y z];
in = inhull(X,xyz,k1);
X_new=[];
for i=1:m
if in(i)==1
X_new(end+1,:)=X(i,:);
end
end
%% Picking up N number of points randomly
N=20;
a=randperm(length(X_new),N);
output=X_new(a,:); % Final Output
hold on
scatter3(output(:,1),output(:,2),output(:,3))
Output:
Refer to the following links:

카테고리

Help CenterFile Exchange에서 Bounding Regions에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by