Scalar.mm 3.4 KB

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