Delaunay triangulation implementation in matlab -
hello it`s first post here. want write matlab script delaunay triangulation. here script:
clear all;clc %% delaunay x=[ 160.1671 366.9226 430.7894 540.1208 660.2771 508.7287 252.1787]; y=[ 223.9615 259.5000 120.5769 245.5000 283.1923 472.7308 469.5000]; % x=x'; y=y'; %orginal plot dd=delaunay(x,y); dt=trirep(dd,x,y); triplot(dt); z=[x.^2+y.^2] i=1:length(x); ptk=[i' x y] %% main loop l=0; i=1:length(x)-2 j=1+i:length(x) k=1+i:length(x) if (j ~= k) l=l+1; xn = (y(j)-y(i))*(z(k)-z(i)) - (y(k)-y(i))*(z(j)-z(i)); yn = (x(k)-x(i))*(z(j)-z(i)) - (x(j)-x(i))*(z(k)-z(i)); zn = (x(j)-x(i))*(y(k)-y(i)) - (x(k)-x(i))*(y(j)-y(i)); if (zn < 0) border=zn; m=1:length(x) border = (border) & ... ((x(m)-x(i))*xn +... (y(m)-y(i))*yn +... (z(m)-z(i))*zn <= 0); if (border) ii(m)=[i]; jj(m)=[j]; kk(m)=[k]; end end end end end end end wart=[ii' jj' kk'] dd figure(2) triplot(wart,x,y)
this should script. matrix generated delaunay() matlab function:
dd = 6 7 2 7 1 2 4 6 2 1 3 2 4 3 5 6 4 5 2 3 4
this implementation :
wart = 4 7 6 4 7 5 4 7 5 4 7 5 4 7 5 4 6 5 4 6 5
could of tell me wrong ? mistake or guide me?
jils.
the issue in innermost loop, here:
if (zn < 0) border=zn; m=1:length(x) border = (border) & ... ((x(m)-x(i))*xn +... (y(m)-y(i))*yn +... (z(m)-z(i))*zn <= 0); if (border) ii(m)=[i]; jj(m)=[j]; kk(m)=[k]; end end end
you want check if triangle defined points [i,j,k] valid. if m
(no points inside circumcircle) want save 3 points output. currently, if first point check outside circumcircle, 3 points saved no matter what. in addition, since loop on same m
each possible triangle, if find correct values you're overwriting them later on. why repeats of same values in output.
in these cases worth stepping through (mentally, manually on command line, or using debug methods) loops see happens. first output 4 7 6
doesn't appear in inbuilt function results. set i,j,k
values , see happens in inner loop.
incidentally, don't need loop there. check values @ once doing like:
border = (x-x(i)).*xn + (y-y(i)).*yn + (z-z(i)).*zn; if all(border<0) % store coordinates end
you can start empty output ([]) , append (using end+1
), or calculate max number of triangles , preallocate output size, use counter variable keep track of how many find , put them in right place in output array, , trim output size right @ end. if you're planning have larger input data sets, better preallocate.
Comments
Post a Comment