vtkScalarBarActor.cxx 71.5 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkScalarBarActor.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Will Schroeder's avatar
Will Schroeder committed
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13
14
15

=========================================================================*/
#include "vtkScalarBarActor.h"
16
#include "vtkScalarBarActorInternal.h"
17

18
19
#include "vtkCellArray.h"
#include "vtkCellData.h"
20
21
22
23
24
#include "vtkColor.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkImageData.h"
#include "vtkMath.h"
25
#include "vtkObjectFactory.h"
26
#include "vtkPolyData.h"
27
#include "vtkPolyDataMapper2D.h"
28
29
#include "vtkProperty2D.h"
#include "vtkRenderer.h"
30
#include "vtkScalarsToColors.h"
31
32
#include "vtkSmartPointer.h"
#include "vtkTextActor.h"
Sebastien Barre's avatar
Sebastien Barre committed
33
#include "vtkTextProperty.h"
34
#include "vtkTexture.h"
35
36
#include "vtkViewport.h"
#include "vtkWindow.h"
37
#include "vtkMathTextUtilities.h"
38

39
40
#include <vector>
#include <set>
41
42
43
44
45
46
47
48
49
#include <map>

#include <stdio.h> // for snprintf

#if defined(_WIN32) && !defined(__CYGWIN__)
#  define SNPRINTF _snprintf
#else
#  define SNPRINTF snprintf
#endif
50

Brad King's avatar
Brad King committed
51
vtkStandardNewMacro(vtkScalarBarActor);
52

53
vtkCxxSetObjectMacro(vtkScalarBarActor,LookupTable,vtkScalarsToColors);
Sebastien Barre's avatar
Sebastien Barre committed
54
55
vtkCxxSetObjectMacro(vtkScalarBarActor,LabelTextProperty,vtkTextProperty);
vtkCxxSetObjectMacro(vtkScalarBarActor,TitleTextProperty,vtkTextProperty);
56
57
vtkCxxSetObjectMacro(vtkScalarBarActor,BackgroundProperty,vtkProperty2D);
vtkCxxSetObjectMacro(vtkScalarBarActor,FrameProperty,vtkProperty2D);
58

Sebastien Barre's avatar
Sebastien Barre committed
59
60
//----------------------------------------------------------------------------
// Instantiate object with 64 maximum colors; 5 labels; %%-#6.3g label
61
62
63
64
// format, no title, and vertical orientation. The initial scalar bar
// size is (0.05 x 0.8) of the viewport size.
vtkScalarBarActor::vtkScalarBarActor()
{
65
  this->P = new vtkScalarBarActorInternal;
66
  this->LookupTable = NULL;
67
  this->Position2Coordinate->SetValue(0.17, 0.8);
68

69
70
  this->PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
  this->PositionCoordinate->SetValue(0.82,0.1);
71

72
73
  this->MaximumNumberOfColors = 64;
  this->NumberOfLabels = 5;
74
  this->NumberOfLabelsBuilt = 0;
75
76
  this->Orientation = VTK_ORIENT_VERTICAL;
  this->Title = NULL;
77
  this->ComponentTitle = NULL;
78

Sebastien Barre's avatar
Sebastien Barre committed
79
80
81
82
83
84
85
86
87
88
  this->LabelTextProperty = vtkTextProperty::New();
  this->LabelTextProperty->SetFontSize(12);
  this->LabelTextProperty->SetBold(1);
  this->LabelTextProperty->SetItalic(1);
  this->LabelTextProperty->SetShadow(1);
  this->LabelTextProperty->SetFontFamilyToArial();

  this->TitleTextProperty = vtkTextProperty::New();
  this->TitleTextProperty->ShallowCopy(this->LabelTextProperty);

89
  this->LabelFormat = new char[8];
90
91
  sprintf(this->LabelFormat,"%s","%-#6.3g");

92
  this->TitleActor = vtkTextActor::New();
93
94
  this->TitleActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
95

96
97
  this->ScalarBar = vtkPolyData::New();
  this->ScalarBarMapper = vtkPolyDataMapper2D::New();
98
  this->ScalarBarMapper->SetInputData(this->ScalarBar);
99
100
  this->ScalarBarActor = vtkActor2D::New();
  this->ScalarBarActor->SetMapper(this->ScalarBarMapper);
101
102
  this->ScalarBarActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
Ken Martin's avatar
Ken Martin committed
103
104
105
106
  this->LastOrigin[0] = 0;
  this->LastOrigin[1] = 0;
  this->LastSize[0] = 0;
  this->LastSize[1] = 0;
107

David Thompson's avatar
David Thompson committed
108
  this->DrawAnnotations = 1;
109
110
111
112
113
114
115
116
117
118
119
120
  this->DrawNanAnnotation = 0;
  this->FixedAnnotationLeaderLineColor = 0;
  this->NanAnnotation = 0;
  this->SetNanAnnotation("NaN");
  this->P->NanSwatch = vtkPolyData::New();
  this->P->NanSwatchMapper = vtkPolyDataMapper2D::New();
  this->P->NanSwatchActor = vtkActor2D::New();
  this->P->NanSwatchMapper->SetInputData(this->P->NanSwatch);
  this->P->NanSwatchActor->SetMapper(this->P->NanSwatchMapper);
  this->P->NanSwatchActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);

David Thompson's avatar
David Thompson committed
121
  this->AnnotationLeaderPadding = 8.;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  this->P->AnnotationBoxes = vtkPolyData::New();
  this->P->AnnotationBoxesMapper = vtkPolyDataMapper2D::New();
  this->P->AnnotationBoxesActor = vtkActor2D::New();
  this->P->AnnotationBoxesMapper->SetInputData(this->P->AnnotationBoxes);
  this->P->AnnotationBoxesActor->SetMapper(this->P->AnnotationBoxesMapper);
  this->P->AnnotationBoxesActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
  this->P->AnnotationLeaders = vtkPolyData::New();
  this->P->AnnotationLeadersMapper = vtkPolyDataMapper2D::New();
  this->P->AnnotationLeadersActor = vtkActor2D::New();
  this->P->AnnotationLeadersMapper->SetInputData(this->P->AnnotationLeaders);
  this->P->AnnotationLeadersActor->SetMapper(this->P->AnnotationLeadersMapper);
  this->P->AnnotationLeadersActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
  this->P->AnnotationLabels = 0; // Can't allocate until we have a lookup table.
  this->P->AnnotationAnchors = 0; // Can't allocate until we have a lookup table.
  this->P->AnnotationColors = 0; // Can't allocate until we have a lookup table.
  this->P->NumberOfAnnotationLabelsBuilt = 0;
