forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkLabelHierarchy.cxx
2551 lines (2367 loc) · 78.4 KB
/
vtkLabelHierarchy.cxx
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLabelHierarchy.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 "vtkLabelHierarchy.h"
#include "vtkCamera.h"
#include "vtkCellType.h"
#include "vtkCoordinate.h"
#include "vtkDataArray.h"
#include "vtkExtractSelectedFrustum.h"
#include "vtkIdTypeArray.h"
#include "vtkIdList.h"
#include "vtkIntArray.h"
#include "vtkLabelHierarchyIterator.h"
#include "vtkLabelHierarchyPrivate.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkCoincidentPoints.h"
#include "vtkPlanes.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPythagoreanQuadruples.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkTextProperty.h"
#include <octree/octree>
#include <deque>
#include <set>
#include <vector>
#include <map>
#include <cstdlib>
// WORKAROUND:
//
// This is the "comment near declaration of Current"
//
// Workaround for the lack of proper assignment of multiset-derived objects
// in the Borland 5.5 compiler's STL Implementation. This must be set prior
// to any possible calls to the PriorityComparator's default constructor so
// that it can have access to a vtkLabelHierarchy object in order to do proper
// comparisons. (Even though assignments are done after the default construction,
// with the Borland 5.5 multiset, the comparators for the multiset do not get
// set properly during those assignments.) So... we use this ha-ha-ha ckish
// workaround to get it to work.
//
// In practical terms, this means if a method in this file calls add_children,
// it should set vtkLabelHierarchy::Implementation::Current to a non-NULL
// vtkLabelHierarchy prior to calling add_children.
//
// Be warned: there is some global/static state here that may bite somebody
// in the future. But, for now, while we are still supporting Borland 5.5
// in the VTK code base, this is one way to do it. Feel free to change it
// if you have a better solution. But make sure it works on Borland 5.5...
//
vtkLabelHierarchy* vtkLabelHierarchy::Implementation::Current;
//----------------------------------------------------------------------------
// vtkLabelHierarchyFrustumIterator - an iterator with no-initial processing
//
// An iterator that has no initial processing, but looks for possible
// ocrtree nodes based on permutations of pythagorean triples.
class vtkLabelHierarchyFrustumIterator : public vtkLabelHierarchyIterator
{
public:
vtkTypeMacro(vtkLabelHierarchyFrustumIterator,vtkLabelHierarchyIterator);
static vtkLabelHierarchyFrustumIterator* New();
void Prepare( vtkLabelHierarchy* hier, vtkCamera* cam, double frustumPlanes[24] );
void Begin( vtkIdTypeArray* lastPlaced ) VTK_OVERRIDE;
virtual void BeginOctreeTraversal();
void Next() VTK_OVERRIDE;
bool IsAtEnd() VTK_OVERRIDE;
vtkIdType GetLabelId() VTK_OVERRIDE;
void GetNodeGeometry( double center[3], double& sz ) VTK_OVERRIDE;
protected:
vtkLabelHierarchyFrustumIterator();
~vtkLabelHierarchyFrustumIterator() VTK_OVERRIDE;
bool IsCursorInFrustum();
virtual void SetCamera( vtkCamera* camera );
vtkCoordinate* Projector;
double* Frustum;
vtkCamera* Camera;
int Level;
int NodeCount;
int HitCount;
int QuadrupleId;
int SignFlip;
int Permutation;
int Work;
int IjkG[3];
int Ijk0[3];
int IjkS[3];
int IjkP[3];
int Ijk[3];
vtkLabelHierarchy::Implementation::LabelSet::iterator LabelIterator;
vtkLabelHierarchy::Implementation::HierarchyCursor3 Cursor;
std::vector<int> Path;
int AtEnd;
vtkSmartPointer<vtkIdTypeArray> PreviousLabels;
vtkIdType PreviousLabelIter;
};
vtkStandardNewMacro(vtkLabelHierarchyFrustumIterator);
vtkCxxSetObjectMacro(vtkLabelHierarchyFrustumIterator, Camera, vtkCamera);
vtkLabelHierarchyFrustumIterator::vtkLabelHierarchyFrustumIterator()
{
this->Projector = vtkCoordinate::New();
this->Projector->SetCoordinateSystemToWorld();
this->Camera = 0;
this->Level = 0;
this->QuadrupleId = 0;
this->Permutation = 0;
this->Work = 0;
}
vtkLabelHierarchyFrustumIterator::~vtkLabelHierarchyFrustumIterator()
{
this->Projector->Delete();
if ( this->Camera )
this->Camera->Delete();
}
void vtkLabelHierarchyFrustumIterator::Prepare(
vtkLabelHierarchy* hier, vtkCamera* cam, double frustumPlanes[24] )
{
this->SetHierarchy( hier );
this->SetCamera( cam );
this->Frustum = frustumPlanes;
this->Level = -1;
this->SignFlip = 8;
this->Permutation = 6;
this->QuadrupleId = vtkMaxPythagoreanQuadrupleId;
this->Work = 0;
}
void vtkLabelHierarchyFrustumIterator::Begin( vtkIdTypeArray* lastPlaced )
{
// First, we'll iterate over labels we placed last frame.
this->PreviousLabels = lastPlaced;
this->PreviousLabelIter = 0;
this->AtEnd = -1;
if ( ! this->PreviousLabels->GetNumberOfTuples() )
{
// No previously placed labels? Look in octree:
this->BeginOctreeTraversal();
}
}
void vtkLabelHierarchyFrustumIterator::BeginOctreeTraversal()
{
// Inject new labels from the hierarchy, starting with
// the highest priority labels nearest the camera
this->AtEnd = 0;
this->Cursor = vtkLabelHierarchy::Implementation::HierarchyCursor3(
this->Hierarchy->Impl->Hierarchy3 );
this->LabelIterator = this->Cursor->value().end(); // Force the label iterator test in Next() to fail.
this->Level = -1; // when we increment the level we'll be at the beginning
this->SignFlip = 8; // force the sign flip to get bypassed
this->Permutation = 6; // force the permutation to get bypassed
this->QuadrupleId = vtkMaxPythagoreanQuadrupleId; // when we increment the index into the quadruples, we'll be at the beginning
this->Work = 0;
this->NodeCount = 0;
this->HitCount = 0;
this->Next();
}
void vtkLabelHierarchyFrustumIterator::Next()
{
if ( this->AtEnd < 0 )
{
vtkDebugMacro( "In strange next. Have previous labels" );
++ this->PreviousLabelIter;
if ( this->PreviousLabelIter < this->PreviousLabels->GetNumberOfTuples() )
{
return;
}
this->BeginOctreeTraversal(); // sets this->AtEnd = 0;
}
else if ( this->AtEnd == 0 )
{
// Invalid LabelIterator occurs only when called from BeginOctreeTraversal():
if ( this->LabelIterator != this->Cursor->value().end() )
{
++ this->LabelIterator;
if ( this->LabelIterator != this->Cursor->value().end() )
{ // Still have anchors left at this node...
return;
}
}
vtkDebugMacro( "In next. Level: " << this->Level << " SgnFlp: " << this->SignFlip << " Perm: " << this->Permutation << " QuadId: " << this->QuadrupleId );
// Either starting traversal or out of anchors at current node.
// Find next valid node at this level or skip to next.
bool gotNode = false;
int lvlMax = 1 << this->Level;
double sz = this->Hierarchy->Impl->Hierarchy3->root()->value().GetSize() / 2.;
double eye[3];
//double vaMax = atan( vtkMath::Pi()/2. - 0.2 * vtkMath::RadiansFromDegrees(t his->Camera->GetViewAngle() ) );
double vaMin = atan( vtkMath::Pi()/2. - 2.0 * vtkMath::RadiansFromDegrees( this->Camera->GetViewAngle() ) );
this->Camera->GetPosition( eye );
while ( ! gotNode )
{
++ this->Work;
// 1. Is there a sign flip of the current quadruple we can do?
if ( this->SignFlip < 8 )
{
bool flippable;
do
{
flippable = true;
++ this->SignFlip;
int flipCoord;
for ( int i = 0; i < 3; ++ i )
{
flipCoord = this->SignFlip & (1 << i);
if ( ( this->IjkP[i] == 0 ) && flipCoord )
{
flippable = false;
break; // this->SignFlip won't work... try the next one.
}
else
{
this->IjkS[i] = flipCoord ? - this->IjkP[i] : this->IjkP[i];
}
}
}
while ( ! flippable && this->SignFlip < 8 );
gotNode = ( flippable && this->SignFlip < 8 ); // skip down and see if the node exists
}
// 2. Is there a permutation of the current quadruple we can do?
if ( ! gotNode && this->Permutation < 6 )
{
bool goodPerm = false;
while ( ! goodPerm && ( ++ this->Permutation < 6 ) )
{
switch ( this->Permutation )
{
case 0: // ijk
this->IjkP[0] = this->Ijk[0]; this->IjkP[1] = this->Ijk[1]; this->IjkP[2] = this->Ijk[2];
goodPerm = true; // no perm is always a good perm. (This means you Mr. Brady!)
break;
case 1: // ikj (swap j,k) but not if j == k
if ( this->Ijk[1] == this->Ijk[2] )
{
goodPerm = false;
}
else
{
this->IjkP[0] = this->Ijk[0]; this->IjkP[1] = this->Ijk[2]; this->IjkP[2] = this->Ijk[1];
goodPerm = true;
}
break;
case 2: // jki (rotate ijk to the left once but not if right neighbors are repeats)
if ( this->Ijk[0] == this->Ijk[1] && this->Ijk[1] == this->Ijk[2] )
{
goodPerm = false;
}
else
{
this->IjkP[0] = this->Ijk[1]; this->IjkP[1] = this->Ijk[2]; this->IjkP[2] = this->Ijk[0];
goodPerm = true;
}
break;
case 3: // jik (swap i,j) but not if i == j
if ( this->Ijk[0] == this->Ijk[1] )
{
goodPerm = false;
}
else
{
this->IjkP[0] = this->Ijk[1]; this->IjkP[1] = this->Ijk[0]; this->IjkP[2] = this->Ijk[2];
goodPerm = true;
}
break;
case 4: // kij (rotate ijk to the right once but not if left neighbors are repeats)
if ( this->Ijk[0] == this->Ijk[1] && this->Ijk[1] == this->Ijk[2] )
{
goodPerm = false;
}
else
{
this->IjkP[0] = this->Ijk[2]; this->IjkP[1] = this->Ijk[0]; this->IjkP[2] = this->Ijk[1];
goodPerm = true;
}
break;
case 5: // kji (swap i,k) but not if i == k
if ( this->Ijk[0] == this->Ijk[2] )
{
goodPerm = false;
}
else
{
this->IjkP[0] = this->Ijk[2]; this->IjkP[1] = this->Ijk[1]; this->IjkP[2] = this->Ijk[0];
goodPerm = true;
}
break;
}
}
if ( goodPerm )
{
this->SignFlip = -1;
continue; // jump above and set this->IjkS
}
}
// 3. Are there more pseudo-Pythagorean quadruples to try?
if ( ! gotNode && ( this->QuadrupleId < 0 || vtkPythagoreanQuadruples[this->QuadrupleId * 4] >= 0 ) )
{
int R2 = vtkPythagoreanQuadruples[(++ this->QuadrupleId) * 4];
double R = VTK_DOUBLE_MAX;
if ( R2 >= 0 )
{
// Check that r/R is in [tan(theta_h/5),tan(min(2*theta_h,pi/2))[
// First: Will these nodes be too close to the camera?
bool tooClose = true; // a large octree node too close to the camera should be ignored.
while ( tooClose )
{
R = sqrt(static_cast<double>(R2));
if ( R >= sz / lvlMax * vaMin * 0 )
{
tooClose = false;
}
else
{
R2 = vtkPythagoreanQuadruples[(++ this->QuadrupleId) * 4];
if ( R2 < 0 )
{
vtkDebugMacro( "Panic: too far from camera for cached tuples!" );
tooClose = false; // exit the loop...
}
}
}
// Second: See if we're too far from the camera
//if ( R2 >= 0 && ( R < sz / lvlMax * vaMax * 2 ) ) // uncomment this for speed and coherence at the cost of completeness
if ( R2 >= 0 ) // uncomment this for completeness at the cost of speed and some popping
{
// If we're in the habitable zone, set this->Ijk and reset SignFlip and Permutation...
for ( int i = 0; i < 3; ++ i )
this->Ijk[i] = vtkPythagoreanQuadruples[this->QuadrupleId * 4 + i + 1];
this->SignFlip = 8;
this->Permutation = -1;
continue; // jump above and set this->IjkP
}
else
{
// Force the radius to be -1 and continue on to step 4.
this->QuadrupleId = vtkMaxPythagoreanQuadrupleId;
}
}
}
// 4. Can we descend a level in the hierarchy?
if ( ! gotNode )
{
if ( ++ this->Level < static_cast<int>( this->Hierarchy->Impl->ActualDepth ) )
{
// Figure out the new "center"
lvlMax = 1 << this->Level;
this->Hierarchy->GetDiscreteNodeCoordinatesFromWorldPoint( this->Ijk0, eye, this->Level );
if ( this->Level == 1 )
{
vtkDebugMacro( "i: " << this->Ijk0[0] << " j: " << this->Ijk0[1] << " k: " << this->Ijk0[2] << " l: " << this->Level );
}
this->QuadrupleId = -1;
this->SignFlip = 8;
this->Permutation = 6;
continue; // find the first quadruple in the "habitable zone"
}
}
if ( gotNode )
{
int R2 = 0;
for ( int i = 0; i < 3; ++ i )
{
this->IjkG[i] = this->Ijk0[i] + this->IjkS[i];
R2 += this->IjkS[i] * this->IjkS[i];
if ( this->IjkG[i] < 0 || this->IjkG[i] >= lvlMax )
{ // out of bounds
gotNode = false;
}
}
if ( this->Debug )
{
if (! this->Level )
{
if ( ! this->IjkG[0] && ! this->IjkG[1] && ! this->IjkG[2] )
{
vtkDebugMacro( "Camera: i: " << this->Ijk0[0] << " j: " << this->Ijk0[1] << " k: " << this->Ijk0[2] );
vtkDebugMacro( "SgnPrm: i: " << this->IjkS[0] << " j: " << this->IjkS[1] << " k: " << this->IjkS[2] );
}
}
}
if ( gotNode )
{
this->NodeCount++;
// OK, we have nodal coordinates... see if the node exists in the hierarchy
// First, translate nodal coordinates into a "path" down the tree
if ( this->Level )
{
this->Path.resize( this->Level );
this->Hierarchy->GetPathForNodalCoordinates( &this->Path[0], this->IjkG, this->Level );
}
else
{
this->Path.clear();
}
// Now see if we can visit it
if ( this->Cursor.visit( this->Path ) )
{
if (this->Debug)
{
vtkDebugMacro( "l: " << this->Level << " i: " << this->IjkG[0] << " j: " << this->IjkG[1] << " k: " << this->IjkG[2] << " (");
for ( std::vector<int>::iterator cit = this->Cursor._M_indices.begin(); cit != this->Cursor._M_indices.end(); ++ cit )
{
vtkDebugMacro( " " << *cit );
}
vtkDebugMacro( ", " << this->Cursor->value().GetLocalAnchorCount() << ")" );
}
this->BoxNode();
if ( this->Cursor->value().GetLocalAnchorCount() )
{
this->HitCount++;
this->LabelIterator = this->Cursor->value().begin();
vtkDebugMacro( " *Level: " << this->Level << " SgnFlp: " << this->SignFlip << " Perm: " << this->Permutation << " QuadId: " << this->QuadrupleId );
return;
}
}
}
gotNode = false; // no node at this place in hierarchy... move to next.
}
else
{
// At end of iteration
vtkDebugMacro( "I did all I could!" );
vtkDebugMacro( "Nodes attempted: " << this->NodeCount );
vtkDebugMacro( "Hits: " << this->HitCount );
this->AtEnd = 1;
return;
}
}
}
}
bool vtkLabelHierarchyFrustumIterator::IsAtEnd()
{
return this->AtEnd > 0;
}
vtkIdType vtkLabelHierarchyFrustumIterator::GetLabelId()
{
if ( this->AtEnd < 0 )
{
return this->PreviousLabels->GetValue( this->PreviousLabelIter );
}
return *this->LabelIterator;
}
void vtkLabelHierarchyFrustumIterator::GetNodeGeometry( double center[3], double& sz )
{
const double* x = this->Cursor->value().GetCenter();
for ( int i = 0; i < 3; ++ i )
{
center[i] = x[i];
}
sz = this->Cursor->value().GetSize();
}
bool vtkLabelHierarchyFrustumIterator::IsCursorInFrustum()
{
return true;
}
//----------------------------------------------------------------------------
// vtkLabelHierarchyFullSortIterator - a simple up-front-sorting iterator
//
// An iterator that first sorts the octree nodes based on level and
// distance to the camera.
class vtkLabelHierarchyFullSortIterator : public vtkLabelHierarchyIterator
{
public:
vtkTypeMacro(vtkLabelHierarchyFullSortIterator,vtkLabelHierarchyIterator);
static vtkLabelHierarchyFullSortIterator* New();
void Prepare( vtkLabelHierarchy* hier, vtkCamera* cam,
double frustumPlanes[24], bool positionsAsNormals );
void Begin( vtkIdTypeArray* lastPlaced ) VTK_OVERRIDE;
void Next() VTK_OVERRIDE;
bool IsAtEnd() VTK_OVERRIDE;
vtkIdType GetLabelId() VTK_OVERRIDE;
void GetNodeGeometry( double center[3], double& sz ) VTK_OVERRIDE;
// Give internal class access to this protected type.
typedef vtkLabelHierarchy::Implementation::HierarchyType3::octree_node_pointer NodePointer;
struct vtkHierarchyNode
{
int Level;
double DistanceToCamera;
NodePointer Node;
bool TotallyInside;
};
class vtkHierarchyNodeSorter
{
public:
bool operator()(const vtkHierarchyNode & a,
const vtkHierarchyNode & b) const
{
if (a.Level != b.Level)
{
return a.Level < b.Level;
}
return a.DistanceToCamera < b.DistanceToCamera;
}
};
protected:
vtkLabelHierarchyFullSortIterator();
~vtkLabelHierarchyFullSortIterator() VTK_OVERRIDE;
std::set<vtkHierarchyNode, vtkHierarchyNodeSorter> NodeSet;
std::set<vtkHierarchyNode, vtkHierarchyNodeSorter>::iterator NodeIterator;
virtual void SetCamera(vtkCamera* camera);
vtkCamera* Camera;
vtkExtractSelectedFrustum* FrustumExtractor;
bool PositionsAsNormals;
vtkLabelHierarchy::Implementation::LabelSet::iterator LabelIterator;
bool AtStart;
bool AtEnd;
int NodesTraversed;
};
vtkStandardNewMacro(vtkLabelHierarchyFullSortIterator);
vtkCxxSetObjectMacro(vtkLabelHierarchyFullSortIterator, Camera, vtkCamera);
void vtkLabelHierarchyFullSortIterator::Prepare( vtkLabelHierarchy* hier, vtkCamera* cam,
double frustumPlanes[24], bool positionsAsNormals )
{
this->SetHierarchy( hier );
this->SetCamera( cam );
vtkSmartPointer<vtkPlanes> frustum = vtkSmartPointer<vtkPlanes>::New();
frustum->SetFrustumPlanes( frustumPlanes );
this->FrustumExtractor->SetFrustum( frustum );
this->PositionsAsNormals = positionsAsNormals;
}
void vtkLabelHierarchyFullSortIterator::Begin( vtkIdTypeArray* vtkNotUsed(lastPlaced) )
{
double cameraPos[3];
this->Camera->GetPosition(cameraPos);
int maxLevel = 1;
std::deque<vtkHierarchyNode> s;
vtkHierarchyNode root;
root.Level = 0;
root.Node = this->Hierarchy->Impl->Hierarchy3->root();
root.DistanceToCamera = vtkMath::Distance2BetweenPoints(cameraPos, root.Node->value().GetCenter());
root.TotallyInside = false;
s.push_back(root);
int numNodes = 0;
int numLeaf = 0;
int totalLeafDepth = 0;
size_t numLabels = 0;
int maxLabels = 10000;
while (!s.empty())
{
vtkHierarchyNode node = s.front();
s.pop_front();
this->NodeSet.insert(node);
numLabels += node.Node->value().GetLocalAnchorCount();
if ( numLabels > static_cast<size_t>(maxLabels) )
{
break;
}
int level = node.Level;
++numNodes;
if ( node.Node->num_children() > 0 )
{
++level;
if ( level > maxLevel )
{
maxLevel = level;
}
for ( int c = 0; c < 8; ++c )
{
vtkHierarchyNode child;
child.Node = &( *node.Node )[c];
child.Level = level;
child.DistanceToCamera = vtkMath::Distance2BetweenPoints(cameraPos, child.Node->value().GetCenter());
if ( !node.TotallyInside )
{
// First check if the box is on the other side of the world.
// This is for the 3D world view only.
if ( this->PositionsAsNormals && vtkMath::Dot( cameraPos, child.Node->value().GetCenter() ) < 0.0 )
{
continue;
}
// Determine if box is offscreen. If so, skip node and children.
double nodeSize = node.Node->value().GetSize() / 2.0;
double bbox[6] = {
child.Node->value().GetCenter()[0] - nodeSize,
child.Node->value().GetCenter()[0] + nodeSize,
child.Node->value().GetCenter()[1] - nodeSize,
child.Node->value().GetCenter()[1] + nodeSize,
child.Node->value().GetCenter()[2] - nodeSize,
child.Node->value().GetCenter()[2] + nodeSize};
int ret = this->FrustumExtractor->OverallBoundsTest(bbox);
child.TotallyInside = false;
if ( ret == 0 )
{
// Totally outside, no need to visit this node.
continue;
}
else if ( ret == 2 )
{
// Totally inside, no need to check children.
child.TotallyInside = true;
}
}
else
{
child.TotallyInside = true;
}
s.push_back(child);
}
}
else
{
++numLeaf;
totalLeafDepth += level;
}
}
vtkDebugMacro( "max level is " << maxLevel );
vtkDebugMacro( "num nodes " << numNodes );
vtkDebugMacro( "avg leaf depth " << static_cast<double>(totalLeafDepth) / numLeaf );
this->NodesTraversed = 0;
this->NodeIterator = this->NodeSet.begin();
this->AtStart = true;
this->AtEnd = false;
this->Next();
}
void vtkLabelHierarchyFullSortIterator::Next()
{
if ( !this->AtStart && this->LabelIterator != this->NodeIterator->Node->value().end() )
{
++ this->LabelIterator;
if ( this->LabelIterator != this->NodeIterator->Node->value().end() )
{
vtkDebugMacro( "Still have anchors at the node" );
return;
}
}
// Move to next octree node
if ( !this->AtStart )
{
++ this->NodeIterator;
}
else
{
this->AtStart = false;
}
while ( this->NodeIterator != this->NodeSet.end() )
{
this->BoxNode();
if ( this->NodeIterator->Node->value().GetLocalAnchorCount() > 0 )
{
this->LabelIterator = this->NodeIterator->Node->value().begin();
++ this->NodesTraversed;
vtkDebugMacro( "At the beginning of a new node" );
return;
}
++ this->NodeIterator;
}
// Done
vtkDebugMacro( << this->NodesTraversed << " nodes traversed." );
this->AtEnd = true;
}
bool vtkLabelHierarchyFullSortIterator::IsAtEnd()
{
return this->AtEnd;
}
vtkIdType vtkLabelHierarchyFullSortIterator::GetLabelId()
{
if (!(this->IsAtEnd()))
{
return *this->LabelIterator;
}
else
return 0;
}
void vtkLabelHierarchyFullSortIterator::GetNodeGeometry( double center[3], double& sz )
{
const double* x = this->NodeIterator->Node->value().GetCenter();
for ( int i = 0; i < 3; ++ i )
{
center[i] = x[i];
}
sz = this->NodeIterator->Node->value().GetSize() / 2.;
}
vtkLabelHierarchyFullSortIterator::vtkLabelHierarchyFullSortIterator()
{
this->Camera = 0;
this->FrustumExtractor = vtkExtractSelectedFrustum::New();
}
vtkLabelHierarchyFullSortIterator::~vtkLabelHierarchyFullSortIterator()
{
if ( this->Camera )
{
this->Camera->Delete();
}
if ( this->FrustumExtractor )
{
this->FrustumExtractor->Delete();
}
}
//----------------------------------------------------------------------------
// vtkLabelHierarchyQuadtreeIterator - a simple breadth-first iterator
//
// This iterator maintains a queue of nodes to be visited. When a node is
// popped off the front, any of its children that are in the view frustum
// are sorted by distance to the camera and then pushed onto the back.
// This forces the iterator to perform a breadth-first traversal of nodes
// that are roughly ordered by their distance to the camera.
// Unlike the FULL_SORT iterator, it does not traverse and sort all the nodes
// up front; instead nodes are added as their parents are removed.
//
// The total number of nodes to be processed is limited by the
// MAXIMUM_NODES_TRAVERSED constant.
// The number of nodes processed is roughly proportional to the amount of work
// required to place labels, so this is a good way to maintain interactive
// framerates.
// In the future, it might be useful to weight the number of nodes
// queued by the number of label anchors stored at the node.
class vtkLabelHierarchyQuadtreeIterator : public vtkLabelHierarchyIterator
{
public:
vtkTypeMacro(vtkLabelHierarchyQuadtreeIterator,vtkLabelHierarchyIterator);
static vtkLabelHierarchyQuadtreeIterator* New();
typedef vtkLabelHierarchy::Implementation::HierarchyType2::octree_node_pointer NodePointer;
void Prepare( vtkLabelHierarchy* hier, vtkCamera* cam, double frustumPlanes[24], vtkRenderer* ren, float bucketSize[2] );
void Begin( vtkIdTypeArray* lastPlaced ) VTK_OVERRIDE;
void Next() VTK_OVERRIDE;
bool IsAtEnd() VTK_OVERRIDE;
vtkIdType GetLabelId() VTK_OVERRIDE;
void GetNodeGeometry( double center[3], double& sz ) VTK_OVERRIDE;
bool IsNodeInFrustum( NodePointer node );
vtkGetObjectMacro(Camera,vtkCamera);
vtkGetObjectMacro(Renderer,vtkRenderer);
enum Constants
{
MAXIMUM_NODES_QUEUED = 128 // See notes at QueueChildren() before changing.
};
protected:
vtkLabelHierarchyQuadtreeIterator();
~vtkLabelHierarchyQuadtreeIterator() VTK_OVERRIDE;
virtual void SetCamera( vtkCamera* camera );
virtual void SetRenderer( vtkRenderer* renderer );
void QueueChildren();
vtkCamera* Camera;
vtkRenderer* Renderer;
vtkExtractSelectedFrustum* FrustumExtractor;
vtkLabelHierarchy::Implementation::LabelSet::iterator LabelIterator;
NodePointer Node;
std::deque<NodePointer> Queue; // Queue of nodes to be traversed.
float BucketSize[2]; // size of label placer buckets in pixels
double SizeLimit; // square of smallest allowable distance-normalized octree node size.
bool AtEnd;
int NodesQueued;
};
vtkStandardNewMacro(vtkLabelHierarchyQuadtreeIterator);
vtkCxxSetObjectMacro(vtkLabelHierarchyQuadtreeIterator,Camera,vtkCamera);
vtkCxxSetObjectMacro(vtkLabelHierarchyQuadtreeIterator,Renderer,vtkRenderer);
vtkLabelHierarchyQuadtreeIterator::vtkLabelHierarchyQuadtreeIterator()
{
this->AtEnd = true;
this->Camera = 0;
this->Renderer = 0;
this->FrustumExtractor = vtkExtractSelectedFrustum::New();
this->SizeLimit = 0.;
this->NodesQueued = 0;
}
vtkLabelHierarchyQuadtreeIterator::~vtkLabelHierarchyQuadtreeIterator()
{
this->FrustumExtractor->Delete();
if ( this->Camera )
{
this->Camera->Delete();
}
if ( this->Renderer )
{
this->Renderer->Delete();
}
}
void vtkLabelHierarchyQuadtreeIterator::Prepare(
vtkLabelHierarchy* hier,
vtkCamera* cam,
double frustumPlanes[24],
vtkRenderer* ren,
float bucketSize[2] )
{
this->NodesQueued = 0;
this->SetHierarchy( hier );
this->SetCamera( cam );
vtkSmartPointer<vtkPlanes> frustum = vtkSmartPointer<vtkPlanes>::New();
frustum->SetFrustumPlanes( frustumPlanes );
this->FrustumExtractor->SetFrustum( frustum );
for ( int i = 0; i < 2; ++ i )
{
this->BucketSize[i] = bucketSize[i];
}
this->SetRenderer( ren );
#if 0
if ( cam->GetParallelProjection() )
{ // Compute threshold for quadtree nodes too small to visit using parallel projection
//cout << "SizeLimit ParallelProj ps: " << cam->GetParallelScale() << "\n";
//this->SizeLimit = 0.0001 * cam->GetParallelScale(); // FIXME: Should be set using cam->ParallelScale and pixel size
}
else
{ // Compute threshold for quadtree nodes too small to visit using perspective projection
//double va = vtkMath::RadiansFromDegrees( cam->GetViewAngle() );
//double tva = 2. * tan( va / 2. );
double vsr;
if ( cam->GetUseHorizontalViewAngle() )
{
double vs = ren->GetSize()[0];
vsr = this->BucketSize[0] ? ( vs / this->BucketSize[0] ) : VTK_DOUBLE_MAX;
}
else
{
double vs = ren->GetSize()[1];
vsr = this->BucketSize[1] ? ( vs / this->BucketSize[1] ) : VTK_DOUBLE_MAX;
}
//double fac = vsr ? ( 0.1 * tva / vsr ) : 0.;
//cout << "SizeLimit va: " << va << " tva: " << tva << " vsr: " << vsr << " fac: " << fac << " slim: " << fac * fac << "\n";
//this->SizeLimit = fac * fac;
}
#endif
}
void vtkLabelHierarchyQuadtreeIterator::Begin( vtkIdTypeArray* vtkNotUsed(lastPlaced) )
{
if ( this->Hierarchy->GetImplementation()->Hierarchy2 )
{
this->Node = this->Hierarchy->GetImplementation()->Hierarchy2->root();
if ( this->IsNodeInFrustum( this->Node ) )
{
this->QueueChildren();
this->BoxNode();
++ this->NodesQueued;
this->AtEnd = false;
this->LabelIterator = this->Node->value().begin();
if ( this->LabelIterator == this->Node->value().end() )
{
this->Next();
}
}
else
{
this->AtEnd = true;
}
}
else
{
this->AtEnd = true;
}
}
struct vtkQuadtreeNodeDistCompare
{
double Eye[3];
void SetEye( const double* eye )
{
for ( int i = 0; i < 3; ++ i )
{
this->Eye[i] = eye[i];
}
}
bool operator () (
const vtkLabelHierarchyQuadtreeIterator::NodePointer& a,
const vtkLabelHierarchyQuadtreeIterator::NodePointer& b ) const
{
const double* xa = a->value().GetCenter();
const double* xb = b->value().GetCenter();
double da = 0., db = 0.;
for ( int i = 0; i < 3; ++ i )
{
double va, vb;
va = Eye[i] - xa[i];
vb = Eye[i] - xb[i];
da += va * va;
db += vb * vb;
}
return ( da < db ? true : ( da == db ? ( a < b ? true : false ) : false ) );
}
};
typedef std::set<vtkLabelHierarchyQuadtreeIterator::NodePointer,vtkQuadtreeNodeDistCompare> vtkQuadtreeOrderedChildren;
void vtkLabelHierarchyQuadtreeIterator::Next()
{
//const int maxNumChildren = ( 1 << 2 );
++ this->LabelIterator;
if ( this->LabelIterator == this->Node->value().end() )
{
this->BoxNode();
while ( this->Queue.size() )
{
this->Node = this->Queue.front();
this->Queue.pop_front();
this->QueueChildren();
this->LabelIterator = this->Node->value().begin();
if ( this->LabelIterator != this->Node->value().end() )
{
// We have some labels, stop looking for more nodes
return;
}
}
// We must be done traversing the tree.
this->AtEnd = true;
}
else
{
/* *
cout << "Label: " << *this->LabelIterator << "\n";
* */
}
}
bool vtkLabelHierarchyQuadtreeIterator::IsAtEnd()
{
return this->AtEnd;
}
vtkIdType vtkLabelHierarchyQuadtreeIterator::GetLabelId()
{
if (!(this->IsAtEnd()))
{
return *this->LabelIterator;
}
else
return 0;
}
void vtkLabelHierarchyQuadtreeIterator::GetNodeGeometry( double center[3], double& sz )
{
const double* x = this->Node->value().GetCenter();
for ( int i = 0; i < 2; ++ i )
center[i] = x[i];
center[2] = this->Hierarchy->GetImplementation()->Z2;
sz = this->Node->value().GetSize() / 2.;
}
bool vtkLabelHierarchyQuadtreeIterator::IsNodeInFrustum( NodePointer node )
{
double nodeSize = node->value().GetSize() / 2.;