OTSFieldData.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "stdafx.h"
  2. #include "OTSFieldData.h"
  3. namespace OTSDATA {
  4. // COTSParticle
  5. // constructor
  6. COTSFieldData::COTSFieldData() // constructor
  7. {
  8. Init();
  9. }
  10. COTSFieldData::COTSFieldData(const COTSFieldData& a_oSource) // copy constructor
  11. {
  12. // can't copy itself
  13. if (&a_oSource == this)
  14. {
  15. return;
  16. }
  17. // copy data over
  18. Duplicate(a_oSource);
  19. }
  20. COTSFieldData::COTSFieldData(COTSFieldData* a_poSource) // copy constructor
  21. {
  22. // can't copy itself
  23. if (a_poSource == this)
  24. {
  25. return;
  26. }
  27. // copy data over
  28. Duplicate(*a_poSource);
  29. }
  30. COTSFieldData& COTSFieldData::operator=(const COTSFieldData& a_oSource) // =operator
  31. {
  32. // cleanup
  33. Cleanup();
  34. // copy the class data over
  35. Duplicate(a_oSource);
  36. // return class
  37. return *this;
  38. }
  39. // ==operator
  40. BOOL COTSFieldData::operator==(const COTSFieldData& a_oSource)
  41. {
  42. int nSize = (int)m_listParticles.size();
  43. if (nSize != (int)a_oSource.m_listParticles.size())
  44. {
  45. return FALSE;
  46. }
  47. for (int i = 0; i < (int)nSize; ++i)
  48. {
  49. if (!(*(m_listParticles[i].get()) == *(a_oSource.m_listParticles[i].get())))
  50. {
  51. return FALSE;
  52. }
  53. }
  54. return m_nID == a_oSource.m_nID &&
  55. m_poiPos == a_oSource.m_poiPos ;
  56. }
  57. COTSFieldData::~COTSFieldData() // destructor
  58. {
  59. Cleanup();
  60. }
  61. void COTSFieldData::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  62. {
  63. xmls::xInt xnID;
  64. xmls::xPoint xpoiPos;
  65. xmls::xString xstrFieldFileFolder;
  66. xmls::Collection<COTSParticle> parts;
  67. xmls::Slo slo;
  68. slo.Register("ID", &xnID);
  69. slo.Register("poiPos", &xpoiPos);
  70. slo.Register("FieldFileFolder", &xstrFieldFileFolder);
  71. slo.Register("Particles", &parts);
  72. if (isStoring)
  73. {
  74. xnID = m_nID;
  75. xpoiPos = m_poiPos;
  76. parts.Clear();
  77. for (auto pParticle : m_listParticles)
  78. {
  79. parts.addItem(pParticle.get());
  80. }
  81. slo.Serialize(true, classDoc, rootNode);
  82. }
  83. else
  84. {
  85. slo.Serialize(false, classDoc, rootNode);
  86. m_nID = xnID.value();
  87. m_poiPos = xpoiPos.value();
  88. m_listParticles.clear();
  89. for (unsigned int i=0; i < parts.size(); i++)
  90. {
  91. m_listParticles.push_back(COTSParticlePtr(parts.getItem(i)));
  92. }
  93. }
  94. }
  95. void COTSFieldData::SetParticleList(COTSParticleList& a_listParticles, BOOL a_bClear)
  96. {
  97. // clear holes list if necessary
  98. if (a_bClear)
  99. {
  100. m_listParticles.clear();
  101. }
  102. // copy the list
  103. for (auto pParticle : a_listParticles)
  104. {
  105. m_listParticles.push_back(pParticle);
  106. }
  107. }
  108. void COTSFieldData::SetBigParticleList(COTSParticleList& a_listParticles, BOOL a_bClear)
  109. {
  110. // clear holes list if necessary
  111. if (a_bClear)
  112. {
  113. m_listBigParticles.clear();
  114. }
  115. // copy the list
  116. for (auto pParticle : a_listParticles)
  117. {
  118. m_listBigParticles.push_back(pParticle);
  119. }
  120. }
  121. COTSParticleList COTSFieldData::GetTopBorderedBigParticles()
  122. {
  123. COTSParticleList parts;
  124. for (auto p : m_listBigParticles)
  125. {
  126. auto segs = p->GetFeature()->GetSegmentsList();
  127. for (auto seg : segs)
  128. {
  129. if (seg->GetHeight() == 0)
  130. {
  131. parts.push_back(p);
  132. break;
  133. }
  134. }
  135. }
  136. return parts;
  137. }
  138. COTSParticleList COTSFieldData::GetBottomBorderedBigParticles()
  139. {
  140. COTSParticleList parts;
  141. for (auto p : m_listBigParticles)
  142. {
  143. auto segs = p->GetFeature()->GetSegmentsList();
  144. for (auto seg : segs)
  145. {
  146. if (seg->GetHeight() == this->Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  147. {
  148. parts.push_back(p);
  149. break;
  150. }
  151. }
  152. }
  153. return parts;
  154. }
  155. COTSParticleList COTSFieldData::GetLeftBorderedBigParticles()
  156. {
  157. COTSParticleList parts;
  158. for (auto p : m_listBigParticles)
  159. {
  160. auto segs = p->GetFeature()->GetSegmentsList();
  161. for (auto seg : segs)
  162. {
  163. if (seg->GetStart() == 0)
  164. {
  165. parts.push_back(p);
  166. break;
  167. }
  168. }
  169. }
  170. return parts;
  171. }
  172. COTSParticleList COTSFieldData::GetRightBorderedBigParticles()
  173. {
  174. COTSParticleList parts;
  175. for (auto p : m_listBigParticles)
  176. {
  177. auto segs = p->GetFeature()->GetSegmentsList();
  178. for (auto seg : segs)
  179. {
  180. if (seg->GetStart() + seg->GetLength() == this->Width)
  181. {
  182. parts.push_back(p);
  183. break;
  184. }
  185. }
  186. }
  187. return parts;
  188. }
  189. // cleanup
  190. void COTSFieldData::Cleanup()
  191. {
  192. m_listParticles.clear();
  193. }
  194. // initialization
  195. void COTSFieldData::Init()
  196. {
  197. // initialization
  198. m_nID = -1;
  199. m_poiPos = CPoint(0, 0);
  200. m_listParticles.clear();
  201. }
  202. // duplication
  203. void COTSFieldData::Duplicate(const COTSFieldData& a_oSource)
  204. {
  205. // initialization
  206. Init();
  207. m_nID = a_oSource.m_nID;
  208. m_poiPos = a_oSource.m_poiPos;
  209. // copy data over
  210. for (auto pParticle : a_oSource.m_listParticles)
  211. {
  212. COTSParticlePtr pParticleNew = COTSParticlePtr(new COTSParticle(*pParticle.get()));
  213. m_listParticles.push_back(pParticleNew);
  214. }
  215. }
  216. }