From ba07a1040dc41435ceb4947f2f2952e54b5a5ce6 Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Fri, 4 Jan 2019 15:21:30 +0900 Subject: [PATCH] lottie/vector: added intersected() and size() api to vrect class. Change-Id: Ibb4c764e8a12bc3b95cbd601b42c73b777793afd --- src/vector/CMakeLists.txt | 1 + src/vector/meson.build | 2 +- src/vector/vrect.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/vector/vrect.h | 9 +++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/vector/CMakeLists.txt b/src/vector/CMakeLists.txt index 573a33b..96c55d6 100644 --- a/src/vector/CMakeLists.txt +++ b/src/vector/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(pixman) target_sources(lottie-player PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/vrect.cpp" "${CMAKE_CURRENT_LIST_DIR}/vdasher.cpp" "${CMAKE_CURRENT_LIST_DIR}/vbrush.cpp" "${CMAKE_CURRENT_LIST_DIR}/vbitmap.cpp" diff --git a/src/vector/meson.build b/src/vector/meson.build index 3246e43..338b87e 100644 --- a/src/vector/meson.build +++ b/src/vector/meson.build @@ -14,7 +14,7 @@ source_file += files('vdrawhelper.cpp') source_file += files('vdrawhelper_sse2.cpp') source_file += files('vdrawable.cpp') - +source_file += files('vrect.cpp') source_file += files('vrle.cpp') source_file += files('vpath.cpp') source_file += files('vpathmesure.cpp') diff --git a/src/vector/vrect.cpp b/src/vector/vrect.cpp index c044a53..60d2116 100644 --- a/src/vector/vrect.cpp +++ b/src/vector/vrect.cpp @@ -15,3 +15,52 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#include "vrect.h" + +VRect VRect::operator&(const VRect &r) const +{ + if (empty()) + return VRect(); + + int l1 = x1; + int r1 = x1; + if (x2 - x1 + 1 < 0) + l1 = x2; + else + r1 = x2; + + int l2 = r.x1; + int r2 = r.x1; + if (r.x2 - r.x1 + 1 < 0) + l2 = r.x2; + else + r2 = r.x2; + + if (l1 > r2 || l2 > r1) + return VRect(); + + int t1 = y1; + int b1 = y1; + if (y2 - y1 + 1 < 0) + t1 = y2; + else + b1 = y2; + + int t2 = r.y1; + int b2 = r.y1; + if (r.y2 - r.y1 + 1 < 0) + t2 = r.y2; + else + b2 = r.y2; + + if (t1 > b2 || t2 > b1) + return VRect(); + + VRect tmp; + tmp.x1 = std::max(l1, l2); + tmp.x2 = std::min(r1, r2); + tmp.y1 = std::max(t1, t2); + tmp.y2 = std::min(b1, b2); + return tmp; +} diff --git a/src/vector/vrect.h b/src/vector/vrect.h index b38746f..2fa937e 100644 --- a/src/vector/vrect.h +++ b/src/vector/vrect.h @@ -38,6 +38,7 @@ public: V_CONSTEXPR int height() const {return y2-y1;} V_CONSTEXPR int x() const {return x1;} V_CONSTEXPR int y() const {return y1;} + VSize size() const {return {width(), height()};} void setLeft(int l) { x1 = l; } void setTop(int t) { y1 = t; } void setRight(int r) { x2 = r; } @@ -54,6 +55,9 @@ public: const VRect &) noexcept; friend VDebug & operator<<(VDebug &os, const VRect &o); + VRect intersected(const VRect &r) const; + VRect operator&(const VRect &r) const; + private: int x1{0}; int y1{0}; @@ -61,6 +65,11 @@ private: int y2{0}; }; +inline VRect VRect::intersected(const VRect &r) const +{ + return *this & r; +} + inline bool VRect::intersects(const VRect &r) { return (right() > r.left() && left() < r.right() && bottom() > r.top() && -- 2.7.4