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
|
/*=========================================================================
Program: ParaView
Module: vtkPVComparativeView.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkPVComparativeView.h"
#include "vtkClientServerStream.h"
#include "vtkCollection.h"
#include "vtkImageData.h"
#include "vtkMemberFunctionCommand.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPVComparativeAnimationCue.h"
#include "vtkProcessModule.h"
#include "vtkSMCameraLink.h"
#include "vtkSMComparativeAnimationCueProxy.h"
#include "vtkSMParaViewPipelineController.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMPropertyIterator.h"
#include "vtkSMProxyLink.h"
#include "vtkSMProxyListDomain.h"
#include "vtkSMProxyProperty.h"
#include "vtkSMRepresentationProxy.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMUtilities.h"
#include "vtkSMViewProxy.h"
#include "vtkSmartPointer.h"
#include "vtkVectorOperators.h"
#include <algorithm>
#include <cassert>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
namespace
{
typedef vtkTypeInt64 Fixed64;
static inline Fixed64 toFixed(int i)
{
return (Fixed64)i * 256;
}
static inline int fRound(Fixed64 i)
{
return (i % 256 < 128) ? i / 256 : 1 + i / 256;
}
struct LayoutStruct
{
int pos;
int size;
};
// This code is loosely based on the QGridLayout's workhouse qGeomCalc (in qlayoutengine.cpp
// for Qt version 5.6) which portions out available space to a collection of items in a row or
// a column.
//
// The calculation is done in fixed point: "fixed" variables are scaled by a factor of 256.
void vtkGeomCalc(
std::vector<LayoutStruct>& chain, int start, int count, int pos, int space, int spacing)
{
assert(start == 0 && count >= 1 && pos >= 0 && space >= 0 && spacing >= 0);
// amount of space left after borders are added between views.
int space_left = space - (count > 1 ? count - 1 : 0) * spacing;
int space_allocated = 0;
Fixed64 fp_space = toFixed(space_left);
Fixed64 fp_w = 0;
for (int i = start; i < count; ++i)
{
LayoutStruct* data = &chain[i];
fp_w += (fp_space / count);
int w = fRound(fp_w);
data->size = w;
fp_w -= toFixed(w); // give the difference to the next.
space_allocated += w;
}
space_left -= space_allocated;
assert(space_left == 0);
chain[start].pos = pos;
for (int i = start + 1; i < count; ++i)
{
chain[i].pos = chain[i - 1].pos + chain[i - 1].size + spacing;
}
}
}
#define ENSURE_INIT() \
if (this->RootView == NULL) \
{ \
return; \
}
//----------------------------------------------------------------------------
namespace vtkPVComparativeViewNS
{
static void vtkAddRepresentation(vtkSMProxy* view, vtkSMProxy* repr)
{
// We are always using cache in this view.
vtkSMPropertyHelper(repr, "ForceUseCache", true).Set(1);
vtkSMPropertyHelper(repr, "ForcedCacheKey", true).Set(1);
repr->UpdateVTKObjects();
vtkSMPropertyHelper(view, "Representations").Add(repr);
view->UpdateVTKObjects();
}
static void vtkRemoveRepresentation(vtkSMProxy* view, vtkSMProxy* repr)
{
vtkSMPropertyHelper(view, "Representations").Remove(repr);
view->UpdateVTKObjects();
}
//----------------------------------------------------------------------------
/**
* @class vtkCloningVector
* @brief base class for a vector that resizes itself with clones of the "root".
*
* vtkCloningVector is allows to create and maintain clones for any proxy. The vector can be
* resized to any size (> 1) and it will fill itself will clones of the root proxy (at index 0).
* In keeps properties on the clones linked so that when the properties on the root change,
* the clones' properties also change.
*
*/
//----------------------------------------------------------------------------
class vtkCloningVector : public vtkObject
{
public:
vtkTypeMacro(vtkCloningVector, vtkObject);
/**
* @returns item at a given index, if valid, else nullptr is returned.
*/
vtkSMProxy* GetItem(size_t index) const
{
return (index < this->Items.size()) ? this->Items[index].GetPointer() : nullptr;
}
/**
* @returns root proxy
*/
vtkSMProxy* GetRoot() const { return this->GetItem(0); }
/**
* @returns last proxy or nullptr if the vector hasn't been initialized yet.
*/
vtkSMProxy* GetBack() const
{
return (this->Items.size() > 0) ? this->Items.back().GetPointer() : nullptr;
}
/**
* @returns number of items
*/
size_t GetNumberOfItems() const { return this->Items.size(); }
/**
* Resize to the given size (>=1). Any extra proxies will be removed by iteratively calling
* this->PopBack(). New proxies may be added by iteratively calling this->PushBack().
*
* @returns true if anything changed, otherwise false.
*/
virtual bool Resize(size_t num_items)
{
assert(num_items >= 1);
bool changed = false;
// Remove extra items.
for (size_t cc = this->Items.size() - 1; cc >= num_items; --cc)
{
this->PopBack();
changed = true;
}
// Add new items if needed.
for (size_t cc = this->Items.size(); cc < num_items; ++cc)
{
this->PushBack();
changed = true;
}
assert(this->GetNumberOfItems() == num_items);
return changed;
}
/**
* Create a new clone of the root and append it to the vector.
*/
virtual vtkSMProxy* PushBack()
{
vtkSMProxy* root = this->GetRoot();
assert(root);
vtkSMSessionProxyManager* pxm = root->GetSessionProxyManager();
vtkSmartPointer<vtkSMProxy> clone =
vtkSmartPointer<vtkSMProxy>::Take(pxm->NewProxy(root->GetXMLGroup(), root->GetXMLName()));
if (clone)
{
vtkNew<vtkSMParaViewPipelineController> controller;
controller->PreInitializeProxy(clone);
this->Copy(root, clone);
controller->PostInitializeProxy(clone);
clone->UpdateVTKObjects();
this->Link->AddLinkedProxy(clone, vtkSMLink::OUTPUT);
this->Items.push_back(clone);
// Handle proxy-list domain proxies. Link proxies in the proxy list domains on the clone
// with those in the corresponding domains on the root.
for (auto iter = this->PLDLinks.begin(); iter != this->PLDLinks.end(); ++iter)
{
vtkSMProperty* prop = clone->GetProperty(iter->first.c_str());
vtkSMProxyListDomain* pld = prop
? vtkSMProxyListDomain::SafeDownCast(prop->FindDomain("vtkSMProxyListDomain"))
: nullptr;
if (pld)
{
for (int cc = 0, max = pld->GetNumberOfProxies(); cc < max; ++cc)
{
if (cc < static_cast<int>(iter->second.size()))
{
this->Copy(iter->second[cc]->GetLinkedProxy(0), pld->GetProxy(cc));
iter->second[cc]->AddLinkedProxy(pld->GetProxy(cc), vtkSMLink::OUTPUT);
}
}
}
}
}
return clone;
}
/**
* Remove the last element in the vector.
* This will never remove the root (or 0th element) on a initialized vector.
*/
virtual void PopBack()
{
if (this->Items.size() <= 1)
{
// never pop the root.
return;
}
vtkSmartPointer<vtkSMProxy> back = this->Items.back();
assert(back);
// Handle proxy-list domain. Remove proxies in proxy-list-domain on the proxy being removed
// from the PLDLinks.
for (auto iter = this->PLDLinks.begin(); iter != this->PLDLinks.end(); ++iter)
{
vtkSMProperty* prop = back->GetProperty(iter->first.c_str());
vtkSMProxyListDomain* pld = prop
? vtkSMProxyListDomain::SafeDownCast(prop->FindDomain("vtkSMProxyListDomain"))
: nullptr;
if (pld)
{
for (int cc = 0, max = pld->GetNumberOfProxies(); cc < max; ++cc)
{
if (cc < static_cast<int>(iter->second.size()))
{
iter->second[cc]->RemoveLinkedProxy(pld->GetProxy(cc));
}
}
}
}
this->Link->RemoveLinkedProxy(back);
this->Items.pop_back();
}
protected:
vtkCloningVector() {}
virtual ~vtkCloningVector() {}
/**
* This must be called to initialize the vector with the "root".
* \c exceptions is used to indicate the properties that should be
* linked together.
*/
void Setup(vtkSMProxy* root, const std::set<std::string>& exceptions)
{
assert(root != nullptr);
assert(this->Items.size() == 0);
this->Items.resize(1);
this->Items[0] = root;
for (auto iter = exceptions.begin(); iter != exceptions.end(); ++iter)
{
this->Link->AddException(iter->c_str());
}
this->Link->AddLinkedProxy(root, vtkSMLink::INPUT);
this->Exceptions = exceptions;
// For all properties with proxy-list domains, we have to link the
// proxies in those domains too.
vtkSmartPointer<vtkSMPropertyIterator> iter =
vtkSmartPointer<vtkSMPropertyIterator>::Take(root->NewPropertyIterator());
for (iter->Begin(); !iter->IsAtEnd(); iter->Next())
{
vtkSMProperty* prop = iter->GetProperty();
vtkSMProxyListDomain* pld = prop
? vtkSMProxyListDomain::SafeDownCast(prop->FindDomain("vtkSMProxyListDomain"))
: nullptr;
if (!pld)
{
continue;
}
std::vector<vtkSmartPointer<vtkSMProxyLink> >& links = this->PLDLinks[iter->GetKey()];
links.resize(pld->GetNumberOfProxies());
for (int cc = 0, max = pld->GetNumberOfProxies(); cc < max; ++cc)
{
vtkNew<vtkSMProxyLink> alink;
alink->AddLinkedProxy(pld->GetProxy(cc), vtkSMLink::INPUT);
links[cc] = alink.Get();
}
}
}
private:
vtkCloningVector(const vtkCloningVector&) VTK_DELETE_FUNCTION;
void operator=(const vtkCloningVector&) VTK_DELETE_FUNCTION;
/**
* Copy all properties from source to clone, excluding the ones in
* this->Exceptions.
*/
void Copy(vtkSMProxy* source, vtkSMProxy* clone)
{
vtkSmartPointer<vtkSMPropertyIterator> iterSource;
vtkSmartPointer<vtkSMPropertyIterator> iterDest;
iterSource.TakeReference(source->NewPropertyIterator());
iterDest.TakeReference(clone->NewPropertyIterator());
// Since source/clone are exact copies, the following is safe.
for (; !iterSource->IsAtEnd() && !iterDest->IsAtEnd(); iterSource->Next(), iterDest->Next())
{
// Skip the property if it is in the exceptions list.
if (this->Exceptions.find(iterSource->GetKey()) != this->Exceptions.end())
{
continue;
}
vtkSMProperty* destProp = iterDest->GetProperty();
// Skip the property if it is information only or is internal.
if (destProp->GetInformationOnly())
{
continue;
}
// Copy the property from source to dest
destProp->Copy(iterSource->GetProperty());
// For proxy properties with proxy-list domains, aka enumeration-properties
// with proxies for values, we need to link the proxies in the domain too.
}
}
std::vector<vtkSmartPointer<vtkSMProxy> > Items;
vtkNew<vtkSMProxyLink> Link;
std::set<std::string> Exceptions;
// key: property name
// value: ordered list of vtkSMProxyLinks for each of the proxies in the proxy list domain.
std::map<std::string, std::vector<vtkSmartPointer<vtkSMProxyLink> > > PLDLinks;
};
//----------------------------------------------------------------------------
/**
* @class vtkCloningVectorOfRepresentations
* @brief vtkCloningVector specialization for representations.
*/
//----------------------------------------------------------------------------
class vtkCloningVectorOfRepresentations : public vtkCloningVector
{
public:
vtkTypeMacro(vtkCloningVectorOfRepresentations, vtkCloningVector);
static vtkCloningVectorOfRepresentations* New()
{
VTK_STANDARD_NEW_BODY(vtkCloningVectorOfRepresentations);
}
/**
* Clear cached data for all representations.
*/
void ClearDataCaches(size_t index)
{
// Mark all representations modified. This clears their caches. It's essential
// that SetUseCache(false) is called before we do this, otherwise the caches
// are not cleared.
if (vtkSMRepresentationProxy* reprToClear =
vtkSMRepresentationProxy::SafeDownCast(this->GetItem(index)))
{
vtkSMPropertyHelper helper(reprToClear, "ForceUseCache", true);
helper.Set(0);
reprToClear->UpdateProperty("ForceUseCache");
reprToClear->ClearMarkedModified(); // HACK.
reprToClear->MarkDirty(NULL);
helper.Set(1);
reprToClear->UpdateProperty("ForceUseCache");
}
}
/**
* Initializes the instance with the given root representation.
*/
void Initialize(vtkSMProxy* root)
{
std::set<std::string> exceptions = { "ForceUseCache", "ForceCacheKey" };
this->Setup(root, exceptions);
}
protected:
vtkCloningVectorOfRepresentations() {}
~vtkCloningVectorOfRepresentations() {}
private:
vtkCloningVectorOfRepresentations(const vtkCloningVectorOfRepresentations&) VTK_DELETE_FUNCTION;
void operator=(const vtkCloningVectorOfRepresentations&) VTK_DELETE_FUNCTION;
};
//----------------------------------------------------------------------------
/**
* @class vtkCloningVectorOfViews
* @brief vtkCloningVector specialization for views.
*
* vtkCloningVectorOfViews is a vtkCloningVector specialization for views. It also handles
* cloning of representations for clones of the views.
*/
//----------------------------------------------------------------------------
class vtkCloningVectorOfViews : public vtkCloningVector
{
public:
vtkTypeMacro(vtkCloningVectorOfViews, vtkCloningVector);
static vtkCloningVectorOfViews* New() { VTK_STANDARD_NEW_BODY(vtkCloningVectorOfViews); }
/*
* Must be set to true if all comparisons are to be shown in the same view.
*/
void SetOverlayViews(bool val) { this->OverlayViews = val; }
/**
* Type this->GetItem()
*/
vtkSMViewProxy* GetView(size_t index) const
{
return vtkSMViewProxy::SafeDownCast(this->GetItem(index));
}
/**
* Initialize instance with a root view
*/
void Initialize(vtkSMViewProxy* root)
{
std::set<std::string> exceptions = { // Every view keeps their own representations.
"Representations",
// This view computes view size/view position for each view based on the
// layout.
"ViewSize", "ViewTime", "CacheKey", "UseCache", "ViewPosition",
// Camera is linked via CameraLink.
"CameraPositionInfo", "CameraPosition", "CameraFocalPointInfo", "CameraFocalPoint",
"CameraViewUpInfo", "CameraViewUp", "CameraViewAngleInfo", "CameraViewAngle"
};
this->Setup(root, exceptions);
this->CameraLink->AddLinkedProxy(root, vtkSMLink::INPUT);
this->CameraLink->AddLinkedProxy(root, vtkSMLink::OUTPUT);
this->NumberOfComparisons = 1;
}
/**
* Overridden to create/delete representations as views are added/removed.
* Also handle this->OverlayViews == true.
*/
bool Resize(size_t num_comparisons) VTK_OVERRIDE
{
this->NumberOfComparisons = num_comparisons;
if (this->OverlayViews)
{
// We'll create 1 view, but multiple sets of representations for each comparison.
vtkSMProxy* rootView = this->GetRoot();
bool changed = this->Resize(1);
for (auto iter = this->Representations.begin(); iter != this->Representations.end(); ++iter)
{
size_t old_size = (*iter)->GetNumberOfItems();
for (size_t cc = num_comparisons; cc < old_size; ++cc)
{
vtkRemoveRepresentation(rootView, (*iter)->GetItem(cc));
changed = true;
}
changed = (*iter)->Resize(num_comparisons) || changed;
for (size_t cc = old_size; cc < num_comparisons; ++cc)
{
vtkAddRepresentation(rootView, (*iter)->GetItem(cc));
changed = true;
}
}
return changed;
}
else
{
return this->Superclass::Resize(num_comparisons);
}
}
/**
* Add a representation to the root view. Create clones of the same and add them to
* the comparison views (or same root view if this->OverlayViews == true
*/
void AddRepresentation(vtkSMProxy* repr)
{
vtkSMViewProxy* rootView = this->GetView(0);
vtkNew<vtkCloningVectorOfRepresentations> reprClones;
reprClones->Initialize(repr);
reprClones->Resize(this->NumberOfComparisons);
for (size_t cc = 0; cc < this->NumberOfComparisons; ++cc)
{
vtkAddRepresentation(
this->OverlayViews ? rootView : this->GetView(cc), reprClones->GetItem(cc));
}
this->Representations.push_back(reprClones.Get());
}
/**
* Converse of AddRepresentation, to remove a representation and its clones.
*/
void RemoveRepresentation(vtkSMProxy* repr)
{
vtkSMViewProxy* rootView = this->GetView(0);
for (auto iter = this->Representations.begin(); iter != this->Representations.end(); ++iter)
{
if ((*iter)->GetRoot() == repr)
{
// found it! Let's remove it.
for (size_t cc = 0; cc < this->NumberOfComparisons; ++cc)
{
vtkRemoveRepresentation(
this->OverlayViews ? rootView : this->GetView(cc), (*iter)->GetItem(cc));
}
this->Representations.erase(iter);
break;
}
}
}
void Update(size_t index)
{
// Ensure cache is cleared for the representations corresponding to the given
// position.
for (auto iter = this->Representations.begin(); iter != this->Representations.end(); ++iter)
{
(*iter)->ClearDataCaches(index);
}
vtkSMViewProxy* view = this->GetView(this->OverlayViews ? 0 : index);
// Simply update the corresponding view. That'll update the representations in
// that view.
view->Update();
}
protected:
vtkCloningVectorOfViews()
: OverlayViews(false)
, NumberOfComparisons(0)
{
this->CameraLink->SynchronizeInteractiveRendersOff();
}
~vtkCloningVectorOfViews() {}
/**
* A new view is being created, we need to create clones of representations too
* and add them to the new view.
*/
vtkSMProxy* PushBack() VTK_OVERRIDE
{
if (vtkSMProxy* clone = this->Superclass::PushBack())
{
this->CameraLink->AddLinkedProxy(clone, vtkSMLink::INPUT);
this->CameraLink->AddLinkedProxy(clone, vtkSMLink::OUTPUT);
// todo: do I need to sync camera explicitly? -- YES!
// clone representations for this view.
for (auto iter = this->Representations.begin(); iter != this->Representations.end(); ++iter)
{
vtkSMProxy* reprClone = (*iter)->PushBack();
vtkAddRepresentation(clone, reprClone);
}
return clone;
}
else
{
vtkGenericWarningMacro("Failed to create internal view proxy. Comparative visualization "
"view cannot work.");
return nullptr;
}
}
/**
* A view is being removed; we need to remove clones of representations for that view too.
*/
void PopBack() VTK_OVERRIDE
{
vtkSMProxy* back = this->GetBack();
if (back == this->GetRoot())
{
vtkGenericWarningMacro("Cannot remove the root view!");
return;
}
// remove repr clones.
for (auto iter = this->Representations.begin(); iter != this->Representations.end(); ++iter)
{
vtkSMProxy* reprBack = (*iter)->GetBack();
vtkRemoveRepresentation(back, reprBack);
(*iter)->PopBack();
}
this->CameraLink->RemoveLinkedProxy(back);
this->Superclass::PopBack();
}
private:
vtkCloningVectorOfViews(const vtkCloningVectorOfViews&) VTK_DELETE_FUNCTION;
void operator=(const vtkCloningVectorOfViews&) VTK_DELETE_FUNCTION;
vtkNew<vtkSMCameraLink> CameraLink;
bool OverlayViews;
size_t NumberOfComparisons;
std::vector<vtkSmartPointer<vtkCloningVectorOfRepresentations> > Representations;
};
}
//----------------------------------------------------------------------------
class vtkPVComparativeView::vtkInternal
{
public:
typedef std::vector<vtkSmartPointer<vtkSMComparativeAnimationCueProxy> > VectorOfCues;
VectorOfCues Cues;
vtkNew<vtkPVComparativeViewNS::vtkCloningVectorOfViews> Views;
};
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPVComparativeView);
vtkCxxSetObjectMacro(vtkPVComparativeView, RootView, vtkSMViewProxy);
//----------------------------------------------------------------------------
vtkPVComparativeView::vtkPVComparativeView()
{
this->Internal = new vtkInternal();
this->RootView = NULL;
this->Dimensions[0] = 1;
this->Dimensions[1] = 1;
this->ViewSize[0] = 400;
this->ViewSize[1] = 400;
this->ViewPosition[0] = 0;
this->ViewPosition[1] = 0;
this->ViewTime = 0.0;
this->OverlayAllComparisons = false;
this->Spacing[0] = this->Spacing[1] = 1;
this->Outdated = true;
vtkMemberFunctionCommand<vtkPVComparativeView>* fsO =
vtkMemberFunctionCommand<vtkPVComparativeView>::New();
fsO->SetCallback(*this, &vtkPVComparativeView::MarkOutdated);
this->MarkOutdatedObserver = fsO;
}
//----------------------------------------------------------------------------
vtkPVComparativeView::~vtkPVComparativeView()
{
this->SetRootView(NULL);
delete this->Internal;
this->MarkOutdatedObserver->Delete();
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::AddCue(vtkSMComparativeAnimationCueProxy* cue)
{
this->Internal->Cues.push_back(cue);
cue->UpdateVTKObjects();
vtkObject::SafeDownCast(cue->GetClientSideObject())
->AddObserver(vtkCommand::ModifiedEvent, this->MarkOutdatedObserver);
this->MarkOutdated();
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::RemoveCue(vtkSMComparativeAnimationCueProxy* cue)
{
vtkInternal::VectorOfCues::iterator iter;
for (iter = this->Internal->Cues.begin(); iter != this->Internal->Cues.end(); ++iter)
{
if (iter->GetPointer() == cue)
{
vtkObject::SafeDownCast(cue->GetClientSideObject())
->RemoveObserver(this->MarkOutdatedObserver);
this->Internal->Cues.erase(iter);
this->MarkOutdated();
break;
}
}
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::Initialize(vtkSMViewProxy* rootView)
{
if (this->RootView == rootView || !rootView)
{
return;
}
if (this->RootView)
{
vtkErrorMacro("vtkPVComparativeView::Initialize() can only be called once.");
return;
}
this->SetRootView(rootView);
ENSURE_INIT();
// Root view is the first view in the views list.
this->Internal->Views->Initialize(rootView);
this->Build(this->Dimensions[0], this->Dimensions[1]);
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::SetOverlayAllComparisons(bool overlay)
{
if (this->OverlayAllComparisons == overlay)
{
return;
}
this->OverlayAllComparisons = overlay;
this->Modified();
this->Build(this->Dimensions[0], this->Dimensions[1]);
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::Build(int dx, int dy)
{
if (dx <= 0 || dy <= 0)
{
vtkErrorMacro("Dimensions cannot be 0.");
return;
}
this->Dimensions[0] = dx;
this->Dimensions[1] = dy;
ENSURE_INIT();
this->Internal->Views->SetOverlayViews(this->OverlayAllComparisons);
size_t numComparisons = dx * dy;
assert(numComparisons >= 1);
if (this->Internal->Views->Resize(numComparisons))
{
this->MarkOutdated();
}
this->UpdateViewLayout();
// Whenever the layout changes we'll fire the ConfigureEvent.
this->InvokeEvent(vtkCommand::ConfigureEvent);
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::UpdateViewLayout()
{
ENSURE_INIT();
int numCols = this->OverlayAllComparisons ? 1 : this->Dimensions[0];
int numRows = this->OverlayAllComparisons ? 1 : this->Dimensions[1];
assert(numCols >= 1 && numRows >= 1);
std::vector<LayoutStruct> colData(numCols);
std::vector<LayoutStruct> rowData(numRows);
// We mimic the logic from qlayoutengine.cpp (qGeomCalc) for sizing
// cells in a QGridLayout.
vtkGeomCalc(colData, 0, numCols, this->ViewPosition[0], this->ViewSize[0], this->Spacing[0]);
vtkGeomCalc(rowData, 0, numRows, this->ViewPosition[1], this->ViewSize[1], this->Spacing[1]);
for (int row = 0, view_index = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col, ++view_index)
{
vtkSMViewProxy* view = this->Internal->Views->GetView(view_index);
const vtkVector2i pos(colData[col].pos, rowData[row].pos);
vtkSMPropertyHelper(view, "ViewPosition").Set(pos.GetData(), 2);
const vtkVector2i size(colData[col].size, rowData[row].size);
// Not all view classes have a ViewSize property
vtkSMPropertyHelper(view, "ViewSize", true).Set(size.GetData(), 2);
view->UpdateVTKObjects();
}
}
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::AddRepresentation(vtkSMProxy* repr)
{
ENSURE_INIT();
if (!repr)
{
return;
}
this->MarkOutdated();
this->Internal->Views->AddRepresentation(repr);
// Signal that representations have changed
this->InvokeEvent(vtkCommand::UserEvent);
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::RemoveRepresentation(vtkSMProxy* repr)
{
ENSURE_INIT();
this->MarkOutdated();
this->Internal->Views->RemoveRepresentation(repr);
// Signal that representations have changed
this->InvokeEvent(vtkCommand::UserEvent);
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::InteractiveRender()
{
ENSURE_INIT();
this->GetRootView()->InteractiveRender();
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::StillRender()
{
ENSURE_INIT();
this->GetRootView()->StillRender();
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::Update()
{
if (!this->Outdated)
{
// cout << "Not Outdated" << endl;
return;
}
vtkSMComparativeAnimationCueProxy* timeCue = NULL;
// locate time cue.
for (vtkInternal::VectorOfCues::iterator iter = this->Internal->Cues.begin();
iter != this->Internal->Cues.end(); ++iter)
{
// for now, we are saying that the first cue that has no animatable proxy
// is for animating time.
if (vtkSMPropertyHelper(iter->GetPointer(), "AnimatedProxy").GetAsProxy() == NULL)
{
timeCue = iter->GetPointer();
break;
}
}
int index = 0;
for (int y = 0; y < this->Dimensions[1]; y++)
{
for (int x = 0; x < this->Dimensions[0]; x++)
{
int view_index = this->OverlayAllComparisons ? 0 : index;
vtkSMViewProxy* view = this->Internal->Views->GetView(view_index);
if (timeCue)
{
double value = timeCue->GetValue(x, y, this->Dimensions[0], this->Dimensions[1]);
vtkSMPropertyHelper(view, "ViewTime").Set(value);
}
else
{
vtkSMPropertyHelper(view, "ViewTime").Set(this->ViewTime);
}
view->UpdateVTKObjects();
for (vtkInternal::VectorOfCues::iterator iter = this->Internal->Cues.begin();
iter != this->Internal->Cues.end(); ++iter)
{
if (iter->GetPointer() == timeCue)
{
continue;
}
iter->GetPointer()->UpdateAnimatedValue(x, y, this->Dimensions[0], this->Dimensions[1]);
}
// Make the view cache the current setup.
this->Internal->Views->Update(index);
index++;
}
}
this->Outdated = false;
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::GetViews(vtkCollection* collection)
{
if (!collection)
{
return;
}
for (size_t cc = 0, max = this->Internal->Views->GetNumberOfItems(); cc < max; ++cc)
{
collection->AddItem(this->Internal->Views->GetItem(cc));
}
}
//----------------------------------------------------------------------------
vtkImageData* vtkPVComparativeView::CaptureWindow(int magnification)
{
std::vector<vtkSmartPointer<vtkImageData> > images;
vtkPVComparativeViewNS::vtkCloningVectorOfViews* views = this->Internal->Views.Get();
for (size_t cc = 0, max = views->GetNumberOfItems(); cc < max; ++cc)
{
vtkImageData* image = views->GetView(cc)->CaptureWindow(magnification);
if (image)
{
images.push_back(image);
image->FastDelete();
}
if (this->OverlayAllComparisons)
{
break;
}
}
if (images.size() == 0)
{
return NULL;
}
const unsigned char color[3] = { 0, 0, 0 };
vtkSmartPointer<vtkImageData> img = vtkSMUtilities::MergeImages(images, /*borderWidth=*/0, color);
if (img)
{
img->Register(this);
}
return img.GetPointer();
}
//----------------------------------------------------------------------------
void vtkPVComparativeView::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Dimensions: " << this->Dimensions[0] << ", " << this->Dimensions[1] << endl;
os << indent << "Spacing: " << this->Spacing[0] << ", " << this->Spacing[1] << endl;
}
|