Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cPolygon.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cPolygon_H
7 #define _INC_cPolygon_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cRectI.h"
14 
15 namespace GrayLib
16 {
17  template < typename TYPE, class TYPE_RECT = cRectNT<TYPE> >
19  {
24 
26 
27  public:
28  typedef TYPE DVALUE_t;
30  typedef TYPE_RECT RECT_t;
31 
32  static const ITERATE_t k_MAX_VERTS = 1024;
33 
34  protected:
35  TYPE_RECT m_rectUnion;
37 
38  public:
40  {
41  m_rectUnion.SetRectEmptyX();
42  }
43 
44  const TYPE_RECT& get_BoundingRect(void) const
45  {
46  return m_rectUnion;
47  }
49  {
50  return m_aPoints.GetSize();
51  }
52  bool isPolyEmpty() const
53  {
54  return(m_aPoints.GetSize() <= 0);
55  }
56  const POINT_t& GetVert(ITERATE_t i) const
57  {
58  return m_aPoints.ConstElementAt(i);
59  }
61  {
62  return m_aPoints.ElementAt(i);
63  }
64 
65  bool IsEqual(const THIS_t& p) const
66  {
67  if (!m_rectUnion.IsEqual(p.m_rectUnion))
68  return false;
69  if (get_NumVerts() != p.get_NumVerts())
70  return false;
71  return true;
72  }
73 
75  {
76  SetEmpty();
77  if (nCount <= 0 || nCount > k_MAX_VERTS) // invalid.
78  return 0;
79  m_aPoints.SetSize(nCount);
80  if (pPoints != nullptr)
81  {
82  for (ITERATE_t i = 0; i < nCount; i++)
83  {
84  m_aPoints.SetAt(i, pPoints[i]);
85  m_rectUnion.UnionPoint(pPoints[i].x, pPoints[i].y);
86  }
87  }
88  return nCount;
89  }
90 
91  void SetEmpty()
92  {
93  m_rectUnion.SetRectEmptyX();
94  m_aPoints.RemoveAll();
95  }
97  {
98  m_aPoints.RemoveAt(i);
99  }
100  void InsertVert(ITERATE_t i, const POINT_t& pt)
101  {
102  m_rectUnion.UnionPoint(pt.x, pt.y);
103  m_aPoints.InsertAt(i, pt);
104  }
106  {
107  m_rectUnion.UnionPoint(pt.x, pt.y);
108  return m_aPoints.Add(pt);
109  }
111  {
112  return AddVert(POINT_t(x, y));
113  }
114 
115  void UnionPolyRect(TYPE_RECT& rect) const
116  {
120  ITERATE_t nPoints = m_aPoints.GetSize();
121  for (ITERATE_t i = 0; i < nPoints; i++)
122  {
123  rect.UnionPoint(m_aPoints[i].x, m_aPoints[i].y);
124  }
125  }
126 
127  void SetCopy(const THIS_t& poly)
128  {
129  m_rectUnion = poly.m_rectUnion;
130  m_aPoints.SetCopy(poly.m_aPoints);
131  }
132 
133  void put_PolyRect(const TYPE_RECT& rect)
134  {
136  m_rectUnion = rect;
137  m_aPoints.SetSize(4);
138  m_aPoints.SetAt(0, POINT_t(rect.left, rect.top));
139  m_aPoints.SetAt(1, POINT_t(rect.right, rect.top));
140  m_aPoints.SetAt(2, POINT_t(rect.right, rect.bottom));
141  m_aPoints.SetAt(3, POINT_t(rect.left, rect.bottom));
142  }
143  void put_PolyRect(TYPE fSizeX, TYPE fSizeY)
144  {
145  put_PolyRect(TYPE_RECT(0, 0, fSizeX, fSizeY));
146  }
147 
148  void OffsetPoly(TYPE dx, TYPE dy)
149  {
150  ITERATE_t nPoints = m_aPoints.GetSize();
151  for (ITERATE_t i = 0; i < nPoints; i++)
152  {
153  m_aPoints[i].x += dx;
154  m_aPoints[i].y += dy;
155  }
156  m_rectUnion.OffsetRect(dx, dy);
157  }
158  };
159 
160  template < typename TYPE = float, class TYPE_RECT = cRectFT<TYPE> >
161  class GRAYLIB_LINK cPolygonFT : public cPolygonT<TYPE, TYPE_RECT>
162  {
165 
166  public:
168  typedef typename SUPER_t::DVALUE_t DVALUE_t; // __GNUC__ needs this.
169  typedef typename SUPER_t::POINT_t POINT_t;
170  typedef typename SUPER_t::RECT_t RECT_t; // ;
171 
172  public:
174  {
175  }
177  {
178  }
179 
180  ITERATE_t _cdecl CreatePolygonValuesF(ITERATE_t nCount, /* DVALUE_t x1, DVALUE_t y1, DVALUE_t x2, DVALUE_t y2, DVALUE_t x3, DVALUE_t y3, */ ...);
181  bool FixPolygon();
182 
183  bool PtInPoly(DVALUE_t x, DVALUE_t y) const;
184  bool PtInPoly(const POINT_t& pt) const
185  {
186  return PtInPoly(pt.x, pt.y);
187  }
188 
189  HRESULT v_SetPoly(const cVariant& vVal, ITERATE_t iStart = 0);
190  void v_GetPoly(cVariant& vVal) const;
191 
192  bool IsRectIntersect(const RECT_t& rect) const;
193 
194  cString toString(void) const;
195 
196  ITERATE_t FindClosestVert(const POINT_t& pt) const;
197  };
198 
199  class GRAYLIB_LINK cPolygon : public cPolygonT<int, cRectI>
200  {
203 
204  public:
205  cPolygon();
206  ~cPolygon();
207 
208  ITERATE_t _cdecl CreatePolygonValuesF(ITERATE_t nCount, /* DVALUE_t x1, DVALUE_t y1, DVALUE_t x2, DVALUE_t y2, DVALUE_t x3, DVALUE_t y3, */ ...);
209 
210  bool PtInPoly(DVALUE_t x, DVALUE_t y) const;
211  bool PtInPoly(const POINT_t& pt) const
212  {
213  return PtInPoly(pt.x, pt.y);
214  }
215  };
216 
219 
220 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
221  template class GRAYLIB_LINK cPolygonT<int, cRectI>;
222  template class GRAYLIB_LINK cPolygonT<float, cRectf>;
224 #endif
225 
226 }
227 
228 #endif // _INC_cPolygon_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define TYPE
Definition: StrT.cpp:38
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cPolygon.h:162
SUPER_t::RECT_t RECT_t
Definition: cPolygon.h:170
SUPER_t::DVALUE_t DVALUE_t
Definition: cPolygon.h:168
cPolygonT< TYPE, TYPE_RECT > SUPER_t
Definition: cPolygon.h:167
bool PtInPoly(const POINT_t &pt) const
Definition: cPolygon.h:184
~cPolygonFT()
Definition: cPolygon.h:176
cPolygonFT()
Definition: cPolygon.h:173
SUPER_t::POINT_t POINT_t
Definition: cPolygon.h:169
Definition: cPolygon.h:19
void OffsetPoly(TYPE dx, TYPE dy)
Definition: cPolygon.h:148
TYPE DVALUE_t
Dimension value type.
Definition: cPolygon.h:28
void InsertVert(ITERATE_t i, const POINT_t &pt)
Definition: cPolygon.h:100
TYPE_RECT RECT_t
cRectNT<TYPE>
Definition: cPolygon.h:30
ITERATE_t get_NumVerts() const
Definition: cPolygon.h:48
bool IsEqual(const THIS_t &p) const
Definition: cPolygon.h:65
const POINT_t & GetVert(ITERATE_t i) const
Definition: cPolygon.h:56
cPolygonT()
Definition: cPolygon.h:39
void put_PolyRect(const TYPE_RECT &rect)
Definition: cPolygon.h:133
void SetCopy(const THIS_t &poly)
Definition: cPolygon.h:127
ITERATE_t CreatePolygonPoints(const POINT_t *pPoints, ITERATE_t nCount)
Definition: cPolygon.h:74
cVecT2< TYPE > POINT_t
Definition: cPolygon.h:29
ITERATE_t AddVert(TYPE x, TYPE y)
Definition: cPolygon.h:110
void put_PolyRect(TYPE fSizeX, TYPE fSizeY)
Definition: cPolygon.h:143
void SetEmpty()
Definition: cPolygon.h:91
const TYPE_RECT & get_BoundingRect(void) const
Definition: cPolygon.h:44
void UnionPolyRect(TYPE_RECT &rect) const
Definition: cPolygon.h:115
POINT_t & RefVert(ITERATE_t i)
Definition: cPolygon.h:60
bool isPolyEmpty() const
Definition: cPolygon.h:52
ITERATE_t AddVert(const POINT_t &pt)
Definition: cPolygon.h:105
cArrayStruct< POINT_t > m_aPoints
k_MAX_VERTS
Definition: cPolygon.h:36
TYPE_RECT m_rectUnion
The union rectangle m_aPoints. inclusive if float, non-inclusive if int.
Definition: cPolygon.h:35
void DeleteVert(ITERATE_t i)
Definition: cPolygon.h:96
Definition: cPolygon.h:200
bool PtInPoly(const POINT_t &pt) const
Definition: cPolygon.h:211
Definition: cVariant.h:26
Definition: cVecT.h:473
TYPE x
Definition: cVecT.h:490
TYPE y
Definition: cVecT.h:490
void RemoveAll()
Clean up.
Definition: cArray.h:230
void SetAt(ITERATE_t nIndex, ARG_TYPE newElement)
Definition: cArray.h:173
TYPE & ElementAt(ITERATE_t nIndex)
Definition: cArray.h:167
void InsertAt(ITERATE_t nIndex, ARG_TYPE newElement)
Definition: cArray.h:341
ITERATE_t Add(ARG_TYPE newElement)
Definition: cArray.h:199
ITERATE_t GetSize() const noexcept
Definition: cArray.h:137
void RemoveAt(ITERATE_t nIndex)
Definition: cArray.h:367
void SetSize(ITERATE_t nNewSize)
Definition: cArray.h:248
Definition: cArray.h:932
REF_t ConstElementAt(ITERATE_t nIndex) const
Definition: cArray.h:534
void SetCopy(const cArrayTyped< TYPE, ARG_TYPE > &aValues)
Definition: cArray.h:598
Definition: cMesh.h:22
cPolygonFT< float, cRectf > cPolygonf
Definition: cPolygon.h:217
interface const RECTQ_t & rect
Definition: cQuadtree.h:44
cPolygonFT< double, cRectd > cPolygond
Definition: cPolygon.h:218
int ITERATE_t
like size_t but signed
Definition: Index.h:28