Are Nearest Neighbour functions supported on Simulink Realtime?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Jiangfeng Liu
2021 年 4 月 17 日
Hello, i have tried the same thing as you. It works but very slowly. Do you have any idea about that? Thanks!
回答 (1 件)
Hamid Satarboroujeni
2020 年 9 月 3 日
編集済み: Hamid Satarboroujeni
2020 年 9 月 3 日
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 件のコメント
Hamid Satarboroujeni
2020 年 9 月 4 日
編集済み: Hamid Satarboroujeni
2020 年 9 月 8 日
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:
参考
カテゴリ
Help Center および File Exchange で Code Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!