-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathARWorldMap.mm
170 lines (147 loc) · 4.33 KB
/
ARWorldMap.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Unity Technologies Inc (c) 2018
// ARWorldMap.mm
// Main implementation of ARKit plugin's ARWorldMap
#include "ARKitDefines.h"
#ifdef __cplusplus
extern "C" {
#endif
bool worldMap_GetSupported()
{
return UnityAreFeaturesSupported(kUnityARKitSupportedFeaturesWorldMap);
}
bool worldMap_Save(const void* worldMapPtr, const char* path)
{
if (worldMapPtr == nullptr || path == nullptr || !worldMap_GetSupported())
return false;
if (@available(iOS 12.0, *))
{
ARWorldMap* worldMap = (__bridge ARWorldMap*)worldMapPtr;
NSError* writeError = nil;
NSURL* url = [[NSURL alloc] initFileURLWithPath:[NSString stringWithUTF8String:path] isDirectory:false];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:worldMap];
[data writeToURL:url options:(NSDataWritingAtomic) error:&writeError];
if (writeError)
NSLog(@"%@", writeError);
return (writeError == nil);
}
else
{
// Fallback on earlier versions without ARWorldMap
return false;
}
}
void* worldMap_Load(const char* path)
{
if (!worldMap_GetSupported())
return nullptr;
NSError* error = nil;
NSURL* url = [[NSURL alloc] initFileURLWithPath:[NSString stringWithUTF8String:path] isDirectory:false];
NSData *wmdata = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedAlways error:&error];
if (error)
NSLog(@"%@", error);
if (@available(iOS 12.0, *))
{
ARWorldMap* worldMap = [NSKeyedUnarchiver unarchiveObjectWithData:wmdata];
return (__bridge_retained void*)worldMap;
}
else
{
// Fallback on earlier versions of iOS without ARWorldMap
return nullptr;
}
}
long worldMap_SerializedLength(const void* worldMapPtr)
{
if (@available(iOS 12.0, *))
{
ARWorldMap* worldMap = (__bridge ARWorldMap*)worldMapPtr;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:worldMap];
return data.length;
}
else
{
// Fallback on earlier versions
return 0;
}
}
void worldMap_SerializeToByteArray(const void* worldMapPtr, void* pinnedArray)
{
if (@available(iOS 12.0, *))
{
ARWorldMap* worldMap = (__bridge ARWorldMap*)worldMapPtr;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:worldMap];
memcpy(pinnedArray, data.bytes, data.length);
}
}
void* worldMap_SerializeFromByteArray(const void* pinnedArray, long lengthBytes)
{
if (@available(iOS 12.0, *))
{
NSData *wmdata = [NSData dataWithBytes:pinnedArray length:lengthBytes];
ARWorldMap* worldMap = [NSKeyedUnarchiver unarchiveObjectWithData:wmdata];
return (__bridge_retained void*)worldMap;
}
else
{
// Fallback on earlier versions
return nullptr;
}
}
UnityARVector3 worldMap_GetCenter(const void* worldMapPtr)
{
if (worldMapPtr == nullptr || !worldMap_GetSupported())
return UnityARVector3{0, 0, 0};
if (@available(iOS 12.0, *))
{
ARWorldMap* worldMap = (__bridge ARWorldMap*)worldMapPtr;
return UnityARVector3
{
worldMap.center.x,
worldMap.center.y,
worldMap.center.z
};
}
else
{
// Fallback on earlier versions
return UnityARVector3{0, 0, 0};
}
}
UnityARVector3 worldMap_GetExtent(const void* worldMapPtr)
{
if (worldMapPtr == nullptr || !worldMap_GetSupported())
return UnityARVector3{0, 0, 0};
if (@available(iOS 12.0, *)) {
ARWorldMap* worldMap = (__bridge ARWorldMap*)worldMapPtr;
return UnityARVector3
{
worldMap.extent.x,
worldMap.extent.y,
worldMap.extent.z
};
}
else
{
// Fallback on earlier versions
return UnityARVector3{0, 0, 0};
}
}
void* worldMap_GetPointCloud(const void* worldMapPtr)
{
if (worldMapPtr == nullptr || !worldMap_GetSupported())
return nullptr;
if (@available(iOS 12.0, *))
{
ARWorldMap* worldMap = (__bridge ARWorldMap*)worldMapPtr;
ARPointCloud *pointCloud = [worldMap rawFeaturePoints];
return (__bridge_retained void*)pointCloud;
}
else
{
// Fallback on earlier versions
return nullptr;
}
}
#ifdef __cplusplus
}
#endif