Sorting of points in 2D
33 views (last 30 days)
Show older comments
Dear All,
I face issue in arranging the airfoil coordinates coming from a CAD program. The coordinates are arranged from minumum x value to maximum x value. But I require to arrange the points starting from Trailing Edge ( Max X coordinate) and then proceed on top side all the way to Leading Edge ( Minimum X coordinate) and then again proceed in the bottom side and terminate in the Trailing Edge.
The input.txt ( required input file to be sorted)
Output.txt (the coordinates sorted manually).
I have several airfoil coordinates of similar nature, so a matlab function could help me out doing the job manually everytime.
Any help is really appreciated.
Thanks in Advance,
K Vijay Anand
Accepted Answer
Stephen23
on 17 Oct 2020
Edited: Stephen23
on 19 Oct 2020
Here is some simple code that that works quite nicely for your aerofoil. The code assumes that the top and bottom are both functions of x (i.e. no implicit curves in some other variable) and that they are both sampled with approximately the same step size (like your data). A convolution is used to smooth the data to create a moving average over 3 data points. The y-data are compared against this moving average to decide if the data point belongs to the top or bottom of the aerofoil, and then those two sets of points are sorted separately.
Inp = readtable('Input.txt','Delimiter','\t');
tmp = conv(Inp.y([1,1:end,end]),ones(1,3)/3,'valid'); % smoothed data
idx = Inp.y >= tmp; % true/false == top/bottom
idx(end) = true; % trailing edge -> top
Out = [sortrows(Inp(idx,:),-1);sortrows(Inp(~idx,:),+1)];
writetable(Out,'Out.txt','Delimiter','\t')
Checking against your original "Output.txt" and plotting the data:
>> Chk = readtable('Output.txt','Delimiter','\t');
>> isequal(Chk,Out)
ans =
1
>> plot(Inp.x,Inp.y,'-*', Inp.x,tmp,'-+', Out.x,Out.y,'-x')
>> legend('original','moving average','sorted')
2 Comments
Stephen23
on 17 Oct 2020
Edited: Stephen23
on 17 Oct 2020
@Image Analyst: good suggestions. The function boundary (available for >=R2014b) is certainly easy to use and provides a simple solution but is slower (it relies on the alphashape class, which is not trivial code) and requires a bit of fiddling to find a suitable shrink factor:
>> idy = boundary(T.x,T.y,0.97);
>> V = T(idy,:);
>> plot(V.x,V.y,'-d')
Below 0.97 misses out a few data points along the bottom towards the trailing edge**, above 0.98 starts to make extra connections between the top and bottom... I guess this depends on the sample step size and curvature, so most likely there is no universal solution which does not require fine-tuning.
** now that I look at the above figure, it also seems to be missing a few. 0.98 is probably about right.
More Answers (1)
Vijay Anand
on 18 Oct 2020
Edited: Vijay Anand
on 18 Oct 2020
2 Comments
Stephen23
on 19 Oct 2020
I did some more experimentation and got better results replacing the moving average with a polynomial fitted to the data. First I tried polyfit, but quickly realized that the end points need to be specified. Luckily this FEX submission
allows constraints to be specified, including points that the curve has to pass through:
pts = Inp{[1,end],:}; % polynomial must pass through the leading & trailing edges
pwr = mmpolyfit(Inp.x,Inp.y, 5, 'Point',pts) % fit order 5 polynomial
tmp = polyval(pwr, Inp.x); % replaces TMP in the original answer
And checking the fit:
plot(Inp.x,Inp.y,'-*', Inp.x,tmp,'-+')
See Also
Categories
Find more on Airfoil tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!