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
|
/*=========================================================================
Program: ParaView
Module: vtkSMCompoundSourceProxy.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 "vtkSMCompoundSourceProxy.h"
#include "vtkSMProxyInternals.h"
#include "vtkObjectFactory.h"
#include "vtkPVXMLElement.h"
#include "vtkSMCompoundProxyDefinitionLoader.h"
#include "vtkSMMessage.h"
#include "vtkSMOutputPort.h"
#include "vtkSMProxyLocator.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSessionProxyManager.h"
#include <vtkSmartPointer.h>
#include <list>
#include <vector>
typedef std::list<vtkSmartPointer<vtkPVXMLElement> > ElementsType;
//*****************************************************************************
class vtkSMCompoundSourceProxy::vtkInternals
{
public:
struct PortInfo
{
std::string ProxyName;
std::string ExposedName;
std::string PortName;
unsigned int PortIndex;
PortInfo()
{
this->PortIndex = VTK_UNSIGNED_INT_MAX;
}
bool HasPortIndex()
{
return this->PortIndex != VTK_UNSIGNED_INT_MAX;
}
bool HasPortName()
{
return !this->HasPortIndex();
}
};
void RegisterExposedPort(const char* proxyName, const char* exposedName, int portIndex)
{
PortInfo info;
info.PortIndex = portIndex;
info.ProxyName = proxyName;
info.ExposedName = exposedName;
this->ExposedPorts.push_back(info);
}
void RegisterExposedPort(const char* proxyName, const char* exposedName, const char* portName)
{
PortInfo info;
info.PortName = portName;
info.ProxyName = proxyName;
info.ExposedName = exposedName;
this->ExposedPorts.push_back(info);
}
typedef std::vector<PortInfo> VectorOfPortInfo;
VectorOfPortInfo ExposedPorts;
};
//*****************************************************************************
vtkStandardNewMacro(vtkSMCompoundSourceProxy);
//----------------------------------------------------------------------------
vtkSMCompoundSourceProxy::vtkSMCompoundSourceProxy()
{
this->CSInternals = new vtkInternals();
this->SetSIClassName("vtkSICompoundSourceProxy");
}
//----------------------------------------------------------------------------
vtkSMCompoundSourceProxy::~vtkSMCompoundSourceProxy()
{
delete this->CSInternals;
this->CSInternals = NULL;
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::CreateOutputPorts()
{
if (this->Location == 0 || this->OutputPortsCreated)
{
return;
}
this->OutputPortsCreated = 1;
this->RemoveAllOutputPorts();
this->CreateVTKObjects();
unsigned int index = 0;
vtkInternals::VectorOfPortInfo::iterator iter;
iter = this->CSInternals->ExposedPorts.begin();
while ( iter != this->CSInternals->ExposedPorts.end() )
{
vtkSMProxy* subProxy = this->GetSubProxy(iter->ProxyName.c_str());
vtkSMSourceProxy* source = vtkSMSourceProxy::SafeDownCast(subProxy);
if (!source)
{
vtkErrorMacro("Failed to locate sub proxy with name "
<< iter->ProxyName.c_str());
continue;
}
source->CreateOutputPorts();
vtkSMOutputPort* port = 0;
vtkSMDocumentation* doc = 0;
unsigned int port_index = 0;
if (iter->HasPortIndex())
{
port_index = iter->PortIndex;
}
else
{
port_index = source->GetOutputPortIndex(iter->PortName.c_str());
}
port = source->GetOutputPort(port_index);
doc = source->GetOutputPortDocumentation(port_index);
if (!port)
{
vtkErrorMacro( "Failed to locate requested output port of subproxy "
<< iter->ProxyName.c_str());
continue;
}
port->SetCompoundSourceProxy(this);
this->SetOutputPort(index, iter->ExposedName.c_str(), port, doc);
index++;
// Move forward
iter++;
}
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::CreateSelectionProxies()
{
if (this->DisableSelectionProxies || this->SelectionProxiesCreated)
{
return;
}
this->SelectionProxiesCreated = true;
this->RemoveAllExtractSelectionProxies();
unsigned int numOutputs = this->GetNumberOfOutputPorts();
for (unsigned int cc=0; cc < numOutputs; cc++)
{
vtkSMOutputPort* port = this->GetOutputPort(cc);
vtkSMSourceProxy* source = port->SourceProxy.GetPointer();
if (source && source != this)
{
source->CreateSelectionProxies();
this->SetExtractSelectionProxy(cc,
source->GetSelectionOutput(port->GetPortIndex()));
}
}
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::UpdateVTKObjects()
{
if(this->Location == 0)
{
return;
}
// update subproxies that don't have inputs first.
// This is required for Readers/Sources that need the ExtractPieces filter to
// be insered into the pipeline. Before inserting the ExtractPieces filter,
// the pipeline needs to be updated, which may raise errors if all properties
// of the source haven't been pushed correctly.
unsigned int nbSubProxy = this->GetNumberOfSubProxies();
for( unsigned int i = 0; i < nbSubProxy; i++)
{
vtkSMProxy* subProxy = this->GetSubProxy(i);
vtkSMSourceProxy* source = vtkSMSourceProxy::SafeDownCast(subProxy);
// For vtkSMCompoundSourceProxy,GetNumberOfAlgorithmRequiredInputPorts
// always returns 0.
if (!source || source->GetNumberOfAlgorithmRequiredInputPorts() == 0)
{
subProxy->UpdateVTKObjects();
}
}
this->Superclass::UpdateVTKObjects();
}
//----------------------------------------------------------------------------
int vtkSMCompoundSourceProxy::ReadXMLAttributes( vtkSMSessionProxyManager* pm,
vtkPVXMLElement* element)
{
if (!this->Superclass::ReadXMLAttributes(pm, element))
{
return 0;
}
// Initialise the Proxy based on its definition state
vtkSmartPointer<vtkSMCompoundProxyDefinitionLoader> deserializer;
deserializer = vtkSmartPointer<vtkSMCompoundProxyDefinitionLoader>::New();
deserializer->SetRootElement(element);
deserializer->SetSessionProxyManager(pm);
vtkSmartPointer<vtkSMProxyLocator> locator;
locator = vtkSmartPointer<vtkSMProxyLocator>::New();
locator->SetDeserializer(deserializer.GetPointer());
// Initialise sub-proxy by registering them as sub-proxy --------------------
int currentId;
std::string compoundName;
unsigned int numElems = element->GetNumberOfNestedElements();
for (unsigned int i=0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = element->GetNestedElement(i);
if ( currentElement->GetName() &&
strcmp(currentElement->GetName(), "Proxy") == 0)
{
compoundName = currentElement->GetAttributeOrEmpty("compound_name");
if (!compoundName.empty())
{
if (!currentElement->GetScalarAttribute("id", ¤tId))
{
continue;
}
vtkSMProxy* subProxy = locator->LocateProxy(currentId);
if (subProxy)
{
this->AddSubProxy(compoundName.c_str(), subProxy);
}
}
}
}
// Initialise exposed properties --------------------------------------------
vtkPVXMLElement* exposedProperties = NULL;
exposedProperties = element->FindNestedElementByName("ExposedProperties");
numElems =
exposedProperties? exposedProperties->GetNumberOfNestedElements() : 0;
for (unsigned int i=0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = exposedProperties->GetNestedElement(i);
if ( currentElement->GetName() &&
strcmp(currentElement->GetName(), "Property") == 0)
{
const char* name = currentElement->GetAttribute("name");
const char* proxyName = currentElement->GetAttribute("proxy_name");
const char* exposedName = currentElement->GetAttribute("exposed_name");
if (name && proxyName && exposedName)
{
this->ExposeSubProxyProperty(proxyName, name, exposedName);
}
else if (!name)
{
vtkErrorMacro("Required attribute name could not be found.");
}
else if (!proxyName)
{
vtkErrorMacro("Required attribute proxy_name could not be found.");
}
else if (!exposedName)
{
vtkErrorMacro("Required attribute exposed_name could not be found.");
}
}
}
// Initialise exposed output port -------------------------------------------
int index=0;
numElems = element->GetNumberOfNestedElements();
for (unsigned int i=0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = element->GetNestedElement(i);
if ( currentElement->GetName() &&
strcmp(currentElement->GetName(), "OutputPort") == 0)
{
const char* exposed_name = currentElement->GetAttribute("name");
const char* proxy_name = currentElement->GetAttribute("proxy");
const char* port_name = currentElement->GetAttribute("port_name");
if (!port_name && !currentElement->GetScalarAttribute("port_index", &index))
{
vtkErrorMacro("Missing output port 'index'.");
return 0;
}
if (!exposed_name)
{
vtkErrorMacro("Missing output port 'name'.");
return 0;
}
if (!proxy_name)
{
vtkErrorMacro("Missing output port 'proxy'.");
return 0;
}
if (port_name)
{
this->CSInternals->RegisterExposedPort(proxy_name, exposed_name, port_name);
}
else
{
this->CSInternals->RegisterExposedPort(proxy_name, exposed_name, index);
}
}
}
return 1;
}
//----------------------------------------------------------------------------
// vtkSMSourceProxy guarantees that the output port size is set up correctly
// after CreateVTKObjects(). Hence, we ensure that it is set up correctly.
void vtkSMCompoundSourceProxy::CreateVTKObjects()
{
if (this->ObjectsCreated || this->Location == 0)
{
return;
}
this->Superclass::CreateVTKObjects();
// --- Specific CompoundSourceProxy behaviour ---
unsigned int index = 0;
vtkInternals::VectorOfPortInfo::iterator iter;
iter = this->CSInternals->ExposedPorts.begin();
vtkSMProxy* currentProxy;
while( iter != this->CSInternals->ExposedPorts.end() )
{
currentProxy = this->GetSubProxy(iter->ProxyName.c_str());
vtkSMSourceProxy* subProxy = vtkSMSourceProxy::SafeDownCast(currentProxy);
if (!subProxy)
{
vtkErrorMacro("Failed to locate sub proxy with name "
<< iter->ProxyName.c_str());
iter++;
continue;
}
if ( iter->HasPortIndex() ||
( subProxy->GetOutputPortIndex(iter->PortName.c_str())
== VTK_UNSIGNED_INT_MAX ) )
{
if (subProxy->GetNumberOfOutputPorts() <= iter->PortIndex)
{
vtkErrorMacro("Failed to locate requested output port of subproxy "
<< iter->ProxyName.c_str());
iter++;
continue;
}
}
// We cannot create vtkSMOutputPort proxies right now, since that requires
// that the input to this filter is setup correctly. Hence we wait for the
// CreateOutputPorts() call to create the vtkSMOutputPort proxies.
if(this->GetNumberOfOutputPorts() <= index)
{
this->SetOutputPort(index, iter->ExposedName.c_str(), 0, 0);
}
// This sets up the dependency chain correctly.
subProxy->AddConsumer(0, this);
this->AddProducer(0, subProxy);
index++;
// Move forward
iter++;
}
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::AddProxy(const char* name, vtkSMProxy* proxy)
{
// If proxy with the name already exists, this->AddSubProxy raises a warning.
this->AddSubProxy(name, proxy);
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::ExposeProperty(const char* proxyName,
const char* propertyName, const char* exposedName)
{
this->ExposeSubProxyProperty(proxyName, propertyName, exposedName);
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::ExposeOutputPort(const char* proxyName,
const char* portName, const char* exposedName)
{
this->CSInternals->RegisterExposedPort(proxyName, exposedName, portName);
// We don't access the vtkSMOutputPort from the sub proxy here itself, since
// the subproxy may not have it ports initialized when this method is called
// eg. when loading state.
}
//----------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::ExposeOutputPort(const char* proxyName,
unsigned int portIndex, const char* exposedName)
{
this->CSInternals->RegisterExposedPort(proxyName, exposedName, portIndex);
// We don't access the vtkSMOutputPort from the sub proxy here itself, since
// the subproxy may not have it ports initialized when this method is called
// eg. when loading state.
}
//----------------------------------------------------------------------------
// Definition is
// * State i.e. exposed property states (execept those refererring to outside
// proxies)
// * Subproxy states.
// * Exposed property information.
// * Exposed output port information.
vtkPVXMLElement* vtkSMCompoundSourceProxy::SaveDefinition(
vtkPVXMLElement* root)
{
vtkPVXMLElement* defElement = this->SaveXMLState(0);
defElement->SetName("CompoundSourceProxy");
defElement->RemoveAllNestedElements();
// * Add subproxy states.
unsigned int numProxies = this->GetNumberOfSubProxies();
for (unsigned int cc=0; cc < numProxies; cc++)
{
vtkPVXMLElement* newElem =
this->GetSubProxy(cc)->SaveXMLState(defElement);
const char* compound_name =this->GetSubProxyName(cc);
newElem->AddAttribute("compound_name", compound_name);
}
// * Clean references to any external proxies.
this->TraverseForProperties(defElement);
// * Add exposed property information.
vtkPVXMLElement* exposed = vtkPVXMLElement::New();
exposed->SetName("ExposedProperties");
unsigned int numExposed = 0;
vtkSMProxyInternals* internals = this->Internals;
vtkSMProxyInternals::ExposedPropertyInfoMap::iterator iter =
internals->ExposedProperties.begin();
for(; iter != internals->ExposedProperties.end(); iter++)
{
numExposed++;
vtkPVXMLElement* expElem = vtkPVXMLElement::New();
expElem->SetName("Property");
expElem->AddAttribute("name", iter->second.PropertyName);
expElem->AddAttribute("proxy_name", iter->second.SubProxyName);
expElem->AddAttribute("exposed_name", iter->first.c_str());
exposed->AddNestedElement(expElem);
expElem->Delete();
}
if (numExposed > 0)
{
defElement->AddNestedElement(exposed);
}
exposed->Delete();
// * Add output port information.
vtkInternals::VectorOfPortInfo::iterator iter2 =
this->CSInternals->ExposedPorts.begin();
for (;iter2 != this->CSInternals->ExposedPorts.end(); ++iter2, numExposed++)
{
vtkPVXMLElement* expElem = vtkPVXMLElement::New();
expElem->SetName("OutputPort");
expElem->AddAttribute("name", iter2->ExposedName.c_str());
expElem->AddAttribute("proxy", iter2->ProxyName.c_str());
if (iter2->PortIndex != VTK_UNSIGNED_INT_MAX)
{
expElem->AddAttribute("port_index", iter2->PortIndex);
}
else
{
expElem->AddAttribute("port_name", iter2->PortName.c_str());
}
defElement->AddNestedElement(expElem);
expElem->Delete();
}
if (root)
{
root->AddNestedElement(defElement);
defElement->Delete();
}
return defElement;
}
//---------------------------------------------------------------------------
int vtkSMCompoundSourceProxy::ShouldWriteValue(vtkPVXMLElement* valueElem)
{
if (strcmp(valueElem->GetName(), "Proxy") != 0)
{
return 1;
}
const char* proxyId = valueElem->GetAttribute("value");
if (!proxyId)
{
return 1;
}
unsigned int numProxies = this->GetNumberOfSubProxies();
for (unsigned int i=0; i<numProxies; i++)
{
vtkSMProxy* proxy = this->GetSubProxy(i);
if (proxy &&
strcmp(proxy->GetGlobalIDAsString(), proxyId) == 0)
{
return 1;
}
}
return 0;
}
//---------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::StripValues(vtkPVXMLElement* propertyElem)
{
ElementsType elements;
// Find all elements we want to keep
unsigned int numElements = propertyElem->GetNumberOfNestedElements();
for(unsigned int i=0; i<numElements; i++)
{
vtkPVXMLElement* nested = propertyElem->GetNestedElement(i);
if (this->ShouldWriteValue(nested))
{
elements.push_back(nested);
}
}
// Delete all
propertyElem->RemoveAllNestedElements();
// Add the one we want to keep
ElementsType::iterator iter = elements.begin();
for (; iter != elements.end(); iter++)
{
propertyElem->AddNestedElement(iter->GetPointer());
}
}
//---------------------------------------------------------------------------
void vtkSMCompoundSourceProxy::TraverseForProperties(vtkPVXMLElement* root)
{
unsigned int numProxies = root->GetNumberOfNestedElements();
for(unsigned int i=0; i<numProxies; i++)
{
vtkPVXMLElement* proxyElem = root->GetNestedElement(i);
if (strcmp(proxyElem->GetName(), "Proxy") == 0)
{
unsigned int numProperties = proxyElem->GetNumberOfNestedElements();
for(unsigned int j=0; j<numProperties; j++)
{
vtkPVXMLElement* propertyElem = proxyElem->GetNestedElement(j);
if (strcmp(propertyElem->GetName(), "Property") == 0)
{
this->StripValues(propertyElem);
}
}
}
}
}
|