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

  Program:   Visualization Toolkit
  Module:    vtkPolarAxesActor.cxx

  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"

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

33
#include <vtksys/ios/sstream>
34

35
#define VTK_POLAR_AXES_ACTOR_RTOL ( 1. - 10. * VTK_DBL_EPSILON )
36

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

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

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

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

57
58
  os << indent << "Number Of Radial Axes: " << this->NumberOfRadialAxes << endl;
  os << indent << "Number Of Polar Axis Ticks: " << this->NumberOfPolarAxisTicks << endl;
59
60
  os << indent << "Auto Subdivide Polar Axis: "
     << ( this->AutoSubdividePolarAxis ? "On\n" : "Off\n" );
61

Aashish Chaudhary's avatar
Aashish Chaudhary committed
62
  os << indent << "Pole: ("
63
64
     << this->Pole[0] << ", "
     << this->Pole[1] << ", "
65
     << this->Pole[2] << ")\n";
66

67
68
69
70
71
  os << indent << "Maximum Radius: " << this->MaximumRadius << endl;
  os << indent << "Auto-Scale Radius: " << this->AutoScaleRadius << endl;
  os << indent << "Minimum Angle: " << this->MinimumAngle << endl;
  os << indent << "Maximum Angle: " << this->MaximumAngle << endl;
  os << indent << "Smallest Visible Polar Angle: " << this->SmallestVisiblePolarAngle << endl;
Philippe Pébay's avatar
Philippe Pébay committed
72
73
  os << indent << "Radial Units (degrees): "
     << ( this->RadialUnits ? "On\n" : "Off\n" ) << endl;
74

Philippe Pébay's avatar
Philippe Pébay committed
75
  if ( this->Camera )
76
77
    {
    os << indent << "Camera:\n";
Philippe Pébay's avatar
Philippe Pébay committed
78
    this->Camera->PrintSelf( os,indent.GetNextIndent() );
79
80
81
82
83
84
    }
  else
    {
    os << indent << "Camera: (none)\n";
    }

85
  os << indent << "EnableDistanceLOD: "
Philippe Pébay's avatar
Philippe Pébay committed
86
     << ( this->EnableDistanceLOD ? "On" : "Off" ) << endl;
87
  os << indent << "DistanceLODThreshold: "   << this->DistanceLODThreshold    << "\n";
Philippe Pébay's avatar
Philippe Pébay committed
88

89
  os << indent << "EnableViewAngleLOD: "
90
     << ( this->EnableViewAngleLOD ? "On" : "Off" ) << endl;
91
  os << indent << "ViewAngleLODThreshold: "   << this->ViewAngleLODThreshold    << "\n";
92

Philippe Pébay's avatar
Philippe Pébay committed
93
  os << indent << "Polar Axis Title: " << this->PolarAxisTitle << "\n";
94
  os << indent << "Polar Label Format: " << this->PolarLabelFormat << "\n";
Philippe Pébay's avatar
Philippe Pébay committed
95
96
  os << indent << "PolarAxisLabelTextProperty: " << this->PolarAxisLabelTextProperty << endl;
  os << indent << "PolarAxisTitleTextProperty: " << this->PolarAxisTitleTextProperty << endl;
97
98
99
100
101
102
103
104
  os << indent << "Polar Axis Visibility: "
     << ( this->PolarAxisVisibility ? "On\n" : "Off\n" );
  os << indent << "Polar Title Visibility: "
     << ( this->PolarTitleVisibility ? "On" : "Off" ) << endl;
  os << indent << "Polar Label Visibility: "
     << ( this->PolarLabelVisibility ? "On" : "Off" ) << endl;
  os << indent << "Polar Tick Visibility: "
     << ( this->PolarTickVisibility ? "On" : "Off" ) << endl;
105
106

  os << indent << "Radial Axes Visibility: "
107
     << ( this->RadialAxesVisibility ? "On\n" : "Off\n" );
108
109
  os << indent << "Radial Title Visibility: "
     << ( this->RadialTitleVisibility ? "On" : "Off" ) << endl;
110

111
112
  os << indent << "Polar Arcs Visibility: "
     << ( this->PolarArcsVisibility ? "On" : "Off" ) << endl;
