Point3d.mm 2.7 KB

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