KDTree.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import quickSelect from './quickSelect.js';
  41. var KDTreeNode = /** @class */function () {
  42. function KDTreeNode(axis, data) {
  43. this.axis = axis;
  44. this.data = data;
  45. }
  46. return KDTreeNode;
  47. }();
  48. /**
  49. * @constructor
  50. * @alias module:echarts/data/KDTree
  51. * @param {Array} points List of points.
  52. * each point needs an array property to represent the actual data
  53. * @param {Number} [dimension]
  54. * Point dimension.
  55. * Default will use the first point's length as dimension.
  56. */
  57. var KDTree = /** @class */function () {
  58. function KDTree(points, dimension) {
  59. // Use one stack to avoid allocation
  60. // each time searching the nearest point
  61. this._stack = [];
  62. // Again avoid allocating a new array
  63. // each time searching nearest N points
  64. this._nearstNList = [];
  65. if (!points.length) {
  66. return;
  67. }
  68. if (!dimension) {
  69. dimension = points[0].array.length;
  70. }
  71. this.dimension = dimension;
  72. this.root = this._buildTree(points, 0, points.length - 1, 0);
  73. }
  74. /**
  75. * Recursively build the tree.
  76. */
  77. KDTree.prototype._buildTree = function (points, left, right, axis) {
  78. if (right < left) {
  79. return null;
  80. }
  81. var medianIndex = Math.floor((left + right) / 2);
  82. medianIndex = quickSelect(points, left, right, medianIndex, function (a, b) {
  83. return a.array[axis] - b.array[axis];
  84. });
  85. var median = points[medianIndex];
  86. var node = new KDTreeNode(axis, median);
  87. axis = (axis + 1) % this.dimension;
  88. if (right > left) {
  89. node.left = this._buildTree(points, left, medianIndex - 1, axis);
  90. node.right = this._buildTree(points, medianIndex + 1, right, axis);
  91. }
  92. return node;
  93. };
  94. ;
  95. /**
  96. * Find nearest point
  97. * @param target Target point
  98. * @param squaredDistance Squared distance function
  99. * @return Nearest point
  100. */
  101. KDTree.prototype.nearest = function (target, squaredDistance) {
  102. var curr = this.root;
  103. var stack = this._stack;
  104. var idx = 0;
  105. var minDist = Infinity;
  106. var nearestNode = null;
  107. if (curr.data !== target) {
  108. minDist = squaredDistance(curr.data, target);
  109. nearestNode = curr;
  110. }
  111. if (target.array[curr.axis] < curr.data.array[curr.axis]) {
  112. // Left first
  113. curr.right && (stack[idx++] = curr.right);
  114. curr.left && (stack[idx++] = curr.left);
  115. } else {
  116. // Right first
  117. curr.left && (stack[idx++] = curr.left);
  118. curr.right && (stack[idx++] = curr.right);
  119. }
  120. while (idx--) {
  121. curr = stack[idx];
  122. var currDist = target.array[curr.axis] - curr.data.array[curr.axis];
  123. var isLeft = currDist < 0;
  124. var needsCheckOtherSide = false;
  125. currDist = currDist * currDist;
  126. // Intersecting right hyperplane with minDist hypersphere
  127. if (currDist < minDist) {
  128. currDist = squaredDistance(curr.data, target);
  129. if (currDist < minDist && curr.data !== target) {
  130. minDist = currDist;
  131. nearestNode = curr;
  132. }
  133. needsCheckOtherSide = true;
  134. }
  135. if (isLeft) {
  136. if (needsCheckOtherSide) {
  137. curr.right && (stack[idx++] = curr.right);
  138. }
  139. // Search in the left area
  140. curr.left && (stack[idx++] = curr.left);
  141. } else {
  142. if (needsCheckOtherSide) {
  143. curr.left && (stack[idx++] = curr.left);
  144. }
  145. // Search the right area
  146. curr.right && (stack[idx++] = curr.right);
  147. }
  148. }
  149. return nearestNode.data;
  150. };
  151. ;
  152. KDTree.prototype._addNearest = function (found, dist, node) {
  153. var nearestNList = this._nearstNList;
  154. var i = found - 1;
  155. // Insert to the right position
  156. // Sort from small to large
  157. for (; i > 0; i--) {
  158. if (dist >= nearestNList[i - 1].dist) {
  159. break;
  160. } else {
  161. nearestNList[i].dist = nearestNList[i - 1].dist;
  162. nearestNList[i].node = nearestNList[i - 1].node;
  163. }
  164. }
  165. nearestNList[i].dist = dist;
  166. nearestNList[i].node = node;
  167. };
  168. ;
  169. /**
  170. * Find nearest N points
  171. * @param target Target point
  172. * @param N
  173. * @param squaredDistance Squared distance function
  174. * @param output Output nearest N points
  175. */
  176. KDTree.prototype.nearestN = function (target, N, squaredDistance, output) {
  177. if (N <= 0) {
  178. output.length = 0;
  179. return output;
  180. }
  181. var curr = this.root;
  182. var stack = this._stack;
  183. var idx = 0;
  184. var nearestNList = this._nearstNList;
  185. for (var i = 0; i < N; i++) {
  186. // Allocate
  187. if (!nearestNList[i]) {
  188. nearestNList[i] = {
  189. dist: 0,
  190. node: null
  191. };
  192. }
  193. nearestNList[i].dist = 0;
  194. nearestNList[i].node = null;
  195. }
  196. var currDist = squaredDistance(curr.data, target);
  197. var found = 0;
  198. if (curr.data !== target) {
  199. found++;
  200. this._addNearest(found, currDist, curr);
  201. }
  202. if (target.array[curr.axis] < curr.data.array[curr.axis]) {
  203. // Left first
  204. curr.right && (stack[idx++] = curr.right);
  205. curr.left && (stack[idx++] = curr.left);
  206. } else {
  207. // Right first
  208. curr.left && (stack[idx++] = curr.left);
  209. curr.right && (stack[idx++] = curr.right);
  210. }
  211. while (idx--) {
  212. curr = stack[idx];
  213. var currDist_1 = target.array[curr.axis] - curr.data.array[curr.axis];
  214. var isLeft = currDist_1 < 0;
  215. var needsCheckOtherSide = false;
  216. currDist_1 = currDist_1 * currDist_1;
  217. // Intersecting right hyperplane with minDist hypersphere
  218. if (found < N || currDist_1 < nearestNList[found - 1].dist) {
  219. currDist_1 = squaredDistance(curr.data, target);
  220. if ((found < N || currDist_1 < nearestNList[found - 1].dist) && curr.data !== target) {
  221. if (found < N) {
  222. found++;
  223. }
  224. this._addNearest(found, currDist_1, curr);
  225. }
  226. needsCheckOtherSide = true;
  227. }
  228. if (isLeft) {
  229. if (needsCheckOtherSide) {
  230. curr.right && (stack[idx++] = curr.right);
  231. }
  232. // Search in the left area
  233. curr.left && (stack[idx++] = curr.left);
  234. } else {
  235. if (needsCheckOtherSide) {
  236. curr.left && (stack[idx++] = curr.left);
  237. }
  238. // Search the right area
  239. curr.right && (stack[idx++] = curr.right);
  240. }
  241. }
  242. // Copy to output
  243. for (var i = 0; i < found; i++) {
  244. output[i] = nearestNList[i].node.data;
  245. }
  246. output.length = found;
  247. return output;
  248. };
  249. return KDTree;
  250. }();
  251. export default KDTree;