From a1624af969f9cf07e2056f2399b999396f86f3af Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 3 Jan 2015 22:15:15 +0100 Subject: [PATCH] Improved [Box|Rect]::Intersect performances Allowed early returns Former-commit-id: 59001afdd9734666640d440cb8b87b5a426973ce --- include/Nazara/Math/Box.inl | 35 +++++++++++++++++++---------------- include/Nazara/Math/Rect.inl | 34 +++++++++++++++++----------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/include/Nazara/Math/Box.inl b/include/Nazara/Math/Box.inl index 744d513ab..651a6de13 100644 --- a/include/Nazara/Math/Box.inl +++ b/include/Nazara/Math/Box.inl @@ -247,27 +247,30 @@ bool NzBox::Intersect(const NzBox& box, NzBox* intersection) const { T left = std::max(x, box.x); T right = std::min(x + width, box.x + box.width); + if (left >= right) + return false; + T top = std::max(y, box.y); T bottom = std::min(y + height, box.y + box.height); + if (top >= bottom) + return false; + T up = std::max(z, box.z); T down = std::min(z + depth, box.z + box.depth); - - 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 + if (up >= down) 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 diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 611403706..3d1908b83 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -166,24 +166,24 @@ template bool NzRect::Intersect(const NzRect& rect, NzRect* intersection) const { T left = std::max(x, rect.x); - T right = std::min(x+width, rect.x+rect.width); - T top = std::max(y, rect.y); - 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 + T right = std::min(x + width, rect.x + rect.width); + if (left >= right) 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