hook up entities and avatars to SpatialParentFinder

This commit is contained in:
Seth Alves 2015-10-22 11:05:48 -07:00
parent 3944d7be0f
commit 264b658132
11 changed files with 296 additions and 20 deletions

View file

@ -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;
}

View file

@ -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 <memory>
#include <QUuid>
#include <EntityTree.h>
#include <SpatialParentFinder.h>
// 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

View file

@ -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<SpatialParentFinder, AssignmentParentFinder>();
DependencyManager::set<AssignmentParentFinder>(tree);
return tree;
}

View file

@ -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<LimitedNodeList, NodeList>();
DependencyManager::registerInheritance<AvatarHashMap, AvatarManager>();
DependencyManager::registerInheritance<EntityActionFactoryInterface, InterfaceActionFactory>();
DependencyManager::registerInheritance<SpatialParentFinder, InterfaceParentFinder>();
Setting::init();
@ -327,6 +330,7 @@ bool setupEssentials(int& argc, char** argv) {
DependencyManager::set<InterfaceActionFactory>();
DependencyManager::set<AssetClient>();
DependencyManager::set<UserInputMapper>();
DependencyManager::set<InterfaceParentFinder>();
return true;
}

View file

@ -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 <Application.h>
#include <EntityTree.h>
#include <EntityTreeRenderer.h>
#include <avatar/AvatarManager.h>
#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> avatarManager = DependencyManager::get<AvatarManager>();
return avatarManager->getAvatarBySessionID(parentID);
}

View file

@ -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 <memory>
#include <QUuid>
#include <SpatialParentFinder.h>
class InterfaceParentFinder : public SpatialParentFinder {
public:
InterfaceParentFinder() { }
virtual ~InterfaceParentFinder() { }
virtual SpatiallyNestableWeakPointer find(QUuid parentID) const;
};
#endif // hifi_InterfaceParentFinder_h

View file

@ -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();
}

View file

@ -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

View file

@ -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 <QUuid>
#include "DependencyManager.h"
class SpatiallyNestable;
using SpatiallyNestableWeakPointer = std::weak_ptr<SpatiallyNestable>;
using SpatiallyNestablePointer = std::shared_ptr<SpatiallyNestable>;
class SpatialParentFinder;
using SpatialParentFinderPointer = std::shared_ptr<SpatialParentFinder>;
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

View file

@ -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<SpatialParentFinder> parentFinder = DependencyManager::get<SpatialParentFinder>();
_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);
}

View file

@ -15,22 +15,29 @@
#include <QUuid>
#include "Transform.h"
#include "SpatialParentFinder.h"
class SpatiallyNestable;
typedef std::weak_ptr<SpatiallyNestable> SpatiallyNestableWeakPointer;
typedef std::shared_ptr<SpatiallyNestable> SpatiallyNestablePointer;
using SpatiallyNestableWeakPointer = std::weak_ptr<SpatiallyNestable>;
using SpatiallyNestablePointer = std::shared_ptr<SpatiallyNestable>;
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<SpatiallyNestableWeakPointer> _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;
};