Allow to Rect<float>::Intersect Merge and Inset works well 25/320425/2
authorEunki, Hong <eunkiki.hong@samsung.com>
Fri, 15 Nov 2024 05:29:22 +0000 (14:29 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Tue, 19 Nov 2024 08:12:19 +0000 (17:12 +0900)
Until now, we only use Intersect / Merge / Inset API only for Rect<int>.
Let's allow to use it for Rect<float> case.

Change-Id: I506a81adbb2ee494b70c0c74198232fc74705ce8
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
automated-tests/src/dali/utc-Dali-Rect.cpp
dali/public-api/math/rect.h

index 349989c9e6995d41f66b896df63816d1e84efc29..777130f1d2c8f2dcbae3b23ba70eead2c3ec544a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -365,3 +365,144 @@ int UtcDaliRectOStreamOperatorP(void)
   DALI_TEST_EQUALS(oss.str(), expectedOutput, TEST_LOCATION);
   END_TEST;
 }
+
+int UtcDaliRectIntersectP(void)
+{
+  TestApplication application;
+
+  // Test for float
+  {
+    Rect<float> baseRect(100.0f, 200.0f, 200.0f, 300.0f);
+
+    Rect<float> rf1 = baseRect;
+
+    tet_printf("Check rf1 changed if intersect successed\n");
+    Rect<float> rf2(50.0f, 250.3f, 200.0f, 300.0f);
+    DALI_TEST_CHECK(rf1.Intersect(rf2));
+    DALI_TEST_EQUALS(rf1, Rect<float>(100.0f, 250.3f, 150.0f, 249.7f), TEST_LOCATION);
+
+    tet_printf("Check rf1 didnt changed if not intersect\n");
+    Rect<float> rf3(254.5f, 0.0f, 30.6f, 234.56f);
+    DALI_TEST_CHECK(!rf1.Intersect(rf3));
+    DALI_TEST_EQUALS(rf1, Rect<float>(100.0f, 250.3f, 150.0f, 249.7f), TEST_LOCATION);
+
+    tet_printf("Check baseRect was intersect with rf3\n");
+    rf1 = baseRect;
+    DALI_TEST_CHECK(rf1.Intersect(rf3));
+    DALI_TEST_EQUALS(rf1, Rect<float>(254.5f, 200.0f, 30.6f, 34.56f), TEST_LOCATION);
+  }
+
+  // Test for int
+  {
+    Rect<int> baseRect(100, 200, 200, 300);
+
+    Rect<int> rf1 = baseRect;
+
+    tet_printf("Check rf1 changed if intersect successed\n");
+    Rect<int> rf2(50, 150, 200, 300);
+    DALI_TEST_CHECK(rf1.Intersect(rf2));
+    DALI_TEST_EQUALS(rf1, Rect<int>(100, 200, 150, 250), TEST_LOCATION);
+
+    tet_printf("Check rf1 didnt changed if not intersect\n");
+    Rect<int> rf3(254, 0, 10, 234);
+    DALI_TEST_CHECK(!rf1.Intersect(rf3));
+    DALI_TEST_EQUALS(rf1, Rect<int>(100, 200, 150, 250), TEST_LOCATION);
+
+    tet_printf("Check baseRect was intersect with rf3\n");
+    rf1 = baseRect;
+    DALI_TEST_CHECK(rf1.Intersect(rf3));
+    DALI_TEST_EQUALS(rf1, Rect<int>(254, 200, 10, 34), TEST_LOCATION);
+  }
+
+  END_TEST;
+}
+
+int UtcDaliRectMergeP(void)
+{
+  TestApplication application;
+
+  // Test for float
+  {
+    Rect<float> baseRect(100.0f, 200.0f, 200.0f, 300.0f);
+
+    Rect<float> rf1 = baseRect;
+
+    tet_printf("Check rf1 changed after merge\n");
+    Rect<float> rf2(50.0f, 250.3f, 200.0f, 300.0f);
+    rf1.Merge(rf2);
+    DALI_TEST_EQUALS(rf1, Rect<float>(50.0f, 200.0f, 250.0f, 350.3f), TEST_LOCATION);
+
+    tet_printf("Check rf1 changed after merge\n");
+    Rect<float> rf3(354.5f, 0.0f, 30.6f, 234.56f);
+    rf1.Merge(rf3);
+    DALI_TEST_EQUALS(rf1, Rect<float>(50.0f, 0.0f, 335.1f, 550.3f), TEST_LOCATION);
+
+    tet_printf("Check baseRect merge with rf3\n");
+    rf1 = baseRect;
+    rf1.Merge(rf3);
+    DALI_TEST_EQUALS(rf1, Rect<float>(100.0f, 0.0f, 285.1f, 500.0f), TEST_LOCATION);
+  }
+
+  // Test for int
+  {
+    Rect<int> baseRect(100, 200, 200, 300);
+
+    Rect<int> rf1 = baseRect;
+
+    tet_printf("Check rf1 changed after merge\n");
+    Rect<int> rf2(50, 150, 200, 300);
+    rf1.Merge(rf2);
+    DALI_TEST_EQUALS(rf1, Rect<int>(50, 150, 250, 350), TEST_LOCATION);
+
+    tet_printf("Check rf1 changed after merge\n");
+    Rect<int> rf3(354, 0, 10, 234);
+    rf1.Merge(rf3);
+    DALI_TEST_EQUALS(rf1, Rect<int>(50, 0, 314, 500), TEST_LOCATION);
+
+    tet_printf("Check baseRect merge with rf3\n");
+    rf1 = baseRect;
+    rf1.Merge(rf3);
+    DALI_TEST_EQUALS(rf1, Rect<int>(100, 0, 264, 500), TEST_LOCATION);
+  }
+
+  END_TEST;
+}
+
+int UtcDaliRectInsetP(void)
+{
+  TestApplication application;
+
+  // Test for float
+  {
+    Rect<float> rf1(100.0f, 200.0f, 200.0f, 300.0f);
+
+    tet_printf("Check rf1 changed after inset\n");
+    rf1.Inset(-50.0f, 50.0f);
+    DALI_TEST_EQUALS(rf1, Rect<float>(150.0f, 150.0f, 100.0f, 400.0f), TEST_LOCATION);
+
+    rf1.Inset(100.0f, -100.0f);
+    DALI_TEST_EQUALS(rf1, Rect<float>(50.0f, 250.0f, 300.0f, 200.0f), TEST_LOCATION);
+
+    tet_printf("Check rf1 invalid after large inset\n");
+    rf1.Inset(-150.0f, -150.0f);
+    DALI_TEST_CHECK(!rf1.IsValid());
+  }
+
+  // Test for int
+  {
+    Rect<int> rf1(100, 200, 200, 300);
+
+    tet_printf("Check rf1 changed after inset\n");
+    rf1.Inset(-50, 50);
+    DALI_TEST_EQUALS(rf1, Rect<int>(150, 150, 100, 400), TEST_LOCATION);
+
+    rf1.Inset(100, -100);
+    DALI_TEST_EQUALS(rf1, Rect<int>(50, 250, 300, 200), TEST_LOCATION);
+
+    tet_printf("Check rf1 invalid after large inset\n");
+    rf1.Inset(-100, -101);
+    DALI_TEST_CHECK(!rf1.IsValid());
+  }
+
+  END_TEST;
+}
index 87b8f4e7ed57ead8ed1949ee6844033e22df71a6..e6246af93b8d6b4ef032a905a2ee6a812f66cd47 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_RECT_H
 
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -252,13 +252,13 @@ struct Rect
    */
   bool Intersect(const Rect<T>& rect)
   {
-    const int left   = std::max(rect.x, x);
-    const int top    = std::max(rect.y, y);
-    const int right  = std::min(rect.x + rect.width, x + width);
-    const int bottom = std::min(rect.y + rect.height, y + height);
+    const T left   = std::max(rect.x, x);
+    const T top    = std::max(rect.y, y);
+    const T right  = std::min(rect.x + rect.width, x + width);
+    const T bottom = std::min(rect.y + rect.height, y + height);
 
-    const int width  = right - left;
-    const int height = bottom - top;
+    const T width  = right - left;
+    const T height = bottom - top;
     if(!(width < 0 || height < 0))
     {
       x            = left;
@@ -280,14 +280,14 @@ struct Rect
    */
   void Merge(const Rect<T>& rect)
   {
-    const int left   = std::min(rect.x, x);
-    const int top    = std::min(rect.y, y);
-    const int right  = std::max(rect.x + rect.width, x + width);
-    const int bottom = std::max(rect.y + rect.height, y + height);
-    x                = left;
-    y                = top;
-    width            = right - left;
-    height           = bottom - top;
+    const T left   = std::min(rect.x, x);
+    const T top    = std::min(rect.y, y);
+    const T right  = std::max(rect.x + rect.width, x + width);
+    const T bottom = std::max(rect.y + rect.height, y + height);
+    x              = left;
+    y              = top;
+    width          = right - left;
+    height         = bottom - top;
   }
 
   /**
@@ -298,14 +298,14 @@ struct Rect
    */
   void Inset(T dx, T dy)
   {
-    const int left   = x - dx;
-    const int top    = y - dy;
-    const int right  = x + width + dx;
-    const int bottom = y + height + dy;
-    x                = left;
-    y                = top;
-    width            = right - left;
-    height           = bottom - top;
+    const T left   = x - dx;
+    const T top    = y - dy;
+    const T right  = x + width + dx;
+    const T bottom = y + height + dy;
+    x              = left;
+    y              = top;
+    width          = right - left;
+    height         = bottom - top;
   }
 
   /**