113
114
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
115
//-----------------------------------------------------------------------------
116
117
vtkPolarAxesActor::vtkPolarAxesActor() : vtkActor()
{
118
119
120
121
122
  // 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;

123
  // Default pole coordinates
124
125
126
  this->Pole[0] = 0.;
  this->Pole[1] = 0.;
  this->Pole[2] = 0.;
127

128
  // Default number of radial axes
129
  this->NumberOfRadialAxes = VTK_DEFAULT_NUMBER_OF_RADIAL_AXES;
130

131
132
133
134
  // Invalid default number of polar arcs, and auto-calculate by default
  this->NumberOfPolarAxisTicks = -1;
  this->AutoSubdividePolarAxis = true;

135
  // Default maximum polar radius
136
  this->MaximumRadius = 1.;
137

138
139
140
  // Do not auto-scale radius by default
  this->AutoScaleRadius = false;

141
142
143
  // Default minimum polar angle
  this->MinimumAngle = 0.;

144
  // Default maximum polar angle
145
  this->MaximumAngle = 90.;
146

147
148
  // Default smallest radial angle distinguishable from polar axis
  this->SmallestVisiblePolarAngle = .5;
149
150

  // By default show angle units (degrees)
151
  this->RadialUnits = true;
152
153
154

  this->Camera = NULL;

155
  // Default text screen size
156
157
  this->ScreenSize = 10.0;

158
159
  // Screen offset for labels
  // Pivot point at center of the geometry hence this->ScreenSize * 0.5
Philippe Pébay's avatar
Philippe Pébay committed
160
  this->LabelScreenOffset = 15.0 + this->ScreenSize * 0.5;
161

162
  // Text properties of polar axis title and labels, with default color white
163
164
165
  // Properties of the radial axes, with default color black
  this->PolarAxisProperty = vtkProperty::New();
  this->PolarAxisProperty->SetColor( 0., 0., 0. );
Philippe Pébay's avatar
Philippe Pébay committed
166
  this->PolarAxisTitleTextProperty = vtkTextProperty::New();
167
  this->PolarAxisTitleTextProperty->SetColor( 1., 1. ,1. );
Philippe Pébay's avatar
Philippe Pébay committed
168
169
  this->PolarAxisTitleTextProperty->SetFontFamilyToArial();
  this->PolarAxisLabelTextProperty = vtkTextProperty::New();
170
  this->PolarAxisLabelTextProperty->SetColor( 1., 1. ,1. );
Philippe Pébay's avatar
Philippe Pébay committed
171
172
  this->PolarAxisLabelTextProperty->SetFontFamilyToArial();

173
174
175
176
177
178
179
  // Create and set polar axis of type X
  this->PolarAxis = vtkAxisActor::New();
  this->PolarAxis->SetAxisTypeToX();
  this->PolarAxis->SetAxisPositionToMinMax();
  this->PolarAxis->SetCalculateTitleOffset( 0 );
  this->PolarAxis->SetCalculateLabelOffset( 0 );

180
  // Default distance LOD settings
181
182
  this->EnableDistanceLOD = 1;
  this->DistanceLODThreshold = .7;
183

184
  // Default view angle LOD settings
185
186
  this->EnableViewAngleLOD = 1;
  this->ViewAngleLODThreshold = .3;
187
188
189
190
191
192

  // Properties of the radial axes, with default color black
  this->RadialAxesProperty = vtkProperty::New();
  this->RadialAxesProperty->SetColor( 0., 0., 0. );

  // Create and set radial axes of type X
193
  this->CreateRadialAxes();
194

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

203
  // Default title for polar axis (sometimes also called "Radius")
204
205
  this->PolarAxisTitle = new char[16];
  sprintf(this->PolarAxisTitle, "%s", "Radial Distance");
206
207
  this->PolarLabelFormat = new char[8];
  sprintf( this->PolarLabelFormat, "%s", "%-#6.3g" );
208

209
210
211
212
213
214
215
216
217
218
219
220
  // By default all polar axis features are visible
  this->PolarAxisVisibility = 1;
  this->PolarTitleVisibility = 1;
  this->PolarLabelVisibility = 1;
  this->PolarTickVisibility = 1;

  // By default all radial axes features are visible
  this->RadialAxesVisibility = 1;
  this->RadialTitleVisibility = 1;

  // By default polar arcs are visible
  this->PolarArcsVisibility = 1;
221

222
223
224
225
226
227
  // Default title scale
  this->TitleScale = -1.;

  // Default label scale
  this->LabelScale = -1.;

228
229
230
231
232
  this->RenderCount = 0;

  this->RenderSomething = 0;
}

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

238
239
240
241
242
  if ( this->PolarAxisProperty )
    {
    this->PolarAxisProperty->Delete();
    }

Philippe Pébay's avatar
Philippe Pébay committed
243
  if ( this->RadialAxesProperty )
244
245
246
247
    {
    this->RadialAxesProperty->Delete();
    }

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

Philippe Pébay's avatar
Philippe Pébay committed
254
  if ( this->PolarAxisTitle )
255
256
257
258
259
    {
    delete [] this->PolarAxisTitle;
    this->PolarAxisTitle = NULL;
    }

Philippe Pébay's avatar
Philippe Pébay committed
260
261
262
263
264
265
266
267
268
269
270
271
  if ( this->PolarAxisTitleTextProperty )
    {
    this->PolarAxisTitleTextProperty->Delete();
    this->PolarAxisTitleTextProperty = NULL;
    }

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

272
273
274
275
276
277
  if ( this->PolarAxis )
    {
    this->PolarAxis->Delete();
    this->PolarAxis = NULL;
    }

Philippe Pébay's avatar
Philippe Pébay committed
278
279
  if ( this->RadialAxes )
    {
280
    for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
Philippe Pébay's avatar
Philippe Pébay committed
281
282
283
284
285
286
287
288
289
290
      {
      if ( this->RadialAxes[i] )
        {
        this->RadialAxes[i]->Delete();
        this->RadialAxes[i] = NULL;
        }
      }
    delete [] this->RadialAxes;
    this->RadialAxes = NULL;
    }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

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

308
309
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
310
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
311
int vtkPolarAxesActor::RenderOpaqueGeometry( vtkViewport *viewport )
312
313
{
  // Initialization
314
  int renderedSomething = 0;
315
  if ( !this->Camera )
316
317
318
    {
    vtkErrorMacro( <<"No camera!" );
    this->RenderSomething = 0;
319
    return renderedSomething;
320
321
322
323
    }

  this->BuildAxes( viewport );

324
325
326
327
328
329
330
  // Render the polar axis
  if ( this->PolarAxisVisibility )
    {
    renderedSomething += this->PolarAxis->RenderOpaqueGeometry( viewport );
    }

  // Render the radial axes
331
332
333
334
  if ( this->RadialAxesVisibility )
    {
    for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
      {
335
      renderedSomething += this->RadialAxes[i]->RenderOpaqueGeometry( viewport );
336
337
338
      }
    }

339
340
341
342
343
344
  // Render the polar arcs
  if ( this->PolarArcsVisibility )
    {
    renderedSomething += this->PolarArcsActor->RenderOpaqueGeometry(viewport);
    }

345
346
347
  return renderedSomething;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
348
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
349
void vtkPolarAxesActor::SetScreenSize( double screenSize )
350
351
{
  this->ScreenSize = screenSize;
352
353
  // Considering pivot point at center of the geometry,
  // hence ( this->ScreenSize * 0.5 ).
Philippe Pébay's avatar
Philippe Pébay committed
354
  this->LabelScreenOffset = 15.0 + this->ScreenSize * 0.5;
355

356
357
358
  vtkAxisFollower** labelActors = this->PolarAxis->GetLabelActors();
  int numberOfLabels = this->PolarAxis->GetNumberOfLabelsBuilt();
  for( int i = 0; i < numberOfLabels; ++ i )
359
    {
360
    labelActors[i]->SetScreenOffset( this->LabelScreenOffset );
361
362
363
364
365
    }

  this->Modified();
}

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

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::CalculateBounds()
{
  // Fetch angles, at this point it is already known that angular sector <= 360.
  double minAngle = this->MinimumAngle;
  double maxAngle = this->MaximumAngle;

  // Ensure that angles are not both < -180 nor both > 180 degrees
  if ( maxAngle < -180. )
    {
    // Increment angles modulo 360 degrees
    minAngle += 360.;
    maxAngle += 360.;
    }
  else if ( minAngle > 180. )
    {
    // Decrement angles modulo 360 degrees
    minAngle -= 360.;
    maxAngle -= 360.;
    }
397

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  // Prepare trigonometric quantities
  double thetaMin = vtkMath::RadiansFromDegrees( minAngle );
  double cosThetaMin = cos( thetaMin );
  double sinThetaMin = sin( thetaMin );
  double thetaMax = vtkMath::RadiansFromDegrees( maxAngle );
  double cosThetaMax = cos( thetaMax );
  double sinThetaMax = sin( thetaMax );

  // Calculate extremal cosines across angular sector
  double minCos;
  double maxCos;
  if ( minAngle * maxAngle < 0. )
    {
    // Angular sector contains null angle
    maxCos = 1.;
    if ( minAngle < 180. && maxAngle > 180. )
      {
      // Angular sector also contains flat angle
      minCos = -1.;
      }
    else
      {
      // Angular sector does not contain flat angle
      minCos = cosThetaMin < cosThetaMax ? cosThetaMin : cosThetaMax;
      }

    }
  else if ( minAngle < 180. && maxAngle > 180. )
    {
    // Angular sector does not contain flat angle (and not null angle)
    minCos = -1.;
    maxCos = cosThetaMax > cosThetaMin ? cosThetaMax : cosThetaMin;
    }
  else
    {
    // Angular sector does not contain flat nor null angle
    minCos = cosThetaMin < cosThetaMax ? cosThetaMin : cosThetaMax;
    maxCos = cosThetaMax > cosThetaMin ? cosThetaMax : cosThetaMin;
    }

  // Calculate extremal sines across angular sector
  double minSin;
  double maxSin;
  if ( minAngle < -90. && maxAngle > -90. )
    {
    // Angular sector contains negative right angle
    minSin = -1.;
    if ( minAngle < 90. && maxAngle > 90. )
      {
      // Angular sector also contains positive right angle
      maxSin = 1.;
      }
    else
      {
      // Angular sector contain does not contain positive right angle
      maxSin = sinThetaMax > sinThetaMin ? sinThetaMax : sinThetaMin;
      }
    }
  else if ( minAngle < 90. && maxAngle > 90. )
    {
    // Angular sector contains positive right angle (and not negative one)
    minSin = sinThetaMin < sinThetaMax ? sinThetaMin : sinThetaMax;
    maxSin = 1.;
    }
  else
    {
    // Angular sector contain does not contain either right angle
    minSin = sinThetaMin < sinThetaMax ? sinThetaMin : sinThetaMax;
    maxSin = sinThetaMax > sinThetaMin ? sinThetaMax : sinThetaMin;
    }

  // Now calculate bounds
  // xmin
  this->Bounds[0] = this->Pole[0] + this->MaximumRadius * minCos;
  // xmax
  this->Bounds[1] = this->Pole[0] + this->MaximumRadius * maxCos;
  // ymin
  this->Bounds[2] = this->Pole[1] + this->MaximumRadius * minSin;
  // ymax
  this->Bounds[3] = this->Pole[1] + this->MaximumRadius * maxSin;
  // zmin
  this->Bounds[4] = this->Pole[2];
  // zmax
  this->Bounds[5] = this->Pole[2];
482
483
484

  // Update modification time of bounds
  this->BoundsMTime.Modified();
485
486
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
487
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
488
void vtkPolarAxesActor::GetBounds( double bounds[6])
489
{
Philippe Pébay's avatar
Philippe Pébay committed
490
  for ( int i=0; i< 6; i++)
491
492
493
494
495
    {
    bounds[i] = this->Bounds[i];
    }
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
496
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
497
void vtkPolarAxesActor::GetBounds( double& xmin, double& xmax,
498
499
                                   double& ymin, double& ymax,
                                   double& zmin, double& zmax )
500
501
502
503
504
505
506
507
508
{
  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
509
//-----------------------------------------------------------------------------
510
511
512
513
514
double *vtkPolarAxesActor::GetBounds()
{
  return this->Bounds;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
515
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
516
void vtkPolarAxesActor::BuildAxes( vtkViewport *viewport )
517
{
518
  if ( ( this->GetMTime() < this->BuildTime.GetMTime() ) )
519
    {
Philippe Pébay's avatar
Philippe Pébay committed
520
    this->AutoScale( viewport );
521
522
523
    return;
    }

524
  if ( this->MaximumAngle < this->MinimumAngle )
525
    {
526
527
528
529
    // Incorrect angle input
    vtkWarningMacro( << "Cannot draw radial axes: "
                     << " minimum angle = "
                     << this->MinimumAngle
530
531
532
533
                     << " > maximum angle = "
                     << this->MaximumAngle
                     << ".");
    return;
534
    }
535

536
  if ( this->MaximumAngle - this->MinimumAngle > 360. )
537
    {
538
539
540
541
542
    // Incorrect angle input
    vtkWarningMacro( << "Cannot draw radial axes: "
                     << " angular sector = "
                     << this->MaximumAngle - this->MinimumAngle
                     << " > 360 deg." );
543
    return;
544
    }
545

546
  // Determine the bounds
547
  this->CalculateBounds();
548

549
550
  // Set polar axis endpoints
  vtkAxisActor* axis = this->PolarAxis;
551
552
553
  double ox = this->Pole[0] + this->MaximumRadius;
  axis->GetPoint1Coordinate()->SetValue( this->Pole[0], this->Pole[1], this->Pole[2] );
  axis->GetPoint2Coordinate()->SetValue( ox, this->Pole[1], this->Pole[2] );
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

  // Set common axis attributes
  this->SetCommonAxisAttributes( axis );

  // Set polar axis lines
  axis->SetAxisVisibility( this->PolarAxisVisibility );
  axis->SetAxisLinesProperty( this->PolarAxisProperty );

  // Set polar axis title
  axis->SetTitleVisibility( this->PolarTitleVisibility );
  axis->SetTitle( this->PolarAxisTitle );
  axis->SetTitleTextProperty( this->PolarAxisTitleTextProperty );

  // Set polar axis ticks (major only)
  axis->SetTickVisibility( this->PolarTickVisibility );
569
  axis->SetTickLocation( VTK_TICKS_BOTH );
570
  axis->SetMajorTickSize( .02 * this->MaximumRadius );
571

572
573
574
575
  // Set polar axis labels
  axis->SetLabelVisibility( this->PolarLabelVisibility );
  axis->SetLabelTextProperty( this->PolarAxisLabelTextProperty );

576
577
  // Build radial axes
  this->BuildRadialAxes();
Aashish Chaudhary's avatar
Aashish Chaudhary committed
578

579
  // Build polar axis ticks
580
  this->BuildPolarAxisTicks( this->Pole[0] );
581

582
  // Build polar axis labels
583
584
  this->BuildPolarAxisLabelsArcs();

585
586
  // Build polar axis
  this->PolarAxis->BuildAxis( viewport, true );
587

588
  // Scale appropriately
589
590
591
592
593
594
  this->AutoScale( viewport );

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

Aashish Chaudhary's avatar
Aashish Chaudhary committed
595
//-----------------------------------------------------------------------------
596
void vtkPolarAxesActor::SetCommonAxisAttributes( vtkAxisActor* axis )
597
598
{
  vtkProperty *prop = this->GetProperty();
Philippe Pébay's avatar
Philippe Pébay committed
599
600
  prop->SetAmbient( 1.0 );
  prop->SetDiffuse( 0.0 );
601
602
  axis->SetProperty( prop );

603
  // Common space and range attributes
604
605
606
607
608
609
  axis->SetCamera( this->Camera );
  axis->SetBounds( this->Bounds );
  axis->SetRange( 0., this->MaximumRadius );

  // No minor ticks for any kind of axes
  axis->SetMinorTicksVisible( 0 );
610
611
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
612
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
613
inline double vtkPolarAxesActor::FFix( double value )
614
{
Philippe Pébay's avatar
Philippe Pébay committed
615
  int ivalue = static_cast<int>( value );
616
617
618
  return ivalue;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
619
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
620
inline double vtkPolarAxesActor::FSign( double value, double sign )
621
{
Philippe Pébay's avatar
Philippe Pébay committed
622
623
  value = fabs( value );
  if ( sign < 0.)
624
625
626
627
628
629
    {
    value *= -1.;
    }
  return value;
}

630
//-----------------------------------------------------------------------------
631
void vtkPolarAxesActor::CreateRadialAxes()
632
{
633
  // Create requested number of radial axes
634
635
636
637
638
639
640
641
642
643
  this->RadialAxes = new vtkAxisActor*[this->NumberOfRadialAxes];
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
    // Create axis of type X
    this->RadialAxes[i] = vtkAxisActor::New();
    vtkAxisActor* axis = this->RadialAxes[i];
    axis->SetAxisTypeToX();
    axis->SetAxisPositionToMinMax();
    axis->SetCalculateTitleOffset( 0 );
    axis->SetCalculateLabelOffset( 0 );
644
    }
645
646
}

647
648
649
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::BuildRadialAxes()
{
650
651
652
  // Base offset for radial axis title followers
  double offset = .67 * ( this->LabelScreenOffset + this->ScreenSize * 0.5 );

653
  // Create requested number of radial axes
654
  double dAlpha =
655
656
657
    ( this->MaximumAngle  - this->MinimumAngle ) / ( this->NumberOfRadialAxes - 1. );
  double alpha = this->MinimumAngle;
  for ( int i = 0; i < this->NumberOfRadialAxes;  ++ i, alpha += dAlpha )
658
    {
659
660
    // Calculate endpoint coordinates
    double alphaRad = vtkMath::RadiansFromDegrees( alpha );
661
662
    double x = this->Pole[0] + this->MaximumRadius * cos( alphaRad );
    double y = this->Pole[1] + this->MaximumRadius * sin( alphaRad );
663
664

    // Set radial axis endpoints
665
    vtkAxisActor* axis = this->RadialAxes[i];
666
667
    axis->GetPoint1Coordinate()->SetValue( this->Pole[0], this->Pole[1], this->Pole[2] );
    axis->GetPoint2Coordinate()->SetValue( x, y, this->Pole[2] );
Philippe Pébay's avatar
Philippe Pébay committed
668

669
670
    // Set common axis attributes
    this->SetCommonAxisAttributes( axis );
671

672
673
674
    // Set radial axis lines
    axis->SetAxisVisibility( this->RadialAxesVisibility );
    axis->SetAxisLinesProperty( this->RadialAxesProperty );
675

676
    // Set radial axis title with polar angle as title for non-polar axes
677
    if ( this->PolarAxisVisibility && fabs( alpha ) < 2. )
678
      {
679
680
      // Prevent conflict between radial and polar axes titles
      axis->SetTitleVisibility( false );
681
682

      if ( fabs( alpha ) < this->SmallestVisiblePolarAngle )
683
        {
684
685
686
        // Do not show radial axes too close to polar axis
        axis->SetAxisVisibility( false );
        }
687
688
689
690
691
692
693
694
695
696
      }
    else
      {
      // Use polar angle as a title for the radial axis
      axis->SetTitleVisibility( this->RadialTitleVisibility );
      axis->GetTitleTextProperty()->SetColor( this->RadialAxesProperty->GetColor() );
      vtksys_ios::ostringstream title;
      title << alpha
            << ( this->RadialUnits ? " deg" : "" );
      axis->SetTitle( title.str().c_str() );
697

698
699
700
701
702
703
704
      // Update axis title followers
      axis->GetTitleActor()->SetAxis( axis );
      axis->GetTitleActor()->SetScreenOffset( offset );
      axis->GetTitleActor()->SetEnableDistanceLOD( this->EnableDistanceLOD );
      axis->GetTitleActor()->SetDistanceLODThreshold( this->DistanceLODThreshold );
      axis->GetTitleActor()->SetEnableViewAngleLOD( this->EnableViewAngleLOD );
      axis->GetTitleActor()->SetViewAngleLODThreshold( this->ViewAngleLODThreshold );
705
      }
706
707
708
709

    // No labels nor ticks for radial axes
    axis->SetLabelVisibility( 0 );
    axis->SetTickVisibility( 0 );
710
    }
711
712
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
713
//-----------------------------------------------------------------------------
714
void vtkPolarAxesActor::BuildPolarAxisTicks( double x0 )
715
{
716
  double delta;
717

718
719
720
  if ( this->AutoSubdividePolarAxis
       || this->NumberOfPolarAxisTicks < 0
       || this->NumberOfPolarAxisTicks > VTK_MAXIMUM_NUMBER_OF_POLAR_AXIS_TICKS )
721
    {
722
723
    // Programatically figure the number of divisions of the polar axis
    double pow10 = log10( this->MaximumRadius );
724

725
726
727
728
729
730
    // Build in numerical tolerance
    if ( pow10 != 0.)
      {
      double eps = 10.0e-10;
      pow10 = this->FSign( ( fabs( pow10 ) + eps ), pow10 );
      }
731

732
733
734
735
736
    // FFix will move in the wrong direction if pow10 is negative.
    if ( pow10 < 0.)
      {
      pow10 = pow10 - 1.;
      }
737

738
739
740
741
    // Find the number of integral points in the interval.
    delta = pow( 10., this->FFix( pow10 ) );
    double fnt = this->MaximumRadius / delta;
    fnt  = this->FFix( fnt );
742
    int numTicks = fnt <= 0.5 ?
743
      static_cast<int>( this->FFix( fnt ) ) : static_cast<int>( this->FFix( fnt ) + 1 );
744

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    // If not enough tick points in this decade, scale down
    double div = 1.;
    if ( numTicks < 5 )
      {
      div = 2.;
      }
    if ( numTicks <= 2 )
      {
      div = 5.;
      }
    if ( div != 1.)
      {
      delta /= div;
      }

    // Finally calculate number of tick points
    this->NumberOfPolarAxisTicks = 0;
    while ( delta * this->NumberOfPolarAxisTicks <= this->MaximumRadius )
      {
      ++ this->NumberOfPolarAxisTicks;
      }
    }
  else // if ( this->AutoSubdividePolarAxis || this->NumberOfPolarAxisTicks ... )
768
    {
769
770
    // Use pre-set number of arcs when it is valid and no auto-subdivision was requested
    delta =  this->MaximumRadius / ( this->NumberOfPolarAxisTicks - 1 );
771
    }
Philippe Pébay's avatar
Philippe Pébay committed
772

773
  // Set major start and delta corresponding to range and coordinates
774
775
776
  this->PolarAxis->SetMajorRangeStart( 0. );
  this->PolarAxis->SetDeltaRangeMajor( delta );
  this->PolarAxis->SetMajorStart( VTK_AXIS_TYPE_X, x0 );
777

778
  // Build in numerical robustness to avoid truncation errors at endpoint
779
  delta *= VTK_POLAR_AXES_ACTOR_RTOL;
780
  this->PolarAxis->SetDeltaMajor( VTK_AXIS_TYPE_X, delta );
781
782
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
783
//-----------------------------------------------------------------------------
784
void vtkPolarAxesActor::BuildPolarAxisLabelsArcs()
785
{
786
  // Prepare storage for polar axis labels
787
  vtkStringArray* labels = vtkStringArray::New();
788
  labels->SetNumberOfValues( this->NumberOfPolarAxisTicks );
789

790
  // Prepare trigonometric quantities
791
792
793
794
  double thetaMin = vtkMath::RadiansFromDegrees( this->MinimumAngle );
  double cosThetaMin = cos( thetaMin );
  double sinThetaMin = sin( thetaMin );
  double angularSector = this->MaximumAngle - this->MinimumAngle;
Aashish Chaudhary's avatar
Aashish Chaudhary committed
795
  vtkIdType arcResolution
796
    = static_cast<vtkIdType>( angularSector * VTK_POLAR_ARC_RESOLUTION_PER_DEG );
797

798
  // Arc points
799
  vtkPoints* polarArcsPoints = vtkPoints::New();
800
801
  this->PolarArcs->SetPoints( polarArcsPoints );
  polarArcsPoints->Delete();
802

803
  // Arc lines
804
  vtkCellArray* polarArcsLines = vtkCellArray::New();
805
806
807
  this->PolarArcs->SetLines( polarArcsLines );
  polarArcsLines->Delete();

808
  // Retreave label features
809
  vtkAxisActor* axis = this->PolarAxis;
810
811
812
813
  double delta = axis->GetDeltaMajor( VTK_AXIS_TYPE_X );
  double value = axis->GetMajorRangeStart();

  // Now create labels and polar arcs
814
  const char *format = this->PolarLabelFormat;
815
  char label[64];
816
  vtkIdType pointIdOffset = 0;
817
  for ( int  i = 0; i < this->NumberOfPolarAxisTicks; ++ i )
818
    {
819
    // Store label
820
    sprintf( label, format, value );
821
    labels->SetValue( i, label );
822

823
    // Build polar arcs for non-zero values
824
    if ( value  > 0. )
825
      {
826
      // Compute polar vector for this tick mark
827
828
      double x1 = value * cosThetaMin;
      double y1 = value * sinThetaMin;
829
830

      // Create polar arc with corresponding to this tick mark
831
      vtkArcSource* arc = vtkArcSource::New();
832
      arc->UseNormalAndAngleOn(); // Use new arc source API
833
      arc->SetCenter( this->Pole );
834
835
836
      arc->SetNormal( 0., 0., 1. );
      arc->SetPolarVector( x1, y1, 0. );
      arc->SetAngle( angularSector );
837
838
      arc->SetResolution( arcResolution );
      arc->Update();
Aashish Chaudhary's avatar
Aashish Chaudhary committed
839

840
841
842
843
844
845
846
847
848
849
      // 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
850

851
852
853
      // Clean up
      arc->Delete();
      delete [] arcPointIds;
Aashish Chaudhary's avatar
Aashish Chaudhary committed
854

855
856
      // Update polyline cell offset
      pointIdOffset += nPoints;
857
858
859
      }

    // Move to next value
860
    value += delta;
861
    }
862
863

  // Store labels
864
  this->PolarAxis->SetLabels( labels );
865
866

  // Clean up
867
  labels->Delete();
868

869
870
871
872
873
874
875
876
877
  // Update axis title follower
  vtkAxisFollower* follower = axis->GetTitleActor();
  follower->SetAxis( axis );
  follower->SetScreenOffset( 2. * this->LabelScreenOffset + this->ScreenSize + 5 );
  follower->SetEnableDistanceLOD( this->EnableDistanceLOD );
  follower->SetDistanceLODThreshold( this->DistanceLODThreshold );
  follower->SetEnableViewAngleLOD( this->EnableViewAngleLOD );
  follower->SetViewAngleLODThreshold( this->ViewAngleLODThreshold );

878
879
  // Update axis label followers
  vtkAxisFollower** labelActors = axis->GetLabelActors();
880
  for( int i = 0; i < this->NumberOfPolarAxisTicks; ++ i )
881
882
883
    {
    labelActors[i]->SetAxis( axis );
    labelActors[i]->SetScreenOffset( this->LabelScreenOffset );
884
    labelActors[i]->SetEnableDistanceLOD( this->EnableDistanceLOD );
885
886
887
    labelActors[i]->SetDistanceLODThreshold( this->DistanceLODThreshold );
    labelActors[i]->SetEnableViewAngleLOD( this->EnableViewAngleLOD );
    labelActors[i]->SetViewAngleLODThreshold( this->ViewAngleLODThreshold );
888
    }
889
890
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
891
//-----------------------------------------------------------------------------
892
893
894
void vtkPolarAxesActor::AutoScale( vtkViewport *viewport )
{

895
896
897
  // Scale polar axis title
  vtkAxisActor* axis = this->PolarAxis;
  double newTitleScale
898
    = vtkAxisFollower::AutoScale( viewport,
899
                                  this->Camera,
900
                                  this->ScreenSize,
901
902
903
904
905
906
907
                                  axis->GetTitleActor()->GetPosition() );
  axis->SetTitleScale( newTitleScale );

  // Scale polar axis labels
  vtkAxisFollower** labelActors = axis->GetLabelActors();
  for( int i = 0; i < axis->GetNumberOfLabelsBuilt(); ++ i )
    {
908
    double newLabelScale
909
      = vtkAxisFollower::AutoScale( viewport,
910
911
                                    this->Camera,
                                    this->ScreenSize,
912
913
914
                                    labelActors[i]->GetPosition() );
    labelActors[i]->SetScale( newLabelScale );
    }
915
916

  // Loop over radial axes
917
918
  for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
    {
919
    axis = this->RadialAxes[i];
920
    // Scale title
921
922
    newTitleScale
      = vtkAxisFollower::AutoScale( viewport,
923
924
925
926
                                    this->Camera,
                                    this->ScreenSize,
                                    axis->GetTitleActor()->GetPosition() );
    axis->SetTitleScale( newTitleScale );
927
    }
928
929
}

930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetPole( double p[3] )
{
  this->Pole[0] = p[0];
  this->Pole[1] = p[1];
  this->Pole[2] = p[2];

  // Update bounds
  this->CalculateBounds();
  this->Modified();
}

//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetPole( double x, double y, double z )
{
  this->Pole[0] = x;
  this->Pole[1] = y;
  this->Pole[2] = z;

  // Update bounds
  this->CalculateBounds();
  this->Modified();
}

954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetNumberOfRadialAxes( vtkIdType n )
{
  // Limit number of redial axes
  if ( n > VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES )
    {
    n = VTK_MAXIMUM_NUMBER_OF_RADIAL_AXES;
    }

  // If number of radial axes does not change, do nothing
  if ( this->NumberOfRadialAxes == n )
    {
    return;
    }

  // Delete existing radial axes
  if ( this->RadialAxes )
    {
    for ( int i = 0; i < this->NumberOfRadialAxes; ++ i )
      {
      if ( this->RadialAxes[i] )
        {
        this->RadialAxes[i]->Delete();
        this->RadialAxes[i] = NULL;
        }
      }
Philippe Pébay's avatar
Philippe Pébay committed
980
981
    delete [] this->RadialAxes;
    this->RadialAxes = NULL;
982
983
984
985
    }

  // Create and set n radial axes of type X
  this->NumberOfRadialAxes = n;
986
  this->CreateRadialAxes();
987
988
989
990

  this->Modified();
}

991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetMaximumRadius( double r )
{
  this->MaximumRadius = r > 0. ? r : 0.;

  // Update bounds
  this->CalculateBounds();
  this->Modified();
}

//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetMinimumAngle( double a )
{
  if ( a > 360. )
    {
    this->MinimumAngle = 360.;
    }
  else if ( a < -360. )
    {
    this->MinimumAngle = -360.;
    }
  else
    {
    this->MinimumAngle = a;
    }

  // Update bounds
  this->CalculateBounds();
  this->Modified();
}

//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetMaximumAngle( double a )
{
  if ( a > 360. )
    {
    this->MaximumAngle = 360.;
    }
  else if ( a < -360. )
    {
    this->MaximumAngle = -360.;
    }
  else
    {
    this->MaximumAngle = a;
    }

  // Update bounds
  this->CalculateBounds();
  this->Modified();
}

1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//-----------------------------------------------------------------------------
void vtkPolarAxesActor::SetPolarAxisProperty( vtkProperty *prop )
{
  this->PolarAxisProperty->DeepCopy( prop );
  this->Modified();
}

//-----------------------------------------------------------------------------
vtkProperty* vtkPolarAxesActor::GetPolarAxisProperty()
{
  return this->PolarAxisProperty;
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
1056
//-----------------------------------------------------------------------------
Philippe Pébay's avatar
Philippe Pébay committed
1057
void vtkPolarAxesActor::SetRadialAxesProperty( vtkProperty *prop )
1058
{
Philippe Pébay's avatar
Philippe Pébay committed
1059
  this->RadialAxesProperty->DeepCopy( prop );
1060
1061
1062
  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
1063
//-----------------------------------------------------------------------------
1064
1065
1066
1067
vtkProperty* vtkPolarAxesActor::GetRadialAxesProperty()
{
  return this->RadialAxesProperty;
}
1068

Aashish Chaudhary's avatar
Aashish Chaudhary committed
1069
//-----------------------------------------------------------------------------
1070
1071
1072
1073
1074
1075
void vtkPolarAxesActor::SetPolarArcsProperty( vtkProperty *prop )
{
  this->PolarArcsActor->SetProperty(prop);
  this->Modified();
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
1076
//-----------------------------------------------------------------------------
1077
1078
1079
1080
vtkProperty* vtkPolarAxesActor::GetPolarArcsProperty()
{
  return this->PolarArcsActor->GetProperty();
}