KeyPoint.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // KeyPoint.m
  3. //
  4. // Created by Giles Payne on 2019/12/25.
  5. //
  6. #import "KeyPoint.h"
  7. #import "Point2f.h"
  8. @implementation KeyPoint {
  9. cv::KeyPoint native;
  10. }
  11. - (cv::KeyPoint&)nativeRef {
  12. native.pt.x = self.pt.x;
  13. native.pt.y = self.pt.y;
  14. native.size = self.size;
  15. native.angle = self.angle;
  16. native.response = self.response;
  17. native.octave = self.octave;
  18. native.class_id = self.classId;
  19. return native;
  20. }
  21. - (instancetype)init {
  22. return [self initWithX:0 y:0 size:0];
  23. }
  24. - (instancetype)initWithX:(float)x y:(float)y size:(float)size angle:(float)angle response:(float)response octave:(int)octave classId:(int)classId {
  25. self = [super init];
  26. if (self != nil) {
  27. self.pt = [[Point2f alloc] initWithX:x y:y];
  28. self.size = size;
  29. self.angle = angle;
  30. self.response = response;
  31. self.octave = octave;
  32. self.classId = classId;
  33. }
  34. return self;
  35. }
  36. - (instancetype)initWithX:(float)x y:(float)y size:(float)size angle:(float)angle response:(float)response octave:(int)octave {
  37. return [self initWithX:x y:y size:size angle:angle response:response octave:octave classId:-1];
  38. }
  39. - (instancetype)initWithX:(float)x y:(float)y size:(float)size angle:(float)angle response:(float)response {
  40. return [self initWithX:x y:y size:size angle:angle response:response octave:0];
  41. }
  42. - (instancetype)initWithX:(float)x y:(float)y size:(float)size angle:(float)angle {
  43. return [self initWithX:x y:y size:size angle:angle response:0];
  44. }
  45. - (instancetype)initWithX:(float)x y:(float)y size:(float)size {
  46. return [self initWithX:x y:y size:size angle:-1];
  47. }
  48. + (instancetype)fromNative:(cv::KeyPoint&)keyPoint {
  49. return [[KeyPoint alloc] initWithX:keyPoint.pt.x y:keyPoint.pt.y size:keyPoint.size angle:keyPoint.angle response:keyPoint.response octave:keyPoint.octave classId:keyPoint.class_id];
  50. }
  51. - (KeyPoint*)clone {
  52. return [[KeyPoint alloc] initWithX:self.pt.x y:self.pt.y size:self.size angle:self.angle response:self.response octave:self.octave classId:self.classId];
  53. }
  54. - (BOOL)isEqual:(id)other {
  55. if (other == self) {
  56. return YES;
  57. } else if (![other isKindOfClass:[KeyPoint class]]) {
  58. return NO;
  59. } else {
  60. KeyPoint* keyPoint = (KeyPoint*)other;
  61. return [self.pt isEqual:keyPoint.pt] && self.size == keyPoint.size && self.angle == keyPoint.angle && self.response == keyPoint.response && self.octave == keyPoint.octave && self.classId == keyPoint.classId;
  62. }
  63. }
  64. #define FLOAT_TO_BITS(x) ((Cv32suf){ .f = x }).i
  65. - (NSUInteger)hash {
  66. int prime = 31;
  67. uint32_t result = 1;
  68. result = prime * result + FLOAT_TO_BITS(self.pt.x);
  69. result = prime * result + FLOAT_TO_BITS(self.pt.y);
  70. result = prime * result + FLOAT_TO_BITS(self.size);
  71. result = prime * result + FLOAT_TO_BITS(self.angle);
  72. result = prime * result + FLOAT_TO_BITS(self.response);
  73. result = prime * result + self.octave;
  74. result = prime * result + self.classId;
  75. return result;
  76. }
  77. - (NSString*)description {
  78. return [NSString stringWithFormat:@"KeyPoint { pt: %@, size: %f, angle: %f, response: %f, octave: %d, classId: %d}", self.pt.description, self.size, self.angle, self.response, self.octave, self.classId];
  79. }
  80. @end