-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChipFiring.js
311 lines (265 loc) · 9.66 KB
/
ChipFiring.js
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
const CAMERA_FOV = 75
const CAMERA_FRUSTUM_NEAR = 0.1
const CAMERA_FRUSTUM_FAR = 1000
const CAMERA_INITIAL_DISTANCE = 50
const AXIS_DRAW_DISTANCE = 50
const NODE_RADIUS = 0.25
const NODE_SEGMENTS = 6
const nodeMaterial = new THREE.MeshBasicMaterial( { color: 0x999999 } )
const nodeMaterial_selected = new THREE.MeshBasicMaterial( { color: 0x00cccc } )
const edgeMaterial = new THREE.LineBasicMaterial({ color: 0x009999 })
const getClass = (object) => object.constructor.name
const range = (size) => [...Array(size).keys()]
const getScreenPosition = (renderer, object, camera) => {
var vector = new THREE.Vector3()
var widthHalf = 0.5 * renderer.context.canvas.width;
var heightHalf = 0.5 * renderer.context.canvas.height;
object.updateMatrixWorld()
vector.setFromMatrixPosition(object.matrixWorld)
vector.project(camera)
vector.x = ( vector.x * widthHalf ) + widthHalf
vector.y = - ( vector.y * heightHalf ) + heightHalf
return {
x: vector.x,
y: vector.y
}
}
const calculateNodesOnPlane = (sigma) => {
let nodes = []
for (let alpha = 0; alpha <= sigma; alpha++) {
for (let beta = 0; beta <= (sigma - alpha); beta++) {
nodes = [...nodes, [alpha, beta, sigma - alpha - beta]]
}
}
return nodes
}
const calculateEdges = (node) => {
const isUnstable = (state, index) => state[index] >= 2
const fireIndex = (state, index) => state.map((n, i) => i === index ? n - 2 : n + 1)
return node.map((n, i) => isUnstable(node, i) ? fireIndex(node, i) : undefined).filter(s => s !== undefined)
}
const Store = class {
constructor() {
this.nodes = {}
}
// in order to use a map structure, the search key must be hashable - so we must convert from array -> string whenever we do a lookup
serialize = (node) => JSON.stringify(node)
deserialize = (node) => JSON.parse(node)
hasTraversed = (node) => this.serialize(node) in this.nodes
getEdges = (node) => this.hasTraversed(node) ? this.nodes[this.serialize(node)] : []
addEdges = (node, edges) => this.nodes[this.serialize(node)] = edges
}
const Scene = class {
constructor() {
// create the underlying store
this.store = new Store()
// create the THREE.js scene
this.scene = new THREE.Scene()
// create and mount the THREE.js renderer
this.renderer = new THREE.WebGLRenderer()
this.renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(this.renderer.domElement)
// create the camera
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, CAMERA_FRUSTUM_NEAR, CAMERA_FRUSTUM_FAR)
this.camera.position.set(CAMERA_INITIAL_DISTANCE, CAMERA_INITIAL_DISTANCE, CAMERA_INITIAL_DISTANCE)
this.camera.rotation.set(0, Math.PI / 2, 0) // pointed orthogonal to the planes formed by the constraint alpha + beta + gamma = constant
// setup controls and rerender when controls changed
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement)
this.controls.addEventListener('change', this.handleCameraMove.bind(this)) // we need to bind the render function to this context because JavaScript can be weird with namespaces
// create raycaster for onClick selection
this.raycaster = new THREE.Raycaster()
// create and attach axes
this.axesVisible = true
this.axes = new Axes()
this.axes.addToScene(this.scene)
// create array for holding our currently displayed nodes
this.nodes = []
// create variables for holding our selected node and its immediate edges
this.selectedNode = undefined
this.selectedEdges = []
// attach event listener to raycast on click
this.renderer.domElement.addEventListener('click', (event) => this.handleClick(event))
// render the initial scene
this.render()
}
setAxesVisibility(shouldBeVisible) {
if (shouldBeVisible === this.axesVisible) return
this.axesVisible = shouldBeVisible
if (shouldBeVisible) this.axes.addToScene(this.scene)
else this.axes.removeFromScene(this.scene)
this.render()
}
toggleAxesVisibility() {
this.setAxesVisibility(!this.axesVisible)
}
clearNodes() {
this.deselectNode()
this.nodes.forEach(n => n.removeFromScene(this.scene))
this.nodes = []
}
addNode(node) {
node.addToScene(this.scene)
this.nodes = [...this.nodes, node]
}
drawPlane(sigma) {
this.clearNodes()
const nodesOnPlane = calculateNodesOnPlane(sigma)
for (let index in nodesOnPlane) {
const node = nodesOnPlane[index]
if (!this.store.hasTraversed(node)) {
const edges = calculateEdges(node)
this.store.addEdges(node, edges)
}
this.addNode(new Node(node))
}
this.render()
}
deselectNode() {
if (this.selectedNode === undefined) return
this.deselectEdges()
this.relabelSelectedNodeHUD()
this.selectedNode.setSelected(false)
this.selectedNode = undefined
}
deselectEdges() {
if (this.selectedEdges.length === 0) return
this.selectedEdges.forEach(edge => edge.removeFromScene(this.scene))
this.selectedEdges = []
}
selectNode(node) {
if (this.selectedNode !== undefined) this.deselectNode()
node.setSelected(true)
this.selectedNode = node
// reselect edges when node changed
const edges = this.store.getEdges(node.position)
this.selectedEdges = edges.map(next => new Edge(node.position, next))
this.selectedEdges.forEach(edge => edge.addToScene(this.scene))
// change the label to match the selected node
this.relabelSelectedNodeHUD()
this.render()
}
getSelectedScreenPosition() {
if (this.selectedNode === undefined) return
const screenPosition = getScreenPosition(this.renderer, this.selectedNode.mesh, this.camera)
return screenPosition
}
relabelSelectedNodeHUD() {
const hud = document.getElementById('label-selectedNode')
hud.innerText = JSON.stringify(this.selectedNode?.position)
this.updateSelectedNodeHUD()
}
updateSelectedNodeHUD() {
const hud = document.getElementById('label-selectedNode')
const screenPosition = this.getSelectedScreenPosition()
if (screenPosition === undefined) return
hud.style.left = `${screenPosition.x}px`
hud.style.top = `${screenPosition.y}px`
}
handleCameraMove() {
this.updateSelectedNodeHUD()
this.render()
}
handleClick(event) {
const pointer = new THREE.Vector2()
pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1
pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1
this.raycaster.setFromCamera(pointer, this.camera)
const intersects = this.raycaster.intersectObjects(this.scene.children)
if (intersects.length > 0) {
const object = intersects[0].object
if (object.name) {
const type = getClass(object.name)
if (type === 'Node') {
// we clicked on a node
const node = object.name
this.selectNode(node)
this.render()
}
}
}
}
render() {
this.renderer.render(this.scene, this.camera)
}
}
const Axes = class {
constructor() {
const axisDefinitions = [
{
name: 'alpha',
color: 0x00ff00,
direction: new THREE.Vector3(1, 0, 0),
},
{
name: 'beta',
color: 0xff0000,
direction: new THREE.Vector3(0, 1, 0),
},
{
name: 'gamma',
color: 0x0000ff,
direction: new THREE.Vector3(0, 0, 1),
}
]
this.axes = axisDefinitions.map(definition => {
const points = [new THREE.Vector3(), definition.direction.clone().multiplyScalar(AXIS_DRAW_DISTANCE)]
console.log(points)
const geometry = new THREE.BufferGeometry().setFromPoints(points)
const material = new THREE.LineBasicMaterial({ color: definition.color })
const line = new THREE.Line(geometry, material)
line.name = definition.name
return line
})
}
addToScene(scene) {
this.axes.forEach(a => scene.add(a))
}
removeFromScene(scene) {
this.axes.forEach(a => scene.remove(a))
}
}
const Node = class {
constructor(position) {
const geometry = new THREE.SphereGeometry(NODE_RADIUS, NODE_SEGMENTS, NODE_SEGMENTS)
this.mesh = new THREE.Mesh(geometry, nodeMaterial)
this.mesh.name = this
this.mesh.position.set(position[0], position[1], position[2])
this.position = position
this.selected = false
this.edges = []
}
addToScene(scene) {
scene.add(this.mesh)
}
removeFromScene(scene) {
scene.remove(this.mesh)
}
setSelected(shouldBeSelected) {
if (shouldBeSelected === this.selected) return
this.selected = shouldBeSelected
this.mesh.material = shouldBeSelected ? nodeMaterial_selected : nodeMaterial
}
}
const Edge = class {
constructor(start, end) {
const points = [new THREE.Vector3(start[0], start[1], start[2]), new THREE.Vector3(end[0], end[1], end[2])]
const geometry = new THREE.BufferGeometry().setFromPoints(points)
this.line = new THREE.Line(geometry, edgeMaterial)
this.line.name = this
}
addToScene(scene) {
scene.add(this.line)
}
removeFromScene(scene) {
scene.remove(this.line)
}
}
const scene = new Scene()
// Attach event listeners for DOM elements
document.getElementById('button-generateState').addEventListener('click', () => {
const sigma = document.getElementById('input-constant').value
scene.drawPlane(sigma)
})
document.getElementById('button-toggleAxes').addEventListener('click', () => {
scene.toggleAxesVisibility()
})