Skip to content

Extending RBDL Models

Felix Richter edited this page Aug 12, 2020 · 3 revisions

Extending RBDL-Models

When visualizing RBDL-Models you might not just be interested in seeing what the model looks like in its current configuration. Typically you might have computed some other data such as an animation, or you have some force/torque data. Instead of adding all of the code needed to display this extra data to the RBDLModelWrapper class we can extend it by creating a subclass of the WrapperExtension. It has functions which enable it to interact with the model in such a way to make it easy to display new data.

WrapperExtension

/* This class provides an abstact base for adding data to a loaded model, such as (Animations, Forces, etc.).
 */
class WrapperExtension {
	protected:
		RBDLModelWrapper* model_parent;

	public:
		WrapperExtension();

		void setModelParent(RBDLModelWrapper* model);

		//needs to be implemented by every extention
		virtual std::string getExtensionName() = 0;
		virtual void update(float current_time) = 0; 

		//optional implementetion, default does nothing
		virtual Qt3DCore::QEntity* getVisual();
};

Creating a new Extension

This is pretty straight forward you just need to subclass the WrapperExtension class and implement the two pure virtual functions. Then we can add it a model by calling the void addExtension(WrapperExtension* ext); function of model we are interested in extending.

class ExampleExtension : public WrapperExtension {
    private:
        // ... all private variables holding extension data
    public:
        // ... public functions to setup extension data

        // return extension name MUST be UNIQUE for every extension
        std::string getExtentionName() {
            return "Example";
        }

        // this function always gets called by the model when the current_time changes
        void update(float current_time) {
            // ... caluculate or read data for current frame and update visual components accordingly
        }
}

...

ExampleExtension* ext = new ExampleExtension;

// setup extension data here

model->addExtension(ext);

...

When the extension is added to a model, the model will call the setModelParent function to save the reference to the model in the extension, it will also query it's name to save the instance of the extension in the model in a map.

Adding Visuals to Model

When adding an extension to a model, the model will also call the getVisual function of the extension, and if it returns a QEntity it will add it to the models root entity. That way all the visuals added in this QEntity and it's children will get displayed relative to the models anchor in the scene.

Clone this wiki locally