File: antsJointHistogramParzenShapeAndOrientationListSampleFunction.hxx

package info (click to toggle)
ants 2.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 10,656 kB
  • sloc: cpp: 84,137; sh: 11,419; perl: 694; xml: 115; makefile: 74; python: 48
file content (696 lines) | stat: -rw-r--r-- 25,441 bytes parent folder | download
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
/*=========================================================================

  Program:   Advanced Normalization Tools
  Module:    $RCSfile: antsJointHistogramParzenShapeAndOrientationListSampleFunction.hxx,v $
  Language:  C++
  Date:      $Date: $
  Version:   $Revision: $

  Copyright (c) ConsortiumOfANTS. All rights reserved.
  See accompanying COPYING.txt or
  http://sourceforge.net/projects/advants/files/ANTS/ANTSCopyright.txt
  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 notices for more information.

=========================================================================*/
#ifndef __antsJointHistogramParzenShapeAndOrientationListSampleFunction_hxx
#define __antsJointHistogramParzenShapeAndOrientationListSampleFunction_hxx

#include "antsJointHistogramParzenShapeAndOrientationListSampleFunction.h"

#include "itkArray.h"
#include "itkContinuousIndex.h"
#include "itkDecomposeTensorFunction.h"
#include "itkDiscreteGaussianImageFilter.h"
#include "itkDivideByConstantImageFilter.h"
#include "itkStatisticsImageFilter.h"

namespace itk
{
namespace ants
{
namespace Statistics
{
template <class TListSample, class TOutput, class TCoordRep>
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::JointHistogramParzenShapeAndOrientationListSampleFunction()
{
  this->m_NumberOfShapeJointHistogramBins = 32;
  this->m_NumberOfOrientationJointHistogramBins = 64;
  this->m_ShapeSigma = 1.0;
  this->m_OrientationSigma = 2.0;
  this->m_UseNearestNeighborIncrements = true;
  this->m_MaximumEigenvalue1 = 0;
  this->m_MaximumEigenvalue2 = 0;
  this->m_MinimumEigenvalue1 = 1;
  this->m_MinimumEigenvalue2 = 1;
  this->m_Interpolator = InterpolatorType::New();
  this->m_Interpolator->SetSplineOrder( 3 );
  this->m_JointHistogramImages[0] = ITK_NULLPTR;
  this->m_JointHistogramImages[1] = ITK_NULLPTR;
  this->m_JointHistogramImages[2] = ITK_NULLPTR;
}

template <class TListSample, class TOutput, class TCoordRep>
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::~JointHistogramParzenShapeAndOrientationListSampleFunction()
{
}

template <class TListSample, class TOutput, class TCoordRep>
void
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::IncrementJointHistogramForShape( RealType eigenvalue1, RealType eigenvalue2 )
{
  RealType newWeight = 1.0;

  // now define two joint histograms, one for shape, one for orientation.
  // first, the shape histogram --- 0,0 origin and spacing of 1
  if( !this->m_JointHistogramImages[0] )
    {
    typename JointHistogramImageType::SpacingType spacing;
    spacing.Fill( 1 );
    typename JointHistogramImageType::PointType origin;
    origin.Fill( 0 );
    typename JointHistogramImageType::SizeType size;
    size.Fill( this->m_NumberOfShapeJointHistogramBins );
    this->m_JointHistogramImages[0] =
      AllocImage<JointHistogramImageType>(size);
    this->m_JointHistogramImages[0]->SetOrigin( origin );
    this->m_JointHistogramImages[0]->SetSpacing( spacing );
    this->m_JointHistogramImages[0]->FillBuffer( 0 );
    }

  typename JointHistogramImageType::PointType shapePoint;
  if( eigenvalue1 > 1.0 )
    {
    eigenvalue1 = 1.0;
    }
  if( eigenvalue2 > 1.0 )
    {
    eigenvalue2 = 1.0;
    }
  if( eigenvalue1 < 0.0 )
    {
    eigenvalue1 = 0.0;
    }
  if( eigenvalue2 < 0 )
    {
    eigenvalue2 = 0.0;
    }

  shapePoint[0] = eigenvalue1 * ( this->m_NumberOfShapeJointHistogramBins - 1 );
  shapePoint[1] = eigenvalue2 * ( this->m_NumberOfShapeJointHistogramBins - 1 );

  ContinuousIndex<double, 2> shapeCidx;
  this->m_JointHistogramImages[0]->TransformPhysicalPointToContinuousIndex(
    shapePoint, shapeCidx );

  typedef typename JointHistogramImageType::IndexType JointHistogramImageIndexType;
  JointHistogramImageIndexType shapeIdx;

  /** Nearest neighbor increment to JH */
  if( this->m_UseNearestNeighborIncrements )
    {
    shapeIdx[0] = static_cast<typename JointHistogramImageIndexType::IndexValueType>( vcl_floor( shapeCidx[0] + 0.5 ) );
    shapeIdx[1] = static_cast<typename JointHistogramImageIndexType::IndexValueType>( vcl_floor( shapeCidx[1] + 0.5 ) );
    if( this->m_JointHistogramImages[0]->
        GetLargestPossibleRegion().IsInside( shapeIdx ) )
      {
      RealType oldWeight = this->m_JointHistogramImages[0]->GetPixel( shapeIdx );
      this->m_JointHistogramImages[0]->SetPixel( shapeIdx, 1 + oldWeight );
      }
    }
  else
    {
    /** linear addition */
    shapeIdx[0] = static_cast<IndexValueType>( vcl_floor( shapeCidx[0] ) );
    shapeIdx[1] = static_cast<IndexValueType>( vcl_floor( shapeCidx[1] ) );
    RealType distance1 = vcl_sqrt( vnl_math_sqr( shapeCidx[0] - shapeIdx[0] )
                                   + vnl_math_sqr( shapeCidx[1] - shapeIdx[1] ) );
    shapeIdx[0]++;
    RealType distance2 = vcl_sqrt( vnl_math_sqr( shapeCidx[0] - shapeIdx[0] )
                                   + vnl_math_sqr( shapeCidx[1] - shapeIdx[1] ) );
    shapeIdx[1]++;
    RealType distance3 = vcl_sqrt( vnl_math_sqr( shapeCidx[0] - shapeIdx[0] )
                                   + vnl_math_sqr( shapeCidx[1] - shapeIdx[1] ) );
    shapeIdx[0]--;
    RealType distance4 = vcl_sqrt( vnl_math_sqr( shapeCidx[0] - shapeIdx[0] )
                                   + vnl_math_sqr( shapeCidx[1] - shapeIdx[1] ) );
    RealType sumDistance = distance1 + distance2 + distance3 + distance4;
    distance1 /= sumDistance;
    distance2 /= sumDistance;
    distance3 /= sumDistance;
    distance4 /= sumDistance;

    unsigned int whichHistogram = 0;
    shapeIdx[0] = static_cast<IndexValueType>( vcl_floor( shapeCidx[0] ) );
    shapeIdx[1] = static_cast<IndexValueType>( vcl_floor( shapeCidx[1] ) );
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( shapeIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( shapeIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( shapeIdx,
                                                              ( 1.0 - distance1 ) * newWeight + oldWeight );
      }
    shapeIdx[0]++;
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( shapeIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( shapeIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( shapeIdx,
                                                              ( 1.0 - distance2 ) * newWeight + oldWeight );
      }
    shapeIdx[1]++;
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( shapeIdx ) )
      {
      RealType oldWeight
        = this->m_JointHistogramImages[whichHistogram]->GetPixel( shapeIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( shapeIdx,
                                                              ( 1.0 - distance3 ) * newWeight + oldWeight );
      }
    shapeIdx[0]--;
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( shapeIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( shapeIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( shapeIdx,
                                                              ( 1.0 - distance4) * newWeight + oldWeight );
      }
    }
  return;
}

template <class TListSample, class TOutput, class TCoordRep>
void
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::IncrementJointHistogramForOrientation(
  RealType x, RealType y, RealType z, unsigned int whichHistogram )
{
  RealType newWeight = 1.0;

  // 2nd, the orientation histogram.  origin 0,0.  spacing of 1,1.
  //  need to be careful for wrap around in the 0 to 2*pi case.
  if( !this->m_JointHistogramImages[whichHistogram] )
    {
    typename JointHistogramImageType::SpacingType spacing2;
    spacing2.Fill(1);
    typename JointHistogramImageType::PointType origin2;
    origin2.Fill(0);
    typename JointHistogramImageType::SizeType size2;
    size2.Fill( this->m_NumberOfOrientationJointHistogramBins );
    size2[0] = size2[0] + 2;
    this->m_JointHistogramImages[whichHistogram] =
      AllocImage<JointHistogramImageType>(size2);

    this->m_JointHistogramImages[whichHistogram]->SetOrigin( origin2 );
    this->m_JointHistogramImages[whichHistogram]->SetSpacing( spacing2 );
    this->m_JointHistogramImages[whichHistogram]->FillBuffer( 0 );
    }

  typename JointHistogramImageType::PointType orientPoint;
  RealType tp[2];
  tp[1] = 0.0;

  // If eigenvector has negative x, we reflect it about the origin.
  // We do this to avoid redundancy in representation of the eigenvectors,
  // because they are all redundant.

  if( x < 0 )
    {
    x *= -1;
    y *= -1;
    z *= -1;
    }

  tp[0] = vcl_acos( z );

  // phi goes from 0.0 (+x axis) and goes to -pi/2 and pi/2.
  // theta goes from 0.0 (+z axis) and wraps at PI
  // if x and y are 0.0 or very close, return phi == 0
  // we do this to eliminate redundancy in the distribution of orientations.

  if( vnl_math_abs( x ) + vnl_math_abs( y ) < 1e-9 )
    {
    tp[1] = 0.0;
    }
  else
    {
    if( y == 0.0 )
      {
      if( x > 0.0 )
        {
        tp[1] = 0.0;
        }
      else
        {
        tp[1] = vnl_math::pi;
        }
      }
    else if( x == 0.0 )
      {
      // avoid div by zero
      if( y > 0 )
        {
        tp[1] = vnl_math::pi_over_2;
        }
      else
        {
        tp[1] = -vnl_math::pi_over_2;
        }
      }
    else if( x > 0.0 && y > 0.0 )
      {     // first quadrant
      tp[1] = vcl_atan( y / x );
      }
    else if( x < 0.0 && y > 0.0 )
      {     // second quadrant
      tp[1] = vnl_math::pi + vcl_atan( y / x );
      }
    else if( x < 0.0 && y < 0.0 )
      {     // third quadrant
      tp[1] =  vnl_math::pi + atan( y / x );
      }
    else
      {     // fourth quadrant
      tp[1] = atan( y / x );
      }
    }
  RealType psi = tp[0];
  RealType theta = tp[1];

// note, if a point maps to 0 or 2*pi then it should contribute to both bins -- pretty much only difference between this
// function and matlab code is the next 15 or so lines, as far as we see
  orientPoint[0] = psi / (vnl_math::pi ) *
    ( this->m_NumberOfOrientationJointHistogramBins - 1) + 1;
  orientPoint[1] = ( theta + vnl_math::pi_over_2 ) / vnl_math::pi
    * ( this->m_NumberOfOrientationJointHistogramBins - 1 );

  ContinuousIndex<double, 2> orientCidx;
  this->m_JointHistogramImages[whichHistogram]->
  TransformPhysicalPointToContinuousIndex( orientPoint, orientCidx );

  typedef typename JointHistogramImageType::IndexType JointHistogramImageIndexType;
  JointHistogramImageIndexType orientIdx;

  /** Nearest neighbor interpolation */
  if( this->m_UseNearestNeighborIncrements )
    {
    orientIdx[0] =
      static_cast<typename JointHistogramImageIndexType::IndexValueType>( vcl_floor( orientCidx[0] + 0.5 ) );
    orientIdx[1] =
      static_cast<typename JointHistogramImageIndexType::IndexValueType>( vcl_floor( orientCidx[1] + 0.5 ) );
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( orientIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( orientIdx );
      this->m_JointHistogramImages[whichHistogram]->
      SetPixel( orientIdx, 1 + oldWeight );
      }
    }
  else
    {
    orientIdx[0] = static_cast<IndexValueType>( vcl_floor( orientCidx[0] ) );
    orientIdx[1] = static_cast<IndexValueType>( vcl_floor( orientCidx[1] ) );
    RealType distance1 = vcl_sqrt( vnl_math_sqr( orientCidx[0] - orientIdx[0] )
                                   + vnl_math_sqr( orientCidx[1] - orientIdx[1] ) );
    orientIdx[0]++;
    RealType distance2 = vcl_sqrt( vnl_math_sqr( orientCidx[0] - orientIdx[0] )
                                   + vnl_math_sqr( orientCidx[1] - orientIdx[1] ) );
    orientIdx[1]++;
    RealType distance3 = vcl_sqrt( vnl_math_sqr( orientCidx[0] - orientIdx[0] )
                                   + vnl_math_sqr( orientCidx[1] - orientIdx[1] ) );
    orientIdx[0]--;
    RealType distance4 = vcl_sqrt( vnl_math_sqr( orientCidx[0] - orientIdx[0] )
                                   + vnl_math_sqr( orientCidx[1] - orientIdx[1] ) );
    RealType sumDistance = distance1 + distance2 + distance3 + distance4;
    distance1 /= sumDistance;
    distance2 /= sumDistance;
    distance3 /= sumDistance;
    distance4 /= sumDistance;

    orientIdx[0] = static_cast<IndexValueType>( vcl_floor( orientCidx[0] ) );
    orientIdx[1] = static_cast<IndexValueType>( vcl_floor( orientCidx[1] ) );
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( orientIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( orientIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( orientIdx,
                                                              ( 1.0 - distance1 ) * newWeight + oldWeight );
      }
    orientIdx[0]++;
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( orientIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( orientIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( orientIdx,
                                                              ( 1.0 - distance2 ) * newWeight + oldWeight );
      }
    orientIdx[1]++;
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( orientIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel( orientIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( orientIdx,
                                                              ( 1.0 - distance3 ) * newWeight + oldWeight );
      }
    orientIdx[0]--;
    if( this->m_JointHistogramImages[whichHistogram]->
        GetLargestPossibleRegion().IsInside( orientIdx ) )
      {
      RealType oldWeight =
        this->m_JointHistogramImages[whichHistogram]->GetPixel(orientIdx );
      this->m_JointHistogramImages[whichHistogram]->SetPixel( orientIdx,
                                                              ( 1.0 - distance4) * newWeight + oldWeight );
      }
    }

  // The last thing we do is copy the [1,] column to the [NBins+1,] column and
  // the [NBins,] column to the [0,] column --- circular boundary conditions.

  typedef itk::ImageRegionIteratorWithIndex<JointHistogramImageType> Iterator;
  Iterator tIter( this->m_JointHistogramImages[whichHistogram],
                  this->m_JointHistogramImages[whichHistogram]->GetBufferedRegion() );
  for( tIter.GoToBegin(); !tIter.IsAtEnd(); ++tIter )
    {
    IndexType index = tIter.GetIndex();
    IndexType index2 = tIter.GetIndex();
    if( index[0] == 0 )
      {
      index2[0] = this->m_NumberOfOrientationJointHistogramBins;
      index2[1] = index[1];
      tIter.Set(
        this->m_JointHistogramImages[whichHistogram]->GetPixel( index2 ) );
      }
    if( index[0] ==
        static_cast<typename IndexType::IndexValueType>
        (this->m_NumberOfOrientationJointHistogramBins + 1) )
      {
      index2[0] = 1;
      index2[1] = index[1];
      tIter.Set(
        this->m_JointHistogramImages[whichHistogram]->GetPixel( index2 ) );
      }
    ++tIter;
    }
  return;
}

template <class TListSample, class TOutput, class TCoordRep>
void
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::SetInputListSample( const InputListSampleType * ptr )
{
  Superclass::SetInputListSample( ptr );

  if( !this->GetInputListSample() )
    {
    return;
    }

  if( this->GetInputListSample()->Size() <= 1 )
    {
    itkWarningMacro( "The input list sample has <= 1 element."
                     << "Function evaluations will be equal to 0." );
    return;
    }

  const unsigned int Dimension =
    this->GetInputListSample()->GetMeasurementVectorSize();

  /**
   * Find the min/max values to define the histogram domain
   */
  Array<RealType> minValues( Dimension );
  minValues.Fill( NumericTraits<RealType>::max() );
  Array<RealType> maxValues( Dimension );
  maxValues.Fill( NumericTraits<RealType>::NonpositiveMin() );

  typename InputListSampleType::ConstIterator It
    = this->GetInputListSample()->Begin();
  while( It != this->GetInputListSample()->End() )
    {
    InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
    for( unsigned int d = 0; d < Dimension; d++ )
      {
      if( inputMeasurement[d] < minValues[d] )
        {
        minValues[d] = inputMeasurement[d];
        }
      if( inputMeasurement[d] > maxValues[d] )
        {
        maxValues[d] = inputMeasurement[d];
        }
      }
    ++It;
    }
  for( unsigned int d = 0; d < 3; d++ )
    {
    this->m_JointHistogramImages[d] = ITK_NULLPTR;
    }

  RealType L = static_cast<RealType>(
      this->GetInputListSample()->GetMeasurementVectorSize() );
  unsigned int D = static_cast<unsigned int>( 0.5 * ( -1 + vcl_sqrt( 1.0
                                                                     + 8.0 * L ) ) );

  It = this->GetInputListSample()->Begin();
  while( It != this->GetInputListSample()->End() )
    {
    InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
    // convert to a tensor then get its shape and primary orientation vector
    typedef VariableSizeMatrix<RealType> TensorType;
    TensorType T( D, D );
    T.Fill( 0.0 );
    unsigned int index = 0;
    for( unsigned int i = 0; i < D; i++ )
      {
      for( unsigned int j = i; j < D; j++ )
        {
        T(i, j) = inputMeasurement( index++ );
        T(j, i) = T(i, j);
        }
      }
    // now decompose T into shape and orientation
    TensorType V;
    TensorType W;
    TensorType Tc = T;
    typedef DecomposeTensorFunction<TensorType> DecomposerType;
    typename DecomposerType::Pointer decomposer = DecomposerType::New();
    decomposer->EvaluateSymmetricEigenDecomposition( Tc, W, V );
    // now W holds the eigenvalues ( shape )

    // for each tensor sample, we add its content to the relevant histogram.
    RealType eigenvalue1 = W(2, 2);
    RealType eigenvalue2 = ( W(1, 1) ); // + W(0, 0) ) * 0.5;
    if( eigenvalue1 > this->m_MaximumEigenvalue1 )
      {
      this->m_MaximumEigenvalue1 = eigenvalue1;
      }
    if( eigenvalue2 > this->m_MaximumEigenvalue2 )
      {
      this->m_MaximumEigenvalue2 = eigenvalue2;
      }
    if( eigenvalue1 < this->m_MinimumEigenvalue1 )
      {
      this->m_MinimumEigenvalue1 = eigenvalue1;
      }
    if( eigenvalue2 < this->m_MinimumEigenvalue2 )
      {
      this->m_MinimumEigenvalue2 = eigenvalue2;
      }
    ++It;
    }

  It = this->GetInputListSample()->Begin();
  while( It != this->GetInputListSample()->End() )
    {
    InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
    // convert to a tensor then get its shape and primary orientation vector
    typedef VariableSizeMatrix<RealType> TensorType;
    TensorType T( D, D );
    T.Fill( 0.0 );
    unsigned int index = 0;
    for( unsigned int i = 0; i < D; i++ )
      {
      for( unsigned int j = i; j < D; j++ )
        {
        T(i, j) = inputMeasurement( index++ );
        T(j, i) = T(i, j);
        }
      }
    // now decompose T into shape and orientation
    TensorType V;
    TensorType W;
    TensorType Tc = T;
    typedef DecomposeTensorFunction<TensorType> DecomposerType;
    typename DecomposerType::Pointer decomposer = DecomposerType::New();
    decomposer->EvaluateSymmetricEigenDecomposition( Tc, W, V );
    // now W holds the eigenvalues ( shape )

    // for each tensor sample, we add its content to the relevant histogram.
    RealType eigenvalue1 = W(2, 2);
    RealType eigenvalue2 = W(1, 1);
    eigenvalue1 /= ( this->m_MaximumEigenvalue1 - this->m_MinimumEigenvalue1 );
    eigenvalue2 /= ( this->m_MaximumEigenvalue2 - this->m_MinimumEigenvalue2 );

    //    std::cout << " ev1 " << eigenvalue1 << " oev1 " << W(2,2) << " ev2 " << eigenvalue2 << " oev2 " <<
    // W(1,1) <<
    // std::endl;

    /** joint-hist model for the eigenvalues */
    this->IncrementJointHistogramForShape( eigenvalue1, eigenvalue2 );

    RealType x = V(0, 2);
    RealType y = V(1, 2);
    RealType z = V(2, 2);

    /** joint-hist model for the principal eigenvector */
    this->IncrementJointHistogramForOrientation( x, y, z, 1 );
    x = V(0, 1);
    y = V(1, 1);
    z = V(2, 1);
    /** joint-hist model for the second eigenvector */
    this->IncrementJointHistogramForOrientation( x, y, z, 2 );

    ++It;
    }
  for( unsigned int d = 0; d < 3; d++ )
    {
    typedef DiscreteGaussianImageFilter<JointHistogramImageType,
                                        JointHistogramImageType> GaussianFilterType;
    typename GaussianFilterType::Pointer gaussian = GaussianFilterType::New();
    gaussian->SetInput( this->m_JointHistogramImages[d] );
    if( d == 0 )   // Shape
      {
      gaussian->SetVariance( this->m_ShapeSigma * this->m_ShapeSigma );
      }
    else if( d == 1 )   // Orientation of 1st eigenvector
      {
      gaussian->SetVariance(this->m_OrientationSigma * this->m_OrientationSigma );
      }
    else if( d == 2 )   // Orientation of 2nd eigenvector
      {
      gaussian->SetVariance( this->m_ShapeSigma * this->m_OrientationSigma );
      }
    gaussian->SetMaximumError( 0.01 );
    gaussian->SetUseImageSpacing( false );
    gaussian->Update();

    typedef StatisticsImageFilter<JointHistogramImageType> StatsFilterType;
    typename StatsFilterType::Pointer stats = StatsFilterType::New();
    stats->SetInput( gaussian->GetOutput() );
    stats->Update();

    typedef DivideByConstantImageFilter<JointHistogramImageType, RealType,
                                        JointHistogramImageType> DividerType;
    typename DividerType::Pointer divider = DividerType::New();
    divider->SetInput( gaussian->GetOutput() );
    divider->SetConstant( stats->GetSum() );
    divider->Update();
    this->m_JointHistogramImages[d] = divider->GetOutput();
    }
/*  write out histograms--for debugging
    static int which_class=0;
    which_class++;
    std::string string;
    std::stringstream outstring;
    outstring<<which_class;
    string=outstring.str();
    typedef ImageFileWriter< JointHistogramImageType >  WriterType;
    typename WriterType::Pointer      writer = WriterType::New();
    std::string output( "output_shape"+string+".nii.gz" );
    writer->SetFileName( output.c_str() );
    writer->SetInput(this->m_JointHistogramImages[0] );
    writer->Update();
    typedef ImageFileWriter< JointHistogramImageType >  WriterType2;
    typename WriterType2::Pointer      writer2 = WriterType::New();
    std::string output2( "output_orientation"+string+".nii.gz" );
    writer2->SetFileName( output2.c_str() );
    writer2->SetInput(this->m_JointHistogramImages[1] );
    writer2->Update();
    std::cout << "Writing output of histograms." << std::endl;
*/
}

template <class TListSample, class TOutput, class TCoordRep>
TOutput
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::Evaluate( const InputMeasurementVectorType & measurement ) const
{
  try
    {
    RealType probability = 1.0;
    for( unsigned int d = 0; d < 2; d++ )
      {
      typename JointHistogramImageType::PointType point;
      point[0] = measurement[d];

      this->m_Interpolator->SetInputImage( this->m_JointHistogramImages[d] );
      if( this->m_Interpolator->IsInsideBuffer( point ) )
        {
        probability *= this->m_Interpolator->Evaluate( point );
        }
      else
        {
        return 0;
        }
      }
    return probability;
    }
  catch( ... )
    {
    return 0;
    }
}

/**
 * Standard "PrintSelf" method
 */
template <class TListSample, class TOutput, class TCoordRep>
void
JointHistogramParzenShapeAndOrientationListSampleFunction<TListSample, TOutput, TCoordRep>
::PrintSelf(
  std::ostream& os,
  Indent indent) const
{
  os << indent << "Shape Sigma: " << this->m_ShapeSigma << std::endl;
  os << indent << "Number of shape histogram bins: "
     << this->m_NumberOfShapeJointHistogramBins << std::endl;
  os << indent << "Orientation Sigma: " << this->m_OrientationSigma << std::endl;
  os << indent << "Number of orientation histogram bins: "
     << this->m_NumberOfOrientationJointHistogramBins << std::endl;
  os << indent << "Minimum eigenvalue 1: "
     << this->m_MinimumEigenvalue1;
  os << indent << "Minimum eigenvalue 2: "
     << this->m_MinimumEigenvalue2;
  os << indent << "Maximum eigenvalue 1: "
     << this->m_MaximumEigenvalue1;
  os << indent << "Maximum eigenvalue 2: "
     << this->m_MaximumEigenvalue2;

  if( this->m_UseNearestNeighborIncrements )
    {
    os << indent << "Use nearest neighbor increments." << std::endl;
    }
  else
    {
    os << indent << "Use linear interpolation for increments." << std::endl;
    }
}
} // end of namespace Statistics
} // end of namespace ants
} // end of namespace itk

#endif