How to check if different objects are already linked?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hey guys,
I have following problem:
I created a kind of voronoi mesh and I now know the position of all vertices and which vertices belong to which polygon.
I know want to treat the vertices and the polygons as classes with handels and link them, so that each vertice knows which are its adjacend vertices and its adjacend polygons and each polygon knows which are the adjacend polygons and the vertices that define their borders.
My classes look like following:
classdef Vertex < handle
properties
position;
neighbour_vertex;
adjacend_polygon;
end
methods
function v = Vertex(pos)
v.position = pos;
v.neighbour_vertex = Vertex.empty(0,0);
v.adjacend_polygon = Polygon.empty(0,0);
end
% link vertex to vertex
function link_v2v(v,neighbour)
v.neighbour_vertex(end+1)=neighbour;
neighbour.neighbour_vertex(end+1)=v;
end
end
end
classdef Polygon < handle
properties
rho
adjacend_vertex;
adjacend_polygon;
end
methods
function p = Polygon(rho)
p.rho=rho;
p.adjacend_vertex = Vertex.empty(0,0);
p.adjacend_polygon = Polygon.empty(0,0);
end
% link polygon to vertex
function link_v2p(p,vertex)
for i=1:length(vertex)
p.adjacend_vertex(end+1)=vertex(i);
vertex(i).adjacend_polygon(end+1)=p;
end
end
% link polygon to edge
% link polygon to polygon
function link_p2p(p)
for i=1:length(p.adjacend_vertex)
for j=1:length(p.adjacend_vertex(i).adjacend_polygon)
if p.adjacend_vertex(i).adjacend_polygon(j)==p || p.adjacend_vertex(i).adjacend_polygon(j)==p.adjacend_polygon(end)
continue
else
p.adjacend_polygon(end+1)=p.adjacend_vertex(i).adjacend_polygon(j);
end
end
end
end
end
end
I linked all object stogether as follows:
pos = vertices;
vertex = Vertex.empty(size(pos,1),0);
polygon=Polygon.empty(size(polygons,1),0);
for i = 1: size(pos,1)
vertex(i) = Vertex(pos(i,:));
end
for i=1:length(edge)
v1=edge(i,1);
v2=edge(i,2);
link_v2v(vertex(v1),vertex(v2));
end
rhos=10^12*random('unif',0.9,1.1,length(polygons),1);
for i=1:size(polygons,1)
polygon(i)=Polygon(rhos(i));
end
for i=1:size(polygons,1)
link_v2p(polygon(i),vertex(polygons{i}));
end
for i=1:length(polygon)
link_p2p(polygon(i));
end
But it doesnt work as planned. Since I find the polygons that are neighbours of a specific polygon, by looking for the already correctly linked vertices of that specific polygon and by looking for the adjacend polygons of those vertices, I double link them.... and since the polygon itsself is a adjacend polygon of its own vertices I link the polygon to itsself which I also do not want.
I got rid of the last point by the first expression in the if statement in the link p2p function in the polygon class, but the second statement that should prevent the double linking does not work...
How can i check if an object that i want to link to another object is already linked to that object?
Best regards
Marc
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!