git.s-ol.nu ~forks/DiligentCore / 143d8e7
MathLib: added CheckBox2DBox2DOverlap function assiduous 9 months ago
2 changed file(s) with 96 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
934934 }
935935 }
936936
937
938 /// Checks if two 2D-boxes overlap.
939
940 /// \tparam [in] AllowTouch - Whether to consider two boxes overlapping if
941 /// they only touch at their boundaries or corners.
942 /// \tparam [in] T - Component type.
943 ///
944 /// \param [in] Box0Min - Min corner of the first box.
945 /// \param [in] Box0Max - Max corner of the first box.
946 /// \param [in] Box1Min - Min corner of the second box.
947 /// \param [in] Box1Max - Max corner of the second box.
948 ///
949 /// \return true if the bounding boxes overlap, and false otherwise.
950 template <bool AllowTouch, typename T>
951 bool CheckBox2DBox2DOverlap(const Vector2<T>& Box0Min,
952 const Vector2<T>& Box0Max,
953 const Vector2<T>& Box1Min,
954 const Vector2<T>& Box1Max)
955 {
956 VERIFY_EXPR(Box0Max.x >= Box0Min.x && Box0Max.y >= Box0Min.y &&
957 Box1Max.x >= Box1Min.x && Box1Max.y >= Box1Min.y);
958 if (AllowTouch)
959 {
960 return !(Box0Min.x > Box1Max.x || Box1Min.x > Box0Max.x || Box0Min.y > Box1Max.y || Box1Min.y > Box0Max.y);
961 }
962 else
963 {
964 return !(Box0Min.x >= Box1Max.x || Box1Min.x >= Box0Max.x || Box0Min.y >= Box1Max.y || Box1Min.y >= Box0Max.y);
965 }
966 }
967
968
937969 } // namespace Diligent
938970
939971 namespace std
19611961 }
19621962 }
19631963
1964 TEST(Common_AdvancedMath, CheckBox2DBox2DOverlap)
1965 {
1966 // clang-format off
1967 // Self-intersection
1968 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (uint2(0, 0), uint2(1, 1), uint2(0, 0), uint2(1, 1)));
1969 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(uint2(0, 0), uint2(1, 1), uint2(0, 0), uint2(1, 1)));
1970
1971 // One box fully inside another
1972 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (uint2(0, 0), uint2(10, 10), uint2(1, 1), uint2( 2, 2)));
1973 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (uint2(1, 1), uint2( 2, 2), uint2(0, 0), uint2(10, 10)));
1974 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(uint2(0, 0), uint2(10, 10), uint2(1, 1), uint2( 2, 2)));
1975 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(uint2(1, 1), uint2( 2, 2 ), uint2(0, 0), uint2(10, 10)));
1976
1977 // Touching corners
1978 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (int2(0, 0), int2(10, 10), int2(-1, -1), int2( 0, 0)));
1979 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(int2(0, 0), int2(10, 10), int2(-1, -1), int2( 0, 0)));
1980 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (int2(0, 0), int2(10, 10), int2(-1, 10), int2( 0, 11)));
1981 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(int2(0, 0), int2(10, 10), int2(-1, 10), int2( 0, 11)));
1982 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (int2(0, 0), int2(10, 10), int2(10, 10), int2(11, 11)));
1983 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(int2(0, 0), int2(10, 10), int2(10, 10), int2(11, 11)));
1984 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (int2(0, 0), int2(10, 10), int2(10, -1), int2(11, 0)));
1985 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(int2(0, 0), int2(10, 10), int2(10, -1), int2(11, 0)));
1986
1987 // Intersections
1988 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(-1, -1), float2( 1, 1)));
1989 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(-1, -1), float2( 1, 1)));
1990 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(-1, 9), float2( 1, 11)));
1991 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(-1, 9), float2( 1, 11)));
1992 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2( 9, 9), float2(11, 11)));
1993 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2( 9, 9), float2(11, 11)));
1994 EXPECT_TRUE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2( 9, -1), float2(11, 1)));
1995 EXPECT_TRUE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2( 9, -1), float2(11, 1)));
1996
1997 // No intersections
1998 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(-2, -2), float2(-1, -1)));
1999 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(-2, -2), float2(-1, -1)));
2000 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(-2, 5), float2(-1, 6)));
2001 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(-2, 5), float2(-1, 6)));
2002 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(-2, 11), float2(-1, 12)));
2003 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(-2, 11), float2(-1, 12)));
2004 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2( 5, 11), float2( 6, 12)));
2005 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2( 5, 11), float2( 6, 12)));
2006 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(11, 11), float2(12, 12)));
2007 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(11, 11), float2(12, 12)));
2008 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(11, 5), float2(12, 6)));
2009 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(11, 5), float2(12, 6)));
2010 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2(11, -2), float2(12, -1)));
2011 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2(11, -2), float2(12, -1)));
2012 EXPECT_FALSE(CheckBox2DBox2DOverlap<true> (float2(0, 0), float2(10, 10), float2( 5, -2), float2( 6, -1)));
2013 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(float2(0, 0), float2(10, 10), float2( 5, -2), float2( 6, -1)));
2014
2015 // Touching boundaries
2016 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (double2(0, 0), double2(10, 10), double2(-2, 5), double2( 0, 6)));
2017 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(double2(0, 0), double2(10, 10), double2(-2, 5), double2( 0, 6)));
2018 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (double2(0, 0), double2(10, 10), double2( 5, 10), double2( 6, 12)));
2019 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(double2(0, 0), double2(10, 10), double2( 5, 10), double2( 6, 12)));
2020 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (double2(0, 0), double2(10, 10), double2(10, 5), double2(12, 6)));
2021 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(double2(0, 0), double2(10, 10), double2(10, 5), double2(12, 6)));
2022 EXPECT_TRUE (CheckBox2DBox2DOverlap<true> (double2(0, 0), double2(10, 10), double2( 5, -2), double2( 6, 0)));
2023 EXPECT_FALSE(CheckBox2DBox2DOverlap<false>(double2(0, 0), double2(10, 10), double2( 5, -2), double2( 6, 0)));
2024
2025 // clang-format on
2026 }
2027
19642028 } // namespace