Merge pull request #3965 from Atlante45/dependancy_manager

Dependancy manager
This commit is contained in:
Brad Hefta-Gaub 2014-12-15 08:12:04 -08:00
commit 0b85a4cc49
13 changed files with 167 additions and 62 deletions

View file

@ -55,6 +55,7 @@
#include <AddressManager.h> #include <AddressManager.h>
#include <AccountManager.h> #include <AccountManager.h>
#include <AudioInjector.h> #include <AudioInjector.h>
#include <DependencyManager.h>
#include <EntityScriptingInterface.h> #include <EntityScriptingInterface.h>
#include <HFActionEvent.h> #include <HFActionEvent.h>
#include <HFBackEvent.h> #include <HFBackEvent.h>
@ -76,10 +77,13 @@
#include "ModelUploader.h" #include "ModelUploader.h"
#include "Util.h" #include "Util.h"
#include "devices/DdeFaceTracker.h"
#include "devices/Faceshift.h"
#include "devices/Leapmotion.h" #include "devices/Leapmotion.h"
#include "devices/MIDIManager.h" #include "devices/MIDIManager.h"
#include "devices/OculusManager.h" #include "devices/OculusManager.h"
#include "devices/TV3DManager.h" #include "devices/TV3DManager.h"
#include "devices/Visage.h"
#include "renderer/ProgramObject.h" #include "renderer/ProgramObject.h"
#include "gpu/Batch.h" #include "gpu/Batch.h"
@ -1690,9 +1694,13 @@ int Application::getMouseDragStartedY() const {
} }
FaceTracker* Application::getActiveFaceTracker() { FaceTracker* Application::getActiveFaceTracker() {
return (_dde.isActive() ? static_cast<FaceTracker*>(&_dde) : Faceshift* faceshift = DependencyManager::get<Faceshift>();
(_faceshift.isActive() ? static_cast<FaceTracker*>(&_faceshift) : Visage* visage = DependencyManager::get<Visage>();
(_visage.isActive() ? static_cast<FaceTracker*>(&_visage) : NULL))); DdeFaceTracker* dde = DependencyManager::get<DdeFaceTracker>();
return (dde->isActive() ? static_cast<FaceTracker*>(dde) :
(faceshift->isActive() ? static_cast<FaceTracker*>(faceshift) :
(visage->isActive() ? static_cast<FaceTracker*>(visage) : NULL)));
} }
struct SendVoxelsOperationArgs { struct SendVoxelsOperationArgs {
@ -1976,8 +1984,8 @@ void Application::init() {
#endif #endif
// initialize our face trackers after loading the menu settings // initialize our face trackers after loading the menu settings
_faceshift.init(); DependencyManager::get<Faceshift>()->init();
_visage.init(); DependencyManager::get<Visage>()->init();
Leapmotion::init(); Leapmotion::init();
@ -2101,13 +2109,13 @@ void Application::updateMouseRay() {
void Application::updateFaceshift() { void Application::updateFaceshift() {
bool showWarnings = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings); bool showWarnings = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings);
PerformanceWarning warn(showWarnings, "Application::updateFaceshift()"); PerformanceWarning warn(showWarnings, "Application::updateFaceshift()");
Faceshift* faceshift = DependencyManager::get<Faceshift>();
// Update faceshift // Update faceshift
_faceshift.update(); faceshift->update();
// Copy angular velocity if measured by faceshift, to the head // Copy angular velocity if measured by faceshift, to the head
if (_faceshift.isActive()) { if (faceshift->isActive()) {
_myAvatar->getHead()->setAngularVelocity(_faceshift.getHeadAngularVelocity()); _myAvatar->getHead()->setAngularVelocity(faceshift->getHeadAngularVelocity());
} }
} }
@ -2116,7 +2124,7 @@ void Application::updateVisage() {
PerformanceWarning warn(showWarnings, "Application::updateVisage()"); PerformanceWarning warn(showWarnings, "Application::updateVisage()");
// Update Visage // Update Visage
_visage.update(); DependencyManager::get<Visage>()->update();
} }
void Application::updateDDE() { void Application::updateDDE() {
@ -2124,7 +2132,7 @@ void Application::updateDDE() {
PerformanceWarning warn(showWarnings, "Application::updateDDE()"); PerformanceWarning warn(showWarnings, "Application::updateDDE()");
// Update Cara // Update Cara
_dde.update(); DependencyManager::get<DdeFaceTracker>()->update();
} }
void Application::updateMyAvatarLookAtPosition() { void Application::updateMyAvatarLookAtPosition() {
@ -3549,9 +3557,9 @@ void Application::deleteVoxelAt(const VoxelDetail& voxel) {
} }
void Application::resetSensors() { void Application::resetSensors() {
_faceshift.reset(); DependencyManager::get<Faceshift>()->reset();
_visage.reset(); DependencyManager::get<Visage>()->reset();
_dde.reset(); DependencyManager::get<DdeFaceTracker>()->reset();
OculusManager::reset(); OculusManager::reset();

View file

@ -54,11 +54,8 @@
#include "avatar/Avatar.h" #include "avatar/Avatar.h"
#include "avatar/AvatarManager.h" #include "avatar/AvatarManager.h"
#include "avatar/MyAvatar.h" #include "avatar/MyAvatar.h"
#include "devices/Faceshift.h"
#include "devices/PrioVR.h" #include "devices/PrioVR.h"
#include "devices/SixenseManager.h" #include "devices/SixenseManager.h"
#include "devices/Visage.h"
#include "devices/DdeFaceTracker.h"
#include "entities/EntityTreeRenderer.h" #include "entities/EntityTreeRenderer.h"
#include "renderer/AmbientOcclusionEffect.h" #include "renderer/AmbientOcclusionEffect.h"
#include "renderer/DeferredLightingEffect.h" #include "renderer/DeferredLightingEffect.h"
@ -100,6 +97,7 @@ class QMouseEvent;
class QSettings; class QSettings;
class QWheelEvent; class QWheelEvent;
class FaceTracker;
class Node; class Node;
class ProgramObject; class ProgramObject;
@ -220,10 +218,7 @@ public:
int getMouseDragStartedY() const; int getMouseDragStartedY() const;
int getTrueMouseDragStartedX() const { return _mouseDragStartedX; } int getTrueMouseDragStartedX() const { return _mouseDragStartedX; }
int getTrueMouseDragStartedY() const { return _mouseDragStartedY; } int getTrueMouseDragStartedY() const { return _mouseDragStartedY; }
bool getLastMouseMoveWasSimulated() const { return _lastMouseMoveWasSimulated;; } bool getLastMouseMoveWasSimulated() const { return _lastMouseMoveWasSimulated; }
Faceshift* getFaceshift() { return &_faceshift; }
Visage* getVisage() { return &_visage; }
DdeFaceTracker* getDDE() { return &_dde; }
FaceTracker* getActiveFaceTracker(); FaceTracker* getActiveFaceTracker();
PrioVR* getPrioVR() { return &_prioVR; } PrioVR* getPrioVR() { return &_prioVR; }
BandwidthMeter* getBandwidthMeter() { return &_bandwidthMeter; } BandwidthMeter* getBandwidthMeter() { return &_bandwidthMeter; }
@ -532,10 +527,6 @@ private:
AvatarManager _avatarManager; AvatarManager _avatarManager;
MyAvatar* _myAvatar; // TODO: move this and relevant code to AvatarManager (or MyAvatar as the case may be) MyAvatar* _myAvatar; // TODO: move this and relevant code to AvatarManager (or MyAvatar as the case may be)
Faceshift _faceshift;
Visage _visage;
DdeFaceTracker _dde;
PrioVR _prioVR; PrioVR _prioVR;
Camera _myCamera; // My view onto the world Camera _myCamera; // My view onto the world

View file

@ -31,12 +31,16 @@
#include <AccountManager.h> #include <AccountManager.h>
#include <AddressManager.h> #include <AddressManager.h>
#include <XmppClient.h> #include <DependencyManager.h>
#include <UUID.h> #include <UUID.h>
#include <UserActivityLogger.h> #include <UserActivityLogger.h>
#include <XmppClient.h>
#include "Application.h" #include "Application.h"
#include "AccountManager.h" #include "AccountManager.h"
#include "devices/Faceshift.h"
#include "devices/OculusManager.h"
#include "devices/Visage.h"
#include "Menu.h" #include "Menu.h"
#include "scripting/LocationScriptingInterface.h" #include "scripting/LocationScriptingInterface.h"
#include "scripting/MenuScriptingInterface.h" #include "scripting/MenuScriptingInterface.h"
@ -49,7 +53,6 @@
#include "ui/ModelsBrowser.h" #include "ui/ModelsBrowser.h"
#include "ui/LoginDialog.h" #include "ui/LoginDialog.h"
#include "ui/NodeBounds.h" #include "ui/NodeBounds.h"
#include "devices/OculusManager.h"
Menu* Menu::_instance = NULL; Menu* Menu::_instance = NULL;
@ -432,12 +435,12 @@ Menu::Menu() :
MenuOption::Faceshift, MenuOption::Faceshift,
0, 0,
true, true,
appInstance->getFaceshift(), DependencyManager::get<Faceshift>(),
SLOT(setTCPEnabled(bool))); SLOT(setTCPEnabled(bool)));
#endif #endif
#ifdef HAVE_VISAGE #ifdef HAVE_VISAGE
addCheckableActionToQMenuAndActionHash(avatarDebugMenu, MenuOption::Visage, 0, false, addCheckableActionToQMenuAndActionHash(avatarDebugMenu, MenuOption::Visage, 0, false,
appInstance->getVisage(), SLOT(updateEnabled())); DependencyManager::get<Visage>(), SLOT(updateEnabled()));
#endif #endif
addCheckableActionToQMenuAndActionHash(avatarDebugMenu, MenuOption::RenderSkeletonCollisionShapes); addCheckableActionToQMenuAndActionHash(avatarDebugMenu, MenuOption::RenderSkeletonCollisionShapes);

View file

@ -10,6 +10,9 @@
#include <glm/gtx/quaternion.hpp> #include <glm/gtx/quaternion.hpp>
#include <DependencyManager.h>
#include <devices/Faceshift.h>
#include <devices/DdeFaceTracker.h>
#include <NodeList.h> #include <NodeList.h>
#include "Application.h" #include "Application.h"
@ -75,11 +78,13 @@ void Head::simulate(float deltaTime, bool isMine, bool billboard) {
// Only use face trackers when not playing back a recording. // Only use face trackers when not playing back a recording.
if (!myAvatar->isPlaying()) { if (!myAvatar->isPlaying()) {
FaceTracker* faceTracker = Application::getInstance()->getActiveFaceTracker(); FaceTracker* faceTracker = Application::getInstance()->getActiveFaceTracker();
if ((_isFaceshiftConnected = faceTracker)) { DdeFaceTracker* dde = DependencyManager::get<DdeFaceTracker>();
Faceshift* faceshift = DependencyManager::get<Faceshift>();
if ((_isFaceshiftConnected = (faceshift == faceTracker))) {
_blendshapeCoefficients = faceTracker->getBlendshapeCoefficients(); _blendshapeCoefficients = faceTracker->getBlendshapeCoefficients();
_isFaceshiftConnected = true; } else if (dde->isActive()) {
} else if (Application::getInstance()->getDDE()->isActive()) { faceTracker = dde;
faceTracker = Application::getInstance()->getDDE();
_blendshapeCoefficients = faceTracker->getBlendshapeCoefficients(); _blendshapeCoefficients = faceTracker->getBlendshapeCoefficients();
} }
} }
@ -196,14 +201,14 @@ void Head::simulate(float deltaTime, bool isMine, bool billboard) {
_mouth2 = glm::mix(_audioJawOpen * MMMM_POWER, _mouth2, MMMM_PERIOD + randFloat() * MMMM_RANDOM_PERIOD); _mouth2 = glm::mix(_audioJawOpen * MMMM_POWER, _mouth2, MMMM_PERIOD + randFloat() * MMMM_RANDOM_PERIOD);
_mouth4 = glm::mix(_audioJawOpen, _mouth4, SMILE_PERIOD + randFloat() * SMILE_RANDOM_PERIOD); _mouth4 = glm::mix(_audioJawOpen, _mouth4, SMILE_PERIOD + randFloat() * SMILE_RANDOM_PERIOD);
Application::getInstance()->getFaceshift()->updateFakeCoefficients(_leftEyeBlink, DependencyManager::get<Faceshift>()->updateFakeCoefficients(_leftEyeBlink,
_rightEyeBlink, _rightEyeBlink,
_browAudioLift, _browAudioLift,
_audioJawOpen, _audioJawOpen,
_mouth2, _mouth2,
_mouth3, _mouth3,
_mouth4, _mouth4,
_blendshapeCoefficients); _blendshapeCoefficients);
} else { } else {
_saccade = glm::vec3(); _saccade = glm::vec3();
} }

View file

@ -22,6 +22,7 @@
#include <AccountManager.h> #include <AccountManager.h>
#include <AddressManager.h> #include <AddressManager.h>
#include <DependencyManager.h>
#include <GeometryUtil.h> #include <GeometryUtil.h>
#include <NodeList.h> #include <NodeList.h>
#include <PacketHeaders.h> #include <PacketHeaders.h>
@ -421,8 +422,7 @@ void MyAvatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bo
} }
void MyAvatar::renderHeadMouse(int screenWidth, int screenHeight) const { void MyAvatar::renderHeadMouse(int screenWidth, int screenHeight) const {
Faceshift* faceshift = DependencyManager::get<Faceshift>();
Faceshift* faceshift = Application::getInstance()->getFaceshift();
float pixelsPerDegree = screenHeight / Menu::getInstance()->getFieldOfView(); float pixelsPerDegree = screenHeight / Menu::getInstance()->getFieldOfView();

View file

@ -14,16 +14,14 @@
#include <QUdpSocket> #include <QUdpSocket>
#include <DependencyManager.h>
#include "FaceTracker.h" #include "FaceTracker.h"
class DdeFaceTracker : public FaceTracker { class DdeFaceTracker : public FaceTracker, public DependencyManager::Dependency {
Q_OBJECT Q_OBJECT
public: public:
DdeFaceTracker();
DdeFaceTracker(const QHostAddress& host, quint16 port);
~DdeFaceTracker();
//initialization //initialization
void init(); void init();
void reset(); void reset();
@ -57,6 +55,11 @@ private slots:
void socketStateChanged(QAbstractSocket::SocketState socketState); void socketStateChanged(QAbstractSocket::SocketState socketState);
private: private:
DdeFaceTracker();
DdeFaceTracker(const QHostAddress& host, quint16 port);
~DdeFaceTracker();
friend DependencyManager;
float getBlendshapeCoefficient(int index) const; float getBlendshapeCoefficient(int index) const;
void decodePacket(const QByteArray& buffer); void decodePacket(const QByteArray& buffer);

View file

@ -23,8 +23,8 @@ class FaceTracker : public QObject {
Q_OBJECT Q_OBJECT
public: public:
FaceTracker(); FaceTracker();
virtual ~FaceTracker() {}
const glm::vec3& getHeadTranslation() const { return _headTranslation; } const glm::vec3& getHeadTranslation() const { return _headTranslation; }
const glm::quat& getHeadRotation() const { return _headRotation; } const glm::quat& getHeadRotation() const { return _headRotation; }

View file

@ -14,7 +14,6 @@
#include <PerfStat.h> #include <PerfStat.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include "Application.h"
#include "Faceshift.h" #include "Faceshift.h"
#include "Menu.h" #include "Menu.h"
#include "Util.h" #include "Util.h"

View file

@ -19,15 +19,15 @@
#include <fsbinarystream.h> #include <fsbinarystream.h>
#endif #endif
#include <DependencyManager.h>
#include "FaceTracker.h" #include "FaceTracker.h"
/// Handles interaction with the Faceshift software, which provides head position/orientation and facial features. /// Handles interaction with the Faceshift software, which provides head position/orientation and facial features.
class Faceshift : public FaceTracker { class Faceshift : public FaceTracker, public DependencyManager::Dependency {
Q_OBJECT Q_OBJECT
public: public:
Faceshift();
void init(); void init();
bool isConnectedOrConnecting() const; bool isConnectedOrConnecting() const;
@ -87,6 +87,9 @@ private slots:
void readFromSocket(); void readFromSocket();
private: private:
Faceshift();
virtual ~Faceshift() {}
friend DependencyManager;
float getBlendshapeCoefficient(int index) const; float getBlendshapeCoefficient(int index) const;

View file

@ -11,12 +11,14 @@
#include <QHash> #include <QHash>
#include <DependencyManager.h>
#include <FBXReader.h>
#include <PerfStat.h> #include <PerfStat.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include <FBXReader.h>
#include "Application.h" #include "Application.h"
#include "Faceshift.h"
#include "Visage.h" #include "Visage.h"
// this has to go after our normal includes, because its definition of HANDLE conflicts with Qt's // this has to go after our normal includes, because its definition of HANDLE conflicts with Qt's
@ -119,7 +121,7 @@ static const QMultiHash<QByteArray, QPair<int, float> >& getActionUnitNameMap()
const float TRANSLATION_SCALE = 20.0f; const float TRANSLATION_SCALE = 20.0f;
void Visage::init() { void Visage::init() {
connect(Application::getInstance()->getFaceshift(), SIGNAL(connectionStateChanged()), SLOT(updateEnabled())); connect(DependencyManager::get<Faceshift>(), SIGNAL(connectionStateChanged()), SLOT(updateEnabled()));
updateEnabled(); updateEnabled();
} }
@ -171,7 +173,7 @@ void Visage::reset() {
void Visage::updateEnabled() { void Visage::updateEnabled() {
setEnabled(Menu::getInstance()->isOptionChecked(MenuOption::Visage) && setEnabled(Menu::getInstance()->isOptionChecked(MenuOption::Visage) &&
!(Menu::getInstance()->isOptionChecked(MenuOption::Faceshift) && !(Menu::getInstance()->isOptionChecked(MenuOption::Faceshift) &&
Application::getInstance()->getFaceshift()->isConnectedOrConnecting())); DependencyManager::get<Faceshift>()->isConnectedOrConnecting()));
} }
void Visage::setEnabled(bool enabled) { void Visage::setEnabled(bool enabled) {

View file

@ -16,6 +16,8 @@
#include <QPair> #include <QPair>
#include <QVector> #include <QVector>
#include <DependencyManager.h>
#include "FaceTracker.h" #include "FaceTracker.h"
namespace VisageSDK { namespace VisageSDK {
@ -24,14 +26,10 @@ namespace VisageSDK {
} }
/// Handles input from the Visage webcam feature tracking software. /// Handles input from the Visage webcam feature tracking software.
class Visage : public FaceTracker { class Visage : public FaceTracker, public DependencyManager::Dependency {
Q_OBJECT Q_OBJECT
public: public:
Visage();
virtual ~Visage();
void init(); void init();
bool isActive() const { return _active; } bool isActive() const { return _active; }
@ -44,6 +42,9 @@ public slots:
void updateEnabled(); void updateEnabled();
private: private:
Visage();
virtual ~Visage();
friend DependencyManager;
#ifdef HAVE_VISAGE #ifdef HAVE_VISAGE
VisageSDK::VisageTracker2* _tracker; VisageSDK::VisageTracker2* _tracker;

View file

@ -0,0 +1,24 @@
//
// DependencyManager.cpp
//
//
// Created by Clément Brisset on 12/10/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "DependencyManager.h"
DependencyManager& DependencyManager::getInstance() {
static DependencyManager instance;
return instance;
}
DependencyManager::~DependencyManager() {
foreach (Dependency* instance, _instanceHash) {
delete instance;
}
_instanceHash.clear();
}

View file

@ -0,0 +1,66 @@
//
// DependencyManager.h
//
//
// Created by Clément Brisset on 12/10/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_DependencyManager_h
#define hifi_DependencyManager_h
#include <QHash>
#include <QString>
#include <typeinfo>
#include <assert.h>
class DependencyManager {
public:
// Only accessible method.
// usage: T* instance = DependencyManager::get<T>();
template<typename T>
static T* get();
// Any class T in the DependencyManager needs to subclass Dependency
// They also need to have protected constructor(s) and virtual destructor
// As well as declare DependencyManager a friend class
class Dependency {
protected:
Dependency() {}
virtual ~Dependency() {} // Ensure the proper destruction of the object
friend DependencyManager;
};
private:
static DependencyManager& getInstance();
DependencyManager() {}
~DependencyManager();
typedef QHash<QString, Dependency*> InstanceHash;
static InstanceHash& getInstanceHash() { return getInstance()._instanceHash; }
InstanceHash _instanceHash;
};
template <typename T>
T* DependencyManager::get() {
const QString& typeId = typeid(T).name();
// Search the hash for global instance
Dependency* instance = getInstanceHash().value(typeId, NULL);
if (instance) {
return dynamic_cast<T*>(instance);
}
// Found no instance in hash so we create one.
T* newInstance = new T();
instance = dynamic_cast<Dependency*>(newInstance);
assert(instance != NULL); // If this triggers, check that T is derived from Dependency
getInstanceHash().insert(typeId, instance);
return newInstance;
}
#endif // hifi_DependencyManager_h