140

141
142
143
144
  // If opacity is on, a jail like texture is displayed behind it..

  this->UseOpacity       = 0;
  this->TextureGridWidth = 10.0;
145

146
  this->TexturePolyData = vtkPolyData::New();
147
  vtkPolyDataMapper2D* textureMapper = vtkPolyDataMapper2D::New();
148
  textureMapper->SetInputData(this->TexturePolyData);
149
150
151
152
153
154
155
156
  this->TextureActor = vtkActor2D::New();
  this->TextureActor->SetMapper(textureMapper);
  textureMapper->Delete();
  this->TextureActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
  vtkFloatArray* tc = vtkFloatArray::New();
  tc->SetNumberOfComponents(2);
  tc->SetNumberOfTuples(4);
157
  tc->InsertComponent(0,0, 0.0);
158
159
  tc->InsertComponent(0,1, 0.0);
  tc->InsertComponent(1,1, 0.0);
160
  tc->InsertComponent(3,0, 0.0);
161
162
163
164
165
166
167
168
169
170
171
172
  this->TexturePolyData->GetPointData()->SetTCoords(tc);
  tc->Delete();

  vtkCellArray* polys2 = vtkCellArray::New();
  polys2->InsertNextCell(4);
  polys2->InsertCellPoint(0);
  polys2->InsertCellPoint(1);
  polys2->InsertCellPoint(2);
  polys2->InsertCellPoint(3);
  this->TexturePolyData->SetPolys(polys2);
  polys2->Delete();

173
  vtkProperty2D* imageProperty = vtkProperty2D::New();
174
175
176
177
178
179
180
  imageProperty->SetOpacity(0.08);
  this->TextureActor->SetProperty(imageProperty);
  imageProperty->Delete();

  // Create the default texture. Just a "Jail" like grid

  const unsigned int dim = 128;
181
  vtkImageData* image = vtkImageData::New();
182
  image->SetDimensions(dim, dim, 1);
Berk Geveci's avatar
Berk Geveci committed
183
  image->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
184

185
186
  for (unsigned int y = 0; y < dim; y++)
    {
187
188
    unsigned char* ptr =
      static_cast<unsigned char*>(image->GetScalarPointer(0, y, 0));
189
190
191
192
193
194
195
196
    for (unsigned int x = 0; x < dim; x++)
      {
      *ptr = ((x == y) || (x == (dim-y-1))) ? 255 : 0;
      ++ptr;
      }
    }

  this->Texture = vtkTexture::New();
197
  this->Texture->SetInputData( image );
198
199
  this->Texture->RepeatOn();
  image->Delete();
200
201
202
203

  // Default text position : Above scalar bar if orientation is horizontal
  //                         Right of scalar bar if orientation is vertical
  this->TextPosition = SucceedScalarBar;
204
205
206

  this->MaximumWidthInPixels = VTK_INT_MAX;
  this->MaximumHeightInPixels = VTK_INT_MAX;
207
208
209
210
211
212
213

  this->BackgroundProperty = vtkProperty2D::New();
  this->FrameProperty = vtkProperty2D::New();

  this->DrawBackground = 0;
  this->Background = vtkPolyData::New();
  this->BackgroundMapper = vtkPolyDataMapper2D::New();
214
  this->BackgroundMapper->SetInputData(this->Background);
215
216
  this->BackgroundActor = vtkActor2D::New();
  this->BackgroundActor->SetMapper(this->BackgroundMapper);
217
218
  this->BackgroundActor->GetPositionCoordinate()
    ->SetReferenceCoordinate(this->PositionCoordinate);
219
220
221
222

  this->DrawFrame = 0;
  this->Frame = vtkPolyData::New();
  this->FrameMapper = vtkPolyDataMapper2D::New();
223
  this->FrameMapper->SetInputData(this->Frame);
224
225
  this->FrameActor = vtkActor2D::New();
  this->FrameActor->SetMapper(this->FrameMapper);
226
227
  this->FrameActor->GetPositionCoordinate()
    ->SetReferenceCoordinate(this->PositionCoordinate);
228
229

  this->DrawColorBar = true;
230
231
}

Sebastien Barre's avatar
Sebastien Barre committed
232
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
233
234
235
// Release any graphics resources that are being consumed by this actor.
// The parameter window could be used to determine which graphic
// resources to release.
236
void vtkScalarBarActor::ReleaseGraphicsResources(vtkWindow* win)
Ken Martin's avatar
Ken Martin committed
237
238
{
  this->TitleActor->ReleaseGraphicsResources(win);
239
  if (!this->P->TextActors.empty())
Ken Martin's avatar
Ken Martin committed
240
    {
241
242
243
244
245
    vtkScalarBarActorInternal::ActorVec::iterator it;
    for (
      it = this->P->TextActors.begin();
      it != this->P->TextActors.end();
      ++it)
Ken Martin's avatar
Ken Martin committed
246
      {
247
      (*it)->ReleaseGraphicsResources(win);
Ken Martin's avatar
Ken Martin committed
248
249
      }
    }
250
  if ( this->P->AnnotationLabels != NULL )
251
    {
252
    for ( int i = 0; i < this->P->NumberOfAnnotationLabelsBuilt; ++ i )
253
      {
254
      this->P->AnnotationLabels[i]->ReleaseGraphicsResources(win);
255
256
      }
    }
Ken Martin's avatar
Ken Martin committed
257
  this->ScalarBarActor->ReleaseGraphicsResources(win);
258
259
260
  this->P->NanSwatchActor->ReleaseGraphicsResources(win);
  this->P->AnnotationBoxesActor->ReleaseGraphicsResources(win);
  this->P->AnnotationLeadersActor->ReleaseGraphicsResources(win);
261
262
  this->BackgroundActor->ReleaseGraphicsResources(win);
  this->FrameActor->ReleaseGraphicsResources(win);
Ken Martin's avatar
Ken Martin committed
263
264
}

Sebastien Barre's avatar
Sebastien Barre committed
265
//----------------------------------------------------------------------------
266
267
vtkScalarBarActor::~vtkScalarBarActor()
{
268
  if (this->LabelFormat)
Ken Martin's avatar
Ken Martin committed
269
270
271
272
    {
    delete [] this->LabelFormat;
    this->LabelFormat = NULL;
    }
273
274

  this->TitleActor->Delete();
275
  if (this->P->AnnotationLabels != NULL)
276
    {
277
    for (int i = 0; i < this->P->NumberOfAnnotationLabelsBuilt; ++i)
278
      {
279
      this->P->AnnotationLabels[i]->Delete();
280
      }
281
    delete [] this->P->AnnotationLabels;
282
    }
283
  if (this->P->AnnotationAnchors != NULL)
284
    {
285
    delete [] this->P->AnnotationAnchors;
286
    }
287
288
289
290
291
  if (this->P->AnnotationColors != NULL)
    {
    delete [] this->P->AnnotationColors;
    }
  this->SetNanAnnotation(NULL);
292

293
294
295
  this->ScalarBar->Delete();
  this->ScalarBarMapper->Delete();
  this->ScalarBarActor->Delete();
Ken Martin's avatar
Ken Martin committed
296

297
298
299
300
301
302
303
  this->P->NanSwatch->Delete();
  this->P->NanSwatchMapper->Delete();
  this->P->NanSwatchActor->Delete();

  this->P->AnnotationBoxes->Delete();
  this->P->AnnotationBoxesMapper->Delete();
  this->P->AnnotationBoxesActor->Delete();
304

305
306
307
  this->P->AnnotationLeaders->Delete();
  this->P->AnnotationLeadersMapper->Delete();
  this->P->AnnotationLeadersActor->Delete();
308

Ken Martin's avatar
Ken Martin committed
309
310
311
312
313
  if (this->Title)
    {
    delete [] this->Title;
    this->Title = NULL;
    }
314
315
316
317
318
319

  if ( this->ComponentTitle )
    {
    delete [] this->ComponentTitle;
    this->ComponentTitle = NULL;
    }
320

321
  this->SetLookupTable(NULL);
Sebastien Barre's avatar
Sebastien Barre committed
322
323
  this->SetLabelTextProperty(NULL);
  this->SetTitleTextProperty(NULL);
324
325
326
  this->Texture->Delete();
  this->TextureActor->Delete();
  this->TexturePolyData->Delete();
327
328
329
330
331
332
333
334
  this->Background->Delete();
  this->BackgroundMapper->Delete();
  this->BackgroundActor->Delete();
  this->Frame->Delete();
  this->FrameMapper->Delete();
  this->FrameActor->Delete();
  this->SetBackgroundProperty(NULL);
  this->SetFrameProperty(NULL);
335
  delete this->P;
336
337
}

Sebastien Barre's avatar
Sebastien Barre committed
338
//----------------------------------------------------------------------------
339
int vtkScalarBarActor::RenderOverlay(vtkViewport* viewport)
Ken Martin's avatar
Ken Martin committed
340
{
341
  int renderedSomething = 0;
Ken Martin's avatar
Ken Martin committed
342
  int i;
343

344
  // Everything is built, just have to render
345
346
347
348
  if (this->DrawBackground)
    {
    renderedSomething += this->BackgroundActor->RenderOverlay(viewport);
    }
349

350
  if (this->UseOpacity && this->DrawColorBar)
351
352
353
354
355
    {
    this->Texture->Render(vtkRenderer::SafeDownCast(viewport));
    renderedSomething += this->TextureActor->RenderOverlay(viewport);
    }

356
357
358
  // Draw either the scalar bar (non-indexed mode) or
  // the annotated value boxes (indexed mode).
  if (!this->LookupTable->GetIndexedLookup())
359
    {
360
361
    if (this->DrawColorBar)
      {
362
      renderedSomething += this->ScalarBarActor->RenderOverlay(viewport);
363
      }
364

365
366
367
368
369
    vtkScalarBarActorInternal::ActorVec::iterator it;
    for (
      it = this->P->TextActors.begin();
      it != this->P->TextActors.end();
      ++it)
370
      {
371
      renderedSomething += (*it)->RenderOverlay(viewport);
372
373
      }
    }
374
  else if (this->DrawColorBar)
375
    {
376
377
378
379
380
381
382
383
384
385
386
387
388
    renderedSomething +=
      this->P->AnnotationBoxesActor->RenderOverlay(viewport);
    }

  if (this->DrawNanAnnotation)
    {
    renderedSomething +=
      this->P->NanSwatchActor->RenderOverlay(viewport);
    }

  if (this->DrawFrame)
    {
    renderedSomething += this->FrameActor->RenderOverlay(viewport);
389
    }
390

391
392
393
394
395
396
397
  if (this->Title != NULL)
    {
    renderedSomething += this->TitleActor->RenderOverlay(viewport);
    }

  if (this->P->AnnotationLabels == NULL &&
    this->P->NumberOfAnnotationLabelsBuilt)
Ken Martin's avatar
Ken Martin committed
398
    {
399
400
    vtkWarningMacro(<<"Need a mapper to render the scalar bar");
    return renderedSomething;
Ken Martin's avatar
Ken Martin committed
401
    }
402

David Thompson's avatar
David Thompson committed
403
  if ( this->DrawAnnotations )
404
    {
405
406
407
408
409
410
    if ( this->P->NumberOfAnnotationLabelsBuilt )
      {
      renderedSomething +=
        this->P->AnnotationLeadersActor->RenderOverlay( viewport );
      }
    for ( i = 0; i < this->P->NumberOfAnnotationLabelsBuilt; ++ i )
David Thompson's avatar
David Thompson committed
411
      {
412
413
      renderedSomething +=
        this->P->AnnotationLabels[i]->RenderOverlay( viewport );
David Thompson's avatar
David Thompson committed
414
      }
415
    }
416

417
  renderedSomething = (renderedSomething > 0)?(1):(0);
418
  return renderedSomething;
Ken Martin's avatar
Ken Martin committed
419
420
}

