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
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkTreeLayoutStrategy.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 "vtkTreeLayoutStrategy.h"
#include "vtkAbstractArray.h"
#ifdef VTK_USE_BOOST
#include "vtkBoostBreadthFirstSearchTree.h"
#endif
#include "vtkDataArray.h"
#include "vtkIdTypeArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkTree.h"
#include "vtkTreeDFSIterator.h"
vtkCxxRevisionMacro(vtkTreeLayoutStrategy, "$Revision: 1.3 $");
vtkStandardNewMacro(vtkTreeLayoutStrategy);
vtkTreeLayoutStrategy::vtkTreeLayoutStrategy()
{
this->Angle = 90;
this->Radial = false;
this->LogSpacingValue = 1.0;
this->LeafSpacing = 0.9;
this->DistanceArrayName = NULL;
}
vtkTreeLayoutStrategy::~vtkTreeLayoutStrategy()
{
this->SetDistanceArrayName(NULL);
}
// Tree layout method
void vtkTreeLayoutStrategy::Layout()
{
vtkTree* tree = vtkTree::SafeDownCast(this->Graph);
if (tree == NULL)
{
#ifdef VTK_USE_BOOST
// Use the BFS search tree to perform the layout
vtkBoostBreadthFirstSearchTree* bfs = vtkBoostBreadthFirstSearchTree::New();
bfs->CreateGraphVertexIdArrayOn();
bfs->SetInput(this->Graph);
bfs->Update();
tree = vtkTree::New();
tree->ShallowCopy(bfs->GetOutput());
bfs->Delete();
#else
vtkErrorMacro("Layout only works on vtkTree unless VTK_USE_BOOST is on.");
#endif
}
vtkPoints* newPoints = vtkPoints::New();
newPoints->SetNumberOfPoints(tree->GetNumberOfVertices());
// Check if the distance array is defined.
vtkDataArray* distanceArr = NULL;
if (this->DistanceArrayName != NULL)
{
vtkAbstractArray* aa = tree->GetVertexData()->
GetAbstractArray(this->DistanceArrayName);
if (!aa)
{
vtkErrorMacro("Distance array not found.");
return;
}
distanceArr = vtkDataArray::SafeDownCast(aa);
if (!distanceArr)
{
vtkErrorMacro("Distance array must be a data array.");
return;
}
}
double maxDistance = 1.0;
if (distanceArr)
{
maxDistance = distanceArr->GetMaxNorm();
}
// Count the number of leaves in the tree
// and get the maximum depth
vtkIdType leafCount = 0;
vtkIdType maxLevel = 0;
vtkTreeDFSIterator* iter = vtkTreeDFSIterator::New();
iter->SetTree(tree);
while (iter->HasNext())
{
vtkIdType vertex = iter->Next();
if (tree->IsLeaf(vertex))
{
leafCount++;
}
if (tree->GetLevel(vertex) > maxLevel)
{
maxLevel = tree->GetLevel(vertex);
}
}
// Don't count the root in the list of internal nodes.
vtkIdType internalCount = tree->GetNumberOfVertices() - leafCount - 1;
double leafSpacing = this->LeafSpacing / static_cast<double>(leafCount);
double internalSpacing = (1.0 - this->LeafSpacing) / static_cast<double>(internalCount);
double angleRad = this->Angle * vtkMath::Pi() / 180.0;
double spacing;
if (this->LogSpacingValue == 1.0)
{
if (this->Radial)
{
spacing = 1.0 / maxLevel;
}
else
{
spacing = 0.5 / tan(angleRad / 2);
}
}
else
{
spacing = this->LogSpacingValue;
}
double curPlace = 0;
iter->SetMode(vtkTreeDFSIterator::FINISH);
while (iter->HasNext())
{
vtkIdType vertex = iter->Next();
double height;
if (distanceArr != NULL)
{
height = spacing * distanceArr->GetTuple1(vertex) / maxDistance;
}
else
{
if (this->LogSpacingValue == 1.0)
{
height = spacing * tree->GetLevel(vertex) / static_cast<double>(maxLevel);
}
else
{
height = (1 - pow(spacing, tree->GetLevel(vertex) + 1.0)) / (1 - spacing) - 1.0;
}
}
double x, y;
if (this->Radial)
{
double ang;
if (tree->IsLeaf(vertex))
{
ang = 2.0 * vtkMath::Pi() * curPlace;
ang *= this->Angle / 360.0;
ang -= vtkMath::Pi() / 2.0 + vtkMath::Pi()*this->Angle / 180.0;
curPlace += leafSpacing;
}
else
{
curPlace += internalSpacing;
vtkIdType nchildren;
const vtkIdType* children;
tree->GetChildren(vertex, nchildren, children);
double minAng = 2*vtkMath::Pi();
double maxAng = 0.0;
double angSinSum = 0.0;
double angCosSum = 0.0;
for (vtkIdType c = 0; c < nchildren; c++)
{
double pt[3];
newPoints->GetPoint(children[c], pt);
double leafAngle = atan2(pt[1], pt[0]);
if (leafAngle < 0)
{
leafAngle += 2*vtkMath::Pi();
}
if (leafAngle < minAng)
{
minAng = leafAngle;
}
if (leafAngle > maxAng)
{
maxAng = leafAngle;
}
angSinSum += sin(leafAngle);
angCosSum += cos(leafAngle);
}
// This is how to take the average of the two angles minAng, maxAng
ang = atan2(sin(minAng) + sin(maxAng), cos(minAng) + cos(maxAng));
// Make sure the angle is on the same "side" as the average angle.
// If not, add pi to the angle. This handles some border cases.
double avgAng = atan2(angSinSum, angCosSum);
if (sin(ang)*sin(avgAng) + cos(ang)*cos(avgAng) < 0)
{
ang += vtkMath::Pi();
}
}
x = height * cos(ang);
y = height * sin(ang);
}
else
{
double width = 2.0 * tan(vtkMath::Pi()*this->Angle / 180.0 / 2.0);
y = -height;
if (tree->IsLeaf(vertex))
{
x = width * curPlace;
curPlace += leafSpacing;
}
else
{
curPlace += internalSpacing;
vtkIdType nchildren;
const vtkIdType* children;
tree->GetChildren(vertex, nchildren, children);
double minX = VTK_DOUBLE_MAX;
double maxX = VTK_DOUBLE_MIN;
for (vtkIdType c = 0; c < nchildren; c++)
{
double pt[3];
newPoints->GetPoint(children[c], pt);
if (pt[0] < minX)
{
minX = pt[0];
}
if (pt[0] > maxX)
{
maxX = pt[0];
}
}
x = (minX + maxX) / 2.0;
}
}
newPoints->SetPoint(vertex, x, y, 0.0);
}
// Copy coordinates back into the original graph
if (vtkTree::SafeDownCast(this->Graph))
{
this->Graph->SetPoints(newPoints);
}
#ifdef VTK_USE_BOOST
else
{
// Reorder the points based on the mapping back to graph vertex ids
vtkPoints* reordered = vtkPoints::New();
reordered->SetNumberOfPoints(newPoints->GetNumberOfPoints());
for (vtkIdType i = 0; i < reordered->GetNumberOfPoints(); i++)
{
reordered->SetPoint(i, 0, 0, 0);
}
vtkIdTypeArray* graphVertexIdArr = vtkIdTypeArray::SafeDownCast(
tree->GetVertexData()->GetAbstractArray("GraphVertexId"));
for (vtkIdType i = 0; i < graphVertexIdArr->GetNumberOfTuples(); i++)
{
reordered->SetPoint(graphVertexIdArr->GetValue(i), newPoints->GetPoint(i));
}
this->Graph->SetPoints(reordered);
tree->Delete();
reordered->Delete();
}
#endif
// Clean up.
iter->Delete();
newPoints->Delete();
}
void vtkTreeLayoutStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Angle: " << this->Angle << endl;
os << indent << "Radial: " << (this->Radial ? "true" : "false") << endl;
os << indent << "LogSpacingValue: " << this->LogSpacingValue << endl;
os << indent << "LeafSpacing: " << this->LeafSpacing << endl;
os << indent << "DistanceArrayName: "
<< (this->DistanceArrayName ? this->DistanceArrayName : "(null)") << endl;
}
#if 0
// Code storage
#include "vtkGraphToBoostAdapter.h"
#include "vtkTreeToBoostAdapter.h"
#include <boost/graph/visitors.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/property_map.hpp>
#include <boost/vector_property_map.hpp>
#include <boost/pending/queue.hpp>
using namespace boost;
// Redefine the bfs visitor, the only visitor we
// are using is the tree_edge visitor.
template <typename PlacementMap>
class placement_visitor : public default_dfs_visitor
{
public:
placement_visitor() { }
placement_visitor(PlacementMap dist, typename property_traits<PlacementMap>::value_type ii = 1)
: d(dist), cur_place(0), internal_inc(ii) { }
template <typename Vertex, typename Graph>
void finish_vertex(Vertex v, const Graph& g)
{
put(d, v, cur_place);
if (g->IsLeaf(v))
{
cur_place += 1;
}
else
{
cur_place += internal_inc;
}
}
typename property_traits<PlacementMap>::value_type max_place() { return cur_place; }
private:
PlacementMap d;
typename property_traits<PlacementMap>::value_type cur_place;
typename property_traits<PlacementMap>::value_type internal_inc;
};
// Create a color map (used for marking visited nodes)
//vector_property_map<default_color_type> color;
//vtkDoubleArray* placement = vtkDoubleArray::New();
//depth_first_search(tree,
// placement_visitor<vtkDoubleArray*>(placement, 1.0),
// color, tree->GetRoot());
#endif
|