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 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkHigherOrderTetra.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkHigherOrderTetra.h"
#include "vtkDoubleArray.h"
#include "vtkHigherOrderCurve.h"
#include "vtkHigherOrderTriangle.h"
#include "vtkLine.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkTetra.h"
#include "vtkType.h"
#define ENABLE_CACHING
#define FIFTEEN_POINT_TETRA
namespace
{
// The linearized tetra is comprised of four linearized faces. Each face is
// comprised of three vertices. These must be consistent with vtkTetra.
/*
static constexpr vtkIdType FaceVertices[4][3] = {{0,1,3}, {1,2,3},
{2,0,3}, {0,2,1}};
*/
// The linearized tetra is comprised of six linearized edges. Each edge is
// comprised of two vertices. These must be consistent with vtkTetra.
static constexpr vtkIdType EdgeVertices[6][2] = { { 0, 1 }, { 1, 2 }, { 2, 0 }, { 0, 3 }, { 1, 3 },
{ 2, 3 } };
// The barycentric coordinates of the four vertices of the linear tetra.
static constexpr vtkIdType LinearVertices[4][4] = { { 0, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 0, 0 },
{ 0, 0, 1, 0 } };
// When describing a linearized tetra face, there is a mapping between the
// four-component barycentric tetra system and the three-component barycentric
// triangle system. These are the relevant indices within the four-component
// system for each face (e.g. face 0 varies across the barycentric tetra
// coordinates 0, 2 and 3).
static constexpr vtkIdType FaceBCoords[4][3] = { { 0, 2, 3 }, { 2, 0, 1 }, { 2, 1, 3 },
{ 1, 0, 3 } };
// When describing a linearized tetra face, there is a mapping between the
// four-component barycentric tetra system and the three-component barycentric
// triangle system. These are the constant indices within the four-component
// system for each face (e.g. face 0 holds barycentric tetra coordinate 1
// constant).
static constexpr vtkIdType FaceMinCoord[4] = { 1, 3, 0, 2 };
// Each linearized tetra edge holds two barycentric tetra coordinates constant
// and varies the other two. These are the coordinates that are held constant
// for each edge.
static constexpr vtkIdType EdgeMinCoords[6][2] = { { 1, 2 }, { 2, 3 }, { 0, 2 }, { 0, 1 }, { 1, 3 },
{ 0, 3 } };
// The coordinate that increments when traversing an edge (i.e. the coordinate
// of the nonzero component of the second vertex of the edge).
static constexpr vtkIdType EdgeCountingCoord[6] = { 0, 1, 3, 2, 2, 2 };
// When a linearized tetra vertex is cast into barycentric coordinates, one of
// its coordinates is maximal and the other three are minimal. These are the
// indices of the maximal barycentric coordinate for each vertex.
static constexpr vtkIdType VertexMaxCoords[4] = { 3, 0, 1, 2 };
// There are three different layouts for breaking an octahedron into four
// tetras. given the six vertices of the octahedron, these are the layouts for
// each of the three four-tetra configurations.
static constexpr vtkIdType LinearTetras[3][4][4] = { { { 2, 0, 1, 4 }, { 2, 1, 5, 4 },
{ 2, 5, 3, 4 }, { 2, 3, 0, 4 } },
{ { 0, 4, 1, 5 }, { 0, 1, 2, 5 }, { 0, 2, 3, 5 }, { 0, 3, 4, 5 } },
{ { 1, 5, 2, 3 }, { 1, 2, 0, 3 }, { 1, 0, 4, 3 }, { 1, 4, 5, 3 } } };
#ifdef FIFTEEN_POINT_TETRA
double FifteenPointTetraCoords[15 * 3] = { 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., .5, 0.,
0., .5, .5, 0., 0., .5, 0., 0., 0., .5, .5, 0., .5, 0., .5, .5, 1. / 3., 1. / 3., 0., 1. / 3., 0.,
1. / 3., 1. / 3., 1. / 3, 1. / 3., 0., 1. / 3., 1. / 3., .25, .25, .25 };
static constexpr vtkIdType FifteenPointTetraSubtetras[28][4] = { { 0, 4, 10, 14 }, { 1, 4, 10, 14 },
{ 1, 5, 10, 14 }, { 2, 5, 10, 14 }, { 2, 6, 10, 14 }, { 0, 6, 10, 14 }, { 0, 7, 11, 14 },
{ 3, 7, 11, 14 }, { 3, 8, 11, 14 }, { 1, 8, 11, 14 }, { 1, 4, 11, 14 }, { 0, 4, 11, 14 },
{ 1, 5, 12, 14 }, { 2, 5, 12, 14 }, { 2, 9, 12, 14 }, { 3, 9, 12, 14 }, { 3, 8, 12, 14 },
{ 1, 8, 12, 14 }, { 0, 7, 13, 14 }, { 3, 7, 13, 14 }, { 3, 9, 13, 14 }, { 2, 9, 13, 14 },
{ 2, 6, 13, 14 }, { 0, 6, 13, 14 } };
#endif
}
//----------------------------------------------------------------------------
vtkHigherOrderTetra::vtkHigherOrderTetra()
{
this->Order = 0;
this->Tetra = vtkTetra::New();
this->Scalars = vtkDoubleArray::New();
this->Scalars->SetNumberOfTuples(4);
this->Points->SetNumberOfPoints(4);
this->PointIds->SetNumberOfIds(4);
for (vtkIdType i = 0; i < 4; i++)
{
this->Points->SetPoint(i, 0.0, 0.0, 0.0);
this->PointIds->SetId(i, 0);
}
}
//----------------------------------------------------------------------------
vtkHigherOrderTetra::~vtkHigherOrderTetra()
{
this->Tetra->Delete();
this->Scalars->Delete();
}
//------------------------------------------------------------------------------
void vtkHigherOrderTetra::SetEdgeIdsAndPoints(int edgeId,
const std::function<void(const vtkIdType&)>& set_number_of_ids_and_points,
const std::function<void(const vtkIdType&, const vtkIdType&)>& set_ids_and_points)
{
vtkIdType order = this->GetOrder();
set_number_of_ids_and_points(order + 1);
vtkIdType bindex[4] = { 0, 0, 0, 0 };
bindex[EdgeVertices[edgeId][0]] = order;
for (vtkIdType i = 0; i <= order; i++)
{
set_ids_and_points(i, this->ToIndex(bindex));
bindex[EdgeVertices[edgeId][0]]--;
bindex[EdgeVertices[edgeId][1]]++;
}
}
//------------------------------------------------------------------------------
void vtkHigherOrderTetra::SetFaceIdsAndPoints(vtkHigherOrderTriangle* result, int faceId,
const std::function<void(const vtkIdType&)>& set_number_of_ids_and_points,
const std::function<void(const vtkIdType&, const vtkIdType&)>& set_ids_and_points)
{
assert(faceId >= 0 && faceId < 4);
vtkIdType order = this->GetOrder();
vtkIdType nPoints = (order + 1) * (order + 2) / 2;
#ifdef FIFTEEN_POINT_TETRA
if (this->Points->GetNumberOfPoints() == 15)
{
nPoints = 7;
}
#endif
set_number_of_ids_and_points(nPoints);
vtkIdType tetBCoords[4], triBCoords[3];
for (vtkIdType p = 0; p < nPoints; p++)
{
vtkHigherOrderTriangle::BarycentricIndex(p, triBCoords, order);
for (vtkIdType coord = 0; coord < 3; coord++)
{
tetBCoords[FaceBCoords[faceId][coord]] = triBCoords[coord];
}
tetBCoords[FaceMinCoord[faceId]] = 0;
vtkIdType pointIndex = vtkHigherOrderTetra::Index(tetBCoords, order);
set_ids_and_points(p, pointIndex);
}
#ifdef FIFTEEN_POINT_TETRA
if (this->Points->GetNumberOfPoints() == 15)
{
vtkIdType pointIndex = 10 + ((faceId + 1) % 4);
set_ids_and_points(6, pointIndex);
}
#endif
result->Initialize();
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::Initialize()
{
vtkIdType order = this->ComputeOrder();
if (this->Order != order)
{
// Reset our caches
this->Order = order;
this->NumberOfSubtetras = this->ComputeNumberOfSubtetras();
EdgeIds.resize(this->Order + 1);
#ifdef ENABLE_CACHING
this->BarycentricIndexMap.resize(4 * this->GetPointIds()->GetNumberOfIds());
for (vtkIdType i = 0; i < this->GetPointIds()->GetNumberOfIds(); i++)
{
this->BarycentricIndexMap[4 * i] = -1;
}
// we sacrifice memory for efficiency here
vtkIdType nIndexMap = (this->Order + 1) * (this->Order + 1) * (this->Order + 1);
this->IndexMap.resize(nIndexMap);
for (vtkIdType i = 0; i < nIndexMap; i++)
{
this->IndexMap[i] = -1;
}
vtkIdType nSubtetras = this->GetNumberOfSubtetras();
this->SubtetraIndexMap.resize(16 * nSubtetras);
for (vtkIdType i = 0; i < nSubtetras; i++)
{
this->SubtetraIndexMap[16 * i] = -1;
}
#endif
}
}
//----------------------------------------------------------------------------
vtkIdType vtkHigherOrderTetra::ComputeNumberOfSubtetras()
{
#ifdef FIFTEEN_POINT_TETRA
if (this->Points->GetNumberOfPoints() == 15)
{
return 28;
}
#endif
vtkIdType order = this->GetOrder();
// # of rightside-up tetras: order*(order+1)*(order+2)/6
// # of octahedra: (order-1)*order*(order+1)/6
// # of upside-down tetras: (order-2)*(order-1)*order/6
vtkIdType nRightSideUp = order * (order + 1) * (order + 2) / 6;
vtkIdType nOctahedra = (order - 1) * order * (order + 1) / 6;
vtkIdType nUpsideDown = (order > 2 ? (order - 2) * (order - 1) * order / 6 : 0);
return nRightSideUp + 4 * nOctahedra + nUpsideDown;
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::SubtetraBarycentricPointIndices(
vtkIdType cellIndex, vtkIdType (&pointBIndices)[4][4])
{
// We tesselllate a tetrahedron into a tetrahedral-octahedral honeycomb, and
// then discretize each octahedron into 4 tetrahedra. The pattern is as
// follows: for each additional level in our tetrahedron (propagating
// downwards in parametric z), a pattern of upside-down and rightside-up
// triangles are formed. The rightside-up triangles form tetrahedra with the
// single point above them, and the upside-down triangles form octahedra with
// the righteside-up triangles above them.
assert(cellIndex < this->GetNumberOfSubtetras());
#ifdef FIFTEEN_POINT_TETRA
if (this->Points->GetNumberOfPoints() == 15)
{
pointBIndices[0][0] = FifteenPointTetraSubtetras[cellIndex][0];
pointBIndices[1][0] = FifteenPointTetraSubtetras[cellIndex][1];
pointBIndices[2][0] = FifteenPointTetraSubtetras[cellIndex][2];
pointBIndices[3][0] = FifteenPointTetraSubtetras[cellIndex][3];
return;
}
#endif
#ifdef ENABLE_CACHING
vtkIdType cellIndexStart = cellIndex * 16;
if (this->SubtetraIndexMap[cellIndexStart] == -1)
#endif
{
vtkIdType order = this->GetOrder();
if (order == 1)
{
for (vtkIdType i = 0; i < 4; i++)
{
for (vtkIdType j = 0; j < 4; j++)
{
pointBIndices[i][j] = LinearVertices[i][j];
}
}
}
else
{
vtkIdType nRightSideUp = order * (order + 1) * (order + 2) / 6;
vtkIdType nOctahedra = (order - 1) * order * (order + 1) / 6;
if (cellIndex < nRightSideUp)
{
// there are nRightSideUp subtetras whose orientation is the same as the
// projected tetra. We traverse them here.
vtkHigherOrderTetra::BarycentricIndex(cellIndex, pointBIndices[0], order - 1);
pointBIndices[0][3] += 1;
pointBIndices[1][0] = pointBIndices[0][0];
pointBIndices[1][1] = pointBIndices[0][1] + 1;
pointBIndices[1][2] = pointBIndices[0][2];
pointBIndices[1][3] = pointBIndices[0][3] - 1;
pointBIndices[3][0] = pointBIndices[0][0] + 1;
pointBIndices[3][1] = pointBIndices[0][1];
pointBIndices[3][2] = pointBIndices[0][2];
pointBIndices[3][3] = pointBIndices[0][3] - 1;
pointBIndices[2][0] = pointBIndices[0][0];
pointBIndices[2][1] = pointBIndices[0][1];
pointBIndices[2][2] = pointBIndices[0][2] + 1;
pointBIndices[2][3] = pointBIndices[0][3] - 1;
}
else if (cellIndex < nRightSideUp + 4 * nOctahedra)
{
// the next set of subtetras are embedded in octahedra, so we need to
// identify and subdivide the octahedra. We traverse them here.
cellIndex -= nRightSideUp;
vtkIdType octIndex = cellIndex / 4;
vtkIdType tetIndex = cellIndex % 4;
vtkIdType octBIndices[6][4];
if (order == 2)
{
octBIndices[2][0] = octBIndices[2][1] = octBIndices[2][2] = octBIndices[2][3] = 0;
}
else
{
vtkHigherOrderTetra::BarycentricIndex(octIndex, octBIndices[2], order - 2);
}
octBIndices[2][1] += 1;
octBIndices[2][3] += 1;
octBIndices[1][0] = octBIndices[2][0] + 1;
octBIndices[1][1] = octBIndices[2][1];
octBIndices[1][2] = octBIndices[2][2];
octBIndices[1][3] = octBIndices[2][3] - 1;
octBIndices[0][0] = octBIndices[2][0] + 1;
octBIndices[0][1] = octBIndices[2][1] - 1;
octBIndices[0][2] = octBIndices[2][2];
octBIndices[0][3] = octBIndices[2][3];
octBIndices[3][0] = octBIndices[0][0] - 1;
octBIndices[3][1] = octBIndices[0][1];
octBIndices[3][2] = octBIndices[0][2] + 1;
octBIndices[3][3] = octBIndices[0][3];
octBIndices[4][0] = octBIndices[3][0] + 1;
octBIndices[4][1] = octBIndices[3][1];
octBIndices[4][2] = octBIndices[3][2];
octBIndices[4][3] = octBIndices[3][3] - 1;
octBIndices[5][0] = octBIndices[3][0];
octBIndices[5][1] = octBIndices[3][1] + 1;
octBIndices[5][2] = octBIndices[3][2];
octBIndices[5][3] = octBIndices[3][3] - 1;
this->TetraFromOctahedron(tetIndex, octBIndices, pointBIndices);
}
else
{
// there are nUpsideDown subtetras whose orientation is inverted w.r.t.
// the projected tetra. We traverse them here.
cellIndex -= (nRightSideUp + 4 * nOctahedra);
if (order == 3)
{
pointBIndices[2][0] = pointBIndices[2][1] = pointBIndices[2][2] = pointBIndices[2][3] = 0;
}
else
{
vtkHigherOrderTetra::BarycentricIndex(cellIndex, pointBIndices[2], order - 3);
}
pointBIndices[2][0] += 1;
pointBIndices[2][1] += 1;
pointBIndices[2][3] += 1;
pointBIndices[1][0] = pointBIndices[2][0] - 1;
pointBIndices[1][1] = pointBIndices[2][1];
pointBIndices[1][2] = pointBIndices[2][2] + 1;
pointBIndices[1][3] = pointBIndices[2][3];
pointBIndices[3][0] = pointBIndices[2][0];
pointBIndices[3][1] = pointBIndices[2][1] - 1;
pointBIndices[3][2] = pointBIndices[2][2] + 1;
pointBIndices[3][3] = pointBIndices[2][3];
pointBIndices[0][0] = pointBIndices[2][0];
pointBIndices[0][1] = pointBIndices[2][1];
pointBIndices[0][2] = pointBIndices[2][2] + 1;
pointBIndices[0][3] = pointBIndices[2][3] - 1;
}
}
#ifdef ENABLE_CACHING
for (vtkIdType i = 0; i < 4; i++)
{
for (vtkIdType j = 0; j < 4; j++)
{
this->SubtetraIndexMap[cellIndexStart + 4 * i + j] = pointBIndices[i][j];
}
}
#endif
}
#ifdef ENABLE_CACHING
else
{
for (vtkIdType i = 0; i < 4; i++)
{
for (vtkIdType j = 0; j < 4; j++)
{
pointBIndices[i][j] = this->SubtetraIndexMap[cellIndexStart + 4 * i + j];
}
}
}
#endif
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::TetraFromOctahedron(
vtkIdType cellIndex, const vtkIdType (&octBIndices)[6][4], vtkIdType (&tetraBIndices)[4][4])
{
// TODO: intelligently select which of the three linearizations reduce
// artifacts. For now, we always choose the first linearization.
static vtkIdType linearization = 0;
for (vtkIdType i = 0; i < 4; i++)
{
for (vtkIdType j = 0; j < 4; j++)
{
tetraBIndices[i][j] = octBIndices[LinearTetras[linearization][cellIndex][i]][j];
}
}
}
//----------------------------------------------------------------------------
int vtkHigherOrderTetra::CellBoundary(
int vtkNotUsed(subId), const double pcoords[3], vtkIdList* pts)
{
const double ijk = 1.0 - pcoords[0] - pcoords[1] - pcoords[2];
int axis = 3;
double dmin = ijk;
for (int ii = 0; ii < 3; ++ii)
{
if (dmin > pcoords[ii])
{
axis = ii;
dmin = pcoords[ii];
}
}
const int closestFaceByAxis[4][3] = { { 0, 3, 2 }, { 0, 1, 3 }, { 0, 2, 1 }, { 1, 2, 3 } };
pts->SetNumberOfIds(3);
for (int ii = 0; ii < 3; ++ii)
{
pts->SetId(ii, this->PointIds->GetId(closestFaceByAxis[axis][ii]));
}
return pcoords[0] < 0 || pcoords[0] > 1.0 || pcoords[1] < 0 || pcoords[1] > 1.0 ||
pcoords[2] < 0 || pcoords[2] > 1.0 || ijk < 0 || ijk > 1.0
? 0
: 1;
}
//----------------------------------------------------------------------------
int vtkHigherOrderTetra::EvaluatePosition(const double x[3], double closestPoint[3], int& subId,
double pcoords[3], double& minDist2, double weights[])
{
double pc[3], dist2, tempWeights[4], closest[3];
double pcoordsMin[3] = { 0., 0., 0. };
int returnStatus = 0, status, ignoreId;
vtkIdType minBIndices[4][4], bindices[4][4], pointIndices[4];
vtkIdType order = this->GetOrder();
vtkIdType numberOfSubtetras = this->GetNumberOfSubtetras();
minDist2 = VTK_DOUBLE_MAX;
for (vtkIdType subCellId = 0; subCellId < numberOfSubtetras; subCellId++)
{
this->SubtetraBarycentricPointIndices(subCellId, bindices);
for (vtkIdType i = 0; i < 4; i++)
{
pointIndices[i] = this->ToIndex(bindices[i]);
this->Tetra->Points->SetPoint(i, this->Points->GetPoint(pointIndices[i]));
}
status = this->Tetra->EvaluatePosition(x, closest, ignoreId, pc, dist2, tempWeights);
if (status != -1 && dist2 < minDist2)
{
returnStatus = status;
minDist2 = dist2;
subId = subCellId;
pcoordsMin[0] = pc[0];
pcoordsMin[1] = pc[1];
pcoordsMin[2] = pc[2];
for (vtkIdType i = 0; i < 4; i++)
{
for (vtkIdType j = 0; j < 4; j++)
{
minBIndices[i][j] = bindices[i][j];
}
}
}
}
// adjust parametric coordinates
if (returnStatus != -1)
{
for (vtkIdType i = 0; i < 3; i++)
{
pcoords[i] = (minBIndices[0][i] + pcoordsMin[0] * (minBIndices[1][i] - minBIndices[0][i]) +
pcoordsMin[1] * (minBIndices[2][i] - minBIndices[0][i]) +
pcoordsMin[2] * (minBIndices[3][i] - minBIndices[0][i])) /
order;
}
if (closestPoint != nullptr)
{
// Compute both closestPoint and weights
this->EvaluateLocation(subId, pcoords, closestPoint, weights);
}
else
{
// Compute weights only
this->InterpolateFunctions(pcoords, weights);
}
}
return returnStatus;
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::EvaluateLocation(
int& vtkNotUsed(subId), const double pcoords[3], double x[3], double* weights)
{
x[0] = x[1] = x[2] = 0.;
this->InterpolateFunctions(pcoords, weights);
double p[3];
vtkIdType nPoints = this->GetPoints()->GetNumberOfPoints();
for (vtkIdType idx = 0; idx < nPoints; idx++)
{
this->Points->GetPoint(idx, p);
for (vtkIdType jdx = 0; jdx < 3; jdx++)
{
x[jdx] += p[jdx] * weights[idx];
}
}
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::Contour(double value, vtkDataArray* cellScalars,
vtkIncrementalPointLocator* locator, vtkCellArray* verts, vtkCellArray* lines,
vtkCellArray* polys, vtkPointData* inPd, vtkPointData* outPd, vtkCellData* inCd, vtkIdType cellId,
vtkCellData* outCd)
{
vtkIdType bindices[4][4];
vtkIdType numberOfSubtetras = this->GetNumberOfSubtetras();
for (vtkIdType subCellId = 0; subCellId < numberOfSubtetras; subCellId++)
{
this->SubtetraBarycentricPointIndices(subCellId, bindices);
for (vtkIdType i = 0; i < 4; i++)
{
vtkIdType pointIndex = this->ToIndex(bindices[i]);
this->Tetra->Points->SetPoint(i, this->Points->GetPoint(pointIndex));
if (outPd)
{
this->Tetra->PointIds->SetId(i, this->PointIds->GetId(pointIndex));
}
this->Scalars->SetTuple(i, cellScalars->GetTuple(pointIndex));
}
this->Tetra->Contour(
value, this->Scalars, locator, verts, lines, polys, inPd, outPd, inCd, cellId, outCd);
}
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::Clip(double value, vtkDataArray* cellScalars,
vtkIncrementalPointLocator* locator, vtkCellArray* polys, vtkPointData* inPd, vtkPointData* outPd,
vtkCellData* inCd, vtkIdType cellId, vtkCellData* outCd, int insideOut)
{
vtkIdType bindices[4][4];
vtkIdType numberOfSubtetras = this->GetNumberOfSubtetras();
for (vtkIdType subCellId = 0; subCellId < numberOfSubtetras; subCellId++)
{
this->SubtetraBarycentricPointIndices(subCellId, bindices);
for (vtkIdType i = 0; i < 4; i++)
{
vtkIdType pointIndex = this->ToIndex(bindices[i]);
this->Tetra->Points->SetPoint(i, this->Points->GetPoint(pointIndex));
if (outPd)
{
this->Tetra->PointIds->SetId(i, this->PointIds->GetId(pointIndex));
}
this->Scalars->SetTuple(i, cellScalars->GetTuple(pointIndex));
}
this->Tetra->Clip(
value, this->Scalars, locator, polys, inPd, outPd, inCd, cellId, outCd, insideOut);
}
}
//----------------------------------------------------------------------------
int vtkHigherOrderTetra::IntersectWithLine(
const double* p1, const double* p2, double tol, double& t, double* x, double* pcoords, int& subId)
{
int subTest;
t = VTK_DOUBLE_MAX;
double tTmp;
double xMin[3], pcoordsMin[3];
for (int i = 0; i < this->GetNumberOfFaces(); i++)
{
if (this->GetFace(i)->IntersectWithLine(p1, p2, tol, tTmp, xMin, pcoordsMin, subTest) &&
tTmp < t)
{
for (vtkIdType j = 0; j < 3; j++)
{
x[j] = xMin[j];
if (FaceBCoords[i][j] != 3)
{
pcoords[FaceBCoords[i][j]] = pcoordsMin[j];
}
}
if (FaceMinCoord[i] != 3)
{
pcoords[FaceMinCoord[i]] = 0.;
}
t = tTmp;
}
}
subId = 0;
return (t == VTK_DOUBLE_MAX ? 0 : 1);
}
//----------------------------------------------------------------------------
int vtkHigherOrderTetra::Triangulate(int vtkNotUsed(index), vtkIdList* ptIds, vtkPoints* pts)
{
pts->Reset();
ptIds->Reset();
vtkIdType bindices[4][4];
vtkIdType numberOfSubtetras = this->GetNumberOfSubtetras();
pts->SetNumberOfPoints(4 * numberOfSubtetras);
ptIds->SetNumberOfIds(4 * numberOfSubtetras);
for (vtkIdType subCellId = 0; subCellId < numberOfSubtetras; subCellId++)
{
this->SubtetraBarycentricPointIndices(subCellId, bindices);
for (vtkIdType i = 0; i < 4; i++)
{
vtkIdType pointIndex = this->ToIndex(bindices[i]);
ptIds->SetId(4 * subCellId + i, this->PointIds->GetId(pointIndex));
pts->SetPoint(4 * subCellId + i, this->Points->GetPoint(pointIndex));
}
}
return 1;
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::JacobianInverse(const double pcoords[3], double** inverse, double* derivs)
{
// Given parametric coordinates compute inverse Jacobian transformation
// matrix. Returns 9 elements of 3x3 inverse Jacobian plus interpolation
// function derivatives.
int i, j, k;
double *m[3], m0[3], m1[3], m2[3];
double x[3];
vtkIdType numberOfPoints = this->Points->GetNumberOfPoints();
// compute interpolation function derivatives
this->InterpolateDerivs(pcoords, derivs);
// create Jacobian matrix
m[0] = m0;
m[1] = m1;
m[2] = m2;
for (i = 0; i < 3; i++) // initialize matrix
{
m0[i] = m1[i] = m2[i] = 0.0;
}
for (j = 0; j < numberOfPoints; j++)
{
this->Points->GetPoint(j, x);
for (i = 0; i < 3; i++)
{
for (k = 0; k < 3; k++)
{
m[k][i] += x[i] * derivs[numberOfPoints * k + j];
}
}
}
if (!vtkMath::InvertMatrix(m, inverse, 3))
{
vtkErrorMacro(<< "Jacobian inverse not found");
return;
}
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::Derivatives(
int vtkNotUsed(subId), const double pcoords[3], const double* values, int dim, double* derivs)
{
double *jI[3], j0[3], j1[3], j2[3];
vtkIdType numberOfPoints = this->Points->GetNumberOfPoints();
std::vector<double> fDs(3 * numberOfPoints);
double sum[3];
int i, j, k;
// compute inverse Jacobian and interpolation function derivatives
jI[0] = j0;
jI[1] = j1;
jI[2] = j2;
this->JacobianInverse(pcoords, jI, &fDs[0]);
// now compute derivatives of values provided
for (k = 0; k < dim; k++) // loop over values per vertex
{
sum[0] = sum[1] = sum[2] = 0.0;
for (i = 0; i < numberOfPoints; i++) // loop over interp. function derivatives
{
sum[0] += fDs[i] * values[dim * i + k];
sum[1] += fDs[numberOfPoints + i] * values[dim * i + k];
sum[2] += fDs[numberOfPoints * 2 + i] * values[dim * i + k];
}
for (j = 0; j < 3; j++) // loop over derivative directions
{
derivs[3 * k + j] = 0.;
for (i = 0; i < 3; i++)
{
derivs[3 * k + j] += sum[i] * jI[j][i];
}
}
}
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::SetParametricCoords()
{
vtkIdType nPoints = this->Points->GetNumberOfPoints();
#ifdef FIFTEEN_POINT_TETRA
if (nPoints == 15)
{
return;
}
#endif
if (!this->PointParametricCoordinates)
{
this->PointParametricCoordinates = vtkSmartPointer<vtkPoints>::New();
this->PointParametricCoordinates->SetDataTypeToDouble();
}
// Ensure Order is up-to-date and check that current point size matches:
if (this->PointParametricCoordinates->GetNumberOfPoints() != nPoints)
{
this->PointParametricCoordinates->Initialize();
double order_d = static_cast<vtkIdType>(this->GetOrder());
this->PointParametricCoordinates->SetNumberOfPoints(nPoints);
vtkIdType bindex[4];
for (vtkIdType p = 0; p < nPoints; p++)
{
this->ToBarycentricIndex(p, bindex);
this->PointParametricCoordinates->SetPoint(
p, bindex[0] / order_d, bindex[1] / order_d, bindex[2] / order_d);
}
}
}
double* vtkHigherOrderTetra::GetParametricCoords()
{
#ifdef FIFTEEN_POINT_TETRA
if (this->Points->GetNumberOfPoints() == 15)
{
return FifteenPointTetraCoords;
}
#endif
this->SetParametricCoords();
return vtkDoubleArray::SafeDownCast(this->PointParametricCoordinates->GetData())->GetPointer(0);
}
//----------------------------------------------------------------------------
int vtkHigherOrderTetra::GetParametricCenter(double pcoords[3])
{
pcoords[0] = pcoords[1] = pcoords[2] = 0.25;
return 0;
}
//----------------------------------------------------------------------------
double vtkHigherOrderTetra::GetParametricDistance(const double pcoords[3])
{
int i;
double pDist, pDistMax = 0.0;
double pc[4];
pc[0] = pcoords[0];
pc[1] = pcoords[1];
pc[2] = pcoords[2];
pc[3] = 1.0 - pcoords[0] - pcoords[1] - pcoords[2];
for (i = 0; i < 4; i++)
{
if (pc[i] < 0.0)
{
pDist = -pc[i];
}
else if (pc[i] > 1.0)
{
pDist = pc[i] - 1.0;
}
else // inside the cell in the parametric direction
{
pDist = 0.0;
}
if (pDist > pDistMax)
{
pDistMax = pDist;
}
}
return pDistMax;
}
//----------------------------------------------------------------------------
vtkIdType vtkHigherOrderTetra::ComputeOrder()
{
return vtkHigherOrderTetra::ComputeOrder(this->Points->GetNumberOfPoints());
}
vtkIdType vtkHigherOrderTetra::ComputeOrder(const vtkIdType nPoints)
{
switch (nPoints)
{
case 1:
return 0;
case 4:
return 1;
case 10:
return 2;
#ifdef FIFTEEN_POINT_TETRA
case 15:
return 2;
#endif
case 20:
return 3;
case 35:
return 4;
case 56:
return 5;
case 84:
return 6;
case 120:
return 7;
case 165:
return 8;
case 220:
return 9;
case 286:
return 10;
// this is a iterative solution strategy to find the nearest integer ( order ) given the number
// of points in the tetrahedron. the order is the root of following cubit equation
// nPointsForOrder = (order + 1) * (order + 2) * (order + 3) / 6;
// nPointsForOrder = ( x3 + 6x2 + 11x + 6 ) / 6
default:
{
vtkIdType order = 1;
vtkIdType nPointsForOrder = 4;
while (nPointsForOrder < nPoints)
{
order++;
nPointsForOrder = (order + 1) * (order + 2) * (order + 3) / 6;
}
assert(nPoints == nPointsForOrder);
return order;
}
}
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::ToBarycentricIndex(vtkIdType index, vtkIdType* bindex)
{
#ifdef ENABLE_CACHING
if (this->BarycentricIndexMap[4 * index] == -1)
{
vtkHigherOrderTetra::BarycentricIndex(
index, &this->BarycentricIndexMap[4 * index], this->GetOrder());
}
for (vtkIdType i = 0; i < 4; i++)
{
bindex[i] = this->BarycentricIndexMap[4 * index + i];
}
#else
return vtkHigherOrderTetra::BarycentricIndex(index, bindex, this->GetOrder());
#endif
}
//----------------------------------------------------------------------------
vtkIdType vtkHigherOrderTetra::ToIndex(const vtkIdType* bindex)
{
#ifdef FIFTEEN_POINT_TETRA
if (this->Points->GetNumberOfPoints() == 15)
{
return bindex[0];
}
#endif
#ifdef ENABLE_CACHING
vtkIdType cacheIdx =
((this->Order + 1) * (this->Order + 1) * bindex[0] + (this->Order + 1) * bindex[1] + bindex[2]);
if (this->IndexMap[cacheIdx] == -1)
{
this->IndexMap[cacheIdx] = vtkHigherOrderTetra::Index(bindex, this->GetOrder());
}
return this->IndexMap[cacheIdx];
#else
return vtkHigherOrderTetra::Index(bindex, this->GetOrder());
#endif
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::BarycentricIndex(vtkIdType index, vtkIdType* bindex, vtkIdType order)
{
// "Barycentric index" is a set of 4 integers, each running from 0 to
// <Order>. It is the index of a point in the tetrahedron in barycentric
// coordinates.
assert(order >= 1);
vtkIdType max = order;
vtkIdType min = 0;
// scope into the correct tetra
while (index >= 2 * (order * order + 1) && index != 0 && order > 3)
{
index -= 2 * (order * order + 1);
max -= 3;
min++;
order -= 4;
}
if (index < 4)
{
// we are on a vertex
for (vtkIdType coord = 0; coord < 4; coord++)
{
bindex[coord] = (coord == VertexMaxCoords[index] ? max : min);
}
return;
}
else if (index - 4 < 6 * (order - 1))
{
// we are on an edge
vtkIdType edgeId = (index - 4) / (order - 1);
vtkIdType vertexId = (index - 4) % (order - 1);
for (vtkIdType coord = 0; coord < 4; coord++)
{
bindex[coord] = min +
(LinearVertices[EdgeVertices[edgeId][0]][coord] * (max - min - 1 - vertexId) +
LinearVertices[EdgeVertices[edgeId][1]][coord] * (1 + vertexId));
}
return;
}
else
{
// we are on a face
vtkIdType faceId = (index - 4 - 6 * (order - 1)) / ((order - 2) * (order - 1) / 2);
vtkIdType vertexId = (index - 4 - 6 * (order - 1)) % ((order - 2) * (order - 1) / 2);
vtkIdType projectedBIndex[3];
if (order == 3)
{
projectedBIndex[0] = projectedBIndex[1] = projectedBIndex[2] = 0;
}
else
{
vtkHigherOrderTriangle::BarycentricIndex(vertexId, projectedBIndex, order - 3);
}
for (vtkIdType i = 0; i < 3; i++)
{
bindex[FaceBCoords[faceId][i]] = (min + 1 + projectedBIndex[i]);
}
bindex[FaceMinCoord[faceId]] = min;
return;
}
}
//----------------------------------------------------------------------------
vtkIdType vtkHigherOrderTetra::Index(const vtkIdType* bindex, vtkIdType order)
{
vtkIdType index = 0;
assert(order >= 1);
assert(bindex[0] + bindex[1] + bindex[2] + bindex[3] == order);
vtkIdType max = order;
vtkIdType min = 0;
vtkIdType bmin = std::min(std::min(std::min(bindex[0], bindex[1]), bindex[2]), bindex[3]);
// scope into the correct tetra
while (bmin > min)
{
index += 2 * (order * order + 1);
max -= 3;
min++;
order -= 4;
}
for (vtkIdType vertex = 0; vertex < 4; vertex++)
{
if (bindex[VertexMaxCoords[vertex]] == max)
{
// we are on a vertex
return index;
}
index++;
}
for (vtkIdType edge = 0; edge < 6; edge++)
{
if (bindex[EdgeMinCoords[edge][0]] == min && bindex[EdgeMinCoords[edge][1]] == min)
{
// we are on an edge
return index + bindex[EdgeCountingCoord[edge]] - (min + 1);
}
index += max - (min + 1);
}
for (vtkIdType face = 0; face < 4; face++)
{
if (bindex[FaceMinCoord[face]] == min)
{
// we are on a face
vtkIdType projectedBIndex[3];
for (vtkIdType i = 0; i < 3; i++)
{
projectedBIndex[i] = bindex[FaceBCoords[face][i]] - min;
}
// we must subtract the indices of the face's vertices and edges, which
// total to 3*order
return (index + vtkHigherOrderTriangle::Index(projectedBIndex, order) - 3 * order);
}
index += (order + 1) * (order + 2) / 2 - 3 * order;
}
return index;
}
//----------------------------------------------------------------------------
void vtkHigherOrderTetra::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
|