implemented code review changes. 1. checking for divide by zero before glm::normalize. 2. clearing the ring buffer on reset. 3. renaming _standingHeightMode to _currentStandingHeight

This commit is contained in:
amantley 2018-07-09 11:58:02 -07:00
parent defd6f88e6
commit a3417cace9
2 changed files with 36 additions and 28 deletions

View file

@ -89,6 +89,7 @@ const float MyAvatar::ZOOM_MAX = 25.0f;
const float MyAvatar::ZOOM_DEFAULT = 1.5f; const float MyAvatar::ZOOM_DEFAULT = 1.5f;
const float MIN_SCALE_CHANGED_DELTA = 0.001f; const float MIN_SCALE_CHANGED_DELTA = 0.001f;
const int MODE_READINGS_RING_BUFFER_SIZE = 500; const int MODE_READINGS_RING_BUFFER_SIZE = 500;
const float CENTIMETERS_PER_METER = 100.0f;
//#define DEBUG_DRAW_HMD_MOVING_AVERAGE //#define DEBUG_DRAW_HMD_MOVING_AVERAGE
@ -427,7 +428,11 @@ void MyAvatar::update(float deltaTime) {
_rotationChanged = usecTimestampNow(); _rotationChanged = usecTimestampNow();
_smoothOrientationTimer += deltaTime; _smoothOrientationTimer += deltaTime;
} }
setStandingHeightMode(computeStandingHeightMode(getControllerPoseInAvatarFrame(controller::Action::HEAD)));
float newHeightReading = getControllerPoseInAvatarFrame(controller::Action::HEAD).getTranslation().y;
int newHeightReadingInCentimeters = glm::floor(newHeightReading * CENTIMETERS_PER_METER);
_recentModeReadings.insert(newHeightReadingInCentimeters);
setCurrentStandingHeight(computeStandingHeightMode(getControllerPoseInAvatarFrame(controller::Action::HEAD)));
setAverageHeadRotation(computeAverageHeadRotation(getControllerPoseInAvatarFrame(controller::Action::HEAD))); setAverageHeadRotation(computeAverageHeadRotation(getControllerPoseInAvatarFrame(controller::Action::HEAD)));
#ifdef DEBUG_DRAW_HMD_MOVING_AVERAGE #ifdef DEBUG_DRAW_HMD_MOVING_AVERAGE
@ -2181,11 +2186,7 @@ void MyAvatar::setHasAudioEnabledFaceMovement(bool hasAudioEnabledFaceMovement)
void MyAvatar::setRotationRecenterFilterLength(float length) { void MyAvatar::setRotationRecenterFilterLength(float length) {
const float MINIMUM_ROTATION_RECENTER_FILTER_LENGTH = 0.01f; const float MINIMUM_ROTATION_RECENTER_FILTER_LENGTH = 0.01f;
if (length > MINIMUM_ROTATION_RECENTER_FILTER_LENGTH) { _rotationRecenterFilterLength = std::max(MINIMUM_ROTATION_RECENTER_FILTER_LENGTH, length);
_rotationRecenterFilterLength = length;
} else {
_rotationRecenterFilterLength = 0.01f;
}
} }
void MyAvatar::setRotationThreshold(float angleRadians) { void MyAvatar::setRotationThreshold(float angleRadians) {
@ -3199,46 +3200,44 @@ static bool isWithinThresholdHeightMode(const controller::Pose& head,const float
} }
float MyAvatar::computeStandingHeightMode(const controller::Pose& head) { float MyAvatar::computeStandingHeightMode(const controller::Pose& head) {
const float CENTIMETERS_PER_METER = 100.0f;
const float MODE_CORRECTION_FACTOR = 0.02f; const float MODE_CORRECTION_FACTOR = 0.02f;
int greatestFrequency = 0; int greatestFrequency = 0;
int mode = 0; int mode = 0;
// init mode in meters to the current mode // init mode in meters to the current mode
float modeInMeters = getStandingHeightMode(); float modeInMeters = getCurrentStandingHeight();
if (head.isValid()) { if (head.isValid()) {
float newReading = head.getTranslation().y; float newReading = head.getTranslation().y;
int newReadingInCentimeters = glm::floor(newReading * CENTIMETERS_PER_METER); int newReadingInCentimeters = glm::floor(newReading * CENTIMETERS_PER_METER);
_recentModeReadings.insert(newReadingInCentimeters); _recentModeReadings.insert(newReadingInCentimeters);
RingBufferHistory<quint64>::Iterator recentModeReadingsIterator = _recentModeReadings.begin(); RingBufferHistory<int>::Iterator recentModeReadingsIterator = _recentModeReadings.begin();
RingBufferHistory<quint64>::Iterator end = _recentModeReadings.end(); RingBufferHistory<int>::Iterator end = _recentModeReadings.end();
std::map<int, int> freq; std::map<int, int> freq;
do { while(recentModeReadingsIterator != end){
++recentModeReadingsIterator;
freq[*recentModeReadingsIterator] += 1; freq[*recentModeReadingsIterator] += 1;
if ((freq[*recentModeReadingsIterator] > greatestFrequency) || if (freq[*recentModeReadingsIterator] > greatestFrequency) {
(freq[*recentModeReadingsIterator] == MODE_READINGS_RING_BUFFER_SIZE)) {
greatestFrequency = freq[*recentModeReadingsIterator]; greatestFrequency = freq[*recentModeReadingsIterator];
mode = *recentModeReadingsIterator; mode = *recentModeReadingsIterator;
} }
} while (recentModeReadingsIterator != end); recentModeReadingsIterator++;
}
modeInMeters = ((float)mode) / CENTIMETERS_PER_METER; modeInMeters = ((float)mode) / CENTIMETERS_PER_METER;
if (!(modeInMeters > getStandingHeightMode())) { if (!(modeInMeters > getCurrentStandingHeight())) {
// if not greater check for a reset // if not greater check for a reset
if (getResetMode() && qApp->isHMDMode()) { if (getResetMode() && qApp->isHMDMode()) {
setResetMode(false); setResetMode(false);
float resetModeInCentimeters = glm::floor((newReading - MODE_CORRECTION_FACTOR)*CENTIMETERS_PER_METER); float resetModeInCentimeters = glm::floor((newReading - MODE_CORRECTION_FACTOR)*CENTIMETERS_PER_METER);
modeInMeters = (resetModeInCentimeters / CENTIMETERS_PER_METER); modeInMeters = (resetModeInCentimeters / CENTIMETERS_PER_METER);
_recentModeReadings.clear();
} else { } else {
// if not greater and no reset, keep the mode as it is // if not greater and no reset, keep the mode as it is
modeInMeters = getStandingHeightMode(); modeInMeters = getCurrentStandingHeight();
} }
} }
} }
qCDebug(interfaceapp) << "the mode is " << modeInMeters << "the frequency is " << greatestFrequency;
return modeInMeters; return modeInMeters;
} }
@ -3249,13 +3248,21 @@ static bool handDirectionMatchesHeadDirection(const controller::Pose& leftHand,
glm::vec3 xzHeadVelocity(head.velocity.x, 0.0f, head.velocity.z); glm::vec3 xzHeadVelocity(head.velocity.x, 0.0f, head.velocity.z);
if (leftHand.isValid() && head.isValid()) { if (leftHand.isValid() && head.isValid()) {
glm::vec3 xzLeftHandVelocity(leftHand.velocity.x, 0.0f, leftHand.velocity.z); glm::vec3 xzLeftHandVelocity(leftHand.velocity.x, 0.0f, leftHand.velocity.z);
if ((glm::length(xzLeftHandVelocity) > VELOCITY_EPSILON) && (glm::length(xzHeadVelocity) > VELOCITY_EPSILON)) {
float handDotHeadLeft = glm::dot(glm::normalize(xzLeftHandVelocity), glm::normalize(xzHeadVelocity)); float handDotHeadLeft = glm::dot(glm::normalize(xzLeftHandVelocity), glm::normalize(xzHeadVelocity));
leftHandDirectionMatchesHead = ((handDotHeadLeft > DEFAULT_HANDS_VELOCITY_DIRECTION_STEPPING_THRESHOLD) && (glm::length(leftHand.getVelocity()) > VELOCITY_EPSILON)); leftHandDirectionMatchesHead = ((handDotHeadLeft > DEFAULT_HANDS_VELOCITY_DIRECTION_STEPPING_THRESHOLD));
} else {
leftHandDirectionMatchesHead = false;
}
} }
if (rightHand.isValid() && head.isValid()) { if (rightHand.isValid() && head.isValid()) {
glm::vec3 xzRightHandVelocity(rightHand.velocity.x, 0.0f, rightHand.velocity.z); glm::vec3 xzRightHandVelocity(rightHand.velocity.x, 0.0f, rightHand.velocity.z);
if ((glm::length(xzRightHandVelocity) > VELOCITY_EPSILON) && (glm::length(xzHeadVelocity) > VELOCITY_EPSILON)) {
float handDotHeadRight = glm::dot(glm::normalize(xzRightHandVelocity), glm::normalize(xzHeadVelocity)); float handDotHeadRight = glm::dot(glm::normalize(xzRightHandVelocity), glm::normalize(xzHeadVelocity));
rightHandDirectionMatchesHead = ((handDotHeadRight > DEFAULT_HANDS_VELOCITY_DIRECTION_STEPPING_THRESHOLD) && (glm::length(rightHand.getVelocity()) > VELOCITY_EPSILON)); rightHandDirectionMatchesHead = (handDotHeadRight > DEFAULT_HANDS_VELOCITY_DIRECTION_STEPPING_THRESHOLD);
} else {
rightHandDirectionMatchesHead = false;
}
} }
return leftHandDirectionMatchesHead && rightHandDirectionMatchesHead; return leftHandDirectionMatchesHead && rightHandDirectionMatchesHead;
} }
@ -3297,6 +3304,7 @@ static bool isHeadLevel(const controller::Pose& head, const glm::quat& averageHe
} }
return ((fabs(diffFromAverageEulers.x) < DEFAULT_HEAD_PITCH_STEPPING_TOLERANCE) && (fabs(diffFromAverageEulers.z) < DEFAULT_HEAD_ROLL_STEPPING_TOLERANCE)); return ((fabs(diffFromAverageEulers.x) < DEFAULT_HEAD_PITCH_STEPPING_TOLERANCE) && (fabs(diffFromAverageEulers.z) < DEFAULT_HEAD_ROLL_STEPPING_TOLERANCE));
} }
float MyAvatar::getUserHeight() const { float MyAvatar::getUserHeight() const {
return _userHeight.get(); return _userHeight.get();
} }
@ -3502,7 +3510,7 @@ bool MyAvatar::FollowHelper::shouldActivateHorizontalCG(MyAvatar& myAvatar) cons
bool stepDetected = false; bool stepDetected = false;
if (!withinBaseOfSupport(currentHeadPose) && if (!withinBaseOfSupport(currentHeadPose) &&
headAngularVelocityBelowThreshold(currentHeadPose) && headAngularVelocityBelowThreshold(currentHeadPose) &&
isWithinThresholdHeightMode(currentHeadPose, myAvatar.getStandingHeightMode()) && isWithinThresholdHeightMode(currentHeadPose, myAvatar.getCurrentStandingHeight()) &&
handDirectionMatchesHeadDirection(currentLeftHandPose, currentRightHandPose, currentHeadPose) && handDirectionMatchesHeadDirection(currentLeftHandPose, currentRightHandPose, currentHeadPose) &&
handAngularVelocityBelowThreshold(currentLeftHandPose, currentRightHandPose) && handAngularVelocityBelowThreshold(currentLeftHandPose, currentRightHandPose) &&
headVelocityGreaterThanThreshold(currentHeadPose) && headVelocityGreaterThanThreshold(currentHeadPose) &&

View file

@ -883,8 +883,8 @@ public:
virtual void rebuildCollisionShape() override; virtual void rebuildCollisionShape() override;
const glm::vec2& getHeadControllerFacingMovingAverage() const { return _headControllerFacingMovingAverage; } const glm::vec2& getHeadControllerFacingMovingAverage() const { return _headControllerFacingMovingAverage; }
const float getStandingHeightMode() const { return _standingHeightMode; } const float getCurrentStandingHeight() const { return _currentStandingHeight; }
void setStandingHeightMode(float newMode) { _standingHeightMode = newMode; } void setCurrentStandingHeight(float newMode) { _currentStandingHeight = newMode; }
const glm::quat getAverageHeadRotation() const { return _averageHeadRotation; } const glm::quat getAverageHeadRotation() const { return _averageHeadRotation; }
void setAverageHeadRotation(glm::quat rotation) { _averageHeadRotation = rotation; } void setAverageHeadRotation(glm::quat rotation) { _averageHeadRotation = rotation; }
bool getResetMode() const { return _resetMode; } bool getResetMode() const { return _resetMode; }
@ -1526,9 +1526,9 @@ private:
glm::vec2 _headControllerFacingMovingAverage { 0.0f, 0.0f }; // facing vector in xz plane (sensor space) glm::vec2 _headControllerFacingMovingAverage { 0.0f, 0.0f }; // facing vector in xz plane (sensor space)
glm::quat _averageHeadRotation { 0.0f, 0.0f, 0.0f, 1.0f }; glm::quat _averageHeadRotation { 0.0f, 0.0f, 0.0f, 1.0f };
float _standingHeightMode { 0.0f }; float _currentStandingHeight { 0.0f };
bool _resetMode { true }; bool _resetMode { true };
RingBufferHistory<quint64> _recentModeReadings; RingBufferHistory<int> _recentModeReadings;
// cache of the current body position and orientation of the avatar's body, // cache of the current body position and orientation of the avatar's body,
// in sensor space. // in sensor space.