Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 143 additions & 128 deletions include/fcl/broadphase/broadphase_SaP-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,145 +319,103 @@ void SaPCollisionManager<S>::setup()

//==============================================================================
template <typename S>
void SaPCollisionManager<S>::update_(SaPAABB* updated_aabb)
void SaPCollisionManager<S>::updateEndPoint(
EndPoint* endpoint, int direction, size_t coord, const S& new_value,
const AABB<S>& object_aabb)
{
if(updated_aabb->cached.equal(updated_aabb->obj->getAABB()))
if(direction == 0)
return;

SaPAABB* current = updated_aabb;

Vector3<S> new_min = current->obj->getAABB().min_;
Vector3<S> new_max = current->obj->getAABB().max_;

SaPAABB dummy;
dummy.cached = current->obj->getAABB();

for(int coord = 0; coord < 3; ++coord)
// We care if max crosses in, or min crosses max.
const char crossed_minmax = endpoint->minmax == 0 ? 1 : 0;
// We add overlaps if the lo expands negatively, or the hi expands positively.
const bool adding_overlap = endpoint->minmax == (direction > 0);
// If we're adding, we need to find out if the *new* configuration leads to
// overlap. Otherwise, we need to find out if the *original* configuration
// overlapped.
const AABB<S>& overlap_test_aabb =
adding_overlap ? object_aabb : endpoint->aabb->cached;
// Pick the appropriate action based on endpoint motion: add or remove
// overlapping pairs.
const auto overlap_pair_action =
adding_overlap ? &SaPCollisionManager<S>::addToOverlapPairs
: &SaPCollisionManager<S>::removeFromOverlapPairs;

// Initial candidate is the neighbor in the specified direction.
EndPoint* candidate =
direction > 0 ? endpoint->next[coord] : endpoint->prev[coord];
EndPoint* last_crossed = nullptr;

// We'll now scan in the indicated `direction`, testing endpoints we cross
// (because their values are not as far in `direction` as the `new_value`).
while(candidate != nullptr &&
(direction > 0 ? candidate->getVal(coord) < new_value
: candidate->getVal(coord) > new_value))
{
int direction; // -1 reverse, 0 nochange, 1 forward
EndPoint* temp;

if(current->lo->getVal(coord) > new_min[coord])
direction = -1;
else if(current->lo->getVal(coord) < new_min[coord])
direction = 1;
else direction = 0;

if(direction == -1)
// Note: For a single collision object, lo may temporarily cross hi (or
// vice versa). If that happens, this has no impact on the set of
// overlapping pairs.
if(candidate->aabb != endpoint->aabb &&
candidate->minmax == crossed_minmax &&
candidate->aabb->cached.overlap(overlap_test_aabb))
{
//first update the "lo" endpoint of the interval
if(current->lo->prev[coord] != nullptr)
{
temp = current->lo;
while((temp != nullptr) && (temp->getVal(coord) > new_min[coord]))
{
if(temp->minmax == 1)
if(temp->aabb->cached.overlap(dummy.cached))
addToOverlapPairs(SaPPair(temp->aabb->obj, current->obj));
temp = temp->prev[coord];
}

if(temp == nullptr)
{
current->lo->prev[coord]->next[coord] = current->lo->next[coord];
current->lo->next[coord]->prev[coord] = current->lo->prev[coord];
current->lo->prev[coord] = nullptr;
current->lo->next[coord] = elist[coord];
elist[coord]->prev[coord] = current->lo;
elist[coord] = current->lo;
}
else
{
current->lo->prev[coord]->next[coord] = current->lo->next[coord];
current->lo->next[coord]->prev[coord] = current->lo->prev[coord];
current->lo->prev[coord] = temp;
current->lo->next[coord] = temp->next[coord];
temp->next[coord]->prev[coord] = current->lo;
temp->next[coord] = current->lo;
}
}

current->lo->getVal(coord) = new_min[coord];

// update hi end point
temp = current->hi;
while(temp->getVal(coord) > new_max[coord])
{
if((temp->minmax == 0) && (temp->aabb->cached.overlap(current->cached)))
removeFromOverlapPairs(SaPPair(temp->aabb->obj, current->obj));
temp = temp->prev[coord];
}

current->hi->prev[coord]->next[coord] = current->hi->next[coord];
if(current->hi->next[coord] != nullptr)
current->hi->next[coord]->prev[coord] = current->hi->prev[coord];
current->hi->prev[coord] = temp;
current->hi->next[coord] = temp->next[coord];
if(temp->next[coord] != nullptr)
temp->next[coord]->prev[coord] = current->hi;
temp->next[coord] = current->hi;

current->hi->getVal(coord) = new_max[coord];
(this->*overlap_pair_action)(
SaPPair(candidate->aabb->obj, endpoint->aabb->obj));
}
else if(direction == 1)
{
//here, we first update the "hi" endpoint.
if(current->hi->next[coord] != nullptr)
{
temp = current->hi;
while((temp->next[coord] != nullptr) && (temp->getVal(coord) < new_max[coord]))
{
if(temp->minmax == 0)
if(temp->aabb->cached.overlap(dummy.cached))
addToOverlapPairs(SaPPair(temp->aabb->obj, current->obj));
temp = temp->next[coord];
}
last_crossed = candidate;
candidate =
direction > 0 ? candidate->next[coord] : candidate->prev[coord];
}

if(temp->getVal(coord) < new_max[coord])
{
current->hi->prev[coord]->next[coord] = current->hi->next[coord];
current->hi->next[coord]->prev[coord] = current->hi->prev[coord];
current->hi->prev[coord] = temp;
current->hi->next[coord] = nullptr;
temp->next[coord] = current->hi;
}
else
{
current->hi->prev[coord]->next[coord] = current->hi->next[coord];
current->hi->next[coord]->prev[coord] = current->hi->prev[coord];
current->hi->prev[coord] = temp->prev[coord];
current->hi->next[coord] = temp;
temp->prev[coord]->next[coord] = current->hi;
temp->prev[coord] = current->hi;
}
}
// If during the scan, we've "crossed" other endpoints, we'll have to reorder
// the list. The updated endpoint goes just beyond the last crossed endpoint.
if(last_crossed != nullptr)
{
endpoint->removeFromList(coord, elist[coord]);
if(direction > 0)
endpoint->insertAfter(last_crossed, coord);
else
endpoint->insertBefore(last_crossed, coord, elist[coord]);
}

current->hi->getVal(coord) = new_max[coord];
// Finally, set the value.
endpoint->getVal(coord) = new_value;
}

//then, update the "lo" endpoint of the interval.
temp = current->lo;
//==============================================================================
template <typename S>
void SaPCollisionManager<S>::update_(SaPAABB* updated_sap_aabb)
{
const AABB<S>& object_aabb = updated_sap_aabb->obj->getAABB();
if(updated_sap_aabb->cached.equal(object_aabb))
return;

while(temp->getVal(coord) < new_min[coord])
{
if((temp->minmax == 1) && (temp->aabb->cached.overlap(current->cached)))
removeFromOverlapPairs(SaPPair(temp->aabb->obj, current->obj));
temp = temp->next[coord];
}
const Vector3<S>& new_min = object_aabb.min_;
const Vector3<S>& new_max = object_aabb.max_;

if(current->lo->prev[coord] != nullptr)
current->lo->prev[coord]->next[coord] = current->lo->next[coord];
else
elist[coord] = current->lo->next[coord];
current->lo->next[coord]->prev[coord] = current->lo->prev[coord];
current->lo->prev[coord] = temp->prev[coord];
current->lo->next[coord] = temp;
if(temp->prev[coord] != nullptr)
temp->prev[coord]->next[coord] = current->lo;
else
elist[coord] = current->lo;
temp->prev[coord] = current->lo;
current->lo->getVal(coord) = new_min[coord];
}
for(int coord = 0; coord < 3; ++coord)
{
// End points can move for two reasons:
// 1. the object has translated (both endpoints move rigidly in tandem)
// 2. The object's AABB has changed; collision objects will frequently
// change AABB's based on orientation (the endpoints can move in
// independent directions).
// We need to compute both endpoints explicitly.

// The direction each end point is moving (-1, 0, or 1 indicating in the
// negative direction, not at all, or positive).
const int lo_direction =
updated_sap_aabb->lo->compareValue(coord, new_min[coord]);
const int hi_direction =
updated_sap_aabb->hi->compareValue(coord, new_max[coord]);

// Note: between these two invocations, it's possible for the ordering of
// the lo and hi endpoints to reverse temporarily. Their ordering will
// always be correct at the end of the method.
updateEndPoint(updated_sap_aabb->lo, lo_direction, coord,
new_min[coord], object_aabb);
updateEndPoint(updated_sap_aabb->hi, hi_direction, coord,
new_max[coord], object_aabb);
}
}

