Rect2i.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Rect2i.m
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Rect2i.h"
  7. #import "Point2i.h"
  8. #import "Size2i.h"
  9. @implementation Rect2i {
  10. cv::Rect2i native;
  11. }
  12. - (int)x {
  13. return native.x;
  14. }
  15. - (void)setX:(int)val {
  16. native.x = val;
  17. }
  18. - (int)y {
  19. return native.y;
  20. }
  21. - (void)setY:(int)val {
  22. native.y = val;
  23. }
  24. - (int)width {
  25. return native.width;
  26. }
  27. - (void)setWidth:(int)val {
  28. native.width = val;
  29. }
  30. - (int)height {
  31. return native.height;
  32. }
  33. - (void)setHeight:(int)val {
  34. native.height = val;
  35. }
  36. - (cv::Rect&)nativeRef {
  37. return native;
  38. }
  39. - (instancetype)initWithX:(int)x y:(int)y width:(int)width height:(int)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:(Point2i*)point1 point:(Point2i*)point2 {
  53. int x = (point1.x < point2.x ? point1.x : point2.x);
  54. int y = (point1.y < point2.y ? point1.y : point2.y);
  55. int width = (point1.x > point2.x ? point1.x : point2.x) - x;
  56. int 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:(Point2i*)point size:(Size2i*)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::Rect&)rect {
  70. return [[Rect2i alloc] initWithX:rect.x y:rect.y width:rect.width height:rect.height];
  71. }
  72. - (Rect2i*)clone {
  73. return [[Rect2i alloc] initWithX:self.x y:self.y width:self.width height:self.height];
  74. }
  75. - (Point2i*)tl {
  76. return [[Point2i alloc] initWithX:self.x y:self.y];
  77. }
  78. - (Point2i*)br {
  79. return [[Point2i alloc] initWithX:self.x + self.width y:self.y + self.height];
  80. }
  81. - (Size2i*)size {
  82. return [[Size2i 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:(Point2i*)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].intValue : 0;
  95. self.y = (vals != nil && vals.count > 1) ? vals[1].intValue : 0;
  96. self.width = (vals != nil && vals.count > 2) ? vals[2].intValue : 0;
  97. self.height = (vals != nil && vals.count > 3) ? vals[3].intValue : 0;
  98. }
  99. - (BOOL)isEqual:(id)other{
  100. if (other == self) {
  101. return YES;
  102. } else if (![other isKindOfClass:[Rect2i class]]) {
  103. return NO;
  104. } else {
  105. Rect2i* rect = (Rect2i*)other;
  106. return self.x == rect.x && self.y == rect.y && self.width == rect.width && self.height == rect.height;
  107. }
  108. }
  109. - (NSUInteger)hash {
  110. int prime = 31;
  111. uint32_t result = 1;
  112. result = prime * result + self.x;
  113. result = prime * result + self.y;
  114. result = prime * result + self.width;
  115. result = prime * result + self.height;
  116. return result;
  117. }
  118. - (NSString *)description {
  119. return [NSString stringWithFormat:@"Rect2i {%d,%d,%d,%d}", self.x, self.y, self.width, self.height];
  120. }
  121. @end