From 264b65813211eee445b936994494a406844ebd14 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 22 Oct 2015 11:05:48 -0700 Subject: [PATCH] hook up entities and avatars to SpatialParentFinder --- .../src/entities/AssignmentParentFinder.cpp | 22 +++++ .../src/entities/AssignmentParentFinder.h | 34 +++++++ .../src/entities/EntityServer.cpp | 5 + interface/src/Application.cpp | 4 + interface/src/InterfaceParentFinder.cpp | 35 +++++++ interface/src/InterfaceParentFinder.h | 27 ++++++ libraries/entities/src/EntityItem.cpp | 13 ++- libraries/entities/src/EntityItem.h | 4 - libraries/shared/src/SpatialParentFinder.h | 39 ++++++++ libraries/shared/src/SpatiallyNestable.cpp | 96 +++++++++++++++++-- libraries/shared/src/SpatiallyNestable.h | 37 +++++-- 11 files changed, 296 insertions(+), 20 deletions(-) create mode 100644 assignment-client/src/entities/AssignmentParentFinder.cpp create mode 100644 assignment-client/src/entities/AssignmentParentFinder.h create mode 100644 interface/src/InterfaceParentFinder.cpp create mode 100644 interface/src/InterfaceParentFinder.h create mode 100644 libraries/shared/src/SpatialParentFinder.h diff --git a/assignment-client/src/entities/AssignmentParentFinder.cpp b/assignment-client/src/entities/AssignmentParentFinder.cpp new file mode 100644 index 0000000000..6a66aa3f73 --- /dev/null +++ b/assignment-client/src/entities/AssignmentParentFinder.cpp @@ -0,0 +1,22 @@ +// +// AssignmentParentFinder.cpp +// assignment-client/src/ +// +// Created by Seth Alves on 2015-10-22 +// Copyright 2015 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 "AssignmentParentFinder.h" + +SpatiallyNestableWeakPointer AssignmentParentFinder::find(QUuid parentID) const { + assert(false); + SpatiallyNestableWeakPointer parent; + // search entities + _tree->withReadLock([&] { + parent = _tree->findEntityByEntityItemID(parentID); + }); + return parent; +} diff --git a/assignment-client/src/entities/AssignmentParentFinder.h b/assignment-client/src/entities/AssignmentParentFinder.h new file mode 100644 index 0000000000..4d2e080443 --- /dev/null +++ b/assignment-client/src/entities/AssignmentParentFinder.h @@ -0,0 +1,34 @@ +// +// AssignmentParentFinder.h +// interface/src/ +// +// Created by Seth Alves on 2015-10-21 +// Copyright 2015 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_AssignmentParentFinder_h +#define hifi_AssignmentParentFinder_h + +#include +#include + +#include +#include + +// This interface is used to turn a QUuid into a pointer to a "parent" -- something that children can +// be spatially relative to. At this point, this means either an EntityItem or an Avatar. + +class AssignmentParentFinder : public SpatialParentFinder { +public: + AssignmentParentFinder(EntityTreePointer tree) : _tree(tree) { } + virtual ~AssignmentParentFinder() { } + virtual SpatiallyNestableWeakPointer find(QUuid parentID) const; + +protected: + EntityTreePointer _tree; +}; + +#endif // hifi_AssignmentParentFinder_h diff --git a/assignment-client/src/entities/EntityServer.cpp b/assignment-client/src/entities/EntityServer.cpp index f2a4c2664a..5c9b120a3a 100644 --- a/assignment-client/src/entities/EntityServer.cpp +++ b/assignment-client/src/entities/EntityServer.cpp @@ -16,6 +16,7 @@ #include "EntityServer.h" #include "EntityServerConsts.h" #include "EntityNodeData.h" +#include "AssignmentParentFinder.h" const char* MODEL_SERVER_NAME = "Entity"; const char* MODEL_SERVER_LOGGING_TARGET_NAME = "entity-server"; @@ -60,6 +61,10 @@ OctreePointer EntityServer::createTree() { tree->setSimulation(simpleSimulation); _entitySimulation = simpleSimulation; } + + DependencyManager::registerInheritance(); + DependencyManager::set(tree); + return tree; } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index cab189e7a0..1a02cf912e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -136,6 +136,8 @@ #include "ui/Stats.h" #include "ui/UpdateDialog.h" #include "Util.h" +#include "InterfaceParentFinder.h" + // ON WIndows PC, NVidia Optimus laptop, we want to enable NVIDIA GPU // FIXME seems to be broken. @@ -286,6 +288,7 @@ bool setupEssentials(int& argc, char** argv) { DependencyManager::registerInheritance(); DependencyManager::registerInheritance(); DependencyManager::registerInheritance(); + DependencyManager::registerInheritance(); Setting::init(); @@ -327,6 +330,7 @@ bool setupEssentials(int& argc, char** argv) { DependencyManager::set(); DependencyManager::set(); DependencyManager::set(); + DependencyManager::set(); return true; } diff --git a/interface/src/InterfaceParentFinder.cpp b/interface/src/InterfaceParentFinder.cpp new file mode 100644 index 0000000000..f8604b15d3 --- /dev/null +++ b/interface/src/InterfaceParentFinder.cpp @@ -0,0 +1,35 @@ +// +// InterfaceParentFinder.cpp +// interface/src/ +// +// Created by Seth Alves on 2015-10-21 +// Copyright 2015 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 +#include +#include +#include + +#include "InterfaceParentFinder.h" + +SpatiallyNestableWeakPointer InterfaceParentFinder::find(QUuid parentID) const { + SpatiallyNestableWeakPointer parent; + + // search entities + EntityTreeRenderer* treeRenderer = qApp->getEntities(); + EntityTreePointer tree = treeRenderer->getTree(); + tree->withReadLock([&] { + parent = tree->findEntityByEntityItemID(parentID); + }); + if (!parent.expired()) { + return parent; + } + + // search avatars + QSharedPointer avatarManager = DependencyManager::get(); + return avatarManager->getAvatarBySessionID(parentID); +} diff --git a/interface/src/InterfaceParentFinder.h b/interface/src/InterfaceParentFinder.h new file mode 100644 index 0000000000..c8e8d4ed9f --- /dev/null +++ b/interface/src/InterfaceParentFinder.h @@ -0,0 +1,27 @@ +// +// InterfaceParentFinder.h +// interface/src/ +// +// Created by Seth Alves on 2015-10-21 +// Copyright 2015 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_InterfaceParentFinder_h +#define hifi_InterfaceParentFinder_h + +#include +#include + +#include + +class InterfaceParentFinder : public SpatialParentFinder { +public: + InterfaceParentFinder() { } + virtual ~InterfaceParentFinder() { } + virtual SpatiallyNestableWeakPointer find(QUuid parentID) const; +}; + +#endif // hifi_InterfaceParentFinder_h diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index c2e6a90ca8..0a47a004fd 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -37,8 +37,8 @@ int EntityItem::_maxActionsDataSize = 800; quint64 EntityItem::_rememberDeletedActionTime = 20 * USECS_PER_SECOND; EntityItem::EntityItem(const EntityItemID& entityItemID) : + SpatiallyNestable(entityItemID), _type(EntityTypes::Unknown), - _id(entityItemID), _lastSimulated(0), _lastUpdated(0), _lastEdited(0), @@ -1723,6 +1723,9 @@ void EntityItem::checkWaitingToRemove(EntitySimulation* simulation) { } void EntityItem::setActionData(QByteArray actionData) { + if (_id == QUuid("32147a6e-976d-44ea-8c33-056c820b4dbd")) { + qDebug() << "EntityItem::setActionData to " << actionData.size() << "bytes."; + } withWriteLock([&] { setActionDataInternal(actionData); }); @@ -1730,8 +1733,16 @@ void EntityItem::setActionData(QByteArray actionData) { void EntityItem::setActionDataInternal(QByteArray actionData) { if (_allActionsDataCache != actionData) { + if (_id == QUuid("32147a6e-976d-44ea-8c33-056c820b4dbd")) { + qDebug() << "EntityItem::setActionDataInternal to " << actionData.size() << "bytes."; + } + _allActionsDataCache = actionData; deserializeActionsInternal(); + } else { + if (_id == QUuid("32147a6e-976d-44ea-8c33-056c820b4dbd")) { + qDebug() << "EntityItem::setActionDataInternal NOT setting to " << actionData.size() << "bytes."; + } } checkWaitingToRemove(); } diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 185d052bbd..79fe438c39 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -89,9 +89,6 @@ public: EntityItem(const EntityItemID& entityItemID); virtual ~EntityItem(); - // ID and EntityItemID related methods - const QUuid& getID() const { return _id; } - void setID(const QUuid& id) { _id = id; } EntityItemID getEntityItemID() const { return EntityItemID(_id); } // methods for getting/setting all properties of an entity @@ -399,7 +396,6 @@ protected: static bool _sendPhysicsUpdates; EntityTypes::EntityType _type; - QUuid _id; quint64 _lastSimulated; // last time this entity called simulate(), this includes velocity, angular velocity, // and physics changes quint64 _lastUpdated; // last time this entity called update(), this includes animations and non-physics changes diff --git a/libraries/shared/src/SpatialParentFinder.h b/libraries/shared/src/SpatialParentFinder.h new file mode 100644 index 0000000000..9cadbaf8ce --- /dev/null +++ b/libraries/shared/src/SpatialParentFinder.h @@ -0,0 +1,39 @@ +// +// SpatialParentFinder.h +// libraries/shared/src/ +// +// Created by Seth Alves on 2015-10-18 +// Copyright 2015 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_SpatialParentFinder_h +#define hifi_SpatialParentFinder_h + +#include + +#include "DependencyManager.h" + +class SpatiallyNestable; +using SpatiallyNestableWeakPointer = std::weak_ptr; +using SpatiallyNestablePointer = std::shared_ptr; +class SpatialParentFinder; +using SpatialParentFinderPointer = std::shared_ptr; +class SpatialParentFinder : public Dependency { + + + +// This interface is used to turn a QUuid into a pointer to a "parent" -- something that children can +// be spatially relative to. At this point, this means either an EntityItem or an Avatar. + + +public: + SpatialParentFinder() { } + virtual ~SpatialParentFinder() { } + + virtual SpatiallyNestableWeakPointer find(QUuid parentID) const = 0; +}; + +#endif // hifi_SpatialParentFinder_h diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index a3e427caa8..4cdf61061c 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -9,35 +9,84 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "DependencyManager.h" #include "SpatiallyNestable.h" -SpatiallyNestable::SpatiallyNestable() : - _transform() { +Transform SpatiallyNestable::getParentTransform() const { + Transform result; + SpatiallyNestablePointer parent = getParentPointer(); + if (parent) { + Transform parentTransform = parent->getTransform(); + result = parentTransform.setScale(1.0f); + } + return result; } +SpatiallyNestablePointer SpatiallyNestable::getParentPointer() const { + SpatiallyNestablePointer parent = _parent.lock(); + + if (!parent && _parentID.isNull()) { + // no parent + return nullptr; + } + + if (parent && parent->getID() == _parentID) { + // parent pointer is up-to-date + return parent; + } + + if (parent && _parentID.isNull()) { + // we have a parent pointer but our _parentID is null + _parent.reset(); + return nullptr; + } + + // we have a _parentID but no parent pointer, or our parent pointer is to the wrong thing + QSharedPointer parentFinder = DependencyManager::get(); + _parent = parentFinder->find(_parentID); + return _parent.lock(); +} const glm::vec3& SpatiallyNestable::getPosition() const { - return _transform.getTranslation(); + Transform parentTransformDescaled = getParentTransform(); + glm::mat4 parentMat; + parentTransformDescaled.getMatrix(parentMat); + glm::vec4 absPos = parentMat * glm::vec4(getLocalPosition(), 1.0f); + _absolutePositionCache = glm::vec3(absPos); + return _absolutePositionCache; } void SpatiallyNestable::setPosition(const glm::vec3& position) { - _transform.setTranslation(position); + Transform parentTransform = getParentTransform(); + Transform myWorldTransform; + Transform::mult(myWorldTransform, parentTransform, _transform); + myWorldTransform.setTranslation(position); + Transform::inverseMult(_transform, parentTransform, myWorldTransform); } const glm::quat& SpatiallyNestable::getOrientation() const { - return _transform.getRotation(); + Transform parentTransformDescaled = getParentTransform(); + _absoluteRotationCache = parentTransformDescaled.getRotation() * getLocalOrientation(); + return _absoluteRotationCache; } void SpatiallyNestable::setOrientation(const glm::quat& orientation) { - _transform.setRotation(orientation); + Transform parentTransform = getParentTransform(); + Transform myWorldTransform; + Transform::mult(myWorldTransform, parentTransform, _transform); + myWorldTransform.setRotation(orientation); + Transform::inverseMult(_transform, parentTransform, myWorldTransform); } const Transform& SpatiallyNestable::getTransform() const { - return _transform; + Transform parentTransform = getParentTransform(); + Transform::mult(_worldTransformCache, parentTransform, _transform); + return _worldTransformCache; } void SpatiallyNestable::setTransform(const Transform& transform) { - _transform = transform; + Transform parentTransform = getParentTransform(); + Transform::inverseMult(_transform, parentTransform, transform); } const glm::vec3& SpatiallyNestable::getScale() const { @@ -47,3 +96,34 @@ const glm::vec3& SpatiallyNestable::getScale() const { void SpatiallyNestable::setScale(const glm::vec3& scale) { _transform.setScale(scale); } + +const Transform& SpatiallyNestable::getLocalTransform() const { + return _transform; +} + +void SpatiallyNestable::setLocalTransform(const Transform& transform) { +} + +const glm::vec3& SpatiallyNestable::getLocalPosition() const { + return _transform.getTranslation(); +} + +void SpatiallyNestable::setLocalPosition(const glm::vec3& position) { + _transform.setTranslation(position); +} + +const glm::quat& SpatiallyNestable::getLocalOrientation() const { + return _transform.getRotation(); +} + +void SpatiallyNestable::setLocalOrientation(const glm::quat& orientation) { + _transform.setRotation(orientation); +} + +const glm::vec3& SpatiallyNestable::getLocalScale() const { + return _transform.getScale(); +} + +void SpatiallyNestable::setLocalScale(const glm::vec3& scale) { + _transform.setScale(scale); +} diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index 294450a980..e3d3b8dc0d 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -15,22 +15,29 @@ #include #include "Transform.h" +#include "SpatialParentFinder.h" class SpatiallyNestable; -typedef std::weak_ptr SpatiallyNestableWeakPointer; -typedef std::shared_ptr SpatiallyNestablePointer; +using SpatiallyNestableWeakPointer = std::weak_ptr; +using SpatiallyNestablePointer = std::shared_ptr; class SpatiallyNestable { public: - SpatiallyNestable(); + SpatiallyNestable() : _transform() { } + SpatiallyNestable(QUuid id) : _id(id), _transform() { } virtual ~SpatiallyNestable() { } + const QUuid& getID() const { return _id; } + void setID(const QUuid& id) { _id = id; } + // world frame virtual const Transform& getTransform() const; virtual void setTransform(const Transform& transform); + Transform getParentTransform() const; + virtual const glm::vec3& getPosition() const; virtual void setPosition(const glm::vec3& position); @@ -40,19 +47,35 @@ public: virtual const glm::vec3& getScale() const; virtual void setScale(const glm::vec3& scale); - // model frame - // ... + // object's parent's frame + virtual const Transform& getLocalTransform() const; + virtual void setLocalTransform(const Transform& transform); + virtual const glm::vec3& getLocalPosition() const; + virtual void setLocalPosition(const glm::vec3& position); + + virtual const glm::quat& getLocalOrientation() const; + virtual void setLocalOrientation(const glm::quat& orientation); + + virtual const glm::vec3& getLocalScale() const; + virtual void setLocalScale(const glm::vec3& scale); protected: + QUuid _id; QUuid _parentID; // what is this thing's transform relative to? int _parentJointIndex; // which joint of the parent is this relative to? - SpatiallyNestableWeakPointer _parent; + mutable SpatiallyNestableWeakPointer _parent; QVector _children; private: - Transform _transform; + Transform _transform; // this is to be combined with parent's world-transform to produce this' world-transform. + SpatiallyNestablePointer getParentPointer() const; + + // these are so we can return by reference + mutable glm::vec3 _absolutePositionCache; + mutable glm::quat _absoluteRotationCache; + mutable Transform _worldTransformCache; };