This file is indexed.

/usr/include/ITK-4.9/itkRayCastInterpolateImageFunction.hxx is in libinsighttoolkit4-dev 4.9.0-4ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 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
 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
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 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
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkRayCastInterpolateImageFunction_hxx
#define itkRayCastInterpolateImageFunction_hxx

#include "itkCompensatedSummation.h"
#include "itkRayCastInterpolateImageFunction.h"

#include "vnl/vnl_math.h"

// Put the helper class in an anonymous namespace so that it is not
// exposed to the user
namespace
{
/** \class Helper class to maintain state when casting a ray.
 *  This helper class keeps the RayCastInterpolateImageFunction thread safe.
 */
template< typename TInputImage, typename TCoordRep = float >
class RayCastHelper
{
public:
  /** Constants for the image dimensions */
  itkStaticConstMacro(InputImageDimension, unsigned int,
                      TInputImage::ImageDimension);

  /**
   * Type of the Transform Base class
   * The fixed image should be a 3D image
   */
  typedef itk::Transform< TCoordRep, 3, 3 > TransformType;

  typedef typename TransformType::Pointer         TransformPointer;
  typedef typename TransformType::InputPointType  InputPointType;
  typedef typename TransformType::OutputPointType OutputPointType;
  typedef typename TransformType::ParametersType  TransformParametersType;
  typedef typename TransformType::JacobianType    TransformJacobianType;

  typedef typename TInputImage::SizeType SizeType;
  typedef itk::Vector< TCoordRep, 3 >    DirectionType;
  typedef itk::Point< TCoordRep, 3 >     PointType;

  typedef TInputImage                        InputImageType;
  typedef typename InputImageType::PixelType PixelType;
  typedef typename InputImageType::IndexType IndexType;

  typedef itk::CompensatedSummation< double > CompensatedSummationType;

  /**
   * Set the image class
   */
  void SetImage(const InputImageType *input)
  {
    m_Image = input;
  }

  /**
   *  Initialise the ray using the position and direction of a line.
   *
   *  \param rayPosition       The position of the ray in 3D (mm).
   *  \param rayDirection      The direction of the ray in 3D (mm).
   *
   *  \return True if this is a valid ray.
   */
  bool SetRay(const OutputPointType & rayPosition, const DirectionType & rayDirection);

  /** \brief
   * Integrate the interpolated intensities above a given threshold,
   * along the ray and return the result.
   *
   * This routine can be called after instantiating the ray and
   * calling SetProjectionCoord2D() or Reset(). It may then be called
   * as many times thereafter for different 2D projection
   * coordinates.
   *
   * \param integral      The integrated intensities along the ray.
   * \param threshold     The integration threshold [default value: 0]
   *
   * \return True if a valid ray was specified.
   */
  bool IntegrateAboveThreshold(double & integral, double threshold);

  /// Reset the iterator to the start of the ray.
  void Reset();

  /// Return the interpolated intensity of the current ray point.
  double GetCurrentIntensity() const;

  /// Return the ray point spacing in mm
  double GetRayPointSpacing(void) const
  {
    typename InputImageType::SpacingType spacing = this->m_Image->GetSpacing();

    if ( m_ValidRay )
      {
      return std::sqrt(m_VoxelIncrement[0] * spacing[0] * m_VoxelIncrement[0] * spacing[0]
                      + m_VoxelIncrement[1] * spacing[1] * m_VoxelIncrement[1] * spacing[1]
                      + m_VoxelIncrement[2] * spacing[2] * m_VoxelIncrement[2] * spacing[2]);
      }
    else
      {
      return 0.;
      }
  }

  /// Set the initial zero state of the object
  void ZeroState();

  /// Initialise the object
  void Initialise();

protected:
  /// Calculate the endpoint coordinats of the ray in voxels.
  void EndPointsInVoxels();

  /**
   * Calculate the incremental direction vector in voxels, 'dVoxel',
   * required to traverse the ray.
   */
  void CalcDirnVector();

  /**
   * Reduce the length of the ray until both start and end
   * coordinates lie inside the volume.
   *
   * \return True if a valid ray has been, false otherwise.
   */
  bool AdjustRayLength();

  /**
   *   Obtain pointers to the four voxels surrounding the point where the ray
   *   enters the volume.
   */
  void InitialiseVoxelPointers();

  /// Increment the voxel pointers surrounding the current point on the ray.
  void IncrementVoxelPointers();

  /// Record volume dimensions and resolution
  void RecordVolumeDimensions();

  /// Define the corners of the volume
  void DefineCorners();

  /** \brief
   * Calculate the planes which define the volume.
   *
   * Member function to calculate the equations of the planes of 4 of
   * the sides of the volume, calculate the positions of the 8 corners
   * of the volume in mm in World, also calculate the values of the
   * slopes of the lines which go to make up the volume( defined as
   * lines in cube x,y,z dirn and then each of these lines has a slope
   * in the world x,y,z dirn [3]) and finally also to return the length
   * of the sides of the lines in mm.
   */
  void CalcPlanesAndCorners();

  /** \brief
   *  Calculate the ray intercepts with the volume.
   *
   *  See where the ray cuts the volume, check that truncation does not occur,
   *  if not, then start ray where it first intercepts the volume and set
   *  x_max to be where it leaves the volume.
   *
   *  \return True if a valid ray has been specified, false otherwise.
   */
  bool CalcRayIntercepts();

  /**
   *   The ray is traversed by stepping in the axial direction
   *   that enables the greatest number of planes in the volume to be
   *   intercepted.
   */
  typedef enum {
    UNDEFINED_DIRECTION = 0,      //!< Undefined
    TRANSVERSE_IN_X,              //!< x
    TRANSVERSE_IN_Y,              //!< y
    TRANSVERSE_IN_Z,              //!< z
    LAST_DIRECTION
    } TraversalDirection;

  // Cache the image in the structure. Skip the smart pointer for
  // efficiency. This inner class will go in/out of scope with every
  // call to Evaluate()
  const InputImageType *m_Image;

  /// Flag indicating whether the current ray is valid
  bool m_ValidRay;

  /** \brief
   * The start position of the ray in voxels.
   *
   * NB. Two of the components of this coordinate (i.e. those lying within
   * the planes of voxels being traversed) will be shifted by half a
   * voxel. This enables indices of the neighbouring voxels within the plane
   * to be determined by simply casting to 'int' and optionally adding 1.
   */
  double m_RayVoxelStartPosition[3];

  /** \brief
   * The end coordinate of the ray in voxels.
   *
   * NB. Two of the components of this coordinate (i.e. those lying within
   * the planes of voxels being traversed) will be shifted by half a
   * voxel. This enables indices of the neighbouring voxels within the plane
   * to be determined by simply casting to 'int' and optionally adding 1.
   */
  double m_RayVoxelEndPosition[3];

  /** \brief
   * The current coordinate on the ray in voxels.
   *
   * NB. Two of the components of this coordinate (i.e. those lying within
   * the planes of voxels being traversed) will be shifted by half a
   * voxel. This enables indices of the neighbouring voxels within the plane
   * to be determined by simply casting to 'int' and optionally adding 1.
   */
  CompensatedSummationType m_Position3Dvox[3];

  /** The incremental direction vector of the ray in voxels. */
  double m_VoxelIncrement[3];

  /// The direction in which the ray is incremented thorough the volume (x, y or
  // z).
  TraversalDirection m_TraversalDirection;

  /// The total number of planes of voxels traversed by the ray.
  int m_TotalRayVoxelPlanes;

  /// The current number of planes of voxels traversed by the ray.
  int m_NumVoxelPlanesTraversed;

  /// Pointers to the current four voxels surrounding the ray's trajectory.
  const PixelType *m_RayIntersectionVoxels[4];

  /**
   * The voxel coordinate of the bottom-left voxel of the current
   * four voxels surrounding the ray's trajectory.
   */
  int m_RayIntersectionVoxelIndex[3];

  /// The dimension in voxels of the 3D volume in along the x axis
  int m_NumberOfVoxelsInX;
  /// The dimension in voxels of the 3D volume in along the y axis
  int m_NumberOfVoxelsInY;
  /// The dimension in voxels of the 3D volume in along the z axis
  int m_NumberOfVoxelsInZ;

  /// Voxel dimension in x
  double m_VoxelDimensionInX;
  /// Voxel dimension in y
  double m_VoxelDimensionInY;
  /// Voxel dimension in z
  double m_VoxelDimensionInZ;

  /// The coordinate of the point at which the ray enters the volume in mm.
  double m_RayStartCoordInMM[3];
  /// The coordinate of the point at which the ray exits the volume in mm.
  double m_RayEndCoordInMM[3];

  /** \brief
      Planes which define the boundary of the volume in mm
      (six planes and four parameters: Ax+By+Cz+D). */
  double m_BoundingPlane[6][4];
  /// The eight corners of the volume (x,y,z coordinates for each).
  double m_BoundingCorner[8][3];

  /// The position of the ray
  double m_CurrentRayPositionInMM[3];

  /// The direction of the ray
  double m_RayDirectionInMM[3];
};

/* -----------------------------------------------------------------------
   Initialise() - Initialise the object
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::Initialise(void)
{
  (void)InputImageDimension; // use member variable
  // Save the dimensions of the volume and calculate the bounding box
  this->RecordVolumeDimensions();

  // Calculate the planes and corners which define the volume.
  this->DefineCorners();
  this->CalcPlanesAndCorners();
}

/* -----------------------------------------------------------------------
   RecordVolumeDimensions() - Record volume dimensions and resolution
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::RecordVolumeDimensions(void)
{
  typename InputImageType::SpacingType spacing = this->m_Image->GetSpacing();
  SizeType dim = this->m_Image->GetLargestPossibleRegion().GetSize();

  m_NumberOfVoxelsInX = dim[0];
  m_NumberOfVoxelsInY = dim[1];
  m_NumberOfVoxelsInZ = dim[2];

  m_VoxelDimensionInX = spacing[0];
  m_VoxelDimensionInY = spacing[1];
  m_VoxelDimensionInZ = spacing[2];
}

/* -----------------------------------------------------------------------
   DefineCorners() - Define the corners of the volume
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::DefineCorners(void)
{
  // Define corner positions as if at the origin

  m_BoundingCorner[0][0] =
    m_BoundingCorner[1][0] =
      m_BoundingCorner[2][0] =
        m_BoundingCorner[3][0] = 0;

  m_BoundingCorner[4][0] =
    m_BoundingCorner[5][0] =
      m_BoundingCorner[6][0] =
        m_BoundingCorner[7][0] = m_VoxelDimensionInX * m_NumberOfVoxelsInX;

  m_BoundingCorner[1][1] =
    m_BoundingCorner[3][1] =
      m_BoundingCorner[5][1] =
        m_BoundingCorner[7][1] = m_VoxelDimensionInY * m_NumberOfVoxelsInY;

  m_BoundingCorner[0][1] =
    m_BoundingCorner[2][1] =
      m_BoundingCorner[4][1] =
        m_BoundingCorner[6][1] = 0;

  m_BoundingCorner[0][2] =
    m_BoundingCorner[1][2] =
      m_BoundingCorner[4][2] =
        m_BoundingCorner[5][2] =
          m_VoxelDimensionInZ * m_NumberOfVoxelsInZ;

  m_BoundingCorner[2][2] =
    m_BoundingCorner[3][2] =
      m_BoundingCorner[6][2] =
        m_BoundingCorner[7][2] = 0;
}

/* -----------------------------------------------------------------------
   CalcPlanesAndCorners() - Calculate the planes and corners of the volume.
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::CalcPlanesAndCorners(void)
{
  int j;

  // find the equations of the planes

  int c1 = 0, c2 = 0, c3 = 0;

  for ( j = 0; j < 6; j++ )
    {                                // loop around for planes
    switch ( j )
      {                // which corners to take
      case 0:
        c1 = 1; c2 = 2; c3 = 3;
        break;
      case 1:
        c1 = 4; c2 = 5; c3 = 6;
        break;
      case 2:
        c1 = 5; c2 = 3; c3 = 7;
        break;
      case 3:
        c1 = 2; c2 = 4; c3 = 6;
        break;
      case 4:
        c1 = 1; c2 = 5; c3 = 0;
        break;
      case 5:
        c1 = 3; c2 = 7; c3 = 2;
        break;
      }

    double line1x, line1y, line1z;
    double line2x, line2y, line2z;

    // lines from one corner to another in x,y,z dirns
    line1x = m_BoundingCorner[c1][0] - m_BoundingCorner[c2][0];
    line2x = m_BoundingCorner[c1][0] - m_BoundingCorner[c3][0];

    line1y = m_BoundingCorner[c1][1] - m_BoundingCorner[c2][1];
    line2y = m_BoundingCorner[c1][1] - m_BoundingCorner[c3][1];

    line1z = m_BoundingCorner[c1][2] - m_BoundingCorner[c2][2];
    line2z = m_BoundingCorner[c1][2] - m_BoundingCorner[c3][2];

    double A, B, C, D;

    // take cross product
    A = line1y * line2z - line2y * line1z;
    B = line2x * line1z - line1x * line2z;
    C = line1x * line2y - line2x * line1y;

    // find constant
    D = -(   A * m_BoundingCorner[c1][0]
             + B * m_BoundingCorner[c1][1]
             + C * m_BoundingCorner[c1][2] );

    // initialise plane value and normalise
    m_BoundingPlane[j][0] = A / std::sqrt(A * A + B * B + C * C);
    m_BoundingPlane[j][1] = B / std::sqrt(A * A + B * B + C * C);
    m_BoundingPlane[j][2] = C / std::sqrt(A * A + B * B + C * C);
    m_BoundingPlane[j][3] = D / std::sqrt(A * A + B * B + C * C);

    if ( itk::Math::AlmostEquals( ( A * A + B * B + C * C ), itk::NumericTraits< double >::ZeroValue() ) )
      {
      itk::ExceptionObject err(__FILE__, __LINE__);
      err.SetLocation(ITK_LOCATION);
      err.SetDescription("Division by zero (planes) "
                         "- CalcPlanesAndCorners().");
      throw err;
      }
    }
}

/* -----------------------------------------------------------------------
   CalcRayIntercepts() - Calculate the ray intercepts with the volume.
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
bool
RayCastHelper< TInputImage, TCoordRep >
::CalcRayIntercepts()
{
  bool   noInterceptFlag[6];
  double cubeIntercepts[6][3];

  const unsigned int numSides = 6;  // =6 to allow truncation: =4 to remove truncated rays

  // Calculate intercept of ray with planes
  double interceptx[6];
  double intercepty[6];
  double interceptz[6];
  for ( unsigned int j = 0; j < numSides; ++j )
    {
    const double denom = (  m_BoundingPlane[j][0] * m_RayDirectionInMM[0]
               + m_BoundingPlane[j][1] * m_RayDirectionInMM[1]
               + m_BoundingPlane[j][2] * m_RayDirectionInMM[2] );

    if ( (long)( denom * 100 ) != 0 )
      {
      const double d = -(   m_BoundingPlane[j][3]
                  + m_BoundingPlane[j][0] * m_CurrentRayPositionInMM[0]
                  + m_BoundingPlane[j][1] * m_CurrentRayPositionInMM[1]
                  + m_BoundingPlane[j][2] * m_CurrentRayPositionInMM[2] ) / denom;

      interceptx[j] = m_CurrentRayPositionInMM[0] + d * m_RayDirectionInMM[0];
      intercepty[j] = m_CurrentRayPositionInMM[1] + d * m_RayDirectionInMM[1];
      interceptz[j] = m_CurrentRayPositionInMM[2] + d * m_RayDirectionInMM[2];

      noInterceptFlag[j] = true;  //OK
      }
    else
      {
      noInterceptFlag[j] = false;  //NOT OK
      }
    }

  unsigned int nSidesCrossed = 0;
  for ( unsigned int j = 0; j < numSides; ++j )
    {
    // Work out which corners to use

    int c[4];
    if ( j == 0 )
      {
      c[0] = 0; c[1] = 1; c[2] = 3; c[3] = 2;
      }
    else if ( j == 1 )
      {
      c[0] = 4; c[1] = 5; c[2] = 7; c[3] = 6;
      }
    else if ( j == 2 )
      {
      c[0] = 1; c[1] = 5; c[2] = 7; c[3] = 3;
      }
    else if ( j == 3 )
      {
      c[0] = 0; c[1] = 2; c[2] = 6; c[3] = 4;
      }
    else if ( j == 4 )
      { //TOP
      c[0] = 0; c[1] = 1; c[2] = 5; c[3] = 4;
      }
    else if ( j == 5 )
      { //BOTTOM
      c[0] = 2; c[1] = 3; c[2] = 7; c[3] = 6;
      }

    // Calculate vectors from corner of ct volume to intercept.
    double cornerVect[4][3];
    for ( unsigned int i = 0; i < 4; ++i )
      {
      if ( noInterceptFlag[j] )
        {
        cornerVect[i][0] = m_BoundingCorner[c[i]][0] - interceptx[j];
        cornerVect[i][1] = m_BoundingCorner[c[i]][1] - intercepty[j];
        cornerVect[i][2] = m_BoundingCorner[c[i]][2] - interceptz[j];
        }
      else
        {
        cornerVect[i][0] = 0;
        cornerVect[i][1] = 0;
        cornerVect[i][2] = 0;
        }
      }

    // Do cross product with these vectors
    int cross[4][3];
    unsigned int k = 0;
    for ( unsigned int i = 0; i < 4; ++i )
      {
      if ( i == 3 )
        {
        k = 0;
        }
      else
        {
        k = i + 1;
        }
      const double ax = cornerVect[i][0];
      const double ay = cornerVect[i][1];
      const double az = cornerVect[i][2];
      const double bx = cornerVect[k][0];
      const double by = cornerVect[k][1];
      const double bz = cornerVect[k][2];

      // The int and divide by 100 are to avoid rounding errors.  If
      // these are not included then you get values fluctuating around
      // zero and so in the subsequent check, all the values are not
      // above or below zero.  NB. If you "INT" by too much here though
      // you can get problems in the corners of your volume when rays
      // are allowed to go through more than one plane.
      cross[i][0] = static_cast< int >( ( ay * bz - az * by ) / 100 );
      cross[i][1] = static_cast< int >( ( az * bx - ax * bz ) / 100 );
      cross[i][2] = static_cast< int >( ( ax * by - ay * bx ) / 100 );
      }

    // See if a sign change occurred between all these cross products
    // if not, then the ray went through this plane

    unsigned int crossFlag = 0;
    for ( unsigned int i = 0; i < 3; ++i )
      {
      if ( (   cross[0][i] <= 0
               && cross[1][i] <= 0
               && cross[2][i] <= 0
               && cross[3][i] <= 0 )

           || (   cross[0][i] >= 0
                  && cross[1][i] >= 0
                  && cross[2][i] >= 0
                  && cross[3][i] >= 0 ) )
        {
        ++crossFlag;
        }
      }

    if ( crossFlag == 3 && noInterceptFlag[j] == 1 )
      {
      cubeIntercepts[nSidesCrossed][0] = interceptx[j];
      cubeIntercepts[nSidesCrossed][1] = intercepty[j];
      cubeIntercepts[nSidesCrossed][2] = interceptz[j];
      ++nSidesCrossed;
      }
    } // End of loop over all four planes

  m_RayStartCoordInMM[0] = cubeIntercepts[0][0];
  m_RayStartCoordInMM[1] = cubeIntercepts[0][1];
  m_RayStartCoordInMM[2] = cubeIntercepts[0][2];

  m_RayEndCoordInMM[0] = cubeIntercepts[1][0];
  m_RayEndCoordInMM[1] = cubeIntercepts[1][1];
  m_RayEndCoordInMM[2] = cubeIntercepts[1][2];

  if ( nSidesCrossed >= 5 )
    {
    itkDebugStatement(std::cerr << "WARNING: No. of sides crossed equals: " << nSidesCrossed << std::endl;);
    }

  // If 'nSidesCrossed' is larger than 2, this means that the ray goes through
  // a corner of the volume and due to rounding errors, the ray is
  // deemed to go through more than two planes.  To obtain the correct
  // start and end positions we choose the two intercept values which
  // are furthest from each other.

  if ( nSidesCrossed >= 3 )
    {
    double maxInterDist = 0.0;
    for ( unsigned int j = 0; j < nSidesCrossed - 1; ++j )
      {
      for ( unsigned int k = j + 1; k < nSidesCrossed; ++k )
        {
        double interDist = 0.0;
        for ( unsigned int i = 0; i < 3; ++i )
          {
          interDist += ( cubeIntercepts[j][i] - cubeIntercepts[k][i] )
                       * ( cubeIntercepts[j][i] - cubeIntercepts[k][i] );
          }
        if ( interDist > maxInterDist )
          {
          maxInterDist = interDist;

          m_RayStartCoordInMM[0] = cubeIntercepts[j][0];
          m_RayStartCoordInMM[1] = cubeIntercepts[j][1];
          m_RayStartCoordInMM[2] = cubeIntercepts[j][2];

          m_RayEndCoordInMM[0] = cubeIntercepts[k][0];
          m_RayEndCoordInMM[1] = cubeIntercepts[k][1];
          m_RayEndCoordInMM[2] = cubeIntercepts[k][2];
          }
        }
      }
    nSidesCrossed = 2;
    }

  if ( nSidesCrossed == 2 )
    {
    return true;
    }
  else
    {
    return false;
    }
}

/* -----------------------------------------------------------------------
   SetRay() - Set the position and direction of the ray
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
bool
RayCastHelper< TInputImage, TCoordRep >
::SetRay(const OutputPointType & rayPosition, const DirectionType & rayDirection)
{
  // Store the position and direction of the ray
  typename TInputImage::SpacingType spacing = this->m_Image->GetSpacing();
  SizeType dim = this->m_Image->GetLargestPossibleRegion().GetSize();

  // we need to translate the _center_ of the volume to the origin
  m_NumberOfVoxelsInX = dim[0];
  m_NumberOfVoxelsInY = dim[1];
  m_NumberOfVoxelsInZ = dim[2];

  m_VoxelDimensionInX = spacing[0];
  m_VoxelDimensionInY = spacing[1];
  m_VoxelDimensionInZ = spacing[2];

  m_CurrentRayPositionInMM[0] =
    rayPosition[0] + 0.5 * m_VoxelDimensionInX * (double)m_NumberOfVoxelsInX;

  m_CurrentRayPositionInMM[1] =
    rayPosition[1] + 0.5 * m_VoxelDimensionInY * (double)m_NumberOfVoxelsInY;

  m_CurrentRayPositionInMM[2] =
    rayPosition[2] + 0.5 * m_VoxelDimensionInZ * (double)m_NumberOfVoxelsInZ;

  m_RayDirectionInMM[0] = rayDirection[0];
  m_RayDirectionInMM[1] = rayDirection[1];
  m_RayDirectionInMM[2] = rayDirection[2];

  // Compute the ray path for this coordinate in mm

  m_ValidRay = this->CalcRayIntercepts();

  if ( !m_ValidRay )
    {
    Reset();
    return false;
    }

  // Convert the start and end coordinates of the ray to voxels

  this->EndPointsInVoxels();

  /* Calculate the ray direction vector in voxels and hence the voxel
     increment required to traverse the ray, and the number of
     interpolation points on the ray.

     This routine also shifts the coordinate frame by half a voxel for
     two of the directional components (i.e. those lying within the
     planes of voxels being traversed). */

