All files / Sources/Common/DataModel/IncrementalOctreeNode index.js

8.94% Statements 28/313
0% Branches 0/136
9.52% Functions 2/21
9.18% Lines 28/305

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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815      1x   1x                           1x                                                                                           1x     1x                 1x                                           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 * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
const { vtkErrorMacro } = macro;
 
const OCTREENODE_INSERTPOINT = [
  (points, pointIdx, coords) => pointIdx,
  (points, pointIdx, coords) => {
    points.setTuple(pointIdx, coords);
    return pointIdx;
  },
  (points, pointIdx, coords) => points.insertNextTuple(coords),
];
 
// Given the index (0 ~ 7) of a child node, the spatial bounding axis (0 ~ 2
// for x, y, and z), and the value (0 ~ 1 for min and max) to access, this LUT
// allows for rapid assignment of its spatial bounding box --- MinBounds[3]
// and MaxBounds[3], with each specific value or entry of this LUT pointing to
// MinBounds[3] for 0, center point for 1, or MaxBounds[3] for 2.
const OCTREE_CHILD_BOUNDS_LUT = [
  [
    [0, 1],
    [0, 1],
    [0, 1],
  ],
  [
    [1, 2],
    [0, 1],
    [0, 1],
  ],
  [
    [0, 1],
    [1, 2],
    [0, 1],
  ],
  [
    [1, 2],
    [1, 2],
    [0, 1],
  ],
 
  [
    [0, 1],
    [0, 1],
    [1, 2],
  ],
  [
    [1, 2],
    [0, 1],
    [1, 2],
  ],
  [
    [0, 1],
    [1, 2],
    [1, 2],
  ],
  [
    [1, 2],
    [1, 2],
    [1, 2],
  ],
];
 
