Silhouette outline of polygon surface

조회 수: 13 (최근 30일)
Sven
Sven 2013년 3월 20일
I'm trying to get the outline of a triangulated 3D surface projected onto the XY plane. Here's an example. First, let's get a triangulated faces/vertices surface into FV:
xyz = [5, 2, 6, 0, 3; 0, 2, 4, 2, 1; 5, 6, 6, 7, 9]';
tri = [1, 2, 4; 1, 5, 4; 1, 2, 5; 2, 3, 5; 2, 4, 3; 3, 4, 5] ;
FV = struct('faces',tri,'vertices',xyz);
figure, patch(FV,'FaceColor','c','FaceAlpha',0.95), hold on
Now, if this surface were convex, I could simply take the convex hull of the XY coordinates of all the outline outXY points:
k = convhull(FV.vertices(:,1:2));
outXY = FV.vertices(k,1:2);
plot(outXY(:,1),outXY(:,2),'.r-','LineWidth',6), axis equal
However, of course, I want to work with non-convex surfaces :) Notice the little patch of white where the convex hull didn't follow the "outline" of my blue patch?
I can imagine a relatively simple hack, where I would just make a solid colour patch similar to this figure, take a screenshot using getframe() then use bwboundaries() on a segmentation of the figure window image.
I'm just trying to work out if there's a nifty trick that would provide an exact result. Votes will be given for good ideas about the approach, even if no code is involved. For instance, the freeBoundary method of delaunay triangulations might be related here, but I can't work out how to reduce my 3d triangulation into a 2d triangulation (which will have overlapping triangles) in a way that freeBoundary() is actually useful.
Note, for the moment, let's just say that I'm not interested in internal holes, such that a doughnut surface would simply return the outline of the outer circle (similar to the 'noholes' option of bwboundaries).
Thanks, Sven.
  댓글 수: 6
Sven
Sven 2013년 3월 21일
편집: Sven 2013년 3월 21일
Well, I've got one solution which is a brute force union of all the triangles in the surface, using Polygon Clip, which is mexified so quite fast. I would venture to guess that there's a more optimal way to do this that doesn't involve touching all the triangles one at a time, so I'm still open to suggestions :) A good thing is that using Polygon Clip you get to track holes for free!
% Get a full list of triangular facets as a 3-by-2-by-N matrix
facets = FV.vertices(:,1:2)';
facets = permute(reshape(facets(:,FV.faces'), 2, 3, []),[2 1 3]);
% Turn each facet into a structure acceptable by PolygonClip
facetsCell = num2cell(facets,1);
facetsStruct = squeeze(struct('x',facetsCell(:,1,:),'y',facetsCell(:,2,:)));
% Start with the first facet, then UNIONIZE! (power to the people)
P = PolygonClip(facetsStruct(1),facetsStruct(2),3);
for i = 2:length(facetsStruct)
P = PolygonClip(P,facetsStruct(i),3);
end
outXY = [P(1).x(:) P(1).y(:)];
outXY = outXY([1:end 1],:);
figure, patch(FV, 'FaceColor','g'), view(3), camlight, hold on
minZ = min(FV.vertices(:,3));
plot3(outXY(:,1), outXY(:,2), ones(size(outXY,1),1)*minZ, '.r-'), axis image
Cedric
Cedric 2013년 3월 21일
편집: Cedric 2013년 3월 21일
Another brute-force solution that I had in mind was to compute intersects of all edges of the projection (which isn't too complicated), add these points to the set of vertices, and keep the envelope of the set of all points.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2013년 3월 21일
Here is what I came up with:
%%Sean's try
% Here's the general approach:
%
% # Calculate the Delaunay Triangulation in three dimensions to create
% tetrahedra forming the shape.
% # Use the x and y coordinates of this triangulation to build two
% dimensional polygons irrespective of Z.
% # Calculate the union of all of these two dimensional polygons to get the minimal
% bounding polygon.
%
% 03/21/2013
%%Original Data
xyz = [5, 2, 6, 0, 3; 0, 2, 4, 2, 1; 5, 6, 6, 7, 9]';
tri = [1, 2, 4; 1, 5, 4; 1, 2, 5; 2, 3, 5; 2, 4, 3; 3, 4, 5] ;
FV = struct('faces',tri,'vertices',xyz);
%%Delaunay Triangulation
tri3 = triangulation(tri,xyz);
%%Engine:
% Disable warning since we don't need anything depending on CW v. CCW
warning('off','map:polygon:noExternalContours');
% Initialize final polygon vectors
px = [];
py = [];
% Unionize each polygon with the group using |polybool()| from Mapping
% Toolbox
for ii = 1:size(tri3.ConnectivityList,1)
[px, py] = polybool('union',...
tri3.Points(tri3.ConnectivityList(ii,:),1),...
tri3.Points(tri3.ConnectivityList(ii,:),2),...
px,py);
end
%%Visualize and Compare:
figure;
patch(FV,'FaceColor','c','FaceAlpha',0.95);
line(px,py,zeros(size(px)),'color','r','LineWidth',3)
view(2); % How did we do?
title('Woohoo!');
  댓글 수: 5
Sean de Wolski
Sean de Wolski 2013년 3월 22일
편집: Sean de Wolski 2013년 3월 22일
Also, as far as having all of this knowledge about the structure, I was originally wondering about using this to do a constrained Delaunay triangulation forcing us to only get the triangles that make up this z-buffered shape.
After a playing with it and thinking about it more, I became more and more convinced that it would never work.
Sven
Sven 2013년 3월 22일
1) Yes, you're right. I considered this and thought it might simply choose non-connected CH points where connected points might exist... but I guess it might actually pick the "wrong" vertex that happens to still be connected to a different part of the CH that might screw things up... checking would be required.
2) True... but I couldn't wrap my head around how to use FB
3) You missed 3)
4) But the thing is, all vertices on a (manifold, not-screwed-up) 3D triangular patch are surface polygons. The fact that they may not be surface polygons in a 2D projection is something different, and not revealed by the freeBoundary() of the 3D triangulation. And to "do" the 2D triangulation, you can't just do a delaunayTriangulation of the projected vertices (since it will triangulate everything up to a convex hull even though the 3D patch may not have been convex).
5) Prossibly...
By now it's theoretical... boolean approach is sufficient for my application. I was reducing a big patch to 10000 vertices, and it was taking ~2secs using PolygonClip. I just realised that I have the Mapping Toolbox. I will try the nan-separated route, and report the timings later.
And you're right... every application of this I searched for online was dealing with rendering and Z-buffers.
My particular application may be interesting to you... imagine you've got patches of the skeleton and patches of some organs. Now imagine "punching" from different angles... I'm checking organ exposure to blunt trauma in pediatrics. Don't worry, purely research... no punching kids involved.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Delaunay Triangulation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by