Merge pull request #7772 from AndrewMeadows/minor-optimizations

minor cleanup and optimizations around EntityItem::getDimensions()
This commit is contained in:
Brad Hefta-Gaub 2016-04-28 17:04:42 -07:00
commit e92e4e57b1
7 changed files with 56 additions and 46 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -101,15 +101,13 @@ bool LineEntityItem::setLinePoints(const QVector<glm::vec3>& 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;