knnsearch but with a constraint

4 views (last 30 days)
Pelajar UM
Pelajar UM on 29 Oct 2021
Edited: Pelajar UM on 29 Oct 2021
I have an 11,000x3 array representing x,y,z coordinates of points in 3D space. I want to find the nearest neighbor using knnsearch but with this constraint that the normal vector associated with any pair of points should form an angle larger than 170 degree (I have already calculated the normals).
How can I include this constraint in my knnsearch?
Here's the code so far and I'm trying to use a while loop until the condition is met, but pretty sure, this is not the right way of doing it:
[Idx] =knnsearch (points,points,'K',2, 'Distance',"chebychev"); % find the second nearest point (first point is the point itself)
Idx2=Idx(:,2) %take the index from the second point
sortedN=N(Idx2,:) %sort the normals based on the new inex
angle = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN')), dot(N',sortedN')))) %calculate the angle between the original normal and the sorted normal
% and this is clearly not how one should do a while loop because it doesn't do anything when I run it.... but basically
% I'm trying to tell it to search for the next nearest point until the
% condition is met.
while angle < 170
[Idx, distance] =knnsearch (points,points,'K',i, 'Distance',"chebychev");
i+1
end
% I want to update new index and distance value at the end

Answers (1)

KSSV
KSSV on 29 Oct 2021
You can specify the number of points you want from knnsearch. Get as many number of points you want. After calculate the angle and pick the points you want.
I would suggest you to have a look on rangesearch as well.
  3 Comments
KSSV
KSSV on 29 Oct 2021
You have placed the angle outside the loop, you need to update the angle inside the loop after getting each point right?
Pelajar UM
Pelajar UM on 29 Oct 2021
Edited: Pelajar UM on 29 Oct 2021
hmm, like this? I think I'm still missing something.
while angle < 170
[Idx, dist] =knnsearch (centroid,centroid, 'K',i, 'Distance',"chebychev");
i+1
sortedcentroid = centroid(Idx,:)
sortedN=N(Idx,:)
angle = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN')), dot(N',sortedN'))))
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!