-
Notifications
You must be signed in to change notification settings - Fork 89
/
CTPanoramaView.swift
577 lines (436 loc) · 19.6 KB
/
CTPanoramaView.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//
// CTPanoramaView
// CTPanoramaView
//
// Created by Cihan Tek on 11/10/16.
// Copyright © 2016 Home. All rights reserved.
//
import UIKit
import SceneKit
import CoreMotion
import ImageIO
@objc public protocol CTPanoramaCompass {
func updateUI(rotationAngle: CGFloat, fieldOfViewAngle: CGFloat)
}
@objc public enum CTPanoramaControlMethod: Int {
case motion
case touch
case both
}
@objc public enum CTPanoramaType: Int {
case cylindrical
case spherical
}
@objc public class CTPanoramaView: UIView, UIGestureRecognizerDelegate {
// MARK: Public properties
@objc public var compass: CTPanoramaCompass?
@objc public var movementHandler: ((_ rotationAngle: CGFloat, _ fieldOfViewAngle: CGFloat) -> Void)?
@objc public var panSpeed = CGPoint(x: 0.4, y: 0.4)
@objc public var startAngle: Float = 0
@objc public var angleOffset: Float = 0 {
didSet {
geometryNode?.rotation = SCNQuaternion(0, 1, 0, angleOffset)
}
}
@objc public var minFoV: CGFloat = 40
@objc public var maxFoV: CGFloat = 100
@objc public var image: UIImage? {
didSet {
panoramaType = panoramaTypeForCurrentImage
}
}
@objc public var overlayView: UIView? {
didSet {
replace(overlayView: oldValue, with: overlayView)
}
}
@objc public var panoramaType: CTPanoramaType = .cylindrical {
didSet {
createGeometryNode()
resetCameraAngles();
}
}
@objc public var controlMethod: CTPanoramaControlMethod = .touch {
didSet {
switchControlMethod(to: controlMethod)
resetCameraAngles();
}
}
// MARK: Overriding
public override var backgroundColor: UIColor? {
didSet {
sceneView.backgroundColor = backgroundColor
}
}
// MARK: Private properties
private let MaxPanGestureRotation: Float = GLKMathDegreesToRadians(360)
private let radius: CGFloat = 10
private let sceneView = SCNView()
private let scene = SCNScene()
private let motionManager = CMMotionManager()
private var geometryNode: SCNNode?
private var prevLocation = CGPoint.zero
private var prevRotation = CGFloat.zero
private var prevBounds = CGRect.zero
// Parameters used by the .both method
private var totalX = Float.zero
private var totalY = Float.zero
private var motionPaused = false
private lazy var cameraNode: SCNNode = {
let node = SCNNode()
let camera = SCNCamera()
node.camera = camera
return node
}()
private lazy var opQueue: OperationQueue = {
let queue = OperationQueue()
queue.qualityOfService = .userInteractive
return queue
}()
private lazy var fovHeight: CGFloat = {
return tan(self.yFov/2 * .pi / 180.0) * 2 * self.radius
}()
private var startScale: CGFloat = 0.0
private var xFov: CGFloat {
return yFov * self.bounds.width / self.bounds.height
}
private var yFov: CGFloat {
get {
return cameraNode.camera?.fieldOfView ?? 0
}
set {
cameraNode.camera?.fieldOfView = newValue
}
}
private var panoramaTypeForCurrentImage: CTPanoramaType {
if let image = image {
if image.size.width / image.size.height == 2 {
return .spherical
}
}
return .cylindrical
}
// MARK: Class lifecycle methods
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
public convenience init(frame: CGRect, image: UIImage) {
self.init(frame: frame)
// Force Swift to call the property observer by calling the setter from a non-init context
({ self.image = image })()
}
deinit {
if motionManager.isDeviceMotionActive {
motionManager.stopDeviceMotionUpdates()
}
}
private func commonInit() {
add(view: sceneView)
scene.rootNode.addChildNode(cameraNode)
sceneView.scene = scene
sceneView.backgroundColor = self.backgroundColor
switchControlMethod(to: controlMethod)
}
// MARK: Public methods
public func resetCameraAngles() {
yFov = maxFoV
cameraNode.eulerAngles = SCNVector3Make(0, startAngle, 0)
totalX = Float.zero
totalY = Float.zero
self.reportMovement(CGFloat(startAngle), xFov.toRadians(), callHandler: false)
}
// MARK: Configuration helper methods
private func createGeometryNode() {
guard let image = image else {return}
geometryNode?.removeFromParentNode()
let material = SCNMaterial()
material.diffuse.contents = image
material.diffuse.mipFilter = .nearest
material.diffuse.magnificationFilter = .nearest
material.diffuse.contentsTransform = SCNMatrix4MakeScale(-1, 1, 1)
material.diffuse.wrapS = .repeat
material.cullMode = .front
if panoramaType == .spherical {
let sphere = SCNSphere(radius: radius)
sphere.segmentCount = 300
sphere.firstMaterial = material
let sphereNode = SCNNode()
sphereNode.geometry = sphere
geometryNode = sphereNode
} else {
let tube = SCNTube(innerRadius: radius, outerRadius: radius, height: fovHeight)
tube.heightSegmentCount = 50
tube.radialSegmentCount = 300
tube.firstMaterial = material
let tubeNode = SCNNode()
tubeNode.geometry = tube
geometryNode = tubeNode
}
geometryNode?.rotation = SCNQuaternion(0, 1, 0, angleOffset)
scene.rootNode.addChildNode(geometryNode!)
}
private func replace(overlayView: UIView?, with newOverlayView: UIView?) {
overlayView?.removeFromSuperview()
guard let newOverlayView = newOverlayView else {return}
add(view: newOverlayView)
}
private func startMotionUpdates(){
guard motionManager.isDeviceMotionAvailable else {return}
motionManager.deviceMotionUpdateInterval = 0.015
motionPaused = false
motionManager.startDeviceMotionUpdates(using: .xArbitraryZVertical, to: opQueue,
withHandler: { [weak self] (motionData, error) in
guard let panoramaView = self else {return}
guard !panoramaView.motionPaused else {return}
guard (panoramaView.controlMethod == .motion || panoramaView.controlMethod == .both) else {return}
guard let motionData = motionData else {
print("\(String(describing: error?.localizedDescription))")
panoramaView.motionManager.stopDeviceMotionUpdates()
return
}
DispatchQueue.main.async {
if panoramaView.panoramaType == .cylindrical {
let rotationMatrix = motionData.attitude.rotationMatrix
var userHeading = .pi - atan2(rotationMatrix.m32, rotationMatrix.m31)
userHeading += .pi/2
var startAngle = panoramaView.startAngle
if (panoramaView.controlMethod == .both) {
startAngle += panoramaView.totalY
}
// Prevent vertical movement in a cylindrical panorama
panoramaView.cameraNode.eulerAngles = SCNVector3Make(0, startAngle + Float(-userHeading), 0)
} else {
// Use quaternions when in spherical mode to prevent gimbal lock
var orientation = motionData.orientation()
// Represent the orientation as a GLKQuaternion
if(panoramaView.controlMethod == .both){
// same code as pan rotation
// but with our total accumulated
// movements
var glQuaternion = GLKQuaternionMake(orientation.x, orientation.y, orientation.z, orientation.w)
let xMultiplier = GLKQuaternionMakeWithAngleAndAxis(panoramaView.totalX, 1, 0, 0)
glQuaternion = GLKQuaternionMultiply(glQuaternion, xMultiplier)
let yMultiplier = GLKQuaternionMakeWithAngleAndAxis(panoramaView.totalY, 0, 1, 0)
glQuaternion = GLKQuaternionMultiply(yMultiplier, glQuaternion)
orientation = SCNQuaternion(x: glQuaternion.x, y: glQuaternion.y, z: glQuaternion.z, w: glQuaternion.w)
}
panoramaView.cameraNode.orientation = orientation
}
panoramaView.reportMovement(CGFloat(-panoramaView.cameraNode.eulerAngles.y), panoramaView.xFov.toRadians())
}
})
}
private func switchControlMethod(to method: CTPanoramaControlMethod) {
sceneView.gestureRecognizers?.removeAll()
if method == .touch {
let panGestureRec = UIPanGestureRecognizer(target: self, action: #selector(handlePan(panRec:)))
sceneView.addGestureRecognizer(panGestureRec)
let pinchRec = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(pinchRec:)))
sceneView.addGestureRecognizer(pinchRec)
let rotateRec = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(rotRec:)))
sceneView.addGestureRecognizer(rotateRec)
pinchRec.delegate = self
rotateRec.delegate = self
if motionManager.isDeviceMotionActive {
motionManager.stopDeviceMotionUpdates()
}
}
else {
if method == .both {
let panGestureRec = UIPanGestureRecognizer(target: self, action: #selector(handlePan(panRec:)))
sceneView.addGestureRecognizer(panGestureRec)
let pinchRec = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(pinchRec:)))
sceneView.addGestureRecognizer(pinchRec)
let rotateRec = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(rotRec:)))
sceneView.addGestureRecognizer(rotateRec)
pinchRec.delegate = self
rotateRec.delegate = self
}
startMotionUpdates()
}
}
private func reportMovement(_ rotationAngle: CGFloat, _ fieldOfViewAngle: CGFloat, callHandler: Bool = true) {
compass?.updateUI(rotationAngle: rotationAngle, fieldOfViewAngle: fieldOfViewAngle)
if callHandler {
movementHandler?(rotationAngle, fieldOfViewAngle)
}
}
// MARK: Gesture handling
@objc private func handlePan(panRec: UIPanGestureRecognizer) {
if panRec.state == .began {
prevLocation = CGPoint.zero
} else if panRec.state == .changed {
var modifiedPanSpeed = panSpeed
if (panoramaType == .cylindrical) {
modifiedPanSpeed.y = 0 // Prevent vertical movement in a cylindrical panorama
}
let orientation = cameraNode.orientation
let location = panRec.translation(in: sceneView)
let translationDelta = CGPoint(
x: (location.x - prevLocation.x) * modifiedPanSpeed.x,
y: (location.y - prevLocation.y) * modifiedPanSpeed.y
)
// Accumulate these if wheb using .both method so that we can apply the rotations
// to the sensor data and smoothly move with both touch and motion controls at the same time
// If both, just accumulate, our sensor callback will handle it
if (controlMethod == .both) {
// Use the pan translation along the x axis to adjust the camera's rotation about the y axis (side to side navigation).
let yScalar = Float(translationDelta.x / self.bounds.size.width)
let yRadians = yScalar * MaxPanGestureRotation
let xScalar = Float(translationDelta.y / self.bounds.size.height)
let xRadians = xScalar * MaxPanGestureRotation
totalX += xRadians
totalY += yRadians
} else { // Otherwise, do the math here since we have no sensor
// Use the pan translation along the x axis to adjust the camera's rotation about the y axis (side to side navigation).
let yScalar = Float(translationDelta.x / self.bounds.size.width)
let yRadians = yScalar * MaxPanGestureRotation
// Use the pan translation along the y axis to adjust the camera's rotation about the x axis (up and down navigation).
let xScalar = Float(translationDelta.y / self.bounds.size.height)
let xRadians = xScalar * MaxPanGestureRotation
// Represent the orientation as a GLKQuaternion
var glQuaternion = GLKQuaternionMake(orientation.x, orientation.y, orientation.z, orientation.w)
// Perform up and down rotations around *CAMERA* X axis (note the order of multiplication)
let xMultiplier = GLKQuaternionMakeWithAngleAndAxis(xRadians, 1, 0, 0)
glQuaternion = GLKQuaternionMultiply(glQuaternion, xMultiplier)
// Perform side to side rotations around *WORLD* Y axis (note the order of multiplication, different from above)
let yMultiplier = GLKQuaternionMakeWithAngleAndAxis(yRadians, 0, 1, 0)
glQuaternion = GLKQuaternionMultiply(yMultiplier, glQuaternion)
cameraNode.orientation = SCNQuaternion(x: glQuaternion.x, y: glQuaternion.y, z: glQuaternion.z, w: glQuaternion.w)
}
prevLocation = location
reportMovement(CGFloat(-cameraNode.eulerAngles.y), xFov.toRadians())
}
}
@objc func handlePinch(pinchRec: UIPinchGestureRecognizer) {
if pinchRec.numberOfTouches != 2 {
return
}
let zoom = CGFloat(pinchRec.scale)
switch pinchRec.state {
case .began:
startScale = cameraNode.camera!.fieldOfView
case .changed:
let fov = startScale / zoom
if fov > minFoV && fov <= maxFoV {
cameraNode.camera!.fieldOfView = fov
}
default:
break
}
}
@objc func handleRotate(rotRec: UIRotationGestureRecognizer) {
// no rotation for cylindrical
if panoramaType == .cylindrical{
return
}
if rotRec.state == .began {
prevRotation = CGFloat.zero
if (controlMethod == .both) {
motionPaused = true
}
} else if rotRec.state == .changed {
let orientation = cameraNode.orientation
let rotation = rotRec.rotation
let zRadians = rotation - prevRotation
// use a Quaternion instead of eluer angles
// so we can switch from sensor to finger rotation
// smoothly
var glQuaternion = GLKQuaternionMake(orientation.x, orientation.y, orientation.z, orientation.w)
let zMultiplier = GLKQuaternionMakeWithAngleAndAxis(Float(zRadians), 0, 0, 1)
glQuaternion = GLKQuaternionMultiply(glQuaternion, zMultiplier)
cameraNode.orientation = SCNQuaternion(x: glQuaternion.x, y: glQuaternion.y, z: glQuaternion.z, w: glQuaternion.w)
prevRotation = rotation
}
else {
motionPaused = false
}
}
public override func layoutSubviews() {
super.layoutSubviews()
if bounds.size.width != prevBounds.size.width || bounds.size.height != prevBounds.size.height {
sceneView.setNeedsDisplay()
reportMovement(CGFloat(-cameraNode.eulerAngles.y), xFov.toRadians(), callHandler: false)
}
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// do not mix pan gestures with the others
if(gestureRecognizer is UIPanGestureRecognizer) || (otherGestureRecognizer is UIPanGestureRecognizer){
return false;
}
return true
}
}
private extension CMDeviceMotion {
func orientation() -> SCNVector4 {
let attitude = self.attitude.quaternion
let attitudeQuanternion = GLKQuaternion(quanternion: attitude)
let result: SCNVector4
switch UIApplication.shared.statusBarOrientation {
case .landscapeRight:
let cq1 = GLKQuaternionMakeWithAngleAndAxis(.pi/2, 0, 1, 0)
let cq2 = GLKQuaternionMakeWithAngleAndAxis(-(.pi/2), 1, 0, 0)
var quanternionMultiplier = GLKQuaternionMultiply(cq1, attitudeQuanternion)
quanternionMultiplier = GLKQuaternionMultiply(cq2, quanternionMultiplier)
result = quanternionMultiplier.vector(for: .landscapeRight)
case .landscapeLeft:
let cq1 = GLKQuaternionMakeWithAngleAndAxis(-(.pi/2), 0, 1, 0)
let cq2 = GLKQuaternionMakeWithAngleAndAxis(-(.pi/2), 1, 0, 0)
var quanternionMultiplier = GLKQuaternionMultiply(cq1, attitudeQuanternion)
quanternionMultiplier = GLKQuaternionMultiply(cq2, quanternionMultiplier)
result = quanternionMultiplier.vector(for: .landscapeLeft)
case .portraitUpsideDown:
let cq1 = GLKQuaternionMakeWithAngleAndAxis(-(.pi/2), 1, 0, 0)
let cq2 = GLKQuaternionMakeWithAngleAndAxis(.pi, 0, 0, 1)
var quanternionMultiplier = GLKQuaternionMultiply(cq1, attitudeQuanternion)
quanternionMultiplier = GLKQuaternionMultiply(cq2, quanternionMultiplier)
result = quanternionMultiplier.vector(for: .portraitUpsideDown)
default:
let clockwiseQuanternion = GLKQuaternionMakeWithAngleAndAxis(-(.pi/2), 1, 0, 0)
let quanternionMultiplier = GLKQuaternionMultiply(clockwiseQuanternion, attitudeQuanternion)
result = quanternionMultiplier.vector(for: .portrait)
}
return result
}
}
private extension UIView {
func add(view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
let views = ["view": view]
let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "|[view]|", options: [], metrics: nil, views: views)
let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: [], metrics: nil, views: views)
self.addConstraints(hConstraints)
self.addConstraints(vConstraints)
}
}
private extension FloatingPoint {
func toDegrees() -> Self {
return self * 180 / .pi
}
func toRadians() -> Self {
return self * .pi / 180
}
}
private extension GLKQuaternion {
init(quanternion: CMQuaternion) {
self.init(q: (Float(quanternion.x), Float(quanternion.y), Float(quanternion.z), Float(quanternion.w)))
}
func vector(for orientation: UIInterfaceOrientation) -> SCNVector4 {
switch orientation {
case .landscapeRight:
return SCNVector4(x: -self.y, y: self.x, z: self.z, w: self.w)
case .landscapeLeft:
return SCNVector4(x: self.y, y: -self.x, z: self.z, w: self.w)
case .portraitUpsideDown:
return SCNVector4(x: -self.x, y: -self.y, z: self.z, w: self.w)
default:
return SCNVector4(x: self.x, y: self.y, z: self.z, w: self.w)
}
}
}