Returning 2D array from plugin

Typically: "How do I... ", "How can I... " questions
Post Reply
ravi
Posts: 85
Joined: 24 Oct 2016, 08:00

Returning 2D array from plugin

Post by ravi »

Hi, I want to return 2D double array from CPP plugin. Below is the common way to return data from a function inside plugin-

Code: Select all

std::string data="this is a string.";
D.pushOutData(CScriptFunctionDataItem(data));
D.writeDataToStack(cb->stackID);
The current available CScriptFunctionDataItem doesn't contain 2D vector i.e., std::vector<std::vector<double>>. Below are the available data types-

Code: Select all

CScriptFunctionDataItem(bool v);
CScriptFunctionDataItem(int v);
CScriptFunctionDataItem(float v);
CScriptFunctionDataItem(double v);
CScriptFunctionDataItem(const std::string& str);
CScriptFunctionDataItem(const char* str);
CScriptFunctionDataItem(const char* bufferPtr,unsigned int bufferLength);
CScriptFunctionDataItem(const std::vector<bool>& v);
CScriptFunctionDataItem(const std::vector<int>& v);
CScriptFunctionDataItem(const std::vector<float>& v);
CScriptFunctionDataItem(const std::vector<double>& v);
CScriptFunctionDataItem(const std::vector<std::string>& v);
As a workaround, std::vector<std::string> can be used, where each std::string of this structure represents a row. A row can contain data separated by comma i.e., columns are separated by comma. but this looks like an unnecessary overhead to CPU and may consume more time.

Looking for expert advice. Please.

coppelia
Site Admin
Posts: 10378
Joined: 14 Dec 2012, 00:25

Re: Returning 2D array from plugin

Post by coppelia »

Hello,

the CScriptFunctionData and CScriptFunctionDataItem classes can be used to exchange simple data between your plugin and a V-REP script. If you need to exchange more complex data, then you should directly use the stack functions.

Have a look at the project programming/v_repExtPluginSkeleton, in file v_repExtPluginSkeleton.cpp, the callback function LUA_GETDATA_CALLBACK: it is reading/writing the stack directly.

Using the stack, you can exchange any type of data with a script, including nested arrays, maps, mixed arrays, etc.

Cheers

ravi
Posts: 85
Joined: 24 Oct 2016, 08:00

Re: Returning 2D array from plugin

Post by ravi »

Thanks admin,
One easy alternative is to convert 2d array into 1d array and then use standard data item i.e., CScriptFunctionDataItem(const std::vector<double>& v).

Thank you very much for introducing me to stack functions.

Post Reply