vtkPolarAxesActor.cxx 26.4 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
175
176
177
178
179

    double offset = this->LabelScreenOffset + this->ScreenSize * 0.5;

    // Using base offset if not a polar axis.
    follower->SetScreenOffset( offset );

180
    if ( !i )
181
      {
182
      // Using twice the base offset for the title of the polar axis.
183
184
185
186
187
188
189
190
      follower->SetScreenOffset( 2.0 * offset );

      vtkAxisFollower **labelActors = axis->GetLabelActors();
      int numberOfLabels = axis->GetNumberOfLabelsBuilt();
      for( int k=0; k < numberOfLabels; ++k )
        {
        labelActors[k]->SetScreenOffset( offset );
        }
191
      }
192
193
194
    }

  // Default tick location, defined in vtkAxisActor
195
  this->TickLocation = VTK_TICKS_BOTH;
196

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

205
206
  // By default all features are visible
  this->RadialAxesVisibility = 1;
207
  this->RadialTitleVisibility = 1;
208
209
  this->PolarLabelVisibility = 1;
  this->PolarTickVisibility = 1;
210
  this->PolarArcsVisibility = 1;
211

212
213
214
215
  // Default title for polar axis (can also be called "Radius")
  this->PolarAxisTitle = new char[16];
  sprintf(this->PolarAxisTitle, "%s", "Radial Distance");

216
  this->RadialLabelFormat = new char[8];
217
  sprintf( this->RadialLabelFormat, "%s", "%-#6.3g" );
218
219
220
221
222
223
224
225
226
227

  this->RenderCount = 0;

  this->RenderSomething = 0;

  this->UserRadialPow = 0;

  this->AutoLabelScaling = true;

  this->LabelScale = -1.0;
228
  this->TitleScale = -1.0;
229
230
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
231
//-----------------------------------------------------------------------------
232
233
234
235
vtkPolarAxesActor::~vtkPolarAxesActor()
{
  this->SetCamera( NULL );

Philippe Pébay's avatar
Philippe Pébay committed
236
  if ( this->RadialAxesProperty )
237
238
239
240
    {
    this->RadialAxesProperty->Delete();
    }

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

Philippe Pébay's avatar
Philippe Pébay committed
247
  if ( this->PolarAxisTitle )
248
249
250
251
252
    {
    delete [] this->PolarAxisTitle;
    this->PolarAxisTitle = NULL;
    }

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

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

295
296
}

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

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

331
332
333
334
335
336
  // Render the polar arcs
  if ( this->PolarArcsVisibility )
    {
    renderedSomething += this->PolarArcsActor->RenderOpaqueGeometry(viewport);
    }

337
338
339
  return renderedSomething;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
340
//-----------------------------------------------------------------------------
341
// Screen size affects the screen offset as well.
Aashish Chaudhary's avatar
Aashish Chaudhary committed
342

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

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

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

  this->Modified();
}

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

374
375
}

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
385
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
386
void vtkPolarAxesActor::GetBounds( double& xmin, double& xmax,
387
                                 double& ymin, double& ymax,
Philippe Pébay's avatar
Philippe Pébay committed
388
                                 double& zmin, double& zmax )
