From c686418be0f434139c09e950dab74a2f156e9d12 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 27 Apr 2016 10:34:29 -0700 Subject: [PATCH] optimize use of EntityItem::getDimensions() --- libraries/entities/src/EntityItem.cpp | 73 +++++++++++++++------------ libraries/entities/src/EntityItem.h | 2 +- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 938fbe12ac..2d1bbf2f88 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -781,7 +781,8 @@ void EntityItem::adjustEditPacketForClockSkew(QByteArray& buffer, qint64 clockSk } float EntityItem::computeMass() const { - return _density * _volumeMultiplier * getDimensions().x * getDimensions().y * getDimensions().z; + glm::vec3 dimensions = getDimensions(); + return _density * _volumeMultiplier * dimensions.x * dimensions.y * dimensions.z; } void EntityItem::setDensity(float density) { @@ -801,7 +802,8 @@ void EntityItem::setMass(float mass) { // we must protect the density range to help maintain stability of physics simulation // therefore this method might not accept the mass that is supplied. - float volume = _volumeMultiplier * getDimensions().x * getDimensions().y * getDimensions().z; + glm::vec3 dimensions = getDimensions(); + float volume = _volumeMultiplier * dimensions.x * dimensions.y * dimensions.z; // compute new density const float MIN_VOLUME = 1.0e-6f; // 0.001mm^3 @@ -1222,11 +1224,13 @@ AACube EntityItem::getMaximumAACube(bool& success) const { // * we know that the position is the center of rotation glm::vec3 centerOfRotation = getPosition(success); // also where _registration point is if (success) { + _recalcMaxAACube = false; // * we know that the registration point is the center of rotation // * we can calculate the length of the furthest extent from the registration point // as the dimensions * max (registrationPoint, (1.0,1.0,1.0) - registrationPoint) - glm::vec3 registrationPoint = (getDimensions() * getRegistrationPoint()); - glm::vec3 registrationRemainder = (getDimensions() * (glm::vec3(1.0f, 1.0f, 1.0f) - getRegistrationPoint())); + glm::vec3 dimensions = getDimensions(); + glm::vec3 registrationPoint = (dimensions * _registrationPoint); + glm::vec3 registrationRemainder = (dimensions * (glm::vec3(1.0f, 1.0f, 1.0f) - _registrationPoint)); glm::vec3 furthestExtentFromRegistration = glm::max(registrationPoint, registrationRemainder); // * we know that if you rotate in any direction you would create a sphere @@ -1238,7 +1242,6 @@ AACube EntityItem::getMaximumAACube(bool& success) const { glm::vec3 minimumCorner = centerOfRotation - glm::vec3(radius, radius, radius); _maxAACube = AACube(minimumCorner, radius * 2.0f); - _recalcMaxAACube = false; } } else { success = true; @@ -1251,28 +1254,27 @@ AACube EntityItem::getMaximumAACube(bool& success) const { /// AACube EntityItem::getMinimumAACube(bool& success) const { if (_recalcMinAACube) { - // _position represents the position of the registration point. - glm::vec3 registrationRemainder = glm::vec3(1.0f, 1.0f, 1.0f) - _registrationPoint; - - glm::vec3 unrotatedMinRelativeToEntity = - (getDimensions() * getRegistrationPoint()); - glm::vec3 unrotatedMaxRelativeToEntity = getDimensions() * registrationRemainder; - Extents unrotatedExtentsRelativeToRegistrationPoint = { unrotatedMinRelativeToEntity, unrotatedMaxRelativeToEntity }; - Extents rotatedExtentsRelativeToRegistrationPoint = - unrotatedExtentsRelativeToRegistrationPoint.getRotated(getRotation()); - - // shift the extents to be relative to the position/registration point - rotatedExtentsRelativeToRegistrationPoint.shiftBy(getPosition(success)); - + // position represents the position of the registration point. + glm::vec3 position = getPosition(success); if (success) { + _recalcMinAACube = false; + glm::vec3 dimensions = getDimensions(); + glm::vec3 unrotatedMinRelativeToEntity = - (dimensions * _registrationPoint); + glm::vec3 unrotatedMaxRelativeToEntity = dimensions * (glm::vec3(1.0f, 1.0f, 1.0f) - _registrationPoint); + Extents extents = { unrotatedMinRelativeToEntity, unrotatedMaxRelativeToEntity }; + extents.rotate(getRotation()); + + // shift the extents to be relative to the position/registration point + extents.shiftBy(position); + // the cube that best encompasses extents is... - AABox box(rotatedExtentsRelativeToRegistrationPoint); + AABox box(extents); glm::vec3 centerOfBox = box.calcCenter(); float longestSide = box.getLargestDimension(); float halfLongestSide = longestSide / 2.0f; glm::vec3 cornerOfCube = centerOfBox - glm::vec3(halfLongestSide, halfLongestSide, halfLongestSide); _minAACube = AACube(cornerOfCube, longestSide); - _recalcMinAACube = false; } } else { success = true; @@ -1282,21 +1284,20 @@ AACube EntityItem::getMinimumAACube(bool& success) const { AABox EntityItem::getAABox(bool& success) const { if (_recalcAABox) { - // _position represents the position of the registration point. - glm::vec3 registrationRemainder = glm::vec3(1.0f, 1.0f, 1.0f) - _registrationPoint; - - glm::vec3 unrotatedMinRelativeToEntity = - (getDimensions() * _registrationPoint); - glm::vec3 unrotatedMaxRelativeToEntity = getDimensions() * registrationRemainder; - Extents unrotatedExtentsRelativeToRegistrationPoint = { unrotatedMinRelativeToEntity, unrotatedMaxRelativeToEntity }; - Extents rotatedExtentsRelativeToRegistrationPoint = - unrotatedExtentsRelativeToRegistrationPoint.getRotated(getRotation()); - - // shift the extents to be relative to the position/registration point - rotatedExtentsRelativeToRegistrationPoint.shiftBy(getPosition(success)); - + // position represents the position of the registration point. + glm::vec3 position = getPosition(success); if (success) { - _cachedAABox = AABox(rotatedExtentsRelativeToRegistrationPoint); _recalcAABox = false; + glm::vec3 dimensions = getDimensions(); + glm::vec3 unrotatedMinRelativeToEntity = - (dimensions * _registrationPoint); + glm::vec3 unrotatedMaxRelativeToEntity = dimensions * (glm::vec3(1.0f, 1.0f, 1.0f) - _registrationPoint); + Extents extents = { unrotatedMinRelativeToEntity, unrotatedMaxRelativeToEntity }; + extents.rotate(getRotation()); + + // shift the extents to be relative to the position/registration point + extents.shiftBy(position); + + _cachedAABox = AABox(extents); } } else { success = true; @@ -1373,6 +1374,11 @@ void EntityItem::computeShapeInfo(ShapeInfo& info) { adjustShapeInfoByRegistration(info); } +float EntityItem::getVolumeEstimate() const { + glm::vec3 dimensions = getDimensions(); + return dimensions.x * dimensions.y * dimensions.z; +} + void EntityItem::updateRegistrationPoint(const glm::vec3& value) { if (value != _registrationPoint) { setRegistrationPoint(value); @@ -1433,7 +1439,8 @@ void EntityItem::updateMass(float mass) { // we must protect the density range to help maintain stability of physics simulation // therefore this method might not accept the mass that is supplied. - float volume = _volumeMultiplier * getDimensions().x * getDimensions().y * getDimensions().z; + glm::vec3 dimensions = getDimensions(); + float volume = _volumeMultiplier * dimensions.x * dimensions.y * dimensions.z; // compute new density float newDensity = _density; diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 5e34d942f6..b3689b9b56 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -314,7 +314,7 @@ public: virtual bool isReadyToComputeShape() { return !isDead(); } virtual void computeShapeInfo(ShapeInfo& info); - virtual float getVolumeEstimate() const { return getDimensions().x * getDimensions().y * getDimensions().z; } + virtual float getVolumeEstimate() const; /// return preferred shape type (actual physical shape may differ) virtual ShapeType getShapeType() const { return SHAPE_TYPE_NONE; }