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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGraphLayout.cxx
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 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkGraphLayout.h"
#include "vtkAbstractTransform.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkEventForwarderCommand.h"
#include "vtkFloatArray.h"
#include "vtkGraphLayoutStrategy.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkTable.h"
vtkStandardNewMacro(vtkGraphLayout);
vtkCxxSetObjectMacro(vtkGraphLayout, Transform, vtkAbstractTransform);
// ----------------------------------------------------------------------
vtkGraphLayout::vtkGraphLayout()
{
this->LayoutStrategy = 0;
this->StrategyChanged = false;
this->LastInput = NULL;
this->LastInputMTime = 0;
this->InternalGraph = 0;
this->ZRange = 0.0;
this->Transform = 0;
this->UseTransform = false;
this->EventForwarder = vtkEventForwarderCommand::New();
this->EventForwarder->SetTarget(this);
}
// ----------------------------------------------------------------------
vtkGraphLayout::~vtkGraphLayout()
{
if (this->LayoutStrategy)
{
this->LayoutStrategy->RemoveObserver(this->EventForwarder);
this->LayoutStrategy->Delete();
}
if (this->InternalGraph)
{
this->InternalGraph->Delete();
}
if (this->Transform)
{
this->Transform->Delete();
}
this->EventForwarder->Delete();
}
// ----------------------------------------------------------------------
void
vtkGraphLayout::SetLayoutStrategy(vtkGraphLayoutStrategy *strategy)
{
// This method is a cut and paste of vtkCxxSetObjectMacro
// except for the call to SetGraph in the middle :)
if (strategy != this->LayoutStrategy)
{
vtkGraphLayoutStrategy *tmp = this->LayoutStrategy;
if (tmp)
{
tmp->RemoveObserver(this->EventForwarder);
}
this->LayoutStrategy = strategy;
if (this->LayoutStrategy != NULL)
{
this->StrategyChanged = true;
this->LayoutStrategy->Register(this);
this->LayoutStrategy->AddObserver(vtkCommand::ProgressEvent,
this->EventForwarder);
if (this->InternalGraph)
{
// Set the graph in the layout strategy
this->LayoutStrategy->SetGraph(this->InternalGraph);
}
}
if (tmp != NULL)
{
tmp->UnRegister(this);
}
this->Modified();
}
}
// ----------------------------------------------------------------------
unsigned long
vtkGraphLayout::GetMTime()
{
unsigned long mTime = this->Superclass::GetMTime();
unsigned long time;
if (this->LayoutStrategy != NULL)
{
time = this->LayoutStrategy->GetMTime();
mTime = (time > mTime ? time : mTime);
}
return mTime;
}
// ----------------------------------------------------------------------
int
vtkGraphLayout::IsLayoutComplete()
{
if (this->LayoutStrategy)
{
return this->LayoutStrategy->IsLayoutComplete();
}
// This is an error condition
vtkErrorMacro("IsLayoutComplete called with layout strategy==NULL");
return 0;
}
// ----------------------------------------------------------------------
int
vtkGraphLayout::RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
if (this->LayoutStrategy == NULL)
{
vtkErrorMacro(<< "Layout strategy must be non-null.");
return 0;
}
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkGraph *input = vtkGraph::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkGraph *output = vtkGraph::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Is this a completely new input? Is it the same input as the last
// time the filter ran but with a new MTime? If either of those is
// true, make a copy and give it to the strategy object anew.
if (this->StrategyChanged ||
input != this->LastInput ||
input->GetMTime() > this->LastInputMTime)
{
if (this->StrategyChanged)
{
vtkDebugMacro(<<"Strategy changed so reading in input again.");
this->StrategyChanged = false;
}
else if (input != this->LastInput)
{
vtkDebugMacro(<<"Filter running with different input. Resetting in strategy.");
}
else
{
vtkDebugMacro(<<"Input modified since last run. Resetting in strategy.");
}
if (this->InternalGraph)
{
this->InternalGraph->Delete();
}
this->InternalGraph = input->NewInstance();
// The strategy object is going to modify the Points member so
// we'll replace that with a deep copy. For everything else a
// shallow copy is sufficient.
this->InternalGraph->ShallowCopy(input);
// The copy of the points will be to a float type
vtkPoints* newPoints = vtkPoints::New(VTK_FLOAT);
newPoints->DeepCopy(input->GetPoints());
this->InternalGraph->SetPoints(newPoints);
newPoints->Delete();
// Save information about the input so that we can detect when
// it's changed on future runs. According to the VTK pipeline
// design, this is a bad thing. In this case there's no
// particularly graceful way around it since the pipeline was not
// designed to support incremental execution.
this->LastInput = input;
this->LastInputMTime = input->GetMTime();
// Give the layout strategy a pointer to the input. We set it to
// NULL first to force the layout algorithm to re-initialize
// itself. This is necessary in case the input is the same data
// object with a newer mtime.
this->LayoutStrategy->SetGraph(NULL);
this->LayoutStrategy->SetGraph(this->InternalGraph);
} // Done handling a new or changed filter input.
// No matter whether the input is new or not, the layout strategy
// needs to do its thing. It modifies its input
// (this->InternalGraph) so we can just use that as the output.
this->LayoutStrategy->Layout();
output->ShallowCopy(this->InternalGraph);
// Perturb points so they do not all have the same z value.
if (this->ZRange != 0.0)
{
vtkIdType numVert = output->GetNumberOfVertices();
double x[3];
bool onPlane = true;
for (vtkIdType i = 0; i < numVert; ++i)
{
output->GetPoint(i, x);
if (x[2] != 0.0)
{
onPlane = false;
break;
}
}
if (onPlane)
{
vtkPoints* pts = vtkPoints::New();
pts->SetNumberOfPoints(numVert);
for (vtkIdType i = 0; i < numVert; ++i)
{
output->GetPoint(i, x);
x[2] = this->ZRange*static_cast<double>(i)/numVert;
pts->SetPoint(i, x);
}
output->SetPoints(pts);
pts->Delete();
}
}
if (this->UseTransform && this->Transform)
{
vtkIdType numVert = output->GetNumberOfVertices();
double x[3];
double y[3];
vtkPoints* pts = vtkPoints::New();
pts->SetNumberOfPoints(numVert);
for (vtkIdType i = 0; i < numVert; ++i)
{
output->GetPoint(i, x);
this->Transform->TransformPoint(x, y);
pts->SetPoint(i, y);
}
output->SetPoints(pts);
pts->Delete();
}
return 1;
}
// ----------------------------------------------------------------------
void vtkGraphLayout::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "StrategyChanged: " << (this->StrategyChanged ? "True" : "False") << endl;
os << indent << "LayoutStrategy: " << (this->LayoutStrategy ? "" : "(none)") << endl;
if (this->LayoutStrategy)
{
this->LayoutStrategy->PrintSelf(os, indent.GetNextIndent());
}
os << indent << "InternalGraph: " << (this->InternalGraph ? "" : "(none)") << endl;
if (this->InternalGraph)
{
this->InternalGraph->PrintSelf(os, indent.GetNextIndent());
}
os << indent << "ZRange: " << this->ZRange << endl;
os << indent << "Transform: " << (this->Transform ? "" : "(none)") << endl;
if (this->Transform)
{
this->Transform->PrintSelf(os, indent.GetNextIndent());
}
os << indent << "UseTransform: " << (this->UseTransform ? "True" : "False") << endl;
}
|