Expand Down Expand Up @@ -882,6 +840,53 @@ size_t SaPCollisionManager<S>::size() const
return AABB_arr.size();
}

//==============================================================================
template <typename S>
void SaPCollisionManager<S>::EndPoint::removeFromList(size_t coord,
EndPoint*& head)
{
if(prev[coord] != nullptr)
prev[coord]->next[coord] = next[coord];
else
head = next[coord];

if(next[coord] != nullptr)
next[coord]->prev[coord] = prev[coord];

prev[coord] = nullptr;
next[coord] = nullptr;
}

//==============================================================================
template <typename S>
void SaPCollisionManager<S>::EndPoint::insertBefore(EndPoint* other,
size_t coord,
EndPoint*& head)
{
prev[coord] = other->prev[coord];
next[coord] = other;

if(prev[coord] != nullptr)
prev[coord]->next[coord] = this;
else
head = this;

other->prev[coord] = this;
}

//==============================================================================
template <typename S>
void SaPCollisionManager<S>::EndPoint::insertAfter(EndPoint* other, size_t coord)
{
prev[coord] = other;
next[coord] = other->next[coord];

if(next[coord] != nullptr)
next[coord]->prev[coord] = this;

other->next[coord] = this;
}

//==============================================================================
template <typename S>
const Vector3<S>&SaPCollisionManager<S>::EndPoint::getVal() const
Expand Down Expand Up @@ -918,6 +923,16 @@ S& SaPCollisionManager<S>::EndPoint::getVal(size_t i)
return aabb->cached.min_[i];
}

