Skip to content

Commit

Permalink
Merge pull request #814 from sofa-framework/simplerDataEngine
Browse files Browse the repository at this point in the history
[Data Update] [SofaKernel] BREAKING: Replacing DataEngine with SimpleDataEngine
  • Loading branch information
guparan committed Nov 21, 2018
2 parents 1baed11 + ac2f4ea commit 2a1a434
Show file tree
Hide file tree
Showing 181 changed files with 245 additions and 611 deletions.
60 changes: 1 addition & 59 deletions SofaKernel/framework/framework_test/core/DataEngine_test.cpp
Expand Up @@ -53,10 +53,7 @@ class TestEngine : public core::DataEngine
void init() override
{
addInput(&input);
m_dataTracker.trackData(input); // to connect a DataTracker to the Data 'input'

addOutput(&output);

setDirtyValue();
}

Expand All @@ -65,74 +62,27 @@ class TestEngine : public core::DataEngine
update();
}

void update() override
void doUpdate() override
{
// true only iff the DataTracker associated to the Data 'input' is Dirty
// that could only happen if 'input' was dirtied since last update
if( m_dataTracker.isDirty( input ) )
output.setValue(CHANGED);
else
output.setValue(NO_CHANGED);

cleanDirty();
}

};

/// to test tracked Data
class SimpleTestEngine : public core::SimpleDataEngine
{
public:
SOFA_CLASS(SimpleTestEngine, core::SimpleDataEngine);


Data< bool > input;
Data< int > output;

enum { UNDEFINED=0, CHANGED, NO_CHANGED };

SimpleTestEngine()
: Inherit1()
, input(initData(&input,false,"input","input"))
, output(initData(&output,(int)UNDEFINED,"output","output"))
{}

~SimpleTestEngine() {}

void init() override
{
addInput(&input);
addOutput(&output);
setDirtyValue();
}

void reinit() override
{
update();
}

private:
void doUpdate() override
{
// true only iff the DataTracker associated to the Data 'input' is Dirty
// that could only happen if 'input' was dirtied since last update
if( m_dataTracker.isDirty( input ) )
output.setValue(CHANGED);
else
output.setValue(NO_CHANGED);
}
};


struct DataEngine_test: public BaseTest
{
TestEngine engine;
SimpleTestEngine simpleEngine;

void SetUp()
{
engine.init();
simpleEngine.init();
}

/// to test tracked Data
Expand All @@ -159,18 +109,10 @@ struct DataEngine_test: public BaseTest

};



// Test
TEST_F(DataEngine_test, testDataEngine )
{
this->testTrackedData<TestEngine>(this->engine);
}

TEST_F(DataEngine_test, testSimpleDataEngine )
{
this->testTrackedData<SimpleTestEngine>(this->simpleEngine);
}


}// namespace sofa
19 changes: 19 additions & 0 deletions SofaKernel/framework/sofa/core/DataEngine.cpp
Expand Up @@ -42,9 +42,11 @@ DataEngine::~DataEngine()
/// Add a new input to this engine
void DataEngine::addInput(objectmodel::BaseData* n)
{
m_dataTracker.trackData(*n);
if (n->getOwner() == this && (!n->getGroup() || !n->getGroup()[0]))
n->setGroup("Inputs"); // set the group of input Datas if not yet set
core::objectmodel::DDGNode::addInput(n);
setDirtyValue();
}

/// Add a new output to this engine
Expand All @@ -53,8 +55,25 @@ void DataEngine::addOutput(objectmodel::BaseData* n)
if (n->getOwner() == this && (!n->getGroup() || !n->getGroup()[0]))
n->setGroup("Outputs"); // set the group of output Datas if not yet set
core::objectmodel::DDGNode::addOutput(n);
setDirtyValue();
}

void DataEngine::updateAllInputs()
{
for(auto& input : getInputs())
{
static_cast<sofa::core::objectmodel::BaseData*>(input)
->updateIfDirty();
}
}

void DataEngine::update()
{
updateAllInputs();
DDGNode::cleanDirty();
doUpdate();
m_dataTracker.clean();
}



Expand Down
100 changes: 18 additions & 82 deletions SofaKernel/framework/sofa/core/DataEngine.h
Expand Up @@ -45,7 +45,6 @@ namespace core
* {
* addInput // indicate all inputs
* addOutput // indicate all outputs
* setDirtyValue(); // the engine must start dirty (of course, no output are up-to-date)
* }
*
* // optional (called each time a data is modified in the gui)
Expand All @@ -55,25 +54,16 @@ namespace core
* update();
* }
*
* void update()
* void doUpdate() override
* {
* // FIRST all inputs must be updated
* // can be done by Data::getValue, ReadAccessor, Data::updateIfDirty, DataEngine::updateAllInputsIfDirty
*
* // must be called AFTER updating all inputs, otherwise a modified input will set the engine to dirty again.
* // must be called BEFORE read access to an output, otherwise read-accessing the output will call update
* cleanDirty();
*
* // FINALLY access and set outputs
* // Note that a write-only access has better performance and is enough in 99% engines Data::beginWriteOnly, WriteOnlyAccessor
* // A read access is possible, in that case, be careful the cleanDirty is called before the read-access, otherwise it can call an DataEngine::update itself. Data::beginEdit, WriteAccessor
* access your inputs, set your outputs...
* }
*
*/
class SOFA_CORE_API DataEngine : public core::DataTrackerDDGNode, public virtual core::objectmodel::BaseObject
{
public:
SOFA_ABSTRACT_CLASS(DataEngine, core::objectmodel::BaseObject);
SOFA_ABSTRACT_CLASS2(DataEngine, core::objectmodel::BaseObject, core::DataTrackerDDGNode);
SOFA_BASE_CAST_IMPLEMENTATION(DataEngine)
protected:
/// Constructor
Expand All @@ -85,10 +75,23 @@ class SOFA_CORE_API DataEngine : public core::DataTrackerDDGNode, public virtual
private:
DataEngine(const DataEngine& n) ;
DataEngine& operator=(const DataEngine& n) ;


/// Called in update(), back-propagates the data update
/// in the data dependency graph
void updateAllInputs();

protected:
/// Where you put your engine's impl
virtual void doUpdate() = 0;

public:
/// Updates your inputs and calls cleanDirty() for you.
/// User implementation moved to doUpdate()
virtual void update() final;

/// Add a new input to this engine
void addInput(objectmodel::BaseData* n);
/// Automatically adds the input fields to the datatracker
void addInput(sofa::core::objectmodel::BaseData* data);

/// Add a new output to this engine
void addOutput(objectmodel::BaseData* n);
Expand Down Expand Up @@ -181,75 +184,8 @@ class SOFA_CORE_API DataEngine : public core::DataTrackerDDGNode, public virtual
{
objectmodel::BaseObject::addLink(l);
}

};

/**
* \brief A simpler API for your DataEngines
*
* Implementation good rules:
*
* void init()
* {
* addInput // indicate all inputs
* addOutput // indicate all outputs
* setDirtyValue(); // You still have to set that manually, sorry
* }
*
* // optional (called each time a data is modified in the gui)
* // it is not always desired
* void reinit()
* {
* update();
* }
*
* void doUpdate() override
* {
* don't overthink it, just code. acces your inputs, set your outputs
* No cleanDirty() required
* }
*
*/
class SOFA_CORE_API SimpleDataEngine : public sofa::core::DataEngine
{
public:
SOFA_CLASS(SimpleDataEngine, sofa::core::DataEngine);

SimpleDataEngine() : Inherit1() {}
virtual ~SimpleDataEngine() {}


virtual void updateAllInputs()
{
for(auto& input : getInputs())
{
static_cast<sofa::core::objectmodel::BaseData*>(input)
->updateIfDirty();
}
}
/// Updates your inputs and calls cleanDirty() for you.
/// User implementation moved to doUpdate()
virtual void update() final
{
updateAllInputs();
DDGNode::cleanDirty();
doUpdate();
m_dataTracker.clean();
}

/// Automatically adds the input fields to the datatracker
void addInput(sofa::core::objectmodel::BaseData* data)
{
m_dataTracker.trackData(*data);
Inherit1::addInput(data);
}

protected:
/// Where you put your engine's impl
virtual void doUpdate() = 0;
};


} // namespace core

} // namespace sofa
Expand Down
2 changes: 1 addition & 1 deletion SofaKernel/modules/SofaBaseTopology/GridTopology.cpp
Expand Up @@ -51,7 +51,7 @@ GridTopology::GridUpdate::GridUpdate(GridTopology *t):
setDirtyValue();
}

void GridTopology::GridUpdate::update()
void GridTopology::GridUpdate::doUpdate()
{
updateEdges();
updateQuads();
Expand Down
2 changes: 1 addition & 1 deletion SofaKernel/modules/SofaBaseTopology/GridTopology.h
Expand Up @@ -71,7 +71,7 @@ using MeshTopology::getHexahedron;
typedef MeshTopology::Hexa Hexa;
SOFA_CLASS(GridUpdate,sofa::core::DataEngine);
GridUpdate(GridTopology* t);
virtual void update() override;
virtual void doUpdate() override;
protected:
void updateEdges();
void updateQuads();
Expand Down
6 changes: 3 additions & 3 deletions SofaKernel/modules/SofaBaseTopology/MeshTopology.cpp
Expand Up @@ -59,7 +59,7 @@ MeshTopology::EdgeUpdate::EdgeUpdate(MeshTopology* t)

}

void MeshTopology::EdgeUpdate::update()
void MeshTopology::EdgeUpdate::doUpdate()
{
if(topology->hasVolume() ) updateFromVolume();
else if(topology->hasSurface()) updateFromSurface();
Expand Down Expand Up @@ -242,7 +242,7 @@ MeshTopology::TriangleUpdate::TriangleUpdate(MeshTopology *t)
}


void MeshTopology::TriangleUpdate::update()
void MeshTopology::TriangleUpdate::doUpdate()
{
typedef MeshTopology::SeqTetrahedra SeqTetrahedra;
typedef MeshTopology::SeqTriangles SeqTriangles;
Expand Down Expand Up @@ -306,7 +306,7 @@ MeshTopology::QuadUpdate::QuadUpdate(MeshTopology *t)
setDirtyValue();
}

void MeshTopology::QuadUpdate::update()
void MeshTopology::QuadUpdate::doUpdate()
{
typedef MeshTopology::SeqHexahedra SeqHexahedra;
typedef MeshTopology::SeqQuads SeqQuads;
Expand Down
6 changes: 3 additions & 3 deletions SofaKernel/modules/SofaBaseTopology/MeshTopology.h
Expand Up @@ -71,7 +71,7 @@ class SOFA_BASE_TOPOLOGY_API MeshTopology : public core::topology::BaseMeshTopol
public:
SOFA_CLASS(EdgeUpdate,PrimitiveUpdate);
EdgeUpdate(MeshTopology* t);
void update() override;
void doUpdate() override;
protected:
void updateFromVolume();
void updateFromSurface();
Expand All @@ -84,15 +84,15 @@ class SOFA_BASE_TOPOLOGY_API MeshTopology : public core::topology::BaseMeshTopol

SOFA_CLASS(TriangleUpdate,PrimitiveUpdate);
TriangleUpdate(MeshTopology* t);
void update() override;
void doUpdate() override;
};

class QuadUpdate : public PrimitiveUpdate
{
public:
SOFA_CLASS(QuadUpdate,PrimitiveUpdate);
QuadUpdate(MeshTopology* t);
void update() override;
void doUpdate() override;
};
protected:
MeshTopology();
Expand Down
2 changes: 1 addition & 1 deletion SofaKernel/modules/SofaBaseTopology/TopologyEngine.h
Expand Up @@ -80,7 +80,7 @@ class TopologyEngineImpl : public sofa::core::topology::TopologyEngine

virtual void reinit() override;

virtual void update() override;
virtual void doUpdate() override;

void ApplyTopologyChanges();

Expand Down
3 changes: 1 addition & 2 deletions SofaKernel/modules/SofaBaseTopology/TopologyEngine.inl
Expand Up @@ -82,13 +82,12 @@ void TopologyEngineImpl< VecT>::reinit()


template <typename VecT>
void TopologyEngineImpl< VecT>::update()
void TopologyEngineImpl< VecT>::doUpdate()
{
#ifndef NDEBUG // too much warnings
sout << "TopologyEngine::update" << sendl;
sout<< "Number of topological changes: " << m_changeList.getValue().size() << sendl;
#endif
this->cleanDirty();
this->ApplyTopologyChanges();
}

Expand Down
2 changes: 1 addition & 1 deletion SofaKernel/modules/SofaEngine/BoxROI.h
Expand Up @@ -93,7 +93,7 @@ class BoxROI : public DataEngine
public:
void init() override;
void reinit() override;
void update() override;
void doUpdate() override;
void draw(const VisualParams*) override;

virtual void computeBBox(const ExecParams* params, bool onlyVisible=false ) override;
Expand Down

0 comments on commit 2a1a434

Please sign in to comment.