Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import macro from 'vtk.js/Sources/macros'; import vtkMath from 'vtk.js/Sources/Common/Core/Math'; import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox'; import vtkIncrementalOctreeNode from 'vtk.js/Sources/Common/DataModel/IncrementalOctreeNode'; import vtkAbstractPointLocator from 'vtk.js/Sources/Common/DataModel/AbstractPointLocator'; import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants'; const { vtkErrorMacro } = macro; function vtkIncrementalOctreePointLocator(publicAPI, model) { // Set our className model.classHierarchy.push('vtkIncrementalOctreePointLocator'); function getLeafContainer(node, pnt) { return node.isLeaf() ? node : getLeafContainer(node.getChild(node.getChildIndex(pnt)), pnt); } //------------------------------------------------------------------------------ publicAPI.freeSearchStructure = () => { model.octreeRootNode = null; model.numberOfNodes = 0; model.locatorPoints = null; }; //------------------------------------------------------------------------------ publicAPI.findClosestPointInLeafNode = (leafNode, point) => { // NOTE: dist2 MUST be initiated with a very huge value below, but instead of // model.octreeMaxDimSize * model.octreeMaxDimSize * 4.0, because the point // under check may be outside the octree and hence the squared distance can // be greater than the latter or other similar octree-based specific values. let dist2 = Number.MAX_VALUE; if (leafNode.getPointIdSet() == null) { return [-1, dist2]; } let numPts = 0; let tmpDst = 0.0; const tmpPnt = []; let tmpIdx = -1; let pntIdx = -1; let idList = leafNode.getPointIdSet(); numPts = idList.length; for (let i = 0; i < numPts; i++) { tmpIdx = idList[i]; model.locatorPoints.getPoint(tmpIdx, tmpPnt); tmpDst = vtkMath.distance2BetweenPoints(tmpPnt, point); if (tmpDst < dist2) { dist2 = tmpDst; pntIdx = tmpIdx; } if (dist2 === 0.0) { break; } } idList = null; return [pntIdx, dist2]; }; publicAPI.findClosestPointInSphere = (point, radius2, maskNode, refDist2) => { let pointIndx = -1; let minDist2 = Number.MAX_VALUE; const nodesBase = []; nodesBase.push(model.octreeRootNode); let checkNode; let childNode; let distToData; let tempDist2; let tempPntId; while (!nodesBase.length === 0 && minDist2 > 0.0) { checkNode = nodesBase.top(); nodesBase.pop(); if (!checkNode.isLeaf()) { for (let i = 0; i < 8; i++) { childNode = checkNode.getChild(i); // use ( radius2 + radius2 ) to skip empty nodes distToData = childNode.getNumberOfPoints() ? childNode.getDistance2ToBoundary(point, model.octreeRootNode, 1) : radius2 + radius2; // If a child node is not the mask node AND its distance, specifically // the data bounding box (determined by the points inside or under) to // the point, is less than the threshold radius (one exception is the // point's container nodes), it is pushed to the stack as a suspect. if ( childNode !== maskNode && (distToData <= refDist2 || childNode.containsPoint(point) === 1) ) { nodesBase.push(childNode); } childNode = null; } } else { // now that the node under check is a leaf, let's find the closest // point therein and the minimum distance [tempPntId, tempDist2] = publicAPI.findClosestPointInLeafNode( checkNode, point ); if (tempDist2 < minDist2) { minDist2 = tempDist2; pointIndx = tempPntId; } } checkNode = null; } return [minDist2 <= radius2 ? pointIndx : -1, minDist2]; }; //------------------------------------------------------------------------------ publicAPI.initPointInsertion = (points, bounds, estNumPts = 0) => { let i = 0; let bbIndex = 0; if (points == null) { vtkErrorMacro('a valid vtkPoints object required for point insertion'); return false; } // destroy the existing octree, if any publicAPI.freeSearchStructure(); model.locatorPoints = points; // obtain the threshold squared distance model.insertTolerance2 = model.tolerance * model.tolerance; // Fix bounds // (1) push out a little bit if the original volume is too flat --- a slab // (2) pull back the x, y, and z's lower bounds a little bit such that // points are clearly "inside" the spatial region. Point p is taken as // "inside" range r = [r1, r2] if and only if r1 < p <= r2. model.octreeMaxDimSize = 0.0; const tmpBbox = [...bounds]; const dimDiff = vtkBoundingBox.getLengths(bounds); model.octreeMaxDimSize = Math.max(...dimDiff); if (model.buildCubicOctree) { // make the bounding box a cube and hence descendant octants cubes too for (i = 0; i < 3; i++) { if (dimDiff[i] !== model.octreeMaxDimSize) { const delta = model.octreeMaxDimSize - dimDiff[i]; tmpBbox[2 * i] -= 0.5 * delta; tmpBbox[2 * i + 1] += 0.5 * delta; dimDiff[i] = model.octreeMaxDimSize; } } } model.fudgeFactor = model.octreeMaxDimSize * 10e-6; const minSideSize = model.octreeMaxDimSize * 10e-2; for (i = 0; i < 3; i++) { if (dimDiff[i] < minSideSize) { // case (1) above bbIndex = 2 * i; const tempVal = tmpBbox[bbIndex]; tmpBbox[bbIndex] = tmpBbox[bbIndex + 1] - minSideSize; tmpBbox[bbIndex + 1] = tempVal + minSideSize; } else { // case (2) above tmpBbox[2 * i] -= model.fudgeFactor; } } // init the octree with an empty leaf node model.octreeRootNode = vtkIncrementalOctreeNode.newInstance(); ++model.numberOfNodes; // this call internally inits the middle (center) and data range, too model.octreeRootNode.setBounds(...tmpBbox); return true; }; publicAPI.findClosestPointInSphereWithTolerance = ( point, radius2, maskNode ) => publicAPI.findClosestPointInSphere( point, radius2, maskNode, model.octreeMaxDimSize * model.octreeMaxDimSize * 4.0, radius2 ); //------------------------------------------------------------------------------ publicAPI.findDuplicateFloatTypePointInVisitedLeafNode = ( leafNode, point ) => { let tmpPnt; let tmpIdx = -1; let pntIdx = -1; // float thePnt[3]; // TODO // thePnt[0] = static_cast<float>(point[0]); // thePnt[1] = static_cast<float>(point[1]); // thePnt[2] = static_cast<float>(point[2]); const idList = leafNode.getPointIdSet(); // float* pFloat = (static_cast<vtkFloatArray*>(model.locatorPoints.getData())).getPointer(0); const values = model.locatorPoints.getData(); for (let i = 0; i < idList.length; i++) { tmpIdx = idList[i]; // eslint-disable-next-line no-bitwise tmpPnt = (tmpIdx << 1) + tmpIdx; if ( point[0] === values[tmpPnt] && point[1] === values[tmpPnt + 1] && point[2] === values[tmpPnt + 2] ) { pntIdx = tmpIdx; break; } } return pntIdx; }; //------------------------------------------------------------------------------ publicAPI.findDuplicateDoubleTypePointInVisitedLeafNode = ( leafNode, point ) => { let tmpPnt; let tmpIdx = -1; let pntIdx = -1; const idList = leafNode.getPointIdSet(); const values = model.locatorPoints.getData(); for (let i = 0; i < idList.length; i++) { tmpIdx = idList[i]; // eslint-disable-next-line no-bitwise tmpPnt = (tmpIdx << 1) + tmpIdx; if ( point[0] === values[tmpPnt] && point[1] === values[tmpPnt + 1] && point[2] === values[tmpPnt + 2] ) { pntIdx = tmpIdx; break; } } return pntIdx; }; //------------------------------------------------------------------------------ publicAPI.findDuplicatePointInLeafNode = (leafNode, point) => { if (leafNode.getPointIdSet() == null) { return -1; } return model.locatorPoints.getDataType() === VtkDataTypes.FLOAT ? publicAPI.findDuplicateFloatTypePointInVisitedLeafNode(leafNode, point) : publicAPI.findDuplicateDoubleTypePointInVisitedLeafNode( leafNode, point ); }; //------------------------------------------------------------------------------ publicAPI.insertPoint = (ptId, x) => { const leafcontainer = getLeafContainer(model.octreeRootNode, x); ({ numberOfNodes: model.numberOfNodes } = leafcontainer.insertPoint( model.locatorPoints, x, model.maxPointsPerLeaf, ptId, 1, model.numberOfNodes )); }; //------------------------------------------------------------------------------ publicAPI.insertUniquePoint = (point) => { // TODO: We have a mix of let and const here. // eslint-disable-next-line prefer-const let { pointIdx, leafContainer } = publicAPI.isInsertedPoint(point); if (pointIdx > -1) { return { success: false, idx: pointIdx }; } // TODO: pointIdx let numberOfNodes; // eslint-disable-next-line prefer-const ({ numberOfNodes, pointIdx } = leafContainer.insertPoint( model.locatorPoints, point, model.maxPointsPerLeaf, pointIdx, 2, model.numberOfNodes )); model.numberOfNodes = numberOfNodes; return { success: true, idx: pointIdx }; }; //------------------------------------------------------------------------------ publicAPI.insertNextPoint = (x) => { const leafContainer = getLeafContainer(model.octreeRootNode, x); const { numberOfNodes, pointIdx } = leafContainer.insertPoint( model.locatorPoints, x, model.maxPointsPerLeaf, -1, 2, model.numberOfNodes ); model.numberOfNodes = numberOfNodes; return pointIdx; }; //------------------------------------------------------------------------------ publicAPI.isInsertedPointForZeroTolerance = (x) => { // the target leaf node always exists there since the root node of the // octree has been initialized to cover all possible points to be inserted // and therefore we do not need to check it here const leafContainer = getLeafContainer(model.octreeRootNode, x); const pointIdx = publicAPI.findDuplicatePointInLeafNode(leafContainer, x); return { pointIdx, leafContainer }; }; //------------------------------------------------------------------------------ publicAPI.isInsertedPointForNonZeroTolerance = (x) => { // minDist2 // min distance to ALL existing points // elseDst2 // min distance to other nodes (inner boundaries) let dist2Ext; // min distance to an EXTended set of nodes let pntIdExt; // the target leaf node always exists there since the root node of the // octree has been initialized to cover all possible points to be inserted // and therefore we do not need to check it here const leafContainer = getLeafContainer(model.octreeRootNode, x); let [pointIdx, minDist2] = publicAPI.findClosestPointInLeafNode( leafContainer, x ); if (minDist2 === 0.0) { return { pointIdx, leafContainer }; } // As no any 'duplicate' point exists in this leaf node, we need to expand // the search scope to capture possible closer points in other nodes. const elseDst2 = leafContainer.getDistance2ToInnerBoundary( x, model.octreeRootNode ); if (elseDst2 < model.insertTolerance2) { // one or multiple closer points might exist in the neighboring nodes // TODO: dist2Ext pntIdExt = publicAPI.findClosestPointInSphereWithTolerance( x, model.insertTolerance2, leafContainer, dist2Ext ); if (dist2Ext < minDist2) { minDist2 = dist2Ext; pointIdx = pntIdExt; } } pointIdx = minDist2 <= model.insertTolerance2 ? pointIdx : -1; return { pointIdx, leafContainer }; }; //------------------------------------------------------------------------------ publicAPI.isInsertedPoint = (x, leafContainer) => model.insertTolerance2 === 0.0 ? publicAPI.isInsertedPointForZeroTolerance(x, leafContainer) : publicAPI.isInsertedPointForNonZeroTolerance(x, leafContainer); } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- function defaultValues(initialValues) { return { fudgeFactor: 0, octreeMaxDimSize: 0, buildCubicOctree: false, maxPointsPerLeaf: 128, insertTolerance2: 0.000001, locatorPoints: null, octreeRootNode: null, numberOfNodes: 0, ...initialValues, }; } // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { vtkAbstractPointLocator.extend( publicAPI, model, defaultValues(initialValues) ); // Make this a VTK object macro.obj(publicAPI, model); macro.setGet(publicAPI, model, [ 'fudgeFactor', 'octreeMaxDimSize', 'buildCubicOctree', 'maxPointsPerLeaf', 'insertTolerance2', 'locatorPoints', 'octreeRootNode', 'numberOfNodes', ]); // Object specific methods vtkIncrementalOctreePointLocator(publicAPI, model); } // ---------------------------------------------------------------------------- export const newInstance = macro.newInstance( extend, 'vtkIncrementalOctreePointLocator' ); // ---------------------------------------------------------------------------- export default { newInstance, extend }; |