function vtkIncrementalOctreeNode(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkIncrementalOctreeNode');
 
  //------------------------------------------------------------------------------
  publicAPI.createPointIdSet = (initSize, growSize) => {
    if (model.pointIdSet == null) {
      model.pointIdSet = [];
      // TODO: use initSize and growSize.
      // model.pointIdSet.allocate(initSize, growSize);
    }
  };
 
  //------------------------------------------------------------------------------
  publicAPI.setBounds = (x1, x2, y1, y2, z1, z2) => {
    if (model.minBounds == null) model.minBounds = [];
    if (model.maxBounds == null) model.maxBounds = [];
    if (model.minDataBounds == null) model.minDataBounds = [];
    if (model.maxDataBounds == null) model.maxDataBounds = [];
 
    model.minBounds[0] = x1;
    model.maxBounds[0] = x2;
    model.minBounds[1] = y1;
    model.maxBounds[1] = y2;
    model.minBounds[2] = z1;
    model.maxBounds[2] = z2;
 
    model.minDataBounds[0] = x2;
    model.maxDataBounds[0] = x1;
    model.minDataBounds[1] = y2;
    model.maxDataBounds[1] = y1;
    model.minDataBounds[2] = z2;
    model.maxDataBounds[2] = z1;
  };
 
  //------------------------------------------------------------------------------
  publicAPI.getBounds = (bounds) => {
    bounds[0] = model.minBounds[0];
    bounds[1] = model.maxBounds[0];
    bounds[2] = model.minBounds[1];
    bounds[3] = model.maxBounds[1];
    bounds[4] = model.minBounds[2];
    bounds[5] = model.maxBounds[2];
  };
 
  publicAPI.getChildIndex = (point) =>
    Number(point[0] > model.children[0].getMaxBoundsByReference()[0]) +
    // eslint-disable-next-line no-bitwise
    (Number(point[1] > model.children[0].getMaxBoundsByReference()[1]) << 1) +
    // eslint-disable-next-line no-bitwise
    (Number(point[2] > model.children[0].getMaxBoundsByReference()[2]) << 2);
 
  publicAPI.containsPoint = (pnt) =>
    model.minBounds[0] < pnt[0] &&
    pnt[0] <= model.maxBounds[0] &&
    model.minBounds[1] < pnt[1] &&
    pnt[1] <= model.maxBounds[1] &&
    model.minBounds[2] < pnt[2] &&
    pnt[2] <= model.maxBounds[2]
      ? 1
      : 0;
 
  publicAPI.containsPointByData = (pnt) =>
    model.minDataBounds[0] <= pnt[0] &&
    pnt[0] <= model.maxDataBounds[0] &&
    model.minDataBounds[1] <= pnt[1] &&
    pnt[1] <= model.maxDataBounds[1] &&
    model.minDataBounds[2] <= pnt[2] &&
    pnt[2] <= model.maxDataBounds[2]
      ? 1
      : 0;
 
  //------------------------------------------------------------------------------
  publicAPI.updateCounterAndDataBounds = (point, nHits, updateData) => {
    model.numberOfPoints += nHits;
 
    if (!updateData) return false;
 
    let updated = false;
 
    if (point[0] < model.minDataBounds[0]) {
      updated = true;
      model.minDataBounds[0] = point[0];
    }
    if (point[0] > model.maxDataBounds[0]) {
      updated = true;
      model.maxDataBounds[0] = point[0];
    }
 
    if (point[1] < model.minDataBounds[1]) {
      updated = true;
      model.minDataBounds[1] = point[1];
    }
    if (point[1] > model.maxDataBounds[1]) {
      updated = true;
      model.maxDataBounds[1] = point[1];
    }
 
    if (point[2] < model.minDataBounds[2]) {
      updated = true;
      model.minDataBounds[2] = point[2];
    }
    if (point[2] > model.maxDataBounds[2]) {
      updated = true;
      model.maxDataBounds[2] = point[2];
    }
 
    return updated;
  };
 
  //------------------------------------------------------------------------------
  publicAPI.updateCounterAndDataBoundsRecursively = (
    point,
    nHits,
    updateData,
    endNode
  ) => {
    const updated = publicAPI.updateCounterAndDataBounds(
      point,
      nHits,
      updateData
    );
 
    return model.parent === endNode
      ? updated
      : model.parent.updateCounterAndDataBoundsRecursively(
          point,
          nHits,
          updated,
          endNode
        );
  };
 
  //------------------------------------------------------------------------------
  publicAPI.containsDuplicatePointsOnly = (point) =>
    model.minDataBounds[0] === point[0] &&
    point[0] === model.maxDataBounds[0] &&
    model.minDataBounds[1] === point[1] &&
    point[1] === model.maxDataBounds[1] &&
    model.minDataBounds[2] === point[2] &&
    point[2] === model.maxDataBounds[2];
 
  //------------------------------------------------------------------------------
  publicAPI.isLeaf = () => model.children == null;
 
  //------------------------------------------------------------------------------
  publicAPI.getChild = (i) => model.children[i];
 
  //------------------------------------------------------------------------------
  /* eslint-disable no-use-before-define */
  publicAPI.separateExactlyDuplicatePointsFromNewInsertion = (
    points,
    pntIds,
    newPnt,
    pntIdx,
    maxPts,
    ptMode
  ) => {
    // the number of points already maintained in this leaf node
    // >= maxPts AND all of them are exactly duplicate with one another
    //           BUT the new point is not a duplicate of them any more
    let pointIdx = pntIdx;
    let i;
    const dupPnt = [0.0, 0.0, 0.0];
    const octMin = [0.0, 0.0, 0.0];
    const octMid = [0.0, 0.0, 0.0];
    const octMax = [0.0, 0.0, 0.0];
    const boxPtr = [null, null, null];
    let ocNode = null;
    let duplic = publicAPI;
    let single = publicAPI;
 
    // the coordinate of the duplicate points: note pntIds == model.pointIdSet
    points.getPoint(pntIds[0], dupPnt);
 
    while (duplic === single) {
      // as long as separation has not been achieved
      // update the current (in recursion) node and access the bounding box info
      ocNode = duplic;
      octMid[0] = (ocNode.minBounds[0] + ocNode.maxBounds[0]) * 0.5;
      octMid[1] = (ocNode.minBounds[1] + ocNode.maxBounds[1]) * 0.5;
      octMid[2] = (ocNode.minBounds[2] + ocNode.maxBounds[2]) * 0.5;
      boxPtr[0] = ocNode.minBounds;
      boxPtr[1] = octMid;
      boxPtr[2] = ocNode.maxBounds;
 
      // create eight child nodes
      // FIXME: May be too slow to use vtk newInstance()
      ocNode.children = [
        newInstance(),
        newInstance(),
        newInstance(),
        newInstance(),
        newInstance(),
        newInstance(),
        newInstance(),
        newInstance(),
      ];
      for (i = 0; i < 8; i++) {
        // x-bound: axis 0
        octMin[0] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][0][0]][0];
        octMax[0] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][0][1]][0];
 
        // y-bound: axis 1
        octMin[1] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][1][0]][1];
        octMax[1] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][1][1]][1];
 
        // z-bound: axis 2
        octMin[2] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][2][0]][2];
        octMax[2] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][2][1]][2];
 
        ocNode.children[i] = newInstance();
        ocNode.children[i].setParent(ocNode);
        ocNode.children[i].setBounds(
          octMin[0],
          octMax[0],
          octMin[1],
          octMax[1],
          octMin[2],
          octMax[2]
        );
      }
 
      // determine the leaf node of the duplicate points & that of the new point
      duplic = ocNode.children[ocNode.getChildIndex(dupPnt)];
      single = ocNode.children[ocNode.getChildIndex(newPnt)];
    }
    // Now the duplicate points have been separated from the new point //
 
    // create a vtkIdList object for the new point
    // update the counter and the data bounding box until the root node
    // (including the root node)
    pointIdx = OCTREENODE_INSERTPOINT[ptMode](points, pointIdx, newPnt);
    // eslint-disable-next-line no-bitwise
    single.createPointIdSet(maxPts >> 2, maxPts >> 1);
    single.getPointIdSet().push(pointIdx);
    single.updateCounterAndDataBoundsRecursively(newPnt, 1, 1, null);
 
    // We just need to reference pntIds while un-registering it from 'this'.
    // This avoids deep-copying point ids from pntIds to duplic's PointIdSet.
    // update the counter and the data bounding box, but until 'this' node
    // (excluding 'this' node)
    duplic.setPointIdSet(pntIds);
    duplic.updateCounterAndDataBoundsRecursively(
      dupPnt,
      pntIds.length,
      1,
      publicAPI
    );
    return pointIdx;
  };
  /* eslint-enable no-use-before-define */
 
  //------------------------------------------------------------------------------
  publicAPI.createChildNodes = (
    points,
    pntIds,
    newPnt,
    pntIdx,
    maxPts,
    ptMode,
    numberOfNodes
  ) => {
    // There are two scenarios for which this function is invoked.
    //
    // (1) the number of points already maintained in this leaf node
    //     == maxPts AND not all of them are exactly duplicate
    //               AND the new point is not a duplicate of them all
    // (2) the number of points already maintained in this leaf node
    //     >= maxPts AND all of them are exactly duplicate with one another
    //               BUT the new point is not a duplicate of them any more
 
    // address case (2) first if necessary
    let nbNodes = numberOfNodes;
    let pointIdx = pntIdx;
    const sample = [];
    points.getPoint(pntIds[0], sample);
    if (publicAPI.containsDuplicatePointsOnly(sample)) {
      pointIdx = publicAPI.separateExactlyDuplicatePointsFromNewInsertion(
        points,
        pntIds,
        newPnt,
        pointIdx,
        maxPts,
        ptMode
      );
      return { success: false, nbNodes, pointIdx };
    }
 
    // then address case (1) below
    let i;
    let target;
    let dvidId = -1; // index of the sub-dividing octant, if any
    let fullId = -1; // index of the full octant, if any
    const numIds = [0, 0, 0, 0, 0, 0, 0, 0];
    const octMin = [];
    const octMax = [];
    const tempPt = [];
    let tempId;
 
    const octMid = [
      (model.minBounds[0] + model.maxBounds[0]) * 0.5,
      (model.minBounds[1] + model.maxBounds[1]) * 0.5,
      (model.minBounds[2] + model.maxBounds[2]) * 0.5,
    ];
    const boxPtr = [model.minBounds, octMid, model.maxBounds];
 
    // create eight child nodes
    model.children = [];
    for (i = 0; i < 8; i++) {
      // x-bound: axis 0
      octMin[0] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][0][0]][0];
      octMax[0] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][0][1]][0];
 
      // y-bound: axis 1
      octMin[1] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][1][0]][1];
      octMax[1] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][1][1]][1];
 
      // z-bound: axis 2
      octMin[2] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][2][0]][2];
      octMax[2] = boxPtr[OCTREE_CHILD_BOUNDS_LUT[i][2][1]][2];
 
      // This call internally sets the cener and default data bounding box, too.
      // eslint-disable-next-line no-use-before-define
      model.children[i] = newInstance();
      // model.children[i].iD = nbNodes++;
      model.children[i].setParent(publicAPI);
      model.children[i].setBounds(
        octMin[0],
        octMax[0],
        octMin[1],
        octMax[1],
        octMin[2],
        octMax[2]
      );
 
      // allocate a list of point-indices (size = 2^n) for index registration
      // eslint-disable-next-line no-bitwise
      model.children[i].createPointIdSet(maxPts >> 2, maxPts >> 1);
    }
    boxPtr[0] = null;
    boxPtr[1] = null;
    boxPtr[2] = null;
 
    // distribute the available point-indices to the eight child nodes
    for (i = 0; i < maxPts; i++) {
      tempId = pntIds[i];
      points.getPoint(tempId, tempPt);
      target = publicAPI.getChildIndex(tempPt);
      model.children[target].getPointIdSet().push(tempId);
      model.children[target].updateCounterAndDataBounds(tempPt);
      numIds[target]++;
    }
 
    // locate the full child, just if any
    for (i = 0; i < 8; i++) {
      if (numIds[i] === maxPts) {
        fullId = i;
        break;
      }
    }
 
    target = publicAPI.getChildIndex(newPnt);
    if (fullId === target) {
      // The fact is that we are going to insert the new point to an already
      // full octant (child node). Thus we need to further divide this child
      // to avoid the overflow problem.
      ({ numberOfNodes: nbNodes, pointIdx } = model.children[
        target
      ].createChildNodes(
        points,
        pntIds,
        newPnt,
        pointIdx,
        maxPts,
        ptMode,
        nbNodes
      ));
      dvidId = fullId;
    } else {
      // the initial division is a success
      pointIdx = OCTREENODE_INSERTPOINT[ptMode](points, pointIdx, newPnt);
      model.children[target].getPointIdSet().push(pointIdx);
      model.children[target].updateCounterAndDataBoundsRecursively(
        newPnt,
        1,
        1,
        null
      );
 
      // NOTE: The counter below might reach the threshold, though we delay the
      // sub-division of this child node until the next point insertion occurs.
      numIds[target]++;
    }
 
    // Now it is time to reclaim those un-used vtkIdList objects, of which each
    // either is empty or still needs to be deleted due to further division of
    // the child node. This post-deallocation of the un-used vtkIdList objects
    // (of some child nodes) is based on the assumption that retrieving the
    // 'maxPts' points from vtkPoints and the associated 'maxPts' point-indices
    // from vtkIdList is more expensive than reclaiming at most 8 vtkIdList
    // objects at hand.
    for (i = 0; i < 8; i++) {
      if (numIds[i] === 0 || i === dvidId) {
        model.children[i].getPointIdSet().length = 0;
      }
    }
 
    // notify vtkIncrementalOctreeNode::InsertPoint() to destroy pntIds
    return { success: true, numberOfNodes: nbNodes, pointIdx };
  };
 
  //------------------------------------------------------------------------------
  publicAPI.insertPoint = (
    points,
    newPnt,
    maxPts,
    pntId,
    ptMode,
    numberOfNodes
  ) => {
    let nbNodes = 0;
    let pointIdx = pntId;
 
    if (model.pointIdSet) {
      // there has been at least one point index
      if (
        model.pointIdSet.length < maxPts ||
        publicAPI.containsDuplicatePointsOnly(newPnt)
      ) {
        // this leaf node is not full or
        // this leaf node is full, but of all exactly duplicate points
        // and the point under check is another duplicate of these points
        pointIdx = OCTREENODE_INSERTPOINT[ptMode](points, pointIdx, newPnt);
        model.pointIdSet.push(pointIdx);
        publicAPI.updateCounterAndDataBoundsRecursively(newPnt, 1, 1, null);
      } else {
        // overflow: divide this node and delete the list of point-indices.
        // Note that the number of exactly duplicate points might be greater
        // than or equal to maxPts.
        ({ numberOfNodes: nbNodes, pointIdx } = publicAPI.createChildNodes(
          points,
          model.pointIdSet,
          newPnt,
          pointIdx,
          maxPts,
          ptMode,
          numberOfNodes
        ));
        model.pointIdSet = null;
      }
    } else {
      // There has been no any point index registered in this leaf node
      pointIdx = OCTREENODE_INSERTPOINT[ptMode](points, pointIdx, newPnt);
      model.pointIdSet = [];
      model.pointIdSet.push(pointIdx);
      publicAPI.updateCounterAndDataBoundsRecursively(newPnt, 1, 1, null);
    }
 
    return { numberOfNodes: numberOfNodes + nbNodes, pointIdx };
  };
 
  //------------------------------------------------------------------------------
  publicAPI.getDistance2ToBoundary = (
    point,
    closest,
    innerOnly,
    rootNode,
    checkData
  ) => {
    // It is mandatory that GetMinDataBounds() and GetMaxDataBounds() be used.
    // Direct access to MinDataBounds and MaxDataBounds might incur problems.
    let thisMin = null;
    let thisMax = null;
    let rootMin = null;
    let rootMax = null;
    // TODO: Check
    // let minDist = VTK_DOUBLE_MAX;
    let minDist = Number.MAX_VALUE; // minimum distance to the boundaries
    if (checkData) {
      thisMin = publicAPI.getMinDataBounds();
      thisMax = publicAPI.getMaxDataBounds();
      rootMin = rootNode.getMinDataBounds();
      rootMax = rootNode.getMaxDataBounds();
    } else {
      thisMin = model.minBounds;
      thisMax = model.maxBounds;
      rootMin = rootNode.getMinBounds();
      rootMax = rootNode.getMaxBounds();
    }
 
    let minFace = 0; // index of the face with min distance to the point
    const beXless = Number(point[0] < thisMin[0]);
    const beXmore = Number(point[0] > thisMax[0]);
    const beYless = Number(point[1] < thisMin[1]);
    const beYmore = Number(point[1] > thisMax[1]);
    const beZless = Number(point[2] < thisMin[2]);
    const beZmore = Number(point[2] > thisMax[2]);
    const withinX = Number(!beXless && !beXmore);
    const withinY = Number(!beYless && !beYmore);
    const withinZ = Number(!beZless && !beZmore);
    // eslint-disable-next-line no-bitwise
    const xyzFlag = (withinZ << 2) + (withinY << 1) + withinX;
 
    switch (xyzFlag) {
      case 0: {
        // withinZ = 0; withinY = 0;  withinX = 0
        // closest to a corner
 
        closest[0] = beXless ? thisMin[0] : thisMax[0];
        closest[1] = beYless ? thisMin[1] : thisMax[1];
        closest[2] = beZless ? thisMin[2] : thisMax[2];
        minDist = vtkMath.distance2BetweenPoints(point, closest);
        break;
      }
 
      case 1: {
        // withinZ = 0; withinY = 0; withinX = 1
        // closest to an x-aligned edge
 
        closest[0] = point[0];
        closest[1] = beYless ? thisMin[1] : thisMax[1];
        closest[2] = beZless ? thisMin[2] : thisMax[2];
        minDist = vtkMath.distance2BetweenPoints(point, closest);
        break;
      }
 
      case 2: {
        // withinZ = 0; withinY = 1; withinX = 0
        // closest to a y-aligned edge
 
        closest[0] = beXless ? thisMin[0] : thisMax[0];
        closest[1] = point[1];
        closest[2] = beZless ? thisMin[2] : thisMax[2];
        minDist = vtkMath.distance2BetweenPoints(point, closest);
        break;
      }
 
      case 3: {
        // withinZ = 0; withinY = 1; withinX = 1
        // closest to a z-face
 
        if (beZless) {
          minDist = thisMin[2] - point[2];
          closest[2] = thisMin[2];
        } else {
          minDist = point[2] - thisMax[2];
          closest[2] = thisMax[2];
        }
 
        minDist *= minDist;
        closest[0] = point[0];
        closest[1] = point[1];
        break;
      }
 
      case 4: {
        // withinZ = 1; withinY = 0; withinX = 0
        // cloest to a z-aligned edge
 
        closest[0] = beXless ? thisMin[0] : thisMax[0];
        closest[1] = beYless ? thisMin[1] : thisMax[1];
        closest[2] = point[2];
        minDist = vtkMath.distance2BetweenPoints(point, closest);
        break;
      }
 
      case 5: {
        // withinZ = 1; withinY = 0; withinX = 1
        // closest to a y-face
 
        if (beYless) {
          minDist = thisMin[1] - point[1];
          closest[1] = thisMin[1];
        } else {
          minDist = point[1] - thisMax[1];
          closest[1] = thisMax[1];
        }
 
        minDist *= minDist;
        closest[0] = point[0];
        closest[2] = point[2];
        break;
      }
 
      case 6: {
        // withinZ = 1; withinY = 1; withinX = 0
        // closest to an x-face
 
        if (beXless) {
          minDist = thisMin[0] - point[0];
          closest[0] = thisMin[0];
        } else {
          minDist = point[0] - thisMax[0];
          closest[0] = thisMax[0];
        }
 
        minDist *= minDist;
        closest[1] = point[1];
        closest[2] = point[2];
        break;
      }
 
      case 7: {
        // withinZ = 1; withinY = 1;  withinZ = 1
        // point is inside the box
 
        if (innerOnly) {
          // check only inner boundaries
          let faceDst;
 
          faceDst = point[0] - thisMin[0]; // x-min face
          if (thisMin[0] !== rootMin[0] && faceDst < minDist) {
            minFace = 0;
            minDist = faceDst;
          }
 
          faceDst = thisMax[0] - point[0]; // x-max face
          if (thisMax[0] !== rootMax[0] && faceDst < minDist) {
            minFace = 1;
            minDist = faceDst;
          }
 
          faceDst = point[1] - thisMin[1]; // y-min face
          if (thisMin[1] !== rootMin[1] && faceDst < minDist) {
            minFace = 2;
            minDist = faceDst;
          }
 
          faceDst = thisMax[1] - point[1]; // y-max face
          if (thisMax[1] !== rootMax[1] && faceDst < minDist) {
            minFace = 3;
            minDist = faceDst;
          }
 
          faceDst = point[2] - thisMin[2]; // z-min face
          if (thisMin[2] !== rootMin[2] && faceDst < minDist) {
            minFace = 4;
            minDist = faceDst;
          }
 
          faceDst = thisMax[2] - point[2]; // z-max face
          if (thisMax[2] !== rootMax[2] && faceDst < minDist) {
            minFace = 5;
            minDist = faceDst;
          }
        } else {
          // check all boundaries
          const tmpDist = [
            point[0] - thisMin[0],
            thisMax[0] - point[0],
            point[1] - thisMin[1],
            thisMax[1] - point[1],
            point[2] - thisMin[2],
            thisMax[2] - point[2],
          ];
 
          for (let i = 0; i < 6; i++) {
            if (tmpDist[i] < minDist) {
              minFace = i;
              minDist = tmpDist[i];
            }
          }
        }
 
        // no square operation if no any inner boundary
        if (minDist !== Number.MAX_VALUE) {
          minDist *= minDist;
        }
 
        closest[0] = point[0];
        closest[1] = point[1];
        closest[2] = point[2];
 
        // minFace: the quad with the min distance to the point
        // 0: x-min face  ===>  xyzIndx = 0:  x  and  minFace & 1 = 0:  thisMin
        // 1: x-max face  ===>  xyzIndx = 0:  x  and  minFace & 1 = 1:  thisMax
        // 2: y-min face  ===>  xyzIndx = 1:  y  and  minFace & 1 = 0:  thisMin
        // 3: y-max face  ===>  xyzIndx = 1:  y  and  minFace & 1 = 1:  thisMax
        // 4: z-min face  ===>  xyzIndx = 2:  z  and  minFace & 1 = 0:  thisMin
        // 5: z-max face  ===>  xyzIndx = 2:  z  and  minFace & 1 = 1:  thisMax
        const pMinMax = [thisMin, thisMax];
        // eslint-disable-next-line no-bitwise
        const xyzIndx = minFace >> 1;
        // eslint-disable-next-line no-bitwise
        closest[xyzIndx] = pMinMax[minFace & 1][xyzIndx];
 
        break;
      }
      default:
        vtkErrorMacro('unexpected case in getDistance2ToBoundary');
    }
 
    return minDist;
  };
 
  //------------------------------------------------------------------------------
  publicAPI.getDistance2ToInnerBoundary = (point, rootNode) => {
    const dummy = [];
    return publicAPI.getDistance2ToBoundary(point, dummy, 0, rootNode, 0);
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  pointIdSet: null,
  minBounds: null,
  maxBounds: null,
  minDataBounds: null,
  maxDataBounds: null,
  parent: null,
  children: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  macro.setGetArray(
    publicAPI,
    model,
    ['minBounds', 'maxBounds', 'minDataBounds', 'maxDataBounds'],
    6
  );
 
  macro.get(publicAPI, model, ['pointIdSet', 'numberOfPoints']);
 
  // TODO: No get?
  macro.set(publicAPI, model, ['parent']);
 
  // Object specific methods
  vtkIncrementalOctreeNode(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkIncrementalOctreeNode'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };