From 49ca8eb2aa00bd0d6ff511bd1e73b49f16670676 Mon Sep 17 00:00:00 2001 From: Jonathan Bosson Date: Wed, 22 Feb 2017 15:41:52 -0700 Subject: [PATCH] save input safely between frames with cursor->getPath() and clear list (& refrence path) after process to not lose any information --- modules/touch/include/TuioEar.h | 8 +-- modules/touch/src/TuioEar.cpp | 82 ++++++++++++++++++++++------- modules/touch/touchmodule.cpp | 61 ++++++++++----------- src/interaction/interactionmode.cpp | 2 +- 4 files changed, 97 insertions(+), 56 deletions(-) diff --git a/modules/touch/include/TuioEar.h b/modules/touch/include/TuioEar.h index 93d5e19a6d..ffee968e3a 100644 --- a/modules/touch/include/TuioEar.h +++ b/modules/touch/include/TuioEar.h @@ -60,15 +60,17 @@ class TuioEar : public TuioListener { void refresh(TuioTime frameTime); - std::vector getInput(); + TuioTime getLastProcessedTime(int id); + std::vector getInput(); void clearInput(); - void unlock() { _mx.unlock(); }; private: TuioClient *_tuioClient; OscReceiver *_oscReceiver; - std::vector _list; + std::vector _list; + std::vector _removeList; + std::vector> _processedPath; std::mutex _mx; }; diff --git a/modules/touch/src/TuioEar.cpp b/modules/touch/src/TuioEar.cpp index 8dc8aa58e5..7704b348da 100644 --- a/modules/touch/src/TuioEar.cpp +++ b/modules/touch/src/TuioEar.cpp @@ -34,6 +34,7 @@ #include #include +#include namespace { const std::string _loggerCat = "TuioEar"; @@ -53,26 +54,49 @@ void TuioEar::removeTuioObject(TuioObject *tobj) { } void TuioEar::addTuioCursor(TuioCursor *tcur) { - _mx.lock(); - _list.push_back(new TuioCursor(tcur)); - _mx.unlock(); + _mx.lock(); - //LINFO("add cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/" << tcur->getTuioSourceID() << ") " << tcur->getX() << " " << tcur->getY() << " " << tobj->getY() << ", size: " << _list.size() << "\n"); - //std::cout << "add cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/" << tcur->getTuioSourceID() << ") " << tcur->getX() << " " << tcur->getY() << ", size: " << _list.size() << "\n"; + // find same id in _list if it exists in _removeList (new input with same ID as a previously stored) + int i = tcur->getSessionID(); + auto foundID = std::find_if( + _removeList.begin(), + _removeList.end(), + [&i](int id) { return id == i; }); + + // if found, remove id from _removeList and update, otherwise add new id to list + if (foundID != _removeList.end()) { + std::find_if( + _list.begin(), + _list.end(), + [&i](const TuioCursor& cursor) { + return cursor.getSessionID() == i; + })->update(tcur); + _removeList.erase(foundID); + } + else + _list.push_back(TuioCursor(*tcur)); + + _mx.unlock(); } void TuioEar::updateTuioCursor(TuioCursor *tcur) { _mx.lock(); - _list.push_back(new TuioCursor(tcur)); + int i = tcur->getSessionID(); + std::find_if( + _list.begin(), + _list.end(), + [&i](const TuioCursor& cursor) { + return cursor.getSessionID() == i; + })->update(tcur); _mx.unlock(); - - //LINFO("set obj " << tobj->getSymbolID() << " (" << tobj->getSessionID() << "/" << tobj->getTuioSourceID() << ") " << tobj->getX() << " " << tobj->getY() << ", size: " << _list.size() << "\n"); - //std::cout << "set cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/" << tcur->getTuioSourceID() << ") " << tcur->getX() << " " << tcur->getY() - //<< " " << tcur->getMotionSpeed() << " " << tcur->getMotionAccel() << " " << ", size: " << _list.size() << "\n"; } +// save id to be removed and remove it in clearInput void TuioEar::removeTuioCursor(TuioCursor *tcur) { - //std::cout << "del cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/" << tcur->getTuioSourceID() << ")" << std::endl; + _mx.lock(); + _removeList.push_back(tcur->getSessionID()); + //LINFO("To be removed: " << _removeFromList.size() << "\n"); + _mx.unlock(); } void TuioEar::addTuioBlob(TuioBlob *tblb) { @@ -88,22 +112,44 @@ void TuioEar::removeTuioBlob(TuioBlob *tblb) { std::cout << "del blb " << tblb->getBlobID() << " (" << tblb->getSessionID() << "/" << tblb->getTuioSourceID() << ")" << std::endl; } - void TuioEar::refresh(TuioTime frameTime) { //LINFO("refresh " << frameTime.getTotalMilliseconds() << "\n"); // about every 15ms on TuioPad app } -std::vector TuioEar::getInput() { - _mx.lock(); +TuioTime TuioEar::getLastProcessedTime(int id) { + return std::find_if( + _processedPath.begin(), + _processedPath.end(), + [&id](std::pair t) { return id = t.first; } + )->second; +} + +std::vector TuioEar::getInput() { + std::lock_guard lock(_mx); + _processedPath.clear(); + for (const TuioCursor& c : _list) + _processedPath.push_back(std::make_pair(c.getSessionID(), c.getTuioTime())); return _list; } void TuioEar::clearInput() { _mx.lock(); - for (auto &&j : _list) { - delete j; - } - _list.clear(); + _list.erase( + std::remove_if( + _list.begin(), + _list.end(), + [this](const TuioCursor& cursor) { + return std::find_if( + _removeList.begin(), + _removeList.end(), + [&cursor](int id) { + return cursor.getSessionID() == id; + } + ) != _removeList.end(); + }), + _list.end() + ); + _removeList.clear(); _mx.unlock(); } diff --git a/modules/touch/touchmodule.cpp b/modules/touch/touchmodule.cpp index 5189a1ffb4..f949c380f8 100644 --- a/modules/touch/touchmodule.cpp +++ b/modules/touch/touchmodule.cpp @@ -39,6 +39,12 @@ #include #include #include +#include // std::this_thread::sleep_for +#include // std::chrono::seconds + +namespace { + const std::string _loggerCat = "TouchModule"; +} namespace openspace { @@ -67,52 +73,39 @@ TouchModule::TouchModule() OsEng.registerModuleCallback( // maybe call ear->clearInput() here rather than postdraw OpenSpaceEngine::CallbackOption::PreSync, [&]() { - std::vector list = ear->getInput(); - std::vector group; + //std::this_thread::sleep_for(std::chrono::seconds(1)); + std::vector list = ear->getInput(); + ear->clearInput(); glm::vec2 centroid; - ear->unlock(); + //print list for debugging std::string s = ""; - const std::string _loggerCat = "TouchModule"; std::ostringstream os; - for (auto &&j : list) { - os << " (" << j->getX() << "," << j->getY() << ") "; + for (const TuioCursor &j : list) { + + os << j.getCursorID() << ", path size: " << j.getPath().size() << ", (" << j.getX() << "," << j.getY() << ") "; } if (list.size() > 0) - LINFO("List size: " << list.size() << os.str() << "\n"); + LINFO("List size: " << list.size() << ", Id: " << os.str() << "\n"); os.clear(); - - - ear->clearInput(); /* - // step through the list (from the start) and find each unique id TuioObject - for (auto &&i : list) { - bool sameId = false; + // calculate centroid if multipleID + if (list.size() > 1) { centroid = glm::vec2(0.0f, 0.0f); - if (i->containsTuioPointer()) { // sanity check - int id = i->getSessionID(); - for (auto &&j : group) // change to lambda/find function - if (j->getSessionID() == id) - sameId = true; // step out of for - if (sameId) { // calculate a centroid - for (auto &&j : group) { - centroid.x += j->getTuioPointer()->getX(); - centroid.y += j->getTuioPointer()->getY(); - } - centroid.x /= group.size(); - centroid.y /= group.size(); - } - else - group.push_back(i); - - + for (auto &&i : list) { + centroid.x += i->getX(); + centroid.y += i->getY(); } - }*/ + centroid.x /= list.size(); + centroid.y /= list.size(); + + //LINFO("List size: " << list.size() << ", Centroid: (" << centroid.x << ", " << centroid.y << ")" << "\n"); + } + */ - //if (centroid.x + centroid.y != 0.0f) - //LINFO("List size: " << list.size() << ", Centroid: (" << centroid.x << ", " << centroid.y << ")\n"); - // group + glm::mat4 t; + //OsEng.interactionHandler().camera()->rotate(); } ); diff --git a/src/interaction/interactionmode.cpp b/src/interaction/interactionmode.cpp index d6d18bed88..9b6e88822a 100644 --- a/src/interaction/interactionmode.cpp +++ b/src/interaction/interactionmode.cpp @@ -201,7 +201,7 @@ Interpolator& InteractionMode::rotateToFocusNodeInterpolator() { // KeyframeInteractionMode -KeyframeInteractionMode::KeyframeInteractionMode(){ +KeyframeInteractionMode::KeyframeInteractionMode() { }