From c134b8de5b46b9a20156037c06d1de91bb41349a Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 18 Feb 2014 17:08:12 -0800 Subject: [PATCH] Remove Avatar::_hand, use AvatarData::_handData --- interface/src/avatar/Avatar.cpp | 10 ++++------ interface/src/avatar/Avatar.h | 4 ++-- interface/src/avatar/Hand.cpp | 4 ++-- interface/src/avatar/MyAvatar.cpp | 12 ++++++------ interface/src/avatar/SkeletonModel.cpp | 13 ++++++------ interface/src/devices/SixenseManager.cpp | 18 ++++++++--------- libraries/avatars/src/AvatarData.cpp | 10 +++------- libraries/avatars/src/HandData.cpp | 25 ++++++++++++++++++------ libraries/avatars/src/HandData.h | 4 ++++ 9 files changed, 55 insertions(+), 45 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 4edead7b32..26cb17c7b1 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -60,7 +60,6 @@ const float CHAT_MESSAGE_HEIGHT = 0.1f; Avatar::Avatar() : AvatarData(), _head(this), - _hand(this), _skeletonModel(this), _bodyYawDelta(0.0f), _mode(AVATAR_MODE_STANDING), @@ -82,17 +81,16 @@ Avatar::Avatar() : // give the pointer to our head to inherited _headData variable from AvatarData _headData = &_head; - _handData = &_hand; + _handData = static_cast(new Hand(this)); } Avatar::~Avatar() { _headData = NULL; - _handData = NULL; } void Avatar::init() { _head.init(); - _hand.init(); + getHand()->init(); _skeletonModel.init(); _initialized = true; } @@ -115,7 +113,7 @@ void Avatar::simulate(float deltaTime) { // copy velocity so we can use it later for acceleration glm::vec3 oldVelocity = getVelocity(); - _hand.simulate(deltaTime, false); + getHand()->simulate(deltaTime, false); _skeletonModel.simulate(deltaTime); _head.setBodyRotation(glm::vec3(_bodyPitch, _bodyYaw, _bodyRoll)); glm::vec3 headPosition; @@ -254,7 +252,7 @@ void Avatar::renderBody(bool forceRenderHead) { if (forceRenderHead) { _head.render(1.0f); } - _hand.render(false); + getHand()->render(false); } bool Avatar::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const { diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index cc1168ca88..1df4ae1d74 100755 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -84,7 +84,7 @@ public: float getScale() const { return _scale; } const glm::vec3& getVelocity() const { return _velocity; } Head& getHead() { return _head; } - Hand& getHand() { return _hand; } + Hand* getHand() { return static_cast(_handData); } glm::quat getWorldAlignedOrientation() const; Node* getOwningAvatarMixer() { return _owningAvatarMixer.data(); } @@ -130,7 +130,7 @@ public slots: protected: Head _head; - Hand _hand; + //Hand _hand; SkeletonModel _skeletonModel; float _bodyYawDelta; AvatarMode _mode; diff --git a/interface/src/avatar/Hand.cpp b/interface/src/avatar/Hand.cpp index 999551eb48..efbdfa2438 100644 --- a/interface/src/avatar/Hand.cpp +++ b/interface/src/avatar/Hand.cpp @@ -133,8 +133,8 @@ void Hand::playSlaps(PalmData& palm, Avatar* avatar) bool wasColliding = palm.getIsCollidingWithPalm(); palm.setIsCollidingWithPalm(false); // If 'Play Slaps' is enabled, look for palm-to-palm collisions and make sound - for (size_t j = 0; j < avatar->getHand().getNumPalms(); j++) { - PalmData& otherPalm = avatar->getHand().getPalms()[j]; + for (size_t j = 0; j < avatar->getHand()->getNumPalms(); j++) { + PalmData& otherPalm = avatar->getHand()->getPalms()[j]; if (!otherPalm.isActive()) { continue; } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 41ad4b22b1..0e8438610b 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -73,7 +73,7 @@ void MyAvatar::reset() { //_headMouseX = _glWidget->width() / 2; //_headMouseY = _glWidget->height() / 2; _head.reset(); - _hand.reset(); + getHand()->reset(); setVelocity(glm::vec3(0,0,0)); setThrust(glm::vec3(0,0,0)); @@ -314,8 +314,8 @@ void MyAvatar::simulate(float deltaTime) { _position += _velocity * deltaTime; // update avatar skeleton and simulate hand and head - _hand.collideAgainstOurself(); - _hand.simulate(deltaTime, true); + getHand()->collideAgainstOurself(); + getHand()->simulate(deltaTime, true); _skeletonModel.simulate(deltaTime); _head.setBodyRotation(glm::vec3(_bodyPitch, _bodyYaw, _bodyRoll)); glm::vec3 headPosition; @@ -701,7 +701,7 @@ void MyAvatar::renderBody(bool forceRenderHead) { if (forceRenderHead || (glm::length(myCamera->getPosition() - _head.calculateAverageEyePosition()) > RENDER_HEAD_CUTOFF_DISTANCE)) { _head.render(1.0f); } - _hand.render(true); + getHand()->render(true); } void MyAvatar::updateThrust(float deltaTime) { @@ -994,10 +994,10 @@ void MyAvatar::updateCollisionWithAvatars(float deltaTime) { } // collide our hands against them - _hand.collideAgainstAvatar(avatar, true); + getHand()->collideAgainstAvatar(avatar, true); // collide their hands against us - avatar->getHand().collideAgainstAvatar(this, false); + avatar->getHand()->collideAgainstAvatar(this, false); } } } diff --git a/interface/src/avatar/SkeletonModel.cpp b/interface/src/avatar/SkeletonModel.cpp index 9bf2e0f727..474cefdab5 100644 --- a/interface/src/avatar/SkeletonModel.cpp +++ b/interface/src/avatar/SkeletonModel.cpp @@ -8,10 +8,9 @@ #include -#include - #include "Application.h" #include "Avatar.h" +#include "Hand.h" #include "Menu.h" #include "SkeletonModel.h" @@ -33,8 +32,8 @@ void SkeletonModel::simulate(float deltaTime) { // find the left and rightmost active Leap palms int leftPalmIndex, rightPalmIndex; - HandData& hand = _owningAvatar->getHand(); - hand.getLeftRightPalmIndices(leftPalmIndex, rightPalmIndex); + Hand* hand = _owningAvatar->getHand(); + hand->getLeftRightPalmIndices(leftPalmIndex, rightPalmIndex); const float HAND_RESTORATION_PERIOD = 1.f; // seconds float handRestorePercent = glm::clamp(deltaTime / HAND_RESTORATION_PERIOD, 0.f, 1.f); @@ -52,14 +51,14 @@ void SkeletonModel::simulate(float deltaTime) { } else if (leftPalmIndex == rightPalmIndex) { // right hand only applyPalmData(geometry.rightHandJointIndex, geometry.rightFingerJointIndices, geometry.rightFingertipJointIndices, - hand.getPalms()[leftPalmIndex]); + hand->getPalms()[leftPalmIndex]); restoreLeftHandPosition(handRestorePercent); } else { applyPalmData(geometry.leftHandJointIndex, geometry.leftFingerJointIndices, geometry.leftFingertipJointIndices, - hand.getPalms()[leftPalmIndex]); + hand->getPalms()[leftPalmIndex]); applyPalmData(geometry.rightHandJointIndex, geometry.rightFingerJointIndices, geometry.rightFingertipJointIndices, - hand.getPalms()[rightPalmIndex]); + hand->getPalms()[rightPalmIndex]); } } diff --git a/interface/src/devices/SixenseManager.cpp b/interface/src/devices/SixenseManager.cpp index 79feb5eb3f..9ff34e698e 100644 --- a/interface/src/devices/SixenseManager.cpp +++ b/interface/src/devices/SixenseManager.cpp @@ -45,7 +45,7 @@ void SixenseManager::update(float deltaTime) { return; } MyAvatar* avatar = Application::getInstance()->getAvatar(); - Hand& hand = avatar->getHand(); + Hand* hand = avatar->getHand(); int maxControllers = sixenseGetMaxControllers(); for (int i = 0; i < maxControllers; i++) { @@ -60,16 +60,16 @@ void SixenseManager::update(float deltaTime) { // Either find a palm matching the sixense controller, or make a new one PalmData* palm; bool foundHand = false; - for (int j = 0; j < hand.getNumPalms(); j++) { - if (hand.getPalms()[j].getSixenseID() == data.controller_index) { - palm = &hand.getPalms()[j]; + for (int j = 0; j < hand->getNumPalms(); j++) { + if (hand->getPalms()[j].getSixenseID() == data.controller_index) { + palm = &(hand->getPalms()[j]); foundHand = true; } } if (!foundHand) { - PalmData newPalm(&hand); - hand.getPalms().push_back(newPalm); - palm = &hand.getPalms()[hand.getNumPalms() - 1]; + PalmData newPalm(hand); + hand->getPalms().push_back(newPalm); + palm = &(hand->getPalms()[hand->getNumPalms() - 1]); palm->setSixenseID(data.controller_index); printf("Found new Sixense controller, ID %i\n", data.controller_index); } @@ -107,7 +107,7 @@ void SixenseManager::update(float deltaTime) { } // initialize the "finger" based on the direction - FingerData finger(palm, &hand); + FingerData finger(palm, hand); finger.setActive(true); finger.setRawRootPosition(position); const float FINGER_LENGTH = 300.0f; // Millimeters @@ -130,7 +130,7 @@ void SixenseManager::update(float deltaTime) { // if the controllers haven't been moved in a while, disable const int MOVEMENT_DISABLE_DURATION = 30 * 1000 * 1000; if (usecTimestampNow() - _lastMovement > MOVEMENT_DISABLE_DURATION) { - for (vector::iterator it = hand.getPalms().begin(); it != hand.getPalms().end(); it++) { + for (vector::iterator it = hand->getPalms().begin(); it != hand->getPalms().end(); it++) { it->setActive(false); } } diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index c375f8b82d..3c89f45d23 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -63,10 +63,6 @@ QByteArray AvatarData::toByteArray() { if (!_headData) { _headData = new HeadData(this); } - // lazily allocate memory for HandData in case we're not an Avatar instance - if (!_handData) { - _handData = new HandData(this); - } QByteArray avatarDataByteArray; avatarDataByteArray.resize(MAX_PACKET_SIZE); @@ -152,8 +148,8 @@ QByteArray AvatarData::toByteArray() { // pupil dilation destinationBuffer += packFloatToByte(destinationBuffer, _headData->_pupilDilation, 1.0f); - // leap hand data - destinationBuffer += _handData->encodeRemoteData(destinationBuffer); + // hand data + destinationBuffer += HandData::encodeData(_handData, destinationBuffer); return avatarDataByteArray.left(destinationBuffer - startPosition); } @@ -259,7 +255,7 @@ int AvatarData::parseData(const QByteArray& packet) { // pupil dilation sourceBuffer += unpackFloatFromByte(sourceBuffer, _headData->_pupilDilation, 1.0f); - // leap hand data + // hand data if (sourceBuffer - startPosition < packet.size()) { // check passed, bytes match sourceBuffer += _handData->decodeRemoteData(packet.mid(sourceBuffer - startPosition)); diff --git a/libraries/avatars/src/HandData.cpp b/libraries/avatars/src/HandData.cpp index 5a923eea93..e4bb187f28 100644 --- a/libraries/avatars/src/HandData.cpp +++ b/libraries/avatars/src/HandData.cpp @@ -113,17 +113,30 @@ _owningHandData(owningHandData) setTrailLength(standardTrailLength); } +// static +int HandData::encodeData(HandData* hand, unsigned char* destinationBuffer) { + if (hand) { + return hand->encodeRemoteData(destinationBuffer); + } + // else encode empty data: + // One byte for zero hands + // One byte for error checking. + *destinationBuffer = 0; + *(destinationBuffer + 1) = 1; + return 2; +} + int HandData::encodeRemoteData(unsigned char* destinationBuffer) { const unsigned char* startPosition = destinationBuffer; - unsigned int numHands = 0; + unsigned int numPalms = 0; for (unsigned int handIndex = 0; handIndex < getNumPalms(); ++handIndex) { PalmData& palm = getPalms()[handIndex]; if (palm.isActive()) { - numHands++; + numPalms++; } } - *destinationBuffer++ = numHands; + *destinationBuffer++ = numPalms; for (unsigned int handIndex = 0; handIndex < getNumPalms(); ++handIndex) { PalmData& palm = getPalms()[handIndex]; @@ -162,9 +175,9 @@ int HandData::encodeRemoteData(unsigned char* destinationBuffer) { int HandData::decodeRemoteData(const QByteArray& dataByteArray) { const unsigned char* startPosition; const unsigned char* sourceBuffer = startPosition = reinterpret_cast(dataByteArray.data()); - unsigned int numHands = *sourceBuffer++; + unsigned int numPalms = *sourceBuffer++; - for (unsigned int handIndex = 0; handIndex < numHands; ++handIndex) { + for (unsigned int handIndex = 0; handIndex < numPalms; ++handIndex) { if (handIndex >= getNumPalms()) addNewPalm(); PalmData& palm = getPalms()[handIndex]; @@ -203,7 +216,7 @@ int HandData::decodeRemoteData(const QByteArray& dataByteArray) { } } // Turn off any hands which weren't used. - for (unsigned int handIndex = numHands; handIndex < getNumPalms(); ++handIndex) { + for (unsigned int handIndex = numPalms; handIndex < getNumPalms(); ++handIndex) { PalmData& palm = getPalms()[handIndex]; palm.setActive(false); } diff --git a/libraries/avatars/src/HandData.h b/libraries/avatars/src/HandData.h index 550c62e829..4046a0a875 100755 --- a/libraries/avatars/src/HandData.h +++ b/libraries/avatars/src/HandData.h @@ -71,6 +71,10 @@ public: void setFingerTrailLength(unsigned int length); void updateFingerTrails(); + // use these static methods for safety + static int encodeData(HandData* hand, unsigned char* destinationBuffer); + static int decodeData(HandData* hand, const QByteArray& dataByteArray); + // Use these for sending and receiving hand data int encodeRemoteData(unsigned char* destinationBuffer); int decodeRemoteData(const QByteArray& dataByteArray);