18 #ifndef __itkFlatStructuringElement_txx
19 #define __itkFlatStructuringElement_txx
20 #include "vnl/vnl_math.h"
26 #define M_PI vnl_math::pi
39 template<
unsigned int VDimension>
47 float theta, phi, step;
49 step =
M_PI/(lines - 1);
54 std::cout <<
"theta= " << theta <<
" phi = " << phi << std::endl;
55 LType O = res.mkOffset(phi, theta);
56 std::cout << O << std::endl;
59 std::cout <<
"Already have this line" << std::endl;
68 std::cout <<
"---------------" << std::endl;
75 template<
unsigned int VDimension>
86 for (
unsigned i=0;i<VDimension;i++)
88 if (radius[i] > rr) rr = radius[i];
94 else if (rr <= 8) lines=4;
102 float k1 = (
M_PI * (float)radius[0])/((float)lines);
103 float k2 = (
M_PI * (float)radius[1])/((float)lines);
109 while (theta <=
M_PI/2.0 + 0.0001)
112 O[0] = k1 * vcl_cos(theta);
113 O[1] = k2 * vcl_sin(theta);
119 O[0] = k1 * vcl_cos(-theta);
120 O[1] = k2 * vcl_sin(-theta);
137 template<
unsigned int VDimension>
146 int faces = lines * 2;
147 for (
unsigned i=0;i<VDimension;i++)
149 if (radius[i] > rr) rr = radius[i];
156 float phi = (1.0 + vcl_sqrt(5.0)) / 2.0;
159 unsigned facets = 12;
160 typedef std::vector<FacetType> FacetArrayType;
161 FacetArrayType FacetArray;
162 FacetArray.resize( facets );
171 PP[0]=c;PP[1]=0;PP[2]=0.5;
173 PP[0]=-c;PP[1]=0;PP[2]=0.5;
175 PP[0]=-b;PP[1]=b;PP[2]=b;
179 PP[0]=-c;PP[1]=0;PP[2]=0.5;
181 PP[0]=c;PP[1]=0;PP[2]=0.5;
183 PP[0]=b;PP[1]=-b;PP[2]=b;
187 PP[0]=c;PP[1]=0;PP[2]=-0.5;
189 PP[0]=-c;PP[1]=0;PP[2]=-0.5;
191 PP[0]=-b;PP[1]=-b;PP[2]=-b;
195 PP[0]=-c;PP[1]=0;PP[2]=-0.5;
197 PP[0]=c;PP[1]=0;PP[2]=-0.5;
199 PP[0]=b;PP[1]=b;PP[2]=-b;
203 PP[0]=0;PP[1]=0.5;PP[2]=-c;
205 PP[0]=0;PP[1]=0.5;PP[2]=c;
207 PP[0]=b;PP[1]=b;PP[2]=b;
211 PP[0]=0;PP[1]=0.5;PP[2]=c;
213 PP[0]=0;PP[1]=0.5;PP[2]=-c;
215 PP[0]=-b;PP[1]=b;PP[2]=-b;
219 PP[0]=0;PP[1]=-0.5;PP[2]=-c;
221 PP[0]=0;PP[1]=-0.5;PP[2]=c;
223 PP[0]=-b;PP[1]=-b;PP[2]=b;
227 PP[0]=0;PP[1]=-0.5;PP[2]=c;
229 PP[0]=0;PP[1]=-0.5;PP[2]=-c;
231 PP[0]=b;PP[1]=-b;PP[2]=-b;
235 PP[0]=0.5;PP[1]=c;PP[2]=0;
237 PP[0]=0.5;PP[1]=-c;PP[2]=0;
239 PP[0]=b;PP[1]=-b;PP[2]=b;
243 PP[0]=0.5;PP[1]=-c;PP[2]=0;
245 PP[0]=0.5;PP[1]=c;PP[2]=0;
247 PP[0]=b;PP[1]=b;PP[2]=-b;
251 PP[0]=-0.5;PP[1]=c;PP[2]=0;
253 PP[0]=-0.5;PP[1]=-c;PP[2]=0;
255 PP[0]=-b;PP[1]=-b;PP[2]=-b;
259 PP[0]=-0.5;PP[1]=-c;PP[2]=0;
261 PP[0]=-0.5;PP[1]=c;PP[2]=0;
263 PP[0]=-b;PP[1]=b;PP[2]=b;
266 for (
unsigned j = 0; j < facets; j++)
270 A = FacetArray[j].P2 - FacetArray[j].P1;
271 B = FacetArray[j].P3 - FacetArray[j].P1;
272 L[0] = A[1]*B[2] - B[1]*A[2];
273 L[1] = B[0]*A[2] - A[0]*B[2];
274 L[2] = A[0]*B[1] - B[0]*A[1];
293 A[0]=1;A[1]=0;A[2]=0;
296 A[0]=0;A[1]=1;A[2]=0;
299 A[0]=0;A[1]=0;A[2]=1;
303 A[0]=1;A[1]=1;A[2]=1;
308 A[0]=-1;A[1]=1;A[2]=1;
313 A[0]=1;A[1]=-1;A[2]=1;
318 A[0]=-1;A[1]=-1;A[2]=1;
328 float phi = (1.0 + vcl_sqrt(5.0)) / 2.0;
330 float b = 1.0/(2.0*phi);
331 unsigned facets = 20;
332 typedef std::vector<FacetType> FacetArrayType;
333 FacetArrayType FacetArray;
334 FacetArray.resize( facets );
341 PP[0]=0;PP[1]=b;PP[2]=-a;
343 PP[0]=b;PP[1]=a;PP[2]=0;
345 PP[0]=-b;PP[1]=a;PP[2]=0;
349 PP[0]=0;PP[1]=b;PP[2]=a;
351 PP[0]=-b;PP[1]=a;PP[2]=0;
353 PP[0]=b;PP[1]=a;PP[2]=0;
357 PP[0]=0;PP[1]=b;PP[2]=a;
359 PP[0]=0;PP[1]=-b;PP[2]=a;
361 PP[0]=-a;PP[1]=0;PP[2]=b;
365 PP[0]=0;PP[1]=b;PP[2]=a;
367 PP[0]=a;PP[1]=0;PP[2]=b;
369 PP[0]=0;PP[1]=-b;PP[2]=a;
373 PP[0]=0;PP[1]=b;PP[2]=-a;
375 PP[0]=0;PP[1]=-b;PP[2]=-a;
377 PP[0]=a;PP[1]=0;PP[2]=-b;
381 PP[0]=0;PP[1]=b;PP[2]=-a;
383 PP[0]=-a;PP[1]=0;PP[2]=-b;
385 PP[0]=0;PP[1]=-b;PP[2]=-a;
389 PP[0]=0;PP[1]=-b;PP[2]=a;
391 PP[0]=b;PP[1]=-a;PP[2]=0;
393 PP[0]=-b;PP[1]=-a;PP[2]=0;
397 PP[0]=0;PP[1]=-b;PP[2]=-a;
399 PP[0]=-b;PP[1]=-a;PP[2]=0;
401 PP[0]=b;PP[1]=-a;PP[2]=0;
405 PP[0]=-b;PP[1]=a;PP[2]=0;
407 PP[0]=-a;PP[1]=0;PP[2]=b;
409 PP[0]=-a;PP[1]=0;PP[2]=-b;
413 PP[0]=-b;PP[1]=-a;PP[2]=0;
415 PP[0]=-a;PP[1]=0;PP[2]=-b;
417 PP[0]=-a;PP[1]=0;PP[2]=b;
421 PP[0]=b;PP[1]=a;PP[2]=0;
423 PP[0]=a;PP[1]=0;PP[2]=-b;
425 PP[0]=a;PP[1]=0;PP[2]=b;
429 PP[0]=b;PP[1]=-a;PP[2]=0;
431 PP[0]=a;PP[1]=0;PP[2]=b;
433 PP[0]=a;PP[1]=0;PP[2]=-b;
437 PP[0]=0;PP[1]=b;PP[2]=a;
439 PP[0]=-a;PP[1]=0;PP[2]=b;
441 PP[0]=-b;PP[1]=a;PP[2]=0;
445 PP[0]=0;PP[1]=b;PP[2]=a;
447 PP[0]=b;PP[1]=a;PP[2]=0;
449 PP[0]=a;PP[1]=0;PP[2]=b;
453 PP[0]=0;PP[1]=b;PP[2]=-a;
455 PP[0]=-b;PP[1]=a;PP[2]=0;
457 PP[0]=-a;PP[1]=0;PP[2]=-b;
461 PP[0]=0;PP[1]=b;PP[2]=-a;
463 PP[0]=a;PP[1]=0;PP[2]=-b;
465 PP[0]=b;PP[1]=a;PP[2]=0;
469 PP[0]=0;PP[1]=-b;PP[2]=-a;
471 PP[0]=-a;PP[1]=0;PP[2]=-b;
473 PP[0]=-b;PP[1]=-a;PP[2]=0;
477 PP[0]=0;PP[1]=-b;PP[2]=-a;
479 PP[0]=b;PP[1]=-a;PP[2]=0;
481 PP[0]=a;PP[1]=0;PP[2]=-b;
485 PP[0]=0;PP[1]=-b;PP[2]=a;
487 PP[0]=-b;PP[1]=-a;PP[2]=0;
489 PP[0]=-a;PP[1]=0;PP[2]=b;
493 PP[0]=0;PP[1]=-b;PP[2]=a;
495 PP[0]=a;PP[1]=0;PP[2]=b;
497 PP[0]=b;PP[1]=-a;PP[2]=0;
501 for (
unsigned j = 0; j < facets; j++)
505 A = FacetArray[j].P2 - FacetArray[j].P1;
506 B = FacetArray[j].P3 - FacetArray[j].P1;
507 L[0] = A[1]*B[2] - B[1]*A[2];
508 L[1] = B[0]*A[2] - A[0]*B[2];
509 L[2] = A[0]*B[1] - B[0]*A[1];
530 unsigned int facets = 8 * (int)vcl_pow((
double)4, iterations);
531 float sqrt2 = vcl_sqrt(2.0);
533 typedef std::vector<FacetType> FacetArrayType;
534 FacetArrayType FacetArray;
535 FacetArray.resize( facets );
538 LType P0(0.0), P1(0.0), P2(0.0), P3(0.0), P4(0.0), P5(0.0);
539 P0[0]=0; P0[1]=0; P0[2]=1;
540 P1[0]=0; P1[1]=0; P1[2]=-1;
546 FacetType F0, F1, F2, F3, F4, F5, F6, F7;
547 F0.
P1 = P0; F0.
P2 = P3; F0.
P3 = P4;
548 F1.
P1 = P0; F1.
P2 = P4; F1.
P3 = P5;
549 F2.
P1 = P0; F2.
P2 = P5; F2.
P3 = P2;
550 F3.
P1 = P0; F3.
P2 = P2; F3.
P3 = P3;
551 F4.
P1 = P1; F4.
P2 = P4; F4.
P3 = P3;
552 F5.
P1 = P1; F5.
P2 = P5; F5.
P3 = P4;
553 F6.
P1 = P1; F6.
P2 = P2; F6.
P3 = P5;
554 F7.
P1 = P1; F7.
P2 = P3; F7.
P3 = P2;
566 for (
unsigned it = 0; it<(unsigned)iterations; it++)
569 unsigned ntold = pos;
570 for (
unsigned i = 0; i < ntold; i++)
573 for (
unsigned d = 0; d< VDimension;d++)
575 Pa[d] = (FacetArray[i].P1[d] + FacetArray[i].P2[d])/2;
576 Pb[d] = (FacetArray[i].P2[d] + FacetArray[i].P3[d])/2;
577 Pc[d] = (FacetArray[i].P3[d] + FacetArray[i].P1[d])/2;
582 FacetArray[pos].P1 = FacetArray[i].P1;
583 FacetArray[pos].P2 = Pa;
584 FacetArray[pos].P3 = Pc;
586 FacetArray[pos].P1 = Pa;
587 FacetArray[pos].P2 = FacetArray[i].P2;
588 FacetArray[pos].P3 = Pb;
590 FacetArray[pos].P1 = Pb;
591 FacetArray[pos].P2 = FacetArray[i].P3;
592 FacetArray[pos].P3 = Pc;
594 FacetArray[i].P1 = Pa;
595 FacetArray[i].P2 = Pb;
596 FacetArray[i].P3 = Pc;
601 for (
unsigned j = 0; j < facets; j++)
605 A = FacetArray[j].P2 - FacetArray[j].P1;
606 B = FacetArray[j].P3 - FacetArray[j].P1;
607 L[0] = A[1]*B[2] - B[1]*A[2];
608 L[1] = B[0]*A[2] - A[0]*B[2];
609 L[2] = A[0]*B[1] - B[0]*A[1];
623 std::cout <<
"Unsupported number of lines" << std::endl;
628 template<
unsigned int VDimension>
634 std::cout <<
"Don't know how to deal with this many dimensions" << std::endl;
639 template<
unsigned int VDimension>
647 for (
unsigned i = 0;i<VDimension;i++)
653 L[i] = radius[i]*2+1;
661 for( kernel_it=res.
Begin(); kernel_it != res.
End(); ++kernel_it )
670 template<
unsigned int VDimension>
679 for( kernel_it=res.
Begin(); kernel_it != res.
End(); ++kernel_it )
683 for(
int d = 0; d<(int)VDimension; d++ )
687 for(
int i=-(
int)radius[d]; i<=(int)radius[d]; i++ )
698 template<
unsigned int VDimension>
713 typename ImageType::Pointer sourceImage = ImageType::New();
714 typename ImageType::RegionType region;
716 for( i=0; i<(int)VDimension; i++ )
718 size[i] = 2*size[i] + 1;
722 sourceImage->SetRegions( region );
723 sourceImage->Allocate();
743 typename EllipsoidType::Pointer spatialFunction = EllipsoidType::New();
746 typename EllipsoidType::InputType axes;
747 for (i=0; i < VDimension; i++)
751 spatialFunction->SetAxes( axes );
754 typename EllipsoidType::InputType center;
755 for (i=0; i < VDimension; i++)
760 spatialFunction->SetCenter( center );
764 typename EllipsoidType::OrientationType orientations;
765 orientations.fill( 0.0 );
766 orientations.fill_diagonal( 1.0 );
767 spatialFunction->SetOrientations(orientations);
769 typename ImageType::IndexType seed;
770 for (i=0; i < VDimension; i++)
776 EllipsoidType>(sourceImage, spatialFunction, seed);
791 *kernel_it = it.
Get();
801 template<
unsigned int NDimension>
805 unsigned int thickness,
817 typename ImageType::Pointer kernelImage = ImageType::New();
818 typename ImageType::RegionType region;
820 for(
unsigned int i=0; i<NDimension; i++ )
822 size[i] = 2*size[i] + 1;
826 kernelImage->SetRegions( region );
827 kernelImage->Allocate();
845 typename EllipsoidType::Pointer ellipsoidOuter = EllipsoidType::New();
846 typename EllipsoidType::Pointer ellipsoidInner = EllipsoidType::New();
849 typename EllipsoidType::InputType axesOuter;
850 typename EllipsoidType::InputType axesInner;
851 for (
unsigned int i=0; i < NDimension; i++)
853 axesOuter[i] = result.
GetSize(i);
854 axesInner[i] = std::max( 2*(
long)radius[i] + 1 - 2*(
long)thickness, (
long)1 );
856 ellipsoidOuter->SetAxes( axesOuter );
857 ellipsoidInner->SetAxes( axesInner );
860 typename EllipsoidType::InputType center;
861 for (
unsigned int i=0; i < NDimension; i++)
866 ellipsoidOuter->SetCenter( center );
867 ellipsoidInner->SetCenter( center );
871 typename EllipsoidType::OrientationType orientations;
872 orientations.fill( 0.0 );
873 orientations.fill_diagonal( 1.0 );
874 ellipsoidOuter->SetOrientations( orientations );
875 ellipsoidInner->SetOrientations( orientations );
878 typename ImageType::IndexType seed;
879 for (
unsigned int i=0; i < NDimension; i++)
887 FloodIteratorType itEllipsoidOuter =
888 FloodIteratorType( kernelImage, ellipsoidOuter, seed );
889 FloodIteratorType itEllipsoidInner =
890 FloodIteratorType( kernelImage, ellipsoidInner, seed );
891 itEllipsoidOuter.SetCenterInclusionStrategy();
892 itEllipsoidInner.SetCenterInclusionStrategy();
895 for(; !itEllipsoidOuter.IsAtEnd(); ++itEllipsoidOuter)
897 itEllipsoidOuter.Set(
true);
900 for(; !itEllipsoidInner.IsAtEnd(); ++itEllipsoidInner)
902 itEllipsoidInner.Set(
false);
906 kernelImage->SetPixel( seed, includeCenter );
913 *kernel_it = it.
Get();
921 template<
unsigned int VDimension>
928 for (
unsigned i = 0; i < Lines.size(); i++)
933 if ((1.0 - vcl_fabs(L)) < 0.000001)
return(
true);
938 template<
unsigned int VDimension>
945 os << indent <<
"SE decomposition:" << std::endl;
946 for (
unsigned i = 0;i < m_Lines.size(); i++)
948 os << indent << m_Lines[i] << std::endl;
954 template<
unsigned int VDimension>
973 typename ImageType::Pointer sourceImage = ImageType::New();
974 typename ImageType::RegionType region;
976 for(
int i=0; i<(int)VDimension; i++ )
978 size[i] = 2*size[i] + 1;
981 sourceImage->SetRegions( region );
982 sourceImage->Allocate();
996 typename ImageType::IndexType center;
997 for(
int i=0; i<(int)VDimension; i++)
999 center[i] = this->GetRadius()[i];
1001 sourceImage->SetPixel( center,
true );
1005 for(
Iterator kernel_it=this->Begin(); kernel_it != this->End(); ++kernel_it )
1012 typename DilateType::Pointer dilate = DilateType::New();
1013 dilate->SetInput( sourceImage );
1014 dilate->SetKernel( *
this );
1020 for( oit.GoToBegin(), kernel_it=this->Begin(); !oit.IsAtEnd(); ++oit,++kernel_it )
1022 *kernel_it = oit.
Get();