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
|
/*=========================================================================
Program: ParaView
Module: vtkPVBagChartRepresentation.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 "vtkPVBagChartRepresentation.h"
#include "vtkAppendPolyData.h"
#include "vtkBrush.h"
#include "vtkCellArray.h"
#include "vtkChartXY.h"
#include "vtkClientServerMoveData.h"
#include "vtkColor.h"
#include "vtkColorTransferFunction.h"
#include "vtkCommand.h"
#include "vtkContextView.h"
#include "vtkContourFilter.h"
#include "vtkDataObjectToTable.h"
#include "vtkDoubleArray.h"
#include "vtkExtractBlock.h"
#include "vtkIdList.h"
#include "vtkImageData.h"
#include "vtkInformationVector.h"
#include "vtkLookupTable.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiProcessController.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPlotBag.h"
#include "vtkPlotHistogram2D.h"
#include "vtkPlotLine.h"
#include "vtkPointData.h"
#include "vtkProcessModule.h"
#include "vtkPVCacheKeeper.h"
#include "vtkPVContextView.h"
#include "vtkPVXYChartView.h"
#include "vtkReductionFilter.h"
#include "vtkTable.h"
#include <algorithm>
#include <string>
#include <sstream>
vtkStandardNewMacro(vtkPVBagChartRepresentation);
vtkCxxSetObjectMacro(vtkPVBagChartRepresentation,LookupTable,vtkScalarsToColors)
//----------------------------------------------------------------------------
vtkPVBagChartRepresentation::vtkPVBagChartRepresentation() :
LineThickness(1),
LineStyle(0),
LookupTable(NULL),
Opacity(1.),
PointSize(5),
GridLineThickness(1),
GridLineStyle(0),
XAxisSeriesName(NULL),
YAxisSeriesName(NULL),
DensitySeriesName(NULL),
UseIndexForXAxis(true)
{
this->BagColor[0] = 1.0;
this->BagColor[1] = this->BagColor[2] = 0.0;
this->SelectionColor[0] = this->SelectionColor[2] = 1.0;
this->SelectionColor[1] = 0.0;
this->LineColor[0] = this->LineColor[1] = this->LineColor[2] = 0.0;
this->PointColor[0] = this->PointColor[1] = this->PointColor[2] = 0.0;
this->PUserColor[0] = this->PUserColor[2] = 0.5;
this->PUserColor[1] = 0.0;
this->P50Color[0] = this->P50Color[2] = 0.2;
this->P50Color[1] = 0.0;
vtkNew<vtkColorTransferFunction> lut;
lut->SetColorSpaceToDiverging();
lut->AddRGBPoint(0, 59. / 255., 76. / 255., 192. / 255.);
lut->AddRGBPoint(1, 221. / 255., 221. / 255., 221. / 255.);
lut->AddRGBPoint(2, 180. / 255., 4. / 255., 38. / 255.);
SetLookupTable(lut.Get());
}
//----------------------------------------------------------------------------
vtkPVBagChartRepresentation::~vtkPVBagChartRepresentation()
{
SetLookupTable(NULL);
}
//----------------------------------------------------------------------------
void vtkPVBagChartRepresentation::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
bool vtkPVBagChartRepresentation::AddToView(vtkView* view)
{
if (!this->Superclass::AddToView(view))
{
return false;
}
if (this->GetChart())
{
this->GetChart()->SetVisible(this->GetVisibility());
}
return true;
}
//----------------------------------------------------------------------------
bool vtkPVBagChartRepresentation::RemoveFromView(vtkView* view)
{
if (this->GetChart())
{
this->GetChart()->GetPlot(0)->SetInputData(0);
this->GetChart()->SetVisible(false);
}
return this->Superclass::RemoveFromView(view);
}
//----------------------------------------------------------------------------
void vtkPVBagChartRepresentation::SetVisibility(bool visible)
{
this->Superclass::SetVisibility(visible);
vtkChartXY* chart = this->GetChart();
if (chart && !visible)
{
// Refer to vtkChartRepresentation::PrepareForRendering() documentation to
// know why this is cannot be done in PrepareForRendering();
chart->SetVisible(false);
}
this->Modified();
}
//----------------------------------------------------------------------------
vtkChartXY* vtkPVBagChartRepresentation::GetChart()
{
if (this->ContextView)
{
return vtkChartXY::SafeDownCast(
this->ContextView->GetContextItem());
}
return 0;
}
//----------------------------------------------------------------------------
void vtkPVBagChartRepresentation::PrepareForRendering()
{
this->Superclass::PrepareForRendering();
vtkChartXY* chart = this->GetChart();
vtkPlotBag* bagPlot = 0;
vtkPlotHistogram2D* gridPlot = 0;
vtkPlotLine* p50LinePlot = 0;
vtkPlotLine* pUserLinePlot = 0;
for (int i = 0; i < chart->GetNumberOfPlots() &&
(!bagPlot || !gridPlot || !p50LinePlot || !pUserLinePlot); i++)
{
// Search for the first bag plot of the chart. For now
// the chart only manage one bag plot
if (vtkPlotBag::SafeDownCast(chart->GetPlot(i)))
{
bagPlot = vtkPlotBag::SafeDownCast(chart->GetPlot(i));
}
if (vtkPlotHistogram2D::SafeDownCast(chart->GetPlot(i)))
{
gridPlot = vtkPlotHistogram2D::SafeDownCast(chart->GetPlot(i));
}
if (vtkPlotLine::SafeDownCast(chart->GetPlot(i)))
{
if (p50LinePlot == NULL)
{
p50LinePlot = vtkPlotLine::SafeDownCast(chart->GetPlot(i));
}
else
{
pUserLinePlot = vtkPlotLine::SafeDownCast(chart->GetPlot(i));
}
}
}
if (!gridPlot)
{
gridPlot = vtkPlotHistogram2D::New();
chart->AddPlot(gridPlot);
gridPlot->Delete();
}
if (!p50LinePlot)
{
p50LinePlot = vtkPlotLine::SafeDownCast(chart->AddPlot(vtkChart::LINE));
p50LinePlot->PolyLineOff();
p50LinePlot->SelectableOff();
}
if (!pUserLinePlot)
{
pUserLinePlot = vtkPlotLine::SafeDownCast(chart->AddPlot(vtkChart::LINE));
pUserLinePlot->PolyLineOff();
pUserLinePlot->SelectableOff();
}
vtkDoubleArray* thresholdArray = this->LocalThreshold ?
vtkDoubleArray::SafeDownCast(this->LocalThreshold->GetColumnByName("TValues")) : NULL;
std::ostringstream labelp50;
labelp50 << "P" << (thresholdArray ? thresholdArray->GetValue(0) : 50);
p50LinePlot->SetLabel(labelp50.str().c_str());
std::ostringstream labelpUser;
labelpUser << "P" << (thresholdArray ? thresholdArray->GetValue(2) : 99);
pUserLinePlot->SetLabel(labelpUser.str().c_str());
if (!bagPlot)
{
// Create and add a bag plot to the chart
bagPlot = vtkPlotBag::SafeDownCast(chart->AddPlot(vtkChart::BAG));
}
vtkTable* plotInput = this->GetLocalOutput();
bool hasBagPlot = plotInput && plotInput->GetNumberOfRows() > 0;
chart->SetVisible(
(hasBagPlot || this->LocalGrid != NULL) && this->GetVisibility());
bagPlot->SetVisible(hasBagPlot);
bagPlot->SetBagVisible(false);
// Set bag (polygons) brush properties
bagPlot->GetBrush()->SetColorF(this->BagColor);
bagPlot->GetBrush()->SetOpacityF(this->Opacity);
// Set point pen properties
bagPlot->GetPen()->SetWidth(this->PointSize);
bagPlot->GetPen()->SetLineType(this->LineStyle);
bagPlot->GetPen()->SetColorF(this->PointColor);
bagPlot->GetPen()->SetOpacityF(1.0);
// Set selection point pen properties
bagPlot->GetSelectionPen()->SetColorF(this->SelectionColor);
if (hasBagPlot)
{
// We only consider the first vtkTable.
if (this->YAxisSeriesName && this->DensitySeriesName)
{
bagPlot->SetUseIndexForXSeries(this->UseIndexForXAxis);
if (this->UseIndexForXAxis)
{
bagPlot->SetInputData(plotInput,
this->YAxisSeriesName, this->DensitySeriesName);
}
else
{
bagPlot->SetInputData(plotInput, this->XAxisSeriesName,
this->YAxisSeriesName, this->DensitySeriesName);
}
}
}
gridPlot->SetInputData(this->LocalGrid);
gridPlot->SetVisible(this->LocalGrid != NULL);
p50LinePlot->SetVisible(this->LocalGrid != NULL);
pUserLinePlot->SetVisible(this->LocalGrid != NULL);
if (this->LocalGrid)
{
double range[2];
this->LocalGrid->GetScalarRange(range);
gridPlot->SetTransferFunction(GetLookupTable());
if (gridPlot->GetTransferFunction())
{
vtkLookupTable* lut = vtkLookupTable::SafeDownCast(
gridPlot->GetTransferFunction());
vtkColorTransferFunction* tf =
vtkColorTransferFunction::SafeDownCast(gridPlot->GetTransferFunction());
if (lut)
{
lut->SetTableRange(range);
lut->Build();
}
else if (tf)
{
double oldRange[2];
tf->GetRange(oldRange);
int numberOfNodes = tf->GetSize();
double* newNodes = new double[6*numberOfNodes];
for (int i = 0; i < numberOfNodes; ++i)
{
double* newNode = &newNodes[6*i];
tf->GetNodeValue(i, newNode);
newNode[0] = (range[1] - range[0]) * (newNode[0] - oldRange[0])
/ (oldRange[1] - oldRange[0]) + range[0];
}
tf->RemoveAllPoints();
for (int i = 0; i < numberOfNodes; ++i)
{
double* newNode = &newNodes[6*i];
tf->AddRGBPoint(newNode[0], newNode[1], newNode[2], newNode[3],
newNode[4], newNode[5]);
}
delete [] newNodes;
}
}
double p50 = thresholdArray ? thresholdArray->GetValue(1) : 0.;
double pUser = thresholdArray ? thresholdArray->GetValue(3) : 0.;
vtkNew<vtkContourFilter> medianContour;
medianContour->SetInputData(this->LocalGrid);
medianContour->SetNumberOfContours(1);
medianContour->SetValue(0, p50);
medianContour->Update();
vtkNew<vtkTable> medianTable;
SetPolyLineToTable(vtkPolyData::SafeDownCast(
medianContour->GetOutput()), medianTable.Get());
p50LinePlot->SetInputData(medianTable.Get(), "X", "Y");
p50LinePlot->GetPen()->SetWidth(this->GridLineThickness);
p50LinePlot->GetPen()->SetLineType(this->GridLineStyle);
p50LinePlot->SetColor(this->P50Color[0], this->P50Color[1], this->P50Color[2]);
p50LinePlot->SetOpacity(this->Opacity);
vtkNew<vtkContourFilter> pUserContour;
pUserContour->SetInputData(this->LocalGrid);
pUserContour->SetNumberOfContours(1);
pUserContour->SetValue(0, pUser);
pUserContour->Update();
vtkNew<vtkTable> pUserTable;
SetPolyLineToTable(vtkPolyData::SafeDownCast(pUserContour->GetOutput()),
pUserTable.Get());
pUserLinePlot->SetInputData(pUserTable.Get(), "X", "Y");
pUserLinePlot->GetPen()->SetWidth(this->GridLineThickness);
pUserLinePlot->GetPen()->SetLineType(this->GridLineStyle);
pUserLinePlot->SetColor(this->PUserColor[0], this->PUserColor[1], this->PUserColor[2]);
pUserLinePlot->SetOpacity(this->Opacity);
p50LinePlot->SetVisible(medianTable->GetNumberOfRows() >= 2);
pUserLinePlot->SetVisible(pUserTable->GetNumberOfRows() >= 2);
}
// Initialize the view title with the explained variance
vtkPVXYChartView* view = vtkPVXYChartView::SafeDownCast(this->GetView());
if (view && thresholdArray)
{
std::stringstream title;
title << "Explained variance: " <<
static_cast<int>(thresholdArray->GetValue(4)) << "%";
view->SetTitle(title.str().c_str());
}
}
//----------------------------------------------------------------------------
void vtkPVBagChartRepresentation::SetPolyLineToTable(vtkPolyData* polyline,
vtkTable* table)
{
vtkNew<vtkDoubleArray> x;
x->SetName("X");
vtkNew<vtkDoubleArray> y;
y->SetName("Y");
vtkCellArray* lines = polyline->GetLines();
lines->InitTraversal();
for (vtkIdType nbpts, *pts; lines->GetNextCell(nbpts, pts);)
{
for (vtkIdType i = 0; i < nbpts; ++i)
{
double* point = polyline->GetPoint(pts[i]);
x->InsertNextValue(point[0]);
y->InsertNextValue(point[1]);
}
}
table->AddColumn(x.Get());
table->AddColumn(y.Get());
}
//----------------------------------------------------------------------------
int vtkPVBagChartRepresentation::RequestData(vtkInformation* request,
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
if (vtkProcessModule::GetProcessType() == vtkProcessModule::PROCESS_RENDER_SERVER)
{
return this->Superclass::RequestData(request, inputVector, outputVector);
}
// remove the "cached" delivered data.
this->LocalOutput = NULL;
// Pass caching information to the cache keeper.
this->CacheKeeper->SetCachingEnabled(this->GetUseCache());
this->CacheKeeper->SetCacheTime(this->GetCacheKey());
vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
int myId = pm->GetPartitionId();
int numProcs = pm->GetNumberOfLocalPartitions();
if (inputVector[0]->GetNumberOfInformationObjects()==1)
{
vtkSmartPointer<vtkDataObject> data;
// don't process data is the cachekeeper has already cached the result.
if (!this->CacheKeeper->IsCached())
{
data = vtkDataObject::GetData(inputVector[0], 0);
data = this->TransformInputData(inputVector, data);
if (!data)
{
return 0;
}
// Prune input dataset to only process blocks on interest.
// If input is not a multiblock dataset, we make it one so the rest of the
// pipeline is simple.
if (data->IsA("vtkMultiBlockDataSet"))
{
vtkNew<vtkExtractBlock> extractBlock;
extractBlock->SetInputData(data);
extractBlock->PruneOutputOff();
int i =0;
for (std::set<unsigned int>::const_iterator iter = this->CompositeIndices.begin();
iter != this->CompositeIndices.end(); ++iter, ++i)
{
extractBlock->AddIndex(*iter);
}
extractBlock->Update();
data = extractBlock->GetOutputDataObject(0);
}
else
{
vtkNew<vtkMultiBlockDataSet> mbdata;
mbdata->SetNumberOfBlocks(1);
mbdata->SetBlock(0, data);
data = mbdata.GetPointer();
}
// data must be a multiblock dataset, no matter what.
assert(data->IsA("vtkMultiBlockDataSet"));
// now deliver data to the rendering sides:
// first, reduce it to root node.
vtkNew<vtkReductionFilter> reductionFilter;
// For now we do not support parallel servers
//vtkNew<vtkPVMergeTablesMultiBlock> algo;
//reductionFilter->SetPostGatherHelper(algo.GetPointer());
reductionFilter->SetController(pm->GetGlobalController());
reductionFilter->SetInputData(data);
reductionFilter->Update();
data = reductionFilter->GetOutputDataObject(0);
if (this->EnableServerSideRendering && numProcs > 1)
{
// share the reduction result will all satellites.
pm->GetGlobalController()->Broadcast(data.GetPointer(), 0);
}
this->CacheKeeper->SetInputData(data);
}
// here the cachekeeper will either give us the cached result of the result
// we just processed.
this->CacheKeeper->Update();
data = this->CacheKeeper->GetOutputDataObject(0);
if (myId == 0)
{
// send data to client.
vtkNew<vtkClientServerMoveData> deliver;
deliver->SetInputData(data);
deliver->Update();
}
this->LocalOutput = vtkMultiBlockDataSet::SafeDownCast(data);
}
else
{
// receive data on client.
vtkNew<vtkClientServerMoveData> deliver;
deliver->Update();
this->LocalOutput = vtkMultiBlockDataSet::SafeDownCast(deliver->GetOutputDataObject(0));
}
this->LocalGrid = NULL;
this->LocalThreshold = NULL;
if (this->LocalOutput)
{
this->LocalGrid = vtkImageData::SafeDownCast(this->LocalOutput->GetBlock(2));
this->LocalThreshold = vtkTable::SafeDownCast(this->LocalOutput->GetBlock(3));
}
return vtkPVDataRepresentation::RequestData(request, inputVector, outputVector);
}
|