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