File: vtkVolumeProperty.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (661 lines) | stat: -rw-r--r-- 17,784 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkVolumeProperty.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 "vtkVolumeProperty.h"

#include "vtkObjectFactory.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"

vtkStandardNewMacro(vtkVolumeProperty);

// Construct a new vtkVolumeProperty with default values
vtkVolumeProperty::vtkVolumeProperty()
{
  this->IndependentComponents = 1;

  this->InterpolationType               = VTK_NEAREST_INTERPOLATION;

  for ( int i = 0; i < VTK_MAX_VRCOMP; i++ )
    {
    this->ColorChannels[i]                   = 1;

    this->GrayTransferFunction[i]            = NULL;
    this->RGBTransferFunction[i]             = NULL;
    this->ScalarOpacity[i]                   = NULL;
    this->ScalarOpacityUnitDistance[i]       = 1.0;
    this->GradientOpacity[i]                 = NULL;
    this->DefaultGradientOpacity[i]          = NULL;
    this->DisableGradientOpacity[i]          = 0;

    this->ComponentWeight[i]                 = 1.0;

    this->Shade[i]                           = 0;
    this->Ambient[i]                         = 0.1;
    this->Diffuse[i]                         = 0.7;
    this->Specular[i]                        = 0.2;
    this->SpecularPower[i]                   = 10.0;
    }
}

// Destruct a vtkVolumeProperty
vtkVolumeProperty::~vtkVolumeProperty()
{
  for ( int i = 0; i < VTK_MAX_VRCOMP; i++ )
    {
    if (this->GrayTransferFunction[i] != NULL)
      {
      this->GrayTransferFunction[i]->UnRegister(this);
      }

    if (this->RGBTransferFunction[i] != NULL)
      {
      this->RGBTransferFunction[i]->UnRegister(this);
      }

    if (this->ScalarOpacity[i] != NULL)
      {
      this->ScalarOpacity[i]->UnRegister(this);
      }

    if (this->GradientOpacity[i] != NULL)
      {
      this->GradientOpacity[i]->UnRegister(this);
      }

    if (this->DefaultGradientOpacity[i] != NULL)
      {
      this->DefaultGradientOpacity[i]->UnRegister(this);
      }
    }
}

void vtkVolumeProperty::DeepCopy(vtkVolumeProperty *p)
{
  if (!p)
    {
    return;
    }

  this->SetIndependentComponents(p->GetIndependentComponents());

  this->SetInterpolationType(p->GetInterpolationType());

  for (int i = 0; i < VTK_MAX_VRCOMP; i++)
    {
    this->SetComponentWeight(i, p->GetComponentWeight(i));

    // Force ColorChannels to the right value and/or create a default tfunc
    // then DeepCopy all the points

    if (p->GetColorChannels(i) > 1)
      {
      this->SetColor(i, this->GetRGBTransferFunction(i));
      this->GetRGBTransferFunction(i)->DeepCopy(
        p->GetRGBTransferFunction(i));
      }
    else
      {
      this->SetColor(i, this->GetGrayTransferFunction(i));
      this->GetGrayTransferFunction(i)->DeepCopy(
        p->GetGrayTransferFunction(i));
      }

    this->GetScalarOpacity(i)->DeepCopy(p->GetScalarOpacity(i));

    this->SetScalarOpacityUnitDistance(i, p->GetScalarOpacityUnitDistance(i));

    this->GetGradientOpacity(i)->DeepCopy(p->GetGradientOpacity(i));

    this->SetDisableGradientOpacity(i, p->GetDisableGradientOpacity(i));

    this->SetShade(i, p->GetShade(i));
    this->SetAmbient(i, p->GetAmbient(i));
    this->SetDiffuse(i, p->GetDiffuse(i));
    this->SetSpecular(i, p->GetSpecular(i));
    this->SetSpecularPower(i, p->GetSpecularPower(i));
    }

  this->Modified();
}

void vtkVolumeProperty::UpdateMTimes()
{
  this->Modified();

  for ( int i = 0; i < VTK_MAX_VRCOMP; i++ )
    {
    this->GrayTransferFunctionMTime[i].Modified();
    this->RGBTransferFunctionMTime[i].Modified();
    this->ScalarOpacityMTime[i].Modified();
    this->GradientOpacityMTime[i].Modified();
    }
}

unsigned long int vtkVolumeProperty::GetMTime()
{
  unsigned long mTime=this->vtkObject::GetMTime();
  unsigned long time;

  for ( int i = 0; i < VTK_MAX_VRCOMP; i++ )
    {
    // Color MTimes
    if (this->ColorChannels[i] == 1)
      {
      if (this->GrayTransferFunction[i])
        {
        // time that Gray transfer function pointer was set
        time = this->GrayTransferFunctionMTime[i];
        mTime = (mTime > time ? mTime : time);

        // time that Gray transfer function was last modified
        time = this->GrayTransferFunction[i]->GetMTime();
        mTime = (mTime > time ? mTime : time);
        }
      }
    else if (this->ColorChannels[i] == 3)
      {
      if (this->RGBTransferFunction[i])
        {
        // time that RGB transfer function pointer was set
        time = this->RGBTransferFunctionMTime[i];
        mTime = (mTime > time ? mTime : time);

        // time that RGB transfer function was last modified
        time = this->RGBTransferFunction[i]->GetMTime();
        mTime = (mTime > time ? mTime : time);
        }
      }

    // Opacity MTimes
    if (this->ScalarOpacity[i])
      {
      // time that Scalar opacity transfer function pointer was set
      time = this->ScalarOpacityMTime[i];
      mTime = (mTime > time ? mTime : time);

      // time that Scalar opacity transfer function was last modified
      time = this->ScalarOpacity[i]->GetMTime();
      mTime = (mTime > time ? mTime : time);
      }

    if (this->GradientOpacity[i])
      {
      // time that Gradient opacity transfer function pointer was set
      time = this->GradientOpacityMTime[i];
      mTime = (mTime > time ? mTime : time);

      if (!this->DisableGradientOpacity[i])
        {
        // time that Gradient opacity transfer function was last modified
        time = this->GradientOpacity[i]->GetMTime();
        mTime = (mTime > time ? mTime : time);
        }
      }
    }

  return mTime;
}

int vtkVolumeProperty::GetColorChannels( int index )
{
  if ( index < 0 || index > 3 )
    {
    vtkErrorMacro("Bad index - must be between 0 and 3");
    return 0;
    }

  return this->ColorChannels[index];
}


// Set the color of a volume to a gray transfer function
void vtkVolumeProperty::SetColor( int index, vtkPiecewiseFunction *function )
{
  if (this->GrayTransferFunction[index] != function )
    {
    if (this->GrayTransferFunction[index] != NULL)
      {
      this->GrayTransferFunction[index]->UnRegister(this);
      }
    this->GrayTransferFunction[index]  = function;
    if (this->GrayTransferFunction[index] != NULL)
      {
      this->GrayTransferFunction[index]->Register(this);
      }

    this->GrayTransferFunctionMTime[index].Modified();
    this->Modified();
    }

  if (this->ColorChannels[index] != 1)
    {
    this->ColorChannels[index] = 1;
    this->Modified();
    }
}

// Get the currently set gray transfer function. Create one if none set.
vtkPiecewiseFunction *vtkVolumeProperty::GetGrayTransferFunction( int index )
{
  if (this->GrayTransferFunction[index] == NULL )
    {
    this->GrayTransferFunction[index] = vtkPiecewiseFunction::New();
    this->GrayTransferFunction[index]->Register(this);
    this->GrayTransferFunction[index]->Delete();
    this->GrayTransferFunction[index]->AddPoint(    0, 0.0 );
    this->GrayTransferFunction[index]->AddPoint( 1024, 1.0 );
    if (this->ColorChannels[index] != 1)
      {
      this->ColorChannels[index] = 1;
      }
    this->Modified();
    }

  return this->GrayTransferFunction[index];
}

// Set the color of a volume to an RGB transfer function
void vtkVolumeProperty::SetColor( int index, vtkColorTransferFunction *function )
{
  if (this->RGBTransferFunction[index] != function )
    {
    if (this->RGBTransferFunction[index] != NULL)
      {
      this->RGBTransferFunction[index]->UnRegister(this);
      }
    this->RGBTransferFunction[index]   = function;
    if (this->RGBTransferFunction[index] != NULL)
      {
      this->RGBTransferFunction[index]->Register(this);
      }
    this->RGBTransferFunctionMTime[index].Modified();
    this->Modified();
    }

  if (this->ColorChannels[index] != 3)
    {
    this->ColorChannels[index] = 3;
    this->Modified();
    }
}

// Get the currently set RGB transfer function. Create one if none set.
vtkColorTransferFunction *vtkVolumeProperty::GetRGBTransferFunction( int index )
{
  if (this->RGBTransferFunction[index] == NULL )
    {
    this->RGBTransferFunction[index] = vtkColorTransferFunction::New();
    this->RGBTransferFunction[index]->Register(this);
    this->RGBTransferFunction[index]->Delete();
    this->RGBTransferFunction[index]->AddRGBPoint(      0, 0.0, 0.0, 0.0 );
    this->RGBTransferFunction[index]->AddRGBPoint(   1024, 1.0, 1.0, 1.0 );
    if (this->ColorChannels[index] != 3)
      {
      this->ColorChannels[index] = 3;
      }
    this->Modified();
    }

  return this->RGBTransferFunction[index];
}

// Set the scalar opacity of a volume to a transfer function
void vtkVolumeProperty::SetScalarOpacity( int index, vtkPiecewiseFunction *function )
{
  if ( this->ScalarOpacity[index] != function )
    {
    if (this->ScalarOpacity[index] != NULL)
      {
      this->ScalarOpacity[index]->UnRegister(this);
      }
    this->ScalarOpacity[index] = function;
    if (this->ScalarOpacity[index] != NULL)
      {
      this->ScalarOpacity[index]->Register(this);
      }

    this->ScalarOpacityMTime[index].Modified();
    this->Modified();
    }
}

// Get the scalar opacity transfer function. Create one if none set.
vtkPiecewiseFunction *vtkVolumeProperty::GetScalarOpacity( int index )
{
  if( this->ScalarOpacity[index] == NULL )
    {
    this->ScalarOpacity[index] = vtkPiecewiseFunction::New();
    this->ScalarOpacity[index]->Register(this);
    this->ScalarOpacity[index]->Delete();
    this->ScalarOpacity[index]->AddPoint(    0, 1.0 );
    this->ScalarOpacity[index]->AddPoint( 1024, 1.0 );
    }

  return this->ScalarOpacity[index];
}

void vtkVolumeProperty::SetScalarOpacityUnitDistance( int index, double distance )
{
  if ( index < 0 || index > 3 )
    {
    vtkErrorMacro("Bad index - must be between 0 and 3");
    return;
    }

  if ( this->ScalarOpacityUnitDistance[index] != distance )
    {
    this->ScalarOpacityUnitDistance[index] = distance;
    this->Modified();
    }
}

double vtkVolumeProperty::GetScalarOpacityUnitDistance( int index )
{
  if ( index < 0 || index > 3 )
    {
    vtkErrorMacro("Bad index - must be between 0 and 3");
    return 0;
    }

  return  this->ScalarOpacityUnitDistance[index];
}


