File: itkFlatStructuringElement.txx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (1030 lines) | stat: -rw-r--r-- 27,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkFlatStructuringElement.txx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#ifndef __itkFlatStructuringElement_txx
#define __itkFlatStructuringElement_txx
#include "vnl/vnl_math.h"
#include "itkFlatStructuringElement.h"
#include <math.h>
#include <vector>

#ifndef M_PI
#define M_PI vnl_math::pi
#endif

#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkFloodFilledSpatialFunctionConditionalIterator.h"
#include "itkEllipsoidInteriorExteriorSpatialFunction.h" 

#include "itkVanHerkGilWermanDilateImageFilter.h"


namespace itk
{
template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::Poly(RadiusType radius, unsigned lines)
{
  Self res = Self();
  res = res.PolySub(Dispatch<VDimension>(), radius, lines);
  res.SetRadius( radius );
#if 0
  float theta, phi, step;
  theta = phi = 0;
  step = M_PI/(lines - 1);

  while (theta < M_PI) 
    {

    std::cout << "theta= " << theta << " phi = " << phi << std::endl;
    LType O = res.mkOffset(phi, theta);
    std::cout << O << std::endl;
    if (res.checkParallel(O, res.m_Lines))
      {
      std::cout << "Already have this line" << std::endl;
      }
    else
      {
      res.m_Lines.push_back(O);
      }
    theta += step;
    phi += step;
    }
  std::cout << "---------------" << std::endl;
#endif
  res.ComputeBufferFromLines();
  return(res);

}

template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::PolySub(const Dispatch<2> &, RadiusType radius, unsigned lines) const
{
  // radial decomposition method from "Radial Decomposition of Discs
  // and Spheres" - CVGIP: Graphical Models and Image Processing
  //std::cout << "2 dimensions" << std::endl;
  Self res = Self();
  res.m_Decomposable = true;
  
  unsigned int rr = 0;
  for (unsigned i=0;i<VDimension;i++)
    {
    if (radius[i] > rr) rr = radius[i];
    }
  if (lines == 0)
    {
    // select some default line values
    if (rr <= 3) lines=2;
    else if (rr <= 8) lines=4;
    else lines=6;
    }
  // start with a circle - figure out the length of the structuring
  // element we need -- This method results in a polygon with 2*lines
  // sides, each side with length k, where k is the structuring
  // element length. Therefore the value of k we need to produce the
  // radius we want is: (M_PI * rr * 2)/(2*lines)
  float k1 = (M_PI * (float)radius[0])/((float)lines);
  float k2 = (M_PI * (float)radius[1])/((float)lines);
  //std::cout << "k= " << k << std::endl;
  float theta, step;
  step = M_PI/lines;
  theta = 0;
  // just to ensure that we get the last one
  while (theta <= M_PI/2.0 + 0.0001)
    {
    LType O;
    O[0] = k1 * vcl_cos(theta);
    O[1] = k2 * vcl_sin(theta);
    if (!res.checkParallel(O, res.m_Lines))
      {
      //std::cout << O << std::endl;
      res.m_Lines.push_back(O);
      }
     O[0] = k1 * vcl_cos(-theta);
     O[1] = k2 * vcl_sin(-theta);
     if (!res.checkParallel(O, res.m_Lines))
       {
       //std::cout << O << std::endl;
       res.m_Lines.push_back(O);
       }
     theta += step;
     //std::cout << "theta1 = " << theta << " " << M_PI/2.0 << std::endl;
    }
    
  return(res);
}

//    O[0] = k1 * vcl_cos(phi) * vcl_cos(theta);
//    O[1] = k2 * vcl_cos(phi) * vcl_sin(theta);
//    O[2] = k3 * vcl_sin(theta);

template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::PolySub(const Dispatch<3> &, RadiusType radius, unsigned lines) const
{
  Self res = Self();
  res.m_Decomposable = true;
  // std::cout << "3 dimensions" << std::endl;
  unsigned int rr = 0;
  int iterations = 1;
  int faces = lines * 2;
  for (unsigned i=0;i<VDimension;i++)
    {
    if (radius[i] > rr) rr = radius[i];
    }
  switch (faces)
    {
    case 12:
      {
      // dodecahedron
      float phi = (1.0 + vcl_sqrt(5.0)) / 2.0;
      float b = 1.0/phi;
      float c = 2.0 - phi;
      unsigned facets = 12;
      typedef std::vector<FacetType> FacetArrayType;
      FacetArrayType FacetArray;
      FacetArray.resize( facets );
      // set up vectors normal to the faces - only put in 3 points for
      // each face:
      // face 1
      LType PP(0.0);
      FacetType Fc;
      b /= 2.0;
      c /= 2.0;
  
      PP[0]=c;PP[1]=0;PP[2]=0.5;
      Fc.P1 = PP;
      PP[0]=-c;PP[1]=0;PP[2]=0.5;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=b;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[0] = Fc;
  
      PP[0]=-c;PP[1]=0;PP[2]=0.5;
      Fc.P1 = PP;
      PP[0]=c;PP[1]=0;PP[2]=0.5;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=-b;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[1] = Fc;
      
      PP[0]=c;PP[1]=0;PP[2]=-0.5;
      Fc.P1 = PP;
      PP[0]=-c;PP[1]=0;PP[2]=-0.5;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=-b;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[2] = Fc;
  
      PP[0]=-c;PP[1]=0;PP[2]=-0.5;
      Fc.P1 = PP;
      PP[0]=c;PP[1]=0;PP[2]=-0.5;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=b;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[3] = Fc;
  
      PP[0]=0;PP[1]=0.5;PP[2]=-c;
      Fc.P1 = PP;
      PP[0]=0;PP[1]=0.5;PP[2]=c;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=b;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[4] = Fc;
  
      PP[0]=0;PP[1]=0.5;PP[2]=c;
      Fc.P1 = PP;
      PP[0]=0;PP[1]=0.5;PP[2]=-c;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=b;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[5] = Fc;
  
      PP[0]=0;PP[1]=-0.5;PP[2]=-c;
      Fc.P1 = PP;
      PP[0]=0;PP[1]=-0.5;PP[2]=c;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=-b;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[6] = Fc;
  
      PP[0]=0;PP[1]=-0.5;PP[2]=c;
      Fc.P1 = PP;
      PP[0]=0;PP[1]=-0.5;PP[2]=-c;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=-b;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[7] = Fc;
  
      PP[0]=0.5;PP[1]=c;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=0.5;PP[1]=-c;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=-b;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[8] = Fc;
  
      PP[0]=0.5;PP[1]=-c;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=0.5;PP[1]=c;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=b;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[9] = Fc;
  
      PP[0]=-0.5;PP[1]=c;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=-0.5;PP[1]=-c;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=-b;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[10] = Fc;
  
      PP[0]=-0.5;PP[1]=-c;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=-0.5;PP[1]=c;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=b;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[11] = Fc;
      for (unsigned j = 0; j < facets; j++)
        {
        // Find a line perpendicular to each face
        LType L, A, B;
        A = FacetArray[j].P2 - FacetArray[j].P1;
        B = FacetArray[j].P3 - FacetArray[j].P1;
        L[0] = A[1]*B[2] - B[1]*A[2];
        L[1] = B[0]*A[2] - A[0]*B[2];
        L[2] = A[0]*B[1] - B[0]*A[1];
        
        L.Normalize();
        // Scale to required length
        L *= rr;
        if (!res.checkParallel(L, res.m_Lines))
          {
          res.m_Lines.push_back(L);
          }
        }
      return(res);
  
      }
    break;
    case 14:
      {
      // cube with the corners cut off
      LType A;
      // The axes
      A[0]=1;A[1]=0;A[2]=0;
      A *= rr;
      res.m_Lines.push_back(A);
      A[0]=0;A[1]=1;A[2]=0;
      A *= rr;
      res.m_Lines.push_back(A);
      A[0]=0;A[1]=0;A[2]=1;
      A *= rr;
      res.m_Lines.push_back(A);
      // Diagonals
      A[0]=1;A[1]=1;A[2]=1;
      A.Normalize();
      A *= rr;
      res.m_Lines.push_back(A);
  
      A[0]=-1;A[1]=1;A[2]=1;
      A.Normalize();
      A *= rr;
      res.m_Lines.push_back(A);
  
      A[0]=1;A[1]=-1;A[2]=1;
      A.Normalize();
      A *= rr;
      res.m_Lines.push_back(A);
  
      A[0]=-1;A[1]=-1;A[2]=1;
      A.Normalize();
      A *= rr;
      res.m_Lines.push_back(A);
      return(res);
      }
    break;
    case 20:
      {
      // Icosahedron
      float phi = (1.0 + vcl_sqrt(5.0)) / 2.0;
      float a = 0.5;
      float b = 1.0/(2.0*phi);
      unsigned facets = 20;
      typedef std::vector<FacetType> FacetArrayType;
      FacetArrayType FacetArray;
      FacetArray.resize( facets );
      // set up vectors normal to the faces - only put in 3 points for
      // each face:
      // face 1
      LType PP(0.0);
      FacetType Fc;
  
      PP[0]=0;PP[1]=b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=b;PP[1]=a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[0] = Fc;
      
      PP[0]=0;PP[1]=b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=-b;PP[1]=a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[1] = Fc;
      
      PP[0]=0;PP[1]=b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=0;PP[1]=-b;PP[2]=a;
      Fc.P2 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[2] = Fc;
      
      PP[0]=0;PP[1]=b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=a;PP[1]=0;PP[2]=b;
      Fc.P2 = PP;
      PP[0]=0;PP[1]=-b;PP[2]=a;
      Fc.P3 = PP;
      FacetArray[3] = Fc;
      
      PP[0]=0;PP[1]=b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=0;PP[1]=-b;PP[2]=-a;
      Fc.P2 = PP;
      PP[0]=a;PP[1]=0;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[4] = Fc;
      
      PP[0]=0;PP[1]=b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=-b;
      Fc.P2 = PP;
      PP[0]=0;PP[1]=-b;PP[2]=-a;
      Fc.P3 = PP;
      FacetArray[5] = Fc;
      
      PP[0]=0;PP[1]=-b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=b;PP[1]=-a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=-a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[6] = Fc;
      
      PP[0]=0;PP[1]=-b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=-b;PP[1]=-a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=-a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[7] = Fc;
      
      PP[0]=-b;PP[1]=a;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=b;
      Fc.P2 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[8] = Fc;
      
      PP[0]=-b;PP[1]=-a;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=-b;
      Fc.P2 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[9] = Fc;
       
      PP[0]=b;PP[1]=a;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=a;PP[1]=0;PP[2]=-b;
      Fc.P2 = PP;
      PP[0]=a;PP[1]=0;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[10] = Fc;
       
      PP[0]=b;PP[1]=-a;PP[2]=0;
      Fc.P1 = PP;
      PP[0]=a;PP[1]=0;PP[2]=b;
      Fc.P2 = PP;
      PP[0]=a;PP[1]=0;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[11] = Fc;
       
      PP[0]=0;PP[1]=b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=b;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[12] = Fc;
       
      PP[0]=0;PP[1]=b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=b;PP[1]=a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=a;PP[1]=0;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[13] = Fc;
       
      PP[0]=0;PP[1]=b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=-b;PP[1]=a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[14] = Fc;
       
      PP[0]=0;PP[1]=b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=a;PP[1]=0;PP[2]=-b;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[15] = Fc;
       
      PP[0]=0;PP[1]=-b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=-b;
      Fc.P2 = PP;
      PP[0]=-b;PP[1]=-a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[16] = Fc;
        
      PP[0]=0;PP[1]=-b;PP[2]=-a;
      Fc.P1 = PP;
      PP[0]=b;PP[1]=-a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=a;PP[1]=0;PP[2]=-b;
      Fc.P3 = PP;
      FacetArray[17] = Fc;
         
      PP[0]=0;PP[1]=-b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=-b;PP[1]=-a;PP[2]=0;
      Fc.P2 = PP;
      PP[0]=-a;PP[1]=0;PP[2]=b;
      Fc.P3 = PP;
      FacetArray[18] = Fc;
         
      PP[0]=0;PP[1]=-b;PP[2]=a;
      Fc.P1 = PP;
      PP[0]=a;PP[1]=0;PP[2]=b;
      Fc.P2 = PP;
      PP[0]=b;PP[1]=-a;PP[2]=0;
      Fc.P3 = PP;
      FacetArray[19] = Fc;
   
      for (unsigned j = 0; j < facets; j++)
        {
        // Find a line perpendicular to each face
        LType L, A, B;
        A = FacetArray[j].P2 - FacetArray[j].P1;
        B = FacetArray[j].P3 - FacetArray[j].P1;
        L[0] = A[1]*B[2] - B[1]*A[2];
        L[1] = B[0]*A[2] - A[0]*B[2];
        L[2] = A[0]*B[1] - B[0]*A[1];
        
        L.Normalize();
        // Scale to required length
        L *= rr;
        if (!res.checkParallel(L, res.m_Lines))
          {
          res.m_Lines.push_back(L);
          }
        }
      return(res);
      }
    break;
    case 32:
      {
      iterations = 1;
      // 2 iterations leads to 128 faces, which is too many
      // subdivision of octahedron
      // create triangular facet approximation to a sphere - begin with
      // unit sphere
      // total number of facets is 8 * (4^iterations)
      unsigned int facets = 8 * (int)vcl_pow((double)4, iterations);
      float sqrt2 = vcl_sqrt(2.0);
      // std::cout << facets << " facets" << std::endl;
      typedef std::vector<FacetType> FacetArrayType;
      FacetArrayType FacetArray;
      FacetArray.resize( facets );
      
      // original corners of octahedron
      LType P0(0.0), P1(0.0), P2(0.0), P3(0.0), P4(0.0), P5(0.0); 
      P0[0]=0;         P0[1]=0;       P0[2]=1;
      P1[0]=0;         P1[1]=0;       P1[2]=-1;
      P2[0]=-1.0/sqrt2;P2[1]=-1/sqrt2;P2[2]=0;
      P3[0]=1/sqrt2;   P3[1]=-1/sqrt2;P3[2]=0;
      P4[0]=1/sqrt2;   P4[1]=1/sqrt2; P4[2]=0;
      P5[0]=-1/sqrt2;  P5[1]=1/sqrt2; P5[2]=0;
      
      FacetType F0, F1, F2, F3, F4, F5, F6, F7;
      F0.P1 = P0; F0.P2 = P3; F0.P3 = P4;
      F1.P1 = P0; F1.P2 = P4; F1.P3 = P5;
      F2.P1 = P0; F2.P2 = P5; F2.P3 = P2;
      F3.P1 = P0; F3.P2 = P2; F3.P3 = P3;
      F4.P1 = P1; F4.P2 = P4; F4.P3 = P3;
      F5.P1 = P1; F5.P2 = P5; F5.P3 = P4;
      F6.P1 = P1; F6.P2 = P2; F6.P3 = P5;
      F7.P1 = P1; F7.P2 = P3; F7.P3 = P2;
      
      FacetArray[0] = F0;
      FacetArray[1] = F1;
      FacetArray[2] = F2;
      FacetArray[3] = F3;
      FacetArray[4] = F4;
      FacetArray[5] = F5;
      FacetArray[6] = F6;
      FacetArray[7] = F7;
      int pos = 8;
      // now subdivide the octahedron
      for (unsigned it = 0; it<(unsigned)iterations; it++)
        {
        // Bisect edges and move to sphere
        unsigned ntold = pos;
        for (unsigned i = 0; i < ntold; i++)
          {
          LType Pa, Pb, Pc;
          for (unsigned d = 0; d<  VDimension;d++)
            {
            Pa[d] = (FacetArray[i].P1[d] + FacetArray[i].P2[d])/2;
            Pb[d] = (FacetArray[i].P2[d] + FacetArray[i].P3[d])/2;
            Pc[d] = (FacetArray[i].P3[d] + FacetArray[i].P1[d])/2;
            }
          Pa.Normalize();
          Pb.Normalize();
          Pc.Normalize();
          FacetArray[pos].P1 = FacetArray[i].P1; 
          FacetArray[pos].P2 = Pa; 
          FacetArray[pos].P3 = Pc;
          pos++;
          FacetArray[pos].P1 = Pa;
          FacetArray[pos].P2 = FacetArray[i].P2;  
          FacetArray[pos].P3 = Pb;
          pos++;
          FacetArray[pos].P1 = Pb;
          FacetArray[pos].P2 = FacetArray[i].P3;  
          FacetArray[pos].P3 = Pc;
          pos++;
          FacetArray[i].P1 = Pa;
          FacetArray[i].P2 = Pb;
          FacetArray[i].P3 = Pc;
          }
        }
      
        
      for (unsigned j = 0; j < facets; j++)
        {
        // Find a line perpendicular to each face
        LType L, A, B;
        A = FacetArray[j].P2 - FacetArray[j].P1;
        B = FacetArray[j].P3 - FacetArray[j].P1;
        L[0] = A[1]*B[2] - B[1]*A[2];
        L[1] = B[0]*A[2] - A[0]*B[2];
        L[2] = A[0]*B[1] - B[0]*A[1];
        
        L.Normalize();
        // Scale to required length
        L *= rr;
        if (!res.checkParallel(L, res.m_Lines))
          {
          res.m_Lines.push_back(L);
          }
        }
      return(res);
      }
    break;
    default:
      std::cout << "Unsupported number of lines" << std::endl;
      return(res);
    }
}

template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::PolySub(const DispatchBase &, RadiusType , unsigned) const
{
  Self res = Self();
  res.m_Decomposable = true;
  std::cout << "Don't know how to deal with this many dimensions" << std::endl;
  return(res);
}


template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::Box(RadiusType radius)
{
  // this should work for any number of dimensions
  Self res = Self();
  res.m_Decomposable = true;
  res.SetRadius( radius );
  for (unsigned i = 0;i<VDimension;i++)
    {
    if (radius[i] != 0)
      {
      LType L;
      L.Fill(0);
      L[i] = radius[i]*2+1;
      res.m_Lines.push_back(L);
      }
    }
  // this doesn't work if one of the dimensions is zero. Suspect an
  //"inconsistency" in the way 
//  res.ComputeBufferFromLines();
   Iterator kernel_it;
   for( kernel_it=res.Begin(); kernel_it != res.End(); ++kernel_it )
     {
     *kernel_it= true;
     }

  return(res);
}


template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::Cross(RadiusType radius)
{
  // this should work for any number of dimensions
  Self res = Self();
  res.m_Decomposable = false;
  res.SetRadius( radius );
  Iterator kernel_it;
  for( kernel_it=res.Begin(); kernel_it != res.End(); ++kernel_it )
    {
    *kernel_it= false;
    }
  for( int d = 0; d<(int)VDimension; d++ )
    {
    OffsetType o;
    o.Fill( 0 );
    for( int i=-(int)radius[d]; i<=(int)radius[d]; i++ )
      {
      o[d] = i;
      res[o] = true;
      }
    }

  return(res);
}


template<unsigned int VDimension>
FlatStructuringElement<VDimension> FlatStructuringElement<VDimension>
::Ball(RadiusType radius)
{
  Self res = Self();
  res.SetRadius( radius );
  res.m_Decomposable = false;

  unsigned int i;
  
  // Image typedef
  typedef Image<bool, VDimension> ImageType;

  // Create an image to hold the ellipsoid
  //
  typename ImageType::Pointer sourceImage = ImageType::New();
  typename ImageType::RegionType region;
  RadiusType size = radius;
  for( i=0; i<(int)VDimension; i++ )
    {
    size[i] = 2*size[i] + 1;
    }
  region.SetSize( size );

  sourceImage->SetRegions( region );
  sourceImage->Allocate();
  // sourceImage->Print( std::cout );

  // Set the background to be zero
  //
  ImageRegionIterator<ImageType> it(sourceImage, region);

  for(it.GoToBegin(); !it.IsAtEnd(); ++it)
    {
    it.Set(false);
    }

  
  // Create the ellipsoid
  //

  // Ellipsoid spatial function typedef
  typedef EllipsoidInteriorExteriorSpatialFunction<VDimension> EllipsoidType;
  
  // Create an ellipsoid spatial function for the source image
  typename EllipsoidType::Pointer spatialFunction = EllipsoidType::New();

  // Define and set the axes lengths for the ellipsoid
  typename EllipsoidType::InputType axes;
  for (i=0; i < VDimension; i++)
    {
    axes[i] = res.GetSize(i);
    }
  spatialFunction->SetAxes( axes );

  // Define and set the center of the ellipsoid in physical space
  typename EllipsoidType::InputType center;
  for (i=0; i < VDimension; i++)
    {
    // put the center of ellipse in the middle of the center pixel
    center[i] = res.GetRadius(i) + 0.5; 
    }
  spatialFunction->SetCenter( center );

  // Define the orientations of the ellipsoid axes, for now, we'll use
  // the identify matrix
  typename EllipsoidType::OrientationType orientations;
  orientations.fill( 0.0 );
  orientations.fill_diagonal( 1.0 );
  spatialFunction->SetOrientations(orientations);

  typename ImageType::IndexType seed;
  for (i=0; i < VDimension; i++)
    {
    seed[i] = res.GetRadius(i);
    }
  FloodFilledSpatialFunctionConditionalIterator<ImageType, EllipsoidType> 
    sfi = FloodFilledSpatialFunctionConditionalIterator<ImageType,
    EllipsoidType>(sourceImage, spatialFunction, seed);
  sfi.SetCenterInclusionStrategy();
  
  // Iterate through the entire image and set interior pixels to 1
  for(; !sfi.IsAtEnd(); ++sfi)
    {
    sfi.Set(true);
    }

  
  // Copy the ellipsoid into the kernel
  //
  Iterator kernel_it;
  for (it.GoToBegin(), kernel_it=res.Begin();!it.IsAtEnd();++it,++kernel_it)
    {
    *kernel_it = it.Get();
    }

  // Clean up
  //   ...temporary image should be cleaned up by SmartPointers automatically

  return res;
}


template<unsigned int NDimension>
FlatStructuringElement<NDimension> 
FlatStructuringElement<NDimension>
::Annulus(RadiusType radius,
          unsigned int thickness,
          bool includeCenter)
{
  Self result = Self();
  result.SetRadius( radius );
  result.m_Decomposable = false;
  
  // Image typedef
  typedef Image<bool,NDimension> ImageType;

  // Create an image to hold the ellipsoid
  //
  typename ImageType::Pointer kernelImage = ImageType::New();
  typename ImageType::RegionType region;
  RadiusType size = radius;
  for(unsigned int i=0; i<NDimension; i++ )
    {
    size[i] = 2*size[i] + 1;
    }
  region.SetSize( size );

  kernelImage->SetRegions( region );
  kernelImage->Allocate();

  // Set the background to be zero
  //
  ImageRegionIterator<ImageType> it(kernelImage, region);

  for(it.GoToBegin(); !it.IsAtEnd(); ++it)
    {
    it.Set(false);
    }

  // Create two ellipsoids
  //

  // Ellipsoid spatial function typedef
  typedef EllipsoidInteriorExteriorSpatialFunction<NDimension> EllipsoidType;
  
  // Create an ellipsoid spatial function for the source image
  typename EllipsoidType::Pointer ellipsoidOuter = EllipsoidType::New();
  typename EllipsoidType::Pointer ellipsoidInner = EllipsoidType::New();

  // Define and set the axes lengths for the ellipsoid
  typename EllipsoidType::InputType axesOuter;
  typename EllipsoidType::InputType axesInner;
  for (unsigned int i=0; i < NDimension; i++)
    {
    axesOuter[i] = result.GetSize(i);
    axesInner[i] = std::max( 2*(long)radius[i] + 1 - 2*(long)thickness, (long)1 );
    }
  ellipsoidOuter->SetAxes( axesOuter );
  ellipsoidInner->SetAxes( axesInner );

  // Define and set the center of the ellipsoid in physical space
  typename EllipsoidType::InputType center;
  for (unsigned int i=0; i < NDimension; i++)
    {
    // put the center of ellipse in the middle of the center pixel
    center[i] = result.GetRadius(i) + 0.5; 
    }
  ellipsoidOuter->SetCenter( center );
  ellipsoidInner->SetCenter( center );

  // Define the orientations of the ellipsoid axes, for now, we'll use
  // the identity matrix
  typename EllipsoidType::OrientationType orientations;
  orientations.fill( 0.0 );
  orientations.fill_diagonal( 1.0 );
  ellipsoidOuter->SetOrientations( orientations );
  ellipsoidInner->SetOrientations( orientations );

  // Create the starting seed
  typename ImageType::IndexType seed;
  for (unsigned int i=0; i < NDimension; i++)
    {
    seed[i] = result.GetRadius(i);
    }

  // Define the iterators for each ellipsoid
  typedef FloodFilledSpatialFunctionConditionalIterator<ImageType, EllipsoidType>
    FloodIteratorType;
  FloodIteratorType itEllipsoidOuter =
    FloodIteratorType( kernelImage, ellipsoidOuter, seed );
  FloodIteratorType itEllipsoidInner = 
    FloodIteratorType( kernelImage, ellipsoidInner, seed );
  itEllipsoidOuter.SetCenterInclusionStrategy();
  itEllipsoidInner.SetCenterInclusionStrategy();

  // Iterate through the image and set all outer pixels to 'ON'
  for(; !itEllipsoidOuter.IsAtEnd(); ++itEllipsoidOuter)
    {
    itEllipsoidOuter.Set(true);
    }
  // Iterate through the image and set all inner pixels to 'OFF'
  for(; !itEllipsoidInner.IsAtEnd(); ++itEllipsoidInner)
    {
    itEllipsoidInner.Set(false);
    }

  // Set center pixel if included
  kernelImage->SetPixel( seed, includeCenter );

  // Copy the annulus into the kernel
  //
  Iterator kernel_it;
  for (it.GoToBegin(), kernel_it=result.Begin(); !it.IsAtEnd(); ++it,++kernel_it)
    {
    *kernel_it = it.Get();
    }

  // Clean up
  //   ...temporary image should be cleaned up by SmartPointers automatically

  return result;
}
template<unsigned int VDimension>
bool
FlatStructuringElement<VDimension>::
checkParallel(LType NewVec, DecompType Lines)
{
  LType NN = NewVec;
  NN.Normalize();
  for (unsigned i = 0; i < Lines.size(); i++)
    {
    LType LL = Lines[i];
    LL.Normalize();
    float L = NN*LL;
    if ((1.0 - vcl_fabs(L)) < 0.000001) return(true);
    }
  return(false);
}

template<unsigned int VDimension>
void FlatStructuringElement< VDimension >
::PrintSelf(std::ostream &os, Indent indent) const
{
  //Superclass::PrintSelf(os, indent);
  if (m_Decomposable)
    {
    os << indent << "SE decomposition:" << std::endl;
    for (unsigned i = 0;i < m_Lines.size(); i++)
      {
      os << indent << m_Lines[i] << std::endl;
      }
    }
}


template<unsigned int VDimension>
void
FlatStructuringElement<VDimension>::
ComputeBufferFromLines()
{
  if( m_Decomposable )
    {
/*    itkExceptionMacro("Element must be decomposable.");*/
    }

  // create an image with a single pixel in the center which will be dilated
  // by the structuring lines (with AnchorDilateImageFilter) so the content
  // of the buffer will reflect the shape of the structuring element

  // Image typedef
  typedef Image<bool, VDimension> ImageType;

  // Create an image to hold the ellipsoid
  //
  typename ImageType::Pointer sourceImage = ImageType::New();
  typename ImageType::RegionType region;
  RadiusType size = this->GetRadius();
  for( int i=0; i<(int)VDimension; i++ )
    {
    size[i] = 2*size[i] + 1;
    }
  region.SetSize( size );
  sourceImage->SetRegions( region );
  sourceImage->Allocate();

  // sourceImage->Print(std::cout);

  // Set the background to be zero
  //
  ImageRegionIterator<ImageType> it( sourceImage, region );

  for( it.GoToBegin(); !it.IsAtEnd(); ++it )
    {
    it.Set( false );
    }

  // set the center pixel to 1
  typename ImageType::IndexType center;
  for( int i=0; i<(int)VDimension; i++)
    {
    center[i] = this->GetRadius()[i];
    }
  sourceImage->SetPixel( center, true );

  // initialize the kernel with everything to false, to avoid warnings in
  // valgrind in the SetKernel() method
  for( Iterator kernel_it=this->Begin(); kernel_it != this->End(); ++kernel_it )
    {
    *kernel_it = false;
    }

  // dilate the pixel
  typedef VanHerkGilWermanDilateImageFilter<ImageType, Self> DilateType;
  typename DilateType::Pointer dilate = DilateType::New();
  dilate->SetInput( sourceImage );
  dilate->SetKernel( *this );
  dilate->Update();

  // copy back the image to the kernel
  ImageRegionIterator<ImageType> oit( dilate->GetOutput(), region );
  Iterator kernel_it;
  for( oit.GoToBegin(), kernel_it=this->Begin(); !oit.IsAtEnd(); ++oit,++kernel_it )
    {
    *kernel_it = oit.Get();
    }

}


}

#endif