I am facing with the sorting airfoil coordinates. In particular given a set of coordinates, which are not sorted, I have to sorted them starting from the trailing edge upper surface. Here I report the code that I have developed but as you can see, the starting point do not match with what I suppose, moreover exist several oscillations as you can see in the reported figure (and a detail, in blue the starting point after the sort). Can someone suggest me what I miss? How can I do?
Thanks you in advance.
def sort_airfoil(points):
x0 = np.mean(-points[:,1])
y0 = np.mean(points[:,2])
r = np.sqrt((-points[:,1]-x0)**2 (points[:,2]-y0)**2)
tempx=-points[:,1]
xmax=np.max(tempx)
ind_max=np.where(tempx==xmax)
ymax=np.max(points[ind_max,2])
ind_max_t=np.where((tempx>0.95*xmax) & (tempx<xmax))
ymax_t=points[ind_max_t,2]
ymin=np.min(ymax_t)
indx_temp=np.where(points[:,2]==ymin)
xmin=np.max(tempx[indx_temp])
xmed=(xmin xmax)/2
ymed=(ymin ymax)/2
print(x0,y0)
print(xmin,ymin)
print((xmin xmax)/2, (ymin ymax)/2)
angle0=np.arctan2((ymed-y0),(xmed-x0))
print("angle", angle0)
angles = np.where((points[:,2]-y0) > 0, np.arccos((-points[:,1]-x0)/r), 2*np.pi-np.arccos((-points[:,1]-x0)/r))
angles=angles-angle0
for i in range(len(angles)):
if angles[i]<0:
angles[i]=angles[i] 2*np.pi
elif angles[i]>2*np.pi:
angles[i]=angles[i]-2*np.pi
mask = np.argsort(angles)
x_sorted = points[mask,1]
y_sorted = points[mask,2]
points_new=np.zeros([len(points), 3])
points_new[:,0]=points[:,0]
points_new[:,1]=x_sorted
points_new[:,2]=y_sorted
return points_new
[ 0 -198.949997 -48.73 ]
[ 0 -261.540009 -22.860001 ]
[ 0 110.889999 -8.43 ]
[ 0 -281.26001 -53.189999 ]
[ 0 -230.74000499999997 -50.810001 ]
[ 0 122.019997 5.15 ]
[ 0 -345.52999900000003 -53.009997999999996 ]
[ 0 15.72 30.129998999999998 ]
[ 0 93.220001 -13.1 ]
[ 0 -42.549999 24.33 ]
[ 0 -31.620001000000002 -34.580002 ]
[ 0 -239.33999599999999 -15.89 ]
[ 0 -355.359985 -53.150002 ]
[ 0 104.400002 19.82 ]
[ 0 -338.959991 -53.110001000000004 ]
[ 0 101.940002 -11.04 ]
[ 0 -174.639999 -47.07 ]
[ 0 89.0 -14.04 ]
[ 0 25.790001 30.700001 ]
[ 0 -287.170013 -31.5 ]
[ 0 -263.019989 -23.34 ]
[ 0 71.5 29.139999 ]
[ 0 -104.0 -41.889998999999996 ]
[ 0 -370.869995 -52.099998 ]
[ 0 -215.460007 -9.02 ]
[ 0 -360.109985 -53.34 ]
[ 0 -351.299988 -50.700001 ]
[ 0 -332.01001 -53.259997999999996 ]
[ 0 -330.619995 -45.889998999999996 ]
[ 0 75.349998 -16.889999 ]
[ 0 123.190002 2.91 ]
[ 0 -250.24000499999997 -19.25 ]
[ 0 -299.5 -35.82 ]
[ 0 -208.240005 -49.34 ]
[ 0 -220.27999900000003 -10.35 ]
[ 0 -67.279999 20.9 ]
[ 0 -252.92999300000002 -20.1 ]
[ 0 80.620003 -15.82 ]
[ 0 120.760002 -3.61 ]
[ 0 -165.33999599999999 3.39 ]
[ 0 -292.640015 -33.400002 ]
[ 0 -163.0 -46.259997999999996 ]
[ 0 -265.980011 -52.689999 ]
[ 0 -370.559998 -53.849998 ]
[ 0 -243.919998 -51.630001 ]
[ 0 -309.850006 -53.490002000000004 ]
[ 0 -112.33000200000001 -42.540001000000004 ]
[ 0 -183.279999 -47.68 ]
[ 0 -55.509997999999996 -37.380001 ]
[ 0 55.610001000000004 -20.59 ]
[ 0 -94.199997 16.799999 ]
[ 0 47.52 -22.07 ]
[ 0 62.959998999999996 -19.26 ]
[ 0 -279.859985 -28.969998999999998 ]
[ 0 36.709998999999996 -24.030001000000002 ]
[ 0 -370.589996 -52.040001000000004 ]
[ 0 -313.369995 -53.48 ]
[ 0 65.809998 -18.73 ]
[ 0 33.18 30.93 ]
[ 0 -324.380005 -53.369999 ]
[ 0 120.989998 6.65 ]
[ 0 -53.34 -37.150002 ]
[ 0 -283.799988 -30.32 ]
[ 0 -43.09 -36.0 ]
[ 0 100.209999 21.73 ]
[ 0 88.199997 25.870001000000002 ]
[ 0 18.059998999999998 30.18 ]
[ 0 25.129998999999998 -26.07 ]
[ 0 118.650002 -4.92 ]
[ 0 -160.71000700000002 4.41 ]
[ 0 -24.620001000000002 26.49 ]
[ 0 -179.91000400000001 -47.450001 ]
[ 0 116.300003 -6.17 ]
[ 0 -266.149994 -24.379998999999998 ]
[ 0 7.78 -28.870001000000002 ]
[ 0 116.120003 12.08 ]
[ 0 -69.5 20.57 ]
[ 0 -257.75 -52.34 ]
[ 0 -50.119999 23.34 ]
[ 0 -206.490005 -6.6 ]
[ 0 -261.369995 -52.509997999999996 ]
[ 0 -227.229996 -50.59 ]
[ 0 38.849998 -23.65 ]
[ 0 -153.380005 5.99 ]
[ 0 -223.02999900000003 -11.12 ]
[ 0 -108.589996 -42.25 ]
[ 0 29.469998999999998 30.83 ]
[ 0 -246.960007 -18.219998999999998 ]
[ 0 -339.329987 -48.200001 ]
[ 0 -1.21 28.92 ]
[ 0 -299.48999 -53.450001 ]
[ 0 -52.490002000000004 23.01 ]
[ 0 113.910004 -7.25 ]
[ 0 -24.809998999999998 -33.689999 ]
[ 0 -101.400002 -41.700001 ]
[ 0 22.969998999999998 -26.43 ]
[ 0 108.529999 -9.22 ]
[ 0 -271.109985 -26.01 ]
[ 0 -353.290009 -53.09 ]
[ 0 -130.720001 10.47 ]
[ 0 -356.109985 -51.189999 ]
[ 0 -96.529999 -41.34 ]
[ 0 -107.050003 -42.130001 ]
[ 0 -347.380005 -50.080002 ]
[ 0 -150.470001 -45.369999 ]
[ 0 -27.040001 -33.98 ]
[ 0 -364.890015 -53.549999 ]
[ 0 62.029999 30.18 ]
[ 0 113.860001 13.98 ]
[ 0 51.610001000000004 -21.33 ]
[ 0 -0.05 -29.9 ]
[ 0 -273.459991 -26.790001 ]
[ 0 49.419998 30.9 ]
[ 0 123.410004 0.49 ]
[ 0 82.099998 -15.52 ]
[ 0 -85.599998 18.129998999999998 ]
[ 0 -217.91000400000001 -9.69 ]
[ 0 -258.149994 -21.75 ]
[ 0 56.16 30.610001 ]
[ 0 98.760002 -11.82 ]
[ 0 -5.12 28.57 ]
[ 0 -254.11999500000002 -52.169998 ]
[ 0 -244.729996 -17.52 ]
[ 0 -362.329987 -53.43 ]
[ 0 10.79 29.85 ]
[ 0 122.93 -1.28 ]
[ 0 68.650002 -18.190001000000002 ]
[ 0 -229.940002 -13.09 ]
[ 0 13.78 30.040001 ]
[ 0 -36.049999 25.15 ]
[ 0 -20.389999 26.98 ]
[ 0 -5.09 -30.790001 ]
[ 0 -304.730011 -53.48 ]
[ 0 85.029999 26.690001000000002 ]
[ 0 -277.130005 -28.030001000000002 ]
[ 0 -27.629998999999998 26.139999 ]
[ 0 18.93 -27.110001 ]
[ 0 101.08000200000001 -11.26 ]
[ 0 -334.299988 -53.200001 ]
[ 0 121.629997 -2.95 ]
[ 0 -216.88000499999998 -49.91 ]
[ 0 -310.859985 -39.66 ]
[ 0 13.41 -28.0 ]
[ 0 -302.230011 -53.459998999999996 ]
[ 0 -341.51001 -53.060001 ]
[ 0 -124.089996 -43.439999 ]
[ 0 -182.96000700000002 -0.69 ]
[ 0 -16.209999 -32.490002000000004 ]
[ 0 -291.609985 -53.369999 ]
[ 0 -168.220001 -46.619999 ]
[ 0 105.209999 -10.2 ]
[ 0 -2.85 -30.34 ]
[ 0 -333.589996 -46.700001 ]
[ 0 -150.610001 6.57 ]
[ 0 46.619999 30.969998999999998 ]
[ 0 -32.990002000000004 -34.759997999999996 ]
[ 0 -72.040001 -39.009997999999996 ]
[ 0 -317.369995 -53.439999 ]
[ 0 -139.529999 -44.57 ]
[ 0 45.740002000000004 -22.4 ]
[ 0 -61.060001 21.799999 ]
[ 0 -212.169998 -49.610001000000004 ]
[ 0 -139.940002 8.7 ]
[ 0 -191.270004 -48.209998999999996 ]
[ 0 36.16 31.0 ]
[ 0 -20.74 -33.139998999999996 ]
[ 0 -237.11000099999998 -51.209998999999996 ]
[ 0 -158.300003 -45.919998 ]
[ 0 -213.399994 -8.44 ]
[ 0 -307.160004 -38.419998 ]
[ 0 51.380001 30.83 ]
[ 0 -73.519997 19.969998999999998 ]
[ 0 -236.30000299999998 -14.95 ]
[ 0 -350.130005 -53.02 ]
[ 0 39.950001 31.030001000000002 ]
[ 0 122.809998 3.83 ]
[ 0 -156.130005 5.41 ]
[ 0 4.93 29.43 ]
[ 0 -79.400002 19.07 ]
[ 0 -10.33 28.059998999999998 ]
[ 0 -101.980003 15.56 ]
[ 0 52.810001 30.780001000000002 ]
[ 0 -31.379998999999998 -34.549999 ]
[ 0 58.810001 -20.02 ]
[ 0 -114.190002 13.49 ]
[ 0 -224.11999500000002 -50.380001 ]
[ 0 -135.679993 -44.290001000000004 ]
[ 0 -89.540001 -40.720001 ]
[ 0 -43.240002000000004 24.24 ]
[ 0 -361.100006 -51.490002000000004 ]
[ 0 -232.559998 -13.85 ]
[ 0 42.830002 -22.940001000000002 ]
[ 0 -187.699997 -47.970001 ]
[ 0 -294.359985 -53.41 ]
[ 0 -38.900002 -35.5 ]
[ 0 67.589996 29.629998999999998 ]
[ 0 -241.80000299999998 -51.5 ]
[ 0 -190.580002 -2.55 ]
[ 0 -299.799988 -35.900002 ]
[ 0 -303.109985 -37.049999 ]
[ 0 -85.25 -40.299999 ]
[ 0 -149.470001 -45.299999 ]
[ 0 -145.580002 -45.009997999999996 ]
[ 0 -227.91000400000001 -12.5 ]
[ 0 -77.489998 -39.540001000000004 ]
[ 0 -206.119995 -49.200001 ]
[ 0 -277.589996 -53.09 ]
[ 0 10.03 -28.530001000000002 ]
[ 0 -16.620001000000002 27.4 ]
[ 0 -348.109985 -53.009997999999996 ]
[ 0 -370.640015 -53.82 ]
[ 0 -156.110001 -45.759997999999996 ]
[ 0 86.91999799999999 -14.51 ]
[ 0 -197.100006 -48.599998 ]
[ 0 -107.849998 14.58 ]
[ 0 -270.579987 -52.869999 ]
[ 0 -35.380001 -35.07 ]
[ 0 -67.959999 -38.610001000000004 ]
[ 0 98.190002 22.549999 ]
[ 0 -7.27 -31.15 ]
[ 0 -287.160004 -53.279999 ]
[ 0 -242.88000499999998 -16.950001 ]
[ 0 32.57 -24.780001000000002 ]
[ 0 -32.77 25.530001000000002 ]
[ 0 -143.850006 -44.880001 ]
[ 0 -125.08000200000001 11.52 ]
[ 0 -370.700012 -52.07 ]
[ 0 -78.730003 -39.66 ]
[ 0 -328.359985 -45.209998999999996 ]
[ 0 -341.059998 -48.610001000000004 ]
[ 0 107.989998 17.889999 ]
[ 0 -13.02 27.780001000000002 ]
[ 0 -202.21000700000002 -5.48 ]
[ 0 -117.370003 12.94 ]
[ 0 43.389998999999996 31.01 ]
[ 0 1.08 -29.76 ]
[ 0 -349.73999 -50.470001 ]
[ 0 -30.280001000000002 25.83 ]
[ 0 -332.75 -53.220001 ]
[ 0 -294.98999 -34.25 ]
[ 0 -59.57 22.01 ]
[ 0 -128.110001 10.95 ]
[ 0 21.49 30.52 ]
[ 0 59.16 30.41 ]
[ 0 -179.110001 0.23 ]
[ 0 78.889999 -16.190001000000002 ]
[ 0 -296.630005 -53.43 ]
[ 0 -46.59 -36.400002 ]
[ 0 -170.66000400000001 2.2 ]
[ 0 -353.369995 -50.93 ]
[ 0 -268.100006 -25.01 ]
[ 0 -65.639999 21.139999 ]
[ 0 -364.769989 -51.650002 ]
[ 0 -327.98999 -53.310001 ]
[ 0 -193.190002 -3.19 ]
[ 0 123.18 -0.71 ]
[ 0 -90.959999 17.32 ]
[ 0 -339.950012 -48.34 ]
[ 0 -233.25999500000003 -50.970001 ]
[ 0 79.599998 27.84 ]
[ 0 -105.43 14.99 ]
[ 0 -198.690002 -4.57 ]
[ 0 -220.0 -50.110001000000004 ]
[ 0 89.959999 25.370001000000002 ]
[ 0 -204.820007 -6.14 ]
[ 0 73.989998 28.780001000000002 ]
[ 0 -324.829987 -44.139998999999996 ]
[ 0 -75.849998 -39.369999 ]
[ 0 -81.43 -39.919998 ]
[ 0 -11.87 -31.860001 ]
[ 0 -368.940002 -51.84 ]
[ 0 77.050003 28.299999 ]
[ 0 94.32 23.99 ]
[ 0 -313.440002 -40.509997999999996 ]
[ 0 -356.700012 -53.189999 ]
[ 0 4.45 -29.41 ]
[ 0 29.42 -25.33 ]
[ 0 94.739998 -12.77 ]
[ 0 -77.83000200000001 19.309998999999998 ]
[ 0 -307.609985 -53.48 ]
[ 0 64.25 29.98 ]
[ 0 -57.860001000000004 -37.619999 ]
[ 0 -367.130005 -53.639998999999996 ]
[ 0 72.25 -17.51 ]
[ 0 -116.230003 -42.84 ]
[ 0 -283.26001 -53.220001 ]
[ 0 -289.76001 -32.389998999999996 ]
[ 0 -64.82 -38.32 ]
[ 0 -144.33999599999999 7.84 ]
[ 0 -228.580002 -50.66 ]
[ 0 -279.790009 -53.139998999999996 ]
[ 0 -172.58999599999999 -46.93 ]
[ 0 -186.619995 -1.56 ]
[ 0 -69.720001 20.540001 ]
[ 0 -370.73999 -53.689999 ]
[ 0 119.400002 8.71 ]
[ 0 61.669998 -19.5 ]
[ 0 91.379997 -13.53 ]
[ 0 -163.929993 -46.32 ]
[ 0 -321.700012 -53.400002 ]
[ 0 83.480003 -15.25 ]
[ 0 -255.11999500000002 -20.77 ]
[ 0 82.610001 27.23 ]
[ 0 54.73 -20.77 ]
[ 0 112.449997 15.04 ]
[ 0 -97.910004 16.219998999999998 ]
[ 0 -103.120003 -41.82 ]
[ 0 -29.08 -34.259997999999996 ]
[ 0 -69.370003 -38.75 ]
[ 0 -62.490002000000004 -38.09 ]
[ 0 -119.18 -43.060001 ]
[ 0 123.309998 2.57 ]
[ 0 122.559998 -1.95 ]
[ 0 -119.480003 12.55 ]
[ 0 -209.119995 -7.28 ]
[ 0 -202.330002 -48.950001 ]
[ 0 -336.630005 -53.139998999999996 ]
[ 0 -196.740005 -4.07 ]
[ 0 -127.050003 -43.66 ]
[ 0 -166.630005 -46.509997999999996 ]
[ 0 -172.190002 1.84 ]
[ 0 -7.63 28.33 ]
[ 0 -358.040009 -51.32 ]
[ 0 -147.71000700000002 7.17 ]
[ 0 -134.149994 9.84 ]
[ 0 -50.43 -36.830002 ]
[ 0 -87.260002 17.889999 ]
[ 0 117.900002 10.36 ]
[ 0 37.549999 31.02 ]
[ 0 -131.509995 -43.990002000000004 ]
[ 0 -160.020004 4.58 ]
[ 0 -347.369995 -50.060001 ]
[ 0 2.53 -29.66 ]
[ 0 -51.549999 23.139999 ]
[ 0 -339.570007 -53.080002 ]
[ 0 -137.16000400000001 9.26 ]
[ 0 -42.279999 -35.91 ]
[ 0 -330.589996 -45.84 ]
[ 0 -121.370003 12.21 ]
[ 0 -0.66 28.969998999999998 ]
[ 0 -72.389999 -39.049999 ]
[ 0 -94.66999799999999 16.73 ]
[ 0 75.260002 -16.93 ]
[ 0 -322.019989 -43.27 ]
[ 0 -370.51001 -52.0 ]
[ 0 -219.270004 -10.06 ]
[ 0 -180.360001 -47.470001 ]
[ 0 -56.330002 22.469998999999998 ]
[ 0 -282.579987 -29.889999 ]
[ 0 99.760002 21.92 ]
[ 0 -186.300003 -47.869999 ]
[ 0 -168.330002 2.74 ]
[ 0 -279.380005 -28.790001 ]
[ 0 -293.690002 -53.400002 ]
[ 0 8.97 29.719998999999998 ]
[ 0 -247.0 -51.790001000000004 ]
[ 0 -153.490005 -45.580002 ]
[ 0 50.16 30.879998999999998 ]
[ 0 -251.22000099999997 -52.02 ]
[ 0 -238.02999900000003 -51.27 ]
[ 0 -245.770004 -17.83 ]
[ 0 -214.520004 -8.75 ]
[ 0 85.68 26.52 ]
[ 0 -262.679993 -23.219998999999998 ]
[ 0 123.449997 1.46 ]
[ 0 -314.109985 -53.450001 ]
[ 0 -317.559998 -41.860001000000004 ]
[ 0 105.099998 19.450001 ]
[ 0 -82.529999 18.610001 ]
[ 0 -320.320007 -42.740002000000004 ]
[ 0 -47.73 23.66 ]
[ 0 -370.769989 -52.049999 ]
[ 0 29.83 30.84 ]
[ 0 -140.639999 -44.650002 ]
[ 0 116.040001 12.14 ]
[ 0 -95.0 -41.209998999999996 ]
[ 0 -351.429993 -53.029999 ]
[ 0 -3.24 28.74 ]
[ 0 -370.910004 -53.419998 ]
[ 0 -273.98999 -52.990002000000004 ]
[ 0 -209.320007 -49.419998 ]
[ 0 -97.760002 -41.419998 ]
[ 0 -224.669998 -11.57 ]
[ 0 -290.559998 -53.349998 ]
[ 0 -239.86000099999998 -16.030001000000002 ]
[ 0 -189.16000400000001 -48.060001 ]
[ 0 -336.230011 -47.380001 ]
[ 0 -91.18 17.280001000000002 ]
[ 0 -288.140015 -31.809998999999998 ]
[ 0 -343.48999 -49.169998 ]
[ 0 -291.839996 -33.099998 ]
[ 0 -112.730003 -42.57 ]
[ 0 -358.899994 -53.27 ]
[ 0 -273.440002 -26.780001000000002 ]
[ 0 27.49 30.76 ]
[ 0 24.969998999999998 -26.120001000000002 ]
[ 0 21.41 -26.709999 ]
[ 0 1.94 29.18 ]
[ 0 -258.809998 -21.950001 ]
[ 0 -258.22000099999997 -52.360001000000004 ]
[ 0 -121.769997 -43.259997999999996 ]
[ 0 -261.0 -52.48 ]
[ 0 20.52 30.4 ]
[ 0 69.959999 29.34 ]
[ 0 -109.900002 -42.349998 ]
[ 0 -194.0 -48.380001 ]
[ 0 -287.02999900000003 -53.27 ]
[ 0 -370.910004 -52.09 ]
[ 0 -39.830002 24.66 ]
[ 0 -55.09 -37.34 ]
[ 0 110.110001 16.620001000000002 ]
[ 0 -354.290009 -53.099998 ]
[ 0 -17.540001 27.290001 ]
[ 0 -138.08999599999999 -44.470001 ]
CodePudding user response:
The issue comes from the algorithm itself: it only work when the points form a convex polygon. However, the shape is concave.
More specifically, the first sorted points (and the last ones) form a zigzag-shaped lines because there is two sets of points (green arrows) interleaving with growing angles (red arrow) from the median point (red line).
Note the points are horizontally flipped on the gathered point from the question. Thus the points are sorted clockwise.
One simple solution is to split horizontally the shape in many set of point (eg. 10 set) so that each set form a convex shape. Then, the parts can be merged to form the final shape. The merge consists in finding the points at the "edge" of each locally-sorted set of points (parts) and reorder the partially sorted array of points consequently.
More specifically, the points of each part are split in 2 sub-sets: the upper ones and the lower ones. You can find them easily by selecting the 2 left-most points of a right part with the right-most points of a left part. The 2 top-most points needs to be connected each other and the same for the 2 bottom-most points. Thus, the sequence of the two upper sets of points needs to be reordered so they are contiguous and the same for the lower part.
Here is an example:
Note that if you are unsure about how to split the points in many parts so that each one form a convex-shaped sets of points, then you can: split the shape in n
parts, check if the set of points form a convex shape by computing a
Thanks in advance!
Kind Regards