Sebastien Barre's avatar
Sebastien Barre committed
421
//----------------------------------------------------------------------------
422
int vtkScalarBarActor::RenderOpaqueGeometry(vtkViewport* viewport)
423
{
424
  int renderedSomething = 0;
425
  int i;
426

Sebastien Barre's avatar
Sebastien Barre committed
427
  if (!this->LookupTable)
428
429
    {
    vtkWarningMacro(<<"Need a mapper to render a scalar bar");
430
    return 0;
431
432
    }

Sebastien Barre's avatar
Sebastien Barre committed
433
434
435
436
437
438
439
440
441
442
443
444
  if (!this->TitleTextProperty)
    {
    vtkErrorMacro(<<"Need title text property to render a scalar bar");
    return 0;
    }

  if (!this->LabelTextProperty)
    {
    vtkErrorMacro(<<"Need label text property to render a scalar bar");
    return 0;
    }

Ken Martin's avatar
Ken Martin committed
445
  // Check to see whether we have to rebuild everything
446
  int positionsHaveChanged = 0;
447
448
  if (viewport->GetMTime() > this->BuildTime ||
      (viewport->GetVTKWindow() &&
Sebastien Barre's avatar
Sebastien Barre committed
449
       viewport->GetVTKWindow()->GetMTime() > this->BuildTime))
Ken Martin's avatar
Ken Martin committed
450
451
452
    {
    // if the viewport has changed we may - or may not need
    // to rebuild, it depends on if the projected coords chage
453
454
    int size[2];
    int* barOrigin;
Ken Martin's avatar
Ken Martin committed
455
    barOrigin = this->PositionCoordinate->GetComputedViewportValue(viewport);
456
    size[0] =
Ken Martin's avatar
Ken Martin committed
457
458
      this->Position2Coordinate->GetComputedViewportValue(viewport)[0] -
      barOrigin[0];
459
    size[1] =
Ken Martin's avatar
Ken Martin committed
460
461
      this->Position2Coordinate->GetComputedViewportValue(viewport)[1] -
      barOrigin[1];
462

463
464
    // Check if we have bounds on the maximum size
    size[0] = size[0] > this->MaximumWidthInPixels
465
            ? this->MaximumWidthInPixels : size[0];
466
    size[1] = size[1] > this->MaximumHeightInPixels
467
468
            ? this->MaximumHeightInPixels : size[1];

469
    if (this->LastSize[0] != size[0] ||
Sebastien Barre's avatar
Sebastien Barre committed
470
        this->LastSize[1] != size[1] ||
471
        this->LastOrigin[0] != barOrigin[0] ||
472
        this->LastOrigin[1] != barOrigin[1])
Ken Martin's avatar
Ken Martin committed
473
      {
474
      positionsHaveChanged = 1;
Ken Martin's avatar
Ken Martin committed
475
476
      }
    }
477

478
  // Check to see whether we have to rebuild everything
479
  if (positionsHaveChanged ||
480
      this->GetMTime() > this->BuildTime ||
Sebastien Barre's avatar
Sebastien Barre committed
481
482
      this->LookupTable->GetMTime() > this->BuildTime ||
      this->LabelTextProperty->GetMTime() > this->BuildTime ||
483
484
485
486
      this->TitleTextProperty->GetMTime() > this->BuildTime ||
      this->BackgroundProperty->GetMTime() > this->BuildTime ||
      this->FrameProperty->GetMTime() > this->BuildTime)

487
    {
488
    this->RebuildLayout(viewport);
489
490
491
    }

  // Everything is built, just have to render
Jim Miller's avatar
Style    
Jim Miller committed
492
493
  if (this->Title != NULL)
    {
494
495
    renderedSomething +=
      this->TitleActor->RenderOpaqueGeometry(viewport);
Jim Miller's avatar
Style    
Jim Miller committed
496
    }
497
498
  // Draw either the scalar bar (non-indexed mode) or
  // the annotated value boxes (indexed mode).
499
  if ( ! this->LookupTable->GetIndexedLookup() )
Jim Miller's avatar
Style    
Jim Miller committed
500
    {
501
502
    if (this->DrawColorBar)
      {
503
504
      renderedSomething +=
        this->ScalarBarActor->RenderOpaqueGeometry(viewport);
505
      }
506
507
508
509
510
    vtkScalarBarActorInternal::ActorVec::iterator ait;
    for (
      ait = this->P->TextActors.begin();
      ait != this->P->TextActors.end();
      ++ait)
David Thompson's avatar
David Thompson committed
511
      {
512
      renderedSomething += (*ait)->RenderOpaqueGeometry(viewport);
David Thompson's avatar
David Thompson committed
513
514
515
516
      }
    }
  else
    {
517
518
    if (this->DrawColorBar)
      {
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
      renderedSomething +=
        this->P->AnnotationBoxesActor->RenderOpaqueGeometry( viewport );
      }
    }

  if (this->DrawNanAnnotation)
    {
    renderedSomething +=
      this->P->NanSwatchActor->RenderOpaqueGeometry(viewport);
    }

  // Draw the annotation leaders and labels
  if ( this->DrawAnnotations )
    {
    if ( this->P->NumberOfAnnotationLabelsBuilt )
      {
      renderedSomething +=
        this->P->AnnotationLeadersActor->RenderOpaqueGeometry( viewport );
537
      }
538
    for ( i = 0; i < this->P->NumberOfAnnotationLabelsBuilt; ++ i )
David Thompson's avatar
David Thompson committed
539
      {
540
541
      renderedSomething +=
        this->P->AnnotationLabels[i]->RenderOpaqueGeometry( viewport );
David Thompson's avatar
David Thompson committed
542
      }
Jim Miller's avatar
Style    
Jim Miller committed
543
    }
544
545
546
547

  renderedSomething = (renderedSomething > 0)?(1):(0);

  return renderedSomething;
548
549
}

550
551
552
553
//-----------------------------------------------------------------------------
// Description:
// Does this prop have some translucent polygonal geometry?
int vtkScalarBarActor::HasTranslucentPolygonalGeometry()
554
555
556
557
{ // TODO: Handle case when IndexedLookup is true and any colors in the palette
  // have an alpha value, as the color swatches drawn by
  // this->P->AnnotationBoxesActor have 1 translucent triangle for each
  // alpha-swatch.
558
559
560
  return 0;
}

