File: vtkCirclePackFrontChainLayoutStrategy.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 (818 lines) | stat: -rw-r--r-- 28,211 bytes parent folder | download | duplicates (4)
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCirclePackFrontChainLayoutStrategy.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 "vtkCirclePackFrontChainLayoutStrategy.h"
#include "vtkAdjacentVertexIterator.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkTree.h"
#include "vtkMath.h"
#include <vector>
#include <limits>
#include <list>
#include <math.h>

vtkStandardNewMacro(vtkCirclePackFrontChainLayoutStrategy);

class vtkCirclePackFrontChainLayoutStrategyImplementation
{

public:
  vtkCirclePackFrontChainLayoutStrategyImplementation();
  ~vtkCirclePackFrontChainLayoutStrategyImplementation();

  void createCirclePacking(vtkTree* tree,
                           vtkDataArray* sizeArray,
                           vtkDataArray* circlesArray,
                           int height,
                           int width);

  void packTreeNodes(vtkIdType treeNode,
                     double originX,
                     double originY,
                     double enclosingCircleRadius,
                     vtkDataArray* circlesArray,
                     vtkDataArray* sizeArray,
                     vtkTree* tree);

  void packBrotherNodes(std::vector<vtkIdType>& packedNodes,
                        double originX,
                        double originY,
                        double enclosingCircleRadius,
                        vtkDataArray* circlesArray,
                        vtkDataArray* sizeArray,
                        vtkTree* tree);

  void findCircleCenter(vtkIdType Ci,
                        vtkIdType Cm,
                        vtkIdType Cn,
                        vtkDataArray* circlesArray);

  void findIntersectingCircle(vtkIdType Ci,
                              bool& CjAfterCn,
                              std::list<vtkIdType>::iterator& Cj,
                              std::list<vtkIdType>::iterator Cm,
                              std::list<vtkIdType>::iterator Cn,
                              vtkDataArray* circlesArray,
                              std::list<vtkIdType>& frontChain);

  bool validCjAfterCn(vtkIdType Ci,
                      std::list<vtkIdType>::iterator Cm,
                      std::list<vtkIdType>::iterator Cn,
                      vtkDataArray* circlesArray,
                      std::list<vtkIdType>& frontChain,
                      int searchPathLength);

  bool validCjBeforeCm(vtkIdType Ci,
                       std::list<vtkIdType>::iterator Cm,
                       std::list<vtkIdType>::iterator Cn,
                       vtkDataArray* circlesArray,
                       std::list<vtkIdType>& frontChain,
                       int searchPathLength);

  void findCm(double originX,
              double originY,
              vtkDataArray* circlesArray,
              std::list<vtkIdType>::iterator& Cm,
              std::list<vtkIdType>& frontChain);

  void findCn(std::list<vtkIdType>::iterator Cm,
              std::list<vtkIdType>::iterator& Cn,
              std::list<vtkIdType>& frontChain);

  bool circlesIntersect(vtkIdType circleOne,
                        vtkIdType circleTwo,
                        vtkDataArray* circlesArray);

  void deleteSection(std::list<vtkIdType>::iterator circleToStartAt,
                     std::list<vtkIdType>::iterator circleToEndAt,
                     std::list<vtkIdType>& frontChain);

  void incrListIteratorWrapAround(std::list<vtkIdType>::iterator& i,
                                  std::list<vtkIdType>& frontChain);

  void decrListIteratorWrapAround(std::list<vtkIdType>::iterator& i,
                                  std::list<vtkIdType>& frontChain);

private:

};

vtkCirclePackFrontChainLayoutStrategyImplementation::vtkCirclePackFrontChainLayoutStrategyImplementation()
{
}