389
390
391
392
393
394
395
396
397
{
  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
398
//-----------------------------------------------------------------------------
399
400
401
402
403
double *vtkPolarAxesActor::GetBounds()
{
  return this->Bounds;
}

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
431
//-----------------------------------------------------------------------------
432
433
434
435
436
437
438
439
440
441
442
//  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
443

Philippe Pébay's avatar
Philippe Pébay committed
444
int vtkPolarAxesActor::LabelExponent( double min, double max )
445
{
Philippe Pébay's avatar
Philippe Pébay committed
446
  if ( min == max )
447
448
449
450
451
452
453
    {
    return 0;
    }

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

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

Philippe Pébay's avatar
Philippe Pébay committed
480
  return static_cast<int>( ipow10 );
481
482
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
483
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
484
void vtkPolarAxesActor::BuildAxes( vtkViewport *viewport )
485
486
487
{
  double bounds[6];

Philippe Pébay's avatar
Philippe Pébay committed
488
  if ( ( this->GetMTime() < this->BuildTime.GetMTime() ))
489
    {
Philippe Pébay's avatar
Philippe Pébay committed
490
    this->AutoScale( viewport );
491
492
493
494
    return;
    }

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

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

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

506
507
508
  // 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
509
       || this->MaximumRadius < 1.e-6 * ls
510
511
512
513
       || this->MaximumRadius > 1.e6 * ls )
    {
    this->MaximumRadius = .5 * ls;
    }
514

515
  // Prepare axes for rendering with user-definable options
516
  double dAlpha =  this->MaximumAngle / ( this->NumberOfRadialAxes - 1. );
517

518
  // Set radial axes
Philippe Pébay's avatar
Philippe Pébay committed
519
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++ i )
520
    {
521
    double theta = i * dAlpha;
Philippe Pébay's avatar
Philippe Pébay committed
522
523
    double thetaRad = vtkMath::RadiansFromDegrees( theta );
    vtkAxisActor* axis = this->RadialAxes[i];
524
525
526
527
    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
528

529
    // Set axis ticks
530
531
    axis->SetRange( 0., this->MaximumRadius );
    axis->SetMajorTickSize( .02 * this->MaximumRadius );
532

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

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

553
554
  // Build polar axis ticks
  this->BuildPolarAxisTicks( O[0] );
555

556
  // Build polar axis labels with 0.01 zero-threshold for labels
557
  this->BuildPolarAxisLabelsArcs( O, .01 );
558

559
  // Scale appropriately
560
561
562
563
564
565
  this->AutoScale( viewport );

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

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
600
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
601
double vtkPolarAxesActor::MaxOf( double a, double b )
602
{
Philippe Pébay's avatar
Philippe Pébay committed
603
  return ( a > b ? a : b );
604
605
}

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

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

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

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

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

644
  double fxt = pow( 10., this->FFix( pow10 ) );
645
646

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

652
  double div = 1.;
Philippe Pébay's avatar
Philippe Pébay committed
653
  if ( numTicks < 5 )
654
655
656
    {
    div = 2.;
    }
Philippe Pébay's avatar
Philippe Pébay committed
657
  if ( numTicks <= 2 )
658
659
660
661
662
663
    {
    div = 5.;
    }

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

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

688
  // Set major start and delta corresponding to range
689
690
  axis->SetMajorRangeStart( majorStart );
  axis->SetDeltaRangeMajor( major );
691

692
  // Set major start and delta corresponding to coordinates
693
  majorStart = majorStart - sortedRange[0] + x0;
694
695
  axis->SetMajorStart( VTK_AXIS_TYPE_X, majorStart );
  axis->SetDeltaMajor( VTK_AXIS_TYPE_X, major );
696
697
}

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

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

723
724
725
726
  // Arc points
  vtkPoints *polarArcsPoints = vtkPoints::New();
  this->PolarArcs->SetPoints( polarArcsPoints );
  polarArcsPoints->Delete();
727

728
729
730
731
732
733
  // Arc lines
  vtkCellArray *polarArcsLines = vtkCellArray::New();
  this->PolarArcs->SetLines( polarArcsLines );
  polarArcsLines->Delete();

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

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

790
791
792
793
794
795
796
797
798
799
      // 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
800

801
802
803
      // Clean up
      arc->Delete();
      delete [] arcPointIds;
Aashish Chaudhary's avatar
Aashish Chaudhary committed
804

805
806
      // Update polyline cell offset
      pointIdOffset += nPoints;
807
      }
808
809
    // Store label
    labels->SetValue( i, label );
810
811

    // Move to next value
812
813
    val += deltaMajor;
    }
814
815
816
817
818

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

  // Clean up
819
  labels->Delete();
820
821
822
823
824
825
826
827

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
830
//-----------------------------------------------------------------------------
831
832
833
834
835
836
837
838
839
840
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
841
//-----------------------------------------------------------------------------
842
843
844
845
846
847
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport )
{
  // Current implementation only for perspective projections.
  this->AutoScale( viewport, this->RadialAxes );
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
848
849
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport,
850
851
852
853
854
                                   vtkAxisActor** axis )
{
  double newTitleScale = this->TitleScale;

  // Loop over radial axes
855
856
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
857
    // Scale title
858
859
    newTitleScale = vtkAxisFollower::AutoScale( viewport, this->Camera,
      this->ScreenSize, axis[i]->GetTitleActor()->GetPosition() );
860
861
862
863

    axis[i]->SetTitleScale( newTitleScale );

    // Scale labels
864
    vtkAxisFollower** labelActors = axis[i]->GetLabelActors();
865
866

    for( int j = 0; j < axis[i]->GetNumberOfLabelsBuilt(); ++ j )
867
      {
868
869
      double newLabelScale = vtkAxisFollower::AutoScale( viewport,
        this->Camera, this->ScreenSize, labelActors[j]->GetPosition() );
870
871

      labelActors[j]->SetScale( newLabelScale );
872
873
      }
    }
874
875
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
876
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
877
void vtkPolarAxesActor::SetRadialAxesProperty( vtkProperty *prop )
878
{
Philippe Pébay's avatar
Philippe Pébay committed
879
  this->RadialAxesProperty->DeepCopy( prop );
880
881
882
  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
883
//-----------------------------------------------------------------------------
884
885
886
887
vtkProperty* vtkPolarAxesActor::GetRadialAxesProperty()
{
  return this->RadialAxesProperty;
}
888

Aashish Chaudhary's avatar
Aashish Chaudhary committed
889
//-----------------------------------------------------------------------------
890
891
892
893
894
895
void vtkPolarAxesActor::SetPolarArcsProperty( vtkProperty *prop )
{
  this->PolarArcsActor->SetProperty(prop);
  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
896
//-----------------------------------------------------------------------------
897
898
899
900
vtkProperty* vtkPolarAxesActor::GetPolarArcsProperty()
{
  return this->PolarArcsActor->GetProperty();
}