Simulate graph states in the browser
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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