File: vtkStackedTreeLayoutStrategy.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (604 lines) | stat: -rw-r--r-- 18,937 bytes parent folder | download | duplicates (8)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkStackedTreeLayoutStrategy.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 "vtkStackedTreeLayoutStrategy.h"

#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkFloatArray.h"
#include "vtkDataArray.h"
#include "vtkIntArray.h"
#include "vtkTree.h"
#include "vtkTreeDFSIterator.h"
#include "vtkTreeLevelsFilter.h"

#include "vtkSmartPointer.h"
#define VTK_CREATE(type, name)                                  \
  vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

vtkStandardNewMacro(vtkStackedTreeLayoutStrategy);

vtkStackedTreeLayoutStrategy::vtkStackedTreeLayoutStrategy()
{
  this->InteriorRadius = 6.0;
  this->RingThickness = 1.0;
  this->RootStartAngle = 0.;
  this->RootEndAngle = 360.;
  this->UseRectangularCoordinates = false;
  this->Reverse = false;
  this->InteriorLogSpacingValue = 1.0;
}

vtkStackedTreeLayoutStrategy::~vtkStackedTreeLayoutStrategy()
{
}

void vtkStackedTreeLayoutStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  os << indent << "InteriorRadius: " << this->InteriorRadius << endl;
  os << indent << "RingThickness: " << this->RingThickness << endl;
  os << indent << "RootStartAngle: " << this->RootStartAngle << endl;
  os << indent << "RootEndAngle: " << this->RootEndAngle << endl;
  os << indent << "UseRectangularCoordinates: " << this->UseRectangularCoordinates << endl;
  os << indent << "Reverse: " << this->Reverse << endl;
  os << indent << "InteriorLogSpacingValue: " << this->InteriorLogSpacingValue << endl;
}

