added EntityTree::findEntitiesInMeters()

progress toward conversion to meters
This commit is contained in:
Andrew Meadows 2015-02-26 15:16:59 -08:00
parent 93c77d557a
commit 7210c7a88b
8 changed files with 46 additions and 29 deletions

View file

@ -96,7 +96,7 @@ void EntityTreeRenderer::init() {
// make sure our "last avatar position" is something other than our current position, so that on our
// first chance, we'll check for enter/leave entity events.
_lastAvatarPosition = _viewState->getAvatarPosition() + glm::vec3(1.0f, 1.0f, 1.0f);
_lastAvatarPosition = _viewState->getAvatarPosition() + glm::vec3((float)TREE_SCALE);
connect(entityTree, &EntityTree::deletingEntity, this, &EntityTreeRenderer::deletingEntity);
connect(entityTree, &EntityTree::addingEntity, this, &EntityTreeRenderer::checkAndCallPreload);
@ -276,19 +276,19 @@ void EntityTreeRenderer::update() {
void EntityTreeRenderer::checkEnterLeaveEntities() {
if (_tree && !_shuttingDown) {
_tree->lockForWrite(); // so that our scripts can do edits if they want
glm::vec3 avatarPosition = _viewState->getAvatarPosition() / (float) TREE_SCALE;
glm::vec3 avatarPosition = _viewState->getAvatarPosition();
if (avatarPosition != _lastAvatarPosition) {
float radius = 1.0f / (float) TREE_SCALE; // for now, assume 1 meter radius
float radius = 1.0f; // for now, assume 1 meter radius
QVector<const EntityItem*> foundEntities;
QVector<EntityItemID> entitiesContainingAvatar;
// find the entities near us
static_cast<EntityTree*>(_tree)->findEntities(avatarPosition, radius, foundEntities);
static_cast<EntityTree*>(_tree)->findEntitiesInMeters(avatarPosition, radius, foundEntities);
// create a list of entities that actually contain the avatar's position
foreach(const EntityItem* entity, foundEntities) {
if (entity->contains(avatarPosition)) {
if (entity->containsInMeters(avatarPosition)) {
entitiesContainingAvatar << entity->getEntityItemID();
}
}
@ -341,7 +341,7 @@ void EntityTreeRenderer::leaveAllEntities() {
// make sure our "last avatar position" is something other than our current position, so that on our
// first chance, we'll check for enter/leave entity events.
_lastAvatarPosition = _viewState->getAvatarPosition() + glm::vec3(1.0f, 1.0f, 1.0f);
_lastAvatarPosition = _viewState->getAvatarPosition() + glm::vec3((float)TREE_SCALE);
_tree->unlock();
}
}

View file

@ -951,6 +951,12 @@ AACube EntityItem::getMinimumAACube() const {
return AACube(cornerOfCube, longestSide);
}
AABox EntityItem::getAABoxInMeters() const {
AABox box = getAABox();
box *= (float)TREE_SCALE;
return box;
}
AABox EntityItem::getAABox() const {
// _position represents the position of the registration point.

View file

@ -215,6 +215,7 @@ public:
float getSize() const; /// get maximum dimension in domain scale units (0.0 - 1.0)
AACube getMaximumAACube() const;
AACube getMinimumAACube() const;
AABox getAABoxInMeters() const; /// axis aligned bounding box in world-frame (meters)
AABox getAABox() const; /// axis aligned bounding box in domain scale units (0.0 - 1.0)
const QString& getScript() const { return _script; }
@ -253,6 +254,7 @@ public:
// TODO: We need to get rid of these users of getRadius()...
float getRadius() const;
virtual bool containsInMeters(const glm::vec3& point) const { return getAABoxInMeters().contains(point); }
virtual bool contains(const glm::vec3& point) const { return getAABox().contains(point); }
virtual void computeShapeInfo(ShapeInfo& info) const;

View file

@ -204,7 +204,7 @@ QVector<EntityItemID> EntityScriptingInterface::findEntities(const glm::vec3& ce
if (_entityTree) {
_entityTree->lockForRead();
QVector<const EntityItem*> entities;
_entityTree->findEntities(center/(float)TREE_SCALE, radius/(float)TREE_SCALE, entities);
_entityTree->findEntitiesInMeters(center, radius, entities);
_entityTree->unlock();
foreach (const EntityItem* entity, entities) {

View file

@ -491,8 +491,9 @@ bool EntityTree::findInSphereOperation(OctreeElement* element, void* extraData)
}
// NOTE: assumes caller has handled locking
void EntityTree::findEntities(const glm::vec3& center, float radius, QVector<const EntityItem*>& foundEntities) {
FindAllNearPointArgs args = { center, radius };
void EntityTree::findEntitiesInMeters(const glm::vec3& center, float radius, QVector<const EntityItem*>& foundEntities) {
// position and targetRadius are in meters, so we need to convert to TreeUnits in FindNearPointArgs
FindAllNearPointArgs args = { center / (float)TREE_SCALE, radius / (float)TREE_SCALE };
// NOTE: This should use recursion, since this is a spatial operation
recurseTreeWithOperation(findInSphereOperation, &args);

View file

@ -105,11 +105,11 @@ public:
/// finds all entities that touch a sphere
/// \param center the center of the sphere
/// \param radius the radius of the sphere
/// \param center the center of the sphere in world-frame (meters)
/// \param radius the radius of the sphere in world-frame (meters)
/// \param foundEntities[out] vector of const EntityItem*
/// \remark Side effect: any initial contents in foundEntities will be lost
void findEntities(const glm::vec3& center, float radius, QVector<const EntityItem*>& foundEntities);
void findEntitiesInMeters(const glm::vec3& center, float radius, QVector<const EntityItem*>& foundEntities);
/// finds all entities that touch a cube
/// \param cube the query cube

View file

@ -471,17 +471,23 @@ AABox AABox::clamp(float min, float max) const {
return AABox(clampedCorner, clampedScale);
}
AABox& AABox::operator += (const glm::vec3& point) {
_corner = glm::min(_corner, point);
_scale = glm::max(_scale, point - _corner);
return (*this);
}
AABox& AABox::operator += (const AABox& box) {
if (!box.isInvalid()) {
(*this) += box._corner;
_scale = glm::max(_scale, box.calcTopFarLeft() - _corner);
}
return (*this);
}
AABox& AABox::operator += (const glm::vec3& point) {
_corner = glm::min(_corner, point);
_scale = glm::max(_scale, point - _corner);
return (*this);
}
AABox& AABox::operator += (const AABox& box) {
if (!box.isInvalid()) {
(*this) += box._corner;
_scale = glm::max(_scale, box.calcTopFarLeft() - _corner);
}
return (*this);
}
AABox& AABox::operator *= (float multiplier) {
_corner *= multiplier;
_scale *= multiplier;
return (*this);
}

View file

@ -72,9 +72,11 @@ public:
AABox clamp(const glm::vec3& min, const glm::vec3& max) const;
AABox clamp(float min, float max) const;
AABox& operator += (const glm::vec3& point);
AABox& operator += (const AABox& box);
AABox& operator += (const glm::vec3& point);
AABox& operator += (const AABox& box);
AABox& operator *= (float multiplier);
bool isInvalid() const { return _corner == glm::vec3(std::numeric_limits<float>::infinity()); }
private: