cdjs.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // Copyright (c) 2001-2010, Purdue University. All rights reserved.
  2. // Copyright (C) 2015 Apple Inc. All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in the
  10. // documentation and/or other materials provided with the distribution.
  11. // * Neither the name of the Purdue University nor the
  12. // names of its contributors may be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  19. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. var referenceScore = 20;
  26. ////////////////////////////////////////////////////////////////////////////////
  27. // Runner
  28. ////////////////////////////////////////////////////////////////////////////////
  29. if (typeof (WScript) === "undefined") {
  30. var WScript = {
  31. Echo: print
  32. }
  33. }
  34. var print = function () {};
  35. var performance = performance || {};
  36. performance.now = (function() {
  37. return performance.now ||
  38. performance.mozNow ||
  39. performance.msNow ||
  40. performance.oNow ||
  41. performance.webkitNow ||
  42. Date.now;
  43. })();
  44. function reportResult(score) {
  45. WScript.Echo("### SCORE: " + (100 * referenceScore / score));
  46. }
  47. var top = {};
  48. top.JetStream = {
  49. reportResult: reportResult
  50. };
  51. ////////////////////////////////////////////////////////////////////////////////
  52. // util.js
  53. ////////////////////////////////////////////////////////////////////////////////
  54. function compareNumbers(a, b) {
  55. if (a == b)
  56. return 0;
  57. if (a < b)
  58. return -1;
  59. if (a > b)
  60. return 1;
  61. // We say that NaN is smaller than non-NaN.
  62. if (a == a)
  63. return 1;
  64. return -1;
  65. }
  66. function averageAbovePercentile(numbers, percentile) {
  67. // Don't change the original array.
  68. numbers = numbers.slice();
  69. // Sort in ascending order.
  70. numbers.sort(function(a, b) { return a - b; });
  71. // Now the elements we want are at the end. Keep removing them until the array size shrinks too much.
  72. // Examples assuming percentile = 99:
  73. //
  74. // - numbers.length starts at 100: we will remove just the worst entry and then not remove anymore,
  75. // since then numbers.length / originalLength = 0.99.
  76. //
  77. // - numbers.length starts at 1000: we will remove the ten worst.
  78. //
  79. // - numbers.length starts at 10: we will remove just the worst.
  80. var numbersWeWant = [];
  81. var originalLength = numbers.length;
  82. while (numbers.length / originalLength > percentile / 100)
  83. numbersWeWant.push(numbers.pop());
  84. var sum = 0;
  85. for (var i = 0; i < numbersWeWant.length; ++i)
  86. sum += numbersWeWant[i];
  87. var result = sum / numbersWeWant.length;
  88. // Do a sanity check.
  89. if (numbers.length && result < numbers[numbers.length - 1]) {
  90. throw "Sanity check fail: the worst case result is " + result +
  91. " but we didn't take into account " + numbers;
  92. }
  93. return result;
  94. }
  95. var currentTime;
  96. if (this.performance && performance.now)
  97. currentTime = function() { return performance.now() };
  98. else if (preciseTime)
  99. currentTime = function() { return preciseTime() * 1000; };
  100. else
  101. currentTime = function() { return 0 + new Date(); };
  102. //benchmark.js
  103. function benchmark() {
  104. var verbosity = 0;
  105. var numAircraft = 1000;
  106. var numFrames = 200;
  107. var expectedCollisions = 14484;
  108. var percentile = 95;
  109. var simulator = new Simulator(numAircraft);
  110. var detector = new CollisionDetector();
  111. var lastTime = currentTime();
  112. var results = [];
  113. for (var i = 0; i < numFrames; ++i) {
  114. var time = i / 10;
  115. var collisions = detector.handleNewFrame(simulator.simulate(time));
  116. var before = lastTime;
  117. var after = currentTime();
  118. lastTime = after;
  119. var result = {
  120. time: after - before,
  121. numCollisions: collisions.length
  122. };
  123. if (verbosity >= 2)
  124. result.collisions = collisions;
  125. results.push(result);
  126. }
  127. if (verbosity >= 1) {
  128. for (var i = 0; i < results.length; ++i) {
  129. var string = "Frame " + i + ": " + results[i].time + " ms.";
  130. if (results[i].numCollisions)
  131. string += " (" + results[i].numCollisions + " collisions.)";
  132. print(string);
  133. if (verbosity >= 2 && results[i].collisions.length)
  134. print(" Collisions: " + results[i].collisions);
  135. }
  136. }
  137. // Check results.
  138. var actualCollisions = 0;
  139. for (var i = 0; i < results.length; ++i)
  140. actualCollisions += results[i].numCollisions;
  141. if (actualCollisions != expectedCollisions) {
  142. throw new Error("Bad number of collisions: " + actualCollisions + " (expected " +
  143. expectedCollisions + ")");
  144. }
  145. // Find the worst 5%
  146. var times = [];
  147. for (var i = 0; i < results.length; ++i)
  148. times.push(results[i].time);
  149. return averageAbovePercentile(times, percentile);
  150. }
  151. //call_sign.js
  152. function CallSign(value) {
  153. this._value = value;
  154. }
  155. CallSign.prototype.compareTo = function(other) {
  156. return this._value.localeCompare(other._value);
  157. }
  158. CallSign.prototype.toString = function() {
  159. return this._value;
  160. }
  161. //collision.js
  162. function Collision(aircraft, position) {
  163. this.aircraft = aircraft;
  164. this.position = position;
  165. }
  166. Collision.prototype.toString = function() {
  167. return "Collision(" + this.aircraft + " at " + this.position + ")";
  168. };
  169. //collision_detector.js
  170. function CollisionDetector() {
  171. this._state = new RedBlackTree();
  172. }
  173. CollisionDetector.prototype.handleNewFrame = function(frame) {
  174. var motions = [];
  175. var seen = new RedBlackTree();
  176. for (var i = 0; i < frame.length; ++i) {
  177. var aircraft = frame[i];
  178. var oldPosition = this._state.put(aircraft.callsign, aircraft.position);
  179. var newPosition = aircraft.position;
  180. seen.put(aircraft.callsign, true);
  181. if (!oldPosition) {
  182. // Treat newly introduced aircraft as if they were stationary.
  183. oldPosition = newPosition;
  184. }
  185. motions.push(new Motion(aircraft.callsign, oldPosition, newPosition));
  186. }
  187. // Remove aircraft that are no longer present.
  188. var toRemove = [];
  189. this._state.forEach(function(callsign, position) {
  190. if (!seen.get(callsign))
  191. toRemove.push(callsign);
  192. });
  193. for (var i = 0; i < toRemove.length; ++i)
  194. this._state.remove(toRemove[i]);
  195. var allReduced = reduceCollisionSet(motions);
  196. var collisions = [];
  197. for (var reductionIndex = 0; reductionIndex < allReduced.length; ++reductionIndex) {
  198. var reduced = allReduced[reductionIndex];
  199. for (var i = 0; i < reduced.length; ++i) {
  200. var motion1 = reduced[i];
  201. for (var j = i + 1; j < reduced.length; ++j) {
  202. var motion2 = reduced[j];
  203. var collision = motion1.findIntersection(motion2);
  204. if (collision)
  205. collisions.push(new Collision([motion1.callsign, motion2.callsign], collision));
  206. }
  207. }
  208. }
  209. return collisions;
  210. };
  211. //constants.js
  212. var Constants = {};
  213. Constants.MIN_X = 0;
  214. Constants.MIN_Y = 0;
  215. Constants.MAX_X = 1000;
  216. Constants.MAX_Y = 1000;
  217. Constants.MIN_Z = 0;
  218. Constants.MAX_Z = 10;
  219. Constants.PROXIMITY_RADIUS = 1;
  220. Constants.GOOD_VOXEL_SIZE = Constants.PROXIMITY_RADIUS * 2;
  221. //motion.js
  222. function Motion(callsign, posOne, posTwo) {
  223. this.callsign = callsign;
  224. this.posOne = posOne;
  225. this.posTwo = posTwo;
  226. }
  227. Motion.prototype.toString = function() {
  228. return "Motion(" + this.callsign + " from " + this.posOne + " to " + this.posTwo + ")";
  229. };
  230. Motion.prototype.delta = function() {
  231. return this.posTwo.minus(this.posOne);
  232. };
  233. Motion.prototype.findIntersection = function(other) {
  234. var init1 = this.posOne;
  235. var init2 = other.posOne;
  236. var vec1 = this.delta();
  237. var vec2 = other.delta();
  238. var radius = Constants.PROXIMITY_RADIUS;
  239. // this test is not geometrical 3-d intersection test, it takes the fact that the aircraft move
  240. // into account ; so it is more like a 4d test
  241. // (it assumes that both of the aircraft have a constant speed over the tested interval)
  242. // we thus have two points, each of them moving on its line segment at constant speed ; we are looking
  243. // for times when the distance between these two points is smaller than r
  244. // vec1 is vector of aircraft 1
  245. // vec2 is vector of aircraft 2
  246. // a = (V2 - V1)^T * (V2 - V1)
  247. var a = vec2.minus(vec1).squaredMagnitude();
  248. if (a != 0) {
  249. // we are first looking for instances of time when the planes are exactly r from each other
  250. // at least one plane is moving ; if the planes are moving in parallel, they do not have constant speed
  251. // if the planes are moving in parallel, then
  252. // if the faster starts behind the slower, we can have 2, 1, or 0 solutions
  253. // if the faster plane starts in front of the slower, we can have 0 or 1 solutions
  254. // if the planes are not moving in parallel, then
  255. // point P1 = I1 + vV1
  256. // point P2 = I2 + vV2
  257. // - looking for v, such that dist(P1,P2) = || P1 - P2 || = r
  258. // it follows that || P1 - P2 || = sqrt( < P1-P2, P1-P2 > )
  259. // 0 = -r^2 + < P1 - P2, P1 - P2 >
  260. // from properties of dot product
  261. // 0 = -r^2 + <I1-I2,I1-I2> + v * 2<I1-I2, V1-V2> + v^2 *<V1-V2,V1-V2>
  262. // so we calculate a, b, c - and solve the quadratic equation
  263. // 0 = c + bv + av^2
  264. // b = 2 * <I1-I2, V1-V2>
  265. var b = 2 * init1.minus(init2).dot(vec1.minus(vec2));
  266. // c = -r^2 + (I2 - I1)^T * (I2 - I1)
  267. var c = -radius * radius + init2.minus(init1).squaredMagnitude();
  268. var discr = b * b - 4 * a * c;
  269. if (discr < 0)
  270. return null;
  271. var v1 = (-b - Math.sqrt(discr)) / (2 * a);
  272. var v2 = (-b + Math.sqrt(discr)) / (2 * a);
  273. if (v1 <= v2 && ((v1 <= 1 && 1 <= v2) ||
  274. (v1 <= 0 && 0 <= v2) ||
  275. (0 <= v1 && v2 <= 1))) {
  276. // Pick a good "time" at which to report the collision.
  277. var v;
  278. if (v1 <= 0) {
  279. // The collision started before this frame. Report it at the start of the frame.
  280. v = 0;
  281. } else {
  282. // The collision started during this frame. Report it at that moment.
  283. v = v1;
  284. }
  285. var result1 = init1.plus(vec1.times(v));
  286. var result2 = init2.plus(vec2.times(v));
  287. var result = result1.plus(result2).times(0.5);
  288. if (result.x >= Constants.MIN_X &&
  289. result.x <= Constants.MAX_X &&
  290. result.y >= Constants.MIN_Y &&
  291. result.y <= Constants.MAX_Y &&
  292. result.z >= Constants.MIN_Z &&
  293. result.z <= Constants.MAX_Z)
  294. return result;
  295. }
  296. return null;
  297. }
  298. // the planes have the same speeds and are moving in parallel (or they are not moving at all)
  299. // they thus have the same distance all the time ; we calculate it from the initial point
  300. // dist = || i2 - i1 || = sqrt( ( i2 - i1 )^T * ( i2 - i1 ) )
  301. var dist = init2.minus(init1).magnitude();
  302. if (dist <= radius)
  303. return init1.plus(init2).times(0.5);
  304. return null;
  305. };
  306. //red_black_tree.js
  307. var RedBlackTree = (function(){
  308. function compare(a, b) {
  309. return a.compareTo(b);
  310. }
  311. function treeMinimum(x) {
  312. while (x.left)
  313. x = x.left;
  314. return x;
  315. }
  316. function treeMaximum(x) {
  317. while (x.right)
  318. x = x.right;
  319. return x;
  320. }
  321. function Node(key, value) {
  322. this.key = key;
  323. this.value = value;
  324. this.left = null;
  325. this.right = null;
  326. this.parent = null;
  327. this.color = "red";
  328. }
  329. Node.prototype.successor = function() {
  330. var x = this;
  331. if (x.right)
  332. return treeMinimum(x.right);
  333. var y = x.parent;
  334. while (y && x == y.right) {
  335. x = y;
  336. y = y.parent;
  337. }
  338. return y;
  339. };
  340. Node.prototype.predecessor = function() {
  341. var x = this;
  342. if (x.left)
  343. return treeMaximum(x.left);
  344. var y = x.parent;
  345. while (y && x == y.left) {
  346. x = y;
  347. y = y.parent;
  348. }
  349. return y;
  350. };
  351. Node.prototype.toString = function() {
  352. return this.key + "=>" + this.value + " (" + this.color + ")";
  353. };
  354. function RedBlackTree() {
  355. this._root = null;
  356. }
  357. RedBlackTree.prototype.put = function(key, value) {
  358. var insertionResult = this._treeInsert(key, value);
  359. if (!insertionResult.isNewEntry)
  360. return insertionResult.oldValue;
  361. var x = insertionResult.newNode;
  362. while (x != this._root && x.parent.color == "red") {
  363. if (x.parent == x.parent.parent.left) {
  364. var y = x.parent.parent.right;
  365. if (y && y.color == "red") {
  366. // Case 1
  367. x.parent.color = "black";
  368. y.color = "black";
  369. x.parent.parent.color = "red";
  370. x = x.parent.parent;
  371. } else {
  372. if (x == x.parent.right) {
  373. // Case 2
  374. x = x.parent;
  375. this._leftRotate(x);
  376. }
  377. // Case 3
  378. x.parent.color = "black";
  379. x.parent.parent.color = "red";
  380. this._rightRotate(x.parent.parent);
  381. }
  382. } else {
  383. // Same as "then" clause with "right" and "left" exchanged.
  384. var y = x.parent.parent.left;
  385. if (y && y.color == "red") {
  386. // Case 1
  387. x.parent.color = "black";
  388. y.color = "black";
  389. x.parent.parent.color = "red";
  390. x = x.parent.parent;
  391. } else {
  392. if (x == x.parent.left) {
  393. // Case 2
  394. x = x.parent;
  395. this._rightRotate(x);
  396. }
  397. // Case 3
  398. x.parent.color = "black";
  399. x.parent.parent.color = "red";
  400. this._leftRotate(x.parent.parent);
  401. }
  402. }
  403. }
  404. this._root.color = "black";
  405. return null;
  406. };
  407. RedBlackTree.prototype.remove = function(key) {
  408. var z = this._findNode(key);
  409. if (!z)
  410. return null;
  411. // Y is the node to be unlinked from the tree.
  412. var y;
  413. if (!z.left || !z.right)
  414. y = z;
  415. else
  416. y = z.successor();
  417. // Y is guaranteed to be non-null at this point.
  418. var x;
  419. if (y.left)
  420. x = y.left;
  421. else
  422. x = y.right;
  423. // X is the child of y which might potentially replace y in the tree. X might be null at
  424. // this point.
  425. var xParent;
  426. if (x) {
  427. x.parent = y.parent;
  428. xParent = x.parent;
  429. } else
  430. xParent = y.parent;
  431. if (!y.parent)
  432. this._root = x;
  433. else {
  434. if (y == y.parent.left)
  435. y.parent.left = x;
  436. else
  437. y.parent.right = x;
  438. }
  439. if (y != z) {
  440. if (y.color == "black")
  441. this._removeFixup(x, xParent);
  442. y.parent = z.parent;
  443. y.color = z.color;
  444. y.left = z.left;
  445. y.right = z.right;
  446. if (z.left)
  447. z.left.parent = y;
  448. if (z.right)
  449. z.right.parent = y;
  450. if (z.parent) {
  451. if (z.parent.left == z)
  452. z.parent.left = y;
  453. else
  454. z.parent.right = y;
  455. } else
  456. this._root = y;
  457. } else if (y.color == "black")
  458. this._removeFixup(x, xParent);
  459. return z.value;
  460. };
  461. RedBlackTree.prototype.get = function(key) {
  462. var node = this._findNode(key);
  463. if (!node)
  464. return null;
  465. return node.value;
  466. };
  467. RedBlackTree.prototype.forEach = function(callback) {
  468. if (!this._root)
  469. return;
  470. for (var current = treeMinimum(this._root); current; current = current.successor())
  471. callback(current.key, current.value);
  472. };
  473. RedBlackTree.prototype.asArray = function() {
  474. var result = [];
  475. this.forEach(function(key, value) {
  476. result.push({key: key, value: value});
  477. });
  478. return result;
  479. };
  480. RedBlackTree.prototype.toString = function() {
  481. var result = "[";
  482. var first = true;
  483. this.forEach(function(key, value) {
  484. if (first)
  485. first = false;
  486. else
  487. result += ", ";
  488. result += key + "=>" + value;
  489. });
  490. return result + "]";
  491. };
  492. RedBlackTree.prototype._findNode = function(key) {
  493. for (var current = this._root; current;) {
  494. var comparisonResult = compare(key, current.key);
  495. if (!comparisonResult)
  496. return current;
  497. if (comparisonResult < 0)
  498. current = current.left;
  499. else
  500. current = current.right;
  501. }
  502. return null;
  503. };
  504. RedBlackTree.prototype._treeInsert = function(key, value) {
  505. var y = null;
  506. var x = this._root;
  507. while (x) {
  508. y = x;
  509. var comparisonResult = key.compareTo(x.key);
  510. if (comparisonResult < 0)
  511. x = x.left;
  512. else if (comparisonResult > 0)
  513. x = x.right;
  514. else {
  515. var oldValue = x.value;
  516. x.value = value;
  517. return {isNewEntry:false, oldValue:oldValue};
  518. }
  519. }
  520. var z = new Node(key, value);
  521. z.parent = y;
  522. if (!y)
  523. this._root = z;
  524. else {
  525. if (key.compareTo(y.key) < 0)
  526. y.left = z;
  527. else
  528. y.right = z;
  529. }
  530. return {isNewEntry:true, newNode:z};
  531. };
  532. RedBlackTree.prototype._leftRotate = function(x) {
  533. var y = x.right;
  534. // Turn y's left subtree into x's right subtree.
  535. x.right = y.left;
  536. if (y.left)
  537. y.left.parent = x;
  538. // Link x's parent to y.
  539. y.parent = x.parent;
  540. if (!x.parent)
  541. this._root = y;
  542. else {
  543. if (x == x.parent.left)
  544. x.parent.left = y;
  545. else
  546. x.parent.right = y;
  547. }
  548. // Put x on y's left.
  549. y.left = x;
  550. x.parent = y;
  551. return y;
  552. };
  553. RedBlackTree.prototype._rightRotate = function(y) {
  554. var x = y.left;
  555. // Turn x's right subtree into y's left subtree.
  556. y.left = x.right;
  557. if (x.right)
  558. x.right.parent = y;
  559. // Link y's parent to x;
  560. x.parent = y.parent;
  561. if (!y.parent)
  562. this._root = x;
  563. else {
  564. if (y == y.parent.left)
  565. y.parent.left = x;
  566. else
  567. y.parent.right = x;
  568. }
  569. x.right = y;
  570. y.parent = x;
  571. return x;
  572. };
  573. RedBlackTree.prototype._removeFixup = function(x, xParent) {
  574. while (x != this._root && (!x || x.color == "black")) {
  575. if (x == xParent.left) {
  576. // Note: the text points out that w cannot be null. The reason is not obvious from
  577. // simply looking at the code; it comes about from the properties of the red-black
  578. // tree.
  579. var w = xParent.right;
  580. if (w.color == "red") {
  581. // Case 1
  582. w.color = "black";
  583. xParent.color = "red";
  584. this._leftRotate(xParent);
  585. w = xParent.right;
  586. }
  587. if ((!w.left || w.left.color == "black")
  588. && (!w.right || w.right.color == "black")) {
  589. // Case 2
  590. w.color = "red";
  591. x = xParent;
  592. xParent = x.parent;
  593. } else {
  594. if (!w.right || w.right.color == "black") {
  595. // Case 3
  596. w.left.color = "black";
  597. w.color = "red";
  598. this._rightRotate(w);
  599. w = xParent.right;
  600. }
  601. // Case 4
  602. w.color = xParent.color;
  603. xParent.color = "black";
  604. if (w.right)
  605. w.right.color = "black";
  606. this._leftRotate(xParent);
  607. x = this._root;
  608. xParent = x.parent;
  609. }
  610. } else {
  611. // Same as "then" clause with "right" and "left" exchanged.
  612. var w = xParent.left;
  613. if (w.color == "red") {
  614. // Case 1
  615. w.color = "black";
  616. xParent.color = "red";
  617. this._rightRotate(xParent);
  618. w = xParent.left;
  619. }
  620. if ((!w.right || w.right.color == "black")
  621. && (!w.left || w.left.color == "black")) {
  622. // Case 2
  623. w.color = "red";
  624. x = xParent;
  625. xParent = x.parent;
  626. } else {
  627. if (!w.left || w.left.color == "black") {
  628. // Case 3
  629. w.right.color = "black";
  630. w.color = "red";
  631. this._leftRotate(w);
  632. w = xParent.left;
  633. }
  634. // Case 4
  635. w.color = xParent.color;
  636. xParent.color = "black";
  637. if (w.left)
  638. w.left.color = "black";
  639. this._rightRotate(xParent);
  640. x = this._root;
  641. xParent = x.parent;
  642. }
  643. }
  644. }
  645. if (x)
  646. x.color = "black";
  647. };
  648. return RedBlackTree;
  649. })();
  650. //reduce_collision_set.js
  651. var drawMotionOnVoxelMap = (function() {
  652. var voxelSize = Constants.GOOD_VOXEL_SIZE;
  653. var horizontal = new Vector2D(voxelSize, 0);
  654. var vertical = new Vector2D(0, voxelSize);
  655. function voxelHash(position) {
  656. var xDiv = (position.x / voxelSize) | 0;
  657. var yDiv = (position.y / voxelSize) | 0;
  658. var result = new Vector2D();
  659. result.x = voxelSize * xDiv;
  660. result.y = voxelSize * yDiv;
  661. if (position.x < 0)
  662. result.x -= voxelSize;
  663. if (position.y < 0)
  664. result.y -= voxelSize;
  665. return result;
  666. }
  667. return function(voxelMap, motion) {
  668. var seen = new RedBlackTree();
  669. function putIntoMap(voxel) {
  670. var array = voxelMap.get(voxel);
  671. if (!array)
  672. voxelMap.put(voxel, array = []);
  673. array.push(motion);
  674. }
  675. function isInVoxel(voxel) {
  676. if (voxel.x > Constants.MAX_X ||
  677. voxel.x < Constants.MIN_X ||
  678. voxel.y > Constants.MAX_Y ||
  679. voxel.y < Constants.MIN_Y)
  680. return false;
  681. var init = motion.posOne;
  682. var fin = motion.posTwo;
  683. var v_s = voxelSize;
  684. var r = Constants.PROXIMITY_RADIUS / 2;
  685. var v_x = voxel.x;
  686. var x0 = init.x;
  687. var xv = fin.x - init.x;
  688. var v_y = voxel.y;
  689. var y0 = init.y;
  690. var yv = fin.y - init.y;
  691. var low_x, high_x;
  692. low_x = (v_x - r - x0) / xv;
  693. high_x = (v_x + v_s + r - x0) / xv;
  694. if (xv < 0) {
  695. var tmp = low_x;
  696. low_x = high_x;
  697. high_x = tmp;
  698. }
  699. var low_y, high_y;
  700. low_y = (v_y - r - y0) / yv;
  701. high_y = (v_y + v_s + r - y0) / yv;
  702. if (yv < 0) {
  703. var tmp = low_y;
  704. low_y = high_y;
  705. high_y = tmp;
  706. }
  707. if (false) {
  708. print("v_x = " + v_x + ", x0 = " + x0 + ", xv = " + xv + ", v_y = " + v_y + ", y0 = " + y0 + ", yv = " + yv + ", low_x = " + low_x + ", low_y = " + low_y + ", high_x = " + high_x + ", high_y = " + high_y);
  709. }
  710. return (((xv == 0 && v_x <= x0 + r && x0 - r <= v_x + v_s) /* no motion in x */ ||
  711. ((low_x <= 1 && 1 <= high_x) || (low_x <= 0 && 0 <= high_x) ||
  712. (0 <= low_x && high_x <= 1))) &&
  713. ((yv == 0 && v_y <= y0 + r && y0 - r <= v_y + v_s) /* no motion in y */ ||
  714. ((low_y <= 1 && 1 <= high_y) || (low_y <= 0 && 0 <= high_y) ||
  715. (0 <= low_y && high_y <= 1))) &&
  716. (xv == 0 || yv == 0 || /* no motion in x or y or both */
  717. (low_y <= high_x && high_x <= high_y) ||
  718. (low_y <= low_x && low_x <= high_y) ||
  719. (low_x <= low_y && high_y <= high_x)));
  720. }
  721. function recurse(nextVoxel) {
  722. if (!isInVoxel(nextVoxel, motion))
  723. return;
  724. if (seen.put(nextVoxel, true))
  725. return;
  726. putIntoMap(nextVoxel);
  727. recurse(nextVoxel.minus(horizontal));
  728. recurse(nextVoxel.plus(horizontal));
  729. recurse(nextVoxel.minus(vertical));
  730. recurse(nextVoxel.plus(vertical));
  731. recurse(nextVoxel.minus(horizontal).minus(vertical));
  732. recurse(nextVoxel.minus(horizontal).plus(vertical));
  733. recurse(nextVoxel.plus(horizontal).minus(vertical));
  734. recurse(nextVoxel.plus(horizontal).plus(vertical));
  735. }
  736. recurse(voxelHash(motion.posOne));
  737. };
  738. })();
  739. function reduceCollisionSet(motions) {
  740. var voxelMap = new RedBlackTree();
  741. for (var i = 0; i < motions.length; ++i)
  742. drawMotionOnVoxelMap(voxelMap, motions[i]);
  743. var result = [];
  744. voxelMap.forEach(function(key, value) {
  745. if (value.length > 1)
  746. result.push(value);
  747. });
  748. return result;
  749. }
  750. //simulator.js
  751. function Simulator(numAircraft) {
  752. this._aircraft = [];
  753. for (var i = 0; i < numAircraft; ++i)
  754. this._aircraft.push(new CallSign("foo" + i));
  755. }
  756. Simulator.prototype.simulate = function(time) {
  757. var frame = [];
  758. for (var i = 0; i < this._aircraft.length; i += 2) {
  759. frame.push({
  760. callsign: this._aircraft[i],
  761. position: new Vector3D(time, Math.cos(time) * 2 + i * 3, 10)
  762. });
  763. frame.push({
  764. callsign: this._aircraft[i + 1],
  765. position: new Vector3D(time, Math.sin(time) * 2 + i * 3, 10)
  766. });
  767. }
  768. return frame;
  769. };
  770. //vector_2d.js
  771. function Vector2D(x, y) {
  772. this.x = x;
  773. this.y = y;
  774. }
  775. Vector2D.prototype.plus = function(other) {
  776. return new Vector2D(this.x + other.x,
  777. this.y + other.y);
  778. };
  779. Vector2D.prototype.minus = function(other) {
  780. return new Vector2D(this.x - other.x,
  781. this.y - other.y);
  782. };
  783. Vector2D.prototype.toString = function() {
  784. return "[" + this.x + ", " + this.y + "]";
  785. };
  786. Vector2D.prototype.compareTo = function(other) {
  787. var result = compareNumbers(this.x, other.x);
  788. if (result)
  789. return result;
  790. return compareNumbers(this.y, other.y);
  791. };
  792. //vector_3d.js
  793. function Vector3D(x, y, z) {
  794. this.x = x;
  795. this.y = y;
  796. this.z = z;
  797. }
  798. Vector3D.prototype.plus = function(other) {
  799. return new Vector3D(this.x + other.x,
  800. this.y + other.y,
  801. this.z + other.z);
  802. };
  803. Vector3D.prototype.minus = function(other) {
  804. return new Vector3D(this.x - other.x,
  805. this.y - other.y,
  806. this.z - other.z);
  807. };
  808. Vector3D.prototype.dot = function(other) {
  809. return this.x * other.x + this.y * other.y + this.z * other.z;
  810. };
  811. Vector3D.prototype.squaredMagnitude = function() {
  812. return this.dot(this);
  813. };
  814. Vector3D.prototype.magnitude = function() {
  815. return Math.sqrt(this.squaredMagnitude());
  816. };
  817. Vector3D.prototype.times = function(amount) {
  818. return new Vector3D(this.x * amount,
  819. this.y * amount,
  820. this.z * amount);
  821. };
  822. Vector3D.prototype.as2D = function() {
  823. return new Vector2D(this.x, this.y);
  824. };
  825. Vector3D.prototype.toString = function() {
  826. return "[" + this.x + ", " + this.y + ", " + this.z + "]";
  827. };
  828. //Run the benchmark
  829. var result = benchmark();
  830. ////////////////////////////////////////////////////////////////////////////////
  831. // Runner
  832. ////////////////////////////////////////////////////////////////////////////////
  833. top.JetStream.reportResult(result);