Are Nearest Neighbour functions supported on Simulink Realtime?

3 views (last 30 days)
Follow Up for: https://de.mathworks.com/matlabcentral/answers/584957-are-point-cloud-functions-supported-on-simulink-realtime?s_tid=mlc_ans_email_view
So I’ve tried using kdtree + knnsearch in Simulink Realtime (Matlab 2018b)
I’ve created a Matlab Function Block inside Simulink and then calling e.g.
kd = KDTreeSearcher([0 1 2;10 10 10]);
idx = knnsearch(kd,[[0,0,0]]);
It leads to: “Cannot call protected member 'stats.coder.searcher.KDTreeSearcher.KDTreeSearcher'."
If I use createns(X) instead of KDTreeSearcher => Function 'createns' not supported for code generation.
Is there any other space partioning trees available, which supports code generation? Like octree?
knnsearch itself seems to be compatible with slrt c code generation. e.g.
idx = knnsearch([0 1 2;10 10 10],[[0,0,0]]);
But I think this will not use any spatial partitioning trees, right? So just the naive implementation for nearest neighbour search?
According to https://de.mathworks.com/help/stats/code-generation-for-nearest-neighbor-searcher.html it seams that you could save a pretrained KDTree and then search it after deploying to a target, but this does not help in my use case (live dynamic point cloud processing).
  1 Comment
Jiangfeng Liu
Jiangfeng Liu on 17 Apr 2021
Hello, i have tried the same thing as you. It works but very slowly. Do you have any idea about that? Thanks!

Sign in to comment.

Answers (1)

Hamid Satarboroujeni
Hamid Satarboroujeni on 3 Sep 2020
Edited: Hamid Satarboroujeni on 3 Sep 2020
If you want ot use the knnsearch method of KDTreeSearcher object inside a MATLAB function block, you must follow the code generation workflow:
Simply put,
1- Create the KDTreeSearcher object in MATLAB:
kd = KDTreeSearcher([0 1 2;10 10 10]);
2- Save a code generation version of the object in a MAT-file:
saveLearnerForCoder(kd,'searcherModel')
3- In the MATLAB Function block create an entry point function to load the saved object and call knnsesarch method:
function idx = myknnsearch1(Y) %#codegen
Mdl = loadLearnerForCoder('searcherModel');
idx = knnsearch(Mdl,Y);
end
You should be able to run simulations and generate C/C++ code for this.
As you pointed out, you also have the option of using the knnsearch function, but to make sure the kdtree searcher object is used specify the NSMethod name-value pair:
idx = knnsearch([0 1 2;10 10 10],[[0,0,0]],'NSMethod','kdtree');
I hope this answers your questions.
  2 Comments
Sebastian Wolter
Sebastian Wolter on 4 Sep 2020
Thanks for the explanation.
But this means i cannot build a kdtree on the fly (with new data) inside a MATLAB function block.
It can only prepare a kdtree with datasets known before and then load this.
Hamid Satarboroujeni
Hamid Satarboroujeni on 4 Sep 2020
Edited: Hamid Satarboroujeni on 8 Sep 2020
If you use the knnsearch method you cannot build the kdtree on the fly. But if you use the knnsearch function the kdtree will be built on the fly. It generates code for createns and uses it at runtime to create the kdtree.
The code generation capabilities and limitations of the knnsearch function are listed here:

Sign in to comment.

Categories

Find more on Multicore Processor Targets 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!