Commit dae77bdb authored by VAN TOLL Wouter's avatar VAN TOLL Wouter

Made neighbor queries more efficient by pre-sizing the result vector.

parent 76cc8b01
......@@ -42,15 +42,18 @@ std::vector<size_t> AgentKDTree::FindAllAgentsInRange(const Vector2D& position,
auto nrResults = kdTree->radiusSearch(q, radius*radius, result_indicesAndDistances, params);
// convert the result to a list of agent IDs, possibly ignoring a certain agent
std::vector<size_t> result;
std::vector<size_t> result(nrResults); size_t index = 0;
for (size_t i = 0; i < nrResults; ++i)
{
const size_t pointCloudIndex = result_indicesAndDistances[i].first;
const size_t agentID = pointCloud.agentPositions[pointCloudIndex].first;
if (agentToIgnore != nullptr && agentID == agentToIgnore->getID())
continue;
result.push_back(agentID);
result[index] = agentID;
++index;
}
if (index < nrResults)
result.pop_back();
return result;
}
......
......@@ -54,9 +54,9 @@ std::vector<const Agent*> WorldBase::computeNeighboringAgents_Flat(const Vector2
const auto& agentIDs = agentKDTree->FindAllAgentsInRange(position, search_radius, queryingAgent);
// convert them to agent pointers
std::vector<const Agent*> result;
for (const auto& ID : agentIDs)
result.push_back(GetAgent_const(ID));
std::vector<const Agent*> result(agentIDs.size());
for (int i = 0; i < agentIDs.size(); ++i)
result[i] = GetAgent_const(agentIDs[i]);
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment