123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // Point3f.mm
- //
- // Created by Giles Payne on 2019/10/09.
- //
- #import "Point3f.h"
- #import "Point2f.h"
- @implementation Point3f {
- cv::Point3f native;
- }
- - (float)x {
- return native.x;
- }
- - (void)setX:(float)val {
- native.x = val;
- }
- - (float)y {
- return native.y;
- }
- - (void)setY:(float)val {
- native.y = val;
- }
- - (float)z {
- return native.z;
- }
- - (void)setZ:(float)val {
- native.z = val;
- }
- - (cv::Point3f&)nativeRef {
- return native;
- }
- - (instancetype)init {
- return [self initWithX:0 y:0 z:0];
- }
- - (instancetype)initWithX:(float)x y:(float)y z:(float)z {
- self = [super init];
- if (self) {
- self.x = x;
- self.y = y;
- self.z = z;
- }
- return self;
- }
- - (instancetype)initWithPoint:(Point2f*)point {
- return [self initWithX:point.x y:point.y z:0];
- }
- - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
- self = [super init];
- if (self) {
- [self set:vals];
- }
- return self;
- }
- + (instancetype)fromNative:(cv::Point3f&)point {
- return [[Point3f alloc] initWithX:point.x y:point.y z:point.z];
- }
- - (void)update:(cv::Point3f&)point {
- self.x = point.x;
- self.y = point.y;
- self.z = point.z;
- }
- - (void)set:(NSArray<NSNumber*>*)vals {
- self.x = (vals != nil && vals.count > 0) ? vals[0].floatValue : 0.0;
- self.y = (vals != nil && vals.count > 1) ? vals[1].floatValue : 0.0;
- self.z = (vals != nil && vals.count > 2) ? vals[2].floatValue : 0.0;
- }
- - (Point3f*) clone {
- return [[Point3f alloc] initWithX:self.x y:self.y z:self.z];
- }
- - (double)dot:(Point3f*)point {
- return self.x * point.x + self.y * point.y + self.z * point.z;
- }
- - (Point3f*)cross:(Point3f*)point {
- 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)];
- }
- - (BOOL)isEqual:(id)other {
- if (other == self) {
- return YES;
- } else if (![other isKindOfClass:[Point3f class]]) {
- return NO;
- } else {
- Point3f* point = (Point3f*)other;
- return self.x == point.x && self.y == point.y && self.z == point.z;
- }
- }
- #define FLOAT_TO_BITS(x) ((Cv32suf){ .f = x }).i
- - (NSUInteger)hash {
- int prime = 31;
- uint32_t result = 1;
- result = prime * result + FLOAT_TO_BITS(self.x);
- result = prime * result + FLOAT_TO_BITS(self.y);
- result = prime * result + FLOAT_TO_BITS(self.z);
- return result;
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"Point3f {%f,%f,%f}", self.x, self.y, self.z];
- }
- @end
|