kdbush.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. function kdbush(points, getX, getY, nodeSize, ArrayType) {
  2. return new KDBush(points, getX, getY, nodeSize, ArrayType);
  3. }
  4. function KDBush(points, getX, getY, nodeSize, ArrayType) {
  5. getX = getX || defaultGetX;
  6. getY = getY || defaultGetY;
  7. ArrayType = ArrayType || Array;
  8. this.nodeSize = nodeSize || 64;
  9. this.points = points;
  10. this.ids = new ArrayType(points.length);
  11. this.coords = new ArrayType(points.length * 2);
  12. for (var i = 0; i < points.length; i++) {
  13. this.ids[i] = i;
  14. this.coords[2 * i] = getX(points[i]);
  15. this.coords[2 * i + 1] = getY(points[i]);
  16. }
  17. sort(this.ids, this.coords, this.nodeSize, 0, this.ids.length - 1, 0);
  18. }
  19. KDBush.prototype = {
  20. range: function (minX, minY, maxX, maxY) {
  21. return range(this.ids, this.coords, minX, minY, maxX, maxY, this.nodeSize);
  22. },
  23. within: function (x, y, r) {
  24. return within(this.ids, this.coords, x, y, r, this.nodeSize);
  25. }
  26. };
  27. function defaultGetX(p) { return p[0]; }
  28. function defaultGetY(p) { return p[1]; }
  29. function range(ids, coords, minX, minY, maxX, maxY, nodeSize) {
  30. var stack = [0, ids.length - 1, 0];
  31. var result = [];
  32. var x, y;
  33. while (stack.length) {
  34. var axis = stack.pop();
  35. var right = stack.pop();
  36. var left = stack.pop();
  37. if (right - left <= nodeSize) {
  38. for (var i = left; i <= right; i++) {
  39. x = coords[2 * i];
  40. y = coords[2 * i + 1];
  41. if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]);
  42. }
  43. continue;
  44. }
  45. var m = Math.floor((left + right) / 2);
  46. x = coords[2 * m];
  47. y = coords[2 * m + 1];
  48. if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);
  49. var nextAxis = (axis + 1) % 2;
  50. if (axis === 0 ? minX <= x : minY <= y) {
  51. stack.push(left);
  52. stack.push(m - 1);
  53. stack.push(nextAxis);
  54. }
  55. if (axis === 0 ? maxX >= x : maxY >= y) {
  56. stack.push(m + 1);
  57. stack.push(right);
  58. stack.push(nextAxis);
  59. }
  60. }
  61. return result;
  62. }
  63. function sort(ids, coords, nodeSize, left, right, depth) {
  64. if (right - left <= nodeSize) return;
  65. var m = Math.floor((left + right) / 2);
  66. select(ids, coords, m, left, right, depth % 2);
  67. sort(ids, coords, nodeSize, left, m - 1, depth + 1);
  68. sort(ids, coords, nodeSize, m + 1, right, depth + 1);
  69. }
  70. function select(ids, coords, k, left, right, inc) {
  71. while (right > left) {
  72. if (right - left > 600) {
  73. var n = right - left + 1;
  74. var m = k - left + 1;
  75. var z = Math.log(n);
  76. var s = 0.5 * Math.exp(2 * z / 3);
  77. var sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
  78. var newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
  79. var newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
  80. select(ids, coords, k, newLeft, newRight, inc);
  81. }
  82. var t = coords[2 * k + inc];
  83. var i = left;
  84. var j = right;
  85. swapItem(ids, coords, left, k);
  86. if (coords[2 * right + inc] > t) swapItem(ids, coords, left, right);
  87. while (i < j) {
  88. swapItem(ids, coords, i, j);
  89. i++;
  90. j--;
  91. while (coords[2 * i + inc] < t) i++;
  92. while (coords[2 * j + inc] > t) j--;
  93. }
  94. if (coords[2 * left + inc] === t) swapItem(ids, coords, left, j);
  95. else {
  96. j++;
  97. swapItem(ids, coords, j, right);
  98. }
  99. if (j <= k) left = j + 1;
  100. if (k <= j) right = j - 1;
  101. }
  102. }
  103. function swapItem(ids, coords, i, j) {
  104. swap(ids, i, j);
  105. swap(coords, 2 * i, 2 * j);
  106. swap(coords, 2 * i + 1, 2 * j + 1);
  107. }
  108. function swap(arr, i, j) {
  109. var tmp = arr[i];
  110. arr[i] = arr[j];
  111. arr[j] = tmp;
  112. }
  113. function within(ids, coords, qx, qy, r, nodeSize) {
  114. var stack = [0, ids.length - 1, 0];
  115. var result = [];
  116. var r2 = r * r;
  117. while (stack.length) {
  118. var axis = stack.pop();
  119. var right = stack.pop();
  120. var left = stack.pop();
  121. if (right - left <= nodeSize) {
  122. for (var i = left; i <= right; i++) {
  123. if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]);
  124. }
  125. continue;
  126. }
  127. var m = Math.floor((left + right) / 2);
  128. var x = coords[2 * m];
  129. var y = coords[2 * m + 1];
  130. if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]);
  131. var nextAxis = (axis + 1) % 2;
  132. if (axis === 0 ? qx - r <= x : qy - r <= y) {
  133. stack.push(left);
  134. stack.push(m - 1);
  135. stack.push(nextAxis);
  136. }
  137. if (axis === 0 ? qx + r >= x : qy + r >= y) {
  138. stack.push(m + 1);
  139. stack.push(right);
  140. stack.push(nextAxis);
  141. }
  142. }
  143. return result;
  144. }
  145. function sqDist(ax, ay, bx, by) {
  146. var dx = ax - bx;
  147. var dy = ay - by;
  148. return dx * dx + dy * dy;
  149. }
  150. export default kdbush;