  this->CalcDirnVector();

  /* Reduce the length of the ray until both start and end
     coordinates lie inside the volume. */

  m_ValidRay = this->AdjustRayLength();

  // Reset the iterator to the start of the ray.

  Reset();

  return m_ValidRay;
}

/* -----------------------------------------------------------------------
   EndPointsInVoxels() - Convert the endpoints to voxels
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::EndPointsInVoxels(void)
{
  m_RayVoxelStartPosition[0] = m_RayStartCoordInMM[0] / m_VoxelDimensionInX;
  m_RayVoxelStartPosition[1] = m_RayStartCoordInMM[1] / m_VoxelDimensionInY;
  m_RayVoxelStartPosition[2] = m_RayStartCoordInMM[2] / m_VoxelDimensionInZ;

  m_RayVoxelEndPosition[0] = m_RayEndCoordInMM[0] / m_VoxelDimensionInX;
  m_RayVoxelEndPosition[1] = m_RayEndCoordInMM[1] / m_VoxelDimensionInY;
  m_RayVoxelEndPosition[2] = m_RayEndCoordInMM[2] / m_VoxelDimensionInZ;
}

/* -----------------------------------------------------------------------
   CalcDirnVector() - Calculate the incremental direction vector in voxels.
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::CalcDirnVector(void)
{
  double xNum, yNum, zNum;

  // Calculate the number of voxels in each direction

  xNum = std::fabs(m_RayVoxelStartPosition[0] - m_RayVoxelEndPosition[0]);
  yNum = std::fabs(m_RayVoxelStartPosition[1] - m_RayVoxelEndPosition[1]);
  zNum = std::fabs(m_RayVoxelStartPosition[2] - m_RayVoxelEndPosition[2]);

  // The direction iterated in is that with the greatest number of voxels
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  // Iterate in X direction

  if ( ( xNum >= yNum ) && ( xNum >= zNum ) )
    {
    if ( m_RayVoxelStartPosition[0] < m_RayVoxelEndPosition[0] )
      {
      m_VoxelIncrement[0] = 1;

      m_VoxelIncrement[1] =
        ( m_RayVoxelStartPosition[1]
          - m_RayVoxelEndPosition[1] ) / ( m_RayVoxelStartPosition[0]
                                           - m_RayVoxelEndPosition[0] );

      m_VoxelIncrement[2] =
        ( m_RayVoxelStartPosition[2]
          - m_RayVoxelEndPosition[2] ) / ( m_RayVoxelStartPosition[0]
                                           - m_RayVoxelEndPosition[0] );
      }
    else
      {
      m_VoxelIncrement[0] = -1;

      m_VoxelIncrement[1] =
        -( m_RayVoxelStartPosition[1]
           - m_RayVoxelEndPosition[1] ) / ( m_RayVoxelStartPosition[0]
                                            - m_RayVoxelEndPosition[0] );

      m_VoxelIncrement[2] =
        -( m_RayVoxelStartPosition[2]
           - m_RayVoxelEndPosition[2] ) / ( m_RayVoxelStartPosition[0]
                                            - m_RayVoxelEndPosition[0] );
      }

    // This section is to alter the start position in order to
    // place the center of the voxels in there correct positions,
    // rather than placing them at the corner of voxels which is
    // what happens if this is not carried out.  The reason why
    // x has no -0.5 is because this is the direction we are going
    // to iterate in and therefore we wish to go from center to
    // center rather than finding the surrounding voxels.

    m_RayVoxelStartPosition[1] += ( (int)m_RayVoxelStartPosition[0]
                                    - m_RayVoxelStartPosition[0] ) * m_VoxelIncrement[1] * m_VoxelIncrement[0]
                                  + 0.5 * m_VoxelIncrement[1] - 0.5;

    m_RayVoxelStartPosition[2] += ( (int)m_RayVoxelStartPosition[0]
                                    - m_RayVoxelStartPosition[0] ) * m_VoxelIncrement[2] * m_VoxelIncrement[0]
                                  + 0.5 * m_VoxelIncrement[2] - 0.5;

    m_RayVoxelStartPosition[0] = (int)m_RayVoxelStartPosition[0] + 0.5 * m_VoxelIncrement[0];

    m_TotalRayVoxelPlanes = (int)xNum;

    m_TraversalDirection = TRANSVERSE_IN_X;
    }

  // Iterate in Y direction

  else if ( ( yNum >= xNum ) && ( yNum >= zNum ) )
    {
    if ( m_RayVoxelStartPosition[1] < m_RayVoxelEndPosition[1] )
      {
      m_VoxelIncrement[1] = 1;

      m_VoxelIncrement[0] =
        ( m_RayVoxelStartPosition[0]
          - m_RayVoxelEndPosition[0] ) / ( m_RayVoxelStartPosition[1]
                                           - m_RayVoxelEndPosition[1] );

      m_VoxelIncrement[2] =
        ( m_RayVoxelStartPosition[2]
          - m_RayVoxelEndPosition[2] ) / ( m_RayVoxelStartPosition[1]
                                           - m_RayVoxelEndPosition[1] );
      }
    else
      {
      m_VoxelIncrement[1] = -1;

      m_VoxelIncrement[0] =
        -( m_RayVoxelStartPosition[0]
           - m_RayVoxelEndPosition[0] ) / ( m_RayVoxelStartPosition[1]
                                            - m_RayVoxelEndPosition[1] );

      m_VoxelIncrement[2] =
        -( m_RayVoxelStartPosition[2]
           - m_RayVoxelEndPosition[2] ) / ( m_RayVoxelStartPosition[1]
                                            - m_RayVoxelEndPosition[1] );
      }

    m_RayVoxelStartPosition[0] += ( (int)m_RayVoxelStartPosition[1]
                                    - m_RayVoxelStartPosition[1] ) * m_VoxelIncrement[0] * m_VoxelIncrement[1]
                                  + 0.5 * m_VoxelIncrement[0] - 0.5;

    m_RayVoxelStartPosition[2] += ( (int)m_RayVoxelStartPosition[1]
                                    - m_RayVoxelStartPosition[1] ) * m_VoxelIncrement[2] * m_VoxelIncrement[1]
                                  + 0.5 * m_VoxelIncrement[2] - 0.5;

    m_RayVoxelStartPosition[1] = (int)m_RayVoxelStartPosition[1] + 0.5 * m_VoxelIncrement[1];

    m_TotalRayVoxelPlanes = (int)yNum;

    m_TraversalDirection = TRANSVERSE_IN_Y;
    }

  // Iterate in Z direction

  else
    {
    if ( m_RayVoxelStartPosition[2] < m_RayVoxelEndPosition[2] )
      {
      m_VoxelIncrement[2] = 1;

      m_VoxelIncrement[0] =
        ( m_RayVoxelStartPosition[0]
          - m_RayVoxelEndPosition[0] ) / ( m_RayVoxelStartPosition[2]
                                           - m_RayVoxelEndPosition[2] );

      m_VoxelIncrement[1] =
        ( m_RayVoxelStartPosition[1]
          - m_RayVoxelEndPosition[1] ) / ( m_RayVoxelStartPosition[2]
                                           - m_RayVoxelEndPosition[2] );
      }
    else
      {
      m_VoxelIncrement[2] = -1;

      m_VoxelIncrement[0] =
        -( m_RayVoxelStartPosition[0]
           - m_RayVoxelEndPosition[0] ) / ( m_RayVoxelStartPosition[2]
                                            - m_RayVoxelEndPosition[2] );

      m_VoxelIncrement[1] =
        -( m_RayVoxelStartPosition[1]
           - m_RayVoxelEndPosition[1] ) / ( m_RayVoxelStartPosition[2]
                                            - m_RayVoxelEndPosition[2] );
      }

    m_RayVoxelStartPosition[0] += ( (int)m_RayVoxelStartPosition[2]
                                    - m_RayVoxelStartPosition[2] ) * m_VoxelIncrement[0] * m_VoxelIncrement[2]
                                  + 0.5 * m_VoxelIncrement[0] - 0.5;

    m_RayVoxelStartPosition[1] += ( (int)m_RayVoxelStartPosition[2]
                                    - m_RayVoxelStartPosition[2] ) * m_VoxelIncrement[1] * m_VoxelIncrement[2]
                                  + 0.5 * m_VoxelIncrement[1] - 0.5;

    m_RayVoxelStartPosition[2] = (int)m_RayVoxelStartPosition[2] + 0.5 * m_VoxelIncrement[2];

    m_TotalRayVoxelPlanes = (int)zNum;

    m_TraversalDirection = TRANSVERSE_IN_Z;
    }
}

/* -----------------------------------------------------------------------
   AdjustRayLength() - Ensure that the ray lies within the volume
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
bool
RayCastHelper< TInputImage, TCoordRep >
::AdjustRayLength(void)
{
  bool startOK, endOK;

  int Istart[3];
  int Idirn[3];

  if ( m_TraversalDirection == TRANSVERSE_IN_X )
    {
    Idirn[0] = 0;
    Idirn[1] = 1;
    Idirn[2] = 1;
    }
  else if ( m_TraversalDirection == TRANSVERSE_IN_Y )
    {
    Idirn[0] = 1;
    Idirn[1] = 0;
    Idirn[2] = 1;
    }
  else if ( m_TraversalDirection == TRANSVERSE_IN_Z )
    {
    Idirn[0] = 1;
    Idirn[1] = 1;
    Idirn[2] = 0;
    }
  else
    {
    itk::ExceptionObject err(__FILE__, __LINE__);
    err.SetLocation(ITK_LOCATION);
    err.SetDescription("The ray traversal direction is unset "
                       "- AdjustRayLength().");
    throw err;
    return false;
    }

  do
    {
    startOK = false;
    endOK = false;

    Istart[0] = (int)std::floor(m_RayVoxelStartPosition[0]);
    Istart[1] = (int)std::floor(m_RayVoxelStartPosition[1]);
    Istart[2] = (int)std::floor(m_RayVoxelStartPosition[2]);

    if ( ( Istart[0] >= 0 ) && ( Istart[0] + Idirn[0] < m_NumberOfVoxelsInX )
         && ( Istart[1] >= 0 ) && ( Istart[1] + Idirn[1] < m_NumberOfVoxelsInY )
         && ( Istart[2] >= 0 ) && ( Istart[2] + Idirn[2] < m_NumberOfVoxelsInZ ) )
      {
      startOK = true;
      }
    else
      {
      m_RayVoxelStartPosition[0] += m_VoxelIncrement[0];
      m_RayVoxelStartPosition[1] += m_VoxelIncrement[1];
      m_RayVoxelStartPosition[2] += m_VoxelIncrement[2];

      m_TotalRayVoxelPlanes--;
      }

    Istart[0] = (int)std::floor(m_RayVoxelStartPosition[0]
                               + m_TotalRayVoxelPlanes * m_VoxelIncrement[0]);

    Istart[1] = (int)std::floor(m_RayVoxelStartPosition[1]
                               + m_TotalRayVoxelPlanes * m_VoxelIncrement[1]);

    Istart[2] = (int)std::floor(m_RayVoxelStartPosition[2]
                               + m_TotalRayVoxelPlanes * m_VoxelIncrement[2]);

    if ( ( Istart[0] >= 0 ) && ( Istart[0] + Idirn[0] < m_NumberOfVoxelsInX )
         && ( Istart[1] >= 0 ) && ( Istart[1] + Idirn[1] < m_NumberOfVoxelsInY )
         && ( Istart[2] >= 0 ) && ( Istart[2] + Idirn[2] < m_NumberOfVoxelsInZ ) )
      {
      endOK = true;
      }
    else
      {
      m_TotalRayVoxelPlanes--;
      }
    }
  while ( ( !( startOK && endOK ) ) && ( m_TotalRayVoxelPlanes > 1 ) );

  return ( startOK && endOK );
}

/* -----------------------------------------------------------------------
   Reset() - Reset the iterator to the start of the ray.
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::Reset(void)
{
  int i;

  m_NumVoxelPlanesTraversed = -1;

  // If this is a valid ray...

  if ( m_ValidRay )
    {
    for ( i = 0; i < 3; i++ )
      {
      m_Position3Dvox[i] = m_RayVoxelStartPosition[i];
      }
    this->InitialiseVoxelPointers();
    }

  // otherwise set parameters to zero

  else
    {
    for ( i = 0; i < 3; i++ )
      {
      m_RayVoxelStartPosition[i] = 0.;
      }
    for ( i = 0; i < 3; i++ )
      {
      m_RayVoxelEndPosition[i] = 0.;
      }
    for ( i = 0; i < 3; i++ )
      {
      m_VoxelIncrement[i] = 0.;
      }
    m_TraversalDirection = UNDEFINED_DIRECTION;

    m_TotalRayVoxelPlanes = 0;

    for ( i = 0; i < 4; i++ )
      {
      m_RayIntersectionVoxels[i] = ITK_NULLPTR;
      }
    for ( i = 0; i < 3; i++ )
      {
      m_RayIntersectionVoxelIndex[i] = 0;
      }
    }
}

/* -----------------------------------------------------------------------
   InitialiseVoxelPointers() - Obtain pointers to the first four voxels
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::InitialiseVoxelPointers(void)
{
  IndexType index;

  int Ix, Iy, Iz;

  Ix = (int)( m_RayVoxelStartPosition[0] );
  Iy = (int)( m_RayVoxelStartPosition[1] );
  Iz = (int)( m_RayVoxelStartPosition[2] );

  m_RayIntersectionVoxelIndex[0] = Ix;
  m_RayIntersectionVoxelIndex[1] = Iy;
  m_RayIntersectionVoxelIndex[2] = Iz;

  switch ( m_TraversalDirection )
    {
    case TRANSVERSE_IN_X:
      {
      if ( ( Ix >= 0 ) && ( Ix     < m_NumberOfVoxelsInX )
           && ( Iy >= 0 ) && ( Iy + 1 < m_NumberOfVoxelsInY )
           && ( Iz >= 0 ) && ( Iz + 1 < m_NumberOfVoxelsInZ ) )
        {
        index[0] = Ix; index[1] = Iy; index[2] = Iz;
        m_RayIntersectionVoxels[0] =
          this->m_Image->GetBufferPointer() + this->m_Image->ComputeOffset(index);

        index[0] = Ix; index[1] = Iy + 1; index[2] = Iz;
        m_RayIntersectionVoxels[1] =
          ( this->m_Image->GetBufferPointer() + this->m_Image->ComputeOffset(index) );

        index[0] = Ix; index[1] = Iy; index[2] = Iz + 1;
        m_RayIntersectionVoxels[2] =
          ( this->m_Image->GetBufferPointer() + this->m_Image->ComputeOffset(index) );

        index[0] = Ix; index[1] = Iy + 1; index[2] = Iz + 1;
        m_RayIntersectionVoxels[3] =
          ( this->m_Image->GetBufferPointer() + this->m_Image->ComputeOffset(index) );
        }
      else
        {
        m_RayIntersectionVoxels[0] =
          m_RayIntersectionVoxels[1] =
            m_RayIntersectionVoxels[2] =
              m_RayIntersectionVoxels[3] = ITK_NULLPTR;
        }
      break;
      }

    case TRANSVERSE_IN_Y:
      {
      if ( ( Ix >= 0 ) && ( Ix + 1 < m_NumberOfVoxelsInX )
           && ( Iy >= 0 ) && ( Iy     < m_NumberOfVoxelsInY )
           && ( Iz >= 0 ) && ( Iz + 1 < m_NumberOfVoxelsInZ ) )
        {
        index[0] = Ix; index[1] = Iy; index[2] = Iz;
        m_RayIntersectionVoxels[0] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );

        index[0] = Ix + 1; index[1] = Iy; index[2] = Iz;
        m_RayIntersectionVoxels[1] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );

        index[0] = Ix; index[1] = Iy; index[2] = Iz + 1;
        m_RayIntersectionVoxels[2] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );

        index[0] = Ix + 1; index[1] = Iy; index[2] = Iz + 1;
        m_RayIntersectionVoxels[3] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );
        }
      else
        {
        m_RayIntersectionVoxels[0] =
          m_RayIntersectionVoxels[1] =
            m_RayIntersectionVoxels[2] =
              m_RayIntersectionVoxels[3] = ITK_NULLPTR;
        }
      break;
      }

    case TRANSVERSE_IN_Z:
      {
      if ( ( Ix >= 0 ) && ( Ix + 1 < m_NumberOfVoxelsInX )
           && ( Iy >= 0 ) && ( Iy + 1 < m_NumberOfVoxelsInY )
           && ( Iz >= 0 ) && ( Iz     < m_NumberOfVoxelsInZ ) )
        {
        index[0] = Ix; index[1] = Iy; index[2] = Iz;
        m_RayIntersectionVoxels[0] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );

        index[0] = Ix + 1; index[1] = Iy; index[2] = Iz;
        m_RayIntersectionVoxels[1] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );

        index[0] = Ix; index[1] = Iy + 1; index[2] = Iz;
        m_RayIntersectionVoxels[2] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );

        index[0] = Ix + 1; index[1] = Iy + 1; index[2] = Iz;
        m_RayIntersectionVoxels[3] = ( this->m_Image->GetBufferPointer()
                                       + this->m_Image->ComputeOffset(index) );
        }
      else
        {
        m_RayIntersectionVoxels[0] =
          m_RayIntersectionVoxels[1] =
            m_RayIntersectionVoxels[2] =
              m_RayIntersectionVoxels[3] = ITK_NULLPTR;
        }
      break;
      }

    default:
      {
      itk::ExceptionObject err(__FILE__, __LINE__);
      err.SetLocation(ITK_LOCATION);
      err.SetDescription("The ray traversal direction is unset "
                         "- InitialiseVoxelPointers().");
      throw err;
      return;
      }
    }
}

/* -----------------------------------------------------------------------
   IncrementVoxelPointers() - Increment the voxel pointers
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::IncrementVoxelPointers(void)
{
  double xBefore = m_Position3Dvox[0].GetSum();
  double yBefore = m_Position3Dvox[1].GetSum();
  double zBefore = m_Position3Dvox[2].GetSum();

  m_Position3Dvox[0] += m_VoxelIncrement[0];
  m_Position3Dvox[1] += m_VoxelIncrement[1];
  m_Position3Dvox[2] += m_VoxelIncrement[2];

  int dx = ( (int)m_Position3Dvox[0].GetSum() ) - ( (int)xBefore );
  int dy = ( (int)m_Position3Dvox[1].GetSum() ) - ( (int)yBefore );
  int dz = ( (int)m_Position3Dvox[2].GetSum() ) - ( (int)zBefore );

  m_RayIntersectionVoxelIndex[0] += dx;
  m_RayIntersectionVoxelIndex[1] += dy;
  m_RayIntersectionVoxelIndex[2] += dz;

  int totalRayVoxelPlanes =
    dx + dy * m_NumberOfVoxelsInX + dz * m_NumberOfVoxelsInX * m_NumberOfVoxelsInY;

  m_RayIntersectionVoxels[0] += totalRayVoxelPlanes;
  m_RayIntersectionVoxels[1] += totalRayVoxelPlanes;
  m_RayIntersectionVoxels[2] += totalRayVoxelPlanes;
  m_RayIntersectionVoxels[3] += totalRayVoxelPlanes;
}

/* -----------------------------------------------------------------------
   GetCurrentIntensity() - Get the intensity of the current ray point.
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
double
RayCastHelper< TInputImage, TCoordRep >
::GetCurrentIntensity(void) const
{
  double a, b, c, d;
  double y, z;

  if ( !m_ValidRay )
    {
    return 0;
    }
  a = (double)( *m_RayIntersectionVoxels[0] );
  b = (double)( *m_RayIntersectionVoxels[1] - a );
  c = (double)( *m_RayIntersectionVoxels[2] - a );
  d = (double)( *m_RayIntersectionVoxels[3] - a - b - c );

  switch ( m_TraversalDirection )
    {
    case TRANSVERSE_IN_X:
      {
      y = m_Position3Dvox[1].GetSum() - std::floor(m_Position3Dvox[1].GetSum());
      z = m_Position3Dvox[2].GetSum() - std::floor(m_Position3Dvox[2].GetSum());
      break;
      }
    case TRANSVERSE_IN_Y:
      {
      y = m_Position3Dvox[0].GetSum() - std::floor(m_Position3Dvox[0].GetSum());
      z = m_Position3Dvox[2].GetSum() - std::floor(m_Position3Dvox[2].GetSum());
      break;
      }
    case TRANSVERSE_IN_Z:
      {
      y = m_Position3Dvox[0].GetSum() - std::floor(m_Position3Dvox[0].GetSum());
      z = m_Position3Dvox[1].GetSum() - std::floor(m_Position3Dvox[1].GetSum());
      break;
      }
    default:
      {
      itk::ExceptionObject err(__FILE__, __LINE__);
      err.SetLocation(ITK_LOCATION);
      err.SetDescription("The ray traversal direction is unset "
                         "- GetCurrentIntensity().");
      throw err;
      }
    }

  return a + b * y + c * z + d * y * z;
}

/* -----------------------------------------------------------------------
   IntegrateAboveThreshold() - Integrate intensities above a threshold.
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
bool
RayCastHelper< TInputImage, TCoordRep >
::IntegrateAboveThreshold(double & integral, double threshold)
{
  double intensity;

//  double posn3D_x, posn3D_y, posn3D_z;

  CompensatedSummationType sum;

  // Check if this is a valid ray

  if ( !m_ValidRay )
    {
    return false;
    }
  /* Step along the ray as quickly as possible
     integrating the interpolated intensities. */

  for ( m_NumVoxelPlanesTraversed = 0;
        m_NumVoxelPlanesTraversed < m_TotalRayVoxelPlanes;
        m_NumVoxelPlanesTraversed++ )
    {
    intensity = this->GetCurrentIntensity();

    if ( intensity > threshold )
      {
      sum += intensity - threshold;
      }
    this->IncrementVoxelPointers();
    }

  /* The ray passes through the volume one plane of voxels at a time,
     however, if its moving diagonally the ray points will be further
     apart so account for this by scaling by the distance moved. */

  integral = sum.GetSum();
  integral *= this->GetRayPointSpacing();

  return true;
}

/* -----------------------------------------------------------------------
   ZeroState() - Set the default (zero) state of the object
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastHelper< TInputImage, TCoordRep >
::ZeroState()
{
  int i;

  m_ValidRay = false;

  m_NumberOfVoxelsInX = 0;
  m_NumberOfVoxelsInY = 0;
  m_NumberOfVoxelsInZ = 0;

  m_VoxelDimensionInX = 0;
  m_VoxelDimensionInY = 0;
  m_VoxelDimensionInZ = 0;

  for ( i = 0; i < 3; i++ )
    {
    m_CurrentRayPositionInMM[i] = 0.;
    }
  for ( i = 0; i < 3; i++ )
    {
    m_RayDirectionInMM[i] = 0.;
    }
  for ( i = 0; i < 3; i++ )
    {
    m_RayVoxelStartPosition[i] = 0.;
    }
  for ( i = 0; i < 3; i++ )
    {
    m_RayVoxelEndPosition[i] = 0.;
    }
  for ( i = 0; i < 3; i++ )
    {
    m_VoxelIncrement[i] = 0.;
    }
  m_TraversalDirection = UNDEFINED_DIRECTION;

  m_TotalRayVoxelPlanes = 0;
  m_NumVoxelPlanesTraversed = -1;

  for ( i = 0; i < 4; i++ )
    {
    m_RayIntersectionVoxels[i] = ITK_NULLPTR;
    }
  for ( i = 0; i < 3; i++ )
    {
    m_RayIntersectionVoxelIndex[i] = 0;
    }
}
}; // end of anonymous namespace

namespace itk
{
/**************************************************************************
 *
 *
 * Rest of this code is the actual RayCastInterpolateImageFunction
 * class
 *
 *
 **************************************************************************/

/* -----------------------------------------------------------------------
   Constructor
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
RayCastInterpolateImageFunction< TInputImage, TCoordRep >
::RayCastInterpolateImageFunction()
{
  m_Threshold = 0.;

  m_FocalPoint[0] = 0.;
  m_FocalPoint[1] = 0.;
  m_FocalPoint[2] = 0.;
}

/* -----------------------------------------------------------------------
   PrintSelf
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
void
RayCastInterpolateImageFunction< TInputImage, TCoordRep >
::PrintSelf(std::ostream & os, Indent indent) const
{
  this->Superclass::PrintSelf(os, indent);

  os << indent << "Threshold: " << m_Threshold << std::endl;
  os << indent << "FocalPoint: " << m_FocalPoint << std::endl;
  os << indent << "Transform: " << m_Transform.GetPointer() << std::endl;
  os << indent << "Interpolator: " << m_Interpolator.GetPointer() << std::endl;
}

/* -----------------------------------------------------------------------
   Evaluate at image index position
   ----------------------------------------------------------------------- */

template< typename TInputImage, typename TCoordRep >
typename RayCastInterpolateImageFunction< TInputImage, TCoordRep >
::OutputType
RayCastInterpolateImageFunction< TInputImage, TCoordRep >
::Evaluate(const PointType & point) const
{
  double integral = 0;

  OutputPointType transformedFocalPoint =
    m_Transform->TransformPoint(m_FocalPoint);

  DirectionType direction = transformedFocalPoint - point;

  RayCastHelper< TInputImage, TCoordRep > ray;
  ray.SetImage(this->m_Image);
  ray.ZeroState();
  ray.Initialise();

  ray.SetRay(point, direction);
  ray.IntegrateAboveThreshold(integral, m_Threshold);

  return ( static_cast< OutputType >( integral ) );
}

template< typename TInputImage, typename TCoordRep >
typename RayCastInterpolateImageFunction< TInputImage, TCoordRep >
::OutputType
RayCastInterpolateImageFunction< TInputImage, TCoordRep >
::EvaluateAtContinuousIndex(const ContinuousIndexType & index) const
{
  OutputPointType point;

  this->m_Image->TransformContinuousIndexToPhysicalPoint(index, point);

  return this->Evaluate(point);
}
} // namespace itk

#endif