forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkLabeledTreeMapDataMapper.cxx
1012 lines (923 loc) · 29.5 KB
/
vtkLabeledTreeMapDataMapper.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: vtkLabeledTreeMapDataMapper.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 "vtkActor2D.h"
#include "vtkCoordinate.h"
#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkExecutive.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkLabeledTreeMapDataMapper.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkStringArray.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkTree.h"
#include "vtkTreeDFSIterator.h"
#include "vtkViewport.h"
#include "vtkWindow.h"
vtkStandardNewMacro(vtkLabeledTreeMapDataMapper);
vtkLabeledTreeMapDataMapper::vtkLabeledTreeMapDataMapper()
: CurrentViewPort(0), FontHeights(0), FontWidths(0), MaxFontLevel(0),
MaxTreeLevels(100), ClipTextMode(0), ChildMotion(0),
StartLevel(0), EndLevel(-1), DynamicLevel(0)
{
this->BoxTrans[0][0] = this->BoxTrans[1][0] = 0.0;
this->BoxTrans[0][1] = this->BoxTrans[1][1] = 1.0;
this->WindowLimits[0][0] = this->WindowLimits[1][0] = 0.0;
this->WindowLimits[0][1] = this->WindowLimits[1][1] = 1.0;
this->VCoord = vtkCoordinate::New();
this->VertexList = vtkIdList::New();
this->VertexList->SetNumberOfIds(this->NumberOfLabelsAllocated);
this->TextPoints = vtkPoints::New();
this->TextPoints->Allocate(this->NumberOfLabelsAllocated);
this->VerticalLabelProperty = vtkTextProperty::New();
this->VerticalLabelProperty->SetFontSize(12);
this->VerticalLabelProperty->SetBold(1);
this->VerticalLabelProperty->SetItalic(1);
this->VerticalLabelProperty->SetShadow(1);
this->VerticalLabelProperty->SetFontFamilyToArial();
this->VerticalLabelProperty->SetJustificationToCentered();
this->GetLabelTextProperty()->SetJustificationToCentered();
this->VerticalLabelProperty->SetVerticalJustificationToCentered();
this->GetLabelTextProperty()->SetVerticalJustificationToCentered();
this->VerticalLabelProperty->SetOrientation(90.0);
this->VerticalLabelProperty->SetColor(1, 1, 1);
this->GetLabelTextProperty()->SetColor(1, 1, 1);
this->GetLabelTextProperty()->SetFontSize(12);
this->SetFontSizeRange(24, 10);
this->ChildrenCount = new int[this->MaxTreeLevels+1];
this->LabelMasks = new float[this->MaxTreeLevels+1][4];
this->SetRectanglesArrayName("area");
this->SetLabelFormat("%s");
// Take control of the TextMappers array.
// The superclass just created new TextMapper instances
// up to the currently allocated amount (default 50).
// Instead, we will store NULL values until we need them.
// This class will manage the maintenance and deletion of
// this array.
for (int i = 0; i < this->NumberOfLabelsAllocated; i++)
{
this->TextMappers[i]->Delete();
this->TextMappers[i] = NULL;
}
}
vtkLabeledTreeMapDataMapper::~vtkLabeledTreeMapDataMapper()
{
this->VCoord->Delete();
this->TextPoints->Delete();
this->VertexList->Delete();
this->VerticalLabelProperty->Delete();
int i;
for (i = 0; i <= this->MaxFontLevel; i++)
{
delete [] this->FontWidths[i];
this->HLabelProperties[i]->Delete();
}
delete [] this->FontWidths;
delete [] this->FontHeights;
delete [] this->HLabelProperties;
delete [] this->ChildrenCount;
delete [] this->LabelMasks;
if (this->TextMappers)
{
for (i=0; i < this->NumberOfLabelsAllocated; i++)
{
if (this->TextMappers[i])
{
this->TextMappers[i]->Delete();
}
}
delete [] this->TextMappers;
this->TextMappers = NULL;
}
}
//----------------------------------------------------------------------------
void vtkLabeledTreeMapDataMapper::SetRectanglesArrayName(const char* name)
{
this->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_VERTICES, name);
}
//----------------------------------------------------------------------------
// Release any graphics resources that are being consumed by this mapper.
void vtkLabeledTreeMapDataMapper::ReleaseGraphicsResources(vtkWindow *win)
{
if (this->TextMappers != NULL )
{
for (int i=0; i < this->NumberOfLabelsAllocated; i++)
{
if (this->TextMappers[i])
{
this->TextMappers[i]->ReleaseGraphicsResources(win);
}
}
}
}
vtkTree *vtkLabeledTreeMapDataMapper::GetInputTree()
{
return vtkTree::SafeDownCast(this->GetExecutive()->GetInputData(0, 0));
}
void vtkLabeledTreeMapDataMapper::UpdateFontSizes()
{
char test[2];
test[1] = '\0';
int tSize[2], i;
// Make sure that there is a text mapper at index 0
if (!this->TextMappers[0])
{
this->TextMappers[0] = vtkTextMapper::New();
this->NumberOfLabels = 1;
}
for (i = 0; i <= this->MaxFontLevel; i++)
{
this->TextMappers[0]->SetTextProperty(this->HLabelProperties[i]);
this->FontHeights[i] = 0;
for (test[0] = 32; test[0] < 127; test[0]++)
{
this->TextMappers[0]->SetInput(test);
this->TextMappers[0]->GetSize(this->CurrentViewPort, tSize);
this->FontWidths[i][test[0]-32] = tSize[0];
if (this->FontHeights[i] < tSize[1])
{
this->FontHeights[i] = tSize[1];
}
}
}
}
void vtkLabeledTreeMapDataMapper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ClipTextMode: " << this->ClipTextMode << endl;
os << indent << "ChildMotion: " << this->ChildMotion << endl;
os << indent << "DynamicLevel: " << this->DynamicLevel << endl;
int i;
os << "Font Sizes: ";
for (i = 0; i <= this->MaxFontLevel; i++)
{
os << this->HLabelProperties[i]->GetFontSize() << " ";
}
os << endl;
os << indent << "Level Range: [" << this->StartLevel
<< ", " << this->EndLevel << "]" << endl;
}
int vtkLabeledTreeMapDataMapper::UpdateWindowInfo(vtkViewport *viewport)
{
// Assumes that the view is not rotated w/r to the tree map!
int *dc;
float p[2][2];
this->VCoord->SetViewport(viewport);
// Transform 0,0 and 1,1 into screen coordinates
this->VCoord->SetValue(0.0, 0.0, 0.0);
dc = VCoord->GetComputedDisplayValue(0);
p[0][0] = dc[0];
p[0][1] = dc[1];
this->VCoord->SetValue(1.0, 1.0, 0.0);
dc = this->VCoord->GetComputedDisplayValue(0);
p[1][0] = dc[0];
p[1][1] = dc[1];
// The translation is defined by 0,0's new position
this->BoxTrans[0][0] = p[0][0];
this->BoxTrans[1][0] = p[0][1];
// The scales are defined as p[1] - p[0]
this->BoxTrans[0][1] = p[1][0] - p[0][0];
this->BoxTrans[1][1] = p[1][1] - p[0][1];
// Get the window extents
vtkWindow * win = viewport->GetVTKWindow();
int *winPos = win->GetPosition();
this->WindowLimits[0][0] = winPos[0];
this->WindowLimits[1][0] = winPos[1];
int *winSize = win->GetSize();
this->WindowLimits[0][1] = this->WindowLimits[0][0] + winSize[0];
this->WindowLimits[1][1] = this->WindowLimits[1][0] + winSize[1];
// We are done with the coordinate, so release the viewport
this->VCoord->SetViewport(NULL);
// Ideally we can compare the new scales with the original and
// see if the change has exceeded some threshold - in that case
// we could return 0
return 1;
}
void vtkLabeledTreeMapDataMapper::GetVertexLabel(vtkIdType vertex,
vtkDataArray *numericData,
vtkStringArray *stringData,
int activeComp, int numComp,
char *string)
{
char format[1024];
double val;
int j;
if ( numericData )
{
if ( numComp == 1 )
{
if (numericData->GetDataType() == VTK_CHAR)
{
if (strcmp(this->LabelFormat,"%c") != 0)
{
vtkErrorMacro(<<"Label format must be %c to use with char");
string[0] = '\0';
return;
}
sprintf(string, this->LabelFormat,
static_cast<char>(numericData->GetComponent(vertex, activeComp)));
}
else
{
sprintf(string, this->LabelFormat,
numericData->GetComponent(vertex, activeComp));
}
}
else
{
strcpy(format, "("); strcat(format, this->LabelFormat);
for (j=0; j<(numComp-1); j++)
{
sprintf(string, format, numericData->GetComponent(vertex, j));
strcpy(format,string); strcat(format,", ");
strcat(format, this->LabelFormat);
}
sprintf(string, format, numericData->GetComponent(vertex, numComp-1));
strcat(string, ")");
}
}
else if (stringData)// rendering string data
{
if (strcmp(this->LabelFormat,"%s") != 0)
{
vtkErrorMacro(<<"Label format must be %s to use with strings");
string[0] = '\0';
return;
}
sprintf(string, this->LabelFormat,
stringData->GetValue(vertex).c_str());
}
else // Use the vertex id
{
val = static_cast<double>(vertex);
sprintf(string, this->LabelFormat, val);
}
}
//----------------------------------------------------------------------------
void vtkLabeledTreeMapDataMapper::RenderOverlay(vtkViewport *viewport,
vtkActor2D *actor)
{
int i;
double x[3];
for (i=0; i<this->NumberOfLabels; i++)
{
this->TextPoints->GetPoint(i,x);
actor->GetPositionCoordinate()->SetCoordinateSystemToWorld();
actor->GetPositionCoordinate()->SetValue(x);
this->TextMappers[i]->RenderOverlay(viewport, actor);
}
}
//----------------------------------------------------------------------------
void vtkLabeledTreeMapDataMapper::RenderOpaqueGeometry(vtkViewport *viewport,
vtkActor2D *actor)
{
int i, numComp = 0, pointIdLabels, activeComp = 0;
int numVertices;
double x[3];
vtkAbstractArray *abstractData;
vtkDataArray *numericData, *tempData;
vtkStringArray *stringData;
vtkTree *input=this->GetInputTree();
if ( ! input )
{
vtkErrorMacro(<<"Need input tree to render labels (2)");
return;
}
vtkTextProperty *tprop = this->GetLabelTextProperty();
if (!tprop)
{
vtkErrorMacro(<<"Need text property to render labels");
return;
}
this->GetInputAlgorithm()->Update();
// Input might have changed
input = this->GetInputTree();
vtkDataSetAttributes *pd = input->GetVertexData();
// Get the treeMap info
tempData = this->GetInputArrayToProcess(0, input);
if (!tempData)
{
vtkErrorMacro(<< "Input Tree does not have box information.");
return;
}
vtkFloatArray *boxInfo = vtkFloatArray::SafeDownCast(tempData);
// Check to see whether we have to rebuild everything
if ( this->UpdateWindowInfo(viewport) ||
this->CurrentViewPort != viewport ||
this->GetMTime() > this->BuildTime ||
input->GetMTime() > this->BuildTime ||
tprop->GetMTime() > this->BuildTime)
{
vtkDebugMacro(<<"Rebuilding labels");
// See if we have to recalculate fonts sizes
if (this->CurrentViewPort != viewport)
{
this->CurrentViewPort = viewport;
this->UpdateFontSizes();
}
// figure out what to label, and if we can label it
pointIdLabels = 0;
abstractData = NULL;
numericData = NULL;
stringData = NULL;
switch (this->LabelMode)
{
case VTK_LABEL_IDS:
pointIdLabels = 1;
break;
case VTK_LABEL_SCALARS:
if ( pd->GetScalars() )
{
numericData = pd->GetScalars();
}
break;
case VTK_LABEL_VECTORS:
if ( pd->GetVectors() )
{
numericData = pd->GetVectors();
}
break;
case VTK_LABEL_NORMALS:
if ( pd->GetNormals() )
{
numericData = pd->GetNormals();
}
break;
case VTK_LABEL_TCOORDS:
if ( pd->GetTCoords() )
{
numericData = pd->GetTCoords();
}
break;
case VTK_LABEL_TENSORS:
if ( pd->GetTensors() )
{
numericData = pd->GetTensors();
}
break;
case VTK_LABEL_FIELD_DATA:
{
int arrayNum;
if (this->FieldDataName != NULL)
{
abstractData = pd->GetAbstractArray(this->FieldDataName, arrayNum);
}
else
{
arrayNum = (this->FieldDataArray < pd->GetNumberOfArrays() ?
this->FieldDataArray : pd->GetNumberOfArrays() - 1);
abstractData = pd->GetAbstractArray(arrayNum);
}
numericData = vtkDataArray::SafeDownCast(abstractData);
stringData = vtkStringArray::SafeDownCast(abstractData);
}; break;
}
// determine number of components and check input
if ( pointIdLabels )
{
;
}
else if ( numericData )
{
numComp = numericData->GetNumberOfComponents();
activeComp = 0;
if ( this->LabeledComponent >= 0 )
{
activeComp = (this->LabeledComponent < numComp ?
this->LabeledComponent : numComp - 1);
numComp = 1;
}
}
else if ( !stringData )
{
vtkErrorMacro(<<"Need input data to render labels (3)");
return;
}
// Make sure that the array of TextMappers can accommodate
// the number of vertices in the tree - Note that we may
// not create the actual mappers
numVertices = input->GetNumberOfVertices();
if ( numVertices > this->NumberOfLabelsAllocated )
{
// delete old stuff
for (i=0; i < this->NumberOfLabelsAllocated; i++)
{
if (this->TextMappers[i])
{
this->TextMappers[i]->Delete();
}
}
delete [] this->TextMappers;
this->NumberOfLabelsAllocated = numVertices;
this->TextMappers = new vtkTextMapper * [numVertices];
this->VertexList->SetNumberOfIds(numVertices);
this->TextPoints->Allocate(numVertices);
for (i = 0; i < numVertices; i++)
{
this->TextMappers[i] = 0;
}
}
this->LabelTree(input, boxInfo, numericData, stringData,
activeComp, numComp);
}
for (i=0; i<this->NumberOfLabels; i++)
{
this->TextPoints->GetPoint(i,x);
actor->GetPositionCoordinate()->SetCoordinateSystemToWorld();
actor->GetPositionCoordinate()->SetValue(x);
this->TextMappers[i]->RenderOpaqueGeometry(viewport, actor);
}
}
void vtkLabeledTreeMapDataMapper::LabelTree(vtkTree *tree,
vtkFloatArray *boxInfo,
vtkDataArray *numericData,
vtkStringArray *stringData,
int activeComp,
int numComps)
{
float blimits[4], blimitsDC[4], textPosWC[3];
char string[1024];
int results;
vtkTextProperty *tprop = NULL;
vtkIdType vertex, level, root = tree->GetRoot();
if (root < 0)
{
vtkErrorMacro(<< "Input Tree does not have a root.");
return;
}
this->NumberOfLabels = 0;
vtkTreeDFSIterator* dfs = vtkTreeDFSIterator::New();
dfs->SetTree(tree);
while (dfs->HasNext())
{
// Are we suppose to display this vertex?
vertex = dfs->Next();
level = tree->GetLevel(vertex);
if (level >= this->StartLevel && (this->EndLevel == -1 || level <= this->EndLevel))
{
boxInfo->GetTupleValue(vertex, blimits); // Get the extents of the vertex
if (this->ConvertToDC(blimits, blimitsDC))
{
continue;
}
this->GetVertexLabel(vertex, numericData, stringData, activeComp, numComps,
string);
results = this->AnalyseLabel(string, level, blimitsDC, textPosWC, &tprop);
if (results == 1)
{
// Label doesn't fit in its box - don't both processing children
continue;
}
}
else
{
// results == 2 from AnalyseLabel means that the label can't be
// displayed due to reasons other than size - well in this
// case we can't display due to the level limit we
// also have to deactive the maks for this level
this->LabelMasks[level][0] = -1.0;
results = 2;
}
if (!results)
{
if (!this->TextMappers[this->NumberOfLabels])
{
this->TextMappers[this->NumberOfLabels] = vtkTextMapper::New();
}
this->TextMappers[this->NumberOfLabels]->SetInput(string);
this->TextMappers[this->NumberOfLabels]->
SetTextProperty(tprop);
this->TextPoints->SetPoint(this->NumberOfLabels, textPosWC);
++(this->NumberOfLabels);
}
// Is this level the deepest we can display?
if (this->EndLevel == level)
{
continue;
}
}
dfs->Delete();
this->BuildTime.Modified();
}
int vtkLabeledTreeMapDataMapper::GetStringSize(char *string, int level)
{
if (level > this->MaxFontLevel)
{
level = this->MaxFontLevel;
}
int size = 0, i;
for(i = 0; string[i] != '\0'; i++)
{
if (string[i] < 32)
{
continue;
}
if (string[i] > 126)
{
continue;
}
size += this->FontWidths[level][string[i]-32];
}
return size;
}
int vtkLabeledTreeMapDataMapper::ConvertToDC(float *binfo, float *newBinfo)
{
newBinfo[0] = this->BoxTrans[0][0] + (binfo[0]*this->BoxTrans[0][1]);
newBinfo[1] = this->BoxTrans[0][0] + (binfo[1]*this->BoxTrans[0][1]);
newBinfo[2] = this->BoxTrans[1][0] + (binfo[2]*this->BoxTrans[1][1]);
newBinfo[3] = this->BoxTrans[1][0] + (binfo[3]*this->BoxTrans[1][1]);
// See the comments in AnalyseLabel for why we're comparing against
// these numbers.
double windowWidth = this->WindowLimits[0][1] - this->WindowLimits[0][0];
double windowHeight = this->WindowLimits[1][1] - this->WindowLimits[1][0];
if (newBinfo[0] >= windowWidth)
{
return 1;
}
if (newBinfo[1] <= 0)
{
return 1;
}
if (newBinfo[2] >= windowHeight)
{
return 1;
}
if (newBinfo[3] <= 0)
{
return 1;
}
if (!this->ClipTextMode)
{
return 0;
}
if (newBinfo[0] < 0)
{
newBinfo[0] = 0;
}
if (newBinfo[1] > windowWidth)
{
newBinfo[1] = windowWidth;
}
if (newBinfo[2] < 0)
{
newBinfo[2] = 0;
}
if (newBinfo[3] > windowHeight)
{
newBinfo[3] = windowHeight;
}
return 0;
}
void vtkLabeledTreeMapDataMapper::SetFontSizeRange(int maxSize, int minSize,
int delta)
{
int nLevels = (maxSize - minSize) / delta;
int i, s;
if (nLevels < 0)
{
vtkErrorMacro(<<"maxSize is smaller than minSize");
return;
}
if ((maxSize - (nLevels * delta)) > minSize)
{
++nLevels;
}
if (this->MaxFontLevel != nLevels)
{
if (this->MaxFontLevel)
{
delete [] this->FontHeights;
for (i = 0; i <= this->MaxFontLevel; i++)
{
delete [] this->FontWidths[i];
this->HLabelProperties[i]->Delete();
}
delete [] this->FontWidths;
delete [] this->HLabelProperties;
}
this->MaxFontLevel = nLevels;
this->FontHeights = new int[this->MaxFontLevel+1];
this->FontWidths = new int *[this->MaxFontLevel+1];
this->HLabelProperties = new vtkTextProperty *[this->MaxFontLevel+1];
for(i = 0; i <= this->MaxFontLevel; i++)
{
this->FontWidths[i] = new int[95];
this->HLabelProperties[i] = vtkTextProperty::New();
this->HLabelProperties[i]->SetFontSize(12);
this->HLabelProperties[i]->SetBold(1);
this->HLabelProperties[i]->SetItalic(1);
this->HLabelProperties[i]->SetShadow(1);
this->HLabelProperties[i]->SetFontFamilyToArial();
this->HLabelProperties[i]->SetJustificationToCentered();
this->HLabelProperties[i]->SetVerticalJustificationToCentered();
this->HLabelProperties[i]->SetColor(1, 1, 1);
}
}
for(i = 0, s = maxSize; i < this->MaxFontLevel; i++, s -= delta)
{
this->HLabelProperties[i]->SetFontSize(s);
}
this->HLabelProperties[i]->SetFontSize(minSize);
this->CurrentViewPort = 0;
}
void vtkLabeledTreeMapDataMapper::GetFontSizeRange(int range[3])
{
range[0] = this->HLabelProperties[0]->GetFontSize();
range[1] = this->HLabelProperties[this->MaxFontLevel - 1]->GetFontSize();
range[2] = (range[0] - range[1]) / (this->MaxFontLevel - 1);
}
int vtkLabeledTreeMapDataMapper::AnalyseLabel(char * string, int level,
float *blimitsDC,
float *textPosWC,
vtkTextProperty **tprop)
{
float sizes[2], tPosDC[2], delta, flimits[4];
int fsize, trueLevel, oDir; // oDir is the text orientation
// Calculate the size of the box in DC
sizes[0] = blimitsDC[1] - blimitsDC[0];
sizes[1] = blimitsDC[3] - blimitsDC[2];
trueLevel = level - this->StartLevel;
if (trueLevel < 0)
{
vtkErrorMacro(<< "Invalid level.");
trueLevel = 0;
}
trueLevel =
(trueLevel > this->MaxFontLevel) ? this->MaxFontLevel : trueLevel;
fsize = this->GetStringSize(string, trueLevel);
// Determine the orientation of the label
// Vertical Label - THIS DOESN'T WORK DUE TO ISSUES WITH vtkTextActor
// so always do the following - hence the 1 in the condition
if (1 || (sizes[0] >= sizes[1]))
{
// Horizontal label
oDir = 0;
*tprop = this->HLabelProperties[trueLevel];
}
else
{
// Vertical Label - THIS DOESN'T WORK DUE TO ISSUES WITH vtkTextActor
oDir = 1;
*tprop = this->VerticalLabelProperty;
}
// Is this level dynamic or static?
if (level >= this->DynamicLevel)
{
// See if the text will not even fit in the box
if (sizes[!oDir] < this->FontHeights[trueLevel])
{
// Text will not fit
return 1;
}
if (sizes[oDir] < fsize)
{
// Text will not fit
return 1;
}
}
// Calculate the bounding box of the text
// Determine where to place the text
tPosDC[0] = 0.5 *(blimitsDC[0] + blimitsDC[1]);
tPosDC[1] = 0.5 *(blimitsDC[2] + blimitsDC[3]);
// Compute mask for this level
delta = 0.5 * 1.05 * (!oDir?fsize:this->FontHeights[trueLevel]);
flimits[0] = tPosDC[0] - delta;
flimits[1] = tPosDC[0] + delta;
delta = 0.5 * 1.05 * (oDir?fsize:this->FontHeights[trueLevel]);
flimits[2] = tPosDC[1] - delta;
flimits[3] = tPosDC[1] + delta;
// If the label is not to be centered based on the clipped form of the
// vertex's box see if it has been clipped away
if (!this->ClipTextMode)
{
// The 'flimits' variable contains the bounding box of the label
// in coordinates relative to (0, 0) in the window -- that is, the
// lower left corner of the window. These next few lines test to
// make sure the label is not entirely outside the window. The
// coordinates in WindowLimits are actually in the space of the
// entire screen, not just this application or its OpenGL window.
double windowWidth = this->WindowLimits[0][1] - this->WindowLimits[0][0];
double windowHeight = this->WindowLimits[1][1] - this->WindowLimits[0][0];
if ((flimits[0] >= windowWidth) ||
(flimits[1] <= 0) ||
(flimits[2] >= windowHeight) ||
(flimits[3] <= 0))
{
this->LabelMasks[level][0] = -1.0;
return 2;
}
}
// Apply Masks
if (level && (level > this->DynamicLevel))
{
if (this->ApplyMasks(level, flimits, blimitsDC))
{
// This label does not fit based on the masks
// Since device coordinate can not be < 0 set
// the first component of the mask to be -1
// to indicate the mask is not to be used
this->LabelMasks[level][0] = -1.0;
return 2;
}
}
this->LabelMasks[level][0] = flimits[0];
this->LabelMasks[level][1] = flimits[1];
this->LabelMasks[level][2] = flimits[2];
this->LabelMasks[level][3] = flimits[3];
// since applying the masks can shift the label calculate the
// new position
tPosDC[0] = 0.5 *(flimits[0] + flimits[1]);
tPosDC[1] = 0.5 *(flimits[2] + flimits[3]);
textPosWC[0] = (tPosDC[0] - this->BoxTrans[0][0])/ this->BoxTrans[0][1];
textPosWC[1] = (tPosDC[1] - this->BoxTrans[1][0])/ this->BoxTrans[1][1];
textPosWC[2] = 1.0;
return 0;
}
int vtkLabeledTreeMapDataMapper::ApplyMasks(int level, float flimits[4],
float blimits[4])
{
// Note that all limits and mask information is in Device Coordinates
float dy = 0.0;
int changed = 1, dir = 0, l;
int status = 1;
if (!this->ChildMotion)
{
// If any of the masks intersect the label don't display it
for (l = 0; l < level; l++)
{
// Skip all masks that refer to labels that are not displayed
// (i.e. that the first component < 0)
if (this->LabelMasks[l][0] < 0.0)
{
continue;
}
if (this->LabelMasks[l][0] > flimits[1])
{
continue;
}
if (this->LabelMasks[l][1] < flimits[0])
{
continue;
}
if (this->LabelMasks[l][2] > flimits[3])
{
continue;
}
if (this->LabelMasks[l][3] < flimits[2])
{
continue;
}
// If we are here the label intersects the mask
return 1;
}
return 0;
}
// dir == 0 indicates we are dropping the label and 1 means we are raising it
while (changed)
{
changed = 0;
for (l = 0; l < level; l++)
{
// Skip all masks that refer to labels that are not displayed
// (i.e. that the first component < 0)
// or do not interfere in the y-direction
// (i.e. the second component < 0)
// or has been already fixed in the y-direction
// (.i.e the third component < 0)
if ((this->LabelMasks[l][0] < 0.0) ||
(this->LabelMasks[l][1] < 0.0) ||
(this->LabelMasks[l][2] < 0.0))
{
continue;
}
// If the label passes either x-check it will never
// interfere with a horizontal label
if (this->LabelMasks[l][0] > flimits[1])
{
// Set the second component to be -(value + 1) - the reason
// for the offset is to take of the case the original value is 0
this->LabelMasks[l][1] = -1.0 * (this->LabelMasks[l][1] + 1.0);
continue;
}
if (this->LabelMasks[l][1] < flimits[0])
{
this->LabelMasks[l][1] = -1.0 * (this->LabelMasks[l][1] + 1.0);
continue;
}
if (this->LabelMasks[l][2] > (flimits[3]+dy))
{
// If dy < 0 then this condition will always be true and
// this check can be turned off
if (dy < 0.0)
{
this->LabelMasks[l][2] = -1.0 * (this->LabelMasks[l][2] + 1.0);
}
continue;
}
if (this->LabelMasks[l][3] < (flimits[2]+dy))
{
// If dy > 0 then this condition will always be true and
// this check can be turned off
if (dy > 0.0)
{
this->LabelMasks[l][2] = -1.0 * (this->LabelMasks[l][2] + 1.0);
}
continue;
}
// If we are here then the mask does clip the label
// see which direction we are going it - added a cushion of 5 pixels
if (dir)
{
dy = 5 + this->LabelMasks[l][3] - flimits[2];
}
else
{
dy = this->LabelMasks[l][2] - (5 + flimits[3]);
}
// Indicate that we changed something
changed = 1;
}
// See if anything changed - if it did then repeat the mask loop
if (changed)
{
continue;
}
// See if the current label position will not fit on the screen
if ((blimits[2] > (flimits[2] + dy)) || (blimits[3] < (flimits[3] + dy)))
{
// Have we been dropping the label?
if (!dir)
{
// Try raising it - we will need to reset all the masks with
// negative third components
dir = 1;
changed = 1;
for (l = 0; l < level; l++)
{
if (this->LabelMasks[l][2] < 0.0)
{
this->LabelMasks[l][2] = - 1.0 * (this->LabelMasks[l][2]+ 1.0);
}
}
}
// in this case there was no way to display the label
status = 1;
}
else
{
// Success
status = 0;
flimits[2] += dy;
flimits[3] += dy;
}
}
// Reset masks that were deactivated
for (l = 0; l < level; l++)
{
if (this->LabelMasks[l][1] < 0.0)
{
this->LabelMasks[l][1] = - 1.0 * (this->LabelMasks[l][1]+ 1.0);
continue;
}
if (this->LabelMasks[l][2] < 0.0)
{
this->LabelMasks[l][2] = - 1.0 * (this->LabelMasks[l][2]+ 1.0);
continue;
}
}
return status;
}
void vtkLabeledTreeMapDataMapper::SetLevelRange(int start, int end)
{
if (((end != -1) && (start > end)) || (start < 0))
{
vtkErrorMacro(<<"Invalid level range specified.");
return;