void vtkStackedTreeLayoutStrategy::Layout(vtkTree* inputTree,
                                          vtkDataArray* coordsArray,
                                          vtkDataArray* sizeArray)
{
  if( !inputTree || inputTree->GetNumberOfVertices() == 0 )
    {
    return;
    }
  if (!coordsArray)
    {
    vtkErrorMacro("Area array not defined.");
    return;
    }

  vtkDataSetAttributes* data = inputTree->GetVertexData();

  VTK_CREATE(vtkDoubleArray, textRotationArray);
  textRotationArray->SetName( "TextRotation" );
  textRotationArray->SetNumberOfComponents(1);
  textRotationArray->SetNumberOfTuples(inputTree->GetNumberOfVertices());
  data->AddArray( textRotationArray );

  VTK_CREATE(vtkDoubleArray, textBoundedSizeArray);
  textBoundedSizeArray->SetName( "TextBoundedSize" );
  textBoundedSizeArray->SetNumberOfComponents(2);
  textBoundedSizeArray->SetNumberOfTuples(inputTree->GetNumberOfVertices());
  data->AddArray( textBoundedSizeArray );

  double outer_radius = 0.0;
  if (this->Reverse)
    {
    VTK_CREATE(vtkTreeLevelsFilter, levelFilter);
    VTK_CREATE(vtkTree, newTree);
    newTree->ShallowCopy( inputTree );
    levelFilter->SetInputData( newTree );
    levelFilter->Update();
    vtkTree* levelTree = levelFilter->GetOutput();

    vtkIntArray *levelArray = vtkIntArray::SafeDownCast(
        levelTree->GetVertexData()->GetAbstractArray("level"));
    int max_level = 0;
    for( int i = 0; i < levelTree->GetNumberOfVertices(); i++ )
      {
      int level = levelArray->GetValue(i);
      if( level > max_level )
        {
        max_level = level;
        }
      }
    outer_radius = max_level*this->RingThickness + this->InteriorRadius;
    }

  // Get the root vertex and set it
  vtkIdType rootId = inputTree->GetRoot();
  float coords[4] = {this->RootStartAngle, this->RootEndAngle, 0.0, 0.0};
  if (this->Reverse)
    {
    coords[2] = outer_radius - this->RingThickness;
    coords[3] = outer_radius;
    }
  else
    {
    coords[3] = this->InteriorRadius;
    }
  coordsArray->SetTuple(rootId, coords);

  // Now layout the children vertices
  this->LayoutChildren(inputTree, coordsArray, sizeArray,
      inputTree->GetNumberOfChildren(rootId),
      rootId, 0, coords[2], coords[3], coords[0], coords[1]);

  vtkPoints* points = vtkPoints::New();
  vtkIdType numVerts = inputTree->GetNumberOfVertices();
  points->SetNumberOfPoints(numVerts);
  for( vtkIdType i = 0; i < numVerts; i++ )
    {
    double sector_coords[4];
    coordsArray->GetTuple( i, sector_coords );
    double x, y, z;
    if( this->UseRectangularCoordinates )
      {
      x = 0.5*(sector_coords[0] + sector_coords[1]);
      y = 0.5*(sector_coords[2] + sector_coords[3]);
      z = 0.;

      textRotationArray->SetValue( i, 0 );
      textBoundedSizeArray->SetValue( 2*i, sector_coords[1] - sector_coords[0]);
      textBoundedSizeArray->SetValue( 2*i + 1, sector_coords[3] - sector_coords[2] );
      }
    else
      {
      if( i == rootId )
        {
        x = y = z = 0.;

        textRotationArray->SetValue( i, 0 );
        textBoundedSizeArray->SetValue( 2*i, 0 );
        textBoundedSizeArray->SetValue( 2*i + 1, 0 );
        }
      else
        {
        double r = (0.5*(sector_coords[3] - sector_coords[2])) + sector_coords[2];
        double theta = sector_coords[0] + (0.5*(sector_coords[1]-sector_coords[0]));
        x = r * cos( vtkMath::RadiansFromDegrees( theta ) );
        y = r * sin( vtkMath::RadiansFromDegrees( theta ) );
        z = 0.;

        double sector_arc_length = r * vtkMath::RadiansFromDegrees(sector_coords[1] - sector_coords[0]);
        double radial_arc_length = sector_coords[3] - sector_coords[2];
        double aspect_ratio = sector_arc_length / radial_arc_length;
        if( aspect_ratio > 1 )
          {
          //sector length is greater than radial length;
          // align text with the sector
          if( theta > 0. && theta < 180. )
            {
            textRotationArray->SetValue( i, theta - 90. );
            }
          else
            {
            textRotationArray->SetValue( i, theta + 90. );
            }
          textBoundedSizeArray->SetValue( 2*i, sector_arc_length );
          textBoundedSizeArray->SetValue( 2*i + 1, radial_arc_length );
          }
        else
          {
          //radial length is greater than sector length;
          // align text radially...
          if( theta > 90. && theta < 270. )
            {
            textRotationArray->SetValue( i, theta - 180. );
            }
          else
            {
            textRotationArray->SetValue( i, theta );
            }
          textBoundedSizeArray->SetValue( 2*i, radial_arc_length );
          textBoundedSizeArray->SetValue( 2*i + 1, sector_arc_length );
          }
        }
      }
    points->SetPoint(i, x, y, z);
    }
  inputTree->SetPoints(points);
  points->Delete();
}

