Domain.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. // check if have common area with the given domain
  81. BOOL CDomain::IntersectDomain(const CDomain& a_oDomain)
  82. {
  83. // return FALSE if any domain of the two is invalid
  84. if (IsInvalid() || a_oDomain.IsInvalid())
  85. {
  86. return FALSE;
  87. }
  88. // create two CRgn objects with the two region objects
  89. CRect rectCur, rectGiven;
  90. rectCur = GetDomainRect();
  91. rectGiven = a_oDomain.GetDomainRect();
  92. CRgn rgnCur, rgnGiven, rgnTest;
  93. BOOL bRgnCur, bRgnGiven;
  94. bRgnCur = rgnTest.CreateEllipticRgnIndirect(rectCur);
  95. if (IsRound())
  96. {
  97. bRgnCur = rgnCur.CreateEllipticRgnIndirect(rectCur);
  98. }
  99. else
  100. {
  101. bRgnCur = rgnCur.CreateRectRgnIndirect(rectCur);
  102. }
  103. if (a_oDomain.IsRound())
  104. {
  105. bRgnGiven = rgnGiven.CreateEllipticRgnIndirect(rectGiven);
  106. }
  107. else
  108. {
  109. rgnGiven.CreateRectRgnIndirect(rectGiven);
  110. }
  111. // combine the two CRgn objects with RGN_AND
  112. int nCombineResult = rgnTest.CombineRgn(&rgnCur, &rgnGiven, RGN_AND);
  113. // return TRUE if combine success and combine result is not empty
  114. return (nCombineResult != ERROR) && (nCombineResult != NULLREGION);
  115. }
  116. // check if the point is in domain
  117. BOOL CDomain::PtInDomain(const CPoint& a_poiCheck) const
  118. {
  119. // return FALSE if is invalid
  120. if (IsInvalid())
  121. {
  122. return FALSE;
  123. }
  124. // check sharp
  125. if (IsRound())
  126. {
  127. // round
  128. double dRoundRegionRadius = (double)GetDomainRect().Width() / 2000.0;
  129. dRoundRegionRadius = dRoundRegionRadius * dRoundRegionRadius;
  130. double dDisToRegioCenter;
  131. double dX, dY;
  132. dX = (double)abs(GetDomainCenter().x - a_poiCheck.x) / 1000;
  133. dY = (double)abs(GetDomainCenter().y - a_poiCheck.y) / 1000;
  134. dX = dX * dX;
  135. dY = dY * dY;
  136. dDisToRegioCenter = dX + dY;
  137. if (dRoundRegionRadius >= dDisToRegioCenter)
  138. {
  139. // in region
  140. return TRUE;
  141. }
  142. else
  143. {
  144. // not in region
  145. return FALSE;
  146. }
  147. }
  148. else
  149. {
  150. // rectangle
  151. // use CRect method
  152. return GetDomainRect().PtInRect(a_poiCheck);
  153. }
  154. }
  155. // check if the given domain is in domain
  156. BOOL CDomain::DomainInDomain(const CDomain& a_oDomain)
  157. {
  158. // return FALSE if any domain of the two is invalid
  159. if (IsInvalid() || a_oDomain.IsInvalid())
  160. {
  161. return FALSE;
  162. }
  163. // create two CRgn objects with the two domain objects
  164. CRect rectCur, rectGiven;
  165. rectCur = GetDomainRect();
  166. rectGiven = a_oDomain.GetDomainRect();
  167. CRgn rgnCur, rgnGiven, rgnTest;
  168. BOOL bRgnCur, bRgnGiven;
  169. bRgnCur = rgnTest.CreateEllipticRgnIndirect(rectCur);
  170. if (IsRound())
  171. {
  172. bRgnCur = rgnCur.CreateEllipticRgnIndirect(rectCur);
  173. }
  174. else
  175. {
  176. bRgnCur = rgnCur.CreateRectRgnIndirect(rectCur);
  177. }
  178. if (a_oDomain.IsRound())
  179. {
  180. bRgnGiven = rgnGiven.CreateEllipticRgnIndirect(rectGiven);
  181. }
  182. else
  183. {
  184. bRgnGiven = rgnGiven.CreateRectRgnIndirect(rectGiven);
  185. }
  186. // combine the two CRgn objects with RGN_AND
  187. int nCombineResult = rgnTest.CombineRgn(&rgnCur, &rgnGiven, RGN_AND);
  188. // return FALSE if combine failed or combine result is empty
  189. if (nCombineResult == ERROR || nCombineResult == NULLREGION)
  190. {
  191. return FALSE;
  192. }
  193. // the combined region equals the test domain means that the test domain is in the domain, return TRUE
  194. return rgnTest.EqualRgn(&rgnGiven);
  195. }
  196. void CDomain::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  197. {
  198. xmls::Slo slo;
  199. xmls::xInt xnShape;
  200. xmls::xRect xRecDomain;
  201. xmls::xString xPolygonPoint;
  202. //register at this time point can ensure that the objects aren't nullptr especialy the smart pointer object.
  203. //Slo::Clear();
  204. slo.Register("shape", &xnShape);
  205. slo.Register("rectDomian", &xRecDomain);
  206. slo.Register("PolygonPoint", &xPolygonPoint);
  207. if (isStoring)
  208. {
  209. xnShape = (int)m_nShape;
  210. if (m_nShape == DOMAIN_SHAPE::POLYGON)
  211. {
  212. CString polygonPoints = _T("");
  213. if (m_PolygonPoint.size() > 0)
  214. {
  215. for (size_t i = 0; i < m_PolygonPoint.size(); i++)
  216. {
  217. CString X = _T("");
  218. X.Format(_T("%ld"), m_PolygonPoint[i].x);
  219. CString Y = _T("");
  220. Y.Format(_T("%ld"), m_PolygonPoint[i].y);
  221. //polygonPoints.Format(_T("%ld"), m_PolygonPoint[i].y);
  222. polygonPoints.Append(X);
  223. polygonPoints.Append(",");
  224. polygonPoints.Append(Y);
  225. polygonPoints.Append("_");
  226. }
  227. }
  228. xPolygonPoint = polygonPoints;
  229. }
  230. //we have to save the rectangle parameter according to the previouse rule.(left,top,width,height or centerX centerY,diamiter,0)
  231. xRecDomain = xRect(m_rectDomain, (int)m_nShape);
  232. slo.Serialize(true, classDoc, rootNode);
  233. }
  234. else
  235. {
  236. slo.Serialize(false, classDoc, rootNode);
  237. m_nShape = (DOMAIN_SHAPE)xnShape.value();
  238. m_rectDomain = xRecDomain.value();
  239. if (m_nShape == DOMAIN_SHAPE::POLYGON)
  240. {
  241. std::vector<std::string> point;
  242. SplitString(xPolygonPoint.value(), point, "_");
  243. for (int i = 0; i < point.size(); i++)
  244. {
  245. std::vector<std::string> pointTemp;
  246. SplitString(point[i], pointTemp, ",");
  247. /*m_PolygonPoint[i].x = stoll(pointTemp[0], 0, 0);
  248. m_PolygonPoint[i].y = stoll(pointTemp[1], 0, 0);*/
  249. long x = atoi(pointTemp[0].c_str());
  250. long y = atoi(pointTemp[1].c_str());
  251. m_PolygonPoint.push_back(CPoint(x,y));
  252. }
  253. }
  254. //do the regulation
  255. int nCentreX = m_rectDomain.left;
  256. int nCentreY = m_rectDomain.top;
  257. int nWidth;
  258. int nHeight;
  259. int nDiameter;
  260. if (m_nShape == DOMAIN_SHAPE::ROUND)
  261. {
  262. nDiameter = m_rectDomain.right;
  263. // create domain
  264. m_rectDomain.left = 0;
  265. m_rectDomain.top = 0;
  266. m_rectDomain.right = nDiameter;
  267. m_rectDomain.bottom = nDiameter;
  268. this->OffsetDomain(CPoint(nCentreX - (nDiameter / 2), nCentreY - (nDiameter / 2)));
  269. }
  270. else if (m_nShape == DOMAIN_SHAPE::RECTANGLE)
  271. {
  272. nWidth = m_rectDomain.right;
  273. nHeight = m_rectDomain.bottom;
  274. m_rectDomain.left = 0;
  275. m_rectDomain.top = 0;
  276. m_rectDomain.right = nWidth;
  277. m_rectDomain.bottom = nHeight;
  278. this->OffsetDomain(CPoint(nCentreX - (nWidth / 2), nCentreY - (nHeight / 2)));
  279. }
  280. }
  281. }
  282. // protected
  283. // cleanup
  284. void CDomain::Cleanup()
  285. {
  286. // need to do nothing at the moment
  287. }
  288. // initialization
  289. void CDomain::Init()
  290. {
  291. m_nShape = DOMAIN_SHAPE::INVALID;
  292. m_rectDomain = CRect(0, 0, 0, 0);
  293. }
  294. // duplication
  295. void CDomain::Duplicate(const CDomain& a_oSource)
  296. {
  297. // initialization
  298. Init();
  299. // copy data over
  300. m_nShape = a_oSource.m_nShape;
  301. m_rectDomain = a_oSource.m_rectDomain;
  302. m_PolygonPoint = a_oSource.m_PolygonPoint;
  303. }
  304. }