Forum Replies Created
-
AuthorPosts
-
8 March 2017 at 15:43 in reply to: [SOLVED] Create my own "DataTypes" for template components in SOFA #8761Bruno MarquesBlocked
Hi,
I finally have the solution to my problem:
In order to create template components in Sofa, there is nothing more to do than overloading the templateName() method, inherited from Base.
The templateName function must simply send back, for each template class, the name you want to use in your scene file, coma separated.usually, your implementation of templateName will look like this:
template<class T> std::string MyComponent<T>::templateName(const MyComponent<T>* nullptr = NULL) { return std::string(defaulttype::DataTypeName<T>::name(); }
This will work with SOFA types like defaulttype::Mat*, defaulttype::Vec* etc.. but won’t with your own data types if you created any, as DataTypeName would not have been specialized on your Data type.
The solution is to add to the file describing your data structure (in my case cvKeypoint) the following lines:
namespace sofa { namespace defaulttype { template <> struct DataTypeName<myDataType> { static const char* name() { return "myDataType"; } }; } // namespace defaulttype } // namespace sofa
7 March 2017 at 11:15 in reply to: [SOLVED] Create my own "DataTypes" for template components in SOFA #8760Bruno MarquesBlockedHi,
After a bit more research, I found out I over-complicated things:
I managed to get my templates working by templating my component over the source and destination type directly:
template<class SrcType, class DstType>
and for my own types (the cvKeypoint data structure) I implemented a DataTypeInfo as such:
template <> struct DataTypeInfo<OR::common::cvKeypoint> : public MyTypeInfo<OR::common::cvKeypoint> { static const char* name() { return "unsigned short"; } };
and MyTypeInfo like so:
template<class TDataType> struct MyTypeInfo { typedef TDataType DataType; typedef DataType BaseType; typedef DataType ValueType; typedef long long ConvType; typedef MyTypeInfo<TDataType> BaseTypeInfo; typedef MyTypeInfo<TDataType> ValueTypeInfo; enum { ValidInfo = 1 }; enum { FixedSize = 1 }; enum { ZeroConstructor = 1 }; enum { SimpleCopy = 1 }; enum { SimpleLayout = 1 }; enum { Integer = 0 }; enum { Scalar = 1 }; enum { Text = 0 }; enum { CopyOnWrite = 0 }; enum { Container = 0 }; enum { Size = 1 }; static size_t size() { return 1; } static size_t byteSize() { return sizeof(DataType); } static size_t size(const DataType& /*data*/) { return 1; } static bool setSize(DataType& /*data*/, size_t /*size*/) { return false; } template <typename T> static void getValue(const DataType &data, size_t index, T& value) { if (index != 0) return; // value = (T)data; } template<typename T> static void setValue(DataType &data, size_t index, const T& value ) { if (index != 0) return; // data = (DataType)value; } static void getValueString(const DataType &data, size_t index, std::string& value) { if (index != 0) return; std::ostringstream o; o << data; value = o.str(); } static void setValueString(DataType &data, size_t index, const std::string& value ) { if (index != 0) return; std::istringstream i(value); i >> data; } static const void* getValuePtr(const DataType& data) { return &data; } static void* getValuePtr(DataType& data) { return &data; } };
It is probably possible to make it simpler, but at least it works.
Though it is far from perfect: for some reason, in order to use my template from the XML, I have to type the following value for my attribute “template”:
template="Vec<2, int>, cvKeypoint>"
The ‘>’ at the end doesn’t make sense, and also, I would like to be able to write “Vec2i” and not “Vec<2, int>” Additionally, this syntax is too sensitive, as if I mistype the whitespaces for instance, my template won’t be recognized.
Could you tell me what I am missing?
Cheers,Bruno MarquesBlockedThank you Hugo,
After discussion with some pro users, it happens that my problem is a fake problem:
in the scene example of my first post, I show an example where component 3 and 4 actually depends on the output of Node1 and Node2.So logic would have that my components 3 and 4 would be in a subNode, whose parents are Node1 and Node2.
This is sadly not possible with XML, as an XML node can only have 1 parent, but with Python, it’s OK.
A possible alternative to my problem could be to nest Node2 in Node1, add an Node3, nested in Node2, and set my component3 and component4 inside.
Thanks a lot for your input, though, I learned a lot about Sofa’s animation loop thanks to you π
– Bruno
Bruno MarquesBlockedHi Jean-Nicolas
Thanks for your quick answer.
I tried what you said, but I got the same issue on windows. Though, I used CMAKE_PREFIX_PATH successfully on Linux, so it must be a CMake issue specific to Windows.Does anyone have any idea why it wouldn’t work on Windows?
Cheers,
Bruno14 September 2016 at 14:33 in reply to: [SOLVED] Take a screenshot automatically with SofaPython #7475Bruno MarquesBlockedThank you Hugo,
I found a workaround for now (triggering scrot, a screenshooter utility) from python.
Though I think it can be a very usefull feature to add in Sofa. I will probably create a component for this in a near future πCheers,
Bruno -
AuthorPosts