Anders and Briegel in Python
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.

185 lines
5.6KB

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