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
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkTreeMapView.cxx,v $
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.
=========================================================================*/
/*----------------------------------------------------------------------------
Copyright (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkTreeMapView.h"
#include "vtkActor.h"
#include "vtkActor2D.h"
#include "vtkAlgorithmOutput.h"
#include "vtkBoxLayoutStrategy.h"
#include "vtkCamera.h"
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkDataRepresentation.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInteractorStyleTreeMapHover.h"
#include "vtkLabeledTreeMapDataMapper.h"
#include "vtkLookupTable.h"
#include "vtkObjectFactory.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSelection.h"
#include "vtkSelectionLink.h"
#include "vtkSliceAndDiceLayoutStrategy.h"
#include "vtkSquarifyLayoutStrategy.h"
#include "vtkTextProperty.h"
#include "vtkTreeFieldAggregator.h"
#include "vtkTreeLevelsFilter.h"
#include "vtkTreeMapLayout.h"
#include "vtkTreeMapToPolyData.h"
#include "vtkViewTheme.h"
vtkCxxRevisionMacro(vtkTreeMapView, "$Revision: 1.3 $");
vtkStandardNewMacro(vtkTreeMapView);
//----------------------------------------------------------------------------
vtkTreeMapView::vtkTreeMapView()
{
this->ColorArrayNameInternal = 0;
this->TreeLevelsFilter = vtkTreeLevelsFilter::New();
this->TreeFieldAggregator = vtkTreeFieldAggregator::New();
this->TreeMapLayout = vtkTreeMapLayout::New();
this->BoxLayout = vtkBoxLayoutStrategy::New();
this->SliceAndDiceLayout = vtkSliceAndDiceLayoutStrategy::New();
this->SquarifyLayout = vtkSquarifyLayoutStrategy::New();
this->TreeMapToPolyData = vtkTreeMapToPolyData::New();
this->TreeMapMapper = vtkPolyDataMapper::New();
this->TreeMapActor = vtkActor::New();
this->LabelMapper = vtkLabeledTreeMapDataMapper::New();
this->LabelActor = vtkActor2D::New();
this->ColorLUT = vtkLookupTable::New();
// Replace the interactor style
vtkInteractorStyleTreeMapHover* style = vtkInteractorStyleTreeMapHover::New();
this->SetInteractorStyle(style);
style->Delete();
// Set up view
this->Renderer->GetActiveCamera()->ParallelProjectionOn();
style->SetLayout(this->TreeMapLayout);
style->SetTreeMapToPolyData(this->TreeMapToPolyData);
style->AddObserver(vtkCommand::UserEvent, this->GetObserver());
// Apply default theme
vtkViewTheme* theme = vtkViewTheme::New();
this->ApplyViewTheme(theme);
theme->Delete();
// Set up representation
this->TreeFieldAggregator->SetLeafVertexUnitSize(false);
this->TreeFieldAggregator->SetMinValue(1e-10);
this->TreeFieldAggregator->SetLogScale(false);
this->TreeMapToPolyData->SetLevelsFieldName("level");
this->ColorLUT->SetHueRange(0.667, 0);
this->ColorLUT->Build();
this->TreeMapMapper->SetLookupTable(ColorLUT);
this->LabelMapper->SetLabelFormat("%s");
this->LabelMapper->SetLabelModeToLabelFieldData();
this->LabelMapper->SetClipTextMode(0);
this->LabelActor->SetPickable(false);
// Set default properties
this->SetBorderPercentage(0.1);
this->SetSizeArrayName("size");
this->SetHoverArrayName("name");
this->SetLabelArrayName("name");
this->SetFontSizeRange(24, 10);
this->SetLayoutStrategyToSquarify();
// Wire pipeline
this->TreeFieldAggregator->SetInputConnection(
this->TreeLevelsFilter->GetOutputPort());
this->TreeMapLayout->SetInputConnection(
this->TreeFieldAggregator->GetOutputPort());
this->TreeMapToPolyData->SetInputConnection(
this->TreeMapLayout->GetOutputPort());
this->TreeMapMapper->SetInputConnection(
this->TreeMapToPolyData->GetOutputPort());
this->TreeMapActor->SetMapper(this->TreeMapMapper);
this->LabelMapper->SetInputConnection(
this->TreeMapLayout->GetOutputPort());
this->LabelActor->SetMapper(this->LabelMapper);
}
//----------------------------------------------------------------------------
vtkTreeMapView::~vtkTreeMapView()
{
this->SetColorArrayNameInternal(0);
// Delete objects
this->TreeLevelsFilter->Delete();
this->TreeFieldAggregator->Delete();
this->TreeMapLayout->Delete();
this->BoxLayout->Delete();
this->SliceAndDiceLayout->Delete();
this->SquarifyLayout->Delete();
this->TreeMapToPolyData->Delete();
this->TreeMapMapper->Delete();
this->TreeMapActor->Delete();
this->LabelMapper->Delete();
this->LabelActor->Delete();
this->ColorLUT->Delete();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetBorderPercentage(double pcent)
{
this->BoxLayout->SetBorderPercentage(pcent);
this->SliceAndDiceLayout->SetBorderPercentage(pcent);
this->SquarifyLayout->SetBorderPercentage(pcent);
}
//----------------------------------------------------------------------------
double vtkTreeMapView::GetBorderPercentage()
{
return this->BoxLayout->GetBorderPercentage();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetSizeArrayName(const char* name)
{
this->TreeFieldAggregator->SetField(name);
}
//----------------------------------------------------------------------------
const char* vtkTreeMapView::GetSizeArrayName()
{
return this->TreeFieldAggregator->GetField();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetHoverArrayName(const char* name)
{
vtkInteractorStyleTreeMapHover::SafeDownCast(this->InteractorStyle)->SetLabelField(name);
}
//----------------------------------------------------------------------------
const char* vtkTreeMapView::GetHoverArrayName()
{
return vtkInteractorStyleTreeMapHover::SafeDownCast(this->InteractorStyle)->GetLabelField();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetLabelArrayName(const char* name)
{
this->LabelMapper->SetFieldDataName(name);
}
//----------------------------------------------------------------------------
const char* vtkTreeMapView::GetLabelArrayName()
{
return this->LabelMapper->GetFieldDataName();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetColorArrayName(const char *field)
{
this->TreeMapMapper->SetScalarModeToUseCellFieldData();
this->TreeMapMapper->SelectColorArray(field);
this->SetColorArrayNameInternal(field);
}
//----------------------------------------------------------------------------
const char* vtkTreeMapView::GetColorArrayName()
{
return this->GetColorArrayNameInternal();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetLayoutStrategyToBox()
{
this->SetLayoutStrategy("Box");
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetLayoutStrategyToSliceAndDice()
{
this->SetLayoutStrategy("Slice And Dice");
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetLayoutStrategyToSquarify()
{
this->SetLayoutStrategy("Squarify");
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetLayoutStrategy(const char* name)
{
if (!strcmp(name, "Box"))
{
this->TreeMapLayout->SetLayoutStrategy(this->BoxLayout);
}
else if (!strcmp(name, "Slice And Dice"))
{
this->TreeMapLayout->SetLayoutStrategy(this->SliceAndDiceLayout);
}
else if (!strcmp(name, "Squarify"))
{
this->TreeMapLayout->SetLayoutStrategy(this->SquarifyLayout);
}
else
{
vtkErrorMacro("Unknown layout name: " << name);
}
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetFontSizeRange(const int maxSize, const int minSize, const int delta)
{
this->LabelMapper->SetFontSizeRange(maxSize, minSize, delta);
}
//----------------------------------------------------------------------------
void vtkTreeMapView::GetFontSizeRange(int range[3])
{
this->LabelMapper->GetFontSizeRange(range);
}
//----------------------------------------------------------------------------
void vtkTreeMapView::SetupRenderWindow(vtkRenderWindow* win)
{
this->Superclass::SetupRenderWindow(win);
win->GetInteractor()->SetInteractorStyle(this->InteractorStyle);
}
//----------------------------------------------------------------------------
void vtkTreeMapView::AddInputConnection(vtkAlgorithmOutput* conn)
{
if (this->TreeLevelsFilter->GetNumberOfInputConnections(0) == 0)
{
this->TreeLevelsFilter->SetInputConnection(conn);
this->Renderer->AddActor(this->TreeMapActor);
this->Renderer->AddActor(this->LabelActor);
this->Renderer->ResetCamera();
}
else
{
vtkErrorMacro("This view only supports one representation.");
}
}
//----------------------------------------------------------------------------
void vtkTreeMapView::RemoveInputConnection(vtkAlgorithmOutput* conn)
{
if (this->TreeLevelsFilter->GetNumberOfInputConnections(0) > 0 &&
this->TreeLevelsFilter->GetInputConnection(0, 0) == conn)
{
this->TreeLevelsFilter->RemoveInputConnection(0, conn);
this->Renderer->RemoveActor(this->TreeMapActor);
this->Renderer->RemoveActor(this->LabelActor);
}
}
//----------------------------------------------------------------------------
void vtkTreeMapView::ProcessEvents(
vtkObject* caller,
unsigned long eventId,
void* callData)
{
if (caller == this->InteractorStyle && eventId == vtkCommand::UserEvent)
{
// Create the selection
vtkSelection* selection = vtkSelection::New();
vtkIdTypeArray* list = vtkIdTypeArray::New();
vtkIdType* id = reinterpret_cast<vtkIdType*>(callData);
if (*id >= 0)
{
list->InsertNextValue(*id);
}
selection->SetSelectionList(list);
list->Delete();
// TODO: This should really be pedigree ids.
selection->GetProperties()->Set(vtkSelection::CONTENT_TYPE(), vtkSelection::INDICES);
// Call select on the representation(s)
this->GetRepresentation()->Select(this, selection);
selection->Delete();
}
else
{
Superclass::ProcessEvents(caller, eventId, callData);
}
}
//----------------------------------------------------------------------------
void vtkTreeMapView::PrepareForRendering()
{
vtkDataRepresentation* rep = this->GetRepresentation();
if (!rep)
{
return;
}
// Make sure the input connection is up to date.
vtkAlgorithmOutput* conn = rep->GetInputConnection();
if (this->TreeLevelsFilter->GetInputConnection(0, 0) != conn)
{
this->RemoveInputConnection(this->TreeLevelsFilter->GetInputConnection(0, 0));
this->AddInputConnection(conn);
}
// Use the most recent selection
vtkSelection* selection = rep->GetSelectionLink()->GetSelection();
// TODO: Should be pedigree ids.
if (selection->GetProperties()->Get(vtkSelection::CONTENT_TYPE()) != vtkSelection::INDICES)
{
vtkErrorMacro("Can only handle INDICES selections.");
return;
}
vtkIdTypeArray* arr = vtkIdTypeArray::SafeDownCast(selection->GetSelectionList());
vtkIdType id = -1;
if (arr->GetNumberOfTuples() > 0)
{
id = arr->GetValue(0);
}
vtkInteractorStyleTreeMapHover::SafeDownCast(this->InteractorStyle)->HighLightItem(id);
// Update the pipeline up until the treemap to polydata
this->TreeMapToPolyData->Update();
// Try to find the range the user-specified color array.
// If we cannot find that array, use the scalar range.
double range[2];
vtkDataArray* array = 0;
if (this->GetColorArrayName())
{
array = this->TreeMapToPolyData->GetOutput()->
GetCellData()->GetArray(this->GetColorArrayName());
}
if (array)
{
array->GetRange(range);
}
else
{
this->TreeMapToPolyData->GetOutput()->GetScalarRange(range);
}
this->TreeMapMapper->SetScalarRange(range[0], range[1]);
this->Superclass::PrepareForRendering();
}
//----------------------------------------------------------------------------
void vtkTreeMapView::ApplyViewTheme(vtkViewTheme* theme)
{
this->Renderer->SetBackground(theme->GetBackgroundColor());
//this->VertexActor->GetProperty()->SetColor(theme->GetPointColor());
//this->OutlineActor->GetProperty()->SetColor(theme->GetOutlineColor());
//this->VertexColorLUT->SetHueRange(theme->GetPointHueRange());
//this->VertexColorLUT->SetSaturationRange(theme->GetPointSaturationRange());
//this->VertexColorLUT->SetValueRange(theme->GetPointValueRange());
//this->VertexColorLUT->SetAlphaRange(theme->GetPointAlphaRange());
//this->VertexColorLUT->Build();
this->LabelMapper->GetLabelTextProperty()->SetColor(theme->GetVertexLabelColor());
//this->EdgeActor->GetProperty()->SetColor(theme->GetCellColor());
//this->EdgeActor->GetProperty()->SetOpacity(theme->GetCellOpacity());
//this->EdgeColorLUT->SetHueRange(theme->GetCellHueRange());
//this->EdgeColorLUT->SetSaturationRange(theme->GetCellSaturationRange());
//this->EdgeColorLUT->SetValueRange(theme->GetCellValueRange());
//this->EdgeColorLUT->SetAlphaRange(theme->GetCellAlphaRange());
//this->EdgeColorLUT->Build();
//this->SelectionEdgeActor->GetProperty()->SetColor(theme->GetSelectedCellColor());
//this->SelectionEdgeActor->GetProperty()->SetOpacity(theme->GetSelectedCellOpacity());
double color[3];
theme->GetSelectedPointColor(color);
vtkInteractorStyleTreeMapHover::SafeDownCast(this->InteractorStyle)->
SetSelectionLightColor(color[0], color[1], color[2]);
//this->SelectionVertexActor->GetProperty()->SetOpacity(theme->GetSelectedPointOpacity());
}
//----------------------------------------------------------------------------
void vtkTreeMapView::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "TreeLevelsFilter: " << endl;
this->TreeLevelsFilter->PrintSelf(os, indent.GetNextIndent());
os << indent << "TreeFieldAggregator: " << endl;
this->TreeFieldAggregator->PrintSelf(os, indent.GetNextIndent());
os << indent << "TreeMapLayout: " << endl;
this->TreeMapLayout->PrintSelf(os, indent.GetNextIndent());
os << indent << "BoxLayout: " << endl;
this->BoxLayout->PrintSelf(os, indent.GetNextIndent());
os << indent << "SliceAndDiceLayout: " << endl;
this->SliceAndDiceLayout->PrintSelf(os, indent.GetNextIndent());
os << indent << "SquarifyLayout: " << endl;
this->SquarifyLayout->PrintSelf(os, indent.GetNextIndent());
os << indent << "TreeMapToPolyData: " << endl;
this->TreeMapToPolyData->PrintSelf(os, indent.GetNextIndent());
os << indent << "TreeMapMapper: " << endl;
this->TreeMapMapper->PrintSelf(os, indent.GetNextIndent());
os << indent << "LabelMapper: " << endl;
this->LabelMapper->PrintSelf(os, indent.GetNextIndent());
os << indent << "ColorLUT: " << endl;
this->ColorLUT->PrintSelf(os, indent.GetNextIndent());
if (this->GetRepresentation())
{
os << indent << "TreeMapActor: " << endl;
this->TreeMapActor->PrintSelf(os, indent.GetNextIndent());
os << indent << "LabelActor: " << endl;
this->LabelActor->PrintSelf(os, indent.GetNextIndent());
}
}
|