Point2f.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Point2f.m
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Point2f.h"
  7. #import "Rect2f.h"
  8. @implementation Point2f {
  9. cv::Point2f native;
  10. }
  11. - (float)x {
  12. return native.x;
  13. }
  14. - (void)setX:(float)val {
  15. native.x = val;
  16. }
  17. - (float)y {
  18. return native.y;
  19. }
  20. - (void)setY:(float)val {
  21. native.y = val;
  22. }
  23. - (cv::Point2f&)nativeRef {
  24. return native;
  25. }
  26. - (instancetype)init {
  27. return [self initWithX:0 y:0];
  28. }
  29. - (instancetype)initWithX:(float)x y:(float)y {
  30. self = [super init];
  31. if (self) {
  32. self.x = x;
  33. self.y = y;
  34. }
  35. return self;
  36. }
  37. - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  38. self = [super init];
  39. if (self) {
  40. [self set:vals];
  41. }
  42. return self;
  43. }
  44. + (instancetype)fromNative:(cv::Point2f&)point {
  45. return [[Point2f alloc] initWithX:point.x y:point.y];
  46. }
  47. - (void)update:(cv::Point2f&)point {
  48. self.x = point.x;
  49. self.y = point.y;
  50. }
  51. - (Point2f*) clone {
  52. return [[Point2f alloc] initWithX:self.x y:self.y];
  53. }
  54. - (double)dot:(Point2f*)point {
  55. return self.x * point.x + self.y * point.y;
  56. }
  57. - (void)set:(NSArray<NSNumber*>*)vals {
  58. self.x = (vals != nil && vals.count > 0) ? vals[0].doubleValue : 0;
  59. self.y = (vals != nil && vals.count > 1) ? vals[1].doubleValue : 0;
  60. }
  61. - (BOOL)isEqual:(id)other {
  62. if (other == self) {
  63. return YES;
  64. } else if (![other isKindOfClass:[Point2f class]]) {
  65. return NO;
  66. } else {
  67. Point2f* point = (Point2f*)other;
  68. return self.x == point.x && self.y == point.y;
  69. }
  70. }
  71. - (BOOL)inside:(Rect2f *)rect {
  72. return [rect contains:self];
  73. }
  74. #define FLOAT_TO_BITS(x) ((Cv32suf){ .f = x }).i
  75. - (NSUInteger)hash {
  76. int prime = 31;
  77. uint32_t result = 1;
  78. result = prime * result + FLOAT_TO_BITS(self.x);
  79. result = prime * result + FLOAT_TO_BITS(self.x);
  80. return result;
  81. }
  82. - (NSString *)description {
  83. return [NSString stringWithFormat:@"Point2f {%f,%f}", self.x, self.y];
  84. }
  85. @end