This file is indexed.

/usr/include/vigra/multi_distance.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-2+b5.

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
/************************************************************************/
/*                                                                      */
/*     Copyright 2003-2007 by Kasim Terzic, Christian-Dennis Rahn,      */
/*                            Philipp Schubert and Ullrich Koethe       */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    The VIGRA Website is                                              */
/*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */
/*                                                                      */
/************************************************************************/

#ifndef VIGRA_MULTI_DISTANCE_HXX
#define VIGRA_MULTI_DISTANCE_HXX

#include <vector>
#include <functional>
#include "array_vector.hxx"
#include "multi_array.hxx"
#include "accessor.hxx"
#include "numerictraits.hxx"
#include "navigator.hxx"
#include "metaprogramming.hxx"
#include "multi_pointoperators.hxx"
#include "functorexpression.hxx"

#include "multi_gridgraph.hxx"     //for boundaryGraph & boundaryMultiDistance
#include "union_find.hxx"        //for boundaryGraph & boundaryMultiDistance

namespace vigra
{

namespace detail
{

template <class Value>
struct DistParabolaStackEntry
{
    double left, center, right;
    Value apex_height;
    
    DistParabolaStackEntry(Value const & p, double l, double c, double r)
    : left(l), center(c), right(r), apex_height(p)
    {}
};

/********************************************************/
/*                                                      */
/*                distParabola                          */
/*                                                      */
/*  Version with sigma (parabola spread) for morphology */
/*                                                      */
/********************************************************/

template <class SrcIterator, class SrcAccessor,
          class DestIterator, class DestAccessor >
void distParabola(SrcIterator is, SrcIterator iend, SrcAccessor sa,
                  DestIterator id, DestAccessor da, double sigma )
{
    // We assume that the data in the input is distance squared and treat it as such
    double w = iend - is;
    if(w <= 0)
        return;
        
    double sigma2 = sigma * sigma;
    double sigma22 = 2.0 * sigma2;
    
    typedef typename SrcAccessor::value_type SrcType;
    typedef DistParabolaStackEntry<SrcType> Influence;
    std::vector<Influence> _stack;
    _stack.push_back(Influence(sa(is), 0.0, 0.0, w));
    
    ++is;
    double current = 1.0;
    for(;current < w; ++is, ++current)
    {
        double intersection;
        
        while(true)
        {
            Influence & s = _stack.back();
            double diff = current - s.center;
            intersection = current + (sa(is) - s.apex_height - sigma2*sq(diff)) / (sigma22 * diff);
            
            if( intersection < s.left) // previous point has no influence
            {
                _stack.pop_back();
                if(!_stack.empty())
                    continue;  // try new top of stack without advancing current
                else
                    intersection = 0.0;
            }
            else if(intersection < s.right)
            {
                s.right = intersection;
            }
            break;
        }
        _stack.push_back(Influence(sa(is), intersection, current, w));
    }

    // Now we have the stack indicating which rows are influenced by (and therefore
    // closest to) which row. We can go through the stack and calculate the
    // distance squared for each element of the column.
    typename std::vector<Influence>::iterator it = _stack.begin();
    for(current = 0.0; current < w; ++current, ++id)
    {
        while( current >= it->right) 
            ++it; 
        da.set(sigma2 * sq(current - it->center) + it->apex_height, id);
    }
}

template <class SrcIterator, class SrcAccessor,
          class DestIterator, class DestAccessor>
inline void distParabola(triple<SrcIterator, SrcIterator, SrcAccessor> src,
                         pair<DestIterator, DestAccessor> dest, double sigma)
{
    distParabola(src.first, src.second, src.third,
                 dest.first, dest.second, sigma);
}

/********************************************************/
/*                                                      */
/*        internalSeparableMultiArrayDistTmp            */
/*                                                      */
/********************************************************/

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor, class Array>
void internalSeparableMultiArrayDistTmp(
                      SrcIterator si, SrcShape const & shape, SrcAccessor src,
                      DestIterator di, DestAccessor dest, Array const & sigmas, bool invert)
{
    // Sigma is the spread of the parabolas. It determines the structuring element size
    // for ND morphology. When calculating the distance transforms, sigma is usually set to 1,
    // unless one wants to account for anisotropic pixel pitch
    enum { N =  SrcShape::static_size};

    // we need the Promote type here if we want to invert the image (dilation)
    typedef typename NumericTraits<typename DestAccessor::value_type>::RealPromote TmpType;
    
    // temporary array to hold the current line to enable in-place operation
    ArrayVector<TmpType> tmp( shape[0] );

    typedef MultiArrayNavigator<SrcIterator, N> SNavigator;
    typedef MultiArrayNavigator<DestIterator, N> DNavigator;
    
    
    // only operate on first dimension here
    SNavigator snav( si, shape, 0 );
    DNavigator dnav( di, shape, 0 );

    using namespace vigra::functor;

    for( ; snav.hasMore(); snav++, dnav++ )
    {
            // first copy source to temp for maximum cache efficiency
            // Invert the values if necessary. Only needed for grayscale morphology
            if(invert)
                transformLine( snav.begin(), snav.end(), src, tmp.begin(),
                               typename AccessorTraits<TmpType>::default_accessor(), 
                               Param(NumericTraits<TmpType>::zero())-Arg1());
            else
                copyLine( snav.begin(), snav.end(), src, tmp.begin(),
                          typename AccessorTraits<TmpType>::default_accessor() );

            detail::distParabola( srcIterRange(tmp.begin(), tmp.end(),
                          typename AccessorTraits<TmpType>::default_const_accessor()),
                          destIter( dnav.begin(), dest ), sigmas[0] );
    }
    
    // operate on further dimensions
    for( int d = 1; d < N; ++d )
    {
        DNavigator dnav( di, shape, d );

        tmp.resize( shape[d] );
        

        for( ; dnav.hasMore(); dnav++ )
        {
             // first copy source to temp for maximum cache efficiency
             copyLine( dnav.begin(), dnav.end(), dest,
                       tmp.begin(), typename AccessorTraits<TmpType>::default_accessor() );

             detail::distParabola( srcIterRange(tmp.begin(), tmp.end(),
                           typename AccessorTraits<TmpType>::default_const_accessor()),
                           destIter( dnav.begin(), dest ), sigmas[d] );
        }
    }
    if(invert) transformMultiArray( di, shape, dest, di, dest, -Arg1());
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor, class Array>
inline void internalSeparableMultiArrayDistTmp( SrcIterator si, SrcShape const & shape, SrcAccessor src,
                                                DestIterator di, DestAccessor dest, Array const & sigmas)
{
    internalSeparableMultiArrayDistTmp( si, shape, src, di, dest, sigmas, false );
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor>
inline void internalSeparableMultiArrayDistTmp( SrcIterator si, SrcShape const & shape, SrcAccessor src,
                                                DestIterator di, DestAccessor dest)
{
    ArrayVector<double> sigmas(shape.size(), 1.0);
    internalSeparableMultiArrayDistTmp( si, shape, src, di, dest, sigmas, false );
}

} // namespace detail

/** \addtogroup MultiArrayDistanceTransform Euclidean distance transform for multi-dimensional arrays.

    These functions perform variants of the Euclidean distance transform on 
    arbitrary dimensional arrays. 
*/
//@{

/********************************************************/
/*                                                      */
/*             separableMultiDistSquared                */
/*                                                      */
/********************************************************/

/** \brief Euclidean distance squared on multi-dimensional arrays.

    The algorithm is taken from Donald Bailey: "An Efficient Euclidean Distance Transform",
    Proc. IWCIA'04, Springer LNCS 3322, 2004.

    <b> Declarations:</b>

    pass arbitrary-dimensional array views:
    \code
    namespace vigra {
        // explicitly specify pixel pitch for each coordinate
        template <unsigned int N, class T1, class S1,
                                  class T2, class S2, 
                  class Array>
        void
        separableMultiDistSquared(MultiArrayView<N, T1, S1> const & source,
                                  MultiArrayView<N, T2, S2> dest,
                                  bool background,
                                  Array const & pixelPitch);

        // use default pixel pitch = 1.0 for each coordinate
        template <unsigned int N, class T1, class S1,
                                  class T2, class S2>
        void
        separableMultiDistSquared(MultiArrayView<N, T1, S1> const & source,
                                  MultiArrayView<N, T2, S2> dest, 
                                  bool background);
    }
    \endcode

    \deprecatedAPI{separableMultiDistSquared}
    pass \ref MultiIteratorPage "MultiIterators" and \ref DataAccessors :
    \code
    namespace vigra {
        // explicitly specify pixel pitch for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor, class Array>
        void 
        separableMultiDistSquared( SrcIterator s, SrcShape const & shape, SrcAccessor src,
                                   DestIterator d, DestAccessor dest, 
                                   bool background,
                                   Array const & pixelPitch);
                                        
        // use default pixel pitch = 1.0 for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor>
        void
        separableMultiDistSquared(SrcIterator siter, SrcShape const & shape, SrcAccessor src,
                                  DestIterator diter, DestAccessor dest, 
                                  bool background);

    }
    \endcode
    use argument objects in conjunction with \ref ArgumentObjectFactories :
    \code
    namespace vigra {
        // explicitly specify pixel pitch for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor, class Array>
        void 
        separableMultiDistSquared( triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                   pair<DestIterator, DestAccessor> const & dest, 
                                   bool background,
                                   Array const & pixelPitch);
                                               
        // use default pixel pitch = 1.0 for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor>
        void
        separableMultiDistSquared(triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                  pair<DestIterator, DestAccessor> const & dest,
                                  bool background);

    }
    \endcode
    \deprecatedEnd

    This function performs a squared Euclidean squared distance transform on the given
    multi-dimensional array. Both source and destination
    arrays are represented by iterators, shape objects and accessors.
    The destination array is required to already have the correct size.

    This function expects a mask as its source, where background pixels are 
    marked as zero, and non-background pixels as non-zero. If the parameter 
    <i>background</i> is true, then the squared distance of all background
    pixels to the nearest object is calculated. Otherwise, the distance of all
    object pixels to the nearest background pixel is calculated.
    
    Optionally, one can pass an array that specifies the pixel pitch in each direction. 
    This is necessary when the data have non-uniform resolution (as is common in confocal
    microscopy, for example). 

    This function may work in-place, which means that <tt>siter == diter</tt> is allowed.
    A full-sized internal array is only allocated if working on the destination
    array directly would cause overflow errors (i.e. if
    <tt> NumericTraits<typename DestAccessor::value_type>::max() < N * M*M</tt>, where M is the
    size of the largest dimension of the array.

    <b> Usage:</b>

    <b>\#include</b> \<vigra/multi_distance.hxx\><br/>
    Namespace: vigra

    \code
    Shape3 shape(width, height, depth);
    MultiArray<3, unsigned char> source(shape);
    MultiArray<3, unsigned int> dest(shape);
    ...

    // Calculate Euclidean distance squared for all background pixels 
    separableMultiDistSquared(source, dest, true);
    \endcode

    \see vigra::distanceTransform(), vigra::separableMultiDistance()
*/
doxygen_overloaded_function(template <...> void separableMultiDistSquared)

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor, class Array>
void separableMultiDistSquared( SrcIterator s, SrcShape const & shape, SrcAccessor src,
                                DestIterator d, DestAccessor dest, bool background,
                                Array const & pixelPitch)
{
    int N = shape.size();

    typedef typename SrcAccessor::value_type SrcType;
    typedef typename DestAccessor::value_type DestType;
    typedef typename NumericTraits<DestType>::RealPromote Real;

    SrcType zero = NumericTraits<SrcType>::zero();

    double dmax = 0.0;
    bool pixelPitchIsReal = false;
    for( int k=0; k<N; ++k)
    {
        if(int(pixelPitch[k]) != pixelPitch[k])
            pixelPitchIsReal = true;
        dmax += sq(pixelPitch[k]*shape[k]);
    }
            
    using namespace vigra::functor;
   
    if(dmax > NumericTraits<DestType>::toRealPromote(NumericTraits<DestType>::max()) 
       || pixelPitchIsReal) // need a temporary array to avoid overflows
    {
        // Threshold the values so all objects have infinity value in the beginning
        Real maxDist = (Real)dmax, rzero = (Real)0.0;
        MultiArray<SrcShape::static_size, Real> tmpArray(shape);
        if(background == true)
            transformMultiArray( s, shape, src, 
                                 tmpArray.traverser_begin(), typename AccessorTraits<Real>::default_accessor(),
                                 ifThenElse( Arg1() == Param(zero), Param(maxDist), Param(rzero) ));
        else
            transformMultiArray( s, shape, src, 
                                 tmpArray.traverser_begin(), typename AccessorTraits<Real>::default_accessor(),
                                 ifThenElse( Arg1() != Param(zero), Param(maxDist), Param(rzero) ));
        
        detail::internalSeparableMultiArrayDistTmp( tmpArray.traverser_begin(), 
                shape, typename AccessorTraits<Real>::default_accessor(),
                tmpArray.traverser_begin(), 
                typename AccessorTraits<Real>::default_accessor(), pixelPitch);
        
        copyMultiArray(srcMultiArrayRange(tmpArray), destIter(d, dest));
    }
    else        // work directly on the destination array    
    {
        // Threshold the values so all objects have infinity value in the beginning
        DestType maxDist = DestType(std::ceil(dmax)), rzero = (DestType)0;
        if(background == true)
            transformMultiArray( s, shape, src, d, dest,
                                 ifThenElse( Arg1() == Param(zero), Param(maxDist), Param(rzero) ));
        else
            transformMultiArray( s, shape, src, d, dest, 
                                 ifThenElse( Arg1() != Param(zero), Param(maxDist), Param(rzero) ));
     
        detail::internalSeparableMultiArrayDistTmp( d, shape, dest, d, dest, pixelPitch);
    }
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor>
inline 
void separableMultiDistSquared( SrcIterator s, SrcShape const & shape, SrcAccessor src,
                                DestIterator d, DestAccessor dest, bool background)
{
    ArrayVector<double> pixelPitch(shape.size(), 1.0);
    separableMultiDistSquared( s, shape, src, d, dest, background, pixelPitch );
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor, class Array>
inline void separableMultiDistSquared( triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                       pair<DestIterator, DestAccessor> const & dest, bool background,
                                       Array const & pixelPitch)
{
    separableMultiDistSquared( source.first, source.second, source.third,
                               dest.first, dest.second, background, pixelPitch );
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor>
inline void separableMultiDistSquared( triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                       pair<DestIterator, DestAccessor> const & dest, bool background)
{
    separableMultiDistSquared( source.first, source.second, source.third,
                               dest.first, dest.second, background );
}

template <unsigned int N, class T1, class S1,
                          class T2, class S2, 
          class Array>
inline void
separableMultiDistSquared(MultiArrayView<N, T1, S1> const & source,
                          MultiArrayView<N, T2, S2> dest, bool background,
                          Array const & pixelPitch)
{
    vigra_precondition(source.shape() == dest.shape(),
        "separableMultiDistSquared(): shape mismatch between input and output.");
    separableMultiDistSquared( srcMultiArrayRange(source),
                               destMultiArray(dest), background, pixelPitch );
}

template <unsigned int N, class T1, class S1,
                          class T2, class S2>
inline void
separableMultiDistSquared(MultiArrayView<N, T1, S1> const & source,
                          MultiArrayView<N, T2, S2> dest, bool background)
{
    vigra_precondition(source.shape() == dest.shape(),
        "separableMultiDistSquared(): shape mismatch between input and output.");
    separableMultiDistSquared( srcMultiArrayRange(source),
                               destMultiArray(dest), background );
}

/********************************************************/
/*                                                      */
/*             separableMultiDistance                   */
/*                                                      */
/********************************************************/

/** \brief Euclidean distance on multi-dimensional arrays.

    <b> Declarations:</b>

    pass arbitrary-dimensional array views:
    \code
    namespace vigra {
        // explicitly specify pixel pitch for each coordinate
        template <unsigned int N, class T1, class S1,
                  class T2, class S2, class Array>
        void 
        separableMultiDistance(MultiArrayView<N, T1, S1> const & source,
                               MultiArrayView<N, T2, S2> dest, 
                               bool background,
                               Array const & pixelPitch);

        // use default pixel pitch = 1.0 for each coordinate
        template <unsigned int N, class T1, class S1,
                  class T2, class S2>
        void 
        separableMultiDistance(MultiArrayView<N, T1, S1> const & source,
                               MultiArrayView<N, T2, S2> dest, 
                               bool background);
    }
    \endcode

    \deprecatedAPI{separableMultiDistance}
    pass \ref MultiIteratorPage "MultiIterators" and \ref DataAccessors :
    \code
    namespace vigra {
        // explicitly specify pixel pitch for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor, class Array>
        void 
        separableMultiDistance( SrcIterator s, SrcShape const & shape, SrcAccessor src,
                                DestIterator d, DestAccessor dest, 
                                bool background,
                                Array const & pixelPitch);
                                        
        // use default pixel pitch = 1.0 for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor>
        void
        separableMultiDistance(SrcIterator siter, SrcShape const & shape, SrcAccessor src,
                               DestIterator diter, DestAccessor dest, 
                               bool background);

    }
    \endcode
    use argument objects in conjunction with \ref ArgumentObjectFactories :
    \code
    namespace vigra {
        // explicitly specify pixel pitch for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor, class Array>
        void 
        separableMultiDistance( triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                pair<DestIterator, DestAccessor> const & dest, 
                                bool background,
                                Array const & pixelPitch);
                                               
        // use default pixel pitch = 1.0 for each coordinate
        template <class SrcIterator, class SrcShape, class SrcAccessor,
                  class DestIterator, class DestAccessor>
        void
        separableMultiDistance(triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                               pair<DestIterator, DestAccessor> const & dest,
                               bool background);

    }
    \endcode
    \deprecatedEnd

    This function performs a Euclidean distance transform on the given
    multi-dimensional array. It simply calls \ref separableMultiDistSquared()
    and takes the pixel-wise square root of the result. See \ref separableMultiDistSquared()
    for more documentation.
    
    <b> Usage:</b>

    <b>\#include</b> \<vigra/multi_distance.hxx\><br/>
    Namespace: vigra

    \code
    Shape3 shape(width, height, depth);
    MultiArray<3, unsigned char> source(shape);
    MultiArray<3, float> dest(shape);
    ...

    // Calculate Euclidean distance for all background pixels 
    separableMultiDistance(source, dest, true);
    \endcode

    \see vigra::distanceTransform(), vigra::separableMultiDistSquared()
*/
doxygen_overloaded_function(template <...> void separableMultiDistance)

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor, class Array>
void separableMultiDistance( SrcIterator s, SrcShape const & shape, SrcAccessor src,
                             DestIterator d, DestAccessor dest, bool background,
                             Array const & pixelPitch)
{
    separableMultiDistSquared( s, shape, src, d, dest, background, pixelPitch);
    
    // Finally, calculate the square root of the distances
    using namespace vigra::functor;
   
    transformMultiArray( d, shape, dest, d, dest, sqrt(Arg1()) );
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor>
void separableMultiDistance( SrcIterator s, SrcShape const & shape, SrcAccessor src,
                             DestIterator d, DestAccessor dest, bool background)
{
    separableMultiDistSquared( s, shape, src, d, dest, background);
    
    // Finally, calculate the square root of the distances
    using namespace vigra::functor;
   
    transformMultiArray( d, shape, dest, d, dest, sqrt(Arg1()) );
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor, class Array>
inline void separableMultiDistance( triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                    pair<DestIterator, DestAccessor> const & dest, bool background,
                                    Array const & pixelPitch)
{
    separableMultiDistance( source.first, source.second, source.third,
                            dest.first, dest.second, background, pixelPitch );
}

template <class SrcIterator, class SrcShape, class SrcAccessor,
          class DestIterator, class DestAccessor>
inline void separableMultiDistance( triple<SrcIterator, SrcShape, SrcAccessor> const & source,
                                    pair<DestIterator, DestAccessor> const & dest, bool background)
{
    separableMultiDistance( source.first, source.second, source.third,
                            dest.first, dest.second, background );
}

template <unsigned int N, class T1, class S1,
          class T2, class S2, class Array>
inline void 
separableMultiDistance(MultiArrayView<N, T1, S1> const & source,
                       MultiArrayView<N, T2, S2> dest, 
                       bool background,
                       Array const & pixelPitch)
{
    vigra_precondition(source.shape() == dest.shape(),
        "separableMultiDistance(): shape mismatch between input and output.");
    separableMultiDistance( srcMultiArrayRange(source),
                            destMultiArray(dest), background, pixelPitch );
}

template <unsigned int N, class T1, class S1,
          class T2, class S2>
inline void 
separableMultiDistance(MultiArrayView<N, T1, S1> const & source,
                       MultiArrayView<N, T2, S2> dest, 
                       bool background)
{
    vigra_precondition(source.shape() == dest.shape(),
        "separableMultiDistance(): shape mismatch between input and output.");
    separableMultiDistance( srcMultiArrayRange(source),
                            destMultiArray(dest), background );
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% BoundaryDistanceTransform %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//rewrite labeled data and work with separableMultiDist
namespace lemon_graph { 

template <class Graph, class T1Map, class T2Map>
void 
markRegionBoundaries(Graph const & g,
                     T1Map const & labels,
                     T2Map & out)
{
    typedef typename Graph::NodeIt        graph_scanner;
    typedef typename Graph::OutBackArcIt  neighbor_iterator;

    //find faces
    for (graph_scanner node(g); node != INVALID; ++node) 
    {
        typename T1Map::value_type center = labels[*node];
        
        for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
        {
            // set adjacent nodes with different labels to 1
            if(center != labels[g.target(*arc)])
            {
                out[*node] = 1;
                out[g.target(*arc)] = 1;
            }
        }
    }
}

} //-- namspace lemon_graph

doxygen_overloaded_function(template <...> unsigned int markRegionBoundaries)

template <unsigned int N, class T1, class S1,
                          class T2, class S2>
inline void
markRegionBoundaries(MultiArrayView<N, T1, S1> const & labels,
                     MultiArrayView<N, T2, S2> out,
                     NeighborhoodType neighborhood=DirectNeighborhood)
{
    vigra_precondition(labels.shape() == out.shape(),
        "markRegionBoundaries(): shape mismatch between input and output.");

    GridGraph<N, undirected_tag> graph(labels.shape(), neighborhood);

    lemon_graph::markRegionBoundaries(graph, labels, out);
}

//MultiDistance which works directly on labeled data

namespace detail
{

/********************************************************/
/*                                                      */
/*                boundaryDistParabola                  */
/*                                                      */
/********************************************************/

template <class DestIterator, class LabelIterator>
void 
boundaryDistParabola(DestIterator is, DestIterator iend, 
                     LabelIterator ilabels, 
                     double dmax,
                     bool array_border_is_active=false)
{
    // We assume that the data in the input is distance squared and treat it as such
    double w = iend - is;
    if(w <= 0)
        return;

    DestIterator id = is;
    typedef typename LabelIterator::value_type LabelType;
    typedef typename DestIterator::value_type DestType;
    typedef detail::DistParabolaStackEntry<DestType> Influence;
    typedef std::vector<Influence> Stack;

    double apex_height = array_border_is_active
                             ? 0.0
                             : dmax;
    Stack _stack(1, Influence(apex_height, 0.0, -1.0, w));
    LabelType current_label = *ilabels;
    for(double begin = 0.0, current = 0.0; current <= w; ++ilabels, ++is, ++current)
    {
        apex_height = (current < w)
                          ? (current_label == *ilabels)
                               ? *is
                               : 0.0
                          : array_border_is_active
                                ? 0.0
                                : dmax;
        while(true)
        {
            Influence & s = _stack.back();
            double diff = current - s.center;
            double intersection = current + (apex_height - s.apex_height - sq(diff)) / (2.0 * diff);
            
            if(intersection < s.left) // previous parabola has no influence
            {
                _stack.pop_back();
                if(_stack.empty())
                    intersection = begin; // new parabola is valid for entire present segment
                else
                    continue;  // try new top of stack without advancing to next pixel
            }
            else if(intersection < s.right)
            {
                s.right = intersection;
            }
            if(intersection < w)
                _stack.push_back(Influence(apex_height, intersection, current, w));
            if(current < w && current_label == *ilabels)
                break; // finished present pixel, advance to next one
                
            // label changed => finalize the current segment
            typename Stack::iterator it = _stack.begin();
            for(double c = begin; c < current; ++c, ++id)
            {
                while(c >= it->right) 
                    ++it; 
                *id = sq(c - it->center) + it->apex_height;
            }
            if(current == w)
                break;  // stop when this was the last segment
                
            // initialize the new segment
            begin = current;
            current_label = *ilabels;
            apex_height = *is;
            Stack(1, Influence(0.0, begin-1.0, begin-1.0, w)).swap(_stack);
            // don't advance to next pixel here, because the present pixel must also 
            // be analysed in the context of the new segment
        }
    }
}

/********************************************************/
/*                                                      */
/*           internalBoundaryMultiArrayDist             */
/*                                                      */
/********************************************************/

template <unsigned int N, class T1, class S1,
                          class T2, class S2>
void
internalBoundaryMultiArrayDist(
                      MultiArrayView<N, T1, S1> const & labels,
                      MultiArrayView<N, T2, S2> dest,
                      double dmax, bool array_border_is_active=false)
{
    typedef typename MultiArrayView<N, T1, S1>::const_traverser LabelIterator;
    typedef typename MultiArrayView<N, T2, S2>::traverser DestIterator;
    typedef MultiArrayNavigator<LabelIterator, N> LabelNavigator;
    typedef MultiArrayNavigator<DestIterator, N> DNavigator;
    
    dest = dmax;
    for( int d = 0; d < N; ++d )
    {
        LabelNavigator lnav( labels.traverser_begin(), labels.shape(), d );
        DNavigator dnav( dest.traverser_begin(), dest.shape(), d );

        for( ; dnav.hasMore(); dnav++, lnav++ )
        {
            boundaryDistParabola(dnav.begin(), dnav.end(),
                                 lnav.begin(), 
                                 dmax, array_border_is_active);
        }
    }
}

} // namespace detail

    /** \brief Specify which boundary is used for boundaryMultiDistance().  
    
    */
enum BoundaryDistanceTag {
    OuterBoundary,      ///< Pixels just outside of each region
    InterpixelBoundary, ///< Half-integer points between pixels of different labels
    InnerBoundary       ///< Pixels just inside of each region
};

/********************************************************/
/*                                                      */
/*             boundaryMultiDistance                    */
/*                                                      */
/********************************************************/

/** \brief Euclidean distance to the implicit boundaries of a multi-dimensional label array.


    <b> Declarations:</b>

    pass arbitrary-dimensional array views:
    \code
    namespace vigra {
        template <unsigned int N, class T1, class S1,
                  class T2, class S2>
        void
        boundaryMultiDistance(MultiArrayView<N, T1, S1> const & labels,
                              MultiArrayView<N, T2, S2> dest,
                              bool array_border_is_active=false,
                              BoundaryDistanceTag boundary=InterpixelBoundary);
    }
    \endcode
    
    This function computes the distance transform of a labeled image <i>simultaneously</i>
    for all regions. Depending on the requested type of \a boundary, three modes
    are supported:
    <ul>
    <li><tt>OuterBoundary</tt>: In each region, compute the distance to the nearest pixel not
               belonging to that regions. This is the same as if a normal distance transform
               where applied to a binary image containing just this region.</li>
    <li><tt>InterpixelBoundary</tt> (default): Like <tt>OuterBoundary</tt>, but shift the distance
               to the interpixel boundary by subtractiong 1/2. This make the distences consistent
               accross boundaries.</li>
    <li><tt>InnerBoundary</tt>: In each region, compute the distance to the nearest pixel in the 
               region which is adjacent to the boundary. </li>
    </ul>
    If <tt>array_border_is_active=true</tt>, the 
    outer border of the array (i.e. the interpixel boundary between the array 
    and the infinite region) is also used. Otherwise (the default), regions 
    touching the array border are treated as if they extended to infinity.
    
    <b> Usage:</b>

    <b>\#include</b> \<vigra/multi_distance.hxx\><br/>
    Namespace: vigra

    \code
    Shape3 shape(width, height, depth);
    MultiArray<3, unsigned char> source(shape);
    MultiArray<3, UInt32> labels(shape);
    MultiArray<3, float> dest(shape);
    ...

    // find regions (interpixel boundaries are implied)
    labelMultiArray(source, labels);
    
    // Calculate Euclidean distance to interpixel boundary for all pixels 
    boundaryMultiDistance(labels, dest);
    \endcode

    \see vigra::distanceTransform(), vigra::separableMultiDistance()
*/
doxygen_overloaded_function(template <...> void boundaryMultiDistance)

template <unsigned int N, class T1, class S1,
                          class T2, class S2>
void
boundaryMultiDistance(MultiArrayView<N, T1, S1> const & labels,
                      MultiArrayView<N, T2, S2> dest,
                      bool array_border_is_active=false,
                      BoundaryDistanceTag boundary=InterpixelBoundary)
{
    vigra_precondition(labels.shape() == dest.shape(),
        "boundaryMultiDistance(): shape mismatch between input and output.");
        
    using namespace vigra::functor;
    
    if(boundary == InnerBoundary)
    {
        MultiArray<N, unsigned char> boundaries(labels.shape());
        
        markRegionBoundaries(labels, boundaries, IndirectNeighborhood);
        if(array_border_is_active)
            initMultiArrayBorder(boundaries, 1, 1);
        separableMultiDistance(boundaries, dest, true);
    }
    else
    {
        T2 offset = 0.0;
        
        if(boundary == InterpixelBoundary)
        {
            vigra_precondition(!NumericTraits<T2>::isIntegral::value,
                "boundaryMultiDistance(..., InterpixelBoundary): output pixel type must be float or double.");
            offset = T2(0.5);
        }
        double dmax = squaredNorm(labels.shape()) + N;
        if(dmax > double(NumericTraits<T2>::max()))
        {
            // need a temporary array to avoid overflows
            typedef typename NumericTraits<T2>::RealPromote Real;
            MultiArray<N, Real> tmpArray(labels.shape());
            detail::internalBoundaryMultiArrayDist(labels, tmpArray,
                                                      dmax, array_border_is_active);
            transformMultiArray(tmpArray, dest, sqrt(Arg1()) - Param(offset) );
        }
        else
        {
            // can work directly on the destination array
            detail::internalBoundaryMultiArrayDist(labels, dest, dmax, array_border_is_active);
            transformMultiArray(dest, dest, sqrt(Arg1()) - Param(offset) );
        }
    }
}

//@}

} //-- namespace vigra


#endif        //-- VIGRA_MULTI_DISTANCE_HXX