Looking for a way to partition an alphaShape object between its respective alphaShape sub-regions.

6 visualizaciones (últimos 30 días)
I have an alphaShape object (shp) made of 11 distinct sub-regions (N=11).
I want to partition my alphaShape between it's 11 respective sub-regions.
I want to plot each region in a different color and compute its respective area.
x and y are the coordinates of shp.
For n=1, I compute the x-y coordinates of the sub-region (x1 & y1) , make a new alphaShape (shp1) and compute its area (area1).
I redo these steps for n=2,3,...,N.
The file data.mat attached contains the variables shp, x and y.
Here what it looks like for n=1:
Load('data.mat')
N = numRegions(shp); %# of regions in shp
%For n=1
tf1 = inShape(shp,x,y,1); %true (1) for points in region n=1
index1 = find(tf1==1); %corresponding index for region n=1
x1 = x(index1); %corresponding x-coord for region n=1
y1 = y(index1); %crresponding y-coord for region n=1
shp1 = alphaShape(x1,y1,1); %create new alphaShape object for region n=1 with alpha radius 1
area1 = area(shp1); %area for alphaShape object for region n=1
%Redo the steps for n=2,3,...,N
It works but I feel like I am doing a lot of unnecassary steps.
I am looking for a clever way to use the sub-regions of an alphShape object as distinct alphaShape objects on their own.
Thanks

Respuestas (1)

Milan Bansal
Milan Bansal el 3 de Abr. de 2024 a las 10:58
Hi A LL
To streamline the process of partitioning your alphaShape object into its respective sub-regions, plotting each in a different color, and computing their areas, you can utilize MATLAB's capabilities more efficiently. The approach you've outlined does repeat several steps that can be optimized. Here's a more efficient way to achieve your goal:
  1. Use "inShape" more efficiently: "inShape" is used to determine which points belong to which region. However, please avoid calling "find" by directly using logical indexing with "tf1" to extract x and y coordinates.
  2. Loop through all regions: Instead of manually repeating the steps for each region, use a loop to iterate over all regions.
  3. Plotting: To plot each region in a different color, use MATLAB's built-in color maps or specify a list of colors.
Please refer to the below code snippet for a more efficient code:
load('data.mat');
N = numRegions(shp); % Number of regions in shp
areas = zeros(N, 1); % Pre-allocate array to store areas
figure;
hold on;
% Colors for plotting each region
colors = lines(N); % This generates a colormap with N distinct colors
for n = 1:N
tf = inShape(shp, x, y, n); % Logical array for points in region n
% Extracting x and y coordinates for region n using logical indexing
xn = x(tf);
yn = y(tf);
shpn = alphaShape(xn, yn, 1); % Create new alphaShape object for region n with alpha radius 1
areas(n) = area(shpn); % Compute and store area for region n
% Plot region n in a different color
plot(shpn, 'FaceColor', colors(n,:), 'FaceAlpha', 0.5, 'EdgeColor', 'none');
end
hold off;
% Optionally, print areas
disp('Areas of each sub-region:');
Areas of each sub-region:
disp(areas);
308.5000 176.5000 65.5000 52.5000 28.5000 23.5000 5.5000 3.5000 1.0000 1.0000 0.5000
Please refer to the following documentation links to learn more about "inShape", "alphaShape" and "lines" fucntions.
Hope this helps!

Categorías

Más información sobre Bounding Regions en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by