//==============================================================================
template <typename S>
int SaPCollisionManager<S>::EndPoint::compareValue(size_t i,
const S& new_value) const {
const S val = getVal(i);
if(new_value < val) return -1;
if(new_value > val) return 1;
return 0;
}

//==============================================================================
template <typename S>
SaPCollisionManager<S>::SaPPair::SaPPair(CollisionObject<S>* a, CollisionObject<S>* b)
Expand Down
35 changes: 34 additions & 1 deletion include/fcl/broadphase/broadphase_SaP.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,22 @@ class FCL_EXPORT SaPCollisionManager : public BroadPhaseCollisionManager<S>
/// @brief Functor to help remove collision pairs no longer valid (i.e., should be culled away)
class FCL_EXPORT isNotValidPair;

void update_(SaPAABB* updated_aabb);
// Updates an endpoint to match its collision object's current AABB. This
// method will:
//
// 1. Update the endpoint's value (to new_value).
// 2. Update the endpoint's position in the sorted list of endpoints.
// 3. Update the overlap pairs as necessary (possibly adding, possibly
// removing).
// @param[out] endpoint The EndPoint to update.
// @param direction The direction of movement: -1, 0, or 1.
// @param coord The axis along which the endpoint is moving.
// @param new_value The endpoint's new value.
// @param object_aabb The collision object's updated AABB.
void updateEndPoint(EndPoint* endpoint, int direction, size_t coord,
const S& new_value, const AABB<S>& object_aabb);

void update_(SaPAABB* updated_sap_aabb);

void updateVelist();

Expand Down Expand Up @@ -189,6 +204,18 @@ struct SaPCollisionManager<S>::EndPoint
/// @brief the next end point in the end point list
EndPoint* next[3];

/// @brief Remove this end point from an axis's linked list.
/// @param coord The axis index along which this end point is moving.
/// @param head[out] The *current* head of the axi's linked list (will mutate
/// if `this` is the current head).
void removeFromList(size_t coord, EndPoint*& head);

/// @brief Insert this detached end point immediately before @p other.
void insertBefore(EndPoint* other, size_t coord, EndPoint*& head);

/// @brief Insert this detached end point immediately after @p other.
void insertAfter(EndPoint* other, size_t coord);

/// @brief get the value of the end point
const Vector3<S>& getVal() const;

Expand All @@ -199,6 +226,12 @@ struct SaPCollisionManager<S>::EndPoint

S& getVal(size_t i);

// Compares new_value with getValue(i).
// Returns -1 if new_value < getValue(i).
// Returns 0 if new_value == getValue(i).
// Returns 1 if new_value > getValue(i).
int compareValue(size_t i, const S& new_value) const;

};

/// @brief A pair of objects that are not culling away and should further check collision
Expand Down
1 change: 1 addition & 0 deletions test/broadphase/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(tests
test_broadphase_dynamic_AABB_tree.cpp
test_broadphase_SaP.cpp
)

# Build all the tests
Expand Down
Loading
Loading