diff --git a/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.cpp b/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.cpp index 671a579888ca003277c4d11765815e733e3fbec1..638e86945824c1e9df1caaa0558e0ae6e5a5a566 100644 --- a/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.cpp +++ b/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.cpp @@ -27,6 +27,9 @@ BulletCoinQtViewer::BulletCoinQtViewer(DynamicsWorldPtr world) updateTimerIntervalMS = 5; + // no mutex for standard viewer + //engineMutexPtr.reset(new boost::recursive_mutex()); + //const double TIMER_MS = 5.0f; SIMDYNAMICS_ASSERT(world); @@ -103,7 +106,7 @@ void BulletCoinQtViewer::timerCB(void * data, SoSensor * sensor) } -void BulletCoinQtViewer::initSceneGraph( QFrame* embedViewer, SoNode* scene ) +void BulletCoinQtViewer::initSceneGraph( QFrame* embedViewer, SoNode* scene, int antiAliasingSteps /* =0 */ ) { viewer = new SoQtExaminerViewer(embedViewer,"",TRUE,SoQtExaminerViewer::BUILD_POPUP); @@ -111,7 +114,7 @@ void BulletCoinQtViewer::initSceneGraph( QFrame* embedViewer, SoNode* scene ) viewer->setBackgroundColor(SbColor(1.0f, 1.0f, 1.0f)); viewer->setAccumulationBuffer(true); - viewer->setAntialiasing(true, 4); + //viewer->setAntialiasing(true, 4); viewer->setGLRenderAction(new SoBoxHighlightRenderAction); viewer->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND); @@ -142,7 +145,10 @@ void BulletCoinQtViewer::initSceneGraph( QFrame* embedViewer, SoNode* scene ) sceneGraph->addChild(scene); viewer->setSceneGraph(sceneGraphRoot); - viewer->setAntialiasing(true, 4); + if (antiAliasingSteps>0) + viewer->setAntialiasing(true, antiAliasingSteps); + else + viewer->setAntialiasing(false,0); viewer->viewAll(); } @@ -154,7 +160,7 @@ void BulletCoinQtViewer::scheduleRedraw() void BulletCoinQtViewer::stepPhysics() { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); + MutexLockPtr lock = getScopedLock(); //simple dynamics world doesn't handle fixed-time-stepping double ms = getDeltaTimeMicroseconds(); @@ -216,8 +222,8 @@ void BulletCoinQtViewer::viewAll() void BulletCoinQtViewer::addVisualization(RobotPtr robot, VirtualRobot::SceneObject::VisualizationType visuType, SoSeparator* container) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - //VR_ASSERT(so); + MutexLockPtr lock = getScopedLock(); + //VR_ASSERT(so); removeVisualization(robot); boost::shared_ptr<VirtualRobot::CoinVisualization> visualization = robot->getVisualization<CoinVisualization>(visuType); @@ -240,8 +246,8 @@ void BulletCoinQtViewer::addVisualization(RobotPtr robot, VirtualRobot::SceneObj void BulletCoinQtViewer::addVisualization(SceneObjectPtr so, VirtualRobot::SceneObject::VisualizationType visuType, SoSeparator* container) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(so); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(so); removeVisualization(so); SoNode * n = CoinVisualizationFactory::getCoinVisualization(so, visuType); if (n) @@ -261,8 +267,8 @@ void BulletCoinQtViewer::addVisualization(SceneObjectPtr so, VirtualRobot::Scene void BulletCoinQtViewer::addVisualization(DynamicsObjectPtr o, VirtualRobot::SceneObject::VisualizationType visuType, SoSeparator* container) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(o); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(o); SceneObjectPtr so = o->getSceneObject(); VR_ASSERT(so); removeVisualization(o); @@ -289,8 +295,8 @@ void BulletCoinQtViewer::addStepCallback(BulletStepCallback callback, void* data void BulletCoinQtViewer::addVisualization(DynamicsRobotPtr r, VirtualRobot::SceneObject::VisualizationType visuType, SoSeparator* container) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(r); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(r); RobotPtr ro = r->getRobot(); VR_ASSERT(ro); removeVisualization(r); @@ -320,8 +326,8 @@ void BulletCoinQtViewer::addVisualization(DynamicsRobotPtr r, VirtualRobot::Scen void BulletCoinQtViewer::removeVisualization( RobotPtr o ) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(o); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(o); if (addedSpriteRobotVisualizations.find(o) != addedSpriteRobotVisualizations.end()) { sceneGraph->removeChild(addedSpriteRobotVisualizations[o]); @@ -331,8 +337,8 @@ void BulletCoinQtViewer::removeVisualization( RobotPtr o ) void BulletCoinQtViewer::removeVisualization( SceneObjectPtr o ) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(o); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(o); if (addedSpriteVisualizations.find(o) != addedSpriteVisualizations.end()) { sceneGraph->removeChild(addedSpriteVisualizations[o]); @@ -342,8 +348,8 @@ void BulletCoinQtViewer::removeVisualization( SceneObjectPtr o ) void BulletCoinQtViewer::removeVisualization( DynamicsObjectPtr o ) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(o); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(o); if (addedVisualizations.find(o) != addedVisualizations.end()) { sceneGraph->removeChild(addedVisualizations[o]); @@ -353,8 +359,8 @@ void BulletCoinQtViewer::removeVisualization( DynamicsObjectPtr o ) void BulletCoinQtViewer::removeVisualization( DynamicsRobotPtr r ) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(r); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(r); if (addedRobotVisualizations.find(r) != addedRobotVisualizations.end()) { sceneGraph->removeChild(addedRobotVisualizations[r]); @@ -364,8 +370,8 @@ void BulletCoinQtViewer::removeVisualization( DynamicsRobotPtr r ) void BulletCoinQtViewer::stopCB() { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - if (timerSensor) + MutexLockPtr lock = getScopedLock(); + if (timerSensor) { SoSensorManager *sensor_mgr = SoDB::getSensorManager(); sensor_mgr->removeTimerSensor(timerSensor); @@ -381,39 +387,36 @@ void BulletCoinQtViewer::stopCB() void BulletCoinQtViewer::setBulletSimTimeStepMsec(int msec) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(msec > 0); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(msec > 0); bulletTimeStepMsec = msec; } void BulletCoinQtViewer::setBulletSimMaxSubSteps(int n) { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - VR_ASSERT(n > 0); + MutexLockPtr lock = getScopedLock(); + VR_ASSERT(n > 0); bulletMaxSubSteps = n; } bool BulletCoinQtViewer::engineRunning() { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - return enablePhysicsUpdates; + return enablePhysicsUpdates; } void BulletCoinQtViewer::stopEngine() { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - enablePhysicsUpdates = false; + MutexLockPtr lock = getScopedLock(); + enablePhysicsUpdates = false; } void BulletCoinQtViewer::startEngine() { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - enablePhysicsUpdates = true; + MutexLockPtr lock = getScopedLock(); + enablePhysicsUpdates = true; } void BulletCoinQtViewer::updatePhysics() { - boost::recursive_mutex::scoped_lock scoped_lock(engineMutex); - if (enablePhysicsUpdates) stepPhysics(); } @@ -435,6 +438,24 @@ void BulletCoinQtViewer::setUpdateInterval( int updateTimerIntervalMS ) timerSensor->setInterval(SbTime(float(updateTimerIntervalMS)/1000.0f)); } +void BulletCoinQtViewer::setMutex(boost::shared_ptr<boost::recursive_mutex> engineMutexPtr) +{ + this->engineMutexPtr = engineMutexPtr; +} + +BulletCoinQtViewer::MutexLockPtr BulletCoinQtViewer::getScopedLock() +{ + boost::shared_ptr< boost::recursive_mutex::scoped_lock > scoped_lock; + if (engineMutexPtr) + scoped_lock.reset(new boost::recursive_mutex::scoped_lock(*engineMutexPtr)); + return scoped_lock; +} + +void BulletCoinQtViewer::setAntiAliasing(int steps) +{ + MutexLockPtr lock = getScopedLock(); + viewer->setAntialiasing(steps > 0, steps); +} } diff --git a/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.h b/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.h index b77ac778a99bfa7b6296628d75573065d6d3d020..2b9029e0880b02e0f8dc5e81efb0186ae56ade4a 100644 --- a/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.h +++ b/SimDynamics/DynamicsEngine/BulletEngine/BulletCoinQtViewer.h @@ -55,8 +55,9 @@ public: Call this method to initialize the 3d viewer. \param embedViewer The 3d viewer will be embedded in this QFrame. \param scene The scene that should be displayed. + \param antiAliasingSteps If >0, anti aliasing is enabled. The parameter defines the number of rendering steps. This may slow down rendering on old gfx cards. */ - virtual void initSceneGraph(QFrame* embedViewer, SoNode* scene); + virtual void initSceneGraph(QFrame* embedViewer, SoNode* scene, int antiAliasingSteps = 0); /*! In this mode, the time between two updates is measures and the engine is stepped accordingly. (standard) @@ -146,11 +147,35 @@ public: int getBulletSimMaxSubSteps() const { return bulletMaxSubSteps; } int getUpdateTimerInterval() const { return updateTimerIntervalMS; } + //! If steps==0 anti aliasing is disabled + void setAntiAliasing(int steps); + /*! * Adds callback that is called each time the engine is updated. */ void addStepCallback(BulletStepCallback callback, void* data); + //! If set, all actions are protected with this mutex + virtual void setMutex(boost::shared_ptr<boost::recursive_mutex> engineMutexPtr); + + typedef boost::shared_ptr< boost::recursive_mutex::scoped_lock > MutexLockPtr; + /*! + This lock can be used to protect data access. It locks the mutex until deletion. + If no mutex was specified, an empty lock will be returned which does not protect the engine calls (this is the standard behavior). + \see setMutex + + Exemplary usage: + { + MutexLockPtr lock = getScopedLock(); + // now the mutex is locked + + // access data + // ... + + } // end of scope -> lock gets deleted and mutex is released automatically + */ + MutexLockPtr getScopedLock(); + protected: //checks if physics engine is enabled and performes a time step. @@ -212,7 +237,7 @@ protected: bool enablePhysicsUpdates; int updateTimerIntervalMS; - boost::recursive_mutex engineMutex; + boost::shared_ptr <boost::recursive_mutex> engineMutexPtr; }; diff --git a/SimDynamics/DynamicsEngine/BulletEngine/BulletRobot.cpp b/SimDynamics/DynamicsEngine/BulletEngine/BulletRobot.cpp index 49b9be030734c6021edd266e1ef70a49a0f4a97d..caf3fd7fed14805ffaaa6ec68ac0767bbf971616 100644 --- a/SimDynamics/DynamicsEngine/BulletEngine/BulletRobot.cpp +++ b/SimDynamics/DynamicsEngine/BulletEngine/BulletRobot.cpp @@ -694,7 +694,8 @@ std::vector<BulletRobot::LinkInfo> BulletRobot::getLinks(BulletObjectPtr node) bool BulletRobot::attachObject(const string &nodeName, DynamicsObjectPtr object) { - return attachObjectLink(nodeName,object); + BulletRobot::LinkInfoPtr li = attachObjectLink(nodeName, object); + return bool(li); } BulletRobot::LinkInfoPtr BulletRobot::attachObjectLink(const string &nodeName, DynamicsObjectPtr object) diff --git a/SimDynamics/examples/SimDynamicsViewer/simDynamicsViewer.ui b/SimDynamics/examples/SimDynamicsViewer/simDynamicsViewer.ui index 785563e38a4109f3b6a6912f758205c777cf6ad0..c70f4fc21c56c5fe6ece924746f9c59b7eebe386 100644 --- a/SimDynamics/examples/SimDynamicsViewer/simDynamicsViewer.ui +++ b/SimDynamics/examples/SimDynamicsViewer/simDynamicsViewer.ui @@ -199,8 +199,8 @@ <property name="geometry"> <rect> <x>10</x> - <y>600</y> - <width>201</width> + <y>610</y> + <width>381</width> <height>91</height> </rect> </property> @@ -246,6 +246,32 @@ <string>Collision Model</string> </property> </widget> + <widget class="QSpinBox" name="spinBoxAntiAliasing"> + <property name="geometry"> + <rect> + <x>240</x> + <y>40</y> + <width>42</width> + <height>22</height> + </rect> + </property> + <property name="maximum"> + <number>6</number> + </property> + </widget> + <widget class="QLabel" name="label"> + <property name="geometry"> + <rect> + <x>240</x> + <y>20</y> + <width>111</width> + <height>16</height> + </rect> + </property> + <property name="text"> + <string>Anti Aliasing</string> + </property> + </widget> </widget> <widget class="QGroupBox" name="groupBox_4"> <property name="geometry"> diff --git a/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.cpp b/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.cpp index 3ffb5573e51c4a259ad14cf68317bc0e3e1e2a6e..057785dc65f978e6c6cb13bf5ecfbc26de4e5985 100644 --- a/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.cpp +++ b/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.cpp @@ -155,6 +155,7 @@ void SimDynamicsWindow::setupUI() UI.horizontalSliderUpdateTimer->setValue(viewer->getUpdateTimerInterval()); connect(UI.horizontalSliderFixedTimeStep, SIGNAL(valueChanged(int)), this, SLOT(fixedTimeStepChanged(int))); connect(UI.horizontalSliderUpdateTimer, SIGNAL(valueChanged(int)), this, SLOT(updateTimerChanged(int))); + connect(UI.spinBoxAntiAliasing, SIGNAL(valueChanged(int)), this, SLOT(updateAntiAliasing(int))); /*connect(UI.pushButtonLoad, SIGNAL(clicked()), this, SLOT(selectRobot())); connect(UI.pushButtonClose, SIGNAL(clicked()), this, SLOT(closeHand())); @@ -797,3 +798,8 @@ void SimDynamicsWindow::updateTimerChanged( int n ) viewer->setUpdateInterval(n); } + +void SimDynamicsWindow::updateAntiAliasing(int n) +{ + viewer->setAntiAliasing(n); +} diff --git a/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.h b/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.h index 135e33e5b2c02f58f45876c97e6330b47d1c8b0f..c0e22d1d59176bf9cbf544c637c7a37eb5c28ad2 100644 --- a/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.h +++ b/SimDynamics/examples/SimDynamicsViewer/simDynamicsWindow.h @@ -57,8 +57,9 @@ public slots: void selectRobotNode(int n); void jointValueChanged(int n); void fixedTimeStepChanged(int n); - void updateTimerChanged(int n); - void comVisu(); + void updateTimerChanged(int n); + void updateAntiAliasing(int n); + void comVisu(); void updateJointInfo(); void startStopEngine();