Commit 38632432 authored by Ricardo Ortiz's avatar Ricardo Ortiz
Browse files

ENH: Refactor ObjectSimulator and

DefaultSimulator.
parent 3186990d
Pipeline #4815 passed with stage
......@@ -32,9 +32,9 @@
DefaultSimulator::DefaultSimulator( ) : ObjectSimulator( )
{
this->addOperation([](std::vector<core::Vec3d>& array)
this->addOperation([](std::shared_ptr<MeshModel> &model)
{
for(auto &x : array)
for(auto &x : model->getMesh()->getVertices())
{
x.array() += 0.000001;
}
......@@ -47,54 +47,24 @@ void DefaultSimulator::beginExecution()
void DefaultSimulator::initialize()
{
//do nothing for now
for ( size_t i = 0; i < simulatedModels.size(); i++ )
{
auto object = simulatedModels[i];
switch ( object->getType() )
{
case core::ClassType::StaticSceneObject:
{
auto sceneObject = std::static_pointer_cast<StaticSceneObject>(object);
auto model = std::dynamic_pointer_cast<MeshCollisionModel>(sceneObject->getModel());
if(!model)
{
std::cerr << "Unknown model type." << std::endl;
break;
}
auto mesh = model->getMesh();
object->getLocalVertices().reserve( mesh->getNumberOfVertices() );
// WARNING: Copy!!?
object->getLocalVertices() = mesh->getVertices();
object->getFlags().isSimulatorInit = true;
break;
}
default:
std::cerr << "Unknown scene object type." << std::endl;
}
}
}
void DefaultSimulator::run()
{
beginExecution();
for ( size_t i = 0; i < this->simulatedModels.size(); i++ )
for(auto &sceneModel : this->simulatedModels)
{
auto sceneObj = this->simulatedModels[i];
//ensure that dummy simulator will work on static scene objects only.
if ( sceneObj->getType() == core::ClassType::StaticSceneObject )
for(auto &apply : this->operatorFunctions)
{
auto sceneObject = std::static_pointer_cast<StaticSceneObject>(sceneObj);
for(auto apply : this->operatorFunctions)
auto meshModel = std::dynamic_pointer_cast<MeshModel>(sceneModel->getModel());
if(!meshModel)
{
apply(sceneObject->getLocalVertices());
continue;
}
this->updateHapticForces(sceneObject);
apply(meshModel);
}
this->updateHapticForces(sceneModel);
}
endExecution();
......@@ -105,25 +75,7 @@ void DefaultSimulator::endExecution()
}
void DefaultSimulator::syncBuffers()
{
for ( size_t i = 0; i < this->simulatedModels.size(); i++ )
{
auto sceneObj = this->simulatedModels[i];
//ensure that dummy simulator will work on static scene objects only.
if ( sceneObj->getType() == core::ClassType::StaticSceneObject )
{
auto sceneObject = std::static_pointer_cast<StaticSceneObject>(sceneObj);
auto model = std::dynamic_pointer_cast<MeshCollisionModel>(sceneObject->getModel());
if(!model)
{
std::cerr << "Unknown model type." << std::endl;
break;
}
auto mesh = std::static_pointer_cast<MeshCollisionModel>(model)->getMesh();
// WARNING: Copy??!
mesh->getVertices() = sceneObject->getLocalVertices();
}
}
}
void DefaultSimulator::handleEvent(std::shared_ptr<core::Event> p_event )
{
......@@ -148,7 +100,7 @@ void DefaultSimulator::handleEvent(std::shared_ptr<core::Event> p_event )
}
}
void DefaultSimulator::updateHapticForces(std::shared_ptr<StaticSceneObject> sceneObject)
void DefaultSimulator::updateHapticForces(std::shared_ptr<SceneObject> sceneObject)
{
auto outputDevice = std::dynamic_pointer_cast<VRPNForceDevice>(this->hapticTool->getOutputDevice());
if(!outputDevice)
......@@ -190,5 +142,4 @@ void DefaultSimulator::updateHapticForces(std::shared_ptr<StaticSceneObject> sce
outputDevice->setDynamicFriction(0.0);
outputDevice->setSpringCoefficient(norm);
outputDevice->setStaticFriction(0.0);
}
......@@ -32,10 +32,10 @@
#include "Core/Config.h"
#include "Simulators/ObjectSimulator.h"
#include "Core/ErrorLog.h"
#include <Core/Vector.h>
#include "Core/Vector.h"
class ToolCoupler;
class StaticSceneObject;
class MeshModel;
///
/// \brief Default simulator that applies operations to the position array of the
/// undelying scene object model.
......@@ -44,7 +44,7 @@ class StaticSceneObject;
///
class DefaultSimulator: public ObjectSimulator
{
typedef std::function<void(std::vector<core::Vec3d>&)> OperationType;
typedef std::function<void(std::shared_ptr<MeshModel> &sceneObject)> OperationType;
public:
/// \brief Constructor/Destructor
DefaultSimulator();
......@@ -66,7 +66,7 @@ protected:
void syncBuffers() override;
void handleEvent(std::shared_ptr<core::Event> p_event) override;
void updateHapticForces(std::shared_ptr<StaticSceneObject> sceneObject);
void updateHapticForces(std::shared_ptr<SceneObject> sceneObject);
private:
std::vector<OperationType> operatorFunctions;
......
......@@ -22,6 +22,8 @@
//---------------------------------------------------------------------------
#include "Simulators/ObjectSimulator.h"
#include "SceneModels/SceneObject.h"
ObjectSimulator::ObjectSimulator():
enabled(false),
isObjectSimInitialized(false),
......@@ -46,7 +48,7 @@ void ObjectSimulator::addObject(std::shared_ptr< SceneObject > model)
return;
}
model->objectSim = this->safeDownCast<ObjectSimulator>();
model->attachObjectSimulator(this->safeDownCast<ObjectSimulator>());
this->simulatedModels.emplace_back(model);
}
......@@ -143,7 +145,18 @@ void ObjectSimulator::setTimeStep(const double newTimeStep)
{
this->timeStep = newTimeStep;
}
//---------------------------------------------------------------------------
double ObjectSimulator::getTimeStep() const
{
return this->timeStep;
}
//---------------------------------------------------------------------------
void ObjectSimulator::run()
{
for(auto & model : this->simulatedModels)
{
model->update(this->timeStep);
}
}
......@@ -29,7 +29,6 @@
#include "Core/CoreClass.h"
#include "Core/Timer.h"
#include "SimulationManager/Scheduler.h"
#include "SceneModels/SceneObject.h"
// Forward declarations
class SceneObject;
......@@ -112,12 +111,12 @@ public:
///
double getTimeStep() const;
protected:
///
/// \brief Execute the simulation.
///
virtual void run() = 0;
virtual void run();
protected:
///
/// \brief Called at the beginning of the simulator frame.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment