Rect2f.mm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // Rect2d.mm
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Rect2f.h"
  7. #import "Point2f.h"
  8. #import "Size2f.h"
  9. @implementation Rect2f {
  10. cv::Rect2f native;
  11. }
  12. - (float)x {
  13. return native.x;
  14. }
  15. - (void)setX:(float)val {
  16. native.x = val;
  17. }
  18. - (float)y {
  19. return native.y;
  20. }
  21. - (void)setY:(float)val {
  22. native.y = val;
  23. }
  24. - (float)width {
  25. return native.width;
  26. }
  27. - (void)setWidth:(float)val {
  28. native.width = val;
  29. }
  30. - (float)height {
  31. return native.height;
  32. }
  33. - (void)setHeight:(float)val {
  34. native.height = val;
  35. }
  36. - (cv::Rect2f&)nativeRef {
  37. return native;
  38. }
  39. - (instancetype)initWithX:(float)x y:(float)y width:(float)width height:(float)height {
  40. self = [super init];
  41. if (self) {
  42. self.x = x;
  43. self.y = y;
  44. self.width = width;
  45. self.height = height;
  46. }
  47. return self;
  48. }
  49. - (instancetype)init {
  50. return [self initWithX:0 y:0 width:0 height:0];
  51. }
  52. - (instancetype)initWithPoint:(Point2f*)point1 point:(Point2f*)point2 {
  53. float x = (point1.x < point2.x ? point1.x : point2.x);
  54. float y = (point1.y < point2.y ? point1.y : point2.y);
  55. float width = (point1.x > point2.x ? point1.x : point2.x) - x;
  56. float height = (point1.y > point2.y ? point1.y : point2.y) - y;
  57. return [self initWithX:x y:y width:width height:height];
  58. }
  59. - (instancetype)initWithPoint:(Point2f*)point size:(Size2f*)size {
  60. return [self initWithX:point.x y:point.y width:size.width height:size.height];
  61. }
  62. - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  63. self = [super init];
  64. if (self) {
  65. [self set:vals];
  66. }
  67. return self;
  68. }
  69. + (instancetype)fromNative:(cv::Rect2f&)rect {
  70. return [[Rect2f alloc] initWithX:rect.x y:rect.y width:rect.width height:rect.height];
  71. }
  72. - (Rect2f*)clone {
  73. return [[Rect2f alloc] initWithX:self.x y:self.y width:self.width height:self.height];
  74. }
  75. - (Point2f*)tl {
  76. return [[Point2f alloc] initWithX:self.x y:self.y];
  77. }
  78. - (Point2f*)br {
  79. return [[Point2f alloc] initWithX:self.x + self.width y:self.y + self.height];
  80. }
  81. - (Size2f*)size {
  82. return [[Size2f alloc] initWithWidth:self.width height:self.height];
  83. }
  84. - (double)area {
  85. return self.width * self.height;
  86. }
  87. - (BOOL)empty {
  88. return self.width <= 0 || self.height <= 0;
  89. }
  90. - (BOOL)contains:(Point2f*)point {
  91. return self.x <= point.x && point.x < self.x + self.width && self.y <= point.y && point.y < self.y + self.height;
  92. }
  93. - (void)set:(NSArray<NSNumber*>*)vals {
  94. self.x = (vals != nil && vals.count > 0) ? vals[0].floatValue : 0;
  95. self.y = (vals != nil && vals.count > 1) ? vals[1].floatValue : 0;
  96. self.width = (vals != nil && vals.count > 2) ? vals[2].floatValue : 0;
  97. self.height = (vals != nil && vals.count > 3) ? vals[3].floatValue : 0;
  98. }
  99. - (BOOL)isEqual:(id)other{
  100. if (other == self) {
  101. return YES;
  102. } else if (![other isKindOfClass:[Rect2f class]]) {
  103. return NO;
  104. } else {
  105. Rect2f* rect = (Rect2f*)other;
  106. return self.x == rect.x && self.y == rect.y && self.width == rect.width && self.height == rect.height;
  107. }
  108. }
  109. #define FLOAT_TO_BITS(x) ((Cv32suf){ .f = x }).i
  110. - (NSUInteger)hash {
  111. int prime = 31;
  112. uint32_t result = 1;
  113. result = prime * result + FLOAT_TO_BITS(self.x);
  114. result = prime * result + FLOAT_TO_BITS(self.y);
  115. result = prime * result + FLOAT_TO_BITS(self.width);
  116. result = prime * result + FLOAT_TO_BITS(self.height);
  117. return result;
  118. }
  119. - (NSString *)description {
  120. return [NSString stringWithFormat:@"Rect2f {%lf,%lf,%lf,%lf}", self.x, self.y, self.width, self.height];
  121. }
  122. @end