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