TermCriteria.mm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // TermCriteria.m
  3. //
  4. // Created by Giles Payne on 2019/12/25.
  5. //
  6. #import "TermCriteria.h"
  7. @implementation TermCriteria {
  8. cv::TermCriteria native;
  9. }
  10. + (int)COUNT {
  11. return 1;
  12. }
  13. + (int)EPS {
  14. return 2;
  15. }
  16. + (int)MAX_ITER {
  17. return 1;
  18. }
  19. - (int)type {
  20. return native.type;
  21. }
  22. - (void)setType:(int)val {
  23. native.type = val;
  24. }
  25. - (int)maxCount {
  26. return native.maxCount;
  27. }
  28. - (void)setMaxCount:(int)val {
  29. native.maxCount = val;
  30. }
  31. - (double)epsilon {
  32. return native.epsilon;
  33. }
  34. - (void)setEpsilon:(double)val {
  35. native.epsilon = val;
  36. }
  37. #ifdef __cplusplus
  38. - (cv::TermCriteria&)nativeRef {
  39. return native;
  40. }
  41. #endif
  42. - (instancetype)init {
  43. return [self initWithType:0 maxCount:0 epsilon:0.0];
  44. }
  45. - (instancetype)initWithType:(int)type maxCount:(int)maxCount epsilon:(double)epsilon {
  46. self = [super init];
  47. if (self) {
  48. self.type = type;
  49. self.maxCount = maxCount;
  50. self.epsilon = epsilon;
  51. }
  52. return self;
  53. }
  54. - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  55. self = [super init];
  56. if (self) {
  57. [self set:vals];
  58. }
  59. return self;
  60. }
  61. #ifdef __cplusplus
  62. + (instancetype)fromNative:(cv::TermCriteria&)nativeTermCriteria {
  63. return [[TermCriteria alloc] initWithType:nativeTermCriteria.type maxCount:nativeTermCriteria.maxCount epsilon:nativeTermCriteria.epsilon];
  64. }
  65. #endif
  66. - (void)set:(NSArray<NSNumber*>*)vals {
  67. self.type = (vals != nil && vals.count > 0) ? vals[0].intValue : 0;
  68. self.maxCount = (vals != nil && vals.count > 1) ? vals[1].intValue : 0;
  69. self.epsilon = (vals != nil && vals.count > 2) ? vals[2].doubleValue : 0.0;
  70. }
  71. - (TermCriteria*)clone {
  72. return [[TermCriteria alloc] initWithType:self.type maxCount:self.maxCount epsilon:self.epsilon];
  73. }
  74. - (BOOL)isEqual:(id)other {
  75. if (other == self) {
  76. return YES;
  77. } else if (![other isKindOfClass:[TermCriteria class]]) {
  78. return NO;
  79. } else {
  80. TermCriteria* it = (TermCriteria*)other;
  81. return self.type == it.type && self.maxCount == it.maxCount && self.epsilon == it.epsilon;
  82. }
  83. }
  84. #define DOUBLE_TO_BITS(x) ((Cv64suf){ .f = x }).i
  85. - (NSUInteger)hash {
  86. int prime = 31;
  87. uint32_t result = 1;
  88. result = prime * result + self.type;
  89. result = prime * result + self.maxCount;
  90. int64_t temp = DOUBLE_TO_BITS(self.epsilon);
  91. result = prime * result + (int32_t) (temp ^ (temp >> 32));
  92. return result;
  93. }
  94. - (NSString *)description {
  95. return [NSString stringWithFormat:@"TermCriteria { type: %d, maxCount: %d, epsilon: %lf}", self.type, self.maxCount, self.epsilon];
  96. }
  97. @end