vtkScalarBarActor.cxx 20.2 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

17
18
#include "vtkCellArray.h"
#include "vtkCellData.h"
19
#include "vtkObjectFactory.h"
20
#include "vtkPolyData.h"
21
22
23
#include "vtkPolyDataMapper2D.h"
#include "vtkScalarsToColors.h"
#include "vtkTextMapper.h"
Sebastien Barre's avatar
Sebastien Barre committed
24
#include "vtkTextProperty.h"
25
26
#include "vtkViewport.h"
#include "vtkWindow.h"
27
#include "vtkLookupTable.h"
28

29
vtkCxxRevisionMacro(vtkScalarBarActor, "1.56");
Brad King's avatar
Brad King committed
30
vtkStandardNewMacro(vtkScalarBarActor);
31

32
vtkCxxSetObjectMacro(vtkScalarBarActor,LookupTable,vtkScalarsToColors);
Sebastien Barre's avatar
Sebastien Barre committed
33
34
vtkCxxSetObjectMacro(vtkScalarBarActor,LabelTextProperty,vtkTextProperty);
vtkCxxSetObjectMacro(vtkScalarBarActor,TitleTextProperty,vtkTextProperty);
35

Sebastien Barre's avatar
Sebastien Barre committed
36
37
//----------------------------------------------------------------------------
// Instantiate object with 64 maximum colors; 5 labels; %%-#6.3g label
38
39
40
41
42
// format, no title, and vertical orientation. The initial scalar bar
// size is (0.05 x 0.8) of the viewport size.
vtkScalarBarActor::vtkScalarBarActor()
{
  this->LookupTable = NULL;
43
44
45
46
47
  this->Position2Coordinate->SetValue(0.17, 0.8);
  
  this->PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
  this->PositionCoordinate->SetValue(0.82,0.1);
  
48
49
  this->MaximumNumberOfColors = 64;
  this->NumberOfLabels = 5;
50
  this->NumberOfLabelsBuilt = 0;
51
52
53
  this->Orientation = VTK_ORIENT_VERTICAL;
  this->Title = NULL;

Sebastien Barre's avatar
Sebastien Barre committed
54
55
56
57
58
59
60
61
62
63
  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);

64
65
66
67
68
69
  this->LabelFormat = new char[8]; 
  sprintf(this->LabelFormat,"%s","%-#6.3g");

  this->TitleMapper = vtkTextMapper::New();
  this->TitleActor = vtkActor2D::New();
  this->TitleActor->SetMapper(this->TitleMapper);
70
71
72
  this->TitleActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
  
73
74
75
76
77
78
79
80
  this->TextMappers = NULL;
  this->TextActors = NULL;

  this->ScalarBar = vtkPolyData::New();
  this->ScalarBarMapper = vtkPolyDataMapper2D::New();
  this->ScalarBarMapper->SetInput(this->ScalarBar);
  this->ScalarBarActor = vtkActor2D::New();
  this->ScalarBarActor->SetMapper(this->ScalarBarMapper);
81
82
  this->ScalarBarActor->GetPositionCoordinate()->
    SetReferenceCoordinate(this->PositionCoordinate);
Ken Martin's avatar
Ken Martin committed
83
84
85
86
  this->LastOrigin[0] = 0;
  this->LastOrigin[1] = 0;
  this->LastSize[0] = 0;
  this->LastSize[1] = 0;
87
88
}

Sebastien Barre's avatar
Sebastien Barre committed
89
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Release any graphics resources that are being consumed by this actor.
// The parameter window could be used to determine which graphic
// resources to release.
void vtkScalarBarActor::ReleaseGraphicsResources(vtkWindow *win)
{
  this->TitleActor->ReleaseGraphicsResources(win);
  if (this->TextMappers != NULL )
    {
    for (int i=0; i < this->NumberOfLabelsBuilt; i++)
      {
      this->TextActors[i]->ReleaseGraphicsResources(win);
      }
    }
  this->ScalarBarActor->ReleaseGraphicsResources(win);
}

Sebastien Barre's avatar
Sebastien Barre committed
106
//----------------------------------------------------------------------------
107
108
vtkScalarBarActor::~vtkScalarBarActor()
{
Ken Martin's avatar
Ken Martin committed
109
110
111
112
113
  if (this->LabelFormat) 
    {
    delete [] this->LabelFormat;
    this->LabelFormat = NULL;
    }
114
115
116
117
118
119

  this->TitleMapper->Delete();
  this->TitleActor->Delete();

  if (this->TextMappers != NULL )
    {
120
    for (int i=0; i < this->NumberOfLabelsBuilt; i++)
121
122
123
124
125
126
127
128
129
130
131
      {
      this->TextMappers[i]->Delete();
      this->TextActors[i]->Delete();
      }
    delete [] this->TextMappers;
    delete [] this->TextActors;
    }

  this->ScalarBar->Delete();
  this->ScalarBarMapper->Delete();
  this->ScalarBarActor->Delete();
Ken Martin's avatar
Ken Martin committed
132
133
134
135
136
137

  if (this->Title)
    {
    delete [] this->Title;
    this->Title = NULL;
    }
138
139
  
  this->SetLookupTable(NULL);
Sebastien Barre's avatar
Sebastien Barre committed
140
141
  this->SetLabelTextProperty(NULL);
  this->SetTitleTextProperty(NULL);
142
143
}

Sebastien Barre's avatar
Sebastien Barre committed
144
//----------------------------------------------------------------------------
145
int vtkScalarBarActor::RenderOverlay(vtkViewport *viewport)
Ken Martin's avatar
Ken Martin committed
146
{
147
  int renderedSomething = 0;
Ken Martin's avatar
Ken Martin committed
148
149
150
151
152
  int i;
  
  // Everything is built, just have to render
  if (this->Title != NULL)
    {
153
    renderedSomething += this->TitleActor->RenderOverlay(viewport);
Ken Martin's avatar
Ken Martin committed
154
155
    }
  this->ScalarBarActor->RenderOverlay(viewport);
156
157
158
159
160
161
  if( this->TextActors == NULL)
    {
     vtkWarningMacro(<<"Need a mapper to render a scalar bar");
     return renderedSomething;
    }
  
Ken Martin's avatar
Ken Martin committed
162
163
  for (i=0; i<this->NumberOfLabels; i++)
    {
164
    renderedSomething += this->TextActors[i]->RenderOverlay(viewport);
Ken Martin's avatar
Ken Martin committed
165
    }
166
167
168
169

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

  return renderedSomething;
Ken Martin's avatar
Ken Martin committed
170
171
}

Sebastien Barre's avatar
Sebastien Barre committed
172
//----------------------------------------------------------------------------
173
int vtkScalarBarActor::RenderOpaqueGeometry(vtkViewport *viewport)
174
{
175
  int renderedSomething = 0;
176
  int i;
177
178
  int size[2];
  
Sebastien Barre's avatar
Sebastien Barre committed
179
  if (!this->LookupTable)
180
181
    {
    vtkWarningMacro(<<"Need a mapper to render a scalar bar");
182
    return 0;
183
184
    }

Sebastien Barre's avatar
Sebastien Barre committed
185
186
187
188
189
190
191
192
193
194
195
196
  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
197
  // Check to see whether we have to rebuild everything
198
  int positionsHaveChanged = 0;
Sebastien Barre's avatar
Sebastien Barre committed
199
200
201
  if (viewport->GetMTime() > this->BuildTime || 
      (viewport->GetVTKWindow() && 
       viewport->GetVTKWindow()->GetMTime() > this->BuildTime))
Ken Martin's avatar
Ken Martin committed
202
203
204
    {
    // if the viewport has changed we may - or may not need
    // to rebuild, it depends on if the projected coords chage
Bill Lorensen's avatar
Bill Lorensen committed
205
    int *barOrigin;
Ken Martin's avatar
Ken Martin committed
206
207
208
209
210
211
212
    barOrigin = this->PositionCoordinate->GetComputedViewportValue(viewport);
    size[0] = 
      this->Position2Coordinate->GetComputedViewportValue(viewport)[0] -
      barOrigin[0];
    size[1] = 
      this->Position2Coordinate->GetComputedViewportValue(viewport)[1] -
      barOrigin[1];
Sebastien Barre's avatar
Sebastien Barre committed
213
214
    if (this->LastSize[0] != size[0] || 
        this->LastSize[1] != size[1] ||
215
216
        this->LastOrigin[0] != barOrigin[0] || 
        this->LastOrigin[1] != barOrigin[1])
Ken Martin's avatar
Ken Martin committed
217
      {
218
      positionsHaveChanged = 1;
Ken Martin's avatar
Ken Martin committed
219
220
221
      }
    }
  
222
  // Check to see whether we have to rebuild everything
223
224
  if (positionsHaveChanged ||
      this->GetMTime() > this->BuildTime || 
Sebastien Barre's avatar
Sebastien Barre committed
225
226
227
      this->LookupTable->GetMTime() > this->BuildTime ||
      this->LabelTextProperty->GetMTime() > this->BuildTime ||
      this->TitleTextProperty->GetMTime() > this->BuildTime)
228
229
230
231
232
233
234
    {
    vtkDebugMacro(<<"Rebuilding subobjects");

    // Delete previously constructed objects
    //
    if (this->TextMappers != NULL )
      {
235
      for (i=0; i < this->NumberOfLabelsBuilt; i++)
236
237
238
239
        {
        this->TextMappers[i]->Delete();
        this->TextActors[i]->Delete();
        }
240
241
242
243
      delete [] this->TextMappers;
      delete [] this->TextActors;
      }

244
    // Build scalar bar object; determine its type
245
    //
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    // is this a vtkLookupTable or a subclass of vtkLookupTable 
    // with its scale set to log
    // NOTE: it's possible we could to without the 'lut' variable
    // later in the code, but if the vtkLookupTableSafeDownCast operation
    // fails for some reason, this code will break in new ways. So, the 'LUT'
    // variable is used for this operation only
    vtkLookupTable *LUT = vtkLookupTable::SafeDownCast( this->LookupTable );
    int isLogTable = 0;
    if ( LUT )
      {
      if ( LUT->GetScale() == VTK_SCALE_LOG10 )
        {
        isLogTable = 1; 
        }
      }
261
    
Ken Martin's avatar
Ken Martin committed
262
    // we hard code how many steps to display
263
    vtkScalarsToColors *lut = this->LookupTable;
Ken Martin's avatar
Ken Martin committed
264
    int numColors = this->MaximumNumberOfColors;
Ken Martin's avatar
Ken Martin committed
265
    double *range = lut->GetRange();
266
267
268
269
270
271

    int numPts = 2*(numColors + 1);
    vtkPoints *pts = vtkPoints::New();
    pts->SetNumberOfPoints(numPts);
    vtkCellArray *polys = vtkCellArray::New();
    polys->Allocate(polys->EstimateSize(numColors,4));
272
273
274
    vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
    colors->SetNumberOfComponents(3);
    colors->SetNumberOfTuples(numColors);
275
276
277
278
279
280
281
282
283

    this->ScalarBarActor->SetProperty(this->GetProperty());
    this->ScalarBar->Initialize();
    this->ScalarBar->SetPoints(pts);
    this->ScalarBar->SetPolys(polys);
    this->ScalarBar->GetCellData()->SetScalars(colors);
    pts->Delete(); polys->Delete(); colors->Delete();

    // get the viewport size in display coordinates
284
285
286
287
288
289
290
291
    int *barOrigin, barWidth, barHeight;
    barOrigin = this->PositionCoordinate->GetComputedViewportValue(viewport);
    size[0] = 
      this->Position2Coordinate->GetComputedViewportValue(viewport)[0] -
      barOrigin[0];
    size[1] = 
      this->Position2Coordinate->GetComputedViewportValue(viewport)[1] -
      barOrigin[1];
Ken Martin's avatar
Ken Martin committed
292
293
294
295
296
    this->LastOrigin[0] = barOrigin[0];
    this->LastOrigin[1] = barOrigin[1];
    this->LastSize[0] = size[0];
    this->LastSize[1] = size[1];
    
297
298
299
    // Update all the composing objects
    this->TitleActor->SetProperty(this->GetProperty());
    this->TitleMapper->SetInput(this->Title);
Sebastien Barre's avatar
Sebastien Barre committed
300
301
302
303
304
305
306
307
308
309
310
    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->TitleMapper->GetTextProperty()->ShallowCopy(this->TitleTextProperty);
      this->TitleMapper->GetTextProperty()->SetJustificationToCentered();
      }
311
    
Ken Martin's avatar
Ken Martin committed
312
313
314
    // find the best size for the title font
    int titleSize[2];
    this->SizeTitle(titleSize, size, viewport);
315
    
Ken Martin's avatar
Ken Martin committed
316
317
318
    // find the best size for the ticks
    int labelSize[2];
    this->AllocateAndSizeLabels(labelSize, size, viewport,range);
319
    this->NumberOfLabelsBuilt = this->NumberOfLabels;
Ken Martin's avatar
Ken Martin committed
320
    
321
    // generate points
Ken Martin's avatar
Ken Martin committed
322
323
    double x[3]; x[2] = 0.0;
    double delta;
324
325
    if ( this->Orientation == VTK_ORIENT_VERTICAL )
      {
Ken Martin's avatar
Ken Martin committed
326
327
      barWidth = size[0] - 4 - labelSize[0];
      barHeight = (int)(0.86*size[1]);
Ken Martin's avatar
Ken Martin committed
328
      delta=(double)barHeight/numColors;
329
      for (i=0; i<numPts/2; i++)
330
331
332
        {
        x[0] = 0;
        x[1] = i*delta;
333
        pts->SetPoint(2*i,x);
334
        x[0] = barWidth;
335
        pts->SetPoint(2*i+1,x);
336
        }
337
338
339
      }
    else
      {
340
      barWidth = size[0];
Ken Martin's avatar
Ken Martin committed
341
      barHeight = (int)(0.4*size[1]);
Ken Martin's avatar
Ken Martin committed
342
      delta=(double)barWidth/numColors;
343
      for (i=0; i<numPts/2; i++)
344
345
346
        {
        x[0] = i*delta;
        x[1] = barHeight;
347
        pts->SetPoint(2*i,x);
348
        x[1] = 0;
349
        pts->SetPoint(2*i+1,x);
350
        }
351
352
353
354
      }

    //polygons & cell colors
    unsigned char *rgba, *rgb;
355
    vtkIdType ptIds[4];
356
357
358
359
360
361
362
363
    for (i=0; i<numColors; i++)
      {
      ptIds[0] = 2*i;
      ptIds[1] = ptIds[0] + 1;
      ptIds[2] = ptIds[1] + 2;
      ptIds[3] = ptIds[0] + 2;
      polys->InsertNextCell(4,ptIds);

364
365
      if ( isLogTable )
        {
Ken Martin's avatar
Ken Martin committed
366
        double rgbval = log10(range[0]) + 
367
          i*(log10(range[1])-log10(range[0]))/(numColors -1);
Ken Martin's avatar
Ken Martin committed
368
        rgba = lut->MapValue(pow(10.0,rgbval));
369
370
371
372
        }
      else
        {
        rgba = lut->MapValue(range[0] + (range[1] - range[0])*
Ken Martin's avatar
Ken Martin committed
373
                             ((double)i /(numColors-1.0)));
374
375
        }

376
      rgb = colors->GetPointer(3*i); //write into array directly
377
378
379
380
381
382
383
      rgb[0] = rgba[0];
      rgb[1] = rgba[1];
      rgb[2] = rgba[2];
      }

    // Now position everything properly
    //
Ken Martin's avatar
Ken Martin committed
384
    double val;
385
386
    if (this->Orientation == VTK_ORIENT_VERTICAL)
      {
387
388
      int sizeTextData[2];
      
389
      // center the title
Ken Martin's avatar
Ken Martin committed
390
      this->TitleActor->SetPosition(size[0]/2, 0.9*size[1]);
391
      
Jim Miller's avatar
Style    
Jim Miller committed
392
      for (i=0; i < this->NumberOfLabels; i++)
393
        {
Ken Martin's avatar
Ken Martin committed
394
395
        if (this->NumberOfLabels > 1)
          {
Ken Martin's avatar
Ken Martin committed
396
          val = (double)i/(this->NumberOfLabels-1) *barHeight;
Ken Martin's avatar
Ken Martin committed
397
398
399
400
401
          }
        else 
          {
          val = 0.5*barHeight;
          }
402
        this->TextMappers[i]->GetSize(viewport,sizeTextData);
Sebastien Barre's avatar
Sebastien Barre committed
403
        this->TextMappers[i]->GetTextProperty()->SetJustificationToLeft();
404
405
        this->TextActors[i]->SetPosition(barWidth+3,
                                         val - sizeTextData[1]/2);
406
        }
407
408
409
      }
    else
      {
Ken Martin's avatar
Ken Martin committed
410
411
      this->TitleActor->SetPosition(size[0]/2, 
                                    barHeight + labelSize[1] + 0.1*size[1]);
Jim Miller's avatar
Style    
Jim Miller committed
412
      for (i=0; i < this->NumberOfLabels; i++)
413
        {
Sebastien Barre's avatar
Sebastien Barre committed
414
        this->TextMappers[i]->GetTextProperty()->SetJustificationToCentered();
Ken Martin's avatar
Ken Martin committed
415
416
        if (this->NumberOfLabels > 1)
          {
Ken Martin's avatar
Ken Martin committed
417
          val = (double)i/(this->NumberOfLabels-1) * barWidth;
Ken Martin's avatar
Ken Martin committed
418
419
420
421
422
          }
        else
          {
          val = 0.5*barWidth;
          }
423
424
        this->TextActors[i]->SetPosition(val, barHeight + 0.05*size[1]);
        }
425
426
427
428
429
430
      }

    this->BuildTime.Modified();
    }

  // Everything is built, just have to render
Jim Miller's avatar
Style    
Jim Miller committed
431
432
  if (this->Title != NULL)
    {
433
    renderedSomething += this->TitleActor->RenderOpaqueGeometry(viewport);
Jim Miller's avatar
Style    
Jim Miller committed
434
    }
Ken Martin's avatar
Ken Martin committed
435
  this->ScalarBarActor->RenderOpaqueGeometry(viewport);
Jim Miller's avatar
Style    
Jim Miller committed
436
437
  for (i=0; i<this->NumberOfLabels; i++)
    {
438
    renderedSomething += this->TextActors[i]->RenderOpaqueGeometry(viewport);
Jim Miller's avatar
Style    
Jim Miller committed
439
    }
440
441
442
443

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

  return renderedSomething;
444
445
}

Sebastien Barre's avatar
Sebastien Barre committed
446
//----------------------------------------------------------------------------
447
void vtkScalarBarActor::PrintSelf(ostream& os, vtkIndent indent)
448
{
Brad King's avatar
Brad King committed
449
  this->Superclass::PrintSelf(os,indent);
450
451
452
453
454
455
456
457
458
459
460

  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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  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";
    }

481
482
483
484
  os << indent << "Title: " << (this->Title ? this->Title : "(none)") << "\n";
  os << indent << "Maximum Number Of Colors: " 
     << this->MaximumNumberOfColors << "\n";
  os << indent << "Number Of Labels: " << this->NumberOfLabels << "\n";
485
  os << indent << "Number Of Labels Built: " << this->NumberOfLabelsBuilt << "\n";
486
487

  os << indent << "Orientation: ";
Jim Miller's avatar
Style    
Jim Miller committed
488
489
490
491
492
493
494
495
  if ( this->Orientation == VTK_ORIENT_HORIZONTAL )
    {
    os << "Horizontal\n";
    }
  else
    {
    os << "Vertical\n";
    }
496
497
498

  os << indent << "Label Format: " << this->LabelFormat << "\n";
}
Ken Martin's avatar
Ken Martin committed
499

Sebastien Barre's avatar
Sebastien Barre committed
500
//----------------------------------------------------------------------------
Will Schroeder's avatar
Will Schroeder committed
501
502
503
504
505
506
507
508
509
void vtkScalarBarActor::ShallowCopy(vtkProp *prop)
{
  vtkScalarBarActor *a = vtkScalarBarActor::SafeDownCast(prop);
  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
510
511
    this->SetLabelTextProperty(a->GetLabelTextProperty());
    this->SetTitleTextProperty(a->GetTitleTextProperty());
Will Schroeder's avatar
Will Schroeder committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    this->SetLabelFormat(a->GetLabelFormat());
    this->SetTitle(a->GetTitle());
    this->GetPositionCoordinate()->SetCoordinateSystem(
      a->GetPositionCoordinate()->GetCoordinateSystem());    
    this->GetPositionCoordinate()->SetValue(
      a->GetPositionCoordinate()->GetValue());
    this->GetPosition2Coordinate()->SetCoordinateSystem(
      a->GetPosition2Coordinate()->GetCoordinateSystem());    
    this->GetPosition2Coordinate()->SetValue(
      a->GetPosition2Coordinate()->GetValue());
    }

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

Sebastien Barre's avatar
Sebastien Barre committed
528
529
530
//----------------------------------------------------------------------------
void vtkScalarBarActor::AllocateAndSizeLabels(int *labelSize, 
                                              int *size,
Ken Martin's avatar
Ken Martin committed
531
                                              vtkViewport *viewport,
Ken Martin's avatar
Ken Martin committed
532
                                              double *range)
Ken Martin's avatar
Ken Martin committed
533
{
Sebastien Barre's avatar
Sebastien Barre committed
534
535
  labelSize[0] = labelSize[1] = 0;

Ken Martin's avatar
Ken Martin committed
536
537
  this->TextMappers = new vtkTextMapper * [this->NumberOfLabels];
  this->TextActors = new vtkActor2D * [this->NumberOfLabels];
Sebastien Barre's avatar
Sebastien Barre committed
538

Ken Martin's avatar
Ken Martin committed
539
  char string[512];
Sebastien Barre's avatar
Sebastien Barre committed
540

Ken Martin's avatar
Ken Martin committed
541
  double val;
Ken Martin's avatar
Ken Martin committed
542
543
  int i;
  
Sebastien Barre's avatar
Sebastien Barre committed
544
545
546
547
548
  // 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).
549

550
551
552
553
554
555
556
557
558
559
560
  // is this a vtkLookupTable or a subclass of vtkLookupTable 
  // with its scale set to log
  vtkLookupTable *LUT = vtkLookupTable::SafeDownCast( this->LookupTable );
  int isLogTable = 0;
  if ( LUT )
    {
    if ( LUT->GetScale() == VTK_SCALE_LOG10 )
      {
      isLogTable = 1; 
      }
    }
561

Ken Martin's avatar
Ken Martin committed
562
563
564
  for (i=0; i < this->NumberOfLabels; i++)
    {
    this->TextMappers[i] = vtkTextMapper::New();
Sebastien Barre's avatar
Sebastien Barre committed
565

566
567
    if ( isLogTable )
      {
Ken Martin's avatar
Ken Martin committed
568
      double lval;
Ken Martin's avatar
Ken Martin committed
569
570
      if (this->NumberOfLabels > 1)
        {
Ken Martin's avatar
Ken Martin committed
571
        lval = log10(range[0]) + (double)i/(this->NumberOfLabels-1) *
Ken Martin's avatar
Ken Martin committed
572
573
574
575
576
577
          (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
578
      val = pow(10.0,lval);
579
580
581
      }
    else
      {
Ken Martin's avatar
Ken Martin committed
582
583
584
      if (this->NumberOfLabels > 1)
        {
        val = range[0] + 
Ken Martin's avatar
Ken Martin committed
585
          (double)i/(this->NumberOfLabels-1) * (range[1]-range[0]);
Ken Martin's avatar
Ken Martin committed
586
587
588
589
590
        }
      else
        {
        val = range[0] + 0.5*(range[1]-range[0]);
        }
591
592
      }

Ken Martin's avatar
Ken Martin committed
593
594
    sprintf(string, this->LabelFormat, val);
    this->TextMappers[i]->SetInput(string);
Sebastien Barre's avatar
Sebastien Barre committed
595
596
597
598
599
600
601
602
603
604

    // 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.
    this->TextMappers[i]->GetTextProperty()->ShallowCopy(
      this->LabelTextProperty);

Ken Martin's avatar
Ken Martin committed
605
606
607
608
609
610
    this->TextActors[i] = vtkActor2D::New();
    this->TextActors[i]->SetMapper(this->TextMappers[i]);
    this->TextActors[i]->SetProperty(this->GetProperty());
    this->TextActors[i]->GetPositionCoordinate()->
      SetReferenceCoordinate(this->PositionCoordinate);
    }
Sebastien Barre's avatar
Sebastien Barre committed
611

Ken Martin's avatar
Ken Martin committed
612
613
  if (this->NumberOfLabels)
    {
Sebastien Barre's avatar
Sebastien Barre committed
614
615
    int targetWidth, targetHeight;

Ken Martin's avatar
Ken Martin committed
616
617
618
619
620
621
622
623
624
625
    if ( this->Orientation == VTK_ORIENT_VERTICAL )
      {
      targetWidth = (int)(0.6*size[0]);
      targetHeight = (int)(0.86*size[1]/this->NumberOfLabels);
      }
    else
      {
      targetWidth = (int)(size[0]*0.8/this->NumberOfLabels);
      targetHeight = (int)(0.25*size[1]);
      }
Sebastien Barre's avatar
Sebastien Barre committed
626
627
628
629
630
631
632

    vtkTextMapper::SetMultipleConstrainedFontSize(viewport, 
                                                  targetWidth, 
                                                  targetHeight,
                                                  this->TextMappers,
                                                  this->NumberOfLabels,
                                                  labelSize);
Ken Martin's avatar
Ken Martin committed
633
634
635
    }
}

Sebastien Barre's avatar
Sebastien Barre committed
636
637
638
//----------------------------------------------------------------------------
void vtkScalarBarActor::SizeTitle(int *titleSize, 
                                  int *size, 
Ken Martin's avatar
Ken Martin committed
639
640
                                  vtkViewport *viewport)
{
Sebastien Barre's avatar
Sebastien Barre committed
641
642
643
644
645
646
647
  titleSize[0] = titleSize[1] = 0;

  if (this->Title == NULL || !strlen(this->Title))
    {
    return;
    }

Ken Martin's avatar
Ken Martin committed
648
649
  int targetWidth, targetHeight;
  
Sebastien Barre's avatar
Sebastien Barre committed
650
651
  targetWidth = size[0];
  if ( this->Orientation == VTK_ORIENT_VERTICAL )
Ken Martin's avatar
Ken Martin committed
652
    {
Sebastien Barre's avatar
Sebastien Barre committed
653
654
655
656
657
    targetHeight = (int)(0.1*size[1]);
    }
  else
    {
    targetHeight = (int)(0.25*size[1]);
Ken Martin's avatar
Ken Martin committed
658
    }
Sebastien Barre's avatar
Sebastien Barre committed
659
660
661
662
663
664

  this->TitleMapper->SetConstrainedFontSize(
    viewport, targetWidth, targetHeight);

  this->TitleMapper->GetSize(viewport, titleSize);
}