Sebastien Barre's avatar
Sebastien Barre committed
561
//----------------------------------------------------------------------------
562
void vtkScalarBarActor::PrintSelf(ostream& os, vtkIndent indent)
563
{
Brad King's avatar
Brad King committed
564
  this->Superclass::PrintSelf(os,indent);
565
566
567
568
569
570
571
572
573
574
575

  if ( this->LookupTable )
    {
    os << indent << "Lookup Table:\n";
    this->LookupTable->PrintSelf(os,indent.GetNextIndent());
    }
  else
    {
    os << indent << "Lookup Table: (none)\n";
    }

Sebastien Barre's avatar
Sebastien Barre committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  if (this->TitleTextProperty)
    {
    os << indent << "Title Text Property:\n";
    this->TitleTextProperty->PrintSelf(os,indent.GetNextIndent());
    }
  else
    {
    os << indent << "Title Text Property: (none)\n";
    }

  if (this->LabelTextProperty)
    {
    os << indent << "Label Text Property:\n";
    this->LabelTextProperty->PrintSelf(os,indent.GetNextIndent());
    }
  else
    {
    os << indent << "Label Text Property: (none)\n";
    }

596
  os << indent << "Title: " << (this->Title ? this->Title : "(none)") << "\n";
597
598
  os << indent << "ComponentTitle: "
    << (this->ComponentTitle ? this->ComponentTitle : "(none)") << "\n";
599
  os << indent << "Maximum Number Of Colors: "
600
601
     << this->MaximumNumberOfColors << "\n";
  os << indent << "Number Of Labels: " << this->NumberOfLabels << "\n";
602
  os << indent << "Number Of Labels Built: " << this->NumberOfLabelsBuilt << "\n";
603
604

  os << indent << "Orientation: ";
Jim Miller's avatar
Style    
Jim Miller committed
605
606
607
608
609
610
611
612
  if ( this->Orientation == VTK_ORIENT_HORIZONTAL )
    {
    os << "Horizontal\n";
    }
  else
    {
    os << "Vertical\n";
    }
613
614

  os << indent << "Label Format: " << this->LabelFormat << "\n";
615
616
617
618
  os << indent << "UseOpacity: " << this->UseOpacity << "\n";
  if (this->UseOpacity)
    {
    os << indent << "TextureGridWidth: " << this->TextureGridWidth << "\n";
Karthik Krishnan's avatar
Karthik Krishnan committed
619
620
    os << indent << "TextureActor:\n";
    this->TextureActor->PrintSelf(os, indent.GetNextIndent());
621
    }
622
623
624
625
626
627
628
629
  if (this->TextPosition == vtkScalarBarActor::PrecedeScalarBar)
    {
    os << indent << "TextPosition: PrecedeScalarBar\n";
    }
  else
    {
    os << indent << "TextPosition: SucceedScalarBar\n";
    }
Karthik Krishnan's avatar
Karthik Krishnan committed
630

631
  os << indent << "MaximumWidthInPixels: "
Karthik Krishnan's avatar
Karthik Krishnan committed
632
     << this->MaximumWidthInPixels << endl;
633
  os << indent << "MaximumHeightInPixels: "
Karthik Krishnan's avatar
Karthik Krishnan committed
634
     << this->MaximumHeightInPixels << endl;
635

David Thompson's avatar
David Thompson committed
636
637
  os << indent << "DrawAnnotations: "
    << this->DrawAnnotations << endl;
638
639
640
641
  os << indent << "DrawNanAnnotation: "
    << this->DrawNanAnnotation << endl;
  os << indent << "NanAnnotation: "
    << (this->NanAnnotation ? this->NanAnnotation : "(none)") << endl;
David Thompson's avatar
David Thompson committed
642
643
644
  os << indent << "AnnotationLeaderPadding: "
    << this->AnnotationLeaderPadding << endl;

645
646
647
648
649
650
  os << indent << "DrawBackground: " << this->DrawBackground << "\n";
  os << indent << "Background Property:\n";
  this->BackgroundProperty->PrintSelf(os,indent.GetNextIndent());
  os << indent << "DrawFrame: " << this->DrawFrame << "\n";
  os << indent << "Frame Property:\n";
  this->FrameProperty->PrintSelf(os,indent.GetNextIndent());
651
}
Ken Martin's avatar
Ken Martin committed
652

Sebastien Barre's avatar
Sebastien Barre committed
653
//----------------------------------------------------------------------------
654
void vtkScalarBarActor::ShallowCopy(vtkProp* prop)
Will Schroeder's avatar
Will Schroeder committed
655
{
656
  vtkScalarBarActor* a = vtkScalarBarActor::SafeDownCast(prop);
Will Schroeder's avatar
Will Schroeder committed
657
658
659
660
661
662
  if ( a != NULL )
    {
    this->SetPosition2(a->GetPosition2());
    this->SetLookupTable(a->GetLookupTable());
    this->SetMaximumNumberOfColors(a->GetMaximumNumberOfColors());
    this->SetOrientation(a->GetOrientation());
Sebastien Barre's avatar
Sebastien Barre committed
663
664
    this->SetLabelTextProperty(a->GetLabelTextProperty());
    this->SetTitleTextProperty(a->GetTitleTextProperty());
Will Schroeder's avatar
Will Schroeder committed
665
666
667
    this->SetLabelFormat(a->GetLabelFormat());
    this->SetTitle(a->GetTitle());
    this->GetPositionCoordinate()->SetCoordinateSystem(
668
      a->GetPositionCoordinate()->GetCoordinateSystem());
Will Schroeder's avatar
Will Schroeder committed
669
670
671
    this->GetPositionCoordinate()->SetValue(
      a->GetPositionCoordinate()->GetValue());
    this->GetPosition2Coordinate()->SetCoordinateSystem(
672
      a->GetPosition2Coordinate()->GetCoordinateSystem());
Will Schroeder's avatar
Will Schroeder committed
673
674
    this->GetPosition2Coordinate()->SetValue(
      a->GetPosition2Coordinate()->GetValue());
675
676
677
678
    this->SetDrawBackground(a->GetDrawBackground());
    this->SetBackgroundProperty(a->GetBackgroundProperty());
    this->SetDrawFrame(a->GetDrawFrame());
    this->SetFrameProperty(a->GetFrameProperty());
Will Schroeder's avatar
Will Schroeder committed
679
680
681
682
683
    }

  // Now do superclass
  this->vtkActor2D::ShallowCopy(prop);
}
Ken Martin's avatar
Ken Martin committed
684

Sebastien Barre's avatar
Sebastien Barre committed
685
//----------------------------------------------------------------------------
686
void vtkScalarBarActor::RebuildLayout(vtkViewport* viewport)
Ken Martin's avatar
Ken Martin committed
687
{
688
  vtkDebugMacro(<<"Rebuilding subobjects");
Sebastien Barre's avatar
Sebastien Barre committed
689

690
691
  this->P->Viewport = viewport;
  this->FreeLayoutStorage();
Sebastien Barre's avatar
Sebastien Barre committed
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
  // Permute indices used to measure width and height
  // so that thickness and length according to the orientation
  // of the scalar bar instead.
  if (this->Orientation == VTK_ORIENT_VERTICAL)
    {
    this->P->TL[0] = 0;
    this->P->TL[1] = 1;
    }
  else
    {
    this->P->TL[0] = 1;
    this->P->TL[1] = 0;
    }
  this->P->NumNotes = this->LookupTable->GetNumberOfAnnotatedValues();

  // Warning: The order of these calls is extremely important
  // as each updates members of this->P used by later methods!
  this->ComputeFrame();
  this->ComputeScalarBarThickness();
  this->LayoutNanSwatch();
  this->PrepareTitleText();
  this->LayoutTitle();
  this->ComputeScalarBarLength();
  this->LayoutTicks();
  this->LayoutAnnotations();

  // Now generate/configure the VTK datasets and actors that
  // illustrate the scalar bar when rendered using the
  // layout computed above.
  this->ConfigureAnnotations();
  this->ConfigureFrame();
  this->ConfigureScalarBar();
  this->ConfigureTitle();
  this->ConfigureTicks();
  this->ConfigureNanSwatch();
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::ComputeFrame()
{
  // get the viewport size in display coordinates
  int* p0;
  int* p1;
  int size[2];
  p0 = this->PositionCoordinate->GetComputedViewportValue(this->P->Viewport);
  p1 = this->Position2Coordinate->GetComputedViewportValue(this->P->Viewport);
  for (int i = 0; i < 2; ++i)
    {
    //this->P->Frame.Posn[i] = p0[i];
    this->P->Frame.Posn[i] = 0; // Translate the frame's coordinate system to p0
    size[i] = p1[i] - p0[i];
    }

  // Check if we have bounds on the maximum size
  size[0] = size[0] > this->MaximumWidthInPixels
          ? this->MaximumWidthInPixels : size[0];
  size[1] = size[1] > this->MaximumHeightInPixels
          ? this->MaximumHeightInPixels : size[1];

  for (int i = 0; i < 2; ++i)
    {
    this->P->Frame.Size[i] = size[this->P->TL[i]];
    }

  this->LastOrigin[0] = p0[0];
  this->LastOrigin[1] = p0[1];
  this->LastSize[0] = size[0];
  this->LastSize[1] = size[1];
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::ComputeScalarBarThickness()
{
  // We do not set Size[1] (length), since the title bounds may encroach
  // on it in the vertical orientation.
  this->P->ScalarBarBox.Size[0] =
    static_cast<int>(ceil(this->P->Frame.Size[0] / 2.));

  // The lower-left corner of the scalar bar may be estimated here
  // as identical to the Frame position in one or both coordinates,
  // depending on whether tick marks should precede the scalar bar or
  // not and on the orientation.
  //
  // It will be altered later in this->LayoutTicks to account
  // for the half-height/width of tick labels.
  this->P->ScalarBarBox.Posn = this->P->Frame.Posn;
  if (this->TextPosition == PrecedeScalarBar)
    {
    this->P->ScalarBarBox.Posn[this->P->TL[0]] +=
      this->P->Frame.Size[0] - this->P->ScalarBarBox.Size[0];
    }
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::LayoutNanSwatch()
{
  // We don't have ScalarBarBox.Size[1] set yet; use the frame width instead.
  this->P->NanSwatchSize = static_cast<double>(
    this->P->ScalarBarBox.Size[0] > this->P->Frame.Size[1] / 4 ?
    this->P->Frame.Size[1] / 4 : this->P->ScalarBarBox.Size[0]);
  if (this->P->NanSwatchSize < 4 && this->P->Frame.Size[1] > 16)
    this->P->NanSwatchSize = 4;
  if (!this->DrawNanAnnotation)
    this->P->NanSwatchSize = 0;
  if (this->P->NumNotes)
    {
    this->P->SwatchPad = this->P->Frame.Size[1] / this->P->NumNotes > 16. ?
      4. : ( this->P->Frame.Size[1] / this->P->NumNotes / 4. );
    }
  else
    {
    this->P->SwatchPad = 4.;
    }

  if (this->Orientation == VTK_ORIENT_VERTICAL)
    {
    this->P->NanBox.Posn[0] = this->P->ScalarBarBox.Posn[0];
    this->P->NanBox.Posn[1] = this->P->Frame.Posn[1];
    this->P->ScalarBarBox.Posn[1] +=
      this->P->NanSwatchSize + this->P->SwatchPad;
    }
  else // HORIZONTAL
    {
    this->P->NanBox.Posn = this->P->ScalarBarBox.Posn;
    this->P->NanBox.Posn[this->P->TL[1]] +=
      this->P->Frame.Size[1] - this->P->NanSwatchSize;
    }
  this->P->NanBox.Size[0] = this->P->ScalarBarBox.Size[0];
  this->P->NanBox.Size[1] = this->P->NanSwatchSize;
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::PrepareTitleText()
{
  // Update actor with the latest title/subtitle
  if (this->ComponentTitle && strlen(this->ComponentTitle) > 0)
    {
    //need to account for a space between title & component and null term
    char* combinedTitle =
      new char[strlen(this->Title) + strlen(this->ComponentTitle) + 2];
    strcpy(combinedTitle, this->Title );
    strcat(combinedTitle, " ");
    strcat(combinedTitle, this->ComponentTitle);
    this->TitleActor->SetInput(combinedTitle);
    delete [] combinedTitle;
    }
  else
    {
    this->TitleActor->SetInput(this->Title);
    }

  if (this->TitleTextProperty->GetMTime() > this->BuildTime)
    {
    // Shallow copy here so that the size of the title prop is not affected
    // by the automatic adjustment of its text mapper's size (i.e. its
    // mapper's text property is identical except for the font size
    // which will be modified later). This allows text actors to
    // share the same text property, and in that case specifically allows
    // the title and label text prop to be the same.
    this->TitleActor->GetTextProperty()->ShallowCopy(this->TitleTextProperty);
    this->TitleActor->GetTextProperty()->SetJustificationToCentered();
    this->TitleActor->GetTextProperty()->SetVerticalJustification(
      this->TextPosition == PrecedeScalarBar ? VTK_TEXT_BOTTOM : VTK_TEXT_TOP);
    }
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::LayoutTitle()
{
  if (this->Title == NULL || !strlen(this->Title))
    {
    return;
    }

  int targetWidth, targetHeight;
  // Title spans entire width of frame at top, regardless of orientation.
  targetWidth = static_cast<int>(this->P->Frame.Size[this->P->TL[0]]);
  // Height is either: at most half the frame height or
  // a fixed portion of the frame remaining after subtracting the
  // scalar bar's thickness.
  //
  // When laid out horizontally, ticks share vertical space with title.
  // We want the title to be larger (18pt vs 14pt).
  targetHeight = static_cast<int>((
    this->Orientation == VTK_ORIENT_VERTICAL ||
    this->LookupTable->GetIndexedLookup()) ?
    ceil(this->P->Frame.Size[this->P->TL[1]] / 2. - this->P->TextPad) :
    (this->P->Frame.Size[0] - this->P->ScalarBarBox.Size[0] - this->P->TextPad)
    * 0.625);

  this->TitleActor->SetConstrainedFontSize(
    this->P->Viewport, targetWidth, targetHeight);

  // Now fetch the actual size from the actor and use it to
  // update the box size and position.
  double titleSize[2] = {0, 0};
  this->TitleActor->GetSize(this->P->Viewport, titleSize);
  for (int i = 0; i < 2; ++i)
    {
    this->P->TitleBox.Size[this->P->TL[i]] =
      static_cast<int>(ceil(titleSize[i]));
    }
Sebastien Barre's avatar
Sebastien Barre committed
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
  this->P->TitleBox.Posn[0] =
    this->P->Frame.Posn[0] +
    (this->P->Frame.Size[this->P->TL[0]] - titleSize[0]) / 2;
  this->P->TitleBox.Posn[1] =
    this->P->Frame.Posn[1] + this->P->Frame.Size[this->P->TL[1]];
  if (
    this->Orientation == VTK_ORIENT_VERTICAL ||
    this->TextPosition == vtkScalarBarActor::SucceedScalarBar)
    {
    this->P->TitleBox.Posn[1] -= this->P->TitleBox.Size[this->P->TL[1]];
    }
  else
    {
    this->P->TitleBox.Posn[1] = this->P->Frame.Posn[1];
    }
}

//-----------------------------------------------------------------------------
void vtkScalarBarActor::ComputeScalarBarLength()
{
  this->P->ScalarBarBox.Size[1] =
    this->Orientation == VTK_ORIENT_VERTICAL ?
      this->P->Frame.Size[1] - this->P->TitleBox.Size[1] :
      this->P->Frame.Size[1];

  // The scalar bar does not include the Nan Swatch.
  this->P->ScalarBarBox.Size[1] -= this->P->NanSwatchSize + this->P->SwatchPad;
}

//-----------------------------------------------------------------------------
void vtkScalarBarActor::LayoutTicks()
{
  if (this->LookupTable->GetIndexedLookup())
    { // no tick marks in indexed lookup mode.
    this->NumberOfLabelsBuilt = 0;
    return;
    }
  // find the best size for the ticks
  double* range = this->LookupTable->GetRange();
  char string[512];
Ken Martin's avatar
Ken Martin committed
936
  double val;
Ken Martin's avatar
Ken Martin committed
937
  int i;
938

939
940
941
  int labelSize[2];
  labelSize[0] = labelSize[1] = 0;

Sebastien Barre's avatar
Sebastien Barre committed
942
943
944
945
946
  // TODO: this should be optimized, maybe by keeping a list of
  // allocated mappers, in order to avoid creation/destruction of
  // their underlying text properties (i.e. each time a mapper is
  // created, text properties are created and shallow-assigned a font size
  // which value might be "far" from the target font size).
947
  this->P->TextActors.resize(this->NumberOfLabels);
948

949
  // Does this map have its scale set to log?
950
  int isLogTable = this->LookupTable->UsingLogScale();
951

952
  for (i = 0; i < this->NumberOfLabels; i++)
Ken Martin's avatar
Ken Martin committed
953
    {
954
    this->P->TextActors[i].TakeReference(vtkTextActor::New());
Sebastien Barre's avatar
Sebastien Barre committed
955

956
957
    if ( isLogTable )
      {
Ken Martin's avatar
Ken Martin committed
958
      double lval;
Ken Martin's avatar
Ken Martin committed
959
960
      if (this->NumberOfLabels > 1)
        {
961
962
        lval = log10(range[0]) +
          static_cast<double>(i)/(this->NumberOfLabels-1) *
Ken Martin's avatar
Ken Martin committed
963
964
965
966
967
968
          (log10(range[1])-log10(range[0]));
        }
      else
        {
        lval = log10(range[0]) + 0.5*(log10(range[1])-log10(range[0]));
        }
Ken Martin's avatar
Ken Martin committed
969
      val = pow(10.0,lval);
970
971
972
      }
    else
      {
Ken Martin's avatar
Ken Martin committed
973
974
      if (this->NumberOfLabels > 1)
        {
975
        val = range[0] +
976
          static_cast<double>(i)/(this->NumberOfLabels-1)
977
          * (range[1]-range[0]);
Ken Martin's avatar
Ken Martin committed
978
979
980
981
982
        }
      else
        {
        val = range[0] + 0.5*(range[1]-range[0]);
        }
983
984
      }

985
986
    SNPRINTF(string, 511, this->LabelFormat, val);
    this->P->TextActors[i]->SetInput(string);
Sebastien Barre's avatar
Sebastien Barre committed
987
988
989
990
991
992
993

    // Shallow copy here so that the size of the label prop is not affected
    // by the automatic adjustment of its text mapper's size (i.e. its
    // mapper's text property is identical except for the font size
    // which will be modified later). This allows text actors to
    // share the same text property, and in that case specifically allows
    // the title and label text prop to be the same.
994
    this->P->TextActors[i]->GetTextProperty()->ShallowCopy(
Sebastien Barre's avatar
Sebastien Barre committed
995
996
      this->LabelTextProperty);

997
998
    this->P->TextActors[i]->SetProperty(this->GetProperty());
    this->P->TextActors[i]->GetPositionCoordinate()->
Ken Martin's avatar
Ken Martin committed
999
1000
      SetReferenceCoordinate(this->PositionCoordinate);
    }
Sebastien Barre's avatar
Sebastien Barre committed
1001

Ken Martin's avatar
Ken Martin committed
1002
1003
  if (this->NumberOfLabels)
    {
Sebastien Barre's avatar
Sebastien Barre committed
1004
1005
    int targetWidth, targetHeight;

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
    this->P->TickBox.Posn = this->P->ScalarBarBox.Posn;
    if ( this->Orientation == VTK_ORIENT_VERTICAL )
      { // NB. Size[0] = width, Size[1] = height
      // Ticks share the width with the scalar bar
      this->P->TickBox.Size[0] =
        this->P->Frame.Size[0] - this->P->ScalarBarBox.Size[0];
      // Tick height could be adjusted if title text is
      // lowered by box constraints, but we won't bother:
      this->P->TickBox.Size[1] = this->P->Frame.Size[1] -
        this->P->TitleBox.Size[1] - this->P->TextPad;
      // Tick box height also reduced by NaN swatch size, if present:
      if (this->DrawNanAnnotation)
        {
        this->P->TickBox.Size[1] -=
          this->P->NanBox.Size[1] + this->P->SwatchPad;
        }

      if (this->TextPosition == vtkScalarBarActor::PrecedeScalarBar)
        {
        this->P->TickBox.Posn[0] -= this->P->TickBox.Size[0];
        }
      else
        {
        this->P->TickBox.Posn[0] += this->P->ScalarBarBox.Size[0];
        }

      targetWidth = this->P->TickBox.Size[0];
      targetHeight = static_cast<int>((this->P->TickBox.Size[1] -
          this->P->TextPad * (this->NumberOfLabels - 1)) /
        this->NumberOfLabels);
      }
    else
      { // NB. Size[1] = width, Size[0] = height
      // Ticks span the entire width of the frame
      this->P->TickBox.Size[1] = this->P->ScalarBarBox.Size[1];
      // Ticks share vertical space with title and scalar bar.
      this->P->TickBox.Size[0] =
        this->P->Frame.Size[0] - this->P->ScalarBarBox.Size[0] -
        this->P->TextPad - this->P->TitleBox.Size[0];

      if (this->TextPosition == vtkScalarBarActor::PrecedeScalarBar)
        {
        this->P->TickBox.Posn[1] -= this->P->TickBox.Size[0];
        /* or equivalently: Posn[1] -=
          this->P->Frame.Size[0] -
          this->P->TitleBox.Size[0] - this->P->TextPad -
          this->P->ScalarBarBox.Size[0];
          */
        }
      else
        {
        this->P->TickBox.Posn[1] += this->P->ScalarBarBox.Size[0];
        }

      targetWidth = static_cast<int>((this->P->TickBox.Size[1] -
          this->P->TextPad * (this->NumberOfLabels - 1)) /
        this->NumberOfLabels);
      targetHeight = this->P->TickBox.Size[0];
      }

    vtkTextActor::SetMultipleConstrainedFontSize(
      this->P->Viewport, targetWidth, targetHeight,
      this->P->TextActors.PointerArray(), this->NumberOfLabels,
      labelSize);

    // Now adjust scalar bar size by the half-size of the first and last ticks
    this->P->ScalarBarBox.Posn[this->P->TL[1]]
      += labelSize[this->P->TL[1]] / 2.;
    this->P->ScalarBarBox.Size[1] -= labelSize[this->P->TL[1]];
    this->P->TickBox.Posn[this->P->TL[1]] += labelSize[this->P->TL[1]] / 2.;
    this->P->TickBox.Size[1] -= labelSize[this->P->TL[1]];
    }
  this->NumberOfLabelsBuilt = this->NumberOfLabels;
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::LayoutAnnotations()
{
  if (this->DrawAnnotations)
    {
    double* range = this->LookupTable->GetRange();

    this->P->NumberOfAnnotationLabelsBuilt =
      this->MapAnnotationLabels(
        this->LookupTable,
        this->P->ScalarBarBox.Posn[this->P->TL[1]],
        this->P->ScalarBarBox.Size[1], range);
    }
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::ConfigureFrame()
{
  // set frame structure
  vtkPoints* frPts = vtkPoints::New();
  frPts->SetNumberOfPoints(5);
  vtkCellArray* frLines = vtkCellArray::New();
  frLines->Allocate(frLines->EstimateSize(1,5));

  this->FrameActor->SetProperty(this->FrameProperty);
  this->Frame->Initialize();
  this->Frame->SetPoints(frPts);
  this->Frame->SetLines(frLines);
  frPts->Delete();
  frLines->Delete();

  // set background structure
  vtkPoints* bgPts = vtkPoints::New();
  bgPts->SetNumberOfPoints(4);
  vtkCellArray* bgPolys = vtkCellArray::New();
  bgPolys->Allocate(bgPolys->EstimateSize(1,4));

  this->BackgroundActor->SetProperty(this->BackgroundProperty);
  this->Background->Initialize();
  this->Background->SetPoints(bgPts);
  this->Background->SetPolys(bgPolys);
  bgPts->Delete();
  bgPolys->Delete();

  double x[3];
  x[2] = 0.;

  // generate background and frame points and cell
  x[0]=0; x[1]=0;
  bgPts->SetPoint(0,x);
  frPts->SetPoint(0,x);
  frPts->SetPoint(4,x);

  x[0]=0; x[1]=this->P->Frame.Size[this->P->TL[1]];
  bgPts->SetPoint(1,x);
  frPts->SetPoint(1,x);

  x[0]=this->P->Frame.Size[this->P->TL[0]];
  x[1]=this->P->Frame.Size[this->P->TL[1]];
  bgPts->SetPoint(2,x);
  frPts->SetPoint(2,x);

  x[0]=this->P->Frame.Size[this->P->TL[0]]; x[1]=0;
  bgPts->SetPoint(3,x);
  frPts->SetPoint(3,x);

  vtkIdType bgIds[5] = {0, 1, 2, 3, 4};
  bgPolys->InsertNextCell(4, bgIds);
  frLines->InsertNextCell(5, bgIds);
}

//----------------------------------------------------------------------------
void vtkScalarBarActor::ConfigureScalarBar()
{
  vtkScalarsToColors* lut = this->LookupTable;
  double* range = lut->GetRange();
  this->P->NumColors = lut->GetIndexedLookup() ?
    lut->GetNumberOfAnnotatedValues() : this->MaximumNumberOfColors;
  this->P->NumSwatches = this->P->NumColors + (this->DrawNanAnnotation ? 1 : 0);
  int numPts = 2 * (this->P->NumColors + 1) + (this->DrawNanAnnotation ? 4 : 0);
  this->P->SwatchPts = vtkPoints::New();
  this->P->SwatchPts->SetNumberOfPoints(numPts);
  this->P->Polys = vtkCellArray::New();
  this->P->Polys->Allocate(
    this->P->Polys->EstimateSize(this->P->NumSwatches,4));
  this->P->SwatchColors = vtkUnsignedCharArray::New();

  unsigned int nComponents = ((this->UseOpacity) ? 4 : 3);
  this->P->SwatchColors->SetNumberOfComponents(nComponents);
  this->P->SwatchColors->SetNumberOfTuples(this->P->NumSwatches);

  this->ScalarBarActor->SetProperty(this->GetProperty());
  this->ScalarBar->Initialize();
  this->ScalarBar->SetPoints(this->P->SwatchPts);
  this->ScalarBar->SetPolys(this->P->Polys);
  this->ScalarBar->GetCellData()->SetScalars(this->P->SwatchColors);
  this->P