All files / Sources/Filters/General/TubeFilter index.js

67.44% Statements 346/513
60.41% Branches 87/144
81.81% Functions 9/11
68.43% Lines 323/472

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 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915                        1x 1x               12x     32x 32x 32x         32x   10x   32x       46x 46x 46x 46x 30x 30x 30x 30x       16x       16x 16x   16x 16x 16x 16x     16x 16x 16x 16x   16x 16x 16x   16x 16x 16x 16x 48x 16x     16x       16x 20x 20x 11x 11x 11x 11x 11x 33x   11x           11x 11x 11x 7x 7x 7x         16x     9x 18x 5x 5x 5x 5x           16x     16x 16x 10x 10x 7x     3x 3x 3x 3x 3x 9x   3x     3x 3x 3x   3x 3x 3x                                                                   16x 46x                   16x                                       16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x   46x 16x 16x 16x 48x 48x 48x   16x 30x 16x 48x 48x 48x   16x   14x 42x   14x 14x 42x 42x       46x         46x 138x     46x   46x             46x 46x               46x 46x     46x 4x         42x                   42x       4x 4x             46x 46x 502x 1506x   1506x 1506x 1506x   502x 502x                                                                   16x 5x 5x 5x           5x 49x 49x 147x 147x   49x 49x       5x 5x     5x 49x 49x 147x 147x   49x 49x       16x                       16x 16x 16x 16x 16x 16x 16x 16x 33x 16x   17x 17x   16x 16x 16x       165x 165x 165x 165x 502x 502x 502x   165x                                               16x 5x 5x   5x         5x 5x 5x 5x 5x         39x 18x 18x 18x   21x 21x 21x     5x     5x 5x 5x 5x 5x         39x 18x 18x 18x   21x 21x 21x     5x   16x                                                                                                                                                                                       12x     12x 12x   12x 12x           12x 12x     12x 12x     12x 12x     12x 12x       12x 12x 12x 12x 12x 16x   16x 16x   16x 5x       12x 12x 1x 11x 1x   12x         12x 12x 12x         12x 12x 12x   12x 12x 12x 12x 12x 12x         12x           12x         12x 12x 12x 12x 10x 10x           10x       12x 12x 4x   12x 12x 2x 2x           2x     12x 12x 12x 12x     3x           3x 3x               12x 12x 12x       12x 12x 12x   12x 12x     12x 3x       12x 12x                             12x       12x 12x 12x 12x 12x 16x 16x 16x   16x 16x 16x       16x                                     16x                   16x                               16x 16x     12x 12x 12x 12x 12x               1x                                     12x     12x                                 12x     12x     12x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
import { DesiredOutputPrecision } from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants';
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
 
import Constants from './Constants';
 