void vtkStackedTreeLayoutStrategy::LayoutEdgePoints(
  vtkTree* inputTree,
  vtkDataArray* sectorsArray,
  vtkDataArray* vtkNotUsed(sizeArray),
  vtkTree* outputTree)
{
  VTK_CREATE(vtkTreeLevelsFilter, levelFilter);
  VTK_CREATE(vtkTree, newTree);
  newTree->ShallowCopy( inputTree );
  levelFilter->SetInputData( newTree );
  levelFilter->Update();
  vtkTree* levelTree = levelFilter->GetOutput();
  outputTree->ShallowCopy( levelTree );

  vtkIntArray* levelArray = vtkIntArray::SafeDownCast(
      levelTree->GetVertexData()->GetAbstractArray("level"));

  double exteriorRadius = VTK_DOUBLE_MAX;
  double sector_coords[4];
  int max_level = 0;
  for( int i = 0; i < outputTree->GetNumberOfVertices(); i++ )
    {
    int level = levelArray->GetValue(i);
    if( level > max_level )
      {
      max_level = level;
      }
    if (inputTree->IsLeaf(i))
      {
      sectorsArray->GetTuple( i, sector_coords );
      if (sector_coords[2] < exteriorRadius)
        {
        exteriorRadius = sector_coords[2];
        }
      }
    }

  double spacing = this->InteriorLogSpacingValue;

  // The distance between level L-1 and L is s^L.
  // Thus, if s < 1 then the distance between levels gets smaller in higher levels,
  //       if s = 1 the distance remains the same, and
  //       if s > 1 the distance get larger.
  // The height (distance from the root) of level L, then, is
  // s + s^2 + s^3 + ... + s^L, where s is the log spacing value.
  // The max height (used for normalization) is
  // s + s^2 + s^3 + ... + s^maxLevel.
  // The quick formula for computing this is
  // sum_{i=1}^{n} s^i = (s^(n+1) - 1)/(s - 1) - 1        if s != 1
  //                   = n                                if s == 1
  double maxHeight = max_level;
  double eps = 1e-8;
  double diff = spacing - 1.0 > 0 ? spacing - 1.0 : 1.0 - spacing;
  if (diff > eps)
    {
    maxHeight = (pow(spacing, max_level+1.0) - 1.0)/(spacing - 1.0) - 1.0;
    }

  vtkPoints* points = vtkPoints::New();
  vtkIdType rootId = outputTree->GetRoot();
  vtkIdType numVerts = outputTree->GetNumberOfVertices();
  points->SetNumberOfPoints(numVerts);
  for( vtkIdType i = 0; i < numVerts; i++ )
    {
    if( !this->UseRectangularCoordinates && i == rootId )
      {
      points->SetPoint( i, 0, 0, 0 );
      continue;
      }

    sectorsArray->GetTuple( i, sector_coords );

    double x = 0.0;
    double y = 0.0;
    double z = 0.0;
    if( this->UseRectangularCoordinates )
      {
      if( inputTree->IsLeaf(i) )
        {
        if( this->Reverse )
          {
          y = sector_coords[2];
          }
        else
          {
          y = sector_coords[3];
          }
        }
      else
        {
        if( this->Reverse )
          {
          y = this->InteriorRadius - this->RingThickness*(maxHeight + maxHeight - inputTree->GetLevel(i));
          }
        else
          {
          y = this->InteriorRadius + this->RingThickness*(maxHeight + maxHeight - inputTree->GetLevel(i));
          }
        }
      x = 0.5*(sector_coords[0] + sector_coords[1]);
      z = 0.;
      }
    else
      {
      double r;
      if( inputTree->IsLeaf(i) )
        {
        r = sector_coords[2];
        }
      else
        {
        if (diff <= eps)
          {
          r = outputTree->GetLevel(i)/maxHeight;
          }
        else
          {
          r = ((pow(spacing, outputTree->GetLevel(i)+1.0) - 1.0)/(spacing - 1.0) - 1.0)/maxHeight;
          }
        // scale the spacing value based on the radius of the
        // circle we have to work with...
        r *= exteriorRadius;
        }

      double theta = sector_coords[0] + (0.5*(sector_coords[1]-sector_coords[0]));
      x = r * cos( vtkMath::RadiansFromDegrees( theta ) );
      y = r * sin( vtkMath::RadiansFromDegrees( theta ) );
      z = 0.;
      }
    points->SetPoint(i, x, y, z);
    }
  outputTree->SetPoints(points);
  points->Delete();
}

void vtkStackedTreeLayoutStrategy::LayoutChildren(
  vtkTree *tree, vtkDataArray *coordsArray, vtkDataArray *sizeArray,
  vtkIdType nchildren, vtkIdType parent, vtkIdType begin,
  float parentInnerRad, float parentOuterRad,
  float parentStartAng, float parentEndAng)
{
  double new_interior_rad = 0.0;
  double new_outer_rad = 0.0;
  if (this->Reverse)
    {
    new_interior_rad = parentInnerRad - this->RingThickness;
    new_outer_rad = parentInnerRad;
    }
  else
    {
    new_interior_rad = parentOuterRad;
    new_outer_rad = new_interior_rad + this->RingThickness;
    }
  //FIXME - we may want to do this instead...
  //double new_outer_rad = new_interior_rad +this->RingThickness[level];

  double radial_spacing = this->ShrinkPercentage * this->RingThickness;
  new_outer_rad -= radial_spacing;
  //new_interior_rad += 0.5*radial_spacing;

  //now calculate the width of each of the sectors for each vertex
  // first calculate the total summed weight for each of the children vertices
  double total_weighted_sum = 0;
  vtkIdType i;
  for( i = begin; i < nchildren; i++)
    {
    if (sizeArray)
      {
      total_weighted_sum +=
        static_cast<float>(sizeArray->GetTuple1(tree->GetChild(parent, i)));
      }
    else
      {
      total_weighted_sum += 1.0;
      }
    }

  // If we are doing radial layout, put extra space on the full rings
  // so the first and last children don't butt up against each other.
  vtkIdType num_spaces = nchildren - 1;
  double parent_angle = parentEndAng - parentStartAng;
  if (!this->UseRectangularCoordinates && parent_angle == 360.0)
    {
    num_spaces = nchildren;
    }
  double available_angle = parent_angle;
  double conversion = vtkMath::Pi()/180.0;
  double spacing = 0.0;
  if (nchildren > 1)
    {
    double parent_length;
    if (this->UseRectangularCoordinates)
      {
      parent_length = parent_angle;
      }
    else
      {
      parent_length = conversion * parent_angle * new_outer_rad;
      }
    double spacing_length;
    if (radial_spacing * num_spaces > 0.25 * parent_length)
      {
      spacing_length = 0.25 * parent_length;
      }
    else
      {
      spacing_length = radial_spacing * num_spaces;
      }
    double total_space;
    if (this->UseRectangularCoordinates)
      {
      total_space = spacing_length;
      }
    else
      {
      total_space = spacing_length / new_outer_rad / conversion;
      }
    spacing = total_space / num_spaces;
    available_angle -= total_space;
    }

  float coords[4];
  double current_angle = parentStartAng;
  for( i = begin; i < nchildren; i++)
    {
    int id = tree->GetChild(parent, i);
    float cur_size = 1.0;
    if (sizeArray)
      {
      cur_size = static_cast<float>(sizeArray->GetTuple1(id));
      }
    double this_arc = available_angle *
      ( cur_size / total_weighted_sum );

    coords[2] = new_interior_rad;
    coords[3] = new_outer_rad;
    coords[0] = current_angle;
    coords[1] = current_angle + this_arc;

    coordsArray->SetTuple(id, coords);

    current_angle += this_arc + spacing;

    vtkIdType numNewChildren = tree->GetNumberOfChildren(id);
    if (numNewChildren > 0)
      {
      this->LayoutChildren(tree, coordsArray, sizeArray, numNewChildren, id, 0,
                           coords[2], coords[3], coords[0], coords[1]);
      }
    }
}

