SMIL  1.0.4
DStructuringElement.h
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _STR_ELT_HPP
31 #define _STR_ELT_HPP
32 
33 #include "Core/include/DCommon.h"
34 #include "Core/include/DBaseObject.h"
35 #include "Base/include/DImageDraw.h"
36 
37 #include <string>
38 
39 namespace smil
40 {
48  enum seType {
49  SE_Generic,
50  SE_Hex,
51  SE_Hex0,
52  SE_Squ,
53  SE_Squ0,
54  SE_Cross,
55  SE_Horiz,
56  SE_Vert,
57  SE_Line,
58  SE_Cube,
59  SE_Cross3D,
60  SE_Line3D,
61  SE_CenteredLine,
62  SE_CenteredLine3D,
63  SE_Rhombicuboctahedron
64  };
65 
69  class StrElt : public BaseObject
70  {
71  public:
75  StrElt(UINT s = 1)
76  : BaseObject("StrElt"), odd(false), seT(SE_Generic), size(s)
77  {
78  this->setName();
79  }
80 
85  StrElt(const StrElt &rhs) : BaseObject(rhs)
86  {
87  this->clone(rhs);
88  this->setName();
89  }
90 
91 #ifndef SWIG
93  /* Available only under C++ */
94  StrElt(bool oddSE, UINT nbrPts, ...)
95  : BaseObject("StrElt"), odd(oddSE), seT(SE_Generic), size(1)
96  {
97  UINT index;
98  va_list vl;
99  va_start(vl, nbrPts);
100 
101  for (UINT i = 0; i < nbrPts; i++) {
102  index = va_arg(vl, UINT);
103  addPoint(index);
104  }
105  this->setName();
106  }
108 #endif // SWIG
109 
131  StrElt(bool oddSE, vector<UINT> indexList)
132  : BaseObject("StrElt"), odd(oddSE), seT(SE_Generic), size(1)
133  {
134  vector<UINT>::iterator it;
135  for (it = indexList.begin(); it != indexList.end(); it++)
136  addPoint(*it);
137  this->setName();
138  }
139 
141  ~StrElt()
142  {
143  }
153  IntPoint getPoint(const UINT i)
154  {
155  return points[i];
156  }
157 
163  UINT getSize() const
164  {
165  return size;
166  }
167 
192  StrElt &operator=(const StrElt &rhs);
193 
200  void clone(const StrElt &rhs);
201 
203  vector<IntPoint> points;
204 
233  void addPoint(const UINT index);
234 
240  void addPoint(int x, int y, int z = 0);
241 
248  void addPoint(const IntPoint &pt);
249 
253  const StrElt operator()(int s = 1) const;
254 
261  StrElt homothety(const UINT s) const;
262 
268  StrElt transpose() const;
269 
276  StrElt merge(const StrElt &rhs);
277 
285  StrElt noCenter() const;
286 
292  virtual seType getType() const
293  {
294  return seT;
295  }
296 
301  void setName(string name)
302  {
303  this->name = name;
304  }
305 
310  void setName()
311  {
312  std::map<seType, string> seNames = {
313  {SE_Generic, "GenericSE"},
314  {SE_Squ, "SquSE"},
315  {SE_Squ0, "SquSE0"},
316  {SE_Hex, "HexSE"},
317  {SE_Hex0, "HexSE0"},
318  {SE_Cross, "CrossSE"},
319  {SE_Horiz, "HorizSE"},
320  {SE_Vert, "VertSE"},
321  {SE_Cube, "CubeSE"},
322  {SE_Cross3D, "Cross3DSE"},
323  {SE_Rhombicuboctahedron, "RhombicuboctahedronSE"},
324  {SE_Line, "LineSE"},
325  {SE_Line3D, "Line3DSE"},
326  {SE_CenteredLine, "CenteredLineSE"},
327  {SE_CenteredLine3D, "CenteredLine3DSE"}
328  };
329 
330  std::map<seType, string>::iterator it;
331  it = seNames.find(seT);
332  if (it != seNames.end())
333  this->name = seNames[seT];
334  else
335  this->name = "Unknown";
336  }
337 
343  string getName()
344  {
345  return name;
346  }
347 
354  virtual void printSelf(ostream &os = std::cout, string indent = "") const;
355 
362  virtual void printSelf(string indent) const
363  {
364  printSelf(std::cout, indent);
365  }
366 
367  bool odd;
368  seType seT;
369  UINT size;
370  };
371 
372  inline void operator<<(ostream &os, StrElt &se)
373  {
374  se.printSelf(os);
375  }
376 
385  class SquSE : public StrElt
386  {
387  public:
388  SquSE(UINT s = 1) : StrElt(false, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8)
389  {
390  className = "SquSE : StrElt";
391  seT = SE_Squ;
392  size = s;
393  this->setName();
394  }
395  };
396 
405  class SquSE0 : public StrElt
406  {
407  public:
408  typedef StrElt parentClass;
409  SquSE0(UINT s = 1) : StrElt(false, 8, 1, 2, 3, 4, 5, 6, 7, 8)
410  {
411  className = "SquSE0";
412  seT = SE_Squ0;
413  odd = false;
414  size = s;
415  this->setName();
416  }
417  };
418 
427  class HexSE : public StrElt
428  {
429  public:
430  HexSE(UINT s = 1) : StrElt(true, 7, 0, 1, 2, 3, 4, 5, 6)
431  {
432  className = "HexSE : StrElt";
433  seT = SE_Hex;
434  size = s;
435  this->setName();
436  }
437  };
438 
447  class HexSE0 : public StrElt
448  {
449  public:
450  HexSE0(UINT s = 1) : StrElt(true, 6, 1, 2, 3, 4, 5, 6)
451  {
452  className = "HexSE0";
453  seT = SE_Hex0;
454  size = s;
455  this->setName();
456  }
457  };
458 
468  class CrossSE : public StrElt
469  {
470  public:
471  CrossSE(UINT s = 1) : StrElt(false, 5, 0, 1, 5, 3, 7)
472  {
473  className = "CrossSE : StrElt";
474  seT = SE_Cross;
475  size = s;
476  this->setName();
477  }
478  };
479 
489  class HorizSE : public StrElt
490  {
491  public:
492  HorizSE(UINT s = 1) : StrElt(false, 3, 0, 1, 5)
493  {
494  className = "HorizSE : StrElt";
495  seT = SE_Horiz;
496  size = s;
497  this->setName();
498  }
499  };
500 
510  class VertSE : public StrElt
511  {
512  public:
513  VertSE(UINT s = 1) : StrElt(false, 3, 0, 3, 7)
514  {
515  className = "VertSE : StrElt";
516  seT = SE_Vert;
517  size = s;
518  this->setName();
519  }
520  };
521 
530  class CubeSE : public StrElt
531  {
532  public:
533  CubeSE(UINT s = 1) : StrElt(s)
534  {
535  className = "CubeSE : StrElt";
536  seT = SE_Cube;
537  odd = false;
538  this->setName();
539 
540  int zList[] = {0, -1, 1};
541  for (int i = 0; i < 3; i++) {
542  int z = zList[i];
543  addPoint(0, 0, z);
544  addPoint(1, 0, z);
545  addPoint(1, -1, z);
546  addPoint(0, -1, z);
547  addPoint(-1, -1, z);
548  addPoint(-1, 0, z);
549  addPoint(-1, 1, z);
550  addPoint(0, 1, z);
551  addPoint(1, 1, z);
552  }
553  }
554  };
555 
564  class Cross3DSE : public StrElt
565  {
566  public:
567  Cross3DSE(UINT s = 1) : StrElt(s)
568  {
569  className = "Cross3DSE : StrElt";
570  seT = SE_Cross3D;
571  odd = false;
572  this->setName();
573 
574  addPoint(0, 0, 0);
575  addPoint(1, 0, 0);
576  addPoint(0, -1, 0);
577  addPoint(-1, 0, 0);
578  addPoint(0, 1, 0);
579  addPoint(0, 0, -1);
580  addPoint(0, 0, 1);
581  }
582  };
583 
592  {
593  public:
594  RhombicuboctahedronSE(UINT s = 1) : StrElt(s)
595  {
596  className = "RhombicuboctahedronSE : StrElt";
597  seT = SE_Rhombicuboctahedron;
598  odd = false;
599  this->setName();
600 
601  int x, y, z;
602 
603  addPoint(0, 0, 0);
604  for (x = -2; x <= 2; x++)
605  for (y = -1; y <= 1; y++)
606  for (z = -1; z <= 1; z++)
607  addPoint(x, y, z);
608  for (x = -1; x <= 1; x++)
609  for (y = -2; y <= 2; y++)
610  for (z = -1; z <= 1; z++)
611  addPoint(x, y, z);
612  for (x = -1; x <= 1; x++)
613  for (y = -1; y <= 1; y++)
614  for (z = -2; z <= 2; z++)
615  addPoint(x, y, z);
616  }
617  };
618 
640  class LineSE : public StrElt
641  {
642  public :
654  LineSE(int length, double theta) : StrElt(1)
655  {
656  className = "LineSE : StrElt";
657  seT = SE_Line;
658  odd = false;
659  this->setName();
660 
661  int xf = round(length * cos(-theta * PI / 180.));
662  int yf = round(length * sin(-theta * PI / 180.));
663  //int xf = round(length * cos(-theta));
664  // int yf = round(length * sin(-theta));
665  vector<Point<int>> v;
666 
667  v = bresenhamPoints(0, 0, xf, yf);
668  for (size_t i = 0; i < v.size(); i++)
669  addPoint(v[i].x, v[i].y, v[i].z);
670  }
671  };
672 
695  class Line3DSE : public StrElt
696  {
697  public :
711  Line3DSE(int length, double theta, double zeta) : StrElt(1)
712  {
713  className = "Line3DSE : StrElt";
714  seT = SE_Line3D;
715  odd = false;
716  this->setName();
717 
718  double lenXY = abs(length * cos(zeta * PI / 180.));
719 
720  int zf = round(length * sin(zeta * PI / 180.));
721  int xf = round(lenXY * cos(-theta * PI / 180.));
722  int yf = round(lenXY * sin(-theta * PI / 180.));
723 
724  Bresenham line(0, 0, 0, xf, yf, zf);
725  vector<IntPoint> v = line.getPoints();
726  for (size_t i = 0; i < v.size(); i++)
727  addPoint(v[i].x, v[i].y, v[i].z);
728  }
729  };
730 
741 #if 1
742  class CenteredLineSE : public StrElt
743  {
744  public :
755  CenteredLineSE(int length, double theta = 0) : StrElt(1)
756  {
757  className = "CenteredLineSE : StrElt";
758  seT = SE_CenteredLine;
759  odd = false;
760  this->setName();
761 
762  int xf = round(length * cos(-theta * PI / 180.) / 2);
763  int yf = round(length * sin(-theta * PI / 180.) / 2);
764  //int xf = round(length * cos(-theta));
765  // int yf = round(length * sin(-theta));
766 
767  vector<Point<int>> v;
768  v = bresenhamPoints(0, 0, xf, yf);
769 
770  for (size_t i = 0; i < v.size(); i++) {
771  addPoint(v[i].x, v[i].y, v[i].z);
772  if (v[i].x == 0 && v[i].y == 0 && v[i].z == 0)
773  continue;
774  addPoint(-v[i].x, -v[i].y, -v[i].z);
775  }
776  }
777  };
778 #else
779  inline StrElt CenteredLineSE(int length, double theta = 0)
780  {
781  StrElt se = LineSE(length / 2 + 1, theta);
782  return se.merge(se.transpose());
783  }
784 #endif
785 
799 #if 1
800  class CenteredLine3DSE : public StrElt
801  {
802  public :
815  CenteredLine3DSE(int length, double theta = 0, double zeta = 0) : StrElt(1)
816  {
817  className = "CenteredLine3DSE : StrElt";
818  seT = SE_CenteredLine3D;
819  odd = false;
820  this->setName();
821 
822  length /= 2;
823  double lenXY = abs(length * cos(zeta * PI / 180.));
824 
825  int zf = round(length * sin(zeta * PI / 180.));
826  int xf = round(lenXY * cos(-theta * PI / 180.));
827  int yf = round(lenXY * sin(-theta * PI / 180.));
828 
829  Bresenham line(0, 0, 0, xf, yf, zf);
830  vector<IntPoint> v = line.getPoints();
831 
832  for (size_t i = 0; i < v.size(); i++) {
833  addPoint(v[i].x, v[i].y, v[i].z);
834  if (v[i].x == 0 && v[i].y == 0 && v[i].z == 0)
835  continue;
836  addPoint(-v[i].x, -v[i].y, -v[i].z);
837  }
838  }
839  };
840 #else
841  inline StrElt CenteredLine3DSE(int length, double theta = 0, double zeta = 0)
842  {
843  StrElt se = Line3DSE(length / 2 + 1, theta, zeta);
844  return se.merge(se.transpose());
845  }
846 #endif
847 
848  // Shortcuts
850  inline HexSE hSE(UINT s = 1)
851  {
852  return HexSE(s);
853  }
854  inline HexSE0 hSE0(UINT s = 1)
855  {
856  return HexSE0(s);
857  }
858  inline SquSE sSE(UINT s = 1)
859  {
860  return SquSE(s);
861  }
862  inline SquSE0 sSE0(UINT s = 1)
863  {
864  return SquSE0(s);
865  }
866  inline CrossSE cSE(UINT s = 1)
867  {
868  return CrossSE(s);
869  }
870  inline CubeSE cbSE(UINT s = 1)
871  {
872  return CubeSE(s);
873  }
874  inline RhombicuboctahedronSE rcoSE(UINT s = 1)
875  {
876  return RhombicuboctahedronSE(s);
877  }
890  inline StrElt buildLineSE(int length, int theta)
891  {
892  StrElt se;
893 
894  int xf = round(length * cos(theta * PI / 180.));
895  int yf = round(length * sin(theta * PI / 180.));
896 
897  vector<Point<int>> v;
898 
899  v = bresenhamPoints(0, 0, xf, yf);
900  for (size_t i = 0; i < v.size(); i++)
901  se.addPoint(v[i].x, v[i].y, v[i].z);
902  return se;
903  }
904 
905 
913  inline StrElt merge(StrElt se1, StrElt se2)
914  {
915  StrElt se;
916 
917  typename vector<IntPoint>::iterator it;
918  for (it = se1.points.begin(); it != se1.points.end(); it++) {
919  const IntPoint &p = *it;
920  se.addPoint(p);
921  }
922  for (it = se2.points.begin(); it != se2.points.end(); it++) {
923  const IntPoint &p = *it;
924  se.addPoint(p);
925  }
926  return se;
927  }
928 
929 #define DEFAULT_SE Morpho::getDefaultSE()
930 
933 } // namespace smil
934 
935 #endif
Base Smil Object.
Definition: DBaseObject.h:52
Bresenham Class.
Definition: DImageDraw.h:225
vector< IntPoint > getPoints() const
getPoints() - access a vector with the points of the line
Definition: DImageDraw.h:296
CenteredLine3DSE() - constructor.
Definition: DStructuringElement.h:801
CenteredLine3DSE(int length, double theta=0, double zeta=0)
CenteredLine3DSE() -.
Definition: DStructuringElement.h:815
CenteredLineSE()
Definition: DStructuringElement.h:743
CenteredLineSE(int length, double theta=0)
CenteredLineSE() -.
Definition: DStructuringElement.h:755
3D Cross structuring element (6 neighbors).
Definition: DStructuringElement.h:565
Cross structuring element.
Definition: DStructuringElement.h:469
3D Cubic structuring element (26 neighbors).
Definition: DStructuringElement.h:531
virtual void printSelf(ostream &os=std::cout, string s="") const
printSelf() -
Definition: DGraph.hpp:578
Hexagonal structuring element without center point.
Definition: DStructuringElement.h:448
Hexagonal structuring element.
Definition: DStructuringElement.h:428
Horizontal segment structuring element.
Definition: DStructuringElement.h:490
Line3DSE - 3D structuring element of arbitrary length and direction starting at the origin.
Definition: DStructuringElement.h:696
Line3DSE(int length, double theta, double zeta)
Line3DSE() - constructor.
Definition: DStructuringElement.h:711
LineSE - a line structuring element with arbitrary length and angle.
Definition: DStructuringElement.h:641
LineSE(int length, double theta)
LineSE() - flat structuring element of arbitrary length and direction starting at the origin.
Definition: DStructuringElement.h:654
Rhombicuboctahedron struturing element (80 neighbors).
Definition: DStructuringElement.h:592
Square structuring element without center point.
Definition: DStructuringElement.h:406
Square structuring element.
Definition: DStructuringElement.h:386
Base structuring element.
Definition: DStructuringElement.h:70
string getName()
getName() - Get the name of the structuring element
Definition: DStructuringElement.h:343
StrElt(UINT s=1)
Class constructor - generic structurant element.
Definition: DStructuringElement.h:75
virtual void printSelf(string indent) const
printSelf() - Print the contents of the structuring element
Definition: DStructuringElement.h:362
StrElt(bool oddSE, vector< UINT > indexList)
Class constructor.
Definition: DStructuringElement.h:131
StrElt transpose() const
transpose() - Return the opposite SE (symmetry with respect to 0)
Definition: DStructuringElement.cpp:121
void addPoint(const UINT index)
addPoint() - Add a point to the structurant element based on an index on a grid.
Definition: DStructuringElement.cpp:69
void setName(string name)
setName() - Set the name of the structuring element
Definition: DStructuringElement.h:301
vector< IntPoint > points
List of neighbor points.
Definition: DStructuringElement.h:203
void setName()
setName() - Set the name of the structuring element
Definition: DStructuringElement.h:310
const StrElt operator()(int s=1) const
operator() -
Definition: DStructuringElement.cpp:113
void clone(const StrElt &rhs)
clone() - Clone a structuring element
Definition: DStructuringElement.cpp:61
StrElt noCenter() const
Return the SE with no center.
Definition: DStructuringElement.cpp:160
virtual void printSelf(ostream &os=std::cout, string indent="") const
printSelf() - Print the contents of the structuring element
Definition: DStructuringElement.cpp:179
UINT getSize() const
getSize() - Get the size of the Structuring Element
Definition: DStructuringElement.h:163
StrElt & operator=(const StrElt &rhs)
Clone a structuring element.
Definition: DStructuringElement.cpp:55
StrElt merge(const StrElt &rhs)
merge() - Merge a structuring element
Definition: DStructuringElement.cpp:138
IntPoint getPoint(const UINT i)
getPoint() - Get the coordinates (as a point) of the pixel of order i in the structuring element
Definition: DStructuringElement.h:153
StrElt(const StrElt &rhs)
Class constructor - clone another structuring element.
Definition: DStructuringElement.h:85
StrElt homothety(const UINT s) const
homothety() - Build and return an homothetic SE with size s
Definition: DStructuringElement.cpp:90
virtual seType getType() const
getType() - Get the type of the structuring element
Definition: DStructuringElement.h:292
Vertical segment structuring element.
Definition: DStructuringElement.h:511
vector< IntPoint > bresenhamPoints(int p1x, int p1y, int p2x, int p2y, int xMax=0, int yMax=0)
Find intermediate points forming a line between two end points, using the Bresenham Line Draw Algorit...
Definition: DImageDraw.h:62
StrElt merge(StrElt se1, StrElt se2)
merge() - merge two Structuring Elements
Definition: DStructuringElement.h:913
StrElt buildLineSE(int length, int theta)
buildLineSE() - build a line structuring element with arbitrary length and angle.
Definition: DStructuringElement.h:890
Definition: DUtils.h:13