Point3i.mm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Point3i.mm
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Point3i.h"
  7. #import "Point2i.h"
  8. #import "CVObjcUtil.h"
  9. @implementation Point3i {
  10. cv::Point3i native;
  11. }
  12. - (int)x {
  13. return native.x;
  14. }
  15. - (void)setX:(int)val {
  16. native.x = val;
  17. }
  18. - (int)y {
  19. return native.y;
  20. }
  21. - (void)setY:(int)val {
  22. native.y = val;
  23. }
  24. - (int)z {
  25. return native.z;
  26. }
  27. - (void)setZ:(int)val {
  28. native.z = val;
  29. }
  30. - (cv::Point3i&)nativeRef {
  31. return native;
  32. }
  33. - (instancetype)init {
  34. return [self initWithX:0 y:0 z:0];
  35. }
  36. - (instancetype)initWithX:(int)x y:(int)y z:(int)z {
  37. self = [super init];
  38. if (self) {
  39. self.x = x;
  40. self.y = y;
  41. self.z = z;
  42. }
  43. return self;
  44. }
  45. - (instancetype)initWithPoint:(Point2i*)point {
  46. return [self initWithX:point.x y:point.y z:0];
  47. }
  48. - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  49. self = [super init];
  50. if (self) {
  51. [self set:vals];
  52. }
  53. return self;
  54. }
  55. + (instancetype)fromNative:(cv::Point3i&)point {
  56. return [[Point3i alloc] initWithX:point.x y:point.y z:point.z];
  57. }
  58. - (void)update:(cv::Point3i&)point {
  59. self.x = point.x;
  60. self.y = point.y;
  61. self.z = point.z;
  62. }
  63. - (void)set:(NSArray<NSNumber*>*)vals {
  64. self.x = (vals != nil && vals.count > 0) ? vals[0].intValue : 0;
  65. self.y = (vals != nil && vals.count > 1) ? vals[1].intValue : 0;
  66. self.z = (vals != nil && vals.count > 2) ? vals[2].intValue : 0;
  67. }
  68. - (Point3i*) clone {
  69. return [[Point3i alloc] initWithX:self.x y:self.y z:self.z];
  70. }
  71. - (double)dot:(Point3i*)point {
  72. return self.x * point.x + self.y * point.y + self.z * point.z;
  73. }
  74. - (Point3i*)cross:(Point3i*)point {
  75. return [[Point3i alloc] initWithX:(self.y * point.z - self.z * point.y) y:(self.z * point.x - self.x * point.z) z:(self.x * point.y - self.y * point.x)];
  76. }
  77. - (BOOL)isEqual:(id)other {
  78. if (other == self) {
  79. return YES;
  80. } else if (![other isKindOfClass:[Point3i class]]) {
  81. return NO;
  82. } else {
  83. Point3i* point = (Point3i*)other;
  84. return self.x == point.x && self.y == point.y && self.z == point.z;
  85. }
  86. }
  87. - (NSUInteger)hash {
  88. int prime = 31;
  89. uint32_t result = 1;
  90. result = prime * result + self.x;
  91. result = prime * result + self.y;
  92. result = prime * result + self.z;
  93. return result;
  94. }
  95. - (NSString *)description {
  96. return [NSString stringWithFormat:@"Point3i {%d,%d,%d}", self.x, self.y, self.z];
  97. }
  98. @end