Domain.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Domain.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. //#include "OTSData.h"
  5. #include "Domain.h"
  6. #include "XMLSerialization.h"
  7. namespace OTSDATA {
  8. using namespace xmls;
  9. // CDomain
  10. // constructor
  11. CDomain::CDomain()
  12. {
  13. // initialization
  14. Init();
  15. }
  16. CDomain::CDomain(DOMAIN_SHAPE a_nShape, CRect a_rectDomain)
  17. {
  18. // initialization
  19. Init();
  20. // assign class members
  21. m_nShape = a_nShape;
  22. m_rectDomain = a_rectDomain;
  23. }
  24. // copy constructor
  25. CDomain::CDomain(const CDomain& a_oSource)
  26. {
  27. // can't copy itself
  28. if (&a_oSource == this)
  29. {
  30. return;
  31. }
  32. // copy data over
  33. Duplicate(a_oSource);
  34. }
  35. // copy constructor
  36. CDomain::CDomain(CDomain* a_poSource)
  37. {
  38. // input check
  39. ASSERT(a_poSource);
  40. if (!a_poSource)
  41. {
  42. return;
  43. }
  44. // can't copy itself
  45. if (a_poSource == this)
  46. {
  47. return;
  48. }
  49. // copy data over
  50. Duplicate(*a_poSource);
  51. }
  52. // =operator
  53. CDomain& CDomain::operator=(const CDomain& a_oSource)
  54. {
  55. // cleanup
  56. Cleanup();
  57. // copy the class data over
  58. Duplicate(a_oSource);
  59. // return class
  60. return *this;
  61. }
  62. // ==operator
  63. BOOL CDomain::operator==(const CDomain& a_oSource)
  64. {
  65. return m_nShape == a_oSource.m_nShape && m_rectDomain == a_oSource.m_rectDomain;
  66. }
  67. // destructor
  68. CDomain::~CDomain()
  69. {
  70. // cleanup
  71. Cleanup();
  72. }
  73. // CDomain functions
  74. // public
  75. // check if the region is valid
  76. BOOL CDomain::IsInvalid() const
  77. {
  78. return (m_nShape < DOMAIN_SHAPE::MIN) || m_nShape > DOMAIN_SHAPE::MAX || m_rectDomain.IsRectEmpty();
  79. }
  80. void CDomain::SetPolygonPoint(const std::vector<CPoint> a_PolygonPoint)
  81. {
  82. std::vector<CPoint> ps = a_PolygonPoint;
  83. m_PolygonPoint.clear();
  84. for (auto p : ps)
  85. {
  86. m_PolygonPoint.push_back(p);
  87. }
  88. }
  89. // check if have common area with the given domain
  90. BOOL CDomain::IntersectDomain(const CDomain& a_oDomain)
  91. {
  92. // return FALSE if any domain of the two is invalid
  93. if (IsInvalid() || a_oDomain.IsInvalid())
  94. {
  95. return FALSE;
  96. }
  97. // create two CRgn objects with the two region objects
  98. CRect rectCur, rectGiven;
  99. rectCur = GetDomainRect();
  100. rectGiven = a_oDomain.GetDomainRect();
  101. CRgn rgnCur, rgnGiven, rgnTest;
  102. BOOL bRgnCur, bRgnGiven;
  103. bRgnCur = rgnTest.CreateEllipticRgnIndirect(rectCur);
  104. if (IsRound())
  105. {
  106. bRgnCur = rgnCur.CreateEllipticRgnIndirect(rectCur);
  107. }
  108. else
  109. {
  110. bRgnCur = rgnCur.CreateRectRgnIndirect(rectCur);
  111. }
  112. if (a_oDomain.IsRound())
  113. {
  114. bRgnGiven = rgnGiven.CreateEllipticRgnIndirect(rectGiven);
  115. }
  116. else
  117. {
  118. rgnGiven.CreateRectRgnIndirect(rectGiven);
  119. }
  120. // combine the two CRgn objects with RGN_AND
  121. int nCombineResult = rgnTest.CombineRgn(&rgnCur, &rgnGiven, RGN_AND);
  122. // return TRUE if combine success and combine result is not empty
  123. return (nCombineResult != ERROR) && (nCombineResult != NULLREGION);
  124. }
  125. // check if the point is in domain
  126. BOOL CDomain::PtInDomain(const CPoint& a_poiCheck) const
  127. {
  128. // return FALSE if is invalid
  129. if (IsInvalid())
  130. {
  131. return FALSE;
  132. }
  133. // check sharp
  134. if (IsRound())
  135. {
  136. // round
  137. double dRoundRegionRadius = (double)GetDomainRect().Width() / 2000.0;
  138. dRoundRegionRadius = dRoundRegionRadius * dRoundRegionRadius;
  139. double dDisToRegioCenter;
  140. double dX, dY;
  141. dX = (double)abs(GetDomainCenter().x - a_poiCheck.x) / 1000;
  142. dY = (double)abs(GetDomainCenter().y - a_poiCheck.y) / 1000;
  143. dX = dX * dX;
  144. dY = dY * dY;
  145. dDisToRegioCenter = dX + dY;
  146. if (dRoundRegionRadius >= dDisToRegioCenter)
  147. {
  148. // in region
  149. return TRUE;
  150. }
  151. else
  152. {
  153. // not in region
  154. return FALSE;
  155. }
  156. }
  157. else
  158. {
  159. // rectangle
  160. // use CRect method
  161. return GetDomainRect().PtInRect(a_poiCheck);
  162. }
  163. }
  164. // check if the given domain is in domain
  165. BOOL CDomain::DomainInDomain(const CDomain& a_oDomain)
  166. {
  167. // return FALSE if any domain of the two is invalid
  168. if (IsInvalid() || a_oDomain.IsInvalid())
  169. {
  170. return FALSE;
  171. }
  172. // create two CRgn objects with the two domain objects
  173. CRect rectCur, rectGiven;
  174. rectCur = GetDomainRect();
  175. rectGiven = a_oDomain.GetDomainRect();
  176. CRgn rgnCur, rgnGiven, rgnTest;
  177. BOOL bRgnCur, bRgnGiven;
  178. bRgnCur = rgnTest.CreateEllipticRgnIndirect(rectCur);
  179. if (IsRound())
  180. {
  181. bRgnCur = rgnCur.CreateEllipticRgnIndirect(rectCur);
  182. }
  183. else
  184. {
  185. bRgnCur = rgnCur.CreateRectRgnIndirect(rectCur);
  186. }
  187. if (a_oDomain.IsRound())
  188. {
  189. bRgnGiven = rgnGiven.CreateEllipticRgnIndirect(rectGiven);
  190. }
  191. else
  192. {
  193. bRgnGiven = rgnGiven.CreateRectRgnIndirect(rectGiven);
  194. }
  195. // combine the two CRgn objects with RGN_AND
  196. int nCombineResult = rgnTest.CombineRgn(&rgnCur, &rgnGiven, RGN_AND);
  197. // return FALSE if combine failed or combine result is empty
  198. if (nCombineResult == ERROR || nCombineResult == NULLREGION)
  199. {
  200. return FALSE;
  201. }
  202. // the combined region equals the test domain means that the test domain is in the domain, return TRUE
  203. return rgnTest.EqualRgn(&rgnGiven);
  204. }
  205. // cleanup
  206. void CDomain::Cleanup()
  207. {
  208. // need to do nothing at the moment
  209. m_PolygonPoint.clear();
  210. }
  211. // initialization
  212. void CDomain::Init()
  213. {
  214. m_nShape = DOMAIN_SHAPE::INVALID;
  215. m_rectDomain = CRect(0, 0, 0, 0);
  216. m_PolygonPoint.clear();
  217. }
  218. // duplication
  219. void CDomain::Duplicate(const CDomain& a_oSource)
  220. {
  221. // initialization
  222. Init();
  223. // copy data over
  224. m_nShape = a_oSource.m_nShape;
  225. m_rectDomain = a_oSource.m_rectDomain;
  226. m_PolygonPoint = a_oSource.m_PolygonPoint;
  227. }
  228. void CDomain::Serialize(bool isStoring, tinyxml2::XMLDocument* classDoc, tinyxml2::XMLElement* rootNode)
  229. {
  230. xmls::Slo slo;
  231. xmls::xInt xnShape;
  232. xmls::xRect xRecDomain;
  233. xmls::xString xPolygonPoint;
  234. //register at this time point can ensure that the objects aren't nullptr especialy the smart pointer object.
  235. //Slo::Clear();
  236. slo.Register("shape", &xnShape);
  237. slo.Register("rectDomian", &xRecDomain);
  238. slo.Register("PolygonPoint", &xPolygonPoint);
  239. if (isStoring)
  240. {
  241. xnShape = (int)m_nShape;
  242. if (m_nShape == DOMAIN_SHAPE::POLYGON)
  243. {
  244. CString polygonPoints = _T("");
  245. if (m_PolygonPoint.size() > 0)
  246. {
  247. for (size_t i = 0; i < m_PolygonPoint.size(); i++)
  248. {
  249. CString X = _T("");
  250. X.Format(_T("%ld"), m_PolygonPoint[i].x);
  251. CString Y = _T("");
  252. Y.Format(_T("%ld"), m_PolygonPoint[i].y);
  253. //polygonPoints.Format(_T("%ld"), m_PolygonPoint[i].y);
  254. polygonPoints.Append(X);
  255. polygonPoints.Append(",");
  256. polygonPoints.Append(Y);
  257. polygonPoints.Append("_");
  258. }
  259. }
  260. xPolygonPoint = polygonPoints;
  261. }
  262. //we have to save the rectangle parameter according to the previouse rule.(left,top,width,height or centerX centerY,diamiter,0)
  263. xRecDomain = xRect(m_rectDomain, (int)m_nShape);
  264. slo.Serialize(true, classDoc, rootNode);
  265. }
  266. else
  267. {
  268. slo.Serialize(false, classDoc, rootNode);
  269. m_nShape = (DOMAIN_SHAPE)xnShape.value();
  270. m_rectDomain = xRecDomain.value();
  271. if (m_nShape == DOMAIN_SHAPE::POLYGON)
  272. {
  273. std::vector<std::string> point;
  274. SplitString(xPolygonPoint.value(), point, "_");
  275. for (int i = 0; i < point.size(); i++)
  276. {
  277. std::vector<std::string> pointTemp;
  278. SplitString(point[i], pointTemp, ",");
  279. /*m_PolygonPoint[i].x = stoll(pointTemp[0], 0, 0);
  280. m_PolygonPoint[i].y = stoll(pointTemp[1], 0, 0);*/
  281. long x = atoi(pointTemp[0].c_str());
  282. long y = atoi(pointTemp[1].c_str());
  283. m_PolygonPoint.push_back(CPoint(x, y));
  284. }
  285. }
  286. //do the regulation
  287. int nCentreX = m_rectDomain.left;
  288. int nCentreY = m_rectDomain.top;
  289. int nWidth;
  290. int nHeight;
  291. int nDiameter;
  292. if (m_nShape == DOMAIN_SHAPE::ROUND)
  293. {
  294. nDiameter = m_rectDomain.right;
  295. // create domain
  296. m_rectDomain.left = 0;
  297. m_rectDomain.top = 0;
  298. m_rectDomain.right = nDiameter;
  299. m_rectDomain.bottom = nDiameter;
  300. this->OffsetDomain(CPoint(nCentreX - (nDiameter / 2), nCentreY - (nDiameter / 2)));
  301. }
  302. else if (m_nShape == DOMAIN_SHAPE::RECTANGLE)
  303. {
  304. nWidth = m_rectDomain.right;
  305. nHeight = m_rectDomain.bottom;
  306. m_rectDomain.left = 0;
  307. m_rectDomain.top = 0;
  308. m_rectDomain.right = nWidth;
  309. m_rectDomain.bottom = nHeight;
  310. this->OffsetDomain(CPoint(nCentreX - (nWidth / 2), nCentreY - (nHeight / 2)));
  311. }
  312. }
  313. }
  314. }