vtkCirclePackFrontChainLayoutStrategyImplementation::~vtkCirclePackFrontChainLayoutStrategyImplementation()
{
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::incrListIteratorWrapAround(std::list<vtkIdType>::iterator& i,
                                                                                     std::list<vtkIdType>& frontChain)
{
  if(i != frontChain.end())
    ++i;
  else if(i == frontChain.end())
    i = frontChain.begin();
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::decrListIteratorWrapAround(std::list<vtkIdType>::iterator& i,
                                                                                     std::list<vtkIdType>& frontChain)
{
  if(i == frontChain.begin())
    i = frontChain.end();
  else if(!frontChain.empty())
    --i;
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::createCirclePacking(vtkTree* tree,
                                                                              vtkDataArray* sizeArray,
                                                                              vtkDataArray* circlesArray,
                                                                              int height,
                                                                              int width)
{
  double enclosingCircleRadius = height/2.0;
  if(width < height)
    {
    enclosingCircleRadius = width/2.0;
    }

  this->packTreeNodes(tree->GetRoot(),
                      width/2.0,
                      height/2.0,
                      enclosingCircleRadius,
                      circlesArray,
                      sizeArray,
                      tree);

}

void vtkCirclePackFrontChainLayoutStrategyImplementation::packTreeNodes(vtkIdType treeNode,
                                                                        double originX,
                                                                        double originY,
                                                                        double enclosingCircleRadius,
                                                                        vtkDataArray* circlesArray,
                                                                        vtkDataArray* sizeArray,
                                                                        vtkTree* tree)
{

  if(tree->IsLeaf(treeNode))
    {
    return;
    }
  else
    {
    if(tree->GetRoot() == treeNode)
      {
      double circle[3];
      circle[0] = originX;
      circle[1] = originY;
      circle[2] = enclosingCircleRadius;
      circlesArray->SetTuple(treeNode,
                             circle);
      }
    std::vector<vtkIdType> childNodesPackList;
    for(int i=0;i<tree->GetNumberOfChildren(treeNode);i++)
      {
      childNodesPackList.push_back(tree->GetChild(treeNode,i));
      }
    this->packBrotherNodes(childNodesPackList,
                           originX,
                           originY,
                           enclosingCircleRadius,
                           circlesArray,
                           sizeArray,
                           tree);
    }
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::packBrotherNodes(std::vector<vtkIdType>& packedNodes,
                                                                           double originX,
                                                                           double originY,
                                                                           double enclosingCircleRadius,
                                                                           vtkDataArray* circlesArray,
                                                                           vtkDataArray* sizeArray,
                                                                           vtkTree* tree)
{

  if(!packedNodes.size())
    {
    return;
    }

  std::list<vtkIdType> frontChain;
  double circle[3]; // circle[0] is x,
                    // circle[1] is y,
                    // and circle[2] is the radius

  // Handle only one node
  if(packedNodes.size() == 1)
    {
    frontChain.push_back(packedNodes[0]);
    circle[0] = 0.0;
    circle[1] = 0.0;
    circle[2] = sizeArray->GetTuple1(packedNodes[0]);
    circlesArray->SetTuple(packedNodes[0],
                           circle);
    }
  // Handle two nodes, align circles along x-axis
  else if(packedNodes.size() == 2)
    {
    frontChain.push_back(packedNodes[0]);
    circle[0] = 0.0 - sizeArray->GetTuple1(packedNodes[0]);
    circle[1] = 0.0;
    circle[2] = sizeArray->GetTuple1(packedNodes[0]);
    circlesArray->SetTuple(packedNodes[0],
                           circle);

    frontChain.push_back(packedNodes[1]);
    circle[0] = 0.0 + sizeArray->GetTuple1(packedNodes[1]);
    circle[1] = 0.0;
    circle[2] = sizeArray->GetTuple1(packedNodes[1]);
    circlesArray->SetTuple(packedNodes[1],
                           circle);
    }
  else
    {
    // Base case, find initial front-chain for first three nodes
    double frad = sizeArray->GetTuple1(packedNodes[0]);
    double srad = sizeArray->GetTuple1(packedNodes[1]);
    double trad = sizeArray->GetTuple1(packedNodes[2]);

    // Initially align first two circles along x-axis
    frontChain.push_back(packedNodes[0]);
    circle[0] = 0.0 - frad;
    circle[1] = 0.0;
    circle[2] = frad;
    circlesArray->SetTuple(packedNodes[0],
                           circle);

    frontChain.push_back(packedNodes[1]);
    circle[0] = 0.0 + srad;
    circle[1] = 0.0;
    circle[2] = srad;
    circlesArray->SetTuple(packedNodes[1],
                           circle);

    circle[0] = 0.0;
    circle[1] = 0.0;
    circle[2] = trad;
    circlesArray->SetTuple(packedNodes[2],
                           circle);

    this->findCircleCenter(packedNodes[2],
                           packedNodes[0],
                           packedNodes[1],
                           circlesArray);
    frontChain.insert(--(frontChain.end()),packedNodes[2]);

    // Adjust the three circle centers so that they are centered around the origin point.
    // First, find the radius of the interior Soddy circle.  This is the circle that is
    // tangent to all three circles in the interior space defined by the circles.
    // Simple formula, just google for Soddy circle.  We take the positive solution, which is
    // the interior Soddy circle.
    double r1 = frad;
    double r2 = srad;
    double r3 = trad;
    double soddyRad = (r1*r2*r3)/((r2*r3) + (r1*r2) + (r1*r3) + 2.0*sqrt(r1*r2*r3*(r1 + r2 + r3)));
    // Use the Law of Cosines again to find the angle between the first circle center and the center
    // of the Soddy circle.
    double angle = acos((-pow(srad+soddyRad,2) + pow(frad+soddyRad,2) + pow(frad+srad,2))/(2.0*(frad+soddyRad)*(frad+srad)));
    // Find x and y adjustments
    double yAdjust = (frad+soddyRad)*sin(angle);
    double xAdjust = (frad+soddyRad)*cos(angle);
    double c0[3];
    double c1[3];
    double c2[3];
    circlesArray->GetTuple(packedNodes[0],
                           c0);
    circlesArray->GetTuple(packedNodes[1],
                           c1);
    circlesArray->GetTuple(packedNodes[2],
                           c2);
    c0[1] -= yAdjust;
    c1[1] -= yAdjust;
    c2[1] -= yAdjust;
    if(xAdjust > frad)
      {
      c0[0] -= (xAdjust-frad);
      c1[0] -= (xAdjust-frad);
      c2[0] -= (xAdjust-frad);
      }
    else
      {
      c0[0] += (frad - xAdjust);
      c1[0] += (frad - xAdjust);
      c2[0] += (frad - xAdjust);
      }
    circlesArray->SetTuple(packedNodes[0],
                           c0);
    circlesArray->SetTuple(packedNodes[1],
                           c1);
    circlesArray->SetTuple(packedNodes[2],
                           c2);


    // Iterate over the remaining brother nodes to find circle packing
    std::list<vtkIdType>::iterator Cm;
    std::list<vtkIdType>::iterator Cn;
    this->findCm(0.0,
                 0.0,
                 circlesArray,
                 Cm,
                 frontChain);
    this->findCn(Cm,
                 Cn,
                 frontChain);

    for(int i = 3;i < (int) packedNodes.size();i++)
      {
      circle[0] = 0.0;
      circle[1] = 0.0;
      circle[2] = sizeArray->GetTuple1(packedNodes[i]);
      circlesArray->SetTuple(packedNodes[i],
                             circle);

      std::list<vtkIdType>::iterator Cj;
      bool CjAfterCn;
      this->findIntersectingCircle(packedNodes[i],
                                   CjAfterCn,
                                   Cj,
                                   Cm,
                                   Cn,
                                   circlesArray,
                                   frontChain);

      while(Cj != frontChain.end())
        {
        // We have an intersection, so handle one of the two cases.
        // Cj is greater than Cn on the front chain
        if(CjAfterCn)
          {
          this->deleteSection(Cm,
                              Cj,
                              frontChain);
          Cn = Cj;
          }
        // Cj is less than Cm on the front chain
        else
          {
          this->deleteSection(Cj,
                              Cn,
                              frontChain);
          Cm = Cj;
          }

        this->findIntersectingCircle(packedNodes[i],
                                     CjAfterCn,
                                     Cj,
                                     Cm,
                                     Cn,
                                     circlesArray,
                                     frontChain);

        }

      // First case, there is no intersection so just insert Ci between Cm and Cn
      std::list<vtkIdType>::iterator ti = Cm;
      incrListIteratorWrapAround(ti, frontChain);
      frontChain.insert(ti,packedNodes[i]);
      this->findCn(Cm,
                   Cn,
                   frontChain);

      }

    }

  // Scale circle layout to fit within the enclosing circle radius
  double Xfc = 0.0;
  double Yfc = 0.0;
  std::list<vtkIdType>::iterator it;
  for(it = frontChain.begin();it != frontChain.end();++it)
    {
    circlesArray->GetTuple(*it,
                            circle);
    Xfc += circle[0];
    Yfc += circle[1];
    }

  Xfc = Xfc/((double) frontChain.size());
  Yfc = Yfc/((double) frontChain.size());

  double layoutRadius = 0;
  for(it = frontChain.begin();it != frontChain.end();++it)
    {
    circlesArray->GetTuple(*it,
                           circle);
    double distance = sqrt(pow(circle[0] - Xfc,2) +
                           pow(circle[1] - Yfc,2)) +
                           circle[2];
    if(distance > layoutRadius)
      {
      layoutRadius = distance;
      }
    }

  double scaleFactor = (layoutRadius == 0) ? 1.0 : (enclosingCircleRadius/layoutRadius);

  // Scale and translate each circle
  for(int i = 0;i < (int) packedNodes.size();i++)
    {
    circlesArray->GetTuple(packedNodes[i],
                           circle);
    circle[0] = (circle[0] - Xfc)*scaleFactor + originX;
    circle[1] = (circle[1] - Yfc)*scaleFactor + originY;
    circle[2] = circle[2]*scaleFactor;
    circlesArray->SetTuple(packedNodes[i],
                           circle);

    }

  // Now that each circle at this level is positioned and scaled,
  // find the layout for the children of each circle and add the
  // result to the packing list.
  for(int i = 0;i < (int) packedNodes.size();i++)
    {
    circlesArray->GetTuple(packedNodes[i], circle);
    this->packTreeNodes(packedNodes[i],
                        circle[0],
                        circle[1],
                        circle[2],
                        circlesArray,
                        sizeArray,
                        tree);
    }

}

bool vtkCirclePackFrontChainLayoutStrategyImplementation::validCjAfterCn(vtkIdType Ci,
                                                                         std::list<vtkIdType>::iterator Cm,
                                                                         std::list<vtkIdType>::iterator Cn,
                                                                         vtkDataArray* circlesArray,
                                                                         std::list<vtkIdType>& frontChain,
                                                                         int searchPathLength)
{
  this->findCircleCenter(Ci,
                         *Cm,
                         *Cn,
                         circlesArray);

  int CnSearchPathLength = 0;
  while(CnSearchPathLength < searchPathLength)
    {
    CnSearchPathLength++;
    decrListIteratorWrapAround(Cn, frontChain);
    if(Cn == frontChain.end())
      {
      decrListIteratorWrapAround(Cn, frontChain);
      }
    if(this->circlesIntersect(Ci,
                              *Cn,
                              circlesArray))
      {
      return false;
      }
    }

  return true;
}

bool vtkCirclePackFrontChainLayoutStrategyImplementation::validCjBeforeCm(vtkIdType Ci,
                                                                          std::list<vtkIdType>::iterator Cm,
                                                                          std::list<vtkIdType>::iterator Cn,
                                                                          vtkDataArray* circlesArray,
                                                                          std::list<vtkIdType>& frontChain,
                                                                          int searchPathLength)
{
  this->findCircleCenter(Ci,
                         *Cm,
                         *Cn,
                         circlesArray);

  int CmSearchPathLength = 0;
  while(CmSearchPathLength < searchPathLength)
    {
    CmSearchPathLength++;
    incrListIteratorWrapAround(Cm, frontChain);
    if(Cm == frontChain.end())
      {
      incrListIteratorWrapAround(Cm, frontChain);
      }
    if(this->circlesIntersect(Ci,
                              *Cm,
                              circlesArray))
      {
      return false;
      }
    }
  return true;
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::findIntersectingCircle(vtkIdType Ci,
                                                                                 bool& CjAfterCn,
                                                                                 std::list<vtkIdType>::iterator& Cj,
                                                                                 std::list<vtkIdType>::iterator Cm,
                                                                                 std::list<vtkIdType>::iterator Cn,
                                                                                 vtkDataArray* circlesArray,
                                                                                 std::list<vtkIdType>& frontChain)
{
  // Start from Cn and look for the first intersection Cj until we have searched half
  // of the frontChain.  Do the same for Cm.  Compare the Cm and Cn search lengths and
  // take the shortest, if any intersecting Cj was found.
  std::list<vtkIdType>::iterator CjfromCn = frontChain.end();
  std::list<vtkIdType>::iterator CjfromCm = frontChain.end();
  std::list<vtkIdType>::iterator lCm = Cm;
  std::list<vtkIdType>::iterator lCn = Cn;
  int fcl = (int) frontChain.size();
  int searchPathLength = (int) ceil(((double) fcl - 2.0)/2.0);

  this->findCircleCenter(Ci,
                         *Cm,
                         *Cn,
                         circlesArray);

  int CnSearchPathLength = 0;
  while(CnSearchPathLength < searchPathLength)
    {
    CnSearchPathLength++;
    incrListIteratorWrapAround(lCn, frontChain);
    if(lCn == frontChain.end())
      incrListIteratorWrapAround(lCn, frontChain);

    if(this->circlesIntersect(Ci,
                              *lCn,
                              circlesArray))
      {
      CjfromCn = lCn;
      break;
      }
    }

  // There is an intersection.  Check
  // to see if the front chain is clear
  // to be deleted from Cm to Cj.
  if(CjfromCn != frontChain.end())
    {
    Cj = CjfromCn;
    if(this->validCjAfterCn(Ci,
                            Cm,
                            lCn,
                            circlesArray,
                            frontChain,
                            CnSearchPathLength))
      {
      CjAfterCn = true;
      }
    else
      {
      CjAfterCn = false;
      }
    return;
    }

  int CmSearchPathLength = 0;
  while(CmSearchPathLength < searchPathLength)
   {
   CmSearchPathLength++;
   decrListIteratorWrapAround(lCm, frontChain);
   if(lCm == frontChain.end())
     decrListIteratorWrapAround(lCm, frontChain);

   if(this->circlesIntersect(Ci,
                             *lCm,
                             circlesArray))
     {
     CjfromCm = lCm;
     break;
     }
    }

  // There is an intersection.  Check
  // to see if the front chain is clear
  // to be deleted from Cj to Cn.
  if(CjfromCm != frontChain.end())
    {
    Cj = CjfromCm;
    if(this->validCjBeforeCm(Ci,
                             lCm,
                             Cn,
                             circlesArray,
                             frontChain,
                             CmSearchPathLength))
      {
      CjAfterCn = false;
      }
    else
      {
      CjAfterCn = true;
      }
    return;
    }

  // No intersection found
  Cj = frontChain.end();
  CjAfterCn = false;
  return;
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::findCircleCenter(vtkIdType Ci,
                                                                           vtkIdType Cm,
                                                                           vtkIdType Cn,
                                                                           vtkDataArray* circlesArray)
{

  double circle[3];
  circlesArray->GetTuple(Cm,circle);
  double rCm = circle[2];
  double xCm = circle[0];
  double yCm = circle[1];

  circlesArray->GetTuple(Cn,circle);
  double rCn = circle[2];
  double xCn = circle[0];
  double yCn = circle[1];

  circlesArray->GetTuple(Ci,circle);
  double rCi = circle[2];
  double xCi = circle[0];
  double yCi = circle[1];

  // Find the angle as measured from the x-axis to the line segment between rCm and rCn, with the origin
  // at rCm.
  double xc = xCn - xCm;
  double yc = yCn - yCm;
  double xAxisAngle = atan2(yc,xc);
  // Find positive rotation angle
  if (xAxisAngle < 0)
    {
    xAxisAngle = vtkMath::Pi() + (vtkMath::Pi() + xAxisAngle);
    }

  // Find the distance between the centers of Cm and Cn
  double CmCnDistance = sqrt(pow(xCn - xCm,2) + pow(yCn - yCm,2));

  // Find an interior angle of the triangle defined by all three circle centers using the Law of Cosines
  double angle = acos((-pow(rCn+rCi,2) + pow(rCm+rCi,2) + pow(CmCnDistance,2))/(2.0*(rCm+rCi)*(CmCnDistance)));

  // Find the center of the third triangle by using the interior angle and the length of the triangle side
  xCi = (rCm+rCi)*cos(angle);
  yCi = (rCm+rCi)*sin(angle);

  // Rotate the center of Ci from the x-axis by xAxisAngle
  double x = xCi;
  double y = yCi;
  xCi = x*cos(xAxisAngle) - y*sin(xAxisAngle);
  yCi = x*sin(xAxisAngle) + y*cos(xAxisAngle);
  xCi += xCm;
  yCi += yCm;

  circlesArray->GetTuple(Ci,circle);
  circle[0] = xCi;
  circle[1] = yCi;
  circlesArray->SetTuple(Ci,circle);

}

void vtkCirclePackFrontChainLayoutStrategyImplementation::findCm(double originX,
                                                                 double originY,
                                                                 vtkDataArray* circlesArray,
                                                                 std::list<vtkIdType>::iterator& Cm,
                                                                 std::list<vtkIdType>& frontChain)
{
  std::list<vtkIdType>::iterator it = frontChain.begin();
  Cm = it;
  double circle[3];
  double minDistance = 0.0;
  if(!frontChain.empty())
    {
    circlesArray->GetTuple(*it,circle);
    minDistance = pow(circle[0] - originX,2) + pow(circle[1] - originY,2);
    ++it;
    }

  for(; it != frontChain.end(); ++it)
    {
    circlesArray->GetTuple(*it,circle);
    double distanceSq = pow(circle[0] - originX,2) + pow(circle[1] - originY,2);

    if(distanceSq < minDistance)
      {
      Cm = it;
      minDistance = distanceSq;
      }
    }
}

void vtkCirclePackFrontChainLayoutStrategyImplementation::findCn(std::list<vtkIdType>::iterator Cm,
                                                                 std::list<vtkIdType>::iterator& Cn,
                                                                 std::list<vtkIdType>& frontChain)
{
  incrListIteratorWrapAround(Cm, frontChain);
  if(Cm == frontChain.end())
    {
    Cn = frontChain.begin();
    }
  else
    {
    Cn = Cm;
    }
}

bool vtkCirclePackFrontChainLayoutStrategyImplementation::circlesIntersect(vtkIdType circleOne,
                                                                           vtkIdType circleTwo,
                                                                           vtkDataArray* circlesArray)
{
  double c1[3];
  double c2[3];
  circlesArray->GetTuple(circleOne,c1);
  circlesArray->GetTuple(circleTwo,c2);

  double distanceSq = pow(c1[0]- c2[0],2) + pow(c1[1] - c2[1],2);

  if(distanceSq > pow(c1[2] + c2[2], 2))
    {
    return false;
    }
  else
    {
    return true;
    }
}

// Delete all circles out of fronChain from circleToStartAt to circleToEndAt, not including circleToStartAt and circleToEndAt.
// Insert all deleted elements into circlePacking list.
void vtkCirclePackFrontChainLayoutStrategyImplementation::deleteSection(std::list<vtkIdType>::iterator circleToStartAt,
                                                                        std::list<vtkIdType>::iterator circleToEndAt,
                                                                        std::list<vtkIdType>& frontChain)
{
  incrListIteratorWrapAround(circleToStartAt, frontChain);
  while ( (circleToStartAt != frontChain.end())&&(circleToStartAt != circleToEndAt) )
    {
    circleToStartAt = frontChain.erase(circleToStartAt);
    }

  if(circleToStartAt != circleToEndAt)
    {
    circleToStartAt = frontChain.begin();
    while ( (circleToStartAt != frontChain.end())&&(circleToStartAt != circleToEndAt) )
      {
      circleToStartAt = frontChain.erase(circleToStartAt);
      }
    }
}

vtkCirclePackFrontChainLayoutStrategy::vtkCirclePackFrontChainLayoutStrategy()
{
  this->Width = 1;
  this->Height = 1;
  this->pimpl = new vtkCirclePackFrontChainLayoutStrategyImplementation;
}

vtkCirclePackFrontChainLayoutStrategy::~vtkCirclePackFrontChainLayoutStrategy()
{
  delete this->pimpl;
  this->pimpl=0;
}

void vtkCirclePackFrontChainLayoutStrategy::Layout(vtkTree *inputTree,
                                                   vtkDataArray *areaArray,
                                                   vtkDataArray* sizeArray)
{

    this->pimpl->createCirclePacking(inputTree,
                                     sizeArray,
                                     areaArray,
                                     this->Height,
                                     this->Width);

}

void vtkCirclePackFrontChainLayoutStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Width: " << this->Width << endl;
  os << indent << "Height: " << this->Height << endl;
}