FieldMgr.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. #include "stdafx.h"
  2. #include "otsdataconst.h"
  3. #include "FieldMgr.h"
  4. #include "../OTSLog/COTSUtilityDllFunExport.h"
  5. namespace OTSIMGPROC {
  6. namespace {
  7. // fill the matrics with the spiral sequence number ,n*n is the largest fill number.
  8. // the row and col number should be odd number.
  9. void getSpiralMatrics(std::vector <std::vector <int>>& arrays,int row,int col)
  10. {
  11. int n = max(col, row);
  12. arrays.resize(n, std::vector<int>(n));
  13. int c = 0, i, j;
  14. int z = n * n;
  15. int ou = z;
  16. while (ou >= 1)
  17. {
  18. i = 0;
  19. j = 0;
  20. for (i += c, j += c; j < n - c; j++)//from left to right
  21. {
  22. if (ou > z) break;
  23. arrays[i][j] = ou--;
  24. }
  25. for (j--, i++; i < n - c; i++) //from top to bottom
  26. {
  27. if (ou > z) break;
  28. arrays[i][j] = ou--;
  29. }
  30. for (i--, j--; j >= c; j--)//from right to left
  31. {
  32. if (ou > z) break;
  33. arrays[i][j] = ou--;
  34. }
  35. for (j++, i--; i >= c + 1; i--)//from bottom to top
  36. {
  37. if (ou > z) break;
  38. arrays[i][j] = ou--;
  39. }
  40. c++;
  41. }
  42. // if col<>row then shift the matrics so that the smallest number is in the center of the row*col's matrics.
  43. if (row > col)
  44. {
  45. int offset = (row - col) / 2;
  46. for (int k = 0; k < col; k++)//move mat to left (row-col)/2 cols.
  47. {
  48. for (int m = 0; m < row; m++)
  49. {
  50. arrays[m][k] = arrays[m][k + offset];
  51. }
  52. }
  53. }
  54. else if (col > row)
  55. {
  56. int offset = (col - row) / 2;
  57. for (int k = 0; k < row; k++)//move mat to up (col-row)/2 cols.
  58. {
  59. for (int m = 0; m < col; m++)
  60. {
  61. arrays[k][m] = arrays[k+offset][m];
  62. }
  63. }
  64. }
  65. }
  66. void getZShapeMatrics(std::vector <std::vector <int>>& arrays, int row, int col)
  67. {
  68. arrays.resize(row, std::vector<int>(col));
  69. for (int i = 0; i < row; i++)
  70. {
  71. for (int j = 0; j < col; j++)
  72. {
  73. arrays[i][j] = col *(row- i) + j+1;
  74. }
  75. }
  76. }
  77. void getUpDownMeanderMatrics(std::vector <std::vector <int>>& arrays, int row, int col)
  78. {
  79. arrays.resize(row, std::vector<int>(col));
  80. for (int i = 0; i <row; i++)
  81. {
  82. for (int j = 0; j < col; j++)
  83. {
  84. if (i % 2 == 0)
  85. {
  86. arrays[i][j] = col * (row-i) + j + 1;
  87. }
  88. else
  89. {
  90. arrays[i][j] = col *(row- i) + (col - j);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. using namespace OTSDATA;
  97. // CFieldMgr
  98. CFieldMgr::CFieldMgr(int scanfieldsize,CSize a_ResolutionSize)
  99. {
  100. m_ScanFieldSize = scanfieldsize;
  101. m_ResolutionSize = a_ResolutionSize;
  102. m_pMeasureArea=nullptr;
  103. }
  104. CFieldMgr::~CFieldMgr()
  105. {
  106. }
  107. // initialization
  108. BOOL CFieldMgr::Init(CDomainPtr a_pMeasureArea, int a_FieldStartMode)
  109. {
  110. // input check
  111. ASSERT(a_pMeasureArea);
  112. // assign class member
  113. m_pMeasureArea = CDomainPtr(new CDomain(a_pMeasureArea.get()));
  114. m_fieldStartMode = a_FieldStartMode;
  115. return TRUE;
  116. }
  117. std::vector<CPoint> CFieldMgr::GetUnmeasuredFieldCentrePoints(std::vector<CPoint> a_listMeasuredFieldCentrePoints,int distributionMode)
  118. {
  119. std::vector<CPoint> allPoints = CalculateFieldCentrePoints(distributionMode);
  120. std::vector<CPoint> unmeasuredPoints;
  121. for(auto p:allPoints)
  122. if (!IsInMeasuredFieldList(p,a_listMeasuredFieldCentrePoints))
  123. {
  124. // add the field centre into the unmeasured field centre points list
  125. unmeasuredPoints.push_back(p);
  126. }
  127. return unmeasuredPoints;
  128. }
  129. std::vector<CPoint> CFieldMgr::GetFieldCentrePoints(int distributionMode)
  130. {
  131. auto m_listFieldCentrePoints = CalculateFieldCentrePoints(distributionMode);
  132. return m_listFieldCentrePoints;
  133. }
  134. int CFieldMgr::GetTotalFields(int distributionMode)
  135. {
  136. auto m_listFieldCentrePoints = CalculateFieldCentrePoints(distributionMode);
  137. return (int)m_listFieldCentrePoints.size();
  138. }
  139. // measure area
  140. void CFieldMgr::SetMeasureArea(CDomainPtr a_pMeasureArea)
  141. {
  142. // input check
  143. ASSERT(a_pMeasureArea);
  144. if (!a_pMeasureArea)
  145. {
  146. LogErrorTrace(__FILE__, __LINE__, _T("SetMeasureArea: invalid measure area poiter."));
  147. return;
  148. }
  149. m_pMeasureArea = CDomainPtr(new CDomain(a_pMeasureArea.get()));
  150. }
  151. int CFieldMgr::GetEffectiveFieldWidth()
  152. {
  153. auto width= m_ScanFieldSize - 2*m_overlap;
  154. return width;
  155. }
  156. int CFieldMgr::GetEffectiveFieldHeight()
  157. {
  158. CSize ImageSizeByPixel = m_ResolutionSize;
  159. // scan field size (x, y)
  160. double pixelx = ImageSizeByPixel.cx;
  161. double pixely = ImageSizeByPixel.cy;
  162. double dScanFiledSizeX = m_ScanFieldSize;
  163. double dScanFiledSizeY = dScanFiledSizeX * pixely / pixelx;
  164. auto height= dScanFiledSizeY - 2*m_overlap;
  165. return height;
  166. }
  167. COTSFieldDataPtr CFieldMgr::FindNeighborField(const COTSFieldDataList a_flds, COTSFieldDataPtr a_centerField, SORTING_DIRECTION a_direction)
  168. {
  169. COTSFieldDataPtr fld;
  170. double pixelsize;
  171. for (auto f : a_flds)
  172. {
  173. SORTING_DIRECTION di;
  174. IsNeighborFieldCentre(f->GetPosition(), a_centerField->GetPosition(), GetEffectiveFieldWidth(), GetEffectiveFieldHeight(), di);
  175. if (di == a_direction)
  176. {
  177. return f;
  178. }
  179. }
  180. return fld;
  181. }
  182. bool CFieldMgr::FindNeighborField(const std::vector<CPoint> a_flds, CPoint a_centerField,CPoint& neighbor, SORTING_DIRECTION a_direction)
  183. {
  184. for (auto f : a_flds)
  185. {
  186. SORTING_DIRECTION di;
  187. IsNeighborFieldCentre(f, a_centerField, GetEffectiveFieldWidth(), GetEffectiveFieldHeight(), di);
  188. if (di == a_direction)
  189. {
  190. neighbor=f;
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. // protected
  197. // calculate field centre points list
  198. std::vector<CPoint> CFieldMgr::CalculateFieldCentrePoints(int distributionMode)
  199. {
  200. // field centre points list
  201. std::vector<CPoint> m_listFieldCentrePoints;
  202. // clean up
  203. m_listFieldCentrePoints.clear();
  204. // the measure domain rectangle
  205. CRect rectMeasureDomain = m_pMeasureArea->GetDomainRect();
  206. // the measure domain centre
  207. CPoint poiDomainCentre = rectMeasureDomain.CenterPoint();
  208. double effectiveWidth = GetEffectiveFieldWidth();
  209. double effectiveHeight = GetEffectiveFieldHeight();
  210. CSize sizeImage;
  211. sizeImage.cx = effectiveWidth;
  212. sizeImage.cy = effectiveHeight;
  213. // start mode
  214. OTS_GET_IMAGE_MODE nStartMode = (OTS_GET_IMAGE_MODE)m_fieldStartMode;
  215. rectMeasureDomain.NormalizeRect();
  216. int height = (double)rectMeasureDomain.Height();
  217. int width = (double)rectMeasureDomain.Width();
  218. // calculate total columns, rows and make sure the domain area be covered
  219. int nTotalCols = ceil((width / effectiveWidth));
  220. int nTotalRows = ceil(height / effectiveHeight);
  221. // calculate column on the left of the centre point
  222. int nLeftCols = nTotalCols / 2;
  223. int nRightCols = nLeftCols;
  224. // fields on top
  225. int nRowsOnTop = nTotalRows / 2;
  226. // sure total columns, rows are odd numbers
  227. nTotalCols = nLeftCols * 2 + 1;
  228. //nTotalRows = nTotalRows * 2 + 1;
  229. nTotalRows = nRowsOnTop * 2 + 1;
  230. // calculate left, right field column position (x only
  231. int nLeftMostColX = poiDomainCentre.x - nLeftCols * (effectiveWidth);
  232. int nUpMostRowY = poiDomainCentre.y - nRowsOnTop * (effectiveHeight);
  233. std::vector <std::vector <CPoint>> pointMatrics(nTotalRows, std::vector<CPoint>(nTotalCols));
  234. for (int i = 0; i < nTotalRows; i++)
  235. {
  236. for (int j = 0; j < nTotalCols; j++)
  237. {
  238. pointMatrics[i][j].x = nLeftMostColX + j * (effectiveWidth);
  239. pointMatrics[i][j].y = nUpMostRowY + i * (effectiveHeight);
  240. }
  241. }
  242. std::vector <std::vector <int>> sequenceMat; //construct an matrics map to the pointMatrics,but the content is the sequence number.
  243. switch (nStartMode)
  244. {
  245. case OTS_GET_IMAGE_MODE::SpiralSequnce:
  246. getSpiralMatrics(sequenceMat, nTotalRows,nTotalCols);
  247. break;
  248. case OTS_GET_IMAGE_MODE::SnakeSequnce :
  249. getUpDownMeanderMatrics(sequenceMat, nTotalRows, nTotalCols);
  250. break;
  251. case OTS_GET_IMAGE_MODE::ZShapeSequnce :
  252. getZShapeMatrics(sequenceMat, nTotalRows, nTotalCols);
  253. case OTS_GET_IMAGE_MODE::RANDOM :
  254. break;
  255. }
  256. std::map <int, CPoint> mapCenterPoint;
  257. for (int i = 0; i < nTotalRows; i++)
  258. {
  259. for (int j = 0; j < nTotalCols; j++)
  260. {
  261. int sequenceNum = sequenceMat[i][j];
  262. CPoint p = pointMatrics[i][j];
  263. mapCenterPoint[sequenceNum] = p;// sorting all the field center point by the sequence number.
  264. }
  265. }
  266. // judge if the measure area is polygon
  267. if (m_pMeasureArea->GetShape() == DOMAIN_SHAPE::POLYGON)
  268. {
  269. std::vector<CPoint> ptPolygon = m_pMeasureArea->GetPolygonPoint();
  270. for (auto itr : mapCenterPoint)
  271. {
  272. CPoint itrPoint = itr.second;
  273. if (IsInPolygonMeasureArea(itrPoint, sizeImage, ptPolygon))
  274. {
  275. m_listFieldCentrePoints.push_back(itr.second);
  276. }
  277. }
  278. }
  279. else
  280. {
  281. for (auto itr : mapCenterPoint)
  282. {
  283. switch (distributionMode)
  284. {
  285. case 0://center in
  286. if (IsCenterInMeasureArea(itr.second, sizeImage))
  287. {
  288. m_listFieldCentrePoints.push_back(itr.second);
  289. }
  290. break;
  291. case 1://partly in , field will cover all the measure area
  292. if (IsPartlyInMeasureArea(itr.second, sizeImage))
  293. {
  294. m_listFieldCentrePoints.push_back(itr.second);
  295. }
  296. break;
  297. case 2://totally in, field is totally in the measure area
  298. if (IsTotallyInMeasureArea(itr.second, sizeImage))
  299. {
  300. m_listFieldCentrePoints.push_back(itr.second);
  301. }
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. }
  308. return m_listFieldCentrePoints;
  309. }
  310. BOOL CFieldMgr::IsThisPointInMeasureArea(CPoint a_position)
  311. {
  312. // judge if the measure area is polygon
  313. if (m_pMeasureArea->GetShape() == DOMAIN_SHAPE::POLYGON)
  314. {
  315. std::vector<CPoint> ptPolygon = m_pMeasureArea->GetPolygonPoint();
  316. if (PtInPolygon(a_position, ptPolygon))
  317. {
  318. // centre in the measure domain area, return TRUE
  319. return TRUE;
  320. }
  321. else
  322. {
  323. return FALSE;
  324. }
  325. }
  326. else
  327. {
  328. if (m_pMeasureArea->PtInDomain(a_position))
  329. {
  330. // centre in the measure domain area, return TRUE
  331. return TRUE;
  332. }
  333. else
  334. {
  335. return FALSE;
  336. }
  337. }
  338. }
  339. // test if field is in or partly in the measure domain area
  340. BOOL CFieldMgr::IsInPolygonMeasureArea(CPoint a_poiField, CSize a_sizeImageSize, std::vector<CPoint> ptPolygon)
  341. {
  342. // check measure area parameter
  343. ASSERT(m_pMeasureArea);
  344. if (!m_pMeasureArea)
  345. {
  346. // shouldn't happen
  347. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  348. return FALSE;
  349. }
  350. // test field centre point first
  351. if (PtInPolygon(a_poiField, ptPolygon))
  352. {
  353. // centre in the measure domain area, return TRUE
  354. return TRUE;
  355. }
  356. // get measure field centre
  357. //CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  358. //// move to left top postion.
  359. //a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  360. //// rectangle of the field
  361. //CRect rectFiled(a_poiField, a_sizeImageSize);
  362. // // on the top left side, need to test the bottom right corner
  363. // if (PtInPolygon(CPoint(rectFiled.right, rectFiled.top), ptPolygon))
  364. // {
  365. // return TRUE;
  366. // }
  367. // // on the bottom left side, need to test the top right corner
  368. // if (PtInPolygon(rectFiled.BottomRight(), ptPolygon))
  369. // {
  370. // return TRUE;
  371. // }
  372. // // on the top left side, need to test the bottom right corner
  373. // if (PtInPolygon(rectFiled.TopLeft(), ptPolygon))
  374. // {
  375. // return TRUE;
  376. // }
  377. // // on the bottom left side, need to test the top right corner
  378. // if (PtInPolygon(CPoint(rectFiled.left, rectFiled.bottom), ptPolygon))
  379. // {
  380. // return TRUE;
  381. // }
  382. // this field is not in the area at all, return FALSE.
  383. return FALSE;
  384. }
  385. //function:judge whether the point p is in the polygon ptPolygon
  386. //p is the particular point, ptPolygon is the collection of the vertex point
  387. BOOL CFieldMgr::PtInPolygon(CPoint p, std::vector<CPoint> ptPolygon)
  388. {
  389. int nCount = ptPolygon.size();
  390. // the polygon must have at least 3 points
  391. int nCross = 0;
  392. for (int i = 0; i < nCount; i++)
  393. {
  394. CPoint p1 = ptPolygon[i];
  395. CPoint p2 = ptPolygon[(i + 1) % nCount];// the line between the point p1 and p2
  396. if (p1.y == p2.y)
  397. continue;
  398. if (p.y < min(p1.y, p2.y))
  399. continue;
  400. if (p.y >= max(p1.y, p2.y))
  401. continue;
  402. // get the cross point (from the two point equation)
  403. double x = (double)(p.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
  404. //only count the cross point which is on the right side of the point p
  405. if (x > p.x)
  406. {
  407. nCross++;
  408. }
  409. }
  410. // the cross point number is even then the point is outside the polygon
  411. //the cross point number is odd then the point is inside the polygon
  412. if ((nCross % 2) == 1)
  413. {
  414. //true;
  415. return TRUE;
  416. }
  417. else
  418. {
  419. //false;
  420. return FALSE;
  421. }
  422. }
  423. // test if field is in or partly in the measure domain area
  424. BOOL CFieldMgr::IsCenterInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
  425. {
  426. // check measure area parameter
  427. ASSERT(m_pMeasureArea);
  428. if (!m_pMeasureArea)
  429. {
  430. // shouldn't happen
  431. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  432. return FALSE;
  433. }
  434. // test field centre point first
  435. if (m_pMeasureArea->PtInDomain(a_poiField))
  436. {
  437. // centre in the measure domain area, return TRUE
  438. return TRUE;
  439. }
  440. // this field is not in the area , return FALSE.
  441. return FALSE;
  442. }
  443. // test if field is in or partly in the measure domain area
  444. BOOL CFieldMgr::IsPartlyInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
  445. {
  446. // check measure area parameter
  447. ASSERT(m_pMeasureArea);
  448. if (!m_pMeasureArea)
  449. {
  450. // shouldn't happen
  451. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  452. return FALSE;
  453. }
  454. // test field centre point first
  455. if (m_pMeasureArea->PtInDomain(a_poiField))
  456. {
  457. // centre in the measure domain area, return TRUE
  458. return TRUE;
  459. }
  460. // get measure field centre
  461. CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  462. // move to left top postion.
  463. a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  464. // rectangle of the field
  465. CRect rectFiled(a_poiField, a_sizeImageSize);
  466. if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.right, rectFiled.top)))
  467. {
  468. return true;
  469. }
  470. if (m_pMeasureArea->PtInDomain(rectFiled.BottomRight()))
  471. {
  472. return true;
  473. }
  474. if (m_pMeasureArea->PtInDomain(rectFiled.TopLeft()))
  475. {
  476. return true;
  477. }
  478. if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.left, rectFiled.bottom)))
  479. {
  480. return true;
  481. }
  482. // check field position
  483. // this field is not in the area , return FALSE.
  484. return FALSE;
  485. }
  486. BOOL CFieldMgr::IsTotallyInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
  487. {
  488. // check measure area parameter
  489. ASSERT(m_pMeasureArea);
  490. if (!m_pMeasureArea)
  491. {
  492. // shouldn't happen
  493. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  494. return FALSE;
  495. }
  496. // get measure field centre
  497. CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  498. // move to left top postion.
  499. a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  500. // rectangle of the field
  501. CRect rectFiled(a_poiField, a_sizeImageSize);
  502. if (!m_pMeasureArea->PtInDomain(CPoint(rectFiled.right, rectFiled.top)))
  503. {
  504. return false;
  505. }
  506. if (!m_pMeasureArea->PtInDomain(rectFiled.BottomRight()))
  507. {
  508. return false;
  509. }
  510. if (!m_pMeasureArea->PtInDomain(rectFiled.TopLeft()))
  511. {
  512. return false;
  513. }
  514. if (!m_pMeasureArea->PtInDomain(CPoint(rectFiled.left, rectFiled.bottom)))
  515. {
  516. return false;
  517. }
  518. return true;
  519. }
  520. // test if field is in the measured field centre points list
  521. BOOL CFieldMgr::IsInMeasuredFieldList(CPoint a_poiField, std::vector<CPoint> m_listHaveMeasuredFieldCentrePoints)
  522. {
  523. for (CPoint pnt : m_listHaveMeasuredFieldCentrePoints)
  524. {
  525. double scanHeight = (double)m_ScanFieldSize * ((double)m_ResolutionSize.cy / (double)m_ResolutionSize.cx);
  526. CPoint leftTop = CPoint(pnt.x - m_ScanFieldSize / 2, pnt.y + scanHeight / 2);
  527. CPoint rightBottom = CPoint(pnt.x + m_ScanFieldSize / 2, pnt.y - scanHeight / 2);
  528. COTSRect rec = COTSRect(leftTop, rightBottom);
  529. if (rec.PointInRect(a_poiField))
  530. {
  531. return true;
  532. }
  533. }
  534. // ok, return FALSE
  535. return FALSE;
  536. }
  537. // find the next field centre
  538. BOOL CFieldMgr::FindNeighborFieldCentre(const std::vector<CPoint>& a_listFieldCentres,
  539. double a_dScanFieldSizeX,
  540. double a_dScanFieldSizeY,
  541. CPoint a_poiCurrent,
  542. SORTING_DIRECTION& a_nDirection,
  543. CPoint& a_poiNeighbor)
  544. {
  545. // assume no neighbor
  546. BOOL bFind = FALSE;
  547. // go through the field centres list
  548. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  549. {
  550. // test if this is a neighbor field centre
  551. SORTING_DIRECTION nDirection;
  552. if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
  553. {
  554. // we find a neighbor field centre
  555. // let see if this is neighbor we are looking for
  556. switch (a_nDirection)
  557. {
  558. // last move is left
  559. case SORTING_DIRECTION::LEFT:
  560. {
  561. // we are looking for DOWN neighbor
  562. if (nDirection == SORTING_DIRECTION::DOWN)
  563. {
  564. // we find a neighbor below, get out
  565. a_poiNeighbor = poiFieldCentre;
  566. a_nDirection = SORTING_DIRECTION::DOWN;
  567. return TRUE;
  568. }
  569. }
  570. break;
  571. // last move is down
  572. case SORTING_DIRECTION::DOWN:
  573. {
  574. // we are looking for RIGHT neighbor
  575. if (nDirection == SORTING_DIRECTION::RIGHT)
  576. {
  577. // we find a neighbor on the right, get out
  578. a_poiNeighbor = poiFieldCentre;
  579. a_nDirection = SORTING_DIRECTION::RIGHT;
  580. return TRUE;
  581. }
  582. }
  583. break;
  584. // last move is right
  585. case SORTING_DIRECTION::RIGHT:
  586. {
  587. // we are looking for UP neighbor
  588. if (nDirection == SORTING_DIRECTION::UP)
  589. {
  590. // we find a neighbor above
  591. a_poiNeighbor = poiFieldCentre;
  592. a_nDirection = SORTING_DIRECTION::UP;
  593. return TRUE;
  594. }
  595. }
  596. break;
  597. // last move is up
  598. case SORTING_DIRECTION::UP:
  599. {
  600. // we are looking for LEFT neighbor
  601. if (nDirection == SORTING_DIRECTION::LEFT)
  602. {
  603. // we find a neighbor on the left, get out
  604. a_poiNeighbor = poiFieldCentre;
  605. a_nDirection = SORTING_DIRECTION::LEFT;
  606. return TRUE;
  607. }
  608. }
  609. break;
  610. }
  611. }
  612. }
  613. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  614. {
  615. // test if this is a neighbor field centre
  616. SORTING_DIRECTION nDirection;
  617. if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
  618. {
  619. // we find a neighbor field centre
  620. // let see if this is neighbor we are looking for
  621. switch (a_nDirection)
  622. {
  623. // last move is left
  624. case SORTING_DIRECTION::LEFT:
  625. {
  626. // we are looking for DOWN neighbor , but not found
  627. // or LEFT neighbor otherwise
  628. if (nDirection == SORTING_DIRECTION::LEFT)
  629. {
  630. // we find a neighbor on the left, continue looking
  631. a_poiNeighbor = poiFieldCentre;
  632. return TRUE;
  633. }
  634. }
  635. break;
  636. // last move is down
  637. case SORTING_DIRECTION::DOWN:
  638. {
  639. // we are looking for RIGHT neighbor, but not found
  640. // or DOWN neighbor otherwise
  641. if (nDirection == SORTING_DIRECTION::DOWN)
  642. {
  643. // we find a neighbor below, continue looking
  644. a_poiNeighbor = poiFieldCentre;
  645. return TRUE;
  646. }
  647. }
  648. break;
  649. // last move is right
  650. case SORTING_DIRECTION::RIGHT:
  651. {
  652. // we are looking for UP neighbor, but not found
  653. // or RIGHT neighbor, otherwise
  654. if (nDirection == SORTING_DIRECTION::RIGHT)
  655. {
  656. // we find a neighbor on the right, continue looking
  657. a_poiNeighbor = poiFieldCentre;
  658. return TRUE;
  659. }
  660. }
  661. break;
  662. // last move is up
  663. case SORTING_DIRECTION::UP:
  664. {
  665. // we are looking for LEFT neighbor, but not found
  666. // or UP neighbor, otherwise
  667. if (nDirection == SORTING_DIRECTION::UP)
  668. {
  669. // we find a neighbor above, continue looking
  670. a_poiNeighbor = poiFieldCentre;
  671. return TRUE;
  672. }
  673. }
  674. break;
  675. }
  676. }
  677. }
  678. // return find result
  679. return bFind;
  680. }
  681. // find field centre closest to measure domain point
  682. BOOL CFieldMgr::FindFieldCentreClosestMeasureDomainCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint a_poiMeasureDomain, CPoint& a_poi)
  683. {
  684. // distance ratio
  685. int nDisRadio = -1;
  686. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  687. {
  688. // calculate current field centre distance ratio
  689. int nCurFiledDisRadio = (poiFieldCentre.x - a_poiMeasureDomain.x)*(poiFieldCentre.x - a_poiMeasureDomain.x) + (poiFieldCentre.y - a_poiMeasureDomain.y)*(poiFieldCentre.y - a_poiMeasureDomain.y);
  690. // pick one which more closer to centre
  691. if (nDisRadio > nCurFiledDisRadio || nDisRadio == -1)
  692. {
  693. a_poi = poiFieldCentre;
  694. nDisRadio = nCurFiledDisRadio;
  695. }
  696. }
  697. // nDisRadio != -1 means there still field centre in the a_listFieldCentres
  698. return nDisRadio != -1;
  699. }
  700. // find right far side field centre
  701. void CFieldMgr::FindRightMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  702. {
  703. for (auto& poi : a_listFieldCentres)
  704. {
  705. if (poi.y == a_poi.y && poi.x > a_poi.x)
  706. {
  707. a_poi = poi;
  708. }
  709. }
  710. }
  711. // find left far side field centre
  712. void CFieldMgr::FindLeftMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  713. {
  714. for (auto& poi : a_listFieldCentres)
  715. {
  716. if (poi.y == a_poi.y && poi.x < a_poi.x)
  717. {
  718. a_poi = poi;
  719. }
  720. }
  721. }
  722. // find top far side field centre
  723. void CFieldMgr::FindHeighestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  724. {
  725. for (auto& poi : a_listFieldCentres)
  726. {
  727. if (poi.x == a_poi.x && poi.y > a_poi.y)
  728. {
  729. a_poi = poi;
  730. }
  731. }
  732. }
  733. // find bottom far side field centre
  734. void CFieldMgr::FindLowestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  735. {
  736. for (auto& poi : a_listFieldCentres)
  737. {
  738. if (poi.x == a_poi.x && poi.y < a_poi.y)
  739. {
  740. a_poi = poi;
  741. }
  742. }
  743. }
  744. // check if this is a neighbor field centre
  745. BOOL CFieldMgr::IsNeighborFieldCentre(CPoint a_poiFieldCentre,
  746. CPoint a_poiCurrent,
  747. double a_dScanFieldSizeX,
  748. double a_dScanFieldSizeY,
  749. SORTING_DIRECTION& a_nDirection)
  750. {
  751. // x position of the tow field centres are the same, y positions have one field difference
  752. if (a_poiFieldCentre.x == a_poiCurrent.x && abs(a_poiFieldCentre.y - a_poiCurrent.y) == long(a_dScanFieldSizeY))
  753. {
  754. // test is above or below
  755. if (a_poiCurrent.y > a_poiFieldCentre.y)
  756. {
  757. // below
  758. a_nDirection = SORTING_DIRECTION::DOWN;
  759. }
  760. else
  761. {
  762. // above
  763. a_nDirection = SORTING_DIRECTION::UP;
  764. }
  765. // this is a neighbor field centre, return TRUE
  766. return TRUE;
  767. }
  768. // y position of the tow field centres are the same, x positions have one field difference
  769. else if (a_poiFieldCentre.y == a_poiCurrent.y && abs(a_poiFieldCentre.x - a_poiCurrent.x) == long(a_dScanFieldSizeX))
  770. {
  771. // test is on left or right
  772. if (a_poiCurrent.x > a_poiFieldCentre.x)
  773. {
  774. // on the left
  775. a_nDirection = SORTING_DIRECTION::LEFT;
  776. }
  777. else
  778. {
  779. // on the right
  780. a_nDirection = SORTING_DIRECTION::RIGHT;
  781. }
  782. // this is a neighbor field centre, return TRUE
  783. return TRUE;
  784. }
  785. // this is not a neighbor field centre, return FALSE
  786. return FALSE;
  787. }
  788. }