Rect2d.mm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Rect2d.mm
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Rect2d.h"
  7. #import "Point2d.h"
  8. #import "Size2d.h"
  9. @implementation Rect2d {
  10. cv::Rect2d native;
  11. }
  12. - (double)x {
  13. return native.x;
  14. }
  15. - (void)setX:(double)val {
  16. native.x = val;
  17. }
  18. - (double)y {
  19. return native.y;
  20. }
  21. - (void)setY:(double)val {
  22. native.y = val;
  23. }
  24. - (double)width {
  25. return native.width;
  26. }
  27. - (void)setWidth:(double)val {
  28. native.width = val;
  29. }
  30. - (double)height {
  31. return native.height;
  32. }
  33. - (void)setHeight:(double)val {
  34. native.height = val;
  35. }
  36. - (cv::Rect2d&)nativeRef {
  37. return native;
  38. }
  39. - (instancetype)initWithX:(double)x y:(double)y width:(double)width height:(double)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:(Point2d*)point1 point:(Point2d*)point2 {
  53. double x = (point1.x < point2.x ? point1.x : point2.x);
  54. double y = (point1.y < point2.y ? point1.y : point2.y);
  55. double width = (point1.x > point2.x ? point1.x : point2.x) - x;
  56. double 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:(Point2d*)point size:(Size2d*)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::Rect2d&)rect {
  70. return [[Rect2d alloc] initWithX:rect.x y:rect.y width:rect.width height:rect.height];
  71. }
  72. - (Rect2d*)clone {
  73. return [[Rect2d alloc] initWithX:self.x y:self.y width:self.width height:self.height];
  74. }
  75. - (Point2d*)tl {
  76. return [[Point2d alloc] initWithX:self.x y:self.y];
  77. }
  78. - (Point2d*)br {
  79. return [[Point2d alloc] initWithX:self.x + self.width y:self.y + self.height];
  80. }
  81. - (Size2d*)size {
  82. return [[Size2d 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:(Point2d*)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:[Rect2d class]]) {
  103. return NO;
  104. } else {
  105. Rect2d* rect = (Rect2d*)other;
  106. return self.x == rect.x && self.y == rect.y && self.width == rect.width && self.height == rect.height;
  107. }
  108. }
  109. #define DOUBLE_TO_BITS(x) ((Cv64suf){ .f = x }).i
  110. - (NSUInteger)hash {
  111. int prime = 31;
  112. uint32_t result = 1;
  113. int64_t temp = DOUBLE_TO_BITS(self.x);
  114. result = prime * result + (int32_t) (temp ^ (temp >> 32));
  115. temp = DOUBLE_TO_BITS(self.y);
  116. result = prime * result + (int32_t) (temp ^ (temp >> 32));
  117. temp = DOUBLE_TO_BITS(self.width);
  118. result = prime * result + (int32_t) (temp ^ (temp >> 32));
  119. temp = DOUBLE_TO_BITS(self.height);
  120. result = prime * result + (int32_t) (temp ^ (temp >> 32));
  121. return result;
  122. }
  123. - (NSString *)description {
  124. return [NSString stringWithFormat:@"Rect2d {%lf,%lf,%lf,%lf}", self.x, self.y, self.width, self.height];
  125. }
  126. @end