How to compute a weighted mean between two polygons?

6 visualizaciones (últimos 30 días)
Abb
Abb el 24 de Jun. de 2023
Comentada: Mathieu NOE el 6 de Jul. de 2023
I am going to compute a weighted mean for two polygons with X Y Z (mat file attached). My purpose: I plan to have a polygon resulting from both polygons. These are the weights for each polygon.
W1 = 63.799;
W2 = 60.959;
  4 comentarios
John D'Errico
John D'Errico el 24 de Jun. de 2023
Editada: John D'Errico el 25 de Jun. de 2023
I'm sorry, but this makes no sense that I can see, as you are asking it.
Suppose we have two polygons.
P1 = polyshape([0 0 1 1],[0 1 1 0]); % A simple unit square
P2 = polyshape([-1 2 -1],[-2 -2 1]); % a triangle
Utterly boring things as they are. I can even plot them.
plot(P1)
hold on
plot(P2)
Now, what would you expect as the result of a weighted mean of the two? How would a weighted mean of P1 and P2 be different with weights of [1,1], versus weights of [2,1]?
What does a mean of two polygons mean anyway? Then you can explain how a weighted mean is a different thing.
Anyway, in this simple case, what would you expect to see? I see your comment, that you don't know how to write the code, but before you write ANY code at all, you need ot know what that code should do. And you have not explained anything about that. So what would you expect, even in the trivially simple case I have shown, as a result?
(I suppose, one could decide to form a linear mapping, where we form a linear combination of any point in polygon 1, and in some way, a corresponding point in polygon 2. But that fails to answer the question of how you know which points correspond in that mapping. Without that information, the process seems meaningless. Another vague possibility is to compute a linear combination of any point in polygon1 plus EVERY point in polygon 2. That is, take all possible combinations thereof between two points in each of the two polygons. So, are you effectively asking to compute a (weighted) Minkowski sum of two polyhedra?)
For example, the Minkowski sum of the two polygons I show above is:
Computed using my utility minkowskisum. It can be found on the file exchange. However, it is not of any use for a weighted mean. Nor would it apply to a 3-d polyhedron. Could you write a Minkowski sum in 3-d? Well, yes. I'm still not at all sure how the weights would apply there, nor what they even mean. But if you can compute the sum, then a weighted sum must still apply.
Abb
Abb el 26 de Jun. de 2023
Editada: Abb el 27 de Jun. de 2023
@John D'Errico thanks John, and thanks for your explanantion. sorry for asking unclear question. I sorted polygons coordinates based on the polar coordinate (see attached mat files). I have multiple polygons (I brought two polygons as an example). These polygons overlapped each other. I have a weight value for each polugon.I'm looking for a way to compute a final polygon according weight averaging between these polygons? is that possible?

Iniciar sesión para comentar.

Respuestas (1)

Mathieu NOE
Mathieu NOE el 27 de Jun. de 2023
Editada: Mathieu NOE el 27 de Jun. de 2023
hello
maybe this ? (nothing fancy)
as W1 and W2 are almost the same , the resulting average curve (black dashed ) is half way between P1 and P2
load('P1.mat');
x1 = polygon1(:,1);
y1 = polygon1(:,2);
load('P2.mat');
x2 = polygon2(:,1);
y2 = polygon2(:,2);
% average polygon
W1 = 63.799;
W2 = 60.959;
[x_aw,y_aw] = average_curve(x1,y1,W1,x2,y2,W2);
plot(x1,y1,'b',x2,y2,'r',x_aw,y_aw,'k--')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xn,yn] = average_curve(x1,y1,w1,x2,y2,w2)
theta_new = linspace(-pi,pi,360);
[r1_new,centroid_x1,centroid_y1] = convert(x1,y1,theta_new);
[r2_new,centroid_x2,centroid_y2] = convert(x2,y2,theta_new);
% averaging (according to weigths) the radii
r_awg = (r1_new*w1 + r2_new*w2)/(w1 + w2);
% averaging (according to weigths) the centroids
centroid_x_awg = (centroid_x1*w1 + centroid_x2*w2)/(w1 + w2);
centroid_y_awg = (centroid_y1*w1 + centroid_y2*w2)/(w1 + w2);
% convert back to cartesian
[xn,yn] = pol2cart(theta_new,r_awg);
% add back centroid info
xn = xn + centroid_x_awg;
yn = yn + centroid_y_awg;
end
function [r_new,centroid_x,centroid_y] = convert(x,y,theta_new)
%% method 1
centroid_x = mean(x);
centroid_y = mean(y);
[theta,r] = cart2pol(x-centroid_x,y-centroid_y);
% sort theta in ascending order
[theta,ind] = sort(theta);
r = r(ind);
% remove duplicates
[theta,IA,IC] = unique(theta);
r = r(IA);
r_new = interp1(theta,r,theta_new);
end
  2 comentarios
Abb
Abb el 6 de Jul. de 2023
Editada: Abb el 6 de Jul. de 2023
@Mathieu NOE Thank you for providing the code. Regrettably, it did not yield the desired results. However, I took the initiative to write an alternative code based on this below example that proved to be effective.@John D'Errico thanks alot, John for your help and explanation. In fact, finding a polygon with maximum vertices and the interpolation of the vertices helped me.
polygon1 = [1 1; 2 3; 4 2; 3 1];
polygon2 = [6 1; 7 3; 8 2; 7 1];
polygon3 = [4 4; 5 5; 6 4; 5 3];
weights = [2, 3, 1];
maxVertices = max([size(polygon1, 1), size(polygon2, 1), size(polygon3, 1)]);
polygon1 = interpolateVertices(polygon1, maxVertices);
polygon2 = interpolateVertices(polygon2, maxVertices);
polygon3 = interpolateVertices(polygon3, maxVertices);
meanPolygon = zeros(size(polygon1));
totalWeight = sum(weights);
for i = 1:numel(weights)
meanPolygon = meanPolygon + (weights(i) / totalWeight) * eval(strcat('polygon', num2str(i)));
end
meanPolygon = [meanPolygon; meanPolygon(1,:)];
hold on;
plot(polygon1(:,1), polygon1(:,2), 'r');
plot(polygon2(:,1), polygon2(:,2), 'g');
plot(polygon3(:,1), polygon3(:,2), 'b');
plot(meanPolygon(:,1), meanPolygon(:,2), 'k', 'LineWidth', 2);
legend('Polygon 1', 'Polygon 2', 'Polygon 3', 'Mean Polygon');
axis equal;
hold off;
function interpolatedPolygon = interpolateVertices(polygon, numVertices)
if size(polygon, 1) < numVertices
interpolatedPolygon = interp1(1:size(polygon, 1), polygon, linspace(1, size(polygon, 1), numVertices));
else
interpolatedPolygon = polygon;
end
end
Mathieu NOE
Mathieu NOE el 6 de Jul. de 2023
ok
I tried to provide a solution acording to your initial data (P1 and P2.mat)
this is a different scenario
glad you coud find a viable solution

Iniciar sesión para comentar.

Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by