DMatch.mm 2.5 KB

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