vtkPolarAxesActor.cxx 24.9 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
/*=========================================================================

  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"
25
26
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
27
28
#include "vtkProperty.h"
#include "vtkStringArray.h"
29
#include "vtkTextProperty.h"
30
31
#include "vtkViewport.h"

32
#include <vtksys/ios/sstream>
33
34
35

vtkStandardNewMacro(vtkPolarAxesActor);
vtkCxxSetObjectMacro(vtkPolarAxesActor, Camera,vtkCamera);
36
37
vtkCxxSetObjectMacro(vtkPolarAxesActor,PolarAxisLabelTextProperty,vtkTextProperty);
vtkCxxSetObjectMacro(vtkPolarAxesActor,PolarAxisTitleTextProperty,vtkTextProperty);
38
39

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

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

Philippe Pébay's avatar
Philippe Pébay committed
52
53
  os << indent << "ScreenSize: (" << this->ScreenSize << ")\n";

54
55
56
57
58
  os << indent << "Number Of Radial Axes" << this->NumberOfRadialAxes << endl;

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

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

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

77
78
79
  os << indent << "Rebuild Axes: "
     << ( this->RebuildAxes ? "On\n" : "Off\n" );

80

Philippe Pébay's avatar
Philippe Pébay committed
81
82
83
  os << indent << "Polar Axis Title: " << this->PolarAxisTitle << "\n";
  os << indent << "PolarAxisLabelTextProperty: " << this->PolarAxisLabelTextProperty << endl;
  os << indent << "PolarAxisTitleTextProperty: " << this->PolarAxisTitleTextProperty << endl;
84
85

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

88
89
  os << indent << "Radial Title Visibility: "
     << ( this->RadialTitleVisibility ? "On" : "Off" ) << endl;
90

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

94
95
96
97
  os << indent << "Polar Tick Visibility: "
     << ( this->PolarTickVisibility ? "On" : "Off" ) << endl;

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

99
  os << indent << "Tick Location: " << this->TickLocation << endl;
100
101
102
103
104
}

// *************************************************************************
vtkPolarAxesActor::vtkPolarAxesActor() : vtkActor()
{
105
106
107
108
109
  // 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;

110
  // Default pole coordinates
111
112
113
  this->Pole[0] = VTK_DOUBLE_MAX;
  this->Pole[1] = VTK_DOUBLE_MAX;
  this->Pole[2] = VTK_DOUBLE_MAX;
114
115

  // Default number of polar axes
116
  this->NumberOfRadialAxes = VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES;
117
118
119
120

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

121
122
123
  // Do not auto-scale radius by default
  this->AutoScaleRadius = false;

124
125
126
127
128
  // Default maximum polar angle
  this->MaximumAngle = VTK_DEFAULT_MAXIMUM_POLAR_ANGLE;

  // By default show angle units (degrees)
  this->RadialUnits = 1;
129
130
131
132
133
134
135

  this->RebuildAxes = false;

  this->Camera = NULL;

  this->ScreenSize = 10.0;

136
137
138
139
  // 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
140
141
142
143
144
145
146
147
  // 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();
148
  this->PolarAxisLabelTextProperty->SetColor( .8, 0. ,0. );
Philippe Pébay's avatar
Philippe Pébay committed
149
150
  this->PolarAxisLabelTextProperty->SetFontFamilyToArial();

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

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

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

  // Default tick location, defined in vtkAxisActor
175
  this->TickLocation = VTK_TICKS_BOTH;
176
177
178

  // By default all features are visible
  this->RadialAxesVisibility = 1;
179
  this->RadialTitleVisibility = 1;
180
181
  this->PolarLabelVisibility = 1;
  this->PolarTickVisibility = 1;
182

183
184
185
186
  // Default title for polar axis (can also be called "Radius")
  this->PolarAxisTitle = new char[16];
  sprintf(this->PolarAxisTitle, "%s", "Radial Distance");

187
  this->RadialLabelFormat = new char[8];
188
  sprintf( this->RadialLabelFormat, "%s", "%-#6.3g" );
189
190
191
192
193
194
195
196
197
198

  this->RenderCount = 0;

  this->RenderSomething = 0;

  this->UserRadialPow = 0;

  this->AutoLabelScaling = true;

  this->LabelScale = -1.0;
199
  this->TitleScale = -1.0;
200
201
202
203
204
205
206
}

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

Philippe Pébay's avatar
Philippe Pébay committed
207
  if ( this->RadialAxesProperty )
208
209
210
211
    {
    this->RadialAxesProperty->Delete();
    }

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

Philippe Pébay's avatar
Philippe Pébay committed
218
  if ( this->PolarAxisTitle )
219
220
221
222
223
    {
    delete [] this->PolarAxisTitle;
    this->PolarAxisTitle = NULL;
    }

Philippe Pébay's avatar
Philippe Pébay committed
224
225
226
227
228
229
230
231
232
233
234
235
  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
236
237
  if ( this->RadialAxes )
    {
238
    for ( int i = 0; i < VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES; ++ i )
Philippe Pébay's avatar
Philippe Pébay committed
239
240
241
242
243
244
245
246
247
248
      {
      if ( this->RadialAxes[i] )
        {
        this->RadialAxes[i]->Delete();
        this->RadialAxes[i] = NULL;
        }
      }
    delete [] this->RadialAxes;
    this->RadialAxes = NULL;
    }
249
250
251
}

// ****************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
252
void vtkPolarAxesActor::ShallowCopy( vtkPolarAxesActor *actor )
253
{
Philippe Pébay's avatar
Philippe Pébay committed
254
  this->Superclass::ShallowCopy( actor );
Philippe Pébay's avatar
Philippe Pébay committed
255
256
257
258
259
260

  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
261
  this->SetRadialLabelFormat( actor->GetRadialLabelFormat() );
Philippe Pébay's avatar
Philippe Pébay committed
262
263
  this->SetMaximumAngle( actor->GetMaximumAngle() );
  
Philippe Pébay's avatar
Philippe Pébay committed
264
  this->SetCamera( actor->GetCamera() );
Philippe Pébay's avatar
Philippe Pébay committed
265
266

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

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
276
int vtkPolarAxesActor::RenderOpaqueGeometry( vtkViewport *viewport )
277
278
279
{
  // Initialization
  static bool initialRender = true;
280
  if ( !this->Camera )
281
282
283
284
285
286
287
288
289
290
291
292
    {
    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
293
      this->RadialAxes[i]->BuildAxis( viewport, true );
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
      }
    }
  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
316
void vtkPolarAxesActor::SetScreenSize( double screenSize )
317
318
{
  this->ScreenSize = screenSize;
319
320
  // Considering pivot point at center of the geometry,
  // hence ( this->ScreenSize * 0.5 ).
321
322
323
324
  this->LabelScreenOffset = 20.0 + this->ScreenSize * 0.5;

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

  this->Modified();
}

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

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

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
357
void vtkPolarAxesActor::GetBounds( double& xmin, double& xmax,
358
                                 double& ymin, double& ymax,
Philippe Pébay's avatar
Philippe Pébay committed
359
                                 double& zmin, double& zmax )
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
{
  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;
}

375
// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
376
void vtkPolarAxesActor::TransformBounds( vtkViewport *viewport,
Philippe Pébay's avatar
Philippe Pébay committed
377
                                         double bounds[6] )
378
{
Philippe Pébay's avatar
Philippe Pébay committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  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];
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
}

// ****************************************************************************
//  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
415
int vtkPolarAxesActor::LabelExponent( double min, double max )
416
{
Philippe Pébay's avatar
Philippe Pébay committed
417
  if ( min == max )
418
419
420
421
422
423
424
    {
    return 0;
    }

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

  //
  // 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
435
436
  double cut_min = pow( 10., eformat_cut_min );
  double cut_max = pow( 10., eformat_cut_max );
437
  double ipow10;
Philippe Pébay's avatar
Philippe Pébay committed
438
  if ( range < cut_min || range > cut_max )
439
440
441
442
443
    {
    //
    // 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
444
    ipow10 = ( floor( floor( pow10 )/3.) )*3;
445
446
447
448
449
450
    }
  else
    {
    ipow10 = 0;
    }

Philippe Pébay's avatar
Philippe Pébay committed
451
  return static_cast<int>( ipow10 );
452
453
454
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
455
void vtkPolarAxesActor::BuildAxes( vtkViewport *viewport )
456
457
458
{
  double bounds[6];

Philippe Pébay's avatar
Philippe Pébay committed
459
  if ( ( this->GetMTime() < this->BuildTime.GetMTime() ))
460
    {
Philippe Pébay's avatar
Philippe Pébay committed
461
    this->AutoScale( viewport );
462
463
464
465
    return;
    }

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

467
  // Determine the bounds for possible use ( input, prop, or user-defined )
Philippe Pébay's avatar
Philippe Pébay committed
468
  this->GetBounds( bounds );
469
470
471
472
473
474
475
  
  // 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];
    }
476
477
478
479
480
481
482
483
484
  
  // 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;
    }
485

486
  // Prepare axes for rendering with user-definable options
487
  double dAlpha =  this->MaximumAngle / ( this->NumberOfRadialAxes - 1. );
488

489
  // Set radial axes
Philippe Pébay's avatar
Philippe Pébay committed
490
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++ i )
491
    {
492
    double theta = i * dAlpha;
Philippe Pébay's avatar
Philippe Pébay committed
493
494
    double thetaRad = vtkMath::RadiansFromDegrees( theta );
    vtkAxisActor* axis = this->RadialAxes[i];
495
496
    double x = o[0] + this->MaximumRadius * cos( thetaRad );
    double y = o[1] + this->MaximumRadius * sin( thetaRad );
497
498
    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
499

500
    // Set axis ticks
501
502
    axis->SetRange( 0., this->MaximumRadius );
    axis->SetMajorTickSize( .02 * this->MaximumRadius );
503

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

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

527
528
  // Build polar axis labels with 0.01 zero-threshold for labels
  this->BuildPolarAxisLabels( .01 );
529

530
531
532
533
534
535
536
537
538
539
540
  // 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
541
542
  prop->SetAmbient( 1.0 );
  prop->SetDiffuse( 0.0 );
543
544
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    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
559
560
      axis->SetTitleTextProperty( this->GetPolarAxisTitleTextProperty() );
      axis->SetLabelTextProperty( this->GetPolarAxisLabelTextProperty() );
561
562
563
564
565
      }
    else
      {
      axis->SetLabelVisibility( 0 );
      axis->SetTickVisibility( 0 );
566
      axis->GetTitleTextProperty()->SetColor( this->GetRadialAxesProperty()->GetColor() );
567
      }
568
569
570
571
    }
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
572
double vtkPolarAxesActor::MaxOf( double a, double b )
573
{
Philippe Pébay's avatar
Philippe Pébay committed
574
  return ( a > b ? a : b );
575
576
577
}

// *************************************************************************
Philippe Pébay's avatar
Philippe Pébay committed
578
inline double vtkPolarAxesActor::FFix( double value )
579
{
Philippe Pébay's avatar
Philippe Pébay committed
580
  int ivalue = static_cast<int>( value );
581
582
583
584
  return ivalue;
}

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

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

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

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

615
  double fxt = pow( 10., this->FFix( pow10 ) );
616
617

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

623
  double div = 1.;
Philippe Pébay's avatar
Philippe Pébay committed
624
  if ( numTicks < 5 )
625
626
627
    {
    div = 2.;
    }
Philippe Pébay's avatar
Philippe Pébay committed
628
  if ( numTicks <= 2 )
629
630
631
632
633
634
    {
    div = 5.;
    }

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

641
  // Figure out the first major tick locations, relative to the
642
  // start of the axis.
643
644
645
646
647
648
649
  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
650
  if ( sortedRange[0] <= 0.)
651
    {
652
    majorStart = major * ( this->FFix( sortedRange[0]*( 1./major ) ) + 0. );
653
654
655
    }
  else
    {
656
    majorStart = major * ( this->FFix( sortedRange[0]*( 1./major ) ) + 1. );
657
658
    }

659
660
  axis->SetMajorRangeStart( majorStart );
  axis->SetDeltaRangeMajor( major );
661

662
  double t = ( majorStart - sortedRange[0] ) / range;
663
  majorStart = t * range  + origin;
664

665
666
667
  // Set major start and deltas
  axis->SetMajorStart( VTK_AXIS_TYPE_X, majorStart );
  axis->SetDeltaMajor( VTK_AXIS_TYPE_X, major );
668
669
670
}

// ****************************************************************
671
void vtkPolarAxesActor::BuildPolarAxisLabels(  double zeroThreshold )
672
{
673
674
675
676
677
678
  // 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];
679
680
  vtkIdType nLabels = 0;
  while ( val <= lastVal && nLabels < VTK_MAX_LABELS )
681
    {
682
    ++ nLabels;
683
684
    val += deltaMajor;
    }
685
  vtkStringArray *labels = vtkStringArray::New();
686
  labels->SetNumberOfValues( nLabels );
687

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

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

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

  // Clean up
742
  labels->Delete();
743
744
745
746
747
748
749
750

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

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

764
765
766
767
768
769
770
771
772
773
774
775
776
777
// ****************************************************************
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
778
779
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
780
781
782
783
784
785
786
787
    // Scale title
    newTitleScale = this->AutoScale( viewport, 
                                     this->ScreenSize,
                                     axis[i]->GetTitleActor()->GetPosition() );

    axis[i]->SetTitleScale( newTitleScale );

    // Scale labels
788
    vtkAxisFollower** labelActors = axis[i]->GetLabelActors();
789
790

    for( int j = 0; j < axis[i]->GetNumberOfLabelsBuilt(); ++ j )
791
      {
792
793
794
795
796
      double newLabelScale = this->AutoScale( viewport, 
                                              this->ScreenSize,
                                              labelActors[j]->GetPosition() );

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

// ****************************************************************
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;
}

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

// ****************************************************************************
vtkProperty* vtkPolarAxesActor::GetRadialAxesProperty()
{
  return this->RadialAxesProperty;
}
834
835
836
837
838
839
840
841
842
843
844
845
846

// ****************************************************************************
void vtkPolarAxesActor::SetPolarArcsProperty( vtkProperty *prop )
{
  this->PolarArcsActor->SetProperty(prop);
  this->Modified();
}

// ****************************************************************************
vtkProperty* vtkPolarAxesActor::GetPolarArcsProperty()
{
  return this->PolarArcsActor->GetProperty();
}