This file is indexed.

/usr/include/ITK-4.5/itkHistogram.hxx is in libinsighttoolkit4-dev 4.5.0-3.

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
/*=========================================================================
 *
 *  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 __itkHistogram_hxx
#define __itkHistogram_hxx

#include "itkHistogram.h"
#include "itkNumericTraits.h"

namespace itk
{
namespace Statistics
{
template< typename TMeasurement, typename TFrequencyContainer >
Histogram< TMeasurement, TFrequencyContainer >
::Histogram()
{
  this->m_ClipBinsAtEnds = true;
  this->m_FrequencyContainer = FrequencyContainerType::New();
  this->m_OffsetTable = OffsetTableType(this->GetMeasurementVectorSize() + 1);
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize() + 1; i++ )
    {
    this->m_OffsetTable[i] = itk::NumericTraits< InstanceIdentifier >::Zero;
    }
}

template< typename TMeasurement, typename TFrequencyContainer >
typename
Histogram< TMeasurement, TFrequencyContainer >::InstanceIdentifier
Histogram< TMeasurement, TFrequencyContainer >
::Size() const
{
  if ( this->GetMeasurementVectorSize() == 0 )
    {
    return itk::NumericTraits< InstanceIdentifier >::Zero;
    }
  InstanceIdentifier size = 1;
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
    {
    size *= m_Size[i];
    }
  return size;
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::SizeType &
Histogram< TMeasurement, TFrequencyContainer >
::GetSize() const
{
  return m_Size;
}

template< typename TMeasurement, typename TFrequencyContainer >
typename
Histogram< TMeasurement, TFrequencyContainer >::SizeValueType
Histogram< TMeasurement, TFrequencyContainer >
::GetSize(unsigned int dimension) const
{
  return m_Size[dimension];
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::MeasurementType &
Histogram< TMeasurement, TFrequencyContainer >
::GetBinMin(unsigned int dimension, InstanceIdentifier nbin) const
{
  return m_Min[dimension][nbin];
}

template< typename TMeasurement,
          typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::MeasurementType &
Histogram< TMeasurement, TFrequencyContainer >
::GetBinMax(unsigned int dimension, InstanceIdentifier nbin) const
{
  return m_Max[dimension][nbin];
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::SetBinMin(unsigned int dimension, InstanceIdentifier nbin,
            MeasurementType min)
{
  m_Min[dimension][nbin] = min;
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::SetBinMax(unsigned int dimension, InstanceIdentifier nbin,
            MeasurementType max)
{
  m_Max[dimension][nbin] = max;
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::BinMinVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetDimensionMins(unsigned int dimension) const
{
  return m_Min[dimension];
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::BinMaxVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetDimensionMaxs(unsigned int dimension) const
{
  return m_Max[dimension];
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::BinMinContainerType &
Histogram< TMeasurement, TFrequencyContainer >
::GetMins() const
{
  return m_Min;
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::BinMaxContainerType &
Histogram< TMeasurement, TFrequencyContainer >
::GetMaxs() const
{
  return m_Max;
}

template< typename TMeasurement, typename TFrequencyContainer >
typename
Histogram< TMeasurement, TFrequencyContainer >::AbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetFrequency(InstanceIdentifier id) const
{
  return m_FrequencyContainer->GetFrequency(id);
}

template< typename TMeasurement, typename TFrequencyContainer >
bool
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType value)
{
  return m_FrequencyContainer->SetFrequency(id, value);
}

template< typename TMeasurement, typename TFrequencyContainer >
bool
Histogram< TMeasurement, TFrequencyContainer >
::IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value)
{
  return m_FrequencyContainer->IncreaseFrequency(id, value);
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::Initialize(const SizeType & size)
{
  if ( this->GetMeasurementVectorSize() == 0 )
    {
    itkExceptionMacro("MeasurementVectorSize is Zero. It should be set to a non-zero value before calling Initialize");
    }

  this->m_Size = size;

  // creates offset table which will be used for generation of
  // instance identifiers.
  InstanceIdentifier num = 1;

  this->m_OffsetTable.resize(this->GetMeasurementVectorSize() + 1);

  this->m_OffsetTable[0] = num;
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
    {
    num *= m_Size[i];
    this->m_OffsetTable[i + 1] = num;
    }

  this->m_TempIndex.SetSize( this->GetMeasurementVectorSize() );

  m_NumberOfInstances = num;

  // adjust the sizes of min max value containers
  unsigned int dim;
  m_Min.resize( this->GetMeasurementVectorSize() );
  for ( dim = 0; dim < this->GetMeasurementVectorSize(); dim++ )
    {
    m_Min[dim].resize(m_Size[dim]);
    }

  m_Max.resize( this->GetMeasurementVectorSize() );
  for ( dim = 0; dim < this->GetMeasurementVectorSize(); dim++ )
    {
    m_Max[dim].resize(m_Size[dim]);
    }

  // initialize auxiliary variables
  this->m_TempIndex.SetSize( this->GetMeasurementVectorSize() );
  this->m_TempMeasurementVector.SetSize( this->GetMeasurementVectorSize() );

  // initialize the frequency container
  m_FrequencyContainer->Initialize(this->m_OffsetTable[this->GetMeasurementVectorSize()]);
  this->SetToZero();
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::SetToZero()
{
  m_FrequencyContainer->SetToZero();
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::Initialize(const SizeType & size, MeasurementVectorType & lowerBound,
             MeasurementVectorType & upperBound)
{
  this->Initialize(size);

  float interval;
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
    {
    if ( size[i] > 0 )
      {
      interval = static_cast<float>( upperBound[i] - lowerBound[i] )
        / static_cast< MeasurementType >( size[i] );

      // Set the min vector and max vector
      for ( unsigned int j = 0; j < static_cast< unsigned int >( size[i] - 1 ); j++ )
        {
        this->SetBinMin( i, j, (MeasurementType)( lowerBound[i]
                                                  + ( (float)j * interval ) ) );
        this->SetBinMax( i, j, (MeasurementType)( lowerBound[i]
                                                  + ( ( (float)j + 1 ) * interval ) ) );
        }
      this->SetBinMin( i, size[i] - 1,
                       (MeasurementType)( lowerBound[i]
                                          + ( ( (float)size[i] - 1 ) * interval ) ) );
      this->SetBinMax( i, size[i] - 1,
                       (MeasurementType)( upperBound[i] ) );
      }
    }
}

/** */
template< typename TMeasurement, typename TFrequencyContainer >
bool Histogram< TMeasurement, TFrequencyContainer >
::GetIndex(const MeasurementVectorType & measurement, IndexType & index) const
{
  // now using something similar to binary search to find
  // index.
  unsigned int dim;

  if ( index.Size() != this->GetMeasurementVectorSize() )
    {
    index.SetSize( this->GetMeasurementVectorSize() );
    }

  int begin;
  int mid;
  int end;

  MeasurementType median;
  MeasurementType tempMeasurement;

  for ( dim = 0; dim < this->GetMeasurementVectorSize(); dim++ )
    {
    tempMeasurement = measurement[dim];
    begin = 0;
    if ( tempMeasurement < m_Min[dim][begin] )
      {
      // one of measurement is below the minimum
      // its ok if we extend the bins to infinity.. not ok if we don't
      if ( !m_ClipBinsAtEnds )
        {
        index[dim] = (IndexValueType)0;
        continue;
        }
      else
        {   // set an illegal value and return 0
        index[dim] = (IndexValueType)m_Size[dim];
        return false;
        }
      }

    end = m_Min[dim].size() - 1;
    if ( tempMeasurement >= m_Max[dim][end] )
      {
      // one of measurement is above the maximum
      // its ok if we extend the bins to infinity.. not ok if we don't
      // Need to include the last endpoint in the last bin.
      if ( !m_ClipBinsAtEnds || tempMeasurement ==  m_Max[dim][end])
        {
        index[dim] = (IndexValueType)m_Size[dim] - 1;
        continue;
        }
      else
        {   // set an illegal value and return 0
        index[dim] = (IndexValueType)m_Size[dim];
        return false;
        }
      }

    // Binary search for the bin where this measurement could be
    mid = ( end + 1 ) / 2;
    median = m_Min[dim][mid];

    while ( true )
      {
      if ( tempMeasurement < median )
        {
        end = mid - 1;
        }
      else if ( tempMeasurement > median )
        {
        // test whether it is inside the current bin by comparing to the max of
        // this bin.
        if ( tempMeasurement <  m_Max[dim][mid]
             && tempMeasurement >= m_Min[dim][mid] )
          {
          index[dim] = mid;
          break;
          }
        // otherwise, continue binary search
        begin = mid + 1;
        }
      else
        {
        index[dim] = mid;
        break;
        }
      mid = begin + ( end - begin ) / 2;
      median = m_Min[dim][mid];
      } // end of while
    }   // end of for()
  return true;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::IndexType &
Histogram< TMeasurement, TFrequencyContainer >
::GetIndex(InstanceIdentifier id) const
{
  InstanceIdentifier id2 = id;

  for ( int i = this->GetMeasurementVectorSize() - 1; i > 0; i-- )
    {
    m_TempIndex[i] = static_cast< IndexValueType >( id2 / this->m_OffsetTable[i] );
    id2 -= ( m_TempIndex[i] * this->m_OffsetTable[i] );
    }
  m_TempIndex[0] = static_cast< IndexValueType >( id2 );

  return m_TempIndex;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::IsIndexOutOfBounds(const IndexType & index) const
{
  for ( unsigned int dim = 0; dim < this->GetMeasurementVectorSize(); dim++ )
    {
    if ( index[dim] < 0 || index[dim] >= static_cast< IndexValueType >( m_Size[dim] ) )
      {
      return true;
      }
    }
  return false;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline typename Histogram< TMeasurement, TFrequencyContainer >::InstanceIdentifier
Histogram< TMeasurement, TFrequencyContainer >
::GetInstanceIdentifier(const IndexType & index) const
{
  InstanceIdentifier instanceId = 0;

  for ( int i = this->GetMeasurementVectorSize() - 1; i > 0; i-- )
    {
    instanceId += index[i] * this->m_OffsetTable[i];
    }

  instanceId += index[0];

  return instanceId;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementType &
Histogram< TMeasurement, TFrequencyContainer >
::GetBinMinFromValue(const unsigned int dimension, const float value) const
{
  // If the value is lower than any of min value in the Histogram,
  // it returns the lowest min value
  if ( value <= this->m_Min[dimension][0] )
    {
    return this->m_Min[dimension][0];
    }

  // If the value is higher than any of min value in the Histogram,
  // it returns the highest min value
  if ( value >= m_Min[dimension][m_Size[dimension] - 1] )
    {
    return m_Min[dimension][this->m_Size[dimension] - 1];
    }

  unsigned int binMinFromValue = 0;

  for ( unsigned int i = 0; i < this->m_Size[dimension]; i++ )
    {
    if ( ( value >= this->m_Min[dimension][i] )
         && ( value <  this->m_Max[dimension][i] ) )
      {
      binMinFromValue = i;
      }
    }

  return this->m_Min[dimension][binMinFromValue];
}

template< typename TMeasurement, typename TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementType &
Histogram< TMeasurement, TFrequencyContainer >
::GetBinMaxFromValue(const unsigned int dimension, const float value) const
{
  // If the value is lower than any of max value in the Histogram,
  // it returns the lowest max value
  if ( value <= this->m_Max[dimension][0] )
    {
    return this->m_Max[dimension][0];
    }

  // If the value is higher than any of max value in the Histogram,
  // it returns the highest max value
  if ( value >= m_Max[dimension][m_Size[dimension] - 1] )
    {
    return m_Max[dimension][this->m_Size[dimension] - 1];
    }

  unsigned int binMaxFromValue = 0;

  for ( unsigned int i = 0; i < this->m_Size[dimension]; i++ )
    {
    if ( ( value >= this->m_Min[dimension][i] )
         && ( value <  this->m_Max[dimension][i] ) )
      {
      binMaxFromValue = i;
      }
    }

  return this->m_Max[dimension][binMaxFromValue];
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetHistogramMinFromIndex(const IndexType & index) const
{
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
    {
    m_TempMeasurementVector[i] = this->GetBinMin(i, index[i]);
    }
  return m_TempMeasurementVector;
}

template< typename TMeasurement, typename TFrequencyContainer >
const typename
Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetHistogramMaxFromIndex(const IndexType & index) const
{
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
    {
    m_TempMeasurementVector[i] = this->GetBinMax(i, index[i]);
    }
  return m_TempMeasurementVector;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetMeasurementVector(const IndexType & index) const
{
  for ( unsigned int i = 0; i < this->GetMeasurementVectorSize(); i++ )
    {
    MeasurementType value = ( m_Min[i][index[i]] + m_Max[i][index[i]] );
    m_TempMeasurementVector[i] =  static_cast< MeasurementType >( value / 2.0 );
    }
  return m_TempMeasurementVector;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::MeasurementVectorType &
Histogram< TMeasurement, TFrequencyContainer >
::GetMeasurementVector(InstanceIdentifier id) const
{
  return this->GetMeasurementVector( this->GetIndex(id) );
}

template< typename TMeasurement, typename TFrequencyContainer >
inline void
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequency(const AbsoluteFrequencyType value)
{
  typename Self::Iterator iter = this->Begin();
  typename Self::Iterator end = this->End();

  while ( iter != end )
    {
    iter.SetFrequency(value);
    ++iter;
    }
}

template< typename TMeasurement, typename TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequencyOfIndex(const IndexType & index, const AbsoluteFrequencyType value)
{
  return this->SetFrequency(this->GetInstanceIdentifier(index), value);
}

template< typename TMeasurement, typename TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::SetFrequencyOfMeasurement(const MeasurementVectorType & measurement, const AbsoluteFrequencyType value)
{
  IndexType index;
  this->GetIndex( measurement, index );
  return this->SetFrequencyOfIndex(index, value);
}

template< typename TMeasurement, typename TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::IncreaseFrequencyOfIndex(const IndexType & index, const AbsoluteFrequencyType value)
{
  const bool result =
    this->IncreaseFrequency(this->GetInstanceIdentifier(index), value);

  return result;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline bool
Histogram< TMeasurement, TFrequencyContainer >
::IncreaseFrequencyOfMeasurement(const MeasurementVectorType & measurement, const AbsoluteFrequencyType value)
{
  IndexType index;

  this->GetIndex(measurement, index);
  return this->IncreaseFrequency(this->GetInstanceIdentifier(index), value);
}

template< typename TMeasurement, typename TFrequencyContainer >
inline typename Histogram< TMeasurement, TFrequencyContainer >::AbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetFrequency(const IndexType & index) const
{
  return ( this->GetFrequency( this->GetInstanceIdentifier(index) ) );
}

template< typename TMeasurement, typename TFrequencyContainer >
typename
Histogram< TMeasurement, TFrequencyContainer >::MeasurementType
Histogram< TMeasurement, TFrequencyContainer >
::GetMeasurement(InstanceIdentifier n, unsigned int dimension) const
{
  return static_cast< MeasurementType >( ( m_Min[dimension][n]
                                           + m_Max[dimension][n] ) / 2 );
}

template< typename TMeasurement, typename TFrequencyContainer >
typename
Histogram< TMeasurement, TFrequencyContainer >::AbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetFrequency(InstanceIdentifier n, unsigned int dimension) const
{
  InstanceIdentifier nextOffset = this->m_OffsetTable[dimension + 1];
  InstanceIdentifier current = this->m_OffsetTable[dimension] * n;
  InstanceIdentifier includeLength = this->m_OffsetTable[dimension];
  InstanceIdentifier include;
  InstanceIdentifier includeEnd;
  InstanceIdentifier last = this->m_OffsetTable[this->GetMeasurementVectorSize()];

  AbsoluteFrequencyType frequency = 0;

  while ( current < last )
    {
    include = current;
    includeEnd = include + includeLength;
    while ( include < includeEnd )
      {
      frequency += GetFrequency(include);
      include++;
      }
    current += nextOffset;
    }
  return frequency;
}

template< typename TMeasurement, typename TFrequencyContainer >
inline typename Histogram< TMeasurement, TFrequencyContainer >::TotalAbsoluteFrequencyType
Histogram< TMeasurement, TFrequencyContainer >
::GetTotalFrequency() const
{
  return m_FrequencyContainer->GetTotalFrequency();
}

template< typename TMeasurement, typename TFrequencyContainer >
double
Histogram< TMeasurement, TFrequencyContainer >
::Quantile(unsigned int dimension, double p) const
{
  InstanceIdentifier n;
  const unsigned int size = this->GetSize(dimension);
  double             p_n_prev;
  double             p_n;
  double             f_n;
  double             cumulated = 0;
  double             totalFrequency = double( this->GetTotalFrequency() );
  double             binProportion;
  double             min, max, interval;

  if ( p < 0.5 )
    {
    n = 0;
    p_n = NumericTraits< double >::Zero;
    do
      {
      f_n = this->GetFrequency(n, dimension);
      cumulated += f_n;
      p_n_prev = p_n;
      p_n = cumulated / totalFrequency;
      n++;
      }
    while ( n < size && p_n < p );

    binProportion = f_n / totalFrequency;

    min = double( this->GetBinMin(dimension, n - 1) );
    max = double( this->GetBinMax(dimension, n - 1) );
    interval = max - min;
    return min + ( ( p - p_n_prev ) / binProportion ) * interval;
    }
  else
    {
    n = size - 1;
    InstanceIdentifier m = NumericTraits< InstanceIdentifier >::Zero;
    p_n      = NumericTraits< double >::One;
    do
      {
      f_n = this->GetFrequency(n, dimension);
      cumulated += f_n;
      p_n_prev = p_n;
      p_n = NumericTraits< double >::One - cumulated / totalFrequency;
      n--;
      m++;
      }
    while ( m< size && p_n > p );

    binProportion = f_n / totalFrequency;
    min = double( this->GetBinMin(dimension, n + 1) );
    max = double( this->GetBinMax(dimension, n + 1) );
    interval = max - min;
    return max - ( ( p_n_prev - p ) / binProportion ) * interval;
    }
}

template< typename TMeasurement, typename TFrequencyContainer >
double
Histogram< TMeasurement, TFrequencyContainer >
::Mean(unsigned int dimension) const
{
  const unsigned int size = this->GetSize(dimension);
  double             totalFrequency = double( this->GetTotalFrequency() );
  double             sum = 0;
  for( unsigned int i = 0; i<size; i++ )
    {
    sum += this->GetFrequency( i, dimension );
    }
  return sum / totalFrequency;
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  // os << indent << "MeasurementVectorSize: " << this->GetMeasurementVectorSize() << std::endl;
  os << indent << "TotalFrequency: " << this->GetTotalFrequency() << std::endl;
  os << indent << "Size: ";
  for ( unsigned int i = 0; i < m_Size.Size(); i++ )
    {
    os << m_Size[i] << "  ";
    }
  os << std::endl;
  os << indent << "Bin Minima: ";
  for ( unsigned int i = 0; i < m_Min.size(); i++ )
    {
    os << m_Min[i][0] << "  ";
    }
  os << std::endl;
  os << indent << "Bin Maxima: ";
  for ( unsigned int i = 0; i < m_Max.size(); i++ )
    {
    os <<  m_Max[i][m_Max[i].size()-1] << "  ";
    }
  os << std::endl;
  os << indent << "ClipBinsAtEnds: "
     << itk::NumericTraits< bool >::PrintType( this->GetClipBinsAtEnds() ) << std::endl;
  os << indent << "OffsetTable: ";
  for ( unsigned int i = 0; i < this->m_OffsetTable.size(); i++ )
    {
    os << this->m_OffsetTable[i] << "  ";
    }
  os << std::endl;
  os << indent << "FrequencyContainerPointer: " << std::endl;
  m_FrequencyContainer->Print( os,  indent.GetNextIndent() );
}

template< typename TMeasurement, typename TFrequencyContainer >
void
Histogram< TMeasurement, TFrequencyContainer >
::Graft(const DataObject *thatObject)
{
  this->Superclass::Graft(thatObject);

  const Self *thatConst = dynamic_cast< const Self * >( thatObject );
  if ( thatConst )
    {
    Self *that = const_cast< Self * >( thatConst );
    this->m_Size                  = that->m_Size;
    this->m_OffsetTable           = that->m_OffsetTable;
    this->m_FrequencyContainer    = that->m_FrequencyContainer;
    this->m_NumberOfInstances     = that->m_NumberOfInstances;
    this->m_Min                   = that->m_Min;
    this->m_Max                   = that->m_Max;
    this->m_TempMeasurementVector = that->m_TempMeasurementVector;
    this->m_TempIndex             = that->m_TempIndex;
    this->m_ClipBinsAtEnds        = that->m_ClipBinsAtEnds;
    }
}

#if !defined(ITK_LEGACY_REMOVE)
template< typename TMeasurement, typename TFrequencyContainer >
inline const typename Histogram< TMeasurement, TFrequencyContainer >::IndexType &
Histogram< TMeasurement, TFrequencyContainer >
::GetIndex(const MeasurementVectorType & measurement) const
{
  // Have this deprecated method call the un-deprecated one..
  this->GetIndex(measurement, m_TempIndex);
  return m_TempIndex;
}
#endif
} // end of namespace Statistics
} // end of namespace itk

#endif