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

5 views (last 30 days)
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

Answers (1)

Prabhan Purwar
Prabhan Purwar on 27 Mar 2020
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:

Categories

Find more on Bounding Regions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!