-
Notifications
You must be signed in to change notification settings - Fork 5
/
ARKit+Utilities.swift
196 lines (164 loc) · 6.58 KB
/
ARKit+Utilities.swift
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// ARKit+Utilities.swift
// ARKitCoreML
//
// Created by Jason Clark on 10/11/18.
// Copyright © 2018 Raizlabs. All rights reserved.
//
import ARKit
extension ARSCNView {
/**
Functionally equivalent to `SCNView`'s `snapshot()`, except only including the raw camera image, not any virtual geometry that may be in the scene.
*/
public func capturedImage() -> UIImage? {
guard let frame = session.currentFrame else { return nil }
return frame.getCapturedImage(inSceneView: self)
}
/**
Returns a cropped and deskewed image of the raw camera image of a given `ARImageAnchor`, not including any virtual geometry that may be in the scene.
*/
public func capturedImage(from anchor: ARImageAnchor) -> UIImage? {
guard
let frame = session.currentFrame,
let node = node(for: anchor),
let pov = pointOfView,
isNode(node, insideFrustumOf: pov),
let snapshot = frame.getCapturedImage(inSceneView: self),
let coreImage = CIImage(image: snapshot),
let featureCorners = projectCorners(of: anchor)
else {
return nil
}
// CoreImage uses cartesian coordinates
func cartesianForPoint(point: CGPoint, extent: CGRect) -> CGPoint {
return CGPoint(x: point.x, y: extent.height - point.y)
}
let deskewed = coreImage.perspectiveCorrected(
topLeft: cartesianForPoint(point: featureCorners.topLeft, extent: coreImage.extent),
topRight: cartesianForPoint(point: featureCorners.topRight, extent: coreImage.extent),
bottomLeft: cartesianForPoint(point: featureCorners.bottomLeft, extent: coreImage.extent),
bottomRight: cartesianForPoint(point: featureCorners.bottomRight, extent: coreImage.extent)
)
return UIImage(ciImage: deskewed)
}
}
extension ARFrame {
/**
Gives the camera data in the given frame after scaling and cropping it
in the same way Apple does it for constructing the backing image you
can retrieve via `sceneView.snapshot()`.
*/
func getCapturedImage(inSceneView sceneView: ARSCNView) -> UIImage? {
let rawImage = getOrientationCorrectedCameraImage(forOrientation: UIApplication.shared.statusBarOrientation)
let viewportSize = sceneView.frame.size
switch UIApplication.shared.statusBarOrientation {
case .portrait, .portraitUpsideDown:
guard let resized = rawImage?.resize(toHeight: viewportSize.height) else {
return nil
}
return resized.crop(rect: CGRect(
x: (resized.size.width - viewportSize.width) / 2,
y: 0,
width: viewportSize.width,
height: viewportSize.height)
)
case .landscapeLeft, .landscapeRight:
guard let resized = rawImage?.resize(toWidth: viewportSize.width) else {
return nil
}
return resized.crop(rect: CGRect(
x: 0,
y: (resized.size.height - viewportSize.height) / 2,
width: viewportSize.width,
height: viewportSize.height)
)
case .unknown:
return nil
}
}
}
private extension ARSCNView {
private func projectCorners(of imageAnchor: ARImageAnchor) -> (topRight: CGPoint, bottomRight: CGPoint, bottomLeft: CGPoint, topLeft: CGPoint)? {
guard
let camera = session.currentFrame?.camera,
let corners = CornerTrackingNode.tracking(anchor: imageAnchor, inScene: self)
else { return nil }
defer {
corners.removeFromParentNode()
}
let pointsWorldSpace = [
corners.topRight.simdWorldPosition,
corners.bottomRight.simdWorldPosition,
corners.bottomLeft.simdWorldPosition,
corners.topLeft.simdWorldPosition,
]
let pointsImageSpace: [CGPoint] = pointsWorldSpace.map {
var point = camera.projectPoint($0,
orientation: UIApplication.shared.statusBarOrientation,
viewportSize: bounds.size)
point.x *= UIScreen.main.scale
point.y *= UIScreen.main.scale
return point
}
return (
topRight: pointsImageSpace[0],
bottomRight: pointsImageSpace[1],
bottomLeft: pointsImageSpace[2],
topLeft: pointsImageSpace[3]
)
}
}
private extension ARFrame {
/**
Rotates the image from the camera to match the orientation of the device.
*/
private func getOrientationCorrectedCameraImage(forOrientation orientation: UIInterfaceOrientation) -> UIImage? {
var rotationRadians: Float = 0
switch orientation {
case .portrait:
rotationRadians = .pi / 2
case .portraitUpsideDown:
rotationRadians = -.pi / 2
case .landscapeLeft:
rotationRadians = .pi
case .landscapeRight:
break
case .unknown:
return nil
}
return UIImage(pixelBuffer: capturedImage)?.rotate(radians: rotationRadians)
}
}
private class CornerTrackingNode: SCNNode {
let topLeft = SCNNode()
let topRight = SCNNode()
let bottomLeft = SCNNode()
let bottomRight = SCNNode()
init(anchor: ARImageAnchor) {
super.init()
let physicalSize = anchor.referenceImage.physicalSize
let halfWidth = Float(physicalSize.width / 2)
let halfHeight = Float(physicalSize.height / 2)
addChildNode(topLeft)
topLeft.position = position
topLeft.localTranslate(by: SCNVector3(-halfWidth, 0, halfHeight))
addChildNode(topRight)
topRight.position = position
topRight.localTranslate(by: SCNVector3(halfWidth, 0, halfHeight))
addChildNode(bottomLeft)
bottomLeft.position = position
bottomLeft.localTranslate(by: SCNVector3(-halfWidth, 0, -halfHeight))
addChildNode(bottomRight)
bottomRight.position = position
bottomRight.localTranslate(by: SCNVector3(halfWidth, 0, -halfHeight))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static func tracking(anchor: ARImageAnchor, inScene scene: ARSCNView) -> CornerTrackingNode? {
guard let node = scene.node(for: anchor) else { return nil }
let tracker = CornerTrackingNode(anchor: anchor)
node.addChildNode(tracker)
return tracker
}
}