Simulate graph states in the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.3KB

  1. var editor = {};
  2. var pi2 = Math.PI / 2;
  3. editor.selection = undefined;
  4. editor.mouseOver = undefined;
  5. editor.gridTimeOut = 0;
  6. editor.orientations = [
  7. new THREE.Euler(pi2, 0, 0),
  8. new THREE.Euler(0, 0, 0),
  9. new THREE.Euler(pi2, 0, pi2),
  10. ];
  11. editor.checkTimeOut = function() {
  12. editor.gridTimeOut += 1;
  13. console.log(editor.gridTimeOut);
  14. editor.grid.visible = editor.gridTimeOut < 10;
  15. }
  16. editor.onFreeMove = function() {
  17. var found = editor.findNodeOnRay(mouse.ray);
  18. if (editor.mouseOver !== found) {
  19. editor.mouseOver = found;
  20. if (found) {
  21. var n = abj.node[found];
  22. var s = "Node " + found + "<br/> ";
  23. for (var i in n) {
  24. if (i!="position"){
  25. s += i + ":" + n[i] + " ";
  26. }
  27. }
  28. s += "";
  29. gui.nodeMessage(s);
  30. } else {
  31. gui.hideNodeMessage();
  32. }
  33. }
  34. };
  35. editor.focus = function(node) {
  36. gui.hideNodeMessage();
  37. editor.selection = node;
  38. //gui.serverMessage("Selected node " + node + ".");
  39. };
  40. editor.addQubitAtPoint = function(point) {
  41. if (point === null) {
  42. return;
  43. }
  44. point.round();
  45. var new_node = Math.floor(point.x) + "," + Math.floor(point.y) + "," + Math.floor(point.z);
  46. if (Object.prototype.hasOwnProperty.call(abj.node, new_node)) {
  47. gui.serverMessage("Node " + new_node +" already exists.");
  48. return;
  49. }
  50. api.edit({action:"create", name:new_node, position: point});
  51. editor.focus(new_node);
  52. gui.serverMessage("Created node " + new_node +".");
  53. };
  54. editor.onClick = function() {
  55. var found = editor.findNodeOnRay(mouse.ray);
  56. if (found) {
  57. editor.focus(found);
  58. var node=found;
  59. editor.grid.position.copy(abj.node[node].position);
  60. gui.controls.target.copy(abj.node[node].position);
  61. node_name.innerHTML = "Node " + node;
  62. node_data.className = "visible";
  63. node_vop.innerHTML = "VOP: " + abj.node[node].vop;
  64. } else {
  65. var intersection = mouse.ray.intersectPlane(editor.plane);
  66. if (intersection !== null) {
  67. editor.addQubitAtPoint(intersection);
  68. }
  69. }
  70. };
  71. editor.onShiftClick = function() {
  72. var found = editor.findNodeOnRay(mouse.ray);
  73. if (found === undefined){ return; }
  74. if (editor.selection === undefined){ return; }
  75. if (found === editor.selection){ return; }
  76. //abj.act_cz(found, editor.selection);
  77. api.edit({action:"cz", start:found, end:editor.selection});
  78. gui.serverMessage("Acted CZ between " + found + " & " + editor.selection + ".");
  79. editor.focus(found);
  80. };
  81. editor.onCtrlClick = function() {
  82. var found = editor.findNodeOnRay(mouse.ray);
  83. if (found === undefined){ return; }
  84. if (editor.selection === undefined){ return; }
  85. editor.focus(found);
  86. api.edit({action:"hadamard", node:found});
  87. gui.serverMessage("Acted H on node " + found + ".");
  88. };
  89. editor.prepare = function() {
  90. mouse.onFreeMove = editor.onFreeMove;
  91. mouse.onClick = editor.onClick;
  92. mouse.onShiftClick = editor.onShiftClick;
  93. mouse.onCtrlClick = editor.onCtrlClick;
  94. document.addEventListener("keydown", editor.onKey, false);
  95. editor.makeGrid();
  96. };
  97. editor.onKey = function(evt) {
  98. if (evt.keyCode === 32) {
  99. editor.setOrientation((editor.orientation + 1) % 3);
  100. }
  101. if (evt.keyCode === 46 || evt.keyCode === 68) {
  102. editor.deleteNode();
  103. }
  104. };
  105. editor.setOrientation = function(orientation) {
  106. editor.orientation = orientation;
  107. var rotation = editor.orientations[orientation];
  108. var normal = new THREE.Vector3(0, 1, 0);
  109. normal.applyEuler(rotation);
  110. editor.grid.rotation.copy(rotation);
  111. editor.plane = new THREE.Plane();
  112. editor.plane.setFromNormalAndCoplanarPoint(normal, editor.grid.position);
  113. gui.render();
  114. };
  115. editor.makeGrid = function() {
  116. editor.grid = new THREE.GridHelper(10, 1);
  117. editor.grid.setColors(0xbbbbbb, 0xeeeeee);
  118. editor.grid.renderOrder = 1000;
  119. editor.setOrientation(0);
  120. gui.scene.add(editor.grid);
  121. gui.scene.children[0].renderOrder = -3000;
  122. };
  123. editor.update = function() {};
  124. // Gets a reference to the node nearest to the mouse cursor
  125. editor.findNodeOnRay = function(ray) {
  126. for (var n in abj.node) {
  127. if (ray.distanceSqToPoint(abj.node[n].position) < 0.012) {
  128. return n;
  129. }
  130. }
  131. return undefined;
  132. };
  133. editor.deleteNode = function() {
  134. if (editor.selection === undefined){ return; }
  135. api.edit({action:"delete", node:editor.selection});
  136. gui.serverMessage("Deleted node " + editor.selection + ".");
  137. editor.selection = undefined;
  138. node_data.className = "hidden";
  139. };
  140. //TODO: loadsa space for DRY here
  141. editor.hadamard = function() {
  142. if (editor.selection === undefined){ return; }
  143. api.edit({action:"hadamard", node:editor.selection});
  144. gui.serverMessage("Acted Hadamard on node " + editor.selection + ".");
  145. };
  146. editor.phase = function() {
  147. if (editor.selection === undefined){ return; }
  148. api.edit({action:"phase", node:editor.selection});
  149. gui.serverMessage("Acted phase on node " + editor.selection + ".");
  150. };
  151. editor.measureX = function() {
  152. if (editor.selection === undefined){ return; }
  153. api.edit({action:"measure", node:editor.selection, basis:"x"});
  154. gui.serverMessage("Measured node " + editor.selection + " in X.");
  155. };
  156. editor.measureY = function() {
  157. if (editor.selection === undefined){ return; }
  158. api.edit({action:"measure", node:editor.selection, basis:"y"});
  159. gui.serverMessage("Measured node " + editor.selection + " in Y.");
  160. };
  161. editor.measureZ = function() {
  162. if (editor.selection === undefined){ return; }
  163. api.edit({action:"measure", node:editor.selection, basis:"z"});
  164. gui.serverMessage("Measured node " + editor.selection + " in z.");
  165. };
  166. editor.clear = function() {
  167. api.edit({action:"clear"});
  168. gui.serverMessage("Cleared the graph");
  169. };
  170. editor.raussendorf = function() {
  171. api.edit({action:"raussendorf"});
  172. gui.serverMessage("Made some Raussendorf lattice");
  173. };
  174. editor.localComplementation = function() {
  175. if (editor.selection === undefined){ return; }
  176. api.edit({action:"localcomplementation", node:editor.selection});
  177. abj.local_complementation(editor.selection);
  178. gui.serverMessage("Inverted neighbourhood of " + editor.selection + ".");
  179. };
  180. setInterval(editor.checkTimeOut, 500);