Simulate graph states in the browser
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

graph.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var graph = {};
  2. graph.colors = ["red", "green"];
  3. graph.prepare = function() {
  4. materials.prepare();
  5. websocket.connect(graph.update);
  6. };
  7. graph.center = function() {
  8. var middle = new THREE.Vector3(0, 0, 0);
  9. for (var i in abj.node) {
  10. middle = middle.add(abj.node[i].position);
  11. }
  12. middle = middle.multiplyScalar(1.0/abj.order());
  13. return middle;
  14. };
  15. graph.update = function(newState) {
  16. if (newState){abj.update(newState);}
  17. var gs = gui.scene.getObjectByName("graphstate");
  18. if (gs){ gui.scene.remove(gs); }
  19. var geometry = new THREE.Geometry();
  20. geometry.colors = [];
  21. for (var i in abj.node) {
  22. var color = graph.colors[abj.node[i].vop % graph.colors.length];
  23. if (abj.node[i].color !== undefined){
  24. color = abj.node[i].color;
  25. }
  26. geometry.vertices.push(abj.node[i].position);
  27. geometry.colors.push(new THREE.Color(color));
  28. }
  29. graph.center();
  30. gui.controls.target.copy(graph.center());
  31. var edges = new THREE.Object3D();
  32. var my_edges = abj.edgelist();
  33. for (i = 0; i < my_edges.length; ++i) {
  34. var edge = my_edges[i];
  35. var start = abj.node[edge[0]].position;
  36. var startpos = new THREE.Vector3(start.x, start.y, start.z);
  37. var end = abj.node[edge[1]].position;
  38. var endpos = new THREE.Vector3(end.x, end.y, end.z);
  39. var newEdge = materials.makeCurve(startpos, endpos);
  40. edges.add(newEdge);
  41. }
  42. if (editor.selection) {
  43. console.log(editor.selection);
  44. var node = editor.selection;
  45. if (Object.prototype.hasOwnProperty.call(abj.node, node)) {
  46. editor.grid.position.copy(abj.node[node].position);
  47. gui.controls.target.copy(abj.node[node].position);
  48. node_name.innerHTML = "Node " + node;
  49. node_data.className = "visible";
  50. node_vop.innerHTML = "VOP: " + abj.node[node].vop;
  51. } else {
  52. editor.selection = undefined;
  53. node_data.className = "hidden";
  54. }
  55. } else {
  56. node_data.className = "hidden";
  57. }
  58. var particles = new THREE.Points(geometry, materials.qubit);
  59. var object = new THREE.Object3D();
  60. object.name = "graphstate";
  61. object.add(particles);
  62. object.add(edges);
  63. gui.scene.add(object);
  64. gui.render();
  65. };