필터 지우기
필터 지우기

How to compute a weighted mean between two polygons?

조회 수: 2 (최근 30일)
Abb
Abb 2023년 6월 24일
댓글: Mathieu NOE 2023년 7월 6일
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
John D'Errico
John D'Errico 2023년 6월 24일
편집: John D'Errico 2023년 6월 25일
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 2023년 6월 26일
편집: Abb 2023년 6월 27일
@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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Mathieu NOE
Mathieu NOE 2023년 6월 27일
편집: Mathieu NOE 2023년 6월 27일
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
Abb
Abb 2023년 7월 6일
편집: Abb 2023년 7월 6일
@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 2023년 7월 6일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by