|
1 | 1 | /** |
2 | | - * sparse-octree v5.1.1 build Wed Feb 06 2019 |
| 2 | + * sparse-octree v5.2.0 build Mon Feb 11 2019 |
3 | 3 | * https://github.com/vanruesc/sparse-octree |
4 | 4 | * Copyright 2019 Raoul van Rüschen, Zlib |
5 | 5 | */ |
|
5143 | 5143 | } |
5144 | 5144 |
|
5145 | 5145 | function _findNearestPoint(point, maxDistance, skipSelf, octant) { |
5146 | | - var points = octant.points; |
5147 | | - var children = octant.children; |
5148 | 5146 | var result = null; |
5149 | | - var bestDist = maxDistance; |
| 5147 | + var bestDistance = maxDistance; |
5150 | 5148 | var i, l; |
5151 | | - var p, distSq; |
5152 | | - var sortedChildren; |
5153 | | - var child, childResult; |
5154 | 5149 |
|
5155 | | - if (children !== null) { |
5156 | | - sortedChildren = children.map(function (child) { |
| 5150 | + if (octant.children !== null) { |
| 5151 | + var sortedChildren = octant.children.map(function (child) { |
5157 | 5152 | return { |
5158 | 5153 | octant: child, |
5159 | 5154 | distance: child.distanceToCenterSquared(point) |
5160 | 5155 | }; |
5161 | 5156 | }).sort(function (a, b) { |
5162 | 5157 | return a.distance - b.distance; |
5163 | 5158 | }); |
| 5159 | + var child, intermediateResult; |
5164 | 5160 |
|
5165 | 5161 | for (i = 0, l = sortedChildren.length; i < l; ++i) { |
5166 | 5162 | child = sortedChildren[i].octant; |
5167 | 5163 |
|
5168 | | - if (child.contains(point, bestDist)) { |
5169 | | - childResult = _findNearestPoint(point, bestDist, skipSelf, child); |
| 5164 | + if (child.contains(point, bestDistance)) { |
| 5165 | + intermediateResult = _findNearestPoint(point, bestDistance, skipSelf, child); |
5170 | 5166 |
|
5171 | | - if (childResult !== null) { |
5172 | | - distSq = childResult.point.distanceToSquared(point); |
| 5167 | + if (intermediateResult !== null) { |
| 5168 | + bestDistance = intermediateResult.distance; |
| 5169 | + result = intermediateResult; |
5173 | 5170 |
|
5174 | | - if ((!skipSelf || distSq > 0.0) && distSq < bestDist) { |
5175 | | - bestDist = distSq; |
5176 | | - result = childResult; |
| 5171 | + if (bestDistance === 0.0) { |
| 5172 | + break; |
5177 | 5173 | } |
5178 | 5174 | } |
5179 | 5175 | } |
5180 | 5176 | } |
5181 | | - } else if (points !== null) { |
| 5177 | + } else if (octant.points !== null) { |
| 5178 | + var points = octant.points; |
| 5179 | + var index = -1; |
| 5180 | + var distance; |
| 5181 | + |
5182 | 5182 | for (i = 0, l = points.length; i < l; ++i) { |
5183 | | - p = points[i]; |
5184 | | - distSq = point.distanceToSquared(p); |
| 5183 | + if (points[i].equals(point)) { |
| 5184 | + if (!skipSelf) { |
| 5185 | + bestDistance = 0.0; |
| 5186 | + index = i; |
| 5187 | + break; |
| 5188 | + } |
| 5189 | + } else { |
| 5190 | + distance = point.distanceTo(points[i]); |
5185 | 5191 |
|
5186 | | - if ((!skipSelf || distSq > 0.0) && distSq < bestDist) { |
5187 | | - bestDist = distSq; |
5188 | | - result = { |
5189 | | - point: p.clone(), |
5190 | | - data: octant.data[i] |
5191 | | - }; |
| 5192 | + if (distance < bestDistance) { |
| 5193 | + bestDistance = distance; |
| 5194 | + index = i; |
| 5195 | + } |
5192 | 5196 | } |
5193 | 5197 | } |
| 5198 | + |
| 5199 | + if (index >= 0) { |
| 5200 | + result = { |
| 5201 | + point: points[index], |
| 5202 | + data: octant.data[index], |
| 5203 | + distance: bestDistance |
| 5204 | + }; |
| 5205 | + } |
5194 | 5206 | } |
5195 | 5207 |
|
5196 | 5208 | return result; |
5197 | 5209 | } |
5198 | 5210 |
|
5199 | 5211 | function _findPoints(point, radius, skipSelf, octant, result) { |
5200 | | - var points = octant.points; |
5201 | 5212 | var children = octant.children; |
5202 | | - var rSq = radius * radius; |
5203 | 5213 | var i, l; |
5204 | | - var p, distSq; |
5205 | | - var child; |
5206 | 5214 |
|
5207 | 5215 | if (children !== null) { |
| 5216 | + var child; |
| 5217 | + |
5208 | 5218 | for (i = 0, l = children.length; i < l; ++i) { |
5209 | 5219 | child = children[i]; |
5210 | 5220 |
|
5211 | 5221 | if (child.contains(point, radius)) { |
5212 | 5222 | _findPoints(point, radius, skipSelf, child, result); |
5213 | 5223 | } |
5214 | 5224 | } |
5215 | | - } else if (points !== null) { |
| 5225 | + } else if (octant.points !== null) { |
| 5226 | + var points = octant.points; |
| 5227 | + var rSq = radius * radius; |
| 5228 | + var p; |
| 5229 | + |
5216 | 5230 | for (i = 0, l = points.length; i < l; ++i) { |
5217 | 5231 | p = points[i]; |
5218 | | - distSq = point.distanceToSquared(p); |
5219 | 5232 |
|
5220 | | - if ((!skipSelf || distSq > 0.0) && distSq <= rSq) { |
| 5233 | + if (p.equals(point)) { |
| 5234 | + if (!skipSelf) { |
| 5235 | + result.push({ |
| 5236 | + point: p.clone(), |
| 5237 | + data: octant.data[i] |
| 5238 | + }); |
| 5239 | + } |
| 5240 | + } else if (p.distanceToSquared(point) <= rSq) { |
5221 | 5241 | result.push({ |
5222 | 5242 | point: p.clone(), |
5223 | 5243 | data: octant.data[i] |
|
5278 | 5298 | value: function findNearestPoint(point) { |
5279 | 5299 | var maxDistance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Infinity; |
5280 | 5300 | var skipSelf = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; |
5281 | | - return _findNearestPoint(point, maxDistance, skipSelf, this.root); |
| 5301 | + |
| 5302 | + var result = _findNearestPoint(point, maxDistance, skipSelf, this.root); |
| 5303 | + |
| 5304 | + if (result !== null) { |
| 5305 | + result.point = result.point.clone(); |
| 5306 | + } |
| 5307 | + |
| 5308 | + return result; |
5282 | 5309 | } |
5283 | 5310 | }, { |
5284 | 5311 | key: "findPoints", |
|
0 commit comments