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

216 行
6.5KB

  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 < 10;
  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. if (evt.keyCode === 32) {
  104. editor.setOrientation((editor.orientation + 1) % 3);
  105. }
  106. if (evt.keyCode === 46 || evt.keyCode === 68) {
  107. editor.deleteNode();
  108. }
  109. };
  110. editor.setOrientation = function(orientation) {
  111. editor.orientation = orientation;
  112. var rotation = editor.orientations[orientation];
  113. var normal = new THREE.Vector3(0, 1, 0);
  114. normal.applyEuler(rotation);
  115. editor.grid.rotation.copy(rotation);
  116. editor.plane = new THREE.Plane();
  117. editor.plane.setFromNormalAndCoplanarPoint(normal, editor.grid.position);
  118. gui.render();
  119. };
  120. editor.makeGrid = function() {
  121. editor.grid = new THREE.GridHelper(10, 1);
  122. editor.grid.setColors(0xbbbbbb, 0xeeeeee);
  123. editor.grid.renderOrder = 1000;
  124. editor.setOrientation(0);
  125. gui.scene.add(editor.grid);
  126. gui.scene.children[0].renderOrder = -3000;
  127. };
  128. editor.update = function() {};
  129. // Gets a reference to the node nearest to the mouse cursor
  130. editor.findNodeOnRay = function(ray) {
  131. for (var n in abj.node) {
  132. if (ray.distanceSqToPoint(abj.node[n].position) < 0.012) {
  133. return n;
  134. }
  135. }
  136. return undefined;
  137. };
  138. editor.deleteNode = function() {
  139. if (editor.selection === undefined){ return; }
  140. api.edit({action:"delete", node:editor.selection});
  141. gui.serverMessage("Deleted node " + editor.selection + ".");
  142. editor.selection = undefined;
  143. node_data.className = "hidden";
  144. };
  145. //TODO: loadsa space for DRY here
  146. editor.hadamard = function() {
  147. if (editor.selection === undefined){ return; }
  148. api.edit({action:"hadamard", node:editor.selection});
  149. gui.serverMessage("Acted Hadamard on node " + editor.selection + ".");
  150. };
  151. editor.phase = function() {
  152. if (editor.selection === undefined){ return; }
  153. api.edit({action:"phase", node:editor.selection});
  154. gui.serverMessage("Acted phase on node " + editor.selection + ".");
  155. };
  156. editor.measureX = function() {
  157. if (editor.selection === undefined){ return; }
  158. api.edit({action:"measure", node:editor.selection, basis:"x"});
  159. gui.serverMessage("Measured node " + editor.selection + " in X.");
  160. };
  161. editor.measureY = function() {
  162. if (editor.selection === undefined){ return; }
  163. api.edit({action:"measure", node:editor.selection, basis:"y"});
  164. gui.serverMessage("Measured node " + editor.selection + " in Y.");
  165. };
  166. editor.measureZ = function() {
  167. if (editor.selection === undefined){ return; }
  168. api.edit({action:"measure", node:editor.selection, basis:"z"});
  169. gui.serverMessage("Measured node " + editor.selection + " in z.");
  170. };
  171. editor.clear = function() {
  172. api.edit({action:"clear"});
  173. gui.serverMessage("Cleared the graph");
  174. };
  175. editor.raussendorf = function() {
  176. api.edit({action:"raussendorf"});
  177. gui.serverMessage("Made some Raussendorf lattice");
  178. };
  179. editor.localComplementation = function() {
  180. if (editor.selection === undefined){ return; }
  181. api.edit({action:"localcomplementation", node:editor.selection});
  182. abj.local_complementation(editor.selection);
  183. gui.serverMessage("Inverted neighbourhood of " + editor.selection + ".");
  184. };
  185. setInterval(editor.checkTimeOut, 500);