Looking for a way to patch 44k polygons fast

11 views (last 30 days)
The following is a part of a function that patches triangles with the corresponding colours using a for-loop
for i = 1:numel(tri)/3
color(i, :) = img(center(i,1), center(i,2),:); %gets current color from img
patch(points(tri(i,:), 2), points(tri(i,:), 1), color(i,:)/256, 'EdgeColor','none'); %patches color on polygon
end
%size(tri) = 44721x3
%size(color) = 44271x3
%size(points) = 22429x2
%size(points(tri(i,:),1) = 3x1
%size(points(tri(i,:),2) = 3x1
%size(color(i,:)/256) = 1x3
The only problem is that patching this large number of polygons takes a long time (+/- 19s). Tried using `fill` instead of `patch` to see if that was faster, but sadly that was 13s slower.
Is there a way to speed up the patching process? For example by patching all the polygons in 1 sweep without looping?

Accepted Answer

Greg Dionne
Greg Dionne on 15 Oct 2018
Have you tried using just one patch? You can specify X and Y as matrices (this example taken from the doc page on patch )
x = [2 5; 2 5; 8 8];
y = [4 0; 8 2; 4 0];
c = [0; 1];
figure
patch(x,y,c)
colorbar
You can also use the 'Faces' and 'Vertices' syntax.
v = [2 4; 2 8; 8 4; 5 0; 5 2; 8 0];
f = [1 2 3; 4 5 6];
col = [0; 1];
figure
patch('Faces',f,'Vertices',v,'FaceVertexCData',col,'FaceColor','flat');
colorbar
  1 Comment
YT
YT on 15 Oct 2018
Thank you for your answer. I forgot to look a bit further in the documentation, but this definatly helped me out. Went from 19s to 1.5seconds!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!