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
|
/*=========================================================================
Program: ParaView
Module: vtkPythonView.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 "vtkPython.h" // must be the first thing that's included
#include "vtkPythonView.h"
#include "vtkObjectFactory.h"
#include "vtkDataObject.h"
#include "vtkInformationRequestKey.h"
#include "vtkPVSynchronizedRenderWindows.h"
#include "vtkPythonInterpreter.h"
#include "vtkSmartPyObject.h"
#include "vtkPythonRepresentation.h"
#include "vtkRenderer.h"
#include "vtkRendererCollection.h"
#include "vtkRenderWindow.h"
#include "vtkTexture.h"
#include "vtkTimerLog.h"
#include <algorithm>
#include <sstream>
class vtkPythonView::vtkInternals
{
PyObject* CustomLocals;
public:
vtkInternals() : CustomLocals(0) {}
~vtkInternals()
{
this->CleanupObjects();
}
PyObject* GetCustomLocalsPyObject()
{
if (this->CustomLocals)
{
return this->CustomLocals;
}
// Make sure the python interpreter is initialized
vtkPythonInterpreter::Initialize(1);
{
vtkPythonScopeGilEnsurer gilEnsurer;
const char* code = "__vtkPythonViewLocals={'__builtins__':__builtins__}\n";
PyRun_SimpleString(const_cast<char *>(code));
PyObject* main_module = PyImport_AddModule((char*)"__main__");
PyObject* global_dict = PyModule_GetDict(main_module);
this->CustomLocals = PyDict_GetItemString(global_dict, "__vtkPythonViewLocals");
if (!this->CustomLocals)
{
vtkGenericWarningMacro("Failed to locate the __vtkPythonViewLocals object.");
return NULL;
}
Py_INCREF(this->CustomLocals);
PyRun_SimpleString(const_cast<char*>("del __vtkPythonViewLocals"));
}
return this->CustomLocals;
}
void CleanupObjects()
{
Py_XDECREF(this->CustomLocals);
this->CustomLocals = NULL;
if (vtkPythonInterpreter::IsInitialized())
{
const char* code = "import gc; gc.collect()\n";
vtkPythonInterpreter::RunSimpleString(code);
}
}
void ResetCustomLocals()
{
this->CleanupObjects();
}
};
vtkStandardNewMacro(vtkPythonView);
//----------------------------------------------------------------------------
vtkPythonView::vtkPythonView()
{
this->Internals = new vtkPythonView::vtkInternals();
this->RenderTexture = vtkSmartPointer<vtkTexture>::New();
this->Renderer = vtkSmartPointer<vtkRenderer>::New();
this->Renderer->SetBackgroundTexture(this->RenderTexture);
this->RenderWindow.TakeReference(this->SynchronizedWindows->NewRenderWindow());
this->RenderWindow->AddRenderer(this->Renderer);
this->Magnification = 1;
this->ImageData = NULL;
this->Script = NULL;
}
//----------------------------------------------------------------------------
vtkPythonView::~vtkPythonView()
{
// Clean up memory
delete this->Internals;
this->SetScript(NULL);
this->SetImageData(NULL);
}
//----------------------------------------------------------------------------
vtkInformationKeyMacro(vtkPythonView, REQUEST_DELIVER_DATA_TO_CLIENT, Request);
//----------------------------------------------------------------------------
void vtkPythonView::Update()
{
vtkTimerLog::MarkStartEvent("vtkPythonView::Update");
this->Internals->ResetCustomLocals();
if (this->Script && strlen(this->Script) > 0)
{
this->CallProcessViewRequest(vtkPVView::REQUEST_UPDATE(),
this->RequestInformation,
this->ReplyInformationVector);
// Define the view in Python by creating a new instance of the
// Python vtkPythonView class from the pointer to the C++
// vtkPythonView instance.
char addressOfThis[1024];
sprintf(addressOfThis, "%p", this);
char *address = addressOfThis;
if ((addressOfThis[0] == '0') &&
((addressOfThis[1] == 'x') || (addressOfThis[1] == 'X')))
{
address += 2;
}
// Import necessary items from ParaView
std::ostringstream importStream;
importStream << "import paraview" << endl
<< "from paraview.vtk.vtkPVClientServerCoreRendering import vtkPythonView" << endl
<< "pythonView = vtkPythonView('" << addressOfThis << " ')" << endl;
this->RunSimpleStringWithCustomLocals(importStream.str().c_str());
// Evaluate the user-defined script. It should define two functions,
// setup_data(view) and render(view, figure) that each take a
// vtkPythonView (the render function also takes a matplotlib.figure
// as the second argument). If these functions are not defined in
// this script, they must be defined in the global Python
// interpreter by some other means (e.g. a script executed by
// pvpython).
this->RunSimpleStringWithCustomLocals(this->Script);
// Update the data array settings. Do this only on servers where local data is available
if (this->IsLocalDataAvailable())
{
std::ostringstream setupDataCommandStream;
setupDataCommandStream
<< "from paraview import python_view\n"
<< "try:\n"
<< " python_view.call_setup_data(setup_data, pythonView)\n"
<< "except:\n"
<< " pass\n";
this->RunSimpleStringWithCustomLocals(setupDataCommandStream.str().c_str());
}
this->CallProcessViewRequest(vtkPythonView::REQUEST_DELIVER_DATA_TO_CLIENT(),
this->RequestInformation,
this->ReplyInformationVector);
}
vtkTimerLog::MarkEndEvent("vtkPythonView::Update");
}
//----------------------------------------------------------------------------
vtkRenderer* vtkPythonView::GetRenderer()
{
return this->Renderer;
}
//----------------------------------------------------------------------------
void vtkPythonView::SetRenderer(vtkRenderer* renderer)
{
vtkRendererCollection* rens = this->RenderWindow->GetRenderers();
vtkCollectionSimpleIterator cookie;
rens->InitTraversal(cookie);
while(vtkRenderer *ren = rens->GetNextRenderer(cookie))
{
ren->SetRenderWindow(NULL);
this->RenderWindow->RemoveRenderer(ren);
}
this->RenderWindow->AddRenderer(renderer);
this->Renderer = renderer;
}
//----------------------------------------------------------------------------
vtkRenderWindow* vtkPythonView::GetRenderWindow()
{
return this->RenderWindow;
}
//----------------------------------------------------------------------------
void vtkPythonView::SetRenderWindow(vtkRenderWindow * renWin)
{
if (!renWin)
{
vtkErrorMacro(<< "SetRenderWindow called with a null window pointer."
<< " That can't be right.");
return;
}
// move renderers to new window
vtkRendererCollection* rens = this->RenderWindow->GetRenderers();
while(rens->GetNumberOfItems())
{
vtkRenderer* ren = rens->GetFirstRenderer();
ren->SetRenderWindow(NULL);
renWin->AddRenderer(ren);
this->RenderWindow->RemoveRenderer(ren);
}
}
//----------------------------------------------------------------------------
int vtkPythonView::GetNumberOfVisibleDataObjects()
{
int numberOfVisibleRepresentations = 0;
int numberOfRepresentations = this->GetNumberOfRepresentations();
for (int i = 0; i < numberOfRepresentations; ++i)
{
vtkPVDataRepresentation* representation =
vtkPVDataRepresentation::SafeDownCast(this->GetRepresentation(i));
if (representation && representation->GetVisibility())
{
numberOfVisibleRepresentations++;
}
}
return numberOfVisibleRepresentations;
}
//----------------------------------------------------------------------------
vtkPythonRepresentation* vtkPythonView::GetVisibleRepresentation(int visibleObjectIndex)
{
if (visibleObjectIndex < 0 || visibleObjectIndex >= this->GetNumberOfVisibleDataObjects())
{
return NULL;
}
int numberOfVisibleRepresentations = 0;
int numberOfRepresentations = this->GetNumberOfRepresentations();
for (int i = 0; i < numberOfRepresentations; ++i)
{
vtkPythonRepresentation* representation =
vtkPythonRepresentation::SafeDownCast(this->GetRepresentation(i));
if (representation && representation->GetVisibility())
{
if (visibleObjectIndex == numberOfVisibleRepresentations)
{
return representation;
}
numberOfVisibleRepresentations++;
}
}
return NULL;
}
//----------------------------------------------------------------------------
vtkDataObject* vtkPythonView::GetVisibleDataObjectForSetup(int visibleObjectIndex)
{
vtkPythonRepresentation* representation =
this->GetVisibleRepresentation(visibleObjectIndex);
if (!representation)
{
vtkErrorMacro(<< "No visible representation at index " << visibleObjectIndex);
return NULL;
}
return representation->GetLocalInput();
}
//----------------------------------------------------------------------------
vtkDataObject* vtkPythonView::GetVisibleDataObjectForRendering(int visibleObjectIndex)
{
vtkPythonRepresentation* representation =
this->GetVisibleRepresentation(visibleObjectIndex);
if (!representation)
{
vtkErrorMacro(<< "No visible representation at index " << visibleObjectIndex);
return NULL;
}
return representation->GetClientDataObject();
}
//----------------------------------------------------------------------------
int vtkPythonView::GetNumberOfAttributeArrays(int visibleObjectIndex, int attributeType)
{
// Forward to the visible representation
vtkPythonRepresentation* representation =
this->GetVisibleRepresentation(visibleObjectIndex);
if (!representation)
{
vtkErrorMacro(<< "No visible representation at index " << visibleObjectIndex);
return 0;
}
return representation->GetNumberOfAttributeArrays(attributeType);
}
//----------------------------------------------------------------------------
const char* vtkPythonView::GetAttributeArrayName(int visibleObjectIndex,
int attributeType,
int arrayIndex)
{
// Forward to the visible representation
vtkPythonRepresentation* representation =
this->GetVisibleRepresentation(visibleObjectIndex);
if (!representation)
{
vtkErrorMacro(<< "No visible representation at index " << visibleObjectIndex);
return NULL;
}
return representation->GetAttributeArrayName(attributeType, arrayIndex);
}
//----------------------------------------------------------------------------
void vtkPythonView::SetAttributeArrayStatus(int visibleObjectIndex,
int attributeType,
const char* name,
int status)
{
// Forward to the visible representation
vtkPythonRepresentation* representation =
this->GetVisibleRepresentation(visibleObjectIndex);
if (!representation)
{
vtkErrorMacro(<< "No visible representation at index " << visibleObjectIndex);
return;
}
representation->SetAttributeArrayStatus(attributeType, name, status);
}
//----------------------------------------------------------------------------
int vtkPythonView::GetAttributeArrayStatus(int visibleObjectIndex,
int attributeType,
const char* name)
{
// Forward to the visible representation
vtkPythonRepresentation* representation =
this->GetVisibleRepresentation(visibleObjectIndex);
if (!representation)
{
vtkErrorMacro(<< "No visible representation at index " << visibleObjectIndex);
return 0;
}
return representation->GetAttributeArrayStatus(attributeType, name);
}
//----------------------------------------------------------------------------
void vtkPythonView::EnableAllAttributeArrays()
{
int numRepresentations = this->GetNumberOfRepresentations();
for (int i = 0; i < numRepresentations; ++i)
{
vtkPythonRepresentation* representation =
vtkPythonRepresentation::SafeDownCast(this->GetRepresentation(i));
if (representation)
{
representation->EnableAllAttributeArrays();
}
}
}
//----------------------------------------------------------------------------
void vtkPythonView::DisableAllAttributeArrays()
{
int numRepresentations = this->GetNumberOfRepresentations();
for (int i = 0; i < numRepresentations; ++i)
{
vtkPythonRepresentation* representation =
vtkPythonRepresentation::SafeDownCast(this->GetRepresentation(i));
if (representation)
{
representation->DisableAllAttributeArrays();
}
}
}
//----------------------------------------------------------------------------
void vtkPythonView::StillRender()
{
// Render only on the client
if (this->SynchronizedWindows->GetLocalProcessIsDriver())
{
this->SetImageData(NULL);
// Now draw the image
int width = this->Size[0] * this->Magnification;
int height = this->Size[1] * this->Magnification;
std::ostringstream renderCommandStream;
renderCommandStream
<< "from paraview import python_view\n"
<< "try:\n"
<< " python_view.call_render(render, pythonView, " << width << ", " << height << ")\n"
<< "except:\n"
<< " pass\n";
this->RunSimpleStringWithCustomLocals(renderCommandStream.str().c_str());
// this->ImageData should be set by the call_render() function
// invoked above.
if (this->ImageData)
{
this->RenderTexture->SetInputData(this->ImageData);
this->Renderer->TexturedBackgroundOn();
}
else
{
this->Renderer->TexturedBackgroundOff();
}
this->RenderWindow->Render();
}
}
//----------------------------------------------------------------------------
void vtkPythonView::InteractiveRender()
{
this->StillRender();
}
//----------------------------------------------------------------------------
bool vtkPythonView::IsLocalDataAvailable()
{
// Check with the representations to see if they have local
// input.
bool available = false;
for (int i = 0; i < this->GetNumberOfRepresentations(); ++i)
{
vtkPythonRepresentation* representation =
vtkPythonRepresentation::SafeDownCast(this->GetRepresentation(i));
if (!representation)
{
vtkErrorMacro(<< "Should only have vtkPythonRepresentations");
continue;
}
if (representation->GetLocalInput())
{
available = true;
break;
}
}
return available;
}
//----------------------------------------------------------------------------
int vtkPythonView::RunSimpleStringWithCustomLocals(const char* code)
{
// The embedded python interpreter cannot handle DOS line-endings, see
// http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1167922
std::string buffer = code ? code : "";
buffer.erase(std::remove(buffer.begin(), buffer.end(), '\r'), buffer.end());
PyObject* context = this->Internals->GetCustomLocalsPyObject();
vtkPythonScopeGilEnsurer gilEnsurer;
vtkSmartPyObject result(PyRun_String(const_cast<char*>(buffer.c_str()),
Py_file_input, context, context));
if (result)
{
PyErr_Print();
return -1;
}
result = NULL;
if (Py_FlushLine())
{
PyErr_Clear();
}
return 0;
}
//----------------------------------------------------------------------------
void vtkPythonView::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "RenderTexture: ";
if (this->RenderTexture)
{
os << endl;
this->RenderTexture->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
os << indent << "Renderer: ";
if (this->Renderer)
{
os << endl;
this->Renderer->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
os << indent << "RenderWindow: ";
if (this->RenderWindow)
{
os << endl;
this->RenderWindow->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
os << indent << "Magnification: " << this->Magnification << endl;
os << indent << "Script: \n" << this->Script << endl;
os << indent << "ImageData: ";
if (this->ImageData)
{
os << endl;
this->ImageData->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
|