const { VaryRadius, GenerateTCoords } = Constants;
const { vtkDebugMacro, vtkErrorMacro, vtkWarningMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkTubeFilter methods
// ----------------------------------------------------------------------------
 
function vtkTubeFilter(publicAPI, model) {
  // Set our classname
  model.classHierarchy.push('vtkTubeFilter');
 
  function computeOffset(offset, npts) {
    let newOffset = offset;
    if (model.sidesShareVertices) {
      newOffset += model.numberOfSides * npts;
    } else E{
      // points are duplicated
      newOffset += 2 * model.numberOfSides * npts;
    }
    if (model.capping) {
      // cap points are duplicated
      newOffset += 2 * model.numberOfSides;
    }
    return newOffset;
  }
 
  function findNextValidSegment(points, pointIds, start) {
    const ptId = pointIds[start];
    const ps = points.slice(3 * ptId, 3 * (ptId + 1));
    let end = start + 1;
    while (end < pointIds.length) {
      const endPtId = pointIds[end];
      const pe = points.slice(3 * endPtId, 3 * (endPtId + 1));
      if (ps !== pe) {
        return end - 1;
      }
      ++end;
    }
    return pointIds.length;
  }
 
  function generateSlidingNormals(pts, lines, normals, firstNormal = null) {
    let normal = [0.0, 0.0, 1.0];
    const lineData = lines;
    // lid = 0;
    let npts = lineData[0];
    for (let i = 0; i < lineData.length; i += npts + 1) {
      npts = lineData[i];
      Iif (npts === 1) {
        // return arbitrary
        normals.setTuple(lineData[i + 1], normal);
      } else if (npts > 1) {
        let sNextId = 0;
        let sPrev = [0, 0, 0];
        const sNext = [0, 0, 0];
 
        const linePts = lineData.slice(i + 1, i + 1 + npts);
        sNextId = findNextValidSegment(pts, linePts, 0);
        if (sNextId !== npts) {
          // at least one valid segment
          let pt1Id = linePts[sNextId];
          let pt1 = pts.slice(3 * pt1Id, 3 * (pt1Id + 1));
          let pt2Id = linePts[sNextId + 1];
          let pt2 = pts.slice(3 * pt2Id, 3 * (pt2Id + 1));
          sPrev = pt2.map((elem, idx) => elem - pt1[idx]);
          vtkMath.normalize(sPrev);
 
          // compute first normal
          Iif (firstNormal) {
            normal = firstNormal;
          } else {
            // find the next valid, non-parallel segment
            while (++sNextId < npts) {
              sNextId = findNextValidSegment(pts, linePts, sNextId);
              if (sNextId !== npts) {
                pt1Id = linePts[sNextId];
                pt1 = pts.slice(3 * pt1Id, 3 * (pt1Id + 1));
                pt2Id = linePts[sNextId + 1];
                pt2 = pts.slice(3 * pt2Id, 3 * (pt2Id + 1));
                for (let j = 0; j < 3; ++j) {
                  sNext[j] = pt2[j] - pt1[j];
                }
                vtkMath.normalize(sNext);
 
                // now the starting normal should simply be the cross product.
                // In the following if statement, we check for the case where
                // the two segments are parallel, in which case, continue
                // searching for the next valid segment
                const n = [0.0, 0.0, 0.0];
                vtkMath.cross(sPrev, sNext, n);
                if (vtkMath.norm(n) > 1.0e-3) {
                  normal = n;
                  sPrev = sNext;
                  break;
                }
              }
            }
 
            if (sNextId >= npts) {
              // only one valid segment
              // a little trick to find orthogonal normal
              for (let j = 0; j < 3; ++j) {
                if (sPrev[j] !== 0.0) {
                  normal[(j + 2) % 3] = 0.0;
                  normal[(j + 1) % 3] = 1.0;
                  normal[j] = -sPrev[(j + 1) % 3] / sPrev[j];
                  break;
                }
              }
            }
          }
 
          vtkMath.normalize(normal);
 
          // compute remaining normals
          let lastNormalId = 0;
          while (++sNextId < npts) {
            sNextId = findNextValidSegment(pts, linePts, sNextId);
            if (sNextId === npts) {
              break;
            }
 
            pt1Id = linePts[sNextId];
            pt1 = pts.slice(3 * pt1Id, 3 * (pt1Id + 1));
            pt2Id = linePts[sNextId + 1];
            pt2 = pts.slice(3 * pt2Id, 3 * (pt2Id + 1));
            for (let j = 0; j < 3; ++j) {
              sNext[j] = pt2[j] - pt1[j];
            }
            vtkMath.normalize(sNext);
 
            // compute rotation vector
            const w = [0.0, 0.0, 0.0];
            vtkMath.cross(sPrev, normal, w);
            if (vtkMath.normalize(w) !== 0.0) {
              // can't use this segment otherwise
              const q = [0.0, 0.0, 0.0];
              vtkMath.cross(sNext, sPrev, q);
              Iif (vtkMath.normalize(q) !== 0.0) {
                // can't use this segment otherwise
                const f1 = vtkMath.dot(q, normal);
                let f2 = 1.0 - f1 * f1;
                if (f2 > 0.0) {
                  f2 = Math.sqrt(f2);
                } else {
                  f2 = 0.0;
                }
                const c = [0, 0, 0];
                for (let j = 0; j < 3; ++j) {
                  c[j] = sNext[j] + sPrev[j];
                }
                vtkMath.normalize(c);
                vtkMath.cross(c, q, w);
                vtkMath.cross(sPrev, q, c);
                if (vtkMath.dot(normal, c) * vtkMath.dot(w, c) < 0.0) {
                  f2 *= -1.0;
                }
 
                // insert current normal before updating
                for (let j = lastNormalId; j < sNextId; ++j) {
                  normals.setTuple(linePts[j], normal);
                }
                lastNormalId = sNextId;
                sPrev = sNext;
 
                // compute next normal
                normal = f1 * q + f2 * w;
              }
            }
          }
 
          // insert last normal for the remaining points
          for (let j = lastNormalId; j < npts; ++j) {
            normals.setTuple(linePts[j], normal);
          }
        } else E{
          // no valid segments
          for (let j = 0; j < npts; ++j) {
            normals.setTuple(linePts[j], normal);
          }
        }
      }
    }
    return 1;
  }
 
  function generatePoints(
    offset,
    npts,
    pts,
    inPts,
    newPts,
    pd,
    outPD,
    newNormals,
    inScalars,
    range,
    inVectors,
    maxSpeed,
    inNormals,
    theta
  ) {
    // Use averaged segment to create beveled effect.
    const sNext = [0.0, 0.0, 0.0];
    const sPrev = [0.0, 0.0, 0.0];
    const startCapNorm = [0.0, 0.0, 0.0];
    const endCapNorm = [0.0, 0.0, 0.0];
    let p = [0.0, 0.0, 0.0];
    let pNext = [0.0, 0.0, 0.0];
    let s = [0.0, 0.0, 0.0];
    let n = [0.0, 0.0, 0.0];
    const w = [0.0, 0.0, 0.0];
    const nP = [0.0, 0.0, 0.0];
    const normal = [0.0, 0.0, 0.0];
    let sFactor = 1.0;
    let ptId = offset;
    const vector = [];
    for (let j = 0; j < npts; ++j) {
      // First point
      if (j === 0) {
        p = inPts.slice(3 * pts[0], 3 * (pts[0] + 1));
        pNext = inPts.slice(3 * pts[1], 3 * (pts[1] + 1));
        for (let i = 0; i < 3; ++i) {
          sNext[i] = pNext[i] - p[i];
          sPrev[i] = sNext[i];
          startCapNorm[i] = -sPrev[i];
        }
        vtkMath.normalize(startCapNorm);
      } else if (j === npts - 1) {
        for (let i = 0; i < 3; ++i) {
          sPrev[i] = sNext[i];
          p[i] = pNext[i];
          endCapNorm[i] = sNext[i];
        }
        vtkMath.normalize(endCapNorm);
      } else {
        for (let i = 0; i < 3; ++i) {
          p[i] = pNext[i];
        }
        pNext = inPts.slice(3 * pts[j + 1], 3 * (pts[j + 1] + 1));
        for (let i = 0; i < 3; ++i) {
          sPrev[i] = sNext[i];
          sNext[i] = pNext[i] - p[i];
        }
      }
 
      Iif (vtkMath.normalize(sNext) === 0.0) {
        vtkWarningMacro('Coincident points!');
        return 0;
      }
 
      for (let i = 0; i < 3; ++i) {
        s[i] = (sPrev[i] + sNext[i]) / 2.0; // average vector
      }
 
      n = inNormals.slice(3 * pts[j], 3 * (pts[j] + 1));
      // if s is zero then just use sPrev cross n
      Iif (vtkMath.normalize(s) === 0.0) {
        vtkMath.cross(sPrev, n, s);
        if (vtkMath.normalize(s) === 0.0) {
          vtkDebugMacro('Using alternate bevel vector');
        }
      }
 
      vtkMath.cross(s, n, w);
      Iif (vtkMath.normalize(w) === 0.0) {
        let msg = 'Bad normal: s = ';
        msg += `${s[0]},  ${s[1]}, ${s[2]}`;
        msg += ` n = ${n[0]},  ${n[1]}, ${n[2]}`;
        vtkWarningMacro(msg);
        return 0;
      }
 
      vtkMath.cross(w, s, nP); // create orthogonal coordinate system
      vtkMath.normalize(nP);
 
      // Compute a scalar factor based on scalars or vectors
      if (inScalars && model.varyRadius === VaryRadius.VARY_RADIUS_BY_SCALAR) {
        sFactor =
          1.0 +
          ((model.radiusFactor - 1.0) *
            (inScalars.getComponent(pts[j], 0) - range[0])) /
            (range[1] - range[0]);
      } else Iif (
        inVectors &&
        model.varyRadius === VaryRadius.VARY_RADIUS_BY_VECTOR
      ) {
        sFactor = Math.sqrt(
          maxSpeed / vtkMath.norm(inVectors.getTuple(pts[j], vector))
        );
        if (sFactor > model.radiusFactor) {
          sFactor = model.radiusFactor;
        }
      } else if (
        inScalars &&
        model.varyRadius === VaryRadius.VARY_RADIUS_BY_ABSOLUTE_SCALAR
      ) {
        sFactor = inScalars.getComponent(pts[j], 0);
        Iif (sFactor < 0.0) {
          vtkWarningMacro('Scalar value less than zero, skipping line');
          return 0;
        }
      }
 
      // create points around line
      if (model.sidesShareVertices) {
        for (let k = 0; k < model.numberOfSides; ++k) {
          for (let i = 0; i < 3; ++i) {
            normal[i] =
              w[i] * Math.cos(k * theta) + nP[i] * Math.sin(k * theta);
            s[i] = p[i] + model.radius * sFactor * normal[i];
            newPts[3 * ptId + i] = s[i];
            newNormals[3 * ptId + i] = normal[i];
          }
          outPD.passData(pd, pts[j], ptId);
          ptId++;
        } // for each side
      } else E{
        const nRight = [0, 0, 0];
        const nLeft = [0, 0, 0];
        for (let k = 0; k < model.numberOfSides; ++k) {
          for (let i = 0; i < 3; ++i) {
            // Create duplicate vertices at each point
            // and adjust the associated normals so that they are
            // oriented with the facets. This preserves the tube's
            // polygonal appearance, as if by flat-shading around the tube,
            // while still allowing smooth (gouraud) shading along the
            // tube as it bends.
            normal[i] =
              w[i] * Math.cos(k * theta) + nP[i] * Math.sin(k * theta);
            nRight[i] =
              w[i] * Math.cos((k - 0.5) * theta) +
              nP[i] * Math.sin((k - 0.5) * theta);
            nLeft[i] =
              w[i] * Math.cos((k + 0.5) * theta) +
              nP[i] * Math.sin((k + 0.5) * theta);
            s[i] = p[i] + model.radius * sFactor * normal[i];
            newPts[3 * ptId + i] = s[i];
            newNormals[3 * ptId + i] = nRight[i];
            newPts[3 * (ptId + 1) + i] = s[i];
            newNormals[3 * (ptId + 1) + i] = nLeft[i];
          }
          outPD.passData(pd, pts[j], ptId + 1);
          ptId += 2;
        } // for each side
      } // else separate vertices
    } // for all points in the polyline
 
    // Produce end points for cap. They are placed at tail end of points.
    if (model.capping) {
      let numCapSides = model.numberOfSides;
      let capIncr = 1;
      Iif (!model.sidesShareVertices) {
        numCapSides = 2 * model.numberOfSides;
        capIncr = 2;
      }
 
      // the start cap
      for (let k = 0; k < numCapSides; k += capIncr) {
        s = newPts.slice(3 * (offset + k), 3 * (offset + k + 1));
        for (let i = 0; i < 3; ++i) {
          newPts[3 * ptId + i] = s[i];
          newNormals[3 * ptId + i] = startCapNorm[i];
        }
        outPD.passData(pd, pts[0], ptId);
        ptId++;
      }
 
      // the end cap
      let endOffset = offset + (npts - 1) * model.numberOfSides;
      Iif (!model.sidesShareVertices) {
        endOffset = offset + 2 * (npts - 1) * model.numberOfSides;
      }
      for (let k = 0; k < numCapSides; k += capIncr) {
        s = newPts.slice(3 * (endOffset + k), 3 * (endOffset + k + 1));
        for (let i = 0; i < 3; ++i) {
          newPts[3 * ptId + i] = s[i];
          newNormals[3 * ptId + i] = endCapNorm[i];
        }
        outPD.passData(pd, pts[npts - 1], ptId);
        ptId++;
      }
    } // if capping
 
    return 1;
  }
 
  function generateStrips(
    offset,
    npts,
    inCellId,
    outCellId,
    inCD,
    outCD,
    newStrips
  ) {
    let i1 = 0;
    let i2 = 0;
    let i3 = 0;
    let newOutCellId = outCellId;
    let outCellIdx = 0;
    const newStripsData = newStrips.getData();
    let cellId = 0;
    while (outCellIdx < newStripsData.length) {
      if (cellId === outCellId) {
        break;
      }
      outCellIdx += newStripsData[outCellIdx] + 1;
      cellId++;
    }
    if (model.sidesShareVertices) {
      for (
        let k = offset;
        k < model.numberOfSides + offset;
        k += model.onRatio
      ) {
        i1 = k % model.numberOfSides;
        i2 = (k + 1) % model.numberOfSides;
        newStripsData[outCellIdx++] = npts * 2;
        for (let i = 0; i < npts; ++i) {
          i3 = i * model.numberOfSides;
          newStripsData[outCellIdx++] = offset + i2 + i3;
          newStripsData[outCellIdx++] = offset + i1 + i3;
        }
        outCD.passData(inCD, inCellId, newOutCellId++);
      } // for each side of the tube
    } else E{
      for (
        let k = offset;
        k < model.numberOfSides + offset;
        k += model.onRatio
      ) {
        i1 = 2 * (k % model.numberOfSides) + 1;
        i2 = 2 * ((k + 1) % model.numberOfSides);
        // outCellId = newStrips.getNumberOfCells(true);
        newStripsData[outCellIdx] = npts * 2;
        outCellIdx++;
        for (let i = 0; i < npts; ++i) {
          i3 = i * 2 * model.numberOfSides;
          newStripsData[outCellIdx++] = offset + i2 + i3;
          newStripsData[outCellIdx++] = offset + i1 + i3;
        }
        outCD.passData(inCD, inCellId, newOutCellId++);
      } // for each side of the tube
    }
 
    // Take care of capping. The caps are n-sided polygons that can be easily
    // triangle stripped.
    if (model.capping) {
      let startIdx = offset + npts * model.numberOfSides;
      let idx = 0;
 
      Iif (!model.sidesShareVertices) {
        startIdx = offset + 2 * npts * model.numberOfSides;
      }
 
      // The start cap
      newStripsData[outCellIdx++] = model.numberOfSides;
      newStripsData[outCellIdx++] = startIdx;
      newStripsData[outCellIdx++] = startIdx + 1;
      let k = 0;
      for (
        i1 = model.numberOfSides - 1, i2 = 2, k = 0;
        k < model.numberOfSides - 2;
        ++k
      ) {
        if (k % 2) {
          idx = startIdx + i2;
          newStripsData[outCellIdx++] = idx;
          i2++;
        } else {
          idx = startIdx + i1;
          newStripsData[outCellIdx++] = idx;
          i1--;
        }
      }
      outCD.passData(inCD, inCellId, newOutCellId++);
 
      // The end cap - reversed order to be consistent with normal
      startIdx += model.numberOfSides;
      newStripsData[outCellIdx++] = model.numberOfSides;
      newStripsData[outCellIdx++] = startIdx;
      newStripsData[outCellIdx++] = startIdx + model.numberOfSides - 1;
      for (
        i1 = model.numberOfSides - 2, i2 = 1, k = 0;
        k < model.numberOfSides - 2;
        ++k
      ) {
        if (k % 2) {
          idx = startIdx + i1;
          newStripsData[outCellIdx++] = idx;
          i1--;
        } else {
          idx = startIdx + i2;
          newStripsData[outCellIdx++] = idx;
          i2++;
        }
      }
      outCD.passData(inCD, inCellId, newOutCellId++);
    }
    return newOutCellId;
  }
 
  function generateTCoords(offset, npts, pts, inPts, inScalars, newTCoords) {
    let numSides = model.numberOfSides;
    if (!model.sidesShareVertices) {
      numSides = 2 * model.numberOfSides;
    }
 
    let tc = 0.0;
    let s0 = 0.0;
    let s = 0.0;
    const inScalarsData = inScalars.getData();
    if (model.generateTCoords === GenerateTCoords.TCOORDS_FROM_SCALARS) {
      s0 = inScalarsData[pts[0]];
      for (let i = 0; i < npts; ++i) {
        s = inScalarsData[pts[i]];
        tc = (s - s0) / model.textureLength;
        for (let k = 0; k < numSides; ++k) {
          const tcy = k / (numSides - 1);
          const tcId = 2 * (offset + i * numSides + k);
          newTCoords[tcId] = tc;
          newTCoords[tcId + 1] = tcy;
        }
      }
    } else if (model.generateTCoords === GenerateTCoords.TCOORDS_FROM_LENGTH) {
      let len = 0.0;
      const xPrev = inPts.slice(3 * pts[0], 3 * (pts[0] + 1));
      for (let i = 0; i < npts; ++i) {
        const x = inPts.slice(3 * pts[i], 3 * (pts[i] + 1));
        len += Math.sqrt(vtkMath.distance2BetweenPoints(x, xPrev));
        tc = len / model.textureLength;
        for (let k = 0; k < numSides; ++k) {
          const tcy = k / (numSides - 1);
          const tcId = 2 * (offset + i * numSides + k);
          newTCoords[tcId] = tc;
          newTCoords[tcId + 1] = tcy;
        }
        for (let k = 0; k < 3; ++k) {
          xPrev[k] = x[k];
        }
      }
    } else if (
      model.generateTCoords === GenerateTCoords.TCOORDS_FROM_NORMALIZED_LENGTH
    ) {
      let len = 0.0;
      let len1 = 0.0;
      let xPrev = inPts.slice(3 * pts[0], 3 * (pts[0] + 1));
      for (let i = 0; i < npts; ++i) {
        const x = inPts.slice(3 * pts[i], 3 * (pts[i] + 1));
        len1 += Math.sqrt(vtkMath.distance2BetweenPoints(x, xPrev));
        for (let k = 0; k < 3; ++k) {
          xPrev[k] = x[k];
        }
      }
      xPrev = inPts.slice(3 * pts[0], 3 * (pts[0] + 1));
      for (let i = 0; i < npts; ++i) {
        const x = inPts.slice(3 * pts[i], 3 * (pts[i] + 1));
        len += Math.sqrt(vtkMath.distance2BetweenPoints(x, xPrev));
        tc = len / len1;
        for (let k = 0; k < numSides; ++k) {
          const tcy = k / (numSides - 1);
          const tcId = 2 * (offset + i * numSides + k);
          newTCoords[tcId] = tc;
          newTCoords[tcId + 1] = tcy;
        }
        for (let k = 0; k < 3; ++k) {
          xPrev[k] = x[k];
        }
      }
    }
 
    // Capping, set the endpoints as appropriate
    if (model.capping) {
      const startIdx = offset + npts * numSides;
 
      // start cap
      for (let ik = 0; ik < model.numberOfSides; ++ik) {
        const tcId = 2 * (startIdx + ik);
        newTCoords[tcId] = 0.0;
        newTCoords[tcId + 1] = 0.0;
      }
 
      // end cap
      for (let ik = 0; ik < model.numberOfSides; ++ik) {
        const tcId = 2 * (startIdx + model.numberOfSides + ik);
        newTCoords[tcId] = 0.0;
        newTCoords[tcId + 1] = 0.0;
      }
    }
  }
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    // pass through for now
    const output = vtkPolyData.newInstance();
    outData[0] = output;
 
    const input = inData[0];
    Iif (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    // Allocate output
    const inPts = input.getPoints();
    Iif (!inPts) {
      return;
    }
    const numPts = inPts.getNumberOfPoints();
    Iif (numPts < 1) {
      return;
    }
    const inLines = input.getLines();
    Iif (!inLines) {
      return;
    }
    const numLines = inLines.getNumberOfCells();
    Iif (numLines < 1) {
      return;
    }
 
    let numNewPts = 0;
    let numStrips = 0;
    const inLinesData = inLines.getData();
    let npts = inLinesData[0];
    for (let i = 0; i < inLinesData.length; i += npts + 1) {
      npts = inLinesData[i];
 
      numNewPts = computeOffset(numNewPts, npts);
      numStrips +=
        (2 * npts + 1) * Math.ceil(model.numberOfSides / model.onRatio);
      if (model.capping) {
        numStrips += 2 * (model.numberOfSides + 1);
      }
    }
 
    let pointType = inPts.getDataType();
    if (model.outputPointsPrecision === DesiredOutputPrecision.SINGLE) {
      pointType = VtkDataTypes.FLOAT;
    } else if (model.outputPointsPrecision === DesiredOutputPrecision.DOUBLE) {
      pointType = VtkDataTypes.DOUBLE;
    }
    const newPts = vtkPoints.newInstance({
      dataType: pointType,
      size: numNewPts * 3,
      numberOfComponents: 3,
    });
    const numNormals = 3 * numNewPts;
    const newNormalsData = new Float32Array(numNormals);
    const newNormals = vtkDataArray.newInstance({
      numberOfComponents: 3,
      values: newNormalsData,
      name: 'TubeNormals',
    });
    const newStripsData = new Uint32Array(numStrips);
    const newStrips = vtkCellArray.newInstance({ values: newStripsData });
    let newStripId = 0;
 
    let inNormals = input.getPointData().getNormals();
    let inNormalsData = null;
    let generateNormals = false;
    if (!inNormals || model.useDefaultNormal) {
      inNormalsData = new Float32Array(3 * numPts);
      inNormals = vtkDataArray.newInstance({
        numberOfComponents: 3,
        values: inNormalsData,
        name: 'Normals',
      });
      Iif (model.useDefaultNormal) {
        inNormalsData = inNormalsData.map((elem, index) => {
          const i = index % 3;
          return model.defaultNormal[i];
        });
      } else {
        generateNormals = true;
      }
    }
 
    // loop over pointData arrays and resize based on numNewPts
    const numArrays = input.getPointData().getNumberOfArrays();
    let oldArray = null;
    let newArray = null;
    for (let i = 0; i < numArrays; i++) {
      oldArray = input.getPointData().getArrayByIndex(i);
      newArray = vtkDataArray.newInstance({
        name: oldArray.getName(),
        dataType: oldArray.getDataType(),
        numberOfComponents: oldArray.getNumberOfComponents(),
        size: numNewPts * oldArray.getNumberOfComponents(),
      });
      output.getPointData().addArray(newArray); // concat newArray to end
    }
 
    // loop over cellData arrays and resize based on numNewCells
    let numNewCells = inLines.getNumberOfCells() * model.numberOfSides;
    if (model.capping) {
      numNewCells += 2;
    }
    const numCellArrays = input.getCellData().getNumberOfArrays();
    for (let i = 0; i < numCellArrays; i++) {
      oldArray = input.getCellData().getArrayByIndex(i);
      newArray = vtkDataArray.newInstance({
        name: oldArray.getName(),
        dataType: oldArray.getDataType(),
        numberOfComponents: oldArray.getNumberOfComponents(),
        size: numNewCells * oldArray.getNumberOfComponents(),
      });
      output.getCellData().addArray(newArray); // concat newArray to end
    }
 
    const inScalars = publicAPI.getInputArrayToProcess(0);
    let outScalars = null;
    let range = [];
    if (inScalars) {
      // allocate output scalar array
      // assuming point scalars for now
      outScalars = vtkDataArray.newInstance({
        name: inScalars.getName(),
        dataType: inScalars.getDataType(),
        numberOfComponents: inScalars.getNumberOfComponents(),
        size: numNewPts * inScalars.getNumberOfComponents(),
      });
      range = inScalars.getRange();
      Iif (range[1] - range[0] === 0.0) {
        if (model.varyRadius === VaryRadius.VARY_RADIUS_BY_SCALAR) {
          vtkWarningMacro('Scalar range is zero!');
        }
        range[1] = range[0] + 1.0;
      }
    }
 
    const inVectors = publicAPI.getInputArrayToProcess(1);
    let maxSpeed = 0;
    Iif (inVectors) {
      maxSpeed = inVectors.getMaxNorm();
    }
 
    const outCD = output.getCellData();
    outCD.copyNormalsOff();
    outCD.passData(input.getCellData());
 
    const outPD = output.getPointData();
    Iif (outPD.getNormals() !== null) {
      outPD.copyNormalsOff();
    }
    if (inScalars && outScalars) {
      outPD.setScalars(outScalars);
    }
 
    // TCoords
    let newTCoords = null;
    Iif (
      (model.generateTCoords === GenerateTCoords.TCOORDS_FROM_SCALARS &&
        inScalars) ||
      model.generateTCoords === GenerateTCoords.TCOORDS_FROM_LENGTH ||
      model.generateTCoords === GenerateTCoords.TCOORDS_FROM_NORMALIZED_LENGTH
    ) {
      const newTCoordsData = new Float32Array(2 * numNewPts);
      newTCoords = vtkDataArray.newInstance({
        numberOfComponents: 2,
        values: newTCoordsData,
        name: 'TCoords',
      });
      outPD.copyTCoordsOff();
    }
 
    outPD.passData(input.getPointData());
 
    // Create points along each polyline that are connected into numberOfSides
    // triangle strips.
    const theta = (2.0 * Math.PI) / model.numberOfSides;
    npts = inLinesData[0];
    let offset = 0;
    let inCellId = input.getVerts().getNumberOfCells();
    for (let i = 0; i < inLinesData.length; i += npts + 1) {
      npts = inLinesData[i];
      const pts = inLinesData.slice(i + 1, i + 1 + npts);
      if (npts > 1) {
        // if not, skip tubing this line
        if (generateNormals) {
          const polyLine = inLinesData.slice(i, i + npts + 1);
          generateSlidingNormals(inPts.getData(), polyLine, inNormals);
        }
      }
      // generate points
      if (
        generatePoints(
          offset,
          npts,
          pts,
          inPts.getData(),
          newPts.getData(),
          input.getPointData(),
          outPD,
          newNormalsData,
          inScalars,
          range,
          inVectors,
          maxSpeed,
          inNormalsData,
          theta
        )
      ) {
        // generate strips for the polyline
        newStripId = generateStrips(
          offset,
          npts,
          inCellId,
          newStripId,
          input.getCellData(),
          outCD,
          newStrips
        );
        // generate texture coordinates for the polyline
        Iif (newTCoords) {
          generateTCoords(
            offset,
            npts,
            pts,
            inPts.getData(),
            inScalars,
            newTCoords.getData()
          );
        }
      } else E{
        // skip tubing this line
        vtkWarningMacro('Could not generate points');
      }
      // lineIdx += npts;
      // Compute the new offset for the next polyline
      offset = computeOffset(offset, npts);
      inCellId++;
    }
 
    output.setPoints(newPts);
    output.setStrips(newStrips);
    output.setPointData(outPD);
    outPD.setNormals(newNormals);
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  outputPointsPrecision: DesiredOutputPrecision.DEFAULT,
  radius: 0.5,
  varyRadius: VaryRadius.VARY_RADIUS_OFF,
  numberOfSides: 3,
  radiusFactor: 10,
  defaultNormal: [0, 0, 1],
  useDefaultNormal: false,
  sidesShareVertices: true,
  capping: false,
  onRatio: 1,
  offset: 0,
  generateTCoords: GenerateTCoords.TCOORDS_OFF,
  textureLength: 1.0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.setGet(publicAPI, model, [
    'outputPointsPrecision',
    'radius',
    'varyRadius',
    'numberOfSides',
    'radiusFactor',
    'defaultNormal',
    'useDefaultNormal',
    'sidesShareVertices',
    'capping',
    'onRatio',
    'offset',
    'generateTCoords',
    'textureLength',
  ]);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  // Object specific methods
  vtkTubeFilter(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkTubeFilter');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };