vtkPolarAxesActor.cxx 24.4 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPolarAxesActor.cxx
  Thanks:    Kathleen Bonnell, B Division, Lawrence Livermore National Lab

  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.

 =========================================================================*/
#include "vtkPolarAxesActor.h"

#include "vtkAxisActor.h"
#include "vtkAxisFollower.h"
#include "vtkCamera.h"
#include "vtkCoordinate.h"
#include "vtkFollower.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkProperty.h"
#include "vtkStringArray.h"
27
#include "vtkTextProperty.h"
28
29
#include "vtkViewport.h"

30
#include <vtksys/ios/sstream>
31
32
33

vtkStandardNewMacro(vtkPolarAxesActor);
vtkCxxSetObjectMacro(vtkPolarAxesActor, Camera,vtkCamera);
34
35
vtkCxxSetObjectMacro(vtkPolarAxesActor,PolarAxisLabelTextProperty,vtkTextProperty);
vtkCxxSetObjectMacro(vtkPolarAxesActor,PolarAxisTitleTextProperty,vtkTextProperty);
36
37

// ******************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
38
void vtkPolarAxesActor::PrintSelf( ostream& os, vtkIndent indent )
39
{
Philippe Pébay's avatar
Philippe Pébay committed
40
  this->Superclass::PrintSelf( os,indent );
41

42
43
  os << indent << "Bounds: \n";
  os << indent << "  Xmin,Xmax: (" << this->Bounds[0] << ", "
44
     << this->Bounds[1] << " )\n";
45
  os << indent << "  Ymin,Ymax: (" << this->Bounds[2] << ", "
46
     << this->Bounds[3] << " )\n";
47
  os << indent << "  Zmin,Zmax: (" << this->Bounds[4] << ", "
48
     << this->Bounds[5] << " )\n";
49
50
51
52
53
54

  os << indent << "Number Of Radial Axes" << this->NumberOfRadialAxes << endl;

  os << indent << "Pole: (" 
     << this->Pole[0] << ", "
     << this->Pole[1] << ", "
55
     << this->Pole[2] << " )\n";
56

57
  os << indent << "Maximum Radius" << this->MaximumRadius << endl;
58
  os << indent << "Auto-Scale Radius" << this->AutoScaleRadius << endl;
59
  os << indent << "Maximum Angle" << this->MaximumAngle << endl;
Philippe Pébay's avatar
Philippe Pébay committed
60
61
  os << indent << "Radial Units (degrees): "
     << ( this->RadialUnits ? "On\n" : "Off\n" ) << endl;
62

Philippe Pébay's avatar
Philippe Pébay committed
63
  if ( this->Camera )
64
65
    {
    os << indent << "Camera:\n";
Philippe Pébay's avatar
Philippe Pébay committed
66
    this->Camera->PrintSelf( os,indent.GetNextIndent() );
67
68
69
70
71
72
    }
  else
    {
    os << indent << "Camera: (none)\n";
    }

73
74
75
  os << indent << "Rebuild Axes: "
     << ( this->RebuildAxes ? "On\n" : "Off\n" );

76

Philippe Pébay's avatar
Philippe Pébay committed
77
78
79
  os << indent << "Polar Axis Title: " << this->PolarAxisTitle << "\n";
  os << indent << "PolarAxisLabelTextProperty: " << this->PolarAxisLabelTextProperty << endl;
  os << indent << "PolarAxisTitleTextProperty: " << this->PolarAxisTitleTextProperty << endl;
80
81

  os << indent << "Radial Axes Visibility: "
82
     << ( this->RadialAxesVisibility ? "On\n" : "Off\n" );
83

84
85
  os << indent << "Radial Title Visibility: "
     << ( this->RadialTitleVisibility ? "On" : "Off" ) << endl;
86

87
88
  os << indent << "Polar Label Visibility: "
     << ( this->PolarLabelVisibility ? "On" : "Off" ) << endl;
89

90
91
92
93
  os << indent << "Polar Tick Visibility: "
     << ( this->PolarTickVisibility ? "On" : "Off" ) << endl;

  os << indent << "Radial Axes Label Format: " << this->RadialLabelFormat << "\n";
94

95
  os << indent << "Tick Location: " << this->TickLocation << endl;
96
97
98
99
100
}

// *************************************************************************
vtkPolarAxesActor::vtkPolarAxesActor() : vtkActor()
{
101
102
103
104
105
  // Default bounds
  this->Bounds[0] = -1.0; this->Bounds[1] = 1.0;
  this->Bounds[2] = -1.0; this->Bounds[3] = 1.0;
  this->Bounds[4] = -1.0; this->Bounds[5] = 1.0;

106
  // Default pole coordinates
107
108
109
  this->Pole[0] = VTK_DOUBLE_MAX;
  this->Pole[1] = VTK_DOUBLE_MAX;
  this->Pole[2] = VTK_DOUBLE_MAX;
110
111

  // Default number of polar axes
112
  this->NumberOfRadialAxes = VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES;
113
114
115
116

  // Default maximum polar radius
  this->MaximumRadius = VTK_DOUBLE_MAX;

117
118
119
  // Do not auto-scale radius by default
  this->AutoScaleRadius = false;

120
121
122
123
124
  // Default maximum polar angle
  this->MaximumAngle = VTK_DEFAULT_MAXIMUM_POLAR_ANGLE;

  // By default show angle units (degrees)
  this->RadialUnits = 1;
125
126
127
128
129
130
131

  this->RebuildAxes = false;

  this->Camera = NULL;

  this->ScreenSize = 10.0;

132
133
134
135
  // Considering pivot point at center of the geometry,
  // hence ( this->ScreenSize * 0.5 ).
  this->LabelScreenOffset = 20.0 + this->ScreenSize * 0.5;

Philippe Pébay's avatar
Philippe Pébay committed
136
137
138
139
140
141
142
143
  // Properties of the radial axes
  this->RadialAxesProperty = vtkProperty::New();

  // Text properties of polar axis title and labels, with default red color
  this->PolarAxisTitleTextProperty = vtkTextProperty::New();
  this->PolarAxisTitleTextProperty->SetColor( 1., 0. ,0. );
  this->PolarAxisTitleTextProperty->SetFontFamilyToArial();
  this->PolarAxisLabelTextProperty = vtkTextProperty::New();
144
  this->PolarAxisLabelTextProperty->SetColor( .8, 0. ,0. );
Philippe Pébay's avatar
Philippe Pébay committed
145
146
  this->PolarAxisLabelTextProperty->SetFontFamilyToArial();

147
  // Create and set radial axes
148
149
  this->RadialAxes = new vtkAxisActor*[VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES];
  for ( int i = 0; i < VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES; ++ i )
150
    {
151
    // Create axis of type X
152
    this->RadialAxes[i] = vtkAxisActor::New();
153
154
155
156
157
158
    vtkAxisActor* axis = this->RadialAxes[i];
    axis->SetAxisTypeToX();

    // Title offset is auto-calculated for now
    axis->SetCalculateTitleOffset( 1 );
    axis->SetCalculateLabelOffset( 0 );
159
160

    // Pass information to axes followers
161
162
    vtkAxisFollower* follower = axis->GetTitleActor();
    follower->SetAxis( axis );
163
164
    if ( ! i )
      {
Philippe Pébay's avatar
Philippe Pébay committed
165
      // Replace default screen offset (for polar axis only)
166
167
      follower->SetScreenOffset( this->LabelScreenOffset + this->ScreenSize * 0.5 );
      }
168
169
170
    }

  // Default tick location, defined in vtkAxisActor
171
  this->TickLocation = VTK_TICKS_BOTH;
172
173
174

  // By default all features are visible
  this->RadialAxesVisibility = 1;
175
  this->RadialTitleVisibility = 1;
176
177
  this->PolarLabelVisibility = 1;
  this->PolarTickVisibility = 1;
178

179
180
181
182
  // Default title for polar axis (can also be called "Radius")
  this->PolarAxisTitle = new char[16];
  sprintf(this->PolarAxisTitle, "%s", "Radial Distance");

183
  this->RadialLabelFormat = new char[8];
184
  sprintf( this->RadialLabelFormat, "%s", "%-#6.3g" );
185
186
187
188
189
190
191
192
193
194

  this->RenderCount = 0;

  this->RenderSomething = 0;

  this->UserRadialPow = 0;

  this->AutoLabelScaling = true;

  this->LabelScale = -1.0;
195
  this->TitleScale = -1.0;
196
197
198
199
200
201
202
}

// ****************************************************************************
vtkPolarAxesActor::~vtkPolarAxesActor()
{
  this->SetCamera( NULL );

Philippe Pébay's avatar
Philippe Pébay committed
203
  if ( this->RadialAxesProperty )
204
205
206
207
    {
    this->RadialAxesProperty->Delete();
    }

Philippe Pébay's avatar
Philippe Pébay committed
208
  if ( this->RadialLabelFormat )
209
210
211
212
    {
    delete [] this->RadialLabelFormat;
    this->RadialLabelFormat = NULL;
    }
Philippe Pébay's avatar
Philippe Pébay committed
213

Philippe Pébay's avatar
Philippe Pébay committed
214
  if ( this->PolarAxisTitle )
215
216
217
218
219
    {
    delete [] this->PolarAxisTitle;
    this->PolarAxisTitle = NULL;
    }

Philippe Pébay's avatar
Philippe Pébay committed
220
221
222
223
224
225
226
227
228
229
230
231
  if ( this->PolarAxisTitleTextProperty )
    {
    this->PolarAxisTitleTextProperty->Delete();
    this->PolarAxisTitleTextProperty = NULL;
    }

  if ( this->PolarAxisLabelTextProperty )
    {
    this->PolarAxisLabelTextProperty->Delete();
    this->PolarAxisLabelTextProperty = NULL;
    }

Philippe Pébay's avatar
Philippe Pébay committed
232
233
  if ( this->RadialAxes )
    {
234
    for ( int i = 0; i < VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES; ++ i )
Philippe Pébay's avatar
Philippe Pébay committed
235
236
237
238
239
240
241
242
243
244
      {
      if ( this->RadialAxes[i] )
        {
        this->RadialAxes[i]->Delete();
        this->RadialAxes[i] = NULL;
        }
      }
    delete [] this->RadialAxes;
    this->RadialAxes = NULL;
    }
245
246
247
}

// ****************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
248
void vtkPolarAxesActor::ShallowCopy( vtkPolarAxesActor *actor )
249
{
Philippe Pébay's avatar
Philippe Pébay committed
250
  this->Superclass::ShallowCopy( actor );
Philippe Pébay's avatar
Philippe Pébay committed
251
252
253
254
255
256

  this->SetBounds( actor->GetBounds() );
  this->SetPole( actor->GetPole() );
  this->SetMaximumRadius( actor->GetMaximumRadius() );
  this->SetAutoScaleRadius( actor->GetAutoScaleRadius() );
  this->SetRadialUnits( actor->GetRadialUnits() );
Philippe Pébay's avatar
Philippe Pébay committed
257
  this->SetRadialLabelFormat( actor->GetRadialLabelFormat() );
Philippe Pébay's avatar
Philippe Pébay committed
258
259
  this->SetMaximumAngle( actor->GetMaximumAngle() );
  
Philippe Pébay's avatar
Philippe Pébay committed
260
  this->SetCamera( actor->GetCamera() );
Philippe Pébay's avatar
Philippe Pébay committed
261
262

  this->SetPolarAxisTitle( actor->GetPolarAxisTitle() );
263
264
  this->LabelScreenOffset = actor->LabelScreenOffset;
  this->ScreenSize        = actor->ScreenSize;
265
  this->LabelScreenOffset = actor->LabelScreenOffset;
Philippe Pébay's avatar
Philippe Pébay committed
266
267
268
  this->SetRadialLabelFormat( actor->GetRadialLabelFormat() );
  this->SetPolarAxisTitleTextProperty( actor->GetPolarAxisTitleTextProperty() );
  this->SetPolarAxisLabelTextProperty( actor->GetPolarAxisLabelTextProperty() );
269
270
271
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
272
int vtkPolarAxesActor::RenderOpaqueGeometry( vtkViewport *viewport )
273
274
275
{
  // Initialization
  static bool initialRender = true;
276
  if ( !this->Camera )
277
278
279
280
281
282
283
284
285
286
287
288
    {
    vtkErrorMacro( <<"No camera!" );
    this->RenderSomething = 0;
    return 0;
    }

  this->BuildAxes( viewport );

  if ( initialRender || this->RebuildAxes )
    {
    for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
      {
Philippe Pébay's avatar
Philippe Pébay committed
289
      this->RadialAxes[i]->BuildAxis( viewport, true );
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
      }
    }
  initialRender = false;
  this->RebuildAxes = false;

  //Render the axes
  int renderedSomething = 0;
  if ( this->RadialAxesVisibility )
    {
    for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
      {
      renderedSomething +=
        this->RadialAxes[i]->RenderOpaqueGeometry( viewport );
      }
    }

  return renderedSomething;
}

// *************************************************************************
// Screen size affects the screen offset as well.
// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
312
void vtkPolarAxesActor::SetScreenSize( double screenSize )
313
314
{
  this->ScreenSize = screenSize;
315
316
  // Considering pivot point at center of the geometry,
  // hence ( this->ScreenSize * 0.5 ).
317
318
319
320
  this->LabelScreenOffset = 20.0 + this->ScreenSize * 0.5;

  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
321
322
323
324
325
    vtkAxisActor* axis = this->RadialAxes[i];
  
    int numberOfLabelsBuilt = axis->GetNumberOfLabelsBuilt();
    vtkAxisFollower** labelActors = axis->GetLabelActors();
    for( int k=0; k < numberOfLabelsBuilt; ++k )
326
      {
Philippe Pébay's avatar
Philippe Pébay committed
327
      labelActors[k]->SetScreenOffset( this->LabelScreenOffset );
328
329
330
331
332
333
334
      }
    }

  this->Modified();
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
335
void vtkPolarAxesActor::ReleaseGraphicsResources( vtkWindow *win )
336
337
338
{
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++i )
    {
Philippe Pébay's avatar
Philippe Pébay committed
339
    this->RadialAxes[i]->ReleaseGraphicsResources( win );
340
341
342
    }
}

343
// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
344
void vtkPolarAxesActor::GetBounds( double bounds[6])
345
{
Philippe Pébay's avatar
Philippe Pébay committed
346
  for ( int i=0; i< 6; i++)
347
348
349
350
351
352
    {
    bounds[i] = this->Bounds[i];
    }
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
353
void vtkPolarAxesActor::GetBounds( double& xmin, double& xmax,
354
                                 double& ymin, double& ymax,
Philippe Pébay's avatar
Philippe Pébay committed
355
                                 double& zmin, double& zmax )
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
{
  xmin = this->Bounds[0];
  xmax = this->Bounds[1];
  ymin = this->Bounds[2];
  ymax = this->Bounds[3];
  zmin = this->Bounds[4];
  zmax = this->Bounds[5];
}

// *************************************************************************
double *vtkPolarAxesActor::GetBounds()
{
  return this->Bounds;
}

371
// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
372
void vtkPolarAxesActor::TransformBounds( vtkViewport *viewport,
Philippe Pébay's avatar
Philippe Pébay committed
373
                                         double bounds[6] )
374
{
Philippe Pébay's avatar
Philippe Pébay committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  double minPt[3], maxPt[3], transMinPt[3], transMaxPt[3];
  minPt[0] = this->Bounds[0];
  minPt[1] = this->Bounds[2];
  minPt[2] = this->Bounds[4];
  maxPt[0] = this->Bounds[1];
  maxPt[1] = this->Bounds[3];
  maxPt[2] = this->Bounds[5];

  viewport->SetWorldPoint(minPt[0], minPt[1], minPt[2], 1.0);
  viewport->WorldToDisplay();
  viewport->GetDisplayPoint(transMinPt);
  viewport->SetWorldPoint(maxPt[0], maxPt[1], maxPt[2], 1.0);
  viewport->WorldToDisplay();
  viewport->GetDisplayPoint(transMaxPt);

  bounds[0] = transMinPt[0];
  bounds[2] = transMinPt[1];
  bounds[4] = transMinPt[2];
  bounds[1] = transMaxPt[0];
  bounds[3] = transMaxPt[1];
  bounds[5] = transMaxPt[2];
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
}

// ****************************************************************************
//  Method: LabelExponent
//
//  Purpose:
//      Determines the proper exponent for the min and max values.
//
//  Arguments:
//      min     The minimum value along a certain axis.
//      max     The maximum value along a certain axis.
//
//  Note:       This code is mostly stolen from old MeshTV code,
//              /meshtvx/toolkit/plotgrid.c, axlab_format.
// ****************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
411
int vtkPolarAxesActor::LabelExponent( double min, double max )
412
{
Philippe Pébay's avatar
Philippe Pébay committed
413
  if ( min == max )
414
415
416
417
418
419
420
    {
    return 0;
    }

  //
  // Determine power of 10 to scale axis labels to.
  //
Philippe Pébay's avatar
Philippe Pébay committed
421
422
  double range = ( fabs( min ) > fabs( max ) ? fabs( min ) : fabs( max ) );
  double pow10 = log10( range );
423
424
425
426
427
428
429
430

  //
  // Cutoffs for using scientific notation.  The following 4 variables
  // should all be static for maximum performance but were made non-static
  // to get around a compiler bug with the MIPSpro 7.2.1.3 compiler.
  //
  double eformat_cut_min = -1.5;
  double eformat_cut_max =  3.0;
Philippe Pébay's avatar
Philippe Pébay committed
431
432
  double cut_min = pow( 10., eformat_cut_min );
  double cut_max = pow( 10., eformat_cut_max );
433
  double ipow10;
Philippe Pébay's avatar
Philippe Pébay committed
434
  if ( range < cut_min || range > cut_max )
435
436
437
438
439
    {
    //
    // We are going to use scientific notation and round the exponents to
    // the nearest multiple of three.
    //
Philippe Pébay's avatar
Philippe Pébay committed
440
    ipow10 = ( floor( floor( pow10 )/3.) )*3;
441
442
443
444
445
446
    }
  else
    {
    ipow10 = 0;
    }

Philippe Pébay's avatar
Philippe Pébay committed
447
  return static_cast<int>( ipow10 );
448
449
450
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
451
void vtkPolarAxesActor::BuildAxes( vtkViewport *viewport )
452
453
454
{
  double bounds[6];

Philippe Pébay's avatar
Philippe Pébay committed
455
  if ( ( this->GetMTime() < this->BuildTime.GetMTime() ))
456
    {
Philippe Pébay's avatar
Philippe Pébay committed
457
    this->AutoScale( viewport );
458
459
460
461
    return;
    }

  this->SetNonDependentAttributes();
Philippe Pébay's avatar
Philippe Pébay committed
462

463
  // Determine the bounds for possible use ( input, prop, or user-defined )
Philippe Pébay's avatar
Philippe Pébay committed
464
  this->GetBounds( bounds );
465
466
467
468
469
470
471
  
  // If pole coordinates are invalid, use bounds
  double o[3];
  for ( int i = 0; i < 3; ++ i )
    {
    o[i] = this->Pole[i] == VTK_DOUBLE_MAX ? bounds[i * 2] : this->Pole[i];
    }
472
473
474
475
476
477
478
479
480
  
  // If axial scale it out of proportions with object length scale, reset to ls
  double ls = fabs( bounds[1] -  bounds[0] ) + fabs( bounds[3] -  bounds[2] );
  if ( this->AutoScaleRadius
       || this->MaximumRadius < 1.e-6 * ls 
       || this->MaximumRadius > 1.e6 * ls )
    {
    this->MaximumRadius = .5 * ls;
    }
481

482
  // Prepare axes for rendering with user-definable options
483
  double dAlpha =  this->MaximumAngle / ( this->NumberOfRadialAxes - 1. );
484

485
  // Set radial axes
Philippe Pébay's avatar
Philippe Pébay committed
486
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++ i )
487
    {
488
    double theta = i * dAlpha;
Philippe Pébay's avatar
Philippe Pébay committed
489
490
    double thetaRad = vtkMath::RadiansFromDegrees( theta );
    vtkAxisActor* axis = this->RadialAxes[i];
491
492
    double x = o[0] + this->MaximumRadius * cos( thetaRad );
    double y = o[1] + this->MaximumRadius * sin( thetaRad );
493
494
    axis->GetPoint1Coordinate()->SetValue( o[0], o[1], o[2] );
    axis->GetPoint2Coordinate()->SetValue( x, y, o[2] );
Philippe Pébay's avatar
Philippe Pébay committed
495

496
    // Set axis ticks
497
498
    axis->SetRange( 0., this->MaximumRadius );
    axis->SetMajorTickSize( .02 * this->MaximumRadius );
499

500
    // Set axis title
Philippe Pébay's avatar
Philippe Pébay committed
501
    if ( i )
502
      {
503
      // Use polar angle as title for non-polar axes
Philippe Pébay's avatar
Philippe Pébay committed
504
505
506
507
508
509
      vtksys_ios::ostringstream thetaStream;
      thetaStream << theta;
      if ( this->RadialUnits )
        {
        thetaStream << " deg.";
        }
510

Philippe Pébay's avatar
Philippe Pébay committed
511
      axis->SetTitle( thetaStream.str().c_str() );
512
      }
513
514
    else // if ( i ) 
      {
Philippe Pébay's avatar
Philippe Pébay committed
515
      // Special case of polar axis
516
517
      axis->SetTitle( this->PolarAxisTitle );
      }
518
    }
Philippe Pébay's avatar
Philippe Pébay committed
519
  
520
  // Build polar axis tick, with respect to specified x-origin
521
  this->BuildPolarAxisTicks( o[0] );
522

523
524
  // Build polar axis labels with 0.01 zero-threshold for labels
  this->BuildPolarAxisLabels( .01 );
525

526
527
528
529
530
531
532
533
534
535
536
  // Scale appropriately.
  this->AutoScale( viewport );

  this->RenderSomething = 1;
  this->BuildTime.Modified();
}

// *************************************************************************
void vtkPolarAxesActor::SetNonDependentAttributes()
{
  vtkProperty *prop = this->GetProperty();
Philippe Pébay's avatar
Philippe Pébay committed
537
538
  prop->SetAmbient( 1.0 );
  prop->SetDiffuse( 0.0 );
539
540
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
541
542
543
544
545
546
547
548
549
550
551
552
553
554
    vtkAxisActor* axis = this->RadialAxes[i];
    axis->SetCamera( this->Camera );
    axis->SetProperty( prop );
    axis->SetAxisLinesProperty( this->RadialAxesProperty );
    axis->SetTickLocation( this->TickLocation );
    axis->SetBounds( this->Bounds );
    axis->SetAxisVisibility( this->RadialAxesVisibility );
    axis->SetTitleVisibility( this->RadialTitleVisibility );
    axis->SetMinorTicksVisible( 0 );
    // Features available only on polar axis
    if ( ! i )
      {
      axis->SetLabelVisibility( this->PolarLabelVisibility );
      axis->SetTickVisibility( this->PolarTickVisibility );
Philippe Pébay's avatar
Philippe Pébay committed
555
556
      axis->SetTitleTextProperty( this->GetPolarAxisTitleTextProperty() );
      axis->SetLabelTextProperty( this->GetPolarAxisLabelTextProperty() );
557
558
559
560
561
      }
    else
      {
      axis->SetLabelVisibility( 0 );
      axis->SetTickVisibility( 0 );
562
      axis->GetTitleTextProperty()->SetColor( this->GetRadialAxesProperty()->GetColor() );
563
      }
564
565
566
567
    }
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
568
double vtkPolarAxesActor::MaxOf( double a, double b )
569
{
Philippe Pébay's avatar
Philippe Pébay committed
570
  return ( a > b ? a : b );
571
572
573
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
574
inline double vtkPolarAxesActor::FFix( double value )
575
{
Philippe Pébay's avatar
Philippe Pébay committed
576
  int ivalue = static_cast<int>( value );
577
578
579
580
  return ivalue;
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
581
inline double vtkPolarAxesActor::FSign( double value, double sign )
582
{
Philippe Pébay's avatar
Philippe Pébay committed
583
584
  value = fabs( value );
  if ( sign < 0.)
585
586
587
588
589
590
591
    {
    value *= -1.;
    }
  return value;
}

// *******************************************************************
592
void vtkPolarAxesActor::BuildPolarAxisTicks( double origin )
593
594
{
  // Find the integral points.
595
  double range = this->MaximumRadius;
Philippe Pébay's avatar
Philippe Pébay committed
596
  double pow10 = log10( range );
597
598

  // Build in numerical tolerance
Philippe Pébay's avatar
Philippe Pébay committed
599
  if ( pow10 != 0.)
600
601
    {
    double eps = 10.0e-10;
Philippe Pébay's avatar
Philippe Pébay committed
602
    pow10 = this->FSign( ( fabs( pow10 ) + eps ), pow10 );
603
604
    }

605
  // FFix will move in the wrong direction if pow10 is negative.
Philippe Pébay's avatar
Philippe Pébay committed
606
  if ( pow10 < 0.)
607
608
609
610
    {
    pow10 = pow10 - 1.;
    }

611
  double fxt = pow( 10., this->FFix( pow10 ) );
612
613

  // Find the number of integral points in the interval.
614
  double fnt = range / fxt;
Philippe Pébay's avatar
Philippe Pébay committed
615
  fnt  = this->FFix( fnt );
616
617
  double frac = fnt;
  int numTicks = frac <= 0.5 ? static_cast<int>( this->FFix( fnt ) ) : static_cast<int>( this->FFix( fnt ) + 1 );
618

619
  double div = 1.;
Philippe Pébay's avatar
Philippe Pébay committed
620
  if ( numTicks < 5 )
621
622
623
    {
    div = 2.;
    }
Philippe Pébay's avatar
Philippe Pébay committed
624
  if ( numTicks <= 2 )
625
626
627
628
629
630
    {
    div = 5.;
    }

  // If there aren't enough major tick points in this decade, use the next
  // decade.
631
  double major = fxt;
Philippe Pébay's avatar
Philippe Pébay committed
632
  if ( div != 1.)
633
634
635
636
    {
    major /= div;
    }

637
  // Figure out the first major tick locations, relative to the
638
  // start of the axis.
639
640
641
642
643
644
645
  double sortedRange[2];
  vtkAxisActor* axis = this->RadialAxes[0];
  double *inRange = axis->GetRange();
  sortedRange[0] = inRange[0] < inRange[1] ? inRange[0] : inRange[1];
  sortedRange[1] = inRange[0] > inRange[1] ? inRange[0] : inRange[1];

  double majorStart;
Philippe Pébay's avatar
Philippe Pébay committed
646
  if ( sortedRange[0] <= 0.)
647
    {
648
    majorStart = major * ( this->FFix( sortedRange[0]*( 1./major ) ) + 0. );
649
650
651
    }
  else
    {
652
    majorStart = major * ( this->FFix( sortedRange[0]*( 1./major ) ) + 1. );
653
654
    }

655
656
  axis->SetMajorRangeStart( majorStart );
  axis->SetDeltaRangeMajor( major );
657

658
  double t = ( majorStart - sortedRange[0] ) / range;
659
  majorStart = t * range  + origin;
660

661
662
663
  // Set major start and deltas
  axis->SetMajorStart( VTK_AXIS_TYPE_X, majorStart );
  axis->SetDeltaMajor( VTK_AXIS_TYPE_X, major );
664
665
666
}

// ****************************************************************
667
void vtkPolarAxesActor::BuildPolarAxisLabels(  double zeroThreshold )
668
{
669
670
671
672
673
674
  // Calculate number of labels needed and create array for them
  vtkAxisActor* axis = this->RadialAxes[0];
  double deltaMajor = axis->GetDeltaMajor( VTK_AXIS_TYPE_X );
  double val = axis->GetMajorStart( VTK_AXIS_TYPE_X );
  const double *p2  = axis->GetPoint2Coordinate()->GetValue();
  double lastVal = p2[0];
675
676
  vtkIdType nLabels = 0;
  while ( val <= lastVal && nLabels < VTK_MAX_LABELS )
677
    {
678
    ++ nLabels;
679
680
    val += deltaMajor;
    }
681
  vtkStringArray *labels = vtkStringArray::New();
682
  labels->SetNumberOfValues( nLabels );
683

684
685
686
687
688
  // Now create labels
  val = axis->GetMajorRangeStart();
  deltaMajor = axis->GetDeltaRangeMajor();
  const char *format = this->RadialLabelFormat;
  char label[64];
689
  for ( int  i = 0; i < nLabels; ++ i )
690
    {
691
    if ( fabs( val ) < zeroThreshold && this->MaximumRadius > 1 )
692
693
694
695
696
      {
      // We just happened to fall at something near zero and the range is
      // large, so set it to zero to avoid ugliness.
      val = 0.;
      }
697
698
    sprintf( label, format, val );

699
    if ( fabs( val ) < zeroThreshold )
700
701
702
703
704
      {
      //
      // Ensure that -0.0 is never a label
      // The maximum number of digits that we allow past the decimal is 5.
      //
705
      if ( strcmp( label, "-0" ) == 0 )
706
        {
707
        sprintf( label, "0" );
708
        }
709
      else if ( strcmp( label, "-0.0" ) == 0 )
710
        {
711
        sprintf( label, "0.0" );
712
        }
713
      else if ( strcmp( label, "-0.00" ) == 0 )
714
        {
715
        sprintf( label, "0.00" );
716
        }
717
      else if ( strcmp( label, "-0.000" ) == 0 )
718
        {
719
        sprintf( label, "0.000" );
720
        }
721
      else if ( strcmp( label, "-0.0000" ) == 0 )
722
        {
723
        sprintf( label, "0.0000" );
724
        }
725
      else if ( strcmp( label, "-0.00000" ) == 0 )
726
        {
727
        sprintf( label, "0.00000" );
728
729
        }
      }
Philippe Pébay's avatar
Philippe Pébay committed
730
    labels->SetValue( i, label );
731
732
    val += deltaMajor;
    }
733
734
735
736
737

  // Store labels
  axis->SetLabels( labels );

  // Clean up
738
  labels->Delete();
739
740
741
742
743
744
745
746

  // Update axis label followers
  vtkAxisFollower** labelActors = axis->GetLabelActors();
  for( int i = 0; i < nLabels; ++i )
    {
    labelActors[i]->SetAxis( axis );
    labelActors[i]->SetScreenOffset( this->LabelScreenOffset );
    }
747
748
749
750
751
752
753
754
755
756
757
758
759
}

// ****************************************************************************
void vtkPolarAxesActor::SetLabelScaling( bool autoscale, int upow )
{
  if ( autoscale != this->AutoLabelScaling || upow != this->UserRadialPow )
    {
    this->AutoLabelScaling = autoscale;
    this->UserRadialPow = upow;
    this->Modified();
    }
}

760
761
762
763
764
765
766
767
768
769
770
771
772
773
// ****************************************************************
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport )
{
  // Current implementation only for perspective projections.
  this->AutoScale( viewport, this->RadialAxes );
}

// ****************************************************************
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport, 
                                   vtkAxisActor** axis )
{
  double newTitleScale = this->TitleScale;

  // Loop over radial axes
774
775
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
776
777
778
779
780
781
782
783
    // Scale title
    newTitleScale = this->AutoScale( viewport, 
                                     this->ScreenSize,
                                     axis[i]->GetTitleActor()->GetPosition() );

    axis[i]->SetTitleScale( newTitleScale );

    // Scale labels
784
    vtkAxisFollower** labelActors = axis[i]->GetLabelActors();
785
786

    for( int j = 0; j < axis[i]->GetNumberOfLabelsBuilt(); ++ j )
787
      {
788
789
790
791
792
      double newLabelScale = this->AutoScale( viewport, 
                                              this->ScreenSize,
                                              labelActors[j]->GetPosition() );

      labelActors[j]->SetScale( newLabelScale );
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
}

// ****************************************************************
double vtkPolarAxesActor::AutoScale( vtkViewport *viewport, 
                                     double screenSize,
                                     double position[3] )
{
  double factor = 1;
  if ( viewport->GetSize()[1] > 0 )
    {
    factor = 2.0 * screenSize
      * tan( vtkMath::RadiansFromDegrees( this->Camera->GetViewAngle()/2.0 ) )
      / viewport->GetSize()[1];
    }

    double dist = sqrt(
          vtkMath::Distance2BetweenPoints( position,
                                          this->Camera->GetPosition() ));
    double newScale = factor * dist;

    return newScale;
}

818
// ****************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
819
void vtkPolarAxesActor::SetRadialAxesProperty( vtkProperty *prop )
820
{
Philippe Pébay's avatar
Philippe Pébay committed
821
  this->RadialAxesProperty->DeepCopy( prop );
822
823
824
825
826
827
828
829
  this->Modified();
}

// ****************************************************************************
vtkProperty* vtkPolarAxesActor::GetRadialAxesProperty()
{
  return this->RadialAxesProperty;
}