vtkIdType vtkStackedTreeLayoutStrategy::FindVertex(
    vtkTree* otree,
    vtkDataArray* array,
    float pnt[2])
{
  if (this->UseRectangularCoordinates)
    {
    float blimits[4];
    vtkIdType vertex = otree->GetRoot();
    if(vertex < 0)
      {
      return vertex;
      }
    vtkFloatArray *boundsInfo = vtkFloatArray::SafeDownCast(array);

    // Now try to find the vertex that contains the point
    boundsInfo->GetTupleValue(vertex, blimits); // Get the extents of the root
    if( ((pnt[1] > blimits[2]) && (pnt[1] < blimits[3])) &&
        ((pnt[0] > blimits[0]) && (pnt[0] < blimits[1])) )
      {
      // Point is at the root vertex.
      return vertex;
      }

    // Now traverse the children to try and find
    // the vertex that contains the point
    vtkIdType child;
    VTK_CREATE(vtkTreeDFSIterator, it);
    it->SetTree( otree );
    it->SetStartVertex( vertex );

    while (it->HasNext())
      {
      child = it->Next();
      boundsInfo->GetTupleValue(child, blimits); // Get the extents of the child
      bool beyond_radial_bounds = false;
      bool beyond_angle_bounds = false;
      if( (pnt[1] < blimits[2]) || (pnt[1] > blimits[3]))
        beyond_radial_bounds = true;
      if( (pnt[0] < blimits[0]) || (pnt[0] > blimits[1]))
        beyond_angle_bounds = true;

      if( beyond_radial_bounds || beyond_angle_bounds )
        {
        continue;
        }
      // If we are here then the point is contained by the child
      return child;
      }
    }
  else
    {
    // Radial layout
    float polar_location[2];
    polar_location[0] = sqrt( ( pnt[0] * pnt[0] ) + ( pnt[1] * pnt[1] ) );
    polar_location[1] = vtkMath::DegreesFromRadians( atan2( pnt[1], pnt[0] ) );
    if( polar_location[1] < 0 )
      polar_location[1] += 360.;

    float blimits[4];
    vtkIdType vertex = otree->GetRoot();
    if(vertex < 0)
      {
      return vertex;
      }
    vtkFloatArray *boundsInfo = vtkFloatArray::SafeDownCast(array);

    // Now try to find the vertex that contains the point
    boundsInfo->GetTupleValue(vertex, blimits); // Get the extents of the root
    if( ((polar_location[0] > blimits[2]) && (polar_location[0] < blimits[3])) &&
        ((polar_location[1] > blimits[0]) && (polar_location[1] < blimits[1])) )
      {
      // Point is at the root vertex.
      // but we don't want the root to be pickable, so return -1.
      // This won't work for blimits spanning the 0/360 rollover, but the test below
      // catches that case.
      return -1;
      }

    // Now traverse the children to try and find
    // the vertex that contains the point
    vtkIdType child;
    VTK_CREATE(vtkTreeDFSIterator, it);
    it->SetTree( otree );
    it->SetStartVertex( vertex );

    while (it->HasNext())
      {
      child = it->Next();
      // if the root boundary starts anywhere but zero, the root node will have passed
      // the earlier test.  This will skip the root and prevent it from being picked.
      if (child == vertex)
        {
        continue;
        }
      boundsInfo->GetTupleValue(child, blimits); // Get the extents of the child

      // the range checking below doesn't work if either or both of blimits > 360
      if ((blimits[0] > 360.0) && (blimits[1] > 360.0))
        {
        blimits[0] -= 360.0;
        blimits[1] -= 360.0;
        }
      else if ((blimits[0] < 360.0) && (blimits[1] > 360.0) && (polar_location[1] < 360.0))
        {  // if the range spans the rollover at 0/360 on the circle
        if (polar_location[1] < 90.0)
          {
          blimits[0] = 0.0;
          blimits[1] -= 360.0;
          }
        else if (polar_location[1] > 270.)
          {
          blimits[1] = 360.0;
          }
        }
      bool beyond_radial_bounds = false;
      bool beyond_angle_bounds = false;
      if( (polar_location[0] < blimits[2]) || (polar_location[0] > blimits[3]))
        beyond_radial_bounds = true;
      if( (polar_location[1] < blimits[0]) || (polar_location[1] > blimits[1]))
        beyond_angle_bounds = true;

      if( beyond_radial_bounds || beyond_angle_bounds )
        {
        continue;
        }
      // If we are here then the point is contained by the child
      return child;
      }
    }
  return -1;
}