-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphViewController.swift
More file actions
178 lines (115 loc) · 5.56 KB
/
GraphViewController.swift
File metadata and controls
178 lines (115 loc) · 5.56 KB
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
//
// GraphViewController.swift
// AR Calculator
//
// Created by Johnson Zhou on 06/07/2018.
// Copyright © 2018 Johnson Zhou. All rights reserved.
//
import UIKit
import ARKit
class GraphViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
let configuration = ARWorldTrackingConfiguration()
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
sceneView.debugOptions = [ARSCNDebugOptions.showWorldOrigin, ARSCNDebugOptions.showWorldOrigin]
}
fileprivate var toEstablishCoordinateSystem: Bool = false
@IBAction func establishSys(_ sender: UIButton) {
toEstablishCoordinateSystem = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Set plane detection mode
configuration.planeDetection = [.horizontal, .vertical]
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
// MARK: - ARSCNViewDelegate
@IBInspectable
var axisLength: Double = 0.5
@IBInspectable
var lineRadius: CGFloat = 0.01
// Override to create and configure nodes for anchors added to the view's session.
var previousNode: SCNNode?
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
guard let pointOfView = sceneView.pointOfView else { return }
if toEstablishCoordinateSystem {
previousNode?.removeFromParentNode()
let position = SCNVector3(pointOfView.simdWorldFront)
print(position)
let parent_node = SCNNode()
//
let cyl = SCNCylinder(radius: lineRadius, height: 1.0)
let cyl_node_x = SCNNode(geometry: cyl)
cyl_node_x.position = position
cyl_node_x.geometry?.materials.first?.diffuse.contents = UIColor.orange
parent_node.addChildNode(cyl_node_x)
let cyl_y = SCNCylinder(radius: lineRadius, height: 1.0)
let cyl_node_y = SCNNode(geometry: cyl_y)
cyl_node_y.position = position
cyl_node_y.rotation = SCNVector4(1, 0, 0, Double.pi / 2)
cyl_node_y.geometry?.materials.first?.diffuse.contents = UIColor.green
parent_node.addChildNode(cyl_node_y)
let cyl_z = SCNCylinder(radius: lineRadius, height: 1.0)
let cyl_node_z = SCNNode(geometry: cyl_z)
cyl_node_z.position = position
cyl_node_z.rotation = SCNVector4(0, 0, 1, Double.pi / 2)
cyl_node_z.geometry?.materials.first?.diffuse.contents = UIColor.red
parent_node.addChildNode(cyl_node_z)
drawFunction(scene: scene, parentNode: parent_node, location: position)
scene.rootNode.addChildNode(parent_node)
previousNode = parent_node
toEstablishCoordinateSystem = false
}
}
fileprivate func drawFunction(scene: SCNScene, parentNode: SCNNode, location: SCNVector3) {
let function = SCNCylinder(radius: lineRadius, height: 1.0)
function.materials.first?.diffuse.contents = UIColor.purple
let funcNode = SCNNode(geometry: function)
guard let offsetOfStartingPoint = startingPoint else {
return
}
funcNode.position = location + offsetOfStartingPoint
funcNode.simdRotate(by: rotation, aroundTarget: funcNode.simdPosition)
parentNode.addChildNode(funcNode)
}
var a, b, c, x, y, z: Double!
var startingPoint: SCNVector3!
var rotation: simd_quatf! // Euler's Angle
func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
let alertvc = UIAlertController(title: "An error has occured!", message: "Please restart the app", preferredStyle: .alert)
present(alertvc, animated: true, completion: nil)
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension SCNVector3 {
static func + (left: SCNVector3, right: SCNVector3) -> SCNVector3 {
return SCNVector3Make(left.x + right.x , left.y + right.y, left.z + right.z)
}
}