RotatedRect.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // RotatedRect.m
  3. //
  4. // Created by Giles Payne on 2019/12/26.
  5. //
  6. #import "RotatedRect.h"
  7. #import "Point2f.h"
  8. #import "Size2f.h"
  9. #import "Rect2f.h"
  10. #include <math.h>
  11. @implementation RotatedRect {
  12. cv::RotatedRect native;
  13. }
  14. - (cv::RotatedRect&)nativeRef {
  15. native.center.x = self.center.x;
  16. native.center.y = self.center.y;
  17. native.size.width = self.size.width;
  18. native.size.height = self.size.height;
  19. native.angle = self.angle;
  20. return native;
  21. }
  22. - (instancetype)init {
  23. return [self initWithCenter:[Point2f new] size:[Size2f new] angle:0.0];
  24. }
  25. - (instancetype)initWithCenter:(Point2f*)center size:(Size2f*)size angle:(double)angle {
  26. self = [super init];
  27. if (self) {
  28. self.center = center;
  29. self.size = size;
  30. self.angle = angle;
  31. }
  32. return self;
  33. }
  34. - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  35. self = [self init];
  36. if (self) {
  37. [self set:vals];
  38. }
  39. return self;
  40. }
  41. + (instancetype)fromNative:(cv::RotatedRect&)rotatedRect {
  42. return [[RotatedRect alloc] initWithCenter:[Point2f fromNative:rotatedRect.center] size:[Size2f fromNative:rotatedRect.size] angle:rotatedRect.angle];
  43. }
  44. - (void)set:(NSArray<NSNumber*>*)vals {
  45. self.center.x = (vals != nil && vals.count > 0) ? vals[0].floatValue : 0.0;
  46. self.center.y = (vals != nil && vals.count > 1) ? vals[1].floatValue : 0.0;
  47. self.size.width = (vals != nil && vals.count > 2) ? vals[2].floatValue : 0.0;
  48. self.size.height = (vals != nil && vals.count > 3) ? vals[3].floatValue : 0.0;
  49. self.angle = (vals != nil && vals.count > 4) ? vals[4].doubleValue : 0.0;
  50. }
  51. - (NSArray<Point2f*>*)points {
  52. double angleRadians = self.angle * M_PI / 180.0;
  53. double b = cos(angleRadians) * 0.5;
  54. double a = sin(angleRadians) * 0.5f;
  55. Point2f* p0 = [[Point2f alloc] initWithX:self.center.x - a * self.size.height - b * self.size.width y:self.center.y + b * self.size.height - a * self.size.width];
  56. Point2f* p1 = [[Point2f alloc] initWithX:self.center.x + a * self.size.height - b * self.size.width y:self.center.y - b * self.size.height - a * self.size.width];
  57. Point2f* p2 = [[Point2f alloc] initWithX:2 * self.center.x - p0.x y:2 * self.center.y - p0.y];
  58. Point2f* p3 = [[Point2f alloc] initWithX:2 * self.center.x - p1.x y:2 * self.center.y - p1.y];
  59. return [NSArray arrayWithObjects:p0, p1, p2, p3, nil];
  60. }
  61. - (Rect2f*)boundingRect {
  62. NSArray<Point2f*>* pts = [self points];
  63. Rect2f* rect = [[Rect2f alloc] initWithX:(int)floor(MIN(MIN(MIN(pts[0].x, pts[1].x), pts[2].x), pts[3].x)) y:(int)floor(MIN(MIN(MIN(pts[0].y, pts[1].y), pts[2].y), pts[3].y)) width:(int)ceil(MAX(MAX(MAX(pts[0].x, pts[1].x), pts[2].x), pts[3].x)) height:(int)ceil(MAX(MAX(MAX(pts[0].y, pts[1].y), pts[2].y), pts[3].y))];
  64. rect.width -= rect.x - 1;
  65. rect.height -= rect.y - 1;
  66. return rect;
  67. }
  68. - (RotatedRect*)clone {
  69. return [[RotatedRect alloc] initWithCenter:[self.center clone] size:[self.size clone] angle:self.angle];
  70. }
  71. - (BOOL)isEqual:(id)other {
  72. if (other == self) {
  73. return YES;
  74. } else if (![other isKindOfClass:[RotatedRect class]]) {
  75. return NO;
  76. } else {
  77. RotatedRect* rect = (RotatedRect*)other;
  78. return [self.center isEqual:rect.center] && [self.size isEqual:rect.size] && self.angle == rect.angle;
  79. }
  80. }
  81. #define FLOAT_TO_BITS(x) ((Cv32suf){ .f = x }).i
  82. #define DOUBLE_TO_BITS(x) ((Cv64suf){ .f = x }).i
  83. - (NSUInteger)hash {
  84. int prime = 31;
  85. uint32_t result = 1;
  86. result = prime * result + FLOAT_TO_BITS(self.center.x);
  87. result = prime * result + FLOAT_TO_BITS(self.center.y);
  88. result = prime * result + FLOAT_TO_BITS(self.size.width);
  89. result = prime * result + FLOAT_TO_BITS(self.size.height);
  90. int64_t temp = DOUBLE_TO_BITS(self.angle);
  91. result = prime * result + (int32_t) (temp ^ (temp >> 32));
  92. return result;
  93. }
  94. - (NSString*)description {
  95. return [NSString stringWithFormat:@"RotatedRect {%@,%@,%lf}", self.center.description, self.size.description, self.angle];
  96. }
  97. @end