Point3f.mm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Point3f.mm
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Point3f.h"
  7. #import "Point2f.h"
  8. @implementation Point3f {
  9. cv::Point3f native;
  10. }
  11. - (float)x {
  12. return native.x;
  13. }
  14. - (void)setX:(float)val {
  15. native.x = val;
  16. }
  17. - (float)y {
  18. return native.y;
  19. }
  20. - (void)setY:(float)val {
  21. native.y = val;
  22. }
  23. - (float)z {
  24. return native.z;
  25. }
  26. - (void)setZ:(float)val {
  27. native.z = val;
  28. }
  29. - (cv::Point3f&)nativeRef {
  30. return native;
  31. }
  32. - (instancetype)init {
  33. return [self initWithX:0 y:0 z:0];
  34. }
  35. - (instancetype)initWithX:(float)x y:(float)y z:(float)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:(Point2f*)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::Point3f&)point {
  55. return [[Point3f alloc] initWithX:point.x y:point.y z:point.z];
  56. }
  57. - (void)update:(cv::Point3f&)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].floatValue : 0.0;
  64. self.y = (vals != nil && vals.count > 1) ? vals[1].floatValue : 0.0;
  65. self.z = (vals != nil && vals.count > 2) ? vals[2].floatValue : 0.0;
  66. }
  67. - (Point3f*) clone {
  68. return [[Point3f alloc] initWithX:self.x y:self.y z:self.z];
  69. }
  70. - (double)dot:(Point3f*)point {
  71. return self.x * point.x + self.y * point.y + self.z * point.z;
  72. }
  73. - (Point3f*)cross:(Point3f*)point {
  74. return [[Point3f 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:[Point3f class]]) {
  80. return NO;
  81. } else {
  82. Point3f* point = (Point3f*)other;
  83. return self.x == point.x && self.y == point.y && self.z == point.z;
  84. }
  85. }
  86. #define FLOAT_TO_BITS(x) ((Cv32suf){ .f = x }).i
  87. - (NSUInteger)hash {
  88. int prime = 31;
  89. uint32_t result = 1;
  90. result = prime * result + FLOAT_TO_BITS(self.x);
  91. result = prime * result + FLOAT_TO_BITS(self.y);
  92. result = prime * result + FLOAT_TO_BITS(self.z);
  93. return result;
  94. }
  95. - (NSString *)description {
  96. return [NSString stringWithFormat:@"Point3f {%f,%f,%f}", self.x, self.y, self.z];
  97. }
  98. @end