From c686418be0f434139c09e950dab74a2f156e9d12 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 27 Apr 2016 10:34:29 -0700 Subject: [PATCH 1/2] 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; } From b5ad98981128a586e2adedc05acc184e7da9e358 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 27 Apr 2016 11:04:22 -0700 Subject: [PATCH 2/2] more optimized uses of EntityItem::getDimensions() --- .../entities-renderer/src/RenderableModelEntityItem.cpp | 5 +++-- .../entities-renderer/src/RenderablePolyVoxEntityItem.cpp | 8 +++++--- .../entities-renderer/src/RenderableWebEntityItem.cpp | 5 +++-- libraries/entities/src/LightEntityItem.cpp | 5 +++-- libraries/entities/src/LineEntityItem.cpp | 4 +--- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index f3a8d3110c..c4ac9b09e5 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -339,15 +339,16 @@ void RenderableModelEntityItem::updateModelBounds() { return; } bool movingOrAnimating = isMovingRelativeToParent() || isAnimatingSomething(); + glm::vec3 dimensions = getDimensions(); if ((movingOrAnimating || _needsInitialSimulation || _needsJointSimulation || _model->getTranslation() != getPosition() || - _model->getScaleToFitDimensions() != getDimensions() || + _model->getScaleToFitDimensions() != dimensions || _model->getRotation() != getRotation() || _model->getRegistrationPoint() != getRegistrationPoint()) && _model->isActive() && _dimensionsInitialized) { - _model->setScaleToFit(true, getDimensions()); + _model->setScaleToFit(true, dimensions); _model->setSnapModelToRegistrationPoint(true, getRegistrationPoint()); _model->setRotation(getRotation()); _model->setTranslation(getPosition()); diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index c9c4c8503a..6c4e3994c6 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -206,13 +206,14 @@ glm::mat4 RenderablePolyVoxEntityItem::voxelToLocalMatrix() const { voxelVolumeSize = _voxelVolumeSize; }); - glm::vec3 scale = getDimensions() / voxelVolumeSize; // meters / voxel-units + glm::vec3 dimensions = getDimensions(); + glm::vec3 scale = dimensions / voxelVolumeSize; // meters / voxel-units bool success; // TODO -- Does this actually have to happen in world space? glm::vec3 center = getCenterPosition(success); // this handles registrationPoint changes glm::vec3 position = getPosition(success); glm::vec3 positionToCenter = center - position; - positionToCenter -= getDimensions() * Vectors::HALF - getSurfacePositionAdjustment(); + positionToCenter -= dimensions * Vectors::HALF - getSurfacePositionAdjustment(); glm::mat4 centerToCorner = glm::translate(glm::mat4(), positionToCenter); glm::mat4 scaled = glm::scale(centerToCorner, scale); return scaled; @@ -445,7 +446,8 @@ bool RenderablePolyVoxEntityItem::findDetailedRayIntersection(const glm::vec3& o // the PolyVox ray intersection code requires a near and far point. // set ray cast length to long enough to cover all of the voxel space float distanceToEntity = glm::distance(origin, getPosition()); - float largestDimension = glm::max(getDimensions().x, getDimensions().y, getDimensions().z) * 2.0f; + glm::vec3 dimensions = getDimensions(); + float largestDimension = glm::max(dimensions.x, dimensions.y, dimensions.z) * 2.0f; glm::vec3 farPoint = origin + normDirection * (distanceToEntity + largestDimension); glm::vec4 originInVoxel = wtvMatrix * glm::vec4(origin, 1.0f); diff --git a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp index 855fd16408..26aecf6050 100644 --- a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp @@ -119,12 +119,13 @@ bool RenderableWebEntityItem::buildWebSurface(EntityTreeRenderer* renderer) { // Map the intersection point to an actual offscreen pixel glm::vec3 point = intersection.intersection; + glm::vec3 dimensions = getDimensions(); point -= getPosition(); point = glm::inverse(getRotation()) * point; - point /= getDimensions(); + point /= dimensions; point += 0.5f; point.y = 1.0f - point.y; - point *= getDimensions() * METERS_TO_INCHES * DPI; + point *= dimensions * (METERS_TO_INCHES * DPI); if (event->button() == Qt::MouseButton::LeftButton) { if (event->type() == QEvent::MouseButtonPress) { diff --git a/libraries/entities/src/LightEntityItem.cpp b/libraries/entities/src/LightEntityItem.cpp index 852b37a751..1be133463c 100644 --- a/libraries/entities/src/LightEntityItem.cpp +++ b/libraries/entities/src/LightEntityItem.cpp @@ -76,12 +76,13 @@ void LightEntityItem::setIsSpotlight(bool value) { if (value != _isSpotlight) { _isSpotlight = value; + glm::vec3 dimensions = getDimensions(); if (_isSpotlight) { - const float length = getDimensions().z; + const float length = dimensions.z; const float width = length * glm::sin(glm::radians(_cutoff)); setDimensions(glm::vec3(width, width, length)); } else { - float maxDimension = glm::max(getDimensions().x, getDimensions().y, getDimensions().z); + float maxDimension = glm::max(dimensions.x, dimensions.y, dimensions.z); setDimensions(glm::vec3(maxDimension, maxDimension, maxDimension)); } } diff --git a/libraries/entities/src/LineEntityItem.cpp b/libraries/entities/src/LineEntityItem.cpp index d48780845f..78b6107d88 100644 --- a/libraries/entities/src/LineEntityItem.cpp +++ b/libraries/entities/src/LineEntityItem.cpp @@ -101,15 +101,13 @@ bool LineEntityItem::setLinePoints(const QVector& points) { if (points.size() > MAX_POINTS_PER_LINE) { return false; } + glm::vec3 halfBox = getDimensions() * 0.5f; for (int i = 0; i < points.size(); i++) { glm::vec3 point = points.at(i); - // glm::vec3 pos = getPosition(); - glm::vec3 halfBox = getDimensions() * 0.5f; if ( (point.x < - halfBox.x || point.x > halfBox.x) || (point.y < -halfBox.y || point.y > halfBox.y) || (point.z < - halfBox.z || point.z > halfBox.z) ) { qDebug() << "Point is outside entity's bounding box"; return false; } - } _points = points; _pointsChanged = true;