RotatedRectTest.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // RotatedRectTest.swift
  3. //
  4. // Created by Giles Payne on 2020/01/31.
  5. //
  6. import XCTest
  7. import OpenCV
  8. class RotatedRectTest: OpenCVTestCase {
  9. let angle: Double = 40
  10. let center = Point2f(x: Float(OpenCVTestCase.matSize / 2), y: Float(OpenCVTestCase.matSize / 2))
  11. let size = Size2f(width: Float(OpenCVTestCase.matSize / 4), height: Float(OpenCVTestCase.matSize / 2))
  12. func testBoundingRect() {
  13. let size = Size2f(width: Float(OpenCVTestCase.matSize / 2), height: Float(OpenCVTestCase.matSize / 2));
  14. XCTAssertEqual(size.height, size.width);
  15. let length = size.height;
  16. let angle: Double = 45
  17. let rr = RotatedRect(center: center, size: size, angle: angle)
  18. let r = rr.boundingRect()
  19. let halfDiagonal = length * sqrt(2) / 2
  20. XCTAssert(Float(r.x) == floor(center.x - halfDiagonal) && Float(r.y) == floor(center.y - halfDiagonal))
  21. XCTAssert((r.br().x >= ceil(center.x + halfDiagonal)) && (r.br().y >= ceil(center.y + halfDiagonal)))
  22. XCTAssert((r.br().x - ceil(center.x + halfDiagonal)) <= 1 && (r.br().y - ceil(center.y + halfDiagonal)) <= 1)
  23. }
  24. func testClone() {
  25. let rrect = RotatedRect(center: center, size: size, angle: angle)
  26. let clone = rrect.clone();
  27. XCTAssertNotNil(clone)
  28. XCTAssert(rrect.center == clone.center)
  29. XCTAssert(rrect.size == clone.size)
  30. XCTAssert(rrect.angle == clone.angle)
  31. }
  32. func testEqualsObject() {
  33. let center2 = Point2f(x: Float(OpenCVTestCase.matSize / 3), y: Float(OpenCVTestCase.matSize) / 1.5)
  34. let size2 = Size2f(width: Float(OpenCVTestCase.matSize / 2), height: Float(OpenCVTestCase.matSize / 4))
  35. let angle2:Double = 0
  36. let rrect1 = RotatedRect(center: center, size: size, angle: angle)
  37. let rrect2 = RotatedRect(center: center2, size: size2, angle: angle2)
  38. let rrect3 = rrect1
  39. let clone1 = rrect1.clone()
  40. let clone2 = rrect2.clone()
  41. XCTAssert(rrect1 == rrect3)
  42. XCTAssertFalse(rrect1 == rrect2)
  43. XCTAssert(rrect2 == clone2)
  44. clone2.angle = 10
  45. XCTAssertFalse(rrect2 == clone2)
  46. XCTAssert(rrect1 == clone1)
  47. clone1.center.x += 1
  48. XCTAssertFalse(rrect1 == clone1)
  49. clone1.center.x -= 1
  50. XCTAssert(rrect1 == clone1)
  51. clone1.size.width += 1
  52. XCTAssertFalse(rrect1 == clone1)
  53. XCTAssertFalse(rrect1 == size)
  54. }
  55. func testHashCode() {
  56. let rr = RotatedRect(center: center, size: size, angle: angle)
  57. XCTAssertEqual(rr.hash(), rr.hash())
  58. }
  59. func testPoints() {
  60. let rrect = RotatedRect(center: center, size: size, angle: angle);
  61. let p = rrect.points()
  62. let is_p0_irrational = (100 * p[0].x != round(100 * p[0].x)) && (100 * p[0].y != round(100 * p[0].y))
  63. let is_p1_irrational = (100 * p[1].x != round(100 * p[1].x)) && (100 * p[1].y != round(100 * p[1].y));
  64. let is_p2_irrational = (100 * p[2].x != round(100 * p[2].x)) && (100 * p[2].y != round(100 * p[2].y));
  65. let is_p3_irrational = (100 * p[3].x != round(100 * p[3].x)) && (100 * p[3].y != round(100 * p[3].y));
  66. XCTAssert(is_p0_irrational && is_p1_irrational && is_p2_irrational && is_p3_irrational)
  67. XCTAssert(abs((p[0].x + p[2].x) / 2 - center.x) + abs((p[0].y + p[2].y) / 2 - center.y) < OpenCVTestCase.FEPS, "Symmetric points 0 and 2")
  68. XCTAssert(abs((p[1].x + p[3].x) / 2 - center.x) + abs((p[1].y + p[3].y) / 2 - center.y) < OpenCVTestCase.FEPS, "Symmetric points 1 and 3")
  69. XCTAssert(abs((p[1].x - p[0].x) * (p[2].x - p[1].x) +
  70. (p[1].y - p[0].y) * (p[2].y - p[1].y)) < OpenCVTestCase.FEPS, "Orthogonal vectors 01 and 12")
  71. XCTAssert(abs((p[2].x - p[1].x) * (p[3].x - p[2].x) +
  72. (p[2].y - p[1].y) * (p[3].y - p[2].y)) < OpenCVTestCase.FEPS, "Orthogonal vectors 12 and 23");
  73. XCTAssert(abs((p[3].x - p[2].x) * (p[0].x - p[3].x) +
  74. (p[3].y - p[2].y) * (p[0].y - p[3].y)) < OpenCVTestCase.FEPS, "Orthogonal vectors 23 and 30")
  75. XCTAssert(abs((p[0].x - p[3].x) * (p[1].x - p[0].x) +
  76. (p[0].y - p[3].y) * (p[1].y - p[0].y)) < OpenCVTestCase.FEPS, "Orthogonal vectors 30 and 01")
  77. XCTAssert(abs((p[1].x - p[0].x) * (p[1].x - p[0].x) +
  78. (p[1].y - p[0].y) * (p[1].y - p[0].y) - size.height * size.height) < OpenCVTestCase.FEPS, "Length of the vector 01")
  79. XCTAssert(abs((p[1].x - p[2].x) * (p[1].x - p[2].x) +
  80. (p[1].y - p[2].y) * (p[1].y - p[2].y) - size.width * size.width) < OpenCVTestCase.FEPS, "Length of the vector 21")
  81. XCTAssert(abs((p[2].x - p[1].x) / size.width - Float(cos(angle * Double.pi / 180))) < OpenCVTestCase.FEPS, "Angle of the vector 21 with the axes");
  82. }
  83. func testRotatedRect() {
  84. let rr = RotatedRect()
  85. XCTAssertNotNil(rr)
  86. XCTAssertNotNil(rr.center)
  87. XCTAssertNotNil(rr.size)
  88. XCTAssertEqual(0.0, rr.angle)
  89. }
  90. func testRotatedRectDoubleArray() {
  91. let vals = [1.5, 2.6, 3.7, 4.2, 5.1]
  92. let rr = RotatedRect(vals: vals as [NSNumber])
  93. XCTAssertNotNil(rr)
  94. XCTAssertEqual(1.5, rr.center.x)
  95. XCTAssertEqual(2.6, rr.center.y)
  96. XCTAssertEqual(3.7, rr.size.width)
  97. XCTAssertEqual(4.2, rr.size.height)
  98. XCTAssertEqual(5.1, rr.angle)
  99. }
  100. func testRotatedRectPointSizeDouble() {
  101. let rr = RotatedRect(center: center, size: size, angle: 40);
  102. XCTAssertNotNil(rr)
  103. XCTAssertNotNil(rr.center)
  104. XCTAssertNotNil(rr.size)
  105. XCTAssertEqual(40.0, rr.angle);
  106. }
  107. func testSet() {
  108. let vals1: [Double] = []
  109. let r1 = RotatedRect(center: center, size: size, angle: 40);
  110. r1.set(vals: vals1 as [NSNumber])
  111. XCTAssertEqual(0, r1.angle)
  112. assertPoint2fEquals(Point2f(x: 0, y: 0), r1.center, OpenCVTestCase.FEPS)
  113. assertSize2fEquals(Size2f(width: 0, height: 0), r1.size, OpenCVTestCase.FEPS)
  114. let vals2 = [1, 2, 3, 4, 5]
  115. let r2 = RotatedRect(center: center, size: size, angle: 40)
  116. r2.set(vals: vals2 as [NSNumber])
  117. XCTAssertEqual(5, r2.angle)
  118. assertPoint2fEquals(Point2f(x: 1, y: 2), r2.center, OpenCVTestCase.FEPS)
  119. assertSize2fEquals(Size2f(width: 3, height: 4), r2.size, OpenCVTestCase.FEPS)
  120. }
  121. func testToString() {
  122. let actual = "\(RotatedRect(center: Point2f(x:1, y:2), size: Size2f(width:10, height:12), angle:4.5))"
  123. let expected = "RotatedRect {Point2f {1.000000,2.000000},Size2f {10.000000,12.000000},4.500000}"
  124. XCTAssertEqual(expected, actual);
  125. }
  126. }