File: vtkEncodedGradientShader.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 (593 lines) | stat: -rw-r--r-- 17,987 bytes parent folder | download | duplicates (4)
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
/*=========================================================================

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

#include "vtkCamera.h"
#include "vtkDirectionEncoder.h"
#include "vtkEncodedGradientEstimator.h"
#include "vtkLight.h"
#include "vtkLightCollection.h"
#include "vtkMatrix4x4.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkTransform.h"
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"

#include <math.h>

vtkStandardNewMacro(vtkEncodedGradientShader);

vtkEncodedGradientShader::vtkEncodedGradientShader()
{
  int i, j;

  for ( j = 0; j < VTK_MAX_SHADING_TABLES; j++ )
    {
      this->ShadingTableVolume[j] = NULL;
      this->ShadingTableSize[j] = 0;
      for ( i = 0; i < 6; i++ )
        {
        this->ShadingTable[j][i] = NULL;
        }
    }

  this->ZeroNormalDiffuseIntensity  = 0.0;
  this->ZeroNormalSpecularIntensity = 0.0;
  this->ActiveComponent             = 0;
}

vtkEncodedGradientShader::~vtkEncodedGradientShader()
{
  int i, j;

  for ( j = 0; j < VTK_MAX_SHADING_TABLES; j++ )
    {
    for ( i=0; i<6; i++ )
      {
      delete [] this->ShadingTable[j][i];
      }
    }
}

float *vtkEncodedGradientShader::GetRedDiffuseShadingTable( vtkVolume *vol )
{
  int                   index;

  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "No shading table found for that volume!" );
    return NULL;
    }

  return this->ShadingTable[index][0];
}

float *vtkEncodedGradientShader::GetGreenDiffuseShadingTable( vtkVolume *vol )
{
  int                   index;

  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "No shading table found for that volume!" );
    return NULL;
    }

  return this->ShadingTable[index][1];
}

float *vtkEncodedGradientShader::GetBlueDiffuseShadingTable( vtkVolume *vol )
{
  int                   index;

  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "No shading table found for that volume!" );
    return NULL;
    }

  return this->ShadingTable[index][2];
}

float *vtkEncodedGradientShader::GetRedSpecularShadingTable( vtkVolume *vol )
{
  int                   index;

  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "No shading table found for that volume!" );
    return NULL;
    }

  return this->ShadingTable[index][3];
}

float *vtkEncodedGradientShader::GetGreenSpecularShadingTable( vtkVolume *vol )
{
  int                   index;

  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "No shading table found for that volume!" );
    return NULL;
    }

  return this->ShadingTable[index][4];
}

float *vtkEncodedGradientShader::GetBlueSpecularShadingTable( vtkVolume *vol )
{
  int                   index;

  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "No shading table found for that volume!" );
    return NULL;
    }

  return this->ShadingTable[index][5];
}

void vtkEncodedGradientShader::UpdateShadingTable(
  vtkRenderer *ren,
  vtkVolume *vol,
  vtkEncodedGradientEstimator *gradest)
{
  double                 lightDirection[3], material[4];
  double                 lightAmbientColor[3];
  double                 lightDiffuseColor[3];
  double                 lightSpecularColor[3];
  double                 lightPosition[3], lightFocalPoint[3];
  double                 lightIntensity, viewDirection[3];
  double                 cameraPosition[3], cameraFocalPoint[3], mag;
  vtkLightCollection    *lightCollection;
  vtkLight              *light;
  double                 norm;
  int                    update_flag;
  vtkVolumeProperty     *property;
  vtkTransform          *transform;
  vtkMatrix4x4          *m;
  double                 in[4], out[4], zero[4];
  int                    index;

  // Figure out which shading table we are working with
  // First search through all existing ones, then if one
  // is not found, use the first available index
  for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
    {
    if ( this->ShadingTableVolume[index] == vol )
      {
      break;
      }
    }

  if ( index == VTK_MAX_SHADING_TABLES )
    {
    for ( index = 0; index < VTK_MAX_SHADING_TABLES; index++ )
      {
      if ( this->ShadingTableVolume[index] == NULL )
        {
        this->ShadingTableVolume[index] = vol;
        break;
        }
      }
    }
  if ( index == VTK_MAX_SHADING_TABLES )
    {
    vtkErrorMacro( << "Too many shading tables!\n" <<
      "Increase limit VTK_MAX_SHADING_TABLES and recompile!" );
    return;
    }

  transform = vtkTransform::New();
  m = vtkMatrix4x4::New();

  vol->GetMatrix(m);
  transform->SetMatrix(m);
  transform->Inverse();

  property = vol->GetProperty();

  material[0] = property->GetAmbient(this->ActiveComponent);
  material[1] = property->GetDiffuse(this->ActiveComponent);
  material[2] = property->GetSpecular(this->ActiveComponent);
  material[3] = property->GetSpecularPower(this->ActiveComponent);


  update_flag = 0;

  ren->GetActiveCamera()->GetPosition( cameraPosition );
  ren->GetActiveCamera()->GetFocalPoint( cameraFocalPoint );

  viewDirection[0] =  cameraFocalPoint[0] - cameraPosition[0];
  viewDirection[1] =  cameraFocalPoint[1] - cameraPosition[1];
  viewDirection[2] =  cameraFocalPoint[2] - cameraPosition[2];

  mag = sqrt(static_cast<double>(viewDirection[0] * viewDirection[0] +
                                 viewDirection[1] * viewDirection[1] +
                                 viewDirection[2] * viewDirection[2] ) );

  if ( mag )
    {
    viewDirection[0] /= mag;
    viewDirection[1] /= mag;
    viewDirection[2] /= mag;
    }

  memcpy( in, viewDirection, 3*sizeof(double) );
  in[3] = 1.0;
  transform->MultiplyPoint( in, out );
  viewDirection[0] = out[0] / out[3];
  viewDirection[1] = out[1] / out[3];
  viewDirection[2] = out[2] / out[3];

  in[0] = 0.0;
  in[1] = 0.0;
  in[2] = 0.0;
  transform->MultiplyPoint( in, zero );
  zero[0] /= zero[3];
  zero[1] /= zero[3];
  zero[2] /= zero[3];
  viewDirection[0] -= zero[0];
  viewDirection[1] -= zero[1];
  viewDirection[2] -= zero[2];

  // Loop through all lights and compute a shading table. For
  // the first light, pass in an update_flag of 0, which means
  // overwrite the shading table.  For each light after that, pass
  // in an update flag of 1, which means add to the shading table.
  // All lights are forced to be directional light sources
  // regardless of what they really are

  // Set up the lights for traversal
  lightCollection = ren->GetLights();

  // In rare cases there are no lights
  vtkLight *artificialLight=NULL;
  if ( lightCollection->GetNumberOfItems() == 0 )
    {
    artificialLight = vtkLight::New();
    artificialLight->SetIntensity(0.0);
    lightCollection->AddItem(artificialLight);
    }

  vtkCollectionSimpleIterator sit;
  lightCollection->InitTraversal(sit);
  while ( (light = lightCollection->GetNextLight(sit)) != NULL  )
    {
    if ( ! light->GetSwitch() )
      {
      continue;
      }

    // Get the light color, position, focal point, and intensity
    light->GetAmbientColor(lightAmbientColor);
    light->GetDiffuseColor(lightDiffuseColor);
    light->GetSpecularColor(lightSpecularColor);
    light->GetTransformedPosition( lightPosition );
    light->GetTransformedFocalPoint( lightFocalPoint );
    lightIntensity = light->GetIntensity( );


    // Compute the light direction and normalize it
    lightDirection[0] = lightFocalPoint[0] - lightPosition[0];
    lightDirection[1] = lightFocalPoint[1] - lightPosition[1];
    lightDirection[2] = lightFocalPoint[2] - lightPosition[2];

    norm = sqrt(static_cast<double>( lightDirection[0] * lightDirection[0] +
                                     lightDirection[1] * lightDirection[1] +
                                     lightDirection[2] * lightDirection[2] ) );

    lightDirection[0] /= -norm;
    lightDirection[1] /= -norm;
    lightDirection[2] /= -norm;

    memcpy( in, lightDirection, 3*sizeof(double) );
    transform->MultiplyPoint( in, out );
    lightDirection[0] = out[0] / out[3] - zero[0];
    lightDirection[1] = out[1] / out[3] - zero[1];
    lightDirection[2] = out[2] / out[3] - zero[2];

    // Build / Add to the shading table
    this->BuildShadingTable(index, lightDirection, lightAmbientColor,
                            lightDiffuseColor,lightSpecularColor,
                            lightIntensity, viewDirection,
                            material, ren->GetTwoSidedLighting(),
                            gradest, update_flag );

    update_flag = 1;
    }//while there is a light in the list of lights

  if ( artificialLight )
    {
    lightCollection->RemoveItem(artificialLight);
    artificialLight->Delete();
    }

  transform->Delete();
  m->Delete();
}


// Build a shading table for a light with the given direction and
// color, for a material of the given type. material[0] = ambient,
// material[1] = diffuse, material[2] = specular, material[3] =
// specular exponent.  If the ambient flag is 1, then ambient
// illumination is added. If not, then this means we are calculating
// the "other side" of two sided lighting, so no ambient intensity
// is added in. If update_flag is 0, the table is overwritten
// with the new values.  If update_flag is 1, the new intensity values
// are added into the table.  This way multiple light sources can
// be handled. There is one shading table per volume, and the index
// value indicates which index table is to be updated
void vtkEncodedGradientShader::BuildShadingTable( int index,
                                                  double lightDirection[3],
                                                  double lightAmbientColor[3],
                                                  double lightDiffuseColor[3],
                                                  double lightSpecularColor[3],
                                                  double lightIntensity,
                                                  double viewDirection[3],
                                                  double material[4],
                                                  int   twoSided,
                                                  vtkEncodedGradientEstimator *gradest,
                                                  int   updateFlag )
{
  double   lx, ly, lz;
  double    n_dot_l;
  double    n_dot_v;
  int      i;
  float    *nptr;
  float    *sdr_ptr;
  float    *sdg_ptr;
  float    *sdb_ptr;
  float    *ssr_ptr;
  float    *ssg_ptr;
  float    *ssb_ptr;
  double    Ka, Es, Kd_intensity, Ks_intensity;
  double   half_x, half_y, half_z;
  double    mag, n_dot_h, specular_value;
  int      norm_size;

  // Move to local variables
  lx = lightDirection[0];
  ly = lightDirection[1];
  lz = lightDirection[2];

  half_x = lx - viewDirection[0];
  half_y = ly - viewDirection[1];
  half_z = lz - viewDirection[2];

  mag = sqrt( static_cast<double>(half_x*half_x + half_y*half_y
                                  + half_z*half_z ) );

  if( mag != 0.0 )
    {
    half_x /= mag;
    half_y /= mag;
    half_z /= mag;
    }

  Ka = material[0] * lightIntensity;
  Es = material[3];
  Kd_intensity = material[1] * lightIntensity;
  Ks_intensity = material[2] * lightIntensity;

  nptr = gradest->GetDirectionEncoder()->GetDecodedGradientTable();

  norm_size = gradest->GetDirectionEncoder()->GetNumberOfEncodedDirections();

  if ( this->ShadingTableSize[index] != norm_size )
    {
    for ( i=0; i<6; i++ )
      {
      delete [] this->ShadingTable[index][i];
      this->ShadingTable[index][i] = new float[norm_size];
      }
      this->ShadingTableSize[index] = norm_size;
    }

  sdr_ptr = this->ShadingTable[index][0];
  sdg_ptr = this->ShadingTable[index][1];
  sdb_ptr = this->ShadingTable[index][2];

  ssr_ptr = this->ShadingTable[index][3];
  ssg_ptr = this->ShadingTable[index][4];
  ssb_ptr = this->ShadingTable[index][5];

  // For each possible normal, compute the intensity of light at
  // a location with that normal, and the given lighting and
  // material properties
  for ( i = 0; i < norm_size; i++ )
    {
    // If we have a zero normal, treat it specially
    if ( ( *(nptr+0) == 0.0 ) &&
         ( *(nptr+1) == 0.0 ) &&
         ( *(nptr+2) == 0.0 ) )
      {
      // If we are not updating, initial everything to 0.0
      if ( !updateFlag )
        {
        *(sdr_ptr) = 0.0;
        *(sdg_ptr) = 0.0;
        *(sdb_ptr) = 0.0;

        *(ssr_ptr) = 0.0;
        *(ssg_ptr) = 0.0;
        *(ssb_ptr) = 0.0;
        }

      // Now add in ambient
      *(sdr_ptr) += static_cast<float>(Ka * lightAmbientColor[0]);
      *(sdg_ptr) += static_cast<float>(Ka * lightAmbientColor[1]);
      *(sdb_ptr) += static_cast<float>(Ka * lightAmbientColor[2]);

      // Add in diffuse
      *(sdr_ptr) += static_cast<float>
        (Kd_intensity * this->ZeroNormalDiffuseIntensity*lightDiffuseColor[0]);
      *(sdg_ptr) += static_cast<float>
        (Kd_intensity * this->ZeroNormalDiffuseIntensity*lightDiffuseColor[1]);
      *(sdb_ptr) += static_cast<float>
        (Kd_intensity * this->ZeroNormalDiffuseIntensity*lightDiffuseColor[2]);

      // Add in specular
      *(ssr_ptr) += static_cast<float>(
        this->ZeroNormalSpecularIntensity*lightSpecularColor[0]);
      *(ssg_ptr) += static_cast<float>(
        this->ZeroNormalSpecularIntensity*lightSpecularColor[1]);
      *(ssb_ptr) += static_cast<float>(
        this->ZeroNormalSpecularIntensity*lightSpecularColor[2]);
      }
    else
      {
      // The dot product between the normal and the light vector
      // used for diffuse illumination
      n_dot_l = (*(nptr+0) * lx + *(nptr+1) * ly + *(nptr+2) * lz);

      // The dot product between the normal and the halfway vector
      // used for specular illumination
      n_dot_h = (*(nptr+0) * half_x + *(nptr+1) * half_y + *(nptr+2) * half_z);

      // Flip the normal if two sided lighting is on and the normal
      // is pointing away from the viewer
      if ( twoSided )
        {
        // The dot product between the normal and the view vector
        // used for two sided lighting
        n_dot_v = (*(nptr+0) * viewDirection[0] +
                   *(nptr+1) * viewDirection[1] +
                   *(nptr+2) * viewDirection[2]);

        if ( n_dot_v > 0.0 )
          {
          n_dot_l = -n_dot_l;
          n_dot_h = -n_dot_h;
          }
        }

      // If we are updating, then begin by adding in ambient
      if ( updateFlag )
        {
        *(sdr_ptr) += static_cast<float>(Ka * lightAmbientColor[0]);
        *(sdg_ptr) += static_cast<float>(Ka * lightAmbientColor[1]);
        *(sdb_ptr) += static_cast<float>(Ka * lightAmbientColor[2]);
        }
      // Otherwise begin by setting the value to the ambient contribution
      else
        {
        *(sdr_ptr) = static_cast<float>(Ka * lightAmbientColor[0]);
        *(sdg_ptr) = static_cast<float>(Ka * lightAmbientColor[1]);
        *(sdb_ptr) = static_cast<float>(Ka * lightAmbientColor[2]);
        *(ssr_ptr) = 0.0f;
        *(ssg_ptr) = 0.0f;
        *(ssb_ptr) = 0.0f;
        }

      // If there is some diffuse contribution, add it in
      if ( n_dot_l > 0 )
        {
        *(sdr_ptr) += static_cast<float>(
          Kd_intensity * n_dot_l * lightDiffuseColor[0]);
        *(sdg_ptr) += static_cast<float>(
          Kd_intensity * n_dot_l * lightDiffuseColor[1]);
        *(sdb_ptr) += static_cast<float>(
          Kd_intensity * n_dot_l * lightDiffuseColor[2]);

        if ( n_dot_h > 0.001 )
          {
          specular_value = Ks_intensity * pow(static_cast<double>(n_dot_h),
                                              static_cast<double>(Es) );
          *(ssr_ptr) += static_cast<float>(
            specular_value * lightSpecularColor[0]);
          *(ssg_ptr) += static_cast<float>(
            specular_value * lightSpecularColor[1]);
          *(ssb_ptr) += static_cast<float>(
            specular_value * lightSpecularColor[2]);
          }
        }
      }

    // Increment all the pointers
    nptr += 3;
    sdr_ptr++;
    sdg_ptr++;
    sdb_ptr++;
    ssr_ptr++;
    ssg_ptr++;
    ssb_ptr++;
    }
}


// Print the vtkEncodedGradientShader
void vtkEncodedGradientShader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Zero Normal Diffuse Intensity: " <<
    this->ZeroNormalDiffuseIntensity << endl;

  os << indent << "Zero Normal Specular Intensity: " <<
    this->ZeroNormalSpecularIntensity << endl;
  os << indent << "ActiveComponent: " << this->ActiveComponent << endl;
}