vtkPolarAxesActor.cxx 27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*=========================================================================

  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"

18
#include "vtkArcSource.h"
19
20
21
#include "vtkAxisActor.h"
#include "vtkAxisFollower.h"
#include "vtkCamera.h"
22
#include "vtkCellArray.h"
23
24
25
26
#include "vtkCoordinate.h"
#include "vtkFollower.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
27
28
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
29
30
#include "vtkProperty.h"
#include "vtkStringArray.h"
31
#include "vtkTextProperty.h"
32
33
#include "vtkViewport.h"

34
#include <vtksys/ios/sstream>
35
36
37

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

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

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

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

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
58
  os << indent << "Pole: ("
59
60
     << this->Pole[0] << ", "
     << this->Pole[1] << ", "
61
     << this->Pole[2] << " )\n";
62

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

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

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

82

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

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

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

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

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

99
100
101
  os << indent << "Polar Arcs Visibility: "
     << ( this->PolarArcsVisibility ? "On" : "Off" ) << endl;

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

104
  os << indent << "Tick Location: " << this->TickLocation << endl;
105
106
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
107
//-----------------------------------------------------------------------------
108
109
vtkPolarAxesActor::vtkPolarAxesActor() : vtkActor()
{
110
111
112
113
114
  // 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;

115
  // Default pole coordinates
116
117
118
  this->Pole[0] = VTK_DOUBLE_MAX;
  this->Pole[1] = VTK_DOUBLE_MAX;
  this->Pole[2] = VTK_DOUBLE_MAX;
119
120

  // Default number of polar axes
121
  this->NumberOfRadialAxes = VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES;
122
123
124
125

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

126
127
128
  // Do not auto-scale radius by default
  this->AutoScaleRadius = false;

129
130
131
132
133
  // Default maximum polar angle
  this->MaximumAngle = VTK_DEFAULT_MAXIMUM_POLAR_ANGLE;

  // By default show angle units (degrees)
  this->RadialUnits = 1;
134
135
136
137
138
139
140

  this->RebuildAxes = false;

  this->Camera = NULL;

  this->ScreenSize = 10.0;

141
142
143
144
  // Considering pivot point at center of the geometry,
  // hence ( this->ScreenSize * 0.5 ).
  this->LabelScreenOffset = 20.0 + this->ScreenSize * 0.5;

145
  // Properties of the radial axes, with default color black
Philippe Pébay's avatar
Philippe Pébay committed
146
  this->RadialAxesProperty = vtkProperty::New();
147
  this->RadialAxesProperty->SetColor( 0., 0., 0. );
Philippe Pébay's avatar
Philippe Pébay committed
148

149
  // Text properties of polar axis title and labels, with default color white
Philippe Pébay's avatar
Philippe Pébay committed
150
  this->PolarAxisTitleTextProperty = vtkTextProperty::New();
151
  this->PolarAxisTitleTextProperty->SetColor( 1., 1. ,1. );
Philippe Pébay's avatar
Philippe Pébay committed
152
153
  this->PolarAxisTitleTextProperty->SetFontFamilyToArial();
  this->PolarAxisLabelTextProperty = vtkTextProperty::New();
154
  this->PolarAxisLabelTextProperty->SetColor( 1., 1. ,1. );
Philippe Pébay's avatar
Philippe Pébay committed
155
156
  this->PolarAxisLabelTextProperty->SetFontFamilyToArial();

157
  // Create and set radial axes
158
159
  this->RadialAxes = new vtkAxisActor*[VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES];
  for ( int i = 0; i < VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES; ++ i )
160
    {
161
    // Create axis of type X
162
    this->RadialAxes[i] = vtkAxisActor::New();
163
164
    vtkAxisActor* axis = this->RadialAxes[i];
    axis->SetAxisTypeToX();
165
    axis->SetAxisPositionToMinMax();
166
167

    // Title offset is auto-calculated for now
168
    axis->SetCalculateTitleOffset( 0 );
169
    axis->SetCalculateLabelOffset( 0 );
170
171

    // Pass information to axes followers
172
173
    vtkAxisFollower* follower = axis->GetTitleActor();
    follower->SetAxis( axis );
174
    if ( !i )
175
      {
176
177
      double offset = this->LabelScreenOffset + this->ScreenSize * 0.5;

Philippe Pébay's avatar
Philippe Pébay committed
178
      // Replace default screen offset (for polar axis only)
179
180
181
182
183
184
185
186
      follower->SetScreenOffset( 2.0 * offset );

      vtkAxisFollower **labelActors = axis->GetLabelActors();
      int numberOfLabels = axis->GetNumberOfLabelsBuilt();
      for( int k=0; k < numberOfLabels; ++k )
        {
        labelActors[k]->SetScreenOffset( offset );
        }
187
      }
188
189
190
    }

  // Default tick location, defined in vtkAxisActor
191
  this->TickLocation = VTK_TICKS_BOTH;
192

193
  // Create and set polar arcs and ancillary objects, with default color white
194
195
196
197
198
  this->PolarArcs = vtkPolyData::New();
  this->PolarArcsMapper = vtkPolyDataMapper::New();
  this->PolarArcsMapper->SetInput( this->PolarArcs );
  this->PolarArcsActor = vtkActor::New();
  this->PolarArcsActor->SetMapper( this->PolarArcsMapper );
199
  this->PolarArcsActor->GetProperty()->SetColor( 1., 1., 1. );
200

201
202
  // By default all features are visible
  this->RadialAxesVisibility = 1;
203
  this->RadialTitleVisibility = 1;
204
205
  this->PolarLabelVisibility = 1;
  this->PolarTickVisibility = 1;
206
  this->PolarArcsVisibility = 1;
207

208
209
210
211
  // Default title for polar axis (can also be called "Radius")
  this->PolarAxisTitle = new char[16];
  sprintf(this->PolarAxisTitle, "%s", "Radial Distance");

212
  this->RadialLabelFormat = new char[8];
213
  sprintf( this->RadialLabelFormat, "%s", "%-#6.3g" );
214
215
216
217
218
219
220
221
222
223

  this->RenderCount = 0;

  this->RenderSomething = 0;

  this->UserRadialPow = 0;

  this->AutoLabelScaling = true;

  this->LabelScale = -1.0;
224
  this->TitleScale = -1.0;
225
226
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
227
//-----------------------------------------------------------------------------
228
229
230
231
vtkPolarAxesActor::~vtkPolarAxesActor()
{
  this->SetCamera( NULL );

Philippe Pébay's avatar
Philippe Pébay committed
232
  if ( this->RadialAxesProperty )
233
234
235
236
    {
    this->RadialAxesProperty->Delete();
    }

Philippe Pébay's avatar
Philippe Pébay committed
237
  if ( this->RadialLabelFormat )
238
239
240
241
    {
    delete [] this->RadialLabelFormat;
    this->RadialLabelFormat = NULL;
    }
Philippe Pébay's avatar
Philippe Pébay committed
242

Philippe Pébay's avatar
Philippe Pébay committed
243
  if ( this->PolarAxisTitle )
244
245
246
247
248
    {
    delete [] this->PolarAxisTitle;
    this->PolarAxisTitle = NULL;
    }

Philippe Pébay's avatar
Philippe Pébay committed
249
250
251
252
253
254
255
256
257
258
259
260
  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
261
262
  if ( this->RadialAxes )
    {
263
    for ( int i = 0; i < VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES; ++ i )
Philippe Pébay's avatar
Philippe Pébay committed
264
265
266
267
268
269
270
271
272
273
      {
      if ( this->RadialAxes[i] )
        {
        this->RadialAxes[i]->Delete();
        this->RadialAxes[i] = NULL;
        }
      }
    delete [] this->RadialAxes;
    this->RadialAxes = NULL;
    }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

  if (this->PolarArcs)
    {
    this->PolarArcs->Delete();
    this->PolarArcs = NULL;
    }
  if (this->PolarArcsMapper)
    {
    this->PolarArcsMapper->Delete();
    this->PolarArcsMapper = NULL;
    }
  if (this->PolarArcsActor)
    {
    this->PolarArcsActor->Delete();
    this->PolarArcsActor = NULL;
    }

291
292
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
293
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
294
int vtkPolarAxesActor::RenderOpaqueGeometry( vtkViewport *viewport )
295
296
297
{
  // Initialization
  static bool initialRender = true;
298
  if ( !this->Camera )
299
300
301
302
303
304
305
306
307
308
309
310
    {
    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
311
      this->RadialAxes[i]->BuildAxis( viewport, true );
312
313
314
315
316
      }
    }
  initialRender = false;
  this->RebuildAxes = false;

317
  // Render the radial axes
318
319
320
321
322
  int renderedSomething = 0;
  if ( this->RadialAxesVisibility )
    {
    for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
      {
323
      renderedSomething += this->RadialAxes[i]->RenderOpaqueGeometry( viewport );
324
325
326
      }
    }

327
328
329
330
331
332
  // Render the polar arcs
  if ( this->PolarArcsVisibility )
    {
    renderedSomething += this->PolarArcsActor->RenderOpaqueGeometry(viewport);
    }

333
334
335
  return renderedSomething;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
336
//-----------------------------------------------------------------------------
337
// Screen size affects the screen offset as well.
Aashish Chaudhary's avatar
Aashish Chaudhary committed
338

Philippe Pébay's avatar
Philippe Pébay committed
339
void vtkPolarAxesActor::SetScreenSize( double screenSize )
340
341
{
  this->ScreenSize = screenSize;
342
343
  // Considering pivot point at center of the geometry,
  // hence ( this->ScreenSize * 0.5 ).
344
345
346
347
  this->LabelScreenOffset = 20.0 + this->ScreenSize * 0.5;

  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
348
    vtkAxisActor* axis = this->RadialAxes[i];
Aashish Chaudhary's avatar
Aashish Chaudhary committed
349

350
351
352
    int numberOfLabelsBuilt = axis->GetNumberOfLabelsBuilt();
    vtkAxisFollower** labelActors = axis->GetLabelActors();
    for( int k=0; k < numberOfLabelsBuilt; ++k )
353
      {
Philippe Pébay's avatar
Philippe Pébay committed
354
      labelActors[k]->SetScreenOffset( this->LabelScreenOffset );
355
356
357
358
359
360
      }
    }

  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
361
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
362
void vtkPolarAxesActor::ReleaseGraphicsResources( vtkWindow *win )
363
364
365
{
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++i )
    {
Philippe Pébay's avatar
Philippe Pébay committed
366
    this->RadialAxes[i]->ReleaseGraphicsResources( win );
367
    }
368
369
  this->PolarArcsActor->ReleaseGraphicsResources(win);

370
371
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
372
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
373
void vtkPolarAxesActor::GetBounds( double bounds[6])
374
{
Philippe Pébay's avatar
Philippe Pébay committed
375
  for ( int i=0; i< 6; i++)
376
377
378
379
380
    {
    bounds[i] = this->Bounds[i];
    }
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
381
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
382
void vtkPolarAxesActor::GetBounds( double& xmin, double& xmax,
383
                                 double& ymin, double& ymax,
Philippe Pébay's avatar
Philippe Pébay committed
384
                                 double& zmin, double& zmax )
385
386
387
388
389
390
391
392
393
{
  xmin = this->Bounds[0];
  xmax = this->Bounds[1];
  ymin = this->Bounds[2];
  ymax = this->Bounds[3];
  zmin = this->Bounds[4];
  zmax = this->Bounds[5];
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
394
//-----------------------------------------------------------------------------
395
396
397
398
399
double *vtkPolarAxesActor::GetBounds()
{
  return this->Bounds;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
400
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
401
void vtkPolarAxesActor::TransformBounds( vtkViewport *viewport,
Philippe Pébay's avatar
Philippe Pébay committed
402
                                         double bounds[6] )
403
{
Philippe Pébay's avatar
Philippe Pébay committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  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];
425
426
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
427
//-----------------------------------------------------------------------------
428
429
430
431
432
433
434
435
436
437
438
//  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.
Aashish Chaudhary's avatar
Aashish Chaudhary committed
439

Philippe Pébay's avatar
Philippe Pébay committed
440
int vtkPolarAxesActor::LabelExponent( double min, double max )
441
{
Philippe Pébay's avatar
Philippe Pébay committed
442
  if ( min == max )
443
444
445
446
447
448
449
    {
    return 0;
    }

  //
  // Determine power of 10 to scale axis labels to.
  //
Philippe Pébay's avatar
Philippe Pébay committed
450
451
  double range = ( fabs( min ) > fabs( max ) ? fabs( min ) : fabs( max ) );
  double pow10 = log10( range );
452
453
454
455
456
457
458
459

  //
  // 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
460
461
  double cut_min = pow( 10., eformat_cut_min );
  double cut_max = pow( 10., eformat_cut_max );
462
  double ipow10;
Philippe Pébay's avatar
Philippe Pébay committed
463
  if ( range < cut_min || range > cut_max )
464
465
466
467
468
    {
    //
    // 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
469
    ipow10 = ( floor( floor( pow10 )/3.) )*3;
470
471
472
473
474
475
    }
  else
    {
    ipow10 = 0;
    }

Philippe Pébay's avatar
Philippe Pébay committed
476
  return static_cast<int>( ipow10 );
477
478
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
479
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
480
void vtkPolarAxesActor::BuildAxes( vtkViewport *viewport )
481
482
483
{
  double bounds[6];

Philippe Pébay's avatar
Philippe Pébay committed
484
  if ( ( this->GetMTime() < this->BuildTime.GetMTime() ))
485
    {
Philippe Pébay's avatar
Philippe Pébay committed
486
    this->AutoScale( viewport );
487
488
489
490
    return;
    }

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

492
  // Determine the bounds for possible use ( input, prop, or user-defined )
Philippe Pébay's avatar
Philippe Pébay committed
493
  this->GetBounds( bounds );
Aashish Chaudhary's avatar
Aashish Chaudhary committed
494

495
  // If pole coordinates are invalid, use bounds
496
  double O[3];
497
498
  for ( int i = 0; i < 3; ++ i )
    {
499
    O[i] = this->Pole[i] == VTK_DOUBLE_MAX ? bounds[i * 2] : this->Pole[i];
500
    }
Aashish Chaudhary's avatar
Aashish Chaudhary committed
501

502
503
504
  // 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
Aashish Chaudhary's avatar
Aashish Chaudhary committed
505
       || this->MaximumRadius < 1.e-6 * ls
506
507
508
509
       || this->MaximumRadius > 1.e6 * ls )
    {
    this->MaximumRadius = .5 * ls;
    }
510

511
  // Prepare axes for rendering with user-definable options
512
  double dAlpha =  this->MaximumAngle / ( this->NumberOfRadialAxes - 1. );
513

514
  // Set radial axes
Philippe Pébay's avatar
Philippe Pébay committed
515
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++ i )
516
    {
517
    double theta = i * dAlpha;
Philippe Pébay's avatar
Philippe Pébay committed
518
519
    double thetaRad = vtkMath::RadiansFromDegrees( theta );
    vtkAxisActor* axis = this->RadialAxes[i];
520
521
522
523
    double x = O[0] + this->MaximumRadius * cos( thetaRad );
    double y = O[1] + this->MaximumRadius * sin( thetaRad );
    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
524

525
    // Set axis ticks
526
527
    axis->SetRange( 0., this->MaximumRadius );
    axis->SetMajorTickSize( .02 * this->MaximumRadius );
528

529
    // Set axis title
Philippe Pébay's avatar
Philippe Pébay committed
530
    if ( i )
531
      {
532
      // Use polar angle as title for non-polar axes
Philippe Pébay's avatar
Philippe Pébay committed
533
534
535
536
537
538
      vtksys_ios::ostringstream thetaStream;
      thetaStream << theta;
      if ( this->RadialUnits )
        {
        thetaStream << " deg.";
        }
539

Philippe Pébay's avatar
Philippe Pébay committed
540
      axis->SetTitle( thetaStream.str().c_str() );
541
      }
Aashish Chaudhary's avatar
Aashish Chaudhary committed
542
    else // if ( i )
543
      {
Philippe Pébay's avatar
Philippe Pébay committed
544
      // Special case of polar axis
545
546
      axis->SetTitle( this->PolarAxisTitle );
      }
547
    }
Aashish Chaudhary's avatar
Aashish Chaudhary committed
548

549
550
  // Build polar axis ticks
  this->BuildPolarAxisTicks( O[0] );
551

552
  // Build polar axis labels with 0.01 zero-threshold for labels
553
  this->BuildPolarAxisLabelsArcs( O, .01 );
554

555
  // Scale appropriately
556
557
558
559
560
561
  this->AutoScale( viewport );

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
562
//-----------------------------------------------------------------------------
563
564
565
void vtkPolarAxesActor::SetNonDependentAttributes()
{
  vtkProperty *prop = this->GetProperty();
Philippe Pébay's avatar
Philippe Pébay committed
566
567
  prop->SetAmbient( 1.0 );
  prop->SetDiffuse( 0.0 );
568
569
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    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
584
585
      axis->SetTitleTextProperty( this->GetPolarAxisTitleTextProperty() );
      axis->SetLabelTextProperty( this->GetPolarAxisLabelTextProperty() );
586
587
588
589
590
      }
    else
      {
      axis->SetLabelVisibility( 0 );
      axis->SetTickVisibility( 0 );
591
      axis->GetTitleTextProperty()->SetColor( this->GetRadialAxesProperty()->GetColor() );
592
      }
593
594
595
    }
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
596
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
597
double vtkPolarAxesActor::MaxOf( double a, double b )
598
{
Philippe Pébay's avatar
Philippe Pébay committed
599
  return ( a > b ? a : b );
600
601
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
602
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
603
inline double vtkPolarAxesActor::FFix( double value )
604
{
Philippe Pébay's avatar
Philippe Pébay committed
605
  int ivalue = static_cast<int>( value );
606
607
608
  return ivalue;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
609
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
610
inline double vtkPolarAxesActor::FSign( double value, double sign )
611
{
Philippe Pébay's avatar
Philippe Pébay committed
612
613
  value = fabs( value );
  if ( sign < 0.)
614
615
616
617
618
619
    {
    value *= -1.;
    }
  return value;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
620
//-----------------------------------------------------------------------------
621
void vtkPolarAxesActor::BuildPolarAxisTicks( double x0 )
622
623
{
  // Find the integral points.
624
  double range = this->MaximumRadius;
Philippe Pébay's avatar
Philippe Pébay committed
625
  double pow10 = log10( range );
626
627

  // Build in numerical tolerance
Philippe Pébay's avatar
Philippe Pébay committed
628
  if ( pow10 != 0.)
629
630
    {
    double eps = 10.0e-10;
Philippe Pébay's avatar
Philippe Pébay committed
631
    pow10 = this->FSign( ( fabs( pow10 ) + eps ), pow10 );
632
633
    }

634
  // FFix will move in the wrong direction if pow10 is negative.
Philippe Pébay's avatar
Philippe Pébay committed
635
  if ( pow10 < 0.)
636
637
638
639
    {
    pow10 = pow10 - 1.;
    }

640
  double fxt = pow( 10., this->FFix( pow10 ) );
641
642

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

648
  double div = 1.;
Philippe Pébay's avatar
Philippe Pébay committed
649
  if ( numTicks < 5 )
650
651
652
    {
    div = 2.;
    }
Philippe Pébay's avatar
Philippe Pébay committed
653
  if ( numTicks <= 2 )
654
655
656
657
658
659
    {
    div = 5.;
    }

  // If there aren't enough major tick points in this decade, use the next
  // decade.
660
  double major = fxt;
Philippe Pébay's avatar
Philippe Pébay committed
661
  if ( div != 1.)
662
663
664
665
    {
    major /= div;
    }

666
  // Figure out the first major tick locations, relative to the
667
  // start of the axis.
668
669
670
671
672
673
674
  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
675
  if ( sortedRange[0] <= 0.)
676
    {
677
    majorStart = major * ( this->FFix( sortedRange[0]*( 1./major ) ) + 0. );
678
679
680
    }
  else
    {
681
    majorStart = major * ( this->FFix( sortedRange[0]*( 1./major ) ) + 1. );
682
683
    }

684
  // Set major start and delta corresponding to range
685
686
  axis->SetMajorRangeStart( majorStart );
  axis->SetDeltaRangeMajor( major );
687

688
  // Set major start and delta corresponding to coordinates
689
  majorStart = majorStart - sortedRange[0] + x0;
690
691
  axis->SetMajorStart( VTK_AXIS_TYPE_X, majorStart );
  axis->SetDeltaMajor( VTK_AXIS_TYPE_X, major );
692
693
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
694
//-----------------------------------------------------------------------------
695
696
void vtkPolarAxesActor::BuildPolarAxisLabelsArcs( double* O,
                                                  double zeroThreshold )
697
{
698
699
700
701
702
  // 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();
703
  vtkIdType nLabels = 0;
704
  while ( val <= p2[0] && nLabels < VTK_MAX_LABELS )
705
    {
706
    ++ nLabels;
707
708
    val += deltaMajor;
    }
709
  vtkStringArray *labels = vtkStringArray::New();
710
  labels->SetNumberOfValues( nLabels );
711

712
  // Prepare trigonometric quantities
713
714
715
  double thetaRad = vtkMath::RadiansFromDegrees( this->MaximumAngle );
  double cosTheta = cos( thetaRad );
  double sinTheta = sin( thetaRad );
Aashish Chaudhary's avatar
Aashish Chaudhary committed
716
  vtkIdType arcResolution
717
718
    = static_cast<vtkIdType>( this->MaximumAngle * VTK_POLAR_ARC_RESOLUTION_PER_DEG );

719
720
721
722
  // Arc points
  vtkPoints *polarArcsPoints = vtkPoints::New();
  this->PolarArcs->SetPoints( polarArcsPoints );
  polarArcsPoints->Delete();
723

724
725
726
727
728
729
  // Arc lines
  vtkCellArray *polarArcsLines = vtkCellArray::New();
  this->PolarArcs->SetLines( polarArcsLines );
  polarArcsLines->Delete();

  // Now create labels and polar arcs
730
731
732
  val = axis->GetMajorRangeStart();
  const char *format = this->RadialLabelFormat;
  char label[64];
733
  vtkIdType pointIdOffset = 0;
734
  for ( int  i = 0; i < nLabels; ++ i )
735
    {
736
    if ( fabs( val ) < zeroThreshold && this->MaximumRadius > 1 )
737
738
739
740
741
      {
      // We just happened to fall at something near zero and the range is
      // large, so set it to zero to avoid ugliness.
      val = 0.;
      }
742
743
    sprintf( label, format, val );

744
    if ( fabs( val ) < zeroThreshold )
745
746
747
      {
      // Ensure that -0.0 is never a label
      // The maximum number of digits that we allow past the decimal is 5.
748
      if ( strcmp( label, "-0" ) == 0 )
749
        {
750
        sprintf( label, "0" );
751
        }
752
      else if ( strcmp( label, "-0.0" ) == 0 )
753
        {
754
        sprintf( label, "0.0" );
755
        }
756
      else if ( strcmp( label, "-0.00" ) == 0 )
757
        {
758
        sprintf( label, "0.00" );
759
        }
760
      else if ( strcmp( label, "-0.000" ) == 0 )
761
        {
762
        sprintf( label, "0.000" );
763
        }
764
      else if ( strcmp( label, "-0.0000" ) == 0 )
765
        {
766
        sprintf( label, "0.0000" );
767
        }
768
      else if ( strcmp( label, "-0.00000" ) == 0 )
769
        {
770
        sprintf( label, "0.00000" );
771
772
        }
      }
773
    else // if ( fabs( val ) < zeroThreshold )
774
      {
775
776
777
778
      // Build corresponding polar arc for non-zero values
      double x = val * cosTheta;
      double y = val * sinTheta;
      vtkArcSource* arc = vtkArcSource::New();
779
780
781
      arc->SetCenter( O );
      arc->SetPoint1( O[0] + val, O[1], O[2] );
      arc->SetPoint2( O[0] + x, O[1] + y, O[2] );
782
      arc->SetResolution( arcResolution );
783
      arc->SetNegative( this->MaximumAngle > 180. );
784
      arc->Update();
Aashish Chaudhary's avatar
Aashish Chaudhary committed
785

786
787
788
789
790
791
792
793
794
795
      // Append new polar arc to existing ones
      vtkPoints* arcPoints = arc->GetOutput()->GetPoints();
      vtkIdType nPoints = arcResolution + 1;
      vtkIdType* arcPointIds = new vtkIdType[nPoints];
      for ( vtkIdType j = 0; j < nPoints; ++ j )
        {
        polarArcsPoints->InsertNextPoint( arcPoints->GetPoint( j ) );
        arcPointIds[j] = pointIdOffset + j;
        }
      polarArcsLines->InsertNextCell( nPoints, arcPointIds );
Aashish Chaudhary's avatar
Aashish Chaudhary committed
796

797
798
799
      // Clean up
      arc->Delete();
      delete [] arcPointIds;
Aashish Chaudhary's avatar
Aashish Chaudhary committed
800

801
802
      // Update polyline cell offset
      pointIdOffset += nPoints;
803
      }
804
805
    // Store label
    labels->SetValue( i, label );
806
807

    // Move to next value
808
809
    val += deltaMajor;
    }
810
811
812
813
814

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

  // Clean up
815
  labels->Delete();
816
817
818
819
820
821
822
823

  // Update axis label followers
  vtkAxisFollower** labelActors = axis->GetLabelActors();
  for( int i = 0; i < nLabels; ++i )
    {
    labelActors[i]->SetAxis( axis );
    labelActors[i]->SetScreenOffset( this->LabelScreenOffset );
    }
824
825
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
826
//-----------------------------------------------------------------------------
827
828
829
830
831
832
833
834
835
836
void vtkPolarAxesActor::SetLabelScaling( bool autoscale, int upow )
{
  if ( autoscale != this->AutoLabelScaling || upow != this->UserRadialPow )
    {
    this->AutoLabelScaling = autoscale;
    this->UserRadialPow = upow;
    this->Modified();
    }
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
837
//-----------------------------------------------------------------------------
838
839
840
841
842
843
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport )
{
  // Current implementation only for perspective projections.
  this->AutoScale( viewport, this->RadialAxes );
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
844
845
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport,
846
847
848
849
850
                                   vtkAxisActor** axis )
{
  double newTitleScale = this->TitleScale;

  // Loop over radial axes
851
852
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
853
    // Scale title
Aashish Chaudhary's avatar
Aashish Chaudhary committed
854
    newTitleScale = this->AutoScale( viewport,
855
856
857
858
859
860
                                     this->ScreenSize,
                                     axis[i]->GetTitleActor()->GetPosition() );

    axis[i]->SetTitleScale( newTitleScale );

    // Scale labels
861
    vtkAxisFollower** labelActors = axis[i]->GetLabelActors();
862
863

    for( int j = 0; j < axis[i]->GetNumberOfLabelsBuilt(); ++ j )
864
      {
Aashish Chaudhary's avatar
Aashish Chaudhary committed
865
      double newLabelScale = this->AutoScale( viewport,
866
867
868
869
                                              this->ScreenSize,
                                              labelActors[j]->GetPosition() );

      labelActors[j]->SetScale( newLabelScale );
870
871
      }
    }
872
873
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
874
875
//-----------------------------------------------------------------------------
double vtkPolarAxesActor::AutoScale( vtkViewport *viewport,
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
                                     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;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
895
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
896
void vtkPolarAxesActor::SetRadialAxesProperty( vtkProperty *prop )
897
{
Philippe Pébay's avatar
Philippe Pébay committed
898
  this->RadialAxesProperty->DeepCopy( prop );
899
900
901
  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
902
//-----------------------------------------------------------------------------
903
904
905
906
vtkProperty* vtkPolarAxesActor::GetRadialAxesProperty()
{
  return this->RadialAxesProperty;
}
907

Aashish Chaudhary's avatar
Aashish Chaudhary committed
908
//-----------------------------------------------------------------------------
909
910
911
912
913
914
void vtkPolarAxesActor::SetPolarArcsProperty( vtkProperty *prop )
{
  this->PolarArcsActor->SetProperty(prop);
  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
915
//-----------------------------------------------------------------------------
916
917
918
919
vtkProperty* vtkPolarAxesActor::GetPolarArcsProperty()
{
  return this->PolarArcsActor->GetProperty();
}