Improved [Box|Rect]::Intersect performances

Allowed early returns


Former-commit-id: 59001afdd9734666640d440cb8b87b5a426973ce
This commit is contained in:
Lynix 2015-01-03 22:15:15 +01:00
parent ad3c70bbc8
commit a1624af969
2 changed files with 36 additions and 33 deletions

View File

@ -247,27 +247,30 @@ bool NzBox<T>::Intersect(const NzBox& box, NzBox* intersection) const
{ {
T left = std::max(x, box.x); T left = std::max(x, box.x);
T right = std::min(x + width, box.x + box.width); T right = std::min(x + width, box.x + box.width);
if (left >= right)
return false;
T top = std::max(y, box.y); T top = std::max(y, box.y);
T bottom = std::min(y + height, box.y + box.height); T bottom = std::min(y + height, box.y + box.height);
if (top >= bottom)
return false;
T up = std::max(z, box.z); T up = std::max(z, box.z);
T down = std::min(z + depth, box.z + box.depth); T down = std::min(z + depth, box.z + box.depth);
if (up >= down)
if (left < right && top < bottom && up < down)
{
if (intersection)
{
intersection->x = left;
intersection->y = top;
intersection->z = up;
intersection->width = right - left;
intersection->height = bottom - top;
intersection->depth = down - up;
}
return true;
}
else
return false; return false;
if (intersection)
{
intersection->x = left;
intersection->y = top;
intersection->z = up;
intersection->width = right - left;
intersection->height = bottom - top;
intersection->depth = down - up;
}
return true;
} }
template<typename T> template<typename T>

View File

@ -166,24 +166,24 @@ template<typename T>
bool NzRect<T>::Intersect(const NzRect& rect, NzRect* intersection) const bool NzRect<T>::Intersect(const NzRect& rect, NzRect* intersection) const
{ {
T left = std::max(x, rect.x); T left = std::max(x, rect.x);
T right = std::min(x+width, rect.x+rect.width); T right = std::min(x + width, rect.x + rect.width);
T top = std::max(y, rect.y); if (left >= right)
T bottom = std::min(y+height, rect.y+rect.height);
if (left < right && top < bottom)
{
if (intersection)
{
intersection->x = left;
intersection->y = top;
intersection->width = right-left;
intersection->height = bottom-top;
}
return true;
}
else
return false; return false;
T top = std::max(y, rect.y);
T bottom = std::min(y + height, rect.y + rect.height);
if (top >= bottom)
return false;
if (intersection)
{
intersection->x = left;
intersection->y = top;
intersection->width = right - left;
intersection->height = bottom - top;
}
return true;
} }
template<typename T> template<typename T>