Double2.mm 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Double2.mm
  3. //
  4. // Created by Giles Payne on 2020/05/22.
  5. //
  6. #import "Double2.h"
  7. #import "Mat.h"
  8. @implementation Double2 {
  9. cv::Vec2d native;
  10. }
  11. -(double)v0 {
  12. return native[0];
  13. }
  14. -(void)setV0:(double)v {
  15. native[0] = v;
  16. }
  17. -(double)v1 {
  18. return native[1];
  19. }
  20. -(void)setV1:(double)v {
  21. native[1] = v;
  22. }
  23. -(instancetype)init {
  24. return [self initWithV0:0 v1:0];
  25. }
  26. -(instancetype)initWithV0:(double)v0 v1:(double)v1 {
  27. self = [super init];
  28. if (self) {
  29. self.v0 = v0;
  30. self.v1 = v1;
  31. }
  32. return self;
  33. }
  34. -(instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  35. self = [super init];
  36. if (self) {
  37. [self set:vals];
  38. }
  39. return self;
  40. }
  41. +(instancetype)fromNative:(cv::Vec2d&)vec2d {
  42. return [[Double2 alloc] initWithV0:vec2d[0] v1:vec2d[1]];
  43. }
  44. -(void)set:(NSArray<NSNumber*>*)vals {
  45. self.v0 = (vals != nil && vals.count > 0) ? vals[0].doubleValue : 0;
  46. self.v1 = (vals != nil && vals.count > 1) ? vals[1].doubleValue : 0;
  47. }
  48. -(NSArray<NSNumber*>*)get {
  49. return @[[NSNumber numberWithFloat:native[0]], [NSNumber numberWithFloat:native[1]]];
  50. }
  51. - (BOOL)isEqual:(id)other {
  52. if (other == self) {
  53. return YES;
  54. } else if (![other isKindOfClass:[Double2 class]]) {
  55. return NO;
  56. } else {
  57. Double2* d2 = (Double2*)other;
  58. return self.v0 == d2.v0 && self.v1 == d2.v1;
  59. }
  60. }
  61. @end