RotatedRectTest.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package org.opencv.test.core;
  2. import org.opencv.core.CvType;
  3. import org.opencv.core.Point;
  4. import org.opencv.core.Rect;
  5. import org.opencv.core.RotatedRect;
  6. import org.opencv.core.MatOfRotatedRect;
  7. import org.opencv.core.Size;
  8. import org.opencv.test.OpenCVTestCase;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class RotatedRectTest extends OpenCVTestCase {
  12. private double angle;
  13. private Point center;
  14. private Size size;
  15. @Override
  16. protected void setUp() throws Exception {
  17. super.setUp();
  18. center = new Point(matSize / 2, matSize / 2);
  19. size = new Size(matSize / 4, matSize / 2);
  20. angle = 40;
  21. }
  22. public void testBoundingRect() {
  23. size = new Size(matSize / 2, matSize / 2);
  24. assertEquals(size.height, size.width);
  25. double length = size.height;
  26. angle = 45;
  27. RotatedRect rr = new RotatedRect(center, size, angle);
  28. Rect r = rr.boundingRect();
  29. double halfDiagonal = length * Math.sqrt(2) / 2;
  30. assertTrue((r.x == Math.floor(center.x - halfDiagonal)) && (r.y == Math.floor(center.y - halfDiagonal)));
  31. assertTrue((r.br().x >= Math.ceil(center.x + halfDiagonal)) && (r.br().y >= Math.ceil(center.y + halfDiagonal)));
  32. assertTrue((r.br().x - Math.ceil(center.x + halfDiagonal)) <= 1 && (r.br().y - Math.ceil(center.y + halfDiagonal)) <= 1);
  33. }
  34. public void testClone() {
  35. RotatedRect rrect = new RotatedRect(center, size, angle);
  36. RotatedRect clone = rrect.clone();
  37. assertNotNull(clone);
  38. assertTrue(rrect.center.equals(clone.center));
  39. assertTrue(rrect.size.equals(clone.size));
  40. assertTrue(rrect.angle == clone.angle);
  41. }
  42. public void testEqualsObject() {
  43. Point center2 = new Point(matSize / 3, matSize / 1.5);
  44. Size size2 = new Size(matSize / 2, matSize / 4);
  45. double angle2 = 0;
  46. RotatedRect rrect1 = new RotatedRect(center, size, angle);
  47. RotatedRect rrect2 = new RotatedRect(center2, size2, angle2);
  48. RotatedRect rrect3 = rrect1;
  49. RotatedRect clone1 = rrect1.clone();
  50. RotatedRect clone2 = rrect2.clone();
  51. assertTrue(rrect1.equals(rrect3));
  52. assertFalse(rrect1.equals(rrect2));
  53. assertTrue(rrect2.equals(clone2));
  54. clone2.angle = 10;
  55. assertFalse(rrect2.equals(clone2));
  56. assertTrue(rrect1.equals(clone1));
  57. clone1.center.x += 1;
  58. assertFalse(rrect1.equals(clone1));
  59. clone1.center.x -= 1;
  60. assertTrue(rrect1.equals(clone1));
  61. clone1.size.width += 1;
  62. assertFalse(rrect1.equals(clone1));
  63. assertFalse(rrect1.equals(size));
  64. }
  65. public void testHashCode() {
  66. RotatedRect rr = new RotatedRect(center, size, angle);
  67. assertEquals(rr.hashCode(), rr.hashCode());
  68. }
  69. public void testPoints() {
  70. RotatedRect rrect = new RotatedRect(center, size, angle);
  71. Point p[] = new Point[4];
  72. rrect.points(p);
  73. boolean is_p0_irrational = (100 * p[0].x != (int) (100 * p[0].x)) && (100 * p[0].y != (int) (100 * p[0].y));
  74. boolean is_p1_irrational = (100 * p[1].x != (int) (100 * p[1].x)) && (100 * p[1].y != (int) (100 * p[1].y));
  75. boolean is_p2_irrational = (100 * p[2].x != (int) (100 * p[2].x)) && (100 * p[2].y != (int) (100 * p[2].y));
  76. boolean is_p3_irrational = (100 * p[3].x != (int) (100 * p[3].x)) && (100 * p[3].y != (int) (100 * p[3].y));
  77. assertTrue(is_p0_irrational && is_p1_irrational && is_p2_irrational && is_p3_irrational);
  78. assertTrue("Symmetric points 0 and 2",
  79. Math.abs((p[0].x + p[2].x) / 2 - center.x) + Math.abs((p[0].y + p[2].y) / 2 - center.y) < EPS);
  80. assertTrue("Symmetric points 1 and 3",
  81. Math.abs((p[1].x + p[3].x) / 2 - center.x) + Math.abs((p[1].y + p[3].y) / 2 - center.y) < EPS);
  82. assertTrue("Orthogonal vectors 01 and 12",
  83. Math.abs((p[1].x - p[0].x) * (p[2].x - p[1].x) +
  84. (p[1].y - p[0].y) * (p[2].y - p[1].y)) < EPS);
  85. assertTrue("Orthogonal vectors 12 and 23",
  86. Math.abs((p[2].x - p[1].x) * (p[3].x - p[2].x) +
  87. (p[2].y - p[1].y) * (p[3].y - p[2].y)) < EPS);
  88. assertTrue("Orthogonal vectors 23 and 30",
  89. Math.abs((p[3].x - p[2].x) * (p[0].x - p[3].x) +
  90. (p[3].y - p[2].y) * (p[0].y - p[3].y)) < EPS);
  91. assertTrue("Orthogonal vectors 30 and 01",
  92. Math.abs((p[0].x - p[3].x) * (p[1].x - p[0].x) +
  93. (p[0].y - p[3].y) * (p[1].y - p[0].y)) < EPS);
  94. assertTrue("Length of the vector 01",
  95. Math.abs((p[1].x - p[0].x) * (p[1].x - p[0].x) +
  96. (p[1].y - p[0].y) * (p[1].y - p[0].y) - size.height * size.height) < EPS);
  97. assertTrue("Length of the vector 21",
  98. Math.abs((p[1].x - p[2].x) * (p[1].x - p[2].x) +
  99. (p[1].y - p[2].y) * (p[1].y - p[2].y) - size.width * size.width) < EPS);
  100. assertTrue("Angle of the vector 21 with the axes", Math.abs((p[2].x - p[1].x) / size.width - Math.cos(angle * Math.PI / 180)) < EPS);
  101. }
  102. public void testRotatedRect() {
  103. RotatedRect rr = new RotatedRect();
  104. assertNotNull(rr);
  105. assertNotNull(rr.center);
  106. assertNotNull(rr.size);
  107. assertEquals(0.0, rr.angle);
  108. }
  109. public void testRotatedRectDoubleArray() {
  110. double[] vals = { 1.5, 2.6, 3.7, 4.2, 5.1 };
  111. RotatedRect rr = new RotatedRect(vals);
  112. assertNotNull(rr);
  113. assertEquals(1.5, rr.center.x);
  114. assertEquals(2.6, rr.center.y);
  115. assertEquals(3.7, rr.size.width);
  116. assertEquals(4.2, rr.size.height);
  117. assertEquals(5.1, rr.angle);
  118. }
  119. public void testRotatedRectPointSizeDouble() {
  120. RotatedRect rr = new RotatedRect(center, size, 40);
  121. assertNotNull(rr);
  122. assertNotNull(rr.center);
  123. assertNotNull(rr.size);
  124. assertEquals(40.0, rr.angle);
  125. }
  126. public void testSet() {
  127. double[] vals1 = {};
  128. RotatedRect r1 = new RotatedRect(center, size, 40);
  129. r1.set(vals1);
  130. assertEquals(0., r1.angle);
  131. assertPointEquals(new Point(0, 0), r1.center, EPS);
  132. assertSizeEquals(new Size(0, 0), r1.size, EPS);
  133. double[] vals2 = { 1, 2, 3, 4, 5 };
  134. RotatedRect r2 = new RotatedRect(center, size, 40);
  135. r2.set(vals2);
  136. assertEquals(5., r2.angle);
  137. assertPointEquals(new Point(1, 2), r2.center, EPS);
  138. assertSizeEquals(new Size(3, 4), r2.size, EPS);
  139. }
  140. public void testToString() {
  141. String actual = new RotatedRect(new Point(1, 2), new Size(10, 12), 4.5).toString();
  142. String expected = "{ {1.0, 2.0} 10x12 * 4.5 }";
  143. assertEquals(expected, actual);
  144. }
  145. public void testMatOfRotatedRect() {
  146. RotatedRect a = new RotatedRect(new Point(1,2),new Size(3,4),5.678);
  147. RotatedRect b = new RotatedRect(new Point(9,8),new Size(7,6),5.432);
  148. MatOfRotatedRect m = new MatOfRotatedRect(a,b,a,b,a,b,a,b);
  149. assertEquals(m.rows(), 8);
  150. assertEquals(m.cols(), 1);
  151. assertEquals(m.type(), CvType.CV_32FC(5));
  152. RotatedRect[] arr = m.toArray();
  153. assertEquals(arr[2].angle, a.angle, EPS);
  154. assertEquals(arr[3].center.x, b.center.x);
  155. assertEquals(arr[3].size.width, b.size.width);
  156. List<RotatedRect> li = m.toList();
  157. assertEquals(li.size(), 8);
  158. RotatedRect rr = li.get(7);
  159. assertEquals(rr.angle, b.angle, EPS);
  160. assertEquals(rr.center.y, b.center.y);
  161. }
  162. }