// Set the gradient opacity transfer function
void vtkVolumeProperty::SetGradientOpacity( int index, vtkPiecewiseFunction *function )
{
  if ( this->GradientOpacity[index] != function )
    {
    if (this->GradientOpacity[index] != NULL)
      {
      this->GradientOpacity[index]->UnRegister(this);
      }
    this->GradientOpacity[index]       = function;
    if (this->GradientOpacity[index] != NULL)
      {
      this->GradientOpacity[index]->Register(this);
      }

    this->GradientOpacityMTime[index].Modified();
    this->Modified();
    }
}

void vtkVolumeProperty::CreateDefaultGradientOpacity( int index )
{
  if ( this->DefaultGradientOpacity[index] == NULL )
    {
    this->DefaultGradientOpacity[index] = vtkPiecewiseFunction::New();
    this->DefaultGradientOpacity[index]->Register(this);
    this->DefaultGradientOpacity[index]->Delete();
    }

  this->DefaultGradientOpacity[index]->RemoveAllPoints();
  this->DefaultGradientOpacity[index]->AddPoint(   0, 1.0 );
  this->DefaultGradientOpacity[index]->AddPoint( 255, 1.0 );
}

vtkPiecewiseFunction *vtkVolumeProperty::GetGradientOpacity( int index )
{
  if (this->DisableGradientOpacity[index])
    {
    if ( this->DefaultGradientOpacity[index] == NULL )
      {
      this->CreateDefaultGradientOpacity(index);
      }
    return this->DefaultGradientOpacity[index];
    }

  return this->GetStoredGradientOpacity(index);
}

// Get the gradient opacity transfer function. Create one if none set.
vtkPiecewiseFunction *vtkVolumeProperty::GetStoredGradientOpacity( int index )
{
  if ( this->GradientOpacity[index] == NULL )
    {
    this->GradientOpacity[index] = vtkPiecewiseFunction::New();
    this->GradientOpacity[index]->Register(this);
    this->GradientOpacity[index]->Delete();
    this->GradientOpacity[index]->AddPoint(   0, 1.0 );
    this->GradientOpacity[index]->AddPoint( 255, 1.0 );
    }

  return this->GradientOpacity[index];
}

void vtkVolumeProperty::SetDisableGradientOpacity( int index, int value )
{
  if (this->DisableGradientOpacity[index] == value)
    {
    return;
    }

  this->DisableGradientOpacity[index] = value;

  // Make sure the default function is up-to-date (since the user
  // could have modified the default function)

  if (value)
    {
    this->CreateDefaultGradientOpacity(index);
    }

  // Since this Ivar basically "sets" the gradient opacity function to be
  // either a default one or the user-specified one, update the MTime
  // accordingly

  this->GradientOpacityMTime[index].Modified();

  this->Modified();
}

int vtkVolumeProperty::GetDisableGradientOpacity( int index )
{
  return this->DisableGradientOpacity[index];
}

void vtkVolumeProperty::SetComponentWeight(int index, double value)
{
  if (index < 0 || index >= VTK_MAX_VRCOMP)
    {
    vtkErrorMacro("Invalid index");
    return;
    }

  if (this->ComponentWeight[index] == value)
    {
    return;
    }

  this->ComponentWeight[index] = value;
  this->Modified();
}

double vtkVolumeProperty::GetComponentWeight(int index)
{
  if (index < 0 || index >= VTK_MAX_VRCOMP)
    {
    vtkErrorMacro("Invalid index");
    return 0.0;
    }

  return this->ComponentWeight[index];
}

void vtkVolumeProperty::SetShade( int index, int value )
{
  if ( value != 0 && value != 1 )
    {
    vtkErrorMacro("SetShade accepts values 0 or 1");
    return;
    }

  if ( this->Shade[index] != value )
    {
    this->Shade[index] = value;
    this->Modified();
    }
}

void vtkVolumeProperty::ShadeOn( int index )
{
  this->SetShade( index, 1 );
}


void vtkVolumeProperty::ShadeOff( int index )
{
  this->SetShade( index, 0 );
}


int vtkVolumeProperty::GetShade( int index )
{
  return this->Shade[index];
}

void vtkVolumeProperty::SetAmbient( int index, double value )
{
  if ( this->Ambient[index] != value )
    {
    this->Ambient[index] = value;
    this->Modified();
    }
}

double vtkVolumeProperty::GetAmbient( int index )
{
  return this->Ambient[index];
}

void vtkVolumeProperty::SetDiffuse( int index, double value )
{
  if ( this->Diffuse[index] != value )
    {
    this->Diffuse[index] = value;
    this->Modified();
    }
}

double vtkVolumeProperty::GetDiffuse( int index )
{
  return this->Diffuse[index];
}

void vtkVolumeProperty::SetSpecular( int index, double value )
{
  if ( this->Specular[index] != value )
    {
    this->Specular[index] = value;
    this->Modified();
    }
}

double vtkVolumeProperty::GetSpecular( int index )
{
  return this->Specular[index];
}

void vtkVolumeProperty::SetSpecularPower( int index, double value )
{
  if ( this->SpecularPower[index] != value )
    {
    this->SpecularPower[index] = value;
    this->Modified();
    }
}

double vtkVolumeProperty::GetSpecularPower( int index )
{
  return this->SpecularPower[index];
}

vtkTimeStamp vtkVolumeProperty::GetScalarOpacityMTime( int index )
{
  return this->ScalarOpacityMTime[index];
}

vtkTimeStamp vtkVolumeProperty::GetGradientOpacityMTime( int index )
{
  return this->GradientOpacityMTime[index];
}

vtkTimeStamp vtkVolumeProperty::GetRGBTransferFunctionMTime( int index )
{
  return this->RGBTransferFunctionMTime[index];
}

vtkTimeStamp vtkVolumeProperty::GetGrayTransferFunctionMTime( int index )
{
  return this->GrayTransferFunctionMTime[index];
}

// Print the state of the volume property.
void vtkVolumeProperty::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Independent Components: " <<
    (this->IndependentComponents ? "On\n" : "Off\n");

  os << indent << "Interpolation Type: "
     << this->GetInterpolationTypeAsString() << "\n";

  for ( int i = 0; i < VTK_MAX_VRCOMP; i++ )
    {
    os << indent << "Properties for material " << i << endl;

    os << indent << "Color Channels: " << this->ColorChannels[i] << "\n";

    if( this->ColorChannels[i] == 1 )
      {
      os << indent << "Gray Color Transfer Function: "
         << this->GrayTransferFunction[i] << "\n";
      }
    else if( this->ColorChannels[i] == 3 )
      {
      os << indent << "RGB Color Transfer Function: "
         << this->RGBTransferFunction[i] << "\n";
      }

    os << indent << "Scalar Opacity Transfer Function: "
       << this->ScalarOpacity[i] << "\n";

    os << indent << "Gradient Opacity Transfer Function: "
       << this->GradientOpacity[i] << "\n";

    os << indent << "DisableGradientOpacity: "
       << (this->DisableGradientOpacity[i] ? "On" : "Off") << "\n";


    os << indent << "ComponentWeight: "
       << this->ComponentWeight[i] << "\n";

    os << indent << "Shade: " << this->Shade[i] << "\n";
    os << indent << indent << "Ambient: " << this->Ambient[i] << "\n";
    os << indent << indent << "Diffuse: " << this->Diffuse[i] << "\n";
    os << indent << indent << "Specular: " << this->Specular[i] << "\n";
    os << indent << indent << "SpecularPower: " << this->SpecularPower[i] << "\n";
    }

  // These variables should not be printed to the user:
  // this->GradientOpacityMTime
  // this->GrayTransferFunctionMTime
  // this->RGBTransferFunctionMTime
  // this->ScalarOpacityMTime

}