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 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDendrogramItem.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 "vtkDendrogramItem.h"
#include "vtkBrush.h"
#include "vtkColorLegend.h"
#include "vtkContext2D.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextScene.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkGraphLayout.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkLookupTable.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPruneTreeFilter.h"
#include "vtkStringArray.h"
#include "vtkTextProperty.h"
#include "vtkTransform2D.h"
#include "vtkTree.h"
#include "vtkTreeLayoutStrategy.h"
#include "vtkUnsignedIntArray.h"
#include <algorithm>
#include <queue>
#include <sstream>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkDendrogramItem);
//------------------------------------------------------------------------------
vtkDendrogramItem::vtkDendrogramItem()
: PositionVector(0, 0)
{
this->Position = this->PositionVector.GetData();
this->DendrogramBuildTime = 0;
this->Interactive = true;
this->ColorTree = false;
this->LegendPositionSet = false;
this->Tree = vtkSmartPointer<vtkTree>::New();
this->PrunedTree = vtkSmartPointer<vtkTree>::New();
this->LayoutTree = vtkSmartPointer<vtkTree>::New();
/* initialize bounds with impossible values */
this->MinX = 1.0;
this->MinY = 1.0;
this->MaxX = 0.0;
this->MaxY = 0.0;
this->LabelWidth = 0.0;
this->LineWidth = 1.0;
this->NumberOfLeafNodes = 0;
this->MultiplierX = 100.0;
this->MultiplierY = 100.0;
this->LeafSpacing = 18.0;
this->PruneFilter->SetShouldPruneParentVertex(false);
this->ExtendLeafNodes = false;
this->DrawLabels = true;
this->DisplayNumberOfCollapsedLeafNodes = true;
this->DistanceArrayName = "node weight";
this->VertexNameArrayName = "node name";
this->ColorLegend->SetVisible(false);
this->ColorLegend->DrawBorderOn();
this->ColorLegend->CacheBoundsOff();
this->AddItem(this->ColorLegend);
}
//------------------------------------------------------------------------------
vtkDendrogramItem::~vtkDendrogramItem() = default;
//------------------------------------------------------------------------------
void vtkDendrogramItem::SetPosition(const vtkVector2f& pos)
{
this->PositionVector = pos;
this->DendrogramBuildTime = 0;
}
//------------------------------------------------------------------------------
vtkVector2f vtkDendrogramItem::GetPositionVector()
{
return this->PositionVector;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::SetTree(vtkTree* tree)
{
if (tree == nullptr || tree->GetNumberOfVertices() == 0)
{
this->Tree = vtkSmartPointer<vtkTree>::New();
this->PrunedTree = vtkSmartPointer<vtkTree>::New();
this->LayoutTree = vtkSmartPointer<vtkTree>::New();
return;
}
this->Tree = tree;
// initialize some additional arrays for the tree's vertex data
vtkNew<vtkUnsignedIntArray> vertexIsPruned;
vertexIsPruned->SetNumberOfComponents(1);
vertexIsPruned->SetName("VertexIsPruned");
vertexIsPruned->SetNumberOfValues(this->Tree->GetNumberOfVertices());
vertexIsPruned->FillComponent(0, 0.0);
this->Tree->GetVertexData()->AddArray(vertexIsPruned);
vtkNew<vtkIdTypeArray> originalId;
originalId->SetNumberOfComponents(1);
originalId->SetName("OriginalId");
vtkIdType numVertices = this->Tree->GetNumberOfVertices();
originalId->SetNumberOfValues(numVertices);
for (vtkIdType i = 0; i < numVertices; ++i)
{
originalId->SetValue(i, i);
}
this->Tree->GetVertexData()->AddArray(originalId);
// make a copy of the full tree for later pruning
this->PrunedTree->DeepCopy(this->Tree);
// setup the lookup table that's used to color the triangles representing
// collapsed subtrees. First we find maximum possible value.
vtkIdType root = this->Tree->GetRoot();
if (this->Tree->GetNumberOfChildren(root) == 1)
{
root = this->Tree->GetChild(root, 0);
}
int numLeavesInBiggestSubTree = 0;
for (vtkIdType child = 0; child < this->Tree->GetNumberOfChildren(root); ++child)
{
vtkIdType childVertex = this->Tree->GetChild(root, child);
int numLeaves = this->CountLeafNodes(childVertex);
if (numLeaves > numLeavesInBiggestSubTree)
{
numLeavesInBiggestSubTree = numLeaves;
}
}
double rangeMinimum = 2.0;
if (numLeavesInBiggestSubTree < rangeMinimum)
{
rangeMinimum = numLeavesInBiggestSubTree;
}
this->TriangleLookupTable->SetNumberOfTableValues(256);
this->TriangleLookupTable->SetHueRange(0.5, 0.045);
this->TriangleLookupTable->SetRange(rangeMinimum, static_cast<double>(numLeavesInBiggestSubTree));
this->TriangleLookupTable->Build();
}
//------------------------------------------------------------------------------
vtkTree* vtkDendrogramItem::GetTree()
{
return this->Tree;
}
//------------------------------------------------------------------------------
vtkTree* vtkDendrogramItem::GetPrunedTree()
{
return this->PrunedTree;
}
//------------------------------------------------------------------------------
bool vtkDendrogramItem::Paint(vtkContext2D* painter)
{
if (this->Tree->GetNumberOfVertices() == 0)
{
return true;
}
this->PrepareToPaint(painter);
this->PaintBuffers(painter);
this->PaintChildren(painter);
return true;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::PrepareToPaint(vtkContext2D* painter)
{
if (this->IsDirty())
{
this->RebuildBuffers();
}
this->ComputeLabelWidth(painter);
}
//------------------------------------------------------------------------------
bool vtkDendrogramItem::IsDirty()
{
if (this->Tree->GetNumberOfVertices() == 0)
{
return false;
}
if (this->MTime > this->DendrogramBuildTime)
{
return true;
}
if (this->PrunedTree->GetMTime() > this->DendrogramBuildTime)
{
return true;
}
if (this->Tree->GetMTime() > this->DendrogramBuildTime)
{
return true;
}
return false;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::RebuildBuffers()
{
if (this->Tree->GetNumberOfVertices() == 0)
{
return;
}
// Special case where our input tree has been modified. Refresh PrunedTree
// to be an up-to-date full copy of it.
if (this->Tree->GetMTime() > this->PrunedTree->GetMTime())
{
this->PrunedTree->DeepCopy(this->Tree);
}
int orientation = this->GetOrientation();
vtkNew<vtkTreeLayoutStrategy> strategy;
if (this->PrunedTree->GetVertexData()->GetAbstractArray(this->DistanceArrayName.c_str()) !=
nullptr)
{
strategy->SetDistanceArrayName(this->DistanceArrayName.c_str());
}
strategy->SetLeafSpacing(1.0);
strategy->SetRotation(this->GetAngleForOrientation(orientation));
this->Layout->SetLayoutStrategy(strategy);
this->Layout->SetInputData(this->PrunedTree);
this->Layout->Update();
this->LayoutTree = vtkTree::SafeDownCast(this->Layout->GetOutput());
this->CountLeafNodes();
this->ComputeMultipliers();
this->ComputeBounds();
if (this->ColorTree && !this->LegendPositionSet)
{
this->PositionColorLegend();
}
if (this->PrunedTree->GetMTime() > this->MTime)
{
this->DendrogramBuildTime = this->PrunedTree->GetMTime();
}
else
{
this->DendrogramBuildTime = this->MTime;
}
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::ComputeMultipliers()
{
double xMax = 1;
double yMax = 1;
double targetPoint[3];
if (this->Tree->GetNumberOfVertices() > 0)
{
for (vtkIdType edge = 0; edge < this->LayoutTree->GetNumberOfEdges(); ++edge)
{
vtkIdType target = this->LayoutTree->GetTargetVertex(edge);
this->LayoutTree->GetPoint(target, targetPoint);
double x = fabs(targetPoint[0]);
double y = fabs(targetPoint[1]);
if (x > xMax)
{
xMax = x;
}
if (y > yMax)
{
yMax = y;
}
}
}
int orientation = this->GetOrientation();
if (orientation == vtkDendrogramItem::LEFT_TO_RIGHT ||
orientation == vtkDendrogramItem::RIGHT_TO_LEFT)
{
this->MultiplierX = (this->LeafSpacing * (this->NumberOfLeafNodes - 1)) / yMax;
this->MultiplierY = this->MultiplierX;
}
else
{
this->MultiplierY = (this->LeafSpacing * (this->NumberOfLeafNodes - 1)) / xMax;
this->MultiplierX = this->MultiplierY;
}
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::ComputeBounds()
{
this->MinX = VTK_DOUBLE_MAX;
this->MinY = VTK_DOUBLE_MAX;
this->MaxX = VTK_DOUBLE_MIN;
this->MaxY = VTK_DOUBLE_MIN;
double sourcePoint[3];
double targetPoint[3];
for (vtkIdType edge = 0; edge < this->LayoutTree->GetNumberOfEdges(); ++edge)
{
vtkIdType source = this->LayoutTree->GetSourceVertex(edge);
this->LayoutTree->GetPoint(source, sourcePoint);
double x0 = this->Position[0] + sourcePoint[0] * this->MultiplierX;
double y0 = this->Position[1] + sourcePoint[1] * this->MultiplierY;
vtkIdType target = this->LayoutTree->GetTargetVertex(edge);
this->LayoutTree->GetPoint(target, targetPoint);
double x1 = this->Position[0] + targetPoint[0] * this->MultiplierX;
double y1 = this->Position[1] + targetPoint[1] * this->MultiplierY;
if (x0 < this->MinX)
{
this->MinX = x0;
}
if (y0 < this->MinY)
{
this->MinY = y0;
}
if (x0 > this->MaxX)
{
this->MaxX = x0;
}
if (y0 > this->MaxY)
{
this->MaxY = y0;
}
if (x1 < this->MinX)
{
this->MinX = x1;
}
if (y1 < this->MinY)
{
this->MinY = y1;
}
if (x1 > this->MaxX)
{
this->MaxX = x1;
}
if (y1 > this->MaxY)
{
this->MaxY = y1;
}
}
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::CountLeafNodes()
{
// figure out how many leaf nodes we have.
this->NumberOfLeafNodes = 0;
for (vtkIdType vertex = 0; vertex < this->LayoutTree->GetNumberOfVertices(); ++vertex)
{
if (!this->LayoutTree->IsLeaf(vertex))
{
continue;
}
++this->NumberOfLeafNodes;
}
}
//------------------------------------------------------------------------------
int vtkDendrogramItem::CountLeafNodes(vtkIdType vertex)
{
// figure out how many leaf nodes descend from vertex.
int numLeaves = 0;
for (vtkIdType child = 0; child < this->Tree->GetNumberOfChildren(vertex); ++child)
{
vtkIdType childVertex = this->Tree->GetChild(vertex, child);
if (this->Tree->IsLeaf(childVertex))
{
++numLeaves;
}
else
{
numLeaves += this->CountLeafNodes(childVertex);
}
}
return numLeaves;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::PaintBuffers(vtkContext2D* painter)
{
// Calculate the extent of the data that is visible within the window.
this->UpdateVisibleSceneExtent(painter);
double xStart, yStart;
double sourcePoint[3];
double targetPoint[3];
int numberOfCollapsedSubTrees = 0;
vtkUnsignedIntArray* vertexIsPruned =
vtkArrayDownCast<vtkUnsignedIntArray>(this->Tree->GetVertexData()->GetArray("VertexIsPruned"));
int orientation = this->GetOrientation();
float previousPenWidth = painter->GetPen()->GetWidth();
painter->GetPen()->SetWidth(this->LineWidth);
// draw the tree
for (vtkIdType edge = 0; edge < this->LayoutTree->GetNumberOfEdges(); ++edge)
{
vtkIdType source = this->LayoutTree->GetSourceVertex(edge);
vtkIdType target = this->LayoutTree->GetTargetVertex(edge);
this->LayoutTree->GetPoint(source, sourcePoint);
this->LayoutTree->GetPoint(target, targetPoint);
double x0 = this->Position[0] + sourcePoint[0] * this->MultiplierX;
double y0 = this->Position[1] + sourcePoint[1] * this->MultiplierY;
double x1 = this->Position[0] + targetPoint[0] * this->MultiplierX;
double y1 = this->Position[1] + targetPoint[1] * this->MultiplierY;
// check if the target vertex is the root of a collapsed tree
bool alreadyDrewCollapsedSubTree = false;
vtkIdType originalId = this->GetOriginalId(target);
double color[4];
double colorKey;
if (vertexIsPruned->GetValue(originalId) > 0)
{
++numberOfCollapsedSubTrees;
float trianglePoints[6], triangleLabelX, triangleLabelY;
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
trianglePoints[0] = x1;
trianglePoints[1] = y0;
trianglePoints[2] = x1 - this->LeafSpacing / 2;
trianglePoints[3] = this->MaxY;
trianglePoints[4] = x1 + this->LeafSpacing / 2;
trianglePoints[5] = this->MaxY;
triangleLabelX = trianglePoints[0];
triangleLabelY = trianglePoints[3] - 1;
painter->GetTextProp()->SetJustificationToRight();
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
trianglePoints[0] = x0;
trianglePoints[1] = y1;
trianglePoints[2] = this->MinX;
trianglePoints[3] = y1 - this->LeafSpacing / 2;
trianglePoints[4] = this->MinX;
trianglePoints[5] = y1 + this->LeafSpacing / 2;
triangleLabelX = trianglePoints[2] + 1;
triangleLabelY = trianglePoints[1];
painter->GetTextProp()->SetJustificationToLeft();
break;
case vtkDendrogramItem::UP_TO_DOWN:
trianglePoints[0] = x1;
trianglePoints[1] = y0;
trianglePoints[2] = x1 - this->LeafSpacing / 2;
trianglePoints[3] = this->MinY;
trianglePoints[4] = x1 + this->LeafSpacing / 2;
trianglePoints[5] = this->MinY;
triangleLabelX = trianglePoints[0];
triangleLabelY = trianglePoints[3] + 1;
painter->GetTextProp()->SetJustificationToRight();
break;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
trianglePoints[0] = x0;
trianglePoints[1] = y1;
trianglePoints[2] = this->MaxX;
trianglePoints[3] = y1 - this->LeafSpacing / 2;
trianglePoints[4] = this->MaxX;
trianglePoints[5] = y1 + this->LeafSpacing / 2;
triangleLabelX = trianglePoints[2] - 1;
triangleLabelY = trianglePoints[1];
painter->GetTextProp()->SetJustificationToRight();
break;
}
if (this->LineIsVisible(
trianglePoints[0], trianglePoints[1], trianglePoints[2], trianglePoints[3]) ||
this->LineIsVisible(
trianglePoints[0], trianglePoints[1], trianglePoints[4], trianglePoints[5]) ||
this->LineIsVisible(
trianglePoints[2], trianglePoints[3], trianglePoints[4], trianglePoints[5]))
{
colorKey = static_cast<double>(vertexIsPruned->GetValue(originalId));
this->TriangleLookupTable->GetColor(colorKey, color);
painter->GetBrush()->SetColorF(color[0], color[1], color[2]);
painter->DrawPolygon(trianglePoints, 3);
if (this->DisplayNumberOfCollapsedLeafNodes)
{
unsigned int numCollapsedLeafNodes = vertexIsPruned->GetValue(originalId);
std::stringstream ss;
ss << numCollapsedLeafNodes;
painter->GetTextProp()->SetVerticalJustificationToCentered();
painter->GetTextProp()->SetOrientation(this->GetTextAngleForOrientation(orientation));
painter->DrawString(triangleLabelX, triangleLabelY, ss.str());
}
}
alreadyDrewCollapsedSubTree = true;
}
// color this portion of the tree based on the target node
if (this->ColorTree)
{
colorKey = this->ColorArray->GetValue(target);
this->TreeLookupTable->GetColor(colorKey, color);
painter->GetPen()->SetColorF(color[0], color[1], color[2]);
}
// when drawing horizontal trees, we want to draw the vertical segment
// before the horizontal segment. The opposite is true when we are
// drawing vertical trees. We use the variables midpointX and midpointY
// to handle this behavior. extendedX and extendedY are used similarly
// for extending leaf nodes below.
double midpointX, midpointY, extendedX, extendedY;
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
midpointX = x1;
midpointY = y0;
extendedX = x1;
extendedY = this->MaxY;
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
midpointX = x0;
midpointY = y1;
extendedX = this->MinX;
extendedY = y1;
break;
case vtkDendrogramItem::UP_TO_DOWN:
midpointX = x1;
midpointY = y0;
extendedX = x1;
extendedY = this->MinY;
break;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
midpointX = x0;
midpointY = y1;
extendedX = this->MaxX;
extendedY = y1;
break;
}
if (this->LineIsVisible(x0, y0, midpointX, midpointY))
{
painter->DrawLine(x0, y0, midpointX, midpointY);
}
if (!alreadyDrewCollapsedSubTree)
{
if (this->LineIsVisible(midpointX, midpointY, x1, y1))
{
painter->DrawLine(midpointX, midpointY, x1, y1);
}
// extend leaf nodes so they line up
if (this->ExtendLeafNodes && !(x1 == extendedX && y1 == extendedY) &&
this->LayoutTree->IsLeaf(target) && this->LineIsVisible(x1, y1, extendedX, extendedY))
{
// we draw these extensions as grey lines to distinguish them
// from the actual lengths of the leaf nodes.
painter->GetPen()->SetColorF(0.75, 0.75, 0.75);
painter->DrawLine(x1, y1, extendedX, extendedY);
// revert to drawing black lines when we're done
painter->GetPen()->SetColorF(0.0, 0.0, 0.0);
}
}
if (this->ColorTree)
{
// revert to drawing thin black lines by default
painter->GetPen()->SetColorF(0.0, 0.0, 0.0);
}
}
painter->GetPen()->SetWidth(previousPenWidth);
// the remainder of this function involves drawing the leaf node labels,
// so we can return now if that feature has been disabled.
if (!this->DrawLabels)
{
return;
}
// special case: all the true leaf nodes have been collapsed.
// This means that there aren't any labels left to draw.
if (this->NumberOfLeafNodes <= numberOfCollapsedSubTrees)
{
return;
}
//"Igq" selected for range of height
int fontSize = painter->ComputeFontSizeForBoundedString("Igq", VTK_FLOAT_MAX, this->LeafSpacing);
// make sure our current zoom level allows for a legibly-sized font
if (fontSize < 8)
{
return;
}
// leave a small amount of space between the tree and the vertex labels
double spacing = this->LeafSpacing * 0.5;
// set up our text property to draw leaf node labels
painter->GetTextProp()->SetColor(0.0, 0.0, 0.0);
painter->GetTextProp()->SetJustificationToLeft();
painter->GetTextProp()->SetVerticalJustificationToCentered();
painter->GetTextProp()->SetOrientation(this->GetTextAngleForOrientation(orientation));
// make sure some of the labels would be visible on screen
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
if (this->SceneBottomLeft[1] > this->MaxY + spacing ||
this->SceneTopRight[1] < this->MaxY + spacing)
{
return;
}
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
if (this->SceneBottomLeft[0] > this->MinX - spacing ||
this->SceneTopRight[0] < this->MinX - spacing)
{
return;
}
painter->GetTextProp()->SetJustificationToRight();
break;
case vtkDendrogramItem::UP_TO_DOWN:
if (this->SceneBottomLeft[1] > this->MinY - spacing ||
this->SceneTopRight[1] < this->MinY - spacing)
{
return;
}
painter->GetTextProp()->SetJustificationToRight();
break;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
if (this->SceneBottomLeft[0] > this->MaxX + spacing ||
this->SceneTopRight[0] < this->MaxX + spacing)
{
return;
}
break;
}
// get array of node names from the tree
vtkStringArray* vertexNames = vtkArrayDownCast<vtkStringArray>(
this->LayoutTree->GetVertexData()->GetAbstractArray(this->VertexNameArrayName.c_str()));
// find our leaf nodes & draw their labels
for (vtkIdType vertex = 0; vertex < this->LayoutTree->GetNumberOfVertices(); ++vertex)
{
if (!this->LayoutTree->IsLeaf(vertex))
{
continue;
}
double point[3];
this->LayoutTree->GetPoint(vertex, point);
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
xStart = this->Position[0] + point[0] * this->MultiplierX;
yStart = this->MaxY + spacing;
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
xStart = this->MinX - spacing;
yStart = this->Position[1] + point[1] * this->MultiplierY;
break;
case vtkDendrogramItem::UP_TO_DOWN:
xStart = this->Position[0] + point[0] * this->MultiplierX;
yStart = this->MinY - spacing;
break;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
xStart = this->MaxX + spacing;
yStart = this->Position[1] + point[1] * this->MultiplierY;
break;
}
std::string vertexName = vertexNames->GetValue(vertex);
if (this->SceneBottomLeft[0] < xStart && this->SceneTopRight[0] > xStart &&
this->SceneBottomLeft[1] < yStart && this->SceneTopRight[1] > yStart)
{
painter->DrawString(xStart, yStart, vertexName);
}
}
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::UpdateVisibleSceneExtent(vtkContext2D* painter)
{
float position[2];
painter->GetTransform()->GetPosition(position);
this->SceneBottomLeft[0] = -position[0];
this->SceneBottomLeft[1] = -position[1];
this->SceneBottomLeft[2] = 0.0;
this->SceneTopRight[0] = static_cast<double>(this->GetScene()->GetSceneWidth() - position[0]);
this->SceneTopRight[1] = static_cast<double>(this->GetScene()->GetSceneHeight() - position[1]);
this->SceneTopRight[2] = 0.0;
vtkNew<vtkMatrix3x3> inverse;
painter->GetTransform()->GetInverse(inverse);
inverse->MultiplyPoint(this->SceneBottomLeft, this->SceneBottomLeft);
inverse->MultiplyPoint(this->SceneTopRight, this->SceneTopRight);
}
//------------------------------------------------------------------------------
bool vtkDendrogramItem::LineIsVisible(double x0, double y0, double x1, double y1)
{
// is the line degenerate, if so skip
if (x0 == x1 && y0 == y1)
{
return false;
}
// use local variables to improve readability
double xMinScene = this->SceneBottomLeft[0];
double yMinScene = this->SceneBottomLeft[1];
double xMaxScene = this->SceneTopRight[0];
double yMaxScene = this->SceneTopRight[1];
// if either end point of the line segment falls within the screen,
// then the line segment is visible.
if ((xMinScene <= x0 && xMaxScene >= x0 && yMinScene <= y0 && yMaxScene >= y0) ||
(xMinScene <= x1 && xMaxScene >= x1 && yMinScene <= y1 && yMaxScene >= y1))
{
return true;
}
// figure out which end point is "greater" than the other in both dimensions
double xMinLine, xMaxLine, yMinLine, yMaxLine;
if (x0 < x1)
{
xMinLine = x0;
xMaxLine = x1;
}
else
{
xMinLine = x1;
xMaxLine = x0;
}
if (y0 < y1)
{
yMinLine = y0;
yMaxLine = y1;
}
else
{
yMinLine = y1;
yMaxLine = y0;
}
// case where the Y range of the line falls within the visible scene
// and the X range of the line contains the entire visible scene
if (yMinScene <= yMinLine && yMaxScene >= yMinLine && yMinScene <= yMaxLine &&
yMaxScene >= yMaxLine && xMinLine <= xMinScene && xMaxLine >= xMaxScene)
{
return true;
}
// case where the X range of the line falls within the visible scene
// and the Y range of the line contains the entire visible scene
if (xMinScene <= xMinLine && xMaxScene >= xMinLine && xMinScene <= xMaxLine &&
xMaxScene >= xMaxLine && yMinLine <= yMinScene && yMaxLine >= yMaxScene)
{
return true;
}
return false;
}
//------------------------------------------------------------------------------
bool vtkDendrogramItem::MouseDoubleClickEvent(const vtkContextMouseEvent& event)
{
// get the position of the double click and convert it to scene coordinates
double pos[3];
vtkNew<vtkMatrix3x3> inverse;
pos[0] = event.GetPos().GetX();
pos[1] = event.GetPos().GetY();
pos[2] = 0;
this->GetScene()->GetTransform()->GetInverse(inverse);
inverse->MultiplyPoint(pos, pos);
bool rotatedTree = false;
int orientation = this->GetOrientation();
if (orientation == vtkDendrogramItem::UP_TO_DOWN || orientation == vtkDendrogramItem::DOWN_TO_UP)
{
rotatedTree = true;
}
// this event is only captured within the tree (not the vertex labels)
if ((!rotatedTree && pos[0] <= this->MaxX && pos[0] >= this->MinX) ||
(rotatedTree && pos[1] <= this->MaxY && pos[1] >= this->MinY))
{
vtkIdType collapsedSubTree = this->GetClickedCollapsedSubTree(pos[0], pos[1]);
if (collapsedSubTree != -1)
{
// re-expand the subtree rooted at this vertex
this->ExpandSubTree(collapsedSubTree);
}
else
{
// collapse the subtree rooted at this vertex
vtkIdType closestVertex =
this->GetClosestVertex((pos[0] - this->Position[0]) / this->MultiplierX,
(pos[1] - this->Position[1]) / this->MultiplierY);
this->CollapseSubTree(closestVertex);
}
this->Scene->SetDirty(true);
return true;
}
return false;
}
//------------------------------------------------------------------------------
vtkIdType vtkDendrogramItem::GetClickedCollapsedSubTree(double x, double y)
{
// iterate over all the collapsed subtrees to see if this click refers
// to one of them.
vtkUnsignedIntArray* vertexIsPruned =
vtkArrayDownCast<vtkUnsignedIntArray>(this->Tree->GetVertexData()->GetArray("VertexIsPruned"));
vtkIdTypeArray* originalIdArray =
vtkArrayDownCast<vtkIdTypeArray>(this->PrunedTree->GetVertexData()->GetArray("OriginalId"));
int orientation = this->GetOrientation();
for (vtkIdType originalId = 0; originalId < vertexIsPruned->GetNumberOfTuples(); ++originalId)
{
if (vertexIsPruned->GetValue(originalId) > 0)
{
// Find PrunedTree's vertex that corresponds to this originalId.
for (vtkIdType prunedId = 0; prunedId < originalIdArray->GetNumberOfTuples(); ++prunedId)
{
if (originalIdArray->GetValue(prunedId) == originalId)
{
// determined where this collapsed subtree is rooted.
double point[3];
this->LayoutTree->GetPoint(prunedId, point);
point[0] = point[0] * this->MultiplierX + this->Position[0];
point[1] = point[1] * this->MultiplierY + this->Position[1];
// we also need the location of this node's parent
double parentPoint[3];
this->LayoutTree->GetPoint(this->LayoutTree->GetParent(prunedId), parentPoint);
parentPoint[0] = parentPoint[0] * this->MultiplierX + this->Position[0];
parentPoint[1] = parentPoint[1] * this->MultiplierY + this->Position[1];
float xMin = 0.0;
float xMax = 0.0;
float yMin = 0.0;
float yMax = 0.0;
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
// proper width (X) range: within +/- LeafSpacing of the vertex's
// X value.
xMin = point[0] - this->LeafSpacing / 2;
xMax = point[0] + this->LeafSpacing / 2;
// proper height (Y) range: >= parent's Y value
yMin = parentPoint[1];
yMax = this->MaxY;
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
// proper width (X) range: <= parent's X value.
xMin = this->MinX;
xMax = parentPoint[0];
// proper height (Y) range: within +/- LeafSpacing of the vertex's
// Y value.
yMin = point[1] - this->LeafSpacing / 2;
yMax = point[1] + this->LeafSpacing / 2;
break;
case vtkDendrogramItem::UP_TO_DOWN:
// proper width (X) range: within +/- LeafSpacing of the vertex's
// X value.
xMin = point[0] - this->LeafSpacing / 2;
xMax = point[0] + this->LeafSpacing / 2;
// proper height (Y) range: <= parent's Y value
yMin = this->MinY;
yMax = parentPoint[1];
break;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
// proper width (X) range: >= parent's X value.
xMin = parentPoint[0];
xMax = this->MaxX;
// proper height (Y) range: within +/- LeafSpacing of the vertex's
// Y value.
yMin = point[1] - this->LeafSpacing / 2;
yMax = point[1] + this->LeafSpacing / 2;
break;
}
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax)
{
return prunedId;
}
break;
}
}
}
}
return -1;
}
//------------------------------------------------------------------------------
vtkIdType vtkDendrogramItem::GetClosestVertex(double x, double y)
{
double minDistance = VTK_DOUBLE_MAX;
vtkIdType closestVertex = -1;
for (vtkIdType vertex = 0; vertex < this->LayoutTree->GetNumberOfVertices(); ++vertex)
{
if (this->LayoutTree->IsLeaf(vertex))
{
continue;
}
double point[3];
this->LayoutTree->GetPoint(vertex, point);
double distance = sqrt((x - point[0]) * (x - point[0]) + (y - point[1]) * (y - point[1]));
if (distance < minDistance)
{
minDistance = distance;
closestVertex = vertex;
}
}
return closestVertex;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::CollapseSubTree(vtkIdType vertex)
{
// no removing the root of the tree
vtkIdType root = this->PrunedTree->GetRoot();
if (vertex == root)
{
return;
}
// look up the original ID of the vertex that's being collapsed.
vtkIdTypeArray* originalIdArray =
vtkArrayDownCast<vtkIdTypeArray>(this->PrunedTree->GetVertexData()->GetArray("OriginalId"));
vtkIdType originalId = originalIdArray->GetValue(vertex);
// use this value as the index to the original (un-reindexed) tree's
// "VertexIsPruned" array. Mark that vertex as pruned by recording
// how many collapsed leaf nodes exist beneath it.
int numLeavesCollapsed = this->CountLeafNodes(originalId);
// make sure we're not about to collapse away the whole tree
int totalLeaves = this->CountLeafNodes(root);
if (numLeavesCollapsed >= totalLeaves)
{
return;
}
// no collapsing of leaf nodes. This should never happen, but it doesn't
// hurt to be safe.
if (numLeavesCollapsed == 0)
{
return;
}
vtkUnsignedIntArray* vertexIsPruned =
vtkArrayDownCast<vtkUnsignedIntArray>(this->Tree->GetVertexData()->GetArray("VertexIsPruned"));
vertexIsPruned->SetValue(originalId, numLeavesCollapsed);
vtkNew<vtkTree> prunedTreeCopy;
prunedTreeCopy->ShallowCopy(this->PrunedTree);
this->PruneFilter->SetInputData(prunedTreeCopy);
this->PruneFilter->SetParentVertex(vertex);
this->PruneFilter->Update();
this->PrunedTree = this->PruneFilter->GetOutput();
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::ExpandSubTree(vtkIdType vertex)
{
// mark this vertex as "not pruned"
vtkUnsignedIntArray* vertexIsPruned =
vtkArrayDownCast<vtkUnsignedIntArray>(this->Tree->GetVertexData()->GetArray("VertexIsPruned"));
vtkIdType vertexOriginalId = this->GetOriginalId(vertex);
vertexIsPruned->SetValue(vertexOriginalId, 0);
// momentarily revert PrunedTree to the full (unpruned) Tree.
this->PrunedTree->DeepCopy(this->Tree);
// re-prune as necessary. this->Tree has the list of originalIds that
// need to be re-pruned.
for (vtkIdType originalId = 0; originalId < vertexIsPruned->GetNumberOfTuples(); ++originalId)
{
if (vertexIsPruned->GetValue(originalId) > 0)
{
// Find PrunedTree's vertex that corresponds to this originalId.
// Use this to re-collapse the subtrees that were not just expanded.
vtkIdTypeArray* originalIdArray =
vtkArrayDownCast<vtkIdTypeArray>(this->PrunedTree->GetVertexData()->GetArray("OriginalId"));
for (vtkIdType prunedId = 0; prunedId < originalIdArray->GetNumberOfTuples(); ++prunedId)
{
if (originalIdArray->GetValue(prunedId) == originalId)
{
this->CollapseSubTree(prunedId);
break;
}
}
}
}
}
//------------------------------------------------------------------------------
vtkIdType vtkDendrogramItem::GetOriginalId(vtkIdType vertex)
{
vtkIdTypeArray* originalIdArray =
vtkArrayDownCast<vtkIdTypeArray>(this->PrunedTree->GetVertexData()->GetArray("OriginalId"));
return originalIdArray->GetValue(vertex);
}
//------------------------------------------------------------------------------
vtkIdType vtkDendrogramItem::GetPrunedIdForOriginalId(vtkIdType originalId)
{
vtkIdTypeArray* originalIdArray =
vtkArrayDownCast<vtkIdTypeArray>(this->PrunedTree->GetVertexData()->GetArray("OriginalId"));
for (vtkIdType i = 0; i < originalIdArray->GetNumberOfTuples(); ++i)
{
if (originalIdArray->GetValue(i) == originalId)
{
return i;
}
}
return -1;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::CollapseToNumberOfLeafNodes(unsigned int n)
{
// check that the number requested is actually smaller than the number of
// leaf nodes in the tree.
unsigned int numLeaves = this->CountLeafNodes(this->Tree->GetRoot());
if (n >= numLeaves)
{
vtkWarningMacro(<< "n >= total leaf nodes");
return;
}
// reset pruned tree to contain the entire input tree
this->PrunedTree->DeepCopy(this->Tree);
// Initialize a priority queue of vertices based on their weight.
// Vertices with lower weight (closer to the root) have a higher priority.
std::priority_queue<vtkDendrogramItem::WeightedVertex,
std::vector<vtkDendrogramItem::WeightedVertex>, vtkDendrogramItem::CompareWeightedVertices>
queue;
std::vector<vtkIdType> verticesToCollapse;
vtkDoubleArray* nodeWeights = vtkArrayDownCast<vtkDoubleArray>(
this->Tree->GetVertexData()->GetAbstractArray(this->DistanceArrayName.c_str()));
// initially, the priority queue contains the children of the root node.
vtkIdType root = this->Tree->GetRoot();
for (vtkIdType child = 0; child < this->Tree->GetNumberOfChildren(root); ++child)
{
vtkIdType childVertex = this->Tree->GetChild(root, child);
double weight = 0.0;
if (nodeWeights != nullptr)
{
weight = nodeWeights->GetValue(childVertex);
}
else
{
weight = static_cast<double>(this->Tree->GetLevel(childVertex));
}
vtkDendrogramItem::WeightedVertex v = { childVertex, weight };
queue.push(v);
}
// use the priority queue to find the vertices that we should collapse.
unsigned int numberOfLeafNodesFound = 0;
while (queue.size() + numberOfLeafNodesFound < n)
{
vtkDendrogramItem::WeightedVertex v = queue.top();
queue.pop();
if (this->Tree->GetNumberOfChildren(v.ID) == 0)
{
verticesToCollapse.push_back(v.ID);
++numberOfLeafNodesFound;
continue;
}
for (vtkIdType child = 0; child < this->Tree->GetNumberOfChildren(v.ID); ++child)
{
vtkIdType childVertex = this->Tree->GetChild(v.ID, child);
double weight = 0.0;
if (nodeWeights != nullptr)
{
weight = nodeWeights->GetValue(childVertex);
}
else
{
weight = static_cast<double>(this->Tree->GetLevel(childVertex));
}
vtkDendrogramItem::WeightedVertex v2 = { childVertex, weight };
queue.push(v2);
}
}
// collapse the vertices that we found.
for (unsigned int i = 0; i < verticesToCollapse.size(); ++i)
{
vtkIdType prunedId = this->GetPrunedIdForOriginalId(verticesToCollapse[i]);
if (prunedId == -1)
{
vtkErrorMacro("prunedId is -1");
continue;
}
this->CollapseSubTree(prunedId);
}
while (!queue.empty())
{
vtkDendrogramItem::WeightedVertex v = queue.top();
queue.pop();
vtkIdType prunedId = this->GetPrunedIdForOriginalId(v.ID);
if (prunedId == -1)
{
vtkErrorMacro("prunedId is -1");
continue;
}
this->CollapseSubTree(prunedId);
}
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::SetColorArray(const char* arrayName)
{
this->ColorArray =
vtkArrayDownCast<vtkDoubleArray>(this->Tree->GetVertexData()->GetArray(arrayName));
if (!this->ColorArray)
{
vtkErrorMacro("Could not downcast " << arrayName << " to a vtkDoubleArray");
this->ColorTree = false;
return;
}
this->ColorTree = true;
double minValue = VTK_DOUBLE_MAX;
double maxValue = VTK_DOUBLE_MIN;
for (vtkIdType id = 0; id < this->ColorArray->GetNumberOfTuples(); ++id)
{
double d = this->ColorArray->GetValue(id);
if (d > maxValue)
{
maxValue = d;
}
if (d < minValue)
{
minValue = d;
}
}
// special case: when there is no range of values to display, all edges should
// be drawn in grey. Without this, all the edges would be drawn in either red
// or blue.
if (minValue == maxValue)
{
this->TreeLookupTable->SetNumberOfTableValues(1);
this->TreeLookupTable->SetTableValue(0, 0.60, 0.60, 0.60);
// this is done to prevent the legend from being drawn
this->LegendPositionSet = true;
return;
}
// how much we vary the colors from step to step
double inc = 0.06;
// setup the color lookup table. It will contain 10 shades of red,
// 10 shades of blue, and a grey neutral value.
this->TreeLookupTable->SetNumberOfTableValues(21);
if (fabs(maxValue) > fabs(minValue))
{
this->TreeLookupTable->SetRange(-maxValue, maxValue);
}
else
{
this->TreeLookupTable->SetRange(minValue, -minValue);
}
for (vtkIdType i = 0; i < 10; ++i)
{
this->TreeLookupTable->SetTableValue(i, 1.0, 0.25 + inc * i, 0.25 + inc * i);
}
this->TreeLookupTable->SetTableValue(10, 0.60, 0.60, 0.60);
for (vtkIdType i = 11; i < 21; ++i)
{
this->TreeLookupTable->SetTableValue(i, 0.85 - inc * (i - 10), 0.85 - inc * (i - 10), 1.0);
}
// initialize color legend
this->ColorLegend->SetTransferFunction(this->TreeLookupTable);
this->ColorLegend->SetTitle(arrayName);
this->PositionColorLegend();
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::PositionColorLegend()
{
// bail out early if we don't have meaningful bounds yet.
if (this->MinX > this->MaxX || this->MinY > this->MaxY)
{
return;
}
int orientation = this->GetOrientation();
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
case vtkDendrogramItem::UP_TO_DOWN:
this->ColorLegend->SetHorizontalAlignment(vtkChartLegend::RIGHT);
this->ColorLegend->SetVerticalAlignment(vtkChartLegend::CENTER);
this->ColorLegend->SetOrientation(vtkColorLegend::VERTICAL);
this->ColorLegend->SetPoint(
this->MinX - this->LeafSpacing, this->MinY + (this->MaxY - this->MinY) / 2.0);
this->ColorLegend->SetTextureSize(
this->ColorLegend->GetSymbolWidth(), this->MaxY - this->MinY);
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
this->ColorLegend->SetHorizontalAlignment(vtkChartLegend::CENTER);
this->ColorLegend->SetVerticalAlignment(vtkChartLegend::TOP);
this->ColorLegend->SetOrientation(vtkColorLegend::HORIZONTAL);
this->ColorLegend->SetPoint(
this->MinX + (this->MaxX - this->MinX) / 2.0, this->MinY - this->LeafSpacing);
this->ColorLegend->SetTextureSize(
this->MaxX - this->MinX, this->ColorLegend->GetSymbolWidth());
break;
}
this->ColorLegend->Update();
this->ColorLegend->SetVisible(true);
this->Scene->SetDirty(true);
this->LegendPositionSet = true;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::SetOrientation(int orientation)
{
this->SetOrientation(this->Tree, orientation);
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::SetOrientation(vtkTree* tree, int orientation)
{
vtkIntArray* existingArray =
vtkArrayDownCast<vtkIntArray>(tree->GetFieldData()->GetArray("orientation"));
if (existingArray)
{
existingArray->SetValue(0, orientation);
}
else
{
vtkSmartPointer<vtkIntArray> orientationArray = vtkSmartPointer<vtkIntArray>::New();
orientationArray->SetNumberOfComponents(1);
orientationArray->SetName("orientation");
orientationArray->InsertNextValue(orientation);
tree->GetFieldData()->AddArray(orientationArray);
}
if (tree == this->Tree)
{
this->SetOrientation(this->PrunedTree, orientation);
this->SetOrientation(this->LayoutTree, orientation);
}
}
//------------------------------------------------------------------------------
int vtkDendrogramItem::GetOrientation()
{
vtkIntArray* orientationArray =
vtkArrayDownCast<vtkIntArray>(this->Tree->GetFieldData()->GetArray("orientation"));
if (orientationArray)
{
return orientationArray->GetValue(0);
}
return vtkDendrogramItem::LEFT_TO_RIGHT;
}
//------------------------------------------------------------------------------
double vtkDendrogramItem::GetAngleForOrientation(int orientation)
{
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
return 180.0;
case vtkDendrogramItem::RIGHT_TO_LEFT:
return 270.0;
case vtkDendrogramItem::UP_TO_DOWN:
return 0.0;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
return 90.0;
}
}
//------------------------------------------------------------------------------
double vtkDendrogramItem::GetTextAngleForOrientation(int orientation)
{
switch (orientation)
{
case vtkDendrogramItem::DOWN_TO_UP:
return 90.0;
case vtkDendrogramItem::RIGHT_TO_LEFT:
return 0.0;
case vtkDendrogramItem::UP_TO_DOWN:
return 270.0;
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
return 0.0;
}
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::GetBounds(double bounds[4])
{
bounds[0] = this->MinX;
bounds[1] = this->MaxX;
bounds[2] = this->MinY;
bounds[3] = this->MaxY;
if (this->LabelWidth == 0.0)
{
return;
}
double spacing = this->LeafSpacing * 0.5;
switch (this->GetOrientation())
{
case vtkDendrogramItem::LEFT_TO_RIGHT:
default:
bounds[1] += spacing + this->LabelWidth;
break;
case vtkDendrogramItem::UP_TO_DOWN:
bounds[2] -= spacing + this->LabelWidth;
break;
case vtkDendrogramItem::RIGHT_TO_LEFT:
bounds[0] -= spacing + this->LabelWidth;
break;
case vtkDendrogramItem::DOWN_TO_UP:
bounds[3] += spacing + this->LabelWidth;
break;
}
}
//------------------------------------------------------------------------------
float vtkDendrogramItem::GetLabelWidth()
{
return this->LabelWidth;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::ComputeLabelWidth(vtkContext2D* painter)
{
this->LabelWidth = 0.0;
if (!this->DrawLabels)
{
return;
}
int fontSize = painter->ComputeFontSizeForBoundedString("Igq", VTK_FLOAT_MAX, this->LeafSpacing);
if (fontSize < 8)
{
return;
}
// temporarily set text to default orientation
double orientation = painter->GetTextProp()->GetOrientation();
painter->GetTextProp()->SetOrientation(0.0);
// get array of node names from the tree
vtkStringArray* vertexNames = vtkArrayDownCast<vtkStringArray>(
this->LayoutTree->GetVertexData()->GetAbstractArray(this->VertexNameArrayName.c_str()));
float bounds[4];
for (vtkIdType i = 0; i < vertexNames->GetNumberOfTuples(); ++i)
{
painter->ComputeStringBounds(vertexNames->GetValue(i), bounds);
if (bounds[2] > this->LabelWidth)
{
this->LabelWidth = bounds[2];
}
}
// restore orientation
painter->GetTextProp()->SetOrientation(orientation);
}
//------------------------------------------------------------------------------
bool vtkDendrogramItem::GetPositionOfVertex(const std::string& vertexName, double position[2])
{
vtkStringArray* vertexNames = vtkArrayDownCast<vtkStringArray>(
this->LayoutTree->GetVertexData()->GetAbstractArray(this->VertexNameArrayName.c_str()));
vtkIdType vertex = vertexNames->LookupValue(vertexName);
if (vertex == -1)
{
return false;
}
double point[3];
this->LayoutTree->GetPoint(vertex, point);
position[0] = this->Position[0] + point[0] * this->MultiplierX;
position[1] = this->Position[1] + point[1] * this->MultiplierY;
return true;
}
//------------------------------------------------------------------------------
bool vtkDendrogramItem::Hit(const vtkContextMouseEvent& vtkNotUsed(mouse))
{
// If we are interactive, we want to catch anything that propagates to the
// background, otherwise we do not want any mouse events.
return this->Interactive;
}
//------------------------------------------------------------------------------
void vtkDendrogramItem::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << "Tree: " << (this->Tree ? "" : "(null)") << std::endl;
if (this->Tree->GetNumberOfVertices() > 0)
{
this->Tree->PrintSelf(os, indent.GetNextIndent());
}
}
VTK_ABI_NAMESPACE_END
|