FieldMgr.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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++)//从左到右
  21. {
  22. if (ou > z) break;
  23. arrays[i][j] = ou--;
  24. }
  25. for (j--, i++; i < n - c; i++) // 从上到下
  26. {
  27. if (ou > z) break;
  28. arrays[i][j] = ou--;
  29. }
  30. for (i--, j--; j >= c; j--)//从右到左
  31. {
  32. if (ou > z) break;
  33. arrays[i][j] = ou--;
  34. }
  35. for (j++, i--; i >= c + 1; i--)//从下到上
  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)
  118. {
  119. std::vector<CPoint> allPoints = CalculateFieldCentrePoints();
  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()
  130. {
  131. auto m_listFieldCentrePoints = CalculateFieldCentrePoints();
  132. return m_listFieldCentrePoints;
  133. }
  134. int CFieldMgr::GetTotalFields()
  135. {
  136. auto m_listFieldCentrePoints = CalculateFieldCentrePoints();
  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()
  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. // calculate total columns, rows and make sure the domain area be covered
  216. int nTotalCols = (int)(ceil((double)rectMeasureDomain.Width() / effectiveWidth));
  217. int nTotalRows = (int)(ceil((double)rectMeasureDomain.Height() / effectiveHeight));
  218. // calculate column on the left of the centre point
  219. int nLeftCols = nTotalCols / 2;
  220. int nRightCols = nLeftCols;
  221. // fields on top
  222. int nRowsOnTop = nTotalRows / 2;
  223. // sure total columns, rows are odd numbers
  224. nTotalCols = nLeftCols * 2 + 1;
  225. //nTotalRows = nTotalRows * 2 + 1;
  226. nTotalRows = nRowsOnTop * 2 + 1;
  227. // calculate left, right field column position (x only
  228. int nLeftMostColX = poiDomainCentre.x - nLeftCols * (effectiveWidth);
  229. int nUpMostRowY = poiDomainCentre.y - nRowsOnTop * (effectiveHeight);
  230. std::vector <std::vector <CPoint>> pointMatrics(nTotalRows, std::vector<CPoint>(nTotalCols));
  231. for (int i = 0; i < nTotalRows; i++)
  232. {
  233. for (int j = 0; j < nTotalCols; j++)
  234. {
  235. pointMatrics[i][j].x = nLeftMostColX + j * (effectiveWidth);
  236. pointMatrics[i][j].y = nUpMostRowY + i * (effectiveHeight);
  237. }
  238. }
  239. std::vector <std::vector <int>> sequenceMat; //construct an matrics map to the pointMatrics,but the content is the sequence number.
  240. switch (nStartMode)
  241. {
  242. case OTS_GET_IMAGE_MODE::SpiralSequnce:
  243. getSpiralMatrics(sequenceMat, nTotalRows,nTotalCols);
  244. break;
  245. case OTS_GET_IMAGE_MODE::SnakeSequnce :
  246. getUpDownMeanderMatrics(sequenceMat, nTotalRows, nTotalCols);
  247. break;
  248. case OTS_GET_IMAGE_MODE::ZShapeSequnce :
  249. getZShapeMatrics(sequenceMat, nTotalRows, nTotalCols);
  250. case OTS_GET_IMAGE_MODE::RANDOM :
  251. break;
  252. }
  253. std::map <int, CPoint> mapCenterPoint;
  254. for (int i = 0; i < nTotalRows; i++)
  255. {
  256. for (int j = 0; j < nTotalCols; j++)
  257. {
  258. int sequenceNum = sequenceMat[i][j];
  259. CPoint p = pointMatrics[i][j];
  260. mapCenterPoint[sequenceNum] = p;// sorting all the field center point by the sequence number.
  261. }
  262. }
  263. // 判断当前样品获取帧图信息的测量区域为多边形
  264. if ((int)m_pMeasureArea->GetShape() > 1)
  265. {
  266. std::vector<CPoint> ptPolygon = m_pMeasureArea->GetPolygonPoint();
  267. for (auto itr : mapCenterPoint)
  268. {
  269. CPoint itrPoint = itr.second;
  270. if (IsInPolygonMeasureArea(itrPoint, sizeImage, ptPolygon))
  271. {
  272. m_listFieldCentrePoints.push_back(itr.second);
  273. }
  274. }
  275. }
  276. else
  277. {
  278. for (auto itr : mapCenterPoint)
  279. {
  280. if (IsInMeasureArea(itr.second, sizeImage))
  281. {
  282. m_listFieldCentrePoints.push_back(itr.second);
  283. }
  284. }
  285. }
  286. return m_listFieldCentrePoints;
  287. }
  288. // test if field is in or partly in the measure domain area
  289. BOOL CFieldMgr::IsInPolygonMeasureArea(CPoint a_poiField, CSize a_sizeImageSize, std::vector<CPoint> ptPolygon)
  290. {
  291. // check measure area parameter
  292. ASSERT(m_pMeasureArea);
  293. if (!m_pMeasureArea)
  294. {
  295. // shouldn't happen
  296. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  297. return FALSE;
  298. }
  299. // test field centre point first
  300. if (PtInPolygon(a_poiField, ptPolygon))
  301. {
  302. // centre in the measure domain area, return TRUE
  303. return TRUE;
  304. }
  305. // get measure field centre
  306. CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  307. // move to left top postion.
  308. a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  309. // rectangle of the field
  310. CRect rectFiled(a_poiField, a_sizeImageSize);
  311. // // on the top left side, need to test the bottom right corner
  312. if (PtInPolygon(CPoint(rectFiled.right, rectFiled.top), ptPolygon))
  313. {
  314. return TRUE;
  315. }
  316. // // on the bottom left side, need to test the top right corner
  317. if (PtInPolygon(rectFiled.BottomRight(), ptPolygon))
  318. {
  319. return TRUE;
  320. }
  321. // // on the top left side, need to test the bottom right corner
  322. if (PtInPolygon(rectFiled.TopLeft(), ptPolygon))
  323. {
  324. return TRUE;
  325. }
  326. // // on the bottom left side, need to test the top right corner
  327. if (PtInPolygon(CPoint(rectFiled.left, rectFiled.bottom), ptPolygon))
  328. {
  329. return TRUE;
  330. }
  331. // this field is not in the area at all, return FALSE.
  332. return FALSE;
  333. }
  334. //作用:判断点是否在多边形内
  335. //p指目标点, ptPolygon指多边形的点集合, nCount指多边形的边数
  336. BOOL CFieldMgr::PtInPolygon(CPoint p, std::vector<CPoint> ptPolygon)
  337. {
  338. int nCount = ptPolygon.size();
  339. // 交点个数
  340. int nCross = 0;
  341. for (int i = 0; i < nCount; i++)
  342. {
  343. CPoint p1 = ptPolygon[i];
  344. CPoint p2 = ptPolygon[(i + 1) % nCount];// 点P1与P2形成连线
  345. if (p1.y == p2.y)
  346. continue;
  347. if (p.y < min(p1.y, p2.y))
  348. continue;
  349. if (p.y >= max(p1.y, p2.y))
  350. continue;
  351. // 求交点的x坐标(由直线两点式方程转化而来)
  352. double x = (double)(p.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
  353. // 只统计p1p2与p向右射线的交点
  354. if (x > p.x)
  355. {
  356. nCross++;
  357. }
  358. }
  359. // 交点为偶数,点在多边形之外
  360. // 交点为奇数,点在多边形之内
  361. if ((nCross % 2) == 1)
  362. {
  363. //true;
  364. return TRUE;
  365. }
  366. else
  367. {
  368. //false;
  369. return FALSE;
  370. }
  371. }
  372. // test if field is in or partly in the measure domain area
  373. BOOL CFieldMgr::IsInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
  374. {
  375. // check measure area parameter
  376. ASSERT(m_pMeasureArea);
  377. if (!m_pMeasureArea)
  378. {
  379. // shouldn't happen
  380. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  381. return FALSE;
  382. }
  383. // test field centre point first
  384. if (m_pMeasureArea->PtInDomain(a_poiField))
  385. {
  386. // centre in the measure domain area, return TRUE
  387. return TRUE;
  388. }
  389. // get measure field centre
  390. CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  391. // move to left top postion.
  392. a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  393. // rectangle of the field
  394. CRect rectFiled(a_poiField, a_sizeImageSize);
  395. // check field position
  396. if (rectFiled.left <= poiMsrAreaCentre.x && rectFiled.right >= poiMsrAreaCentre.x)
  397. {
  398. // centre column field or centre field
  399. return TRUE;
  400. }
  401. else if (rectFiled.top <= poiMsrAreaCentre.y && rectFiled.bottom >= poiMsrAreaCentre.y)
  402. {
  403. // centre row field?
  404. return TRUE;
  405. }
  406. else if ( rectFiled.right <= poiMsrAreaCentre.x)
  407. {
  408. // on the left side
  409. //up
  410. if (rectFiled.top >= poiMsrAreaCentre.y)
  411. {
  412. // on the top left side, need to test the bottom right corner
  413. if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.right, rectFiled.top)))
  414. {
  415. return TRUE;
  416. }
  417. }
  418. else if(rectFiled.bottom <= poiMsrAreaCentre.y) //down//
  419. {
  420. // on the bottom left side, need to test the top right corner
  421. if (m_pMeasureArea->PtInDomain(rectFiled.BottomRight()))
  422. {
  423. return TRUE;
  424. }
  425. }
  426. }
  427. else if(rectFiled.left >= poiMsrAreaCentre.x)
  428. {
  429. // on the right side
  430. //up
  431. if (rectFiled.top >= poiMsrAreaCentre.y)
  432. {
  433. // on the top left side, need to test the bottom right corner
  434. if (m_pMeasureArea->PtInDomain(rectFiled.TopLeft()))
  435. {
  436. return TRUE;
  437. }
  438. }
  439. else if (rectFiled.bottom <= poiMsrAreaCentre.y) //down//
  440. {
  441. // on the bottom left side, need to test the top right corner
  442. if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.left, rectFiled.bottom)))
  443. {
  444. return TRUE;
  445. }
  446. }
  447. }
  448. // this field is not in the area at all, return FALSE.
  449. return FALSE;
  450. }
  451. // test if field is in the measured field centre points list
  452. BOOL CFieldMgr::IsInMeasuredFieldList(CPoint a_poiField, std::vector<CPoint> m_listHaveMeasuredFieldCentrePoints)
  453. {
  454. for (CPoint pnt : m_listHaveMeasuredFieldCentrePoints)
  455. {
  456. double scanHeight = (double)m_ScanFieldSize * ((double)m_ResolutionSize.cy / (double)m_ResolutionSize.cx);
  457. CPoint leftTop = CPoint(pnt.x - m_ScanFieldSize / 2, pnt.y + scanHeight / 2);
  458. CPoint rightBottom = CPoint(pnt.x + m_ScanFieldSize / 2, pnt.y - scanHeight / 2);
  459. COTSRect rec = COTSRect(leftTop, rightBottom);
  460. if (rec.PointInRect(a_poiField))
  461. {
  462. return true;
  463. }
  464. }
  465. // ok, return FALSE
  466. return FALSE;
  467. }
  468. // find the next field centre
  469. BOOL CFieldMgr::FindNeighborFieldCentre(const std::vector<CPoint>& a_listFieldCentres,
  470. double a_dScanFieldSizeX,
  471. double a_dScanFieldSizeY,
  472. CPoint a_poiCurrent,
  473. SORTING_DIRECTION& a_nDirection,
  474. CPoint& a_poiNeighbor)
  475. {
  476. // assume no neighbor
  477. BOOL bFind = FALSE;
  478. // go through the field centres list
  479. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  480. {
  481. // test if this is a neighbor field centre
  482. SORTING_DIRECTION nDirection;
  483. if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
  484. {
  485. // we find a neighbor field centre
  486. // let see if this is neighbor we are looking for
  487. switch (a_nDirection)
  488. {
  489. // last move is left
  490. case SORTING_DIRECTION::LEFT:
  491. {
  492. // we are looking for DOWN neighbor
  493. if (nDirection == SORTING_DIRECTION::DOWN)
  494. {
  495. // we find a neighbor below, get out
  496. a_poiNeighbor = poiFieldCentre;
  497. a_nDirection = SORTING_DIRECTION::DOWN;
  498. return TRUE;
  499. }
  500. }
  501. break;
  502. // last move is down
  503. case SORTING_DIRECTION::DOWN:
  504. {
  505. // we are looking for RIGHT neighbor
  506. if (nDirection == SORTING_DIRECTION::RIGHT)
  507. {
  508. // we find a neighbor on the right, get out
  509. a_poiNeighbor = poiFieldCentre;
  510. a_nDirection = SORTING_DIRECTION::RIGHT;
  511. return TRUE;
  512. }
  513. }
  514. break;
  515. // last move is right
  516. case SORTING_DIRECTION::RIGHT:
  517. {
  518. // we are looking for UP neighbor
  519. if (nDirection == SORTING_DIRECTION::UP)
  520. {
  521. // we find a neighbor above
  522. a_poiNeighbor = poiFieldCentre;
  523. a_nDirection = SORTING_DIRECTION::UP;
  524. return TRUE;
  525. }
  526. }
  527. break;
  528. // last move is up
  529. case SORTING_DIRECTION::UP:
  530. {
  531. // we are looking for LEFT neighbor
  532. if (nDirection == SORTING_DIRECTION::LEFT)
  533. {
  534. // we find a neighbor on the left, get out
  535. a_poiNeighbor = poiFieldCentre;
  536. a_nDirection = SORTING_DIRECTION::LEFT;
  537. return TRUE;
  538. }
  539. }
  540. break;
  541. }
  542. }
  543. }
  544. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  545. {
  546. // test if this is a neighbor field centre
  547. SORTING_DIRECTION nDirection;
  548. if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
  549. {
  550. // we find a neighbor field centre
  551. // let see if this is neighbor we are looking for
  552. switch (a_nDirection)
  553. {
  554. // last move is left
  555. case SORTING_DIRECTION::LEFT:
  556. {
  557. // we are looking for DOWN neighbor , but not found
  558. // or LEFT neighbor otherwise
  559. if (nDirection == SORTING_DIRECTION::LEFT)
  560. {
  561. // we find a neighbor on the left, continue looking
  562. a_poiNeighbor = poiFieldCentre;
  563. return TRUE;
  564. }
  565. }
  566. break;
  567. // last move is down
  568. case SORTING_DIRECTION::DOWN:
  569. {
  570. // we are looking for RIGHT neighbor, but not found
  571. // or DOWN neighbor otherwise
  572. if (nDirection == SORTING_DIRECTION::DOWN)
  573. {
  574. // we find a neighbor below, continue looking
  575. a_poiNeighbor = poiFieldCentre;
  576. return TRUE;
  577. }
  578. }
  579. break;
  580. // last move is right
  581. case SORTING_DIRECTION::RIGHT:
  582. {
  583. // we are looking for UP neighbor, but not found
  584. // or RIGHT neighbor, otherwise
  585. if (nDirection == SORTING_DIRECTION::RIGHT)
  586. {
  587. // we find a neighbor on the right, continue looking
  588. a_poiNeighbor = poiFieldCentre;
  589. return TRUE;
  590. }
  591. }
  592. break;
  593. // last move is up
  594. case SORTING_DIRECTION::UP:
  595. {
  596. // we are looking for LEFT neighbor, but not found
  597. // or UP neighbor, otherwise
  598. if (nDirection == SORTING_DIRECTION::UP)
  599. {
  600. // we find a neighbor above, continue looking
  601. a_poiNeighbor = poiFieldCentre;
  602. return TRUE;
  603. }
  604. }
  605. break;
  606. }
  607. }
  608. }
  609. // return find result
  610. return bFind;
  611. }
  612. // find field centre closest to measure domain point
  613. BOOL CFieldMgr::FindFieldCentreClosestMeasureDomainCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint a_poiMeasureDomain, CPoint& a_poi)
  614. {
  615. // distance ratio
  616. int nDisRadio = -1;
  617. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  618. {
  619. // calculate current field centre distance ratio
  620. int nCurFiledDisRadio = (poiFieldCentre.x - a_poiMeasureDomain.x)*(poiFieldCentre.x - a_poiMeasureDomain.x) + (poiFieldCentre.y - a_poiMeasureDomain.y)*(poiFieldCentre.y - a_poiMeasureDomain.y);
  621. // pick one which more closer to centre
  622. if (nDisRadio > nCurFiledDisRadio || nDisRadio == -1)
  623. {
  624. a_poi = poiFieldCentre;
  625. nDisRadio = nCurFiledDisRadio;
  626. }
  627. }
  628. // nDisRadio != -1 means there still field centre in the a_listFieldCentres
  629. return nDisRadio != -1;
  630. }
  631. // find right far side field centre
  632. void CFieldMgr::FindRightMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  633. {
  634. for (auto& poi : a_listFieldCentres)
  635. {
  636. if (poi.y == a_poi.y && poi.x > a_poi.x)
  637. {
  638. a_poi = poi;
  639. }
  640. }
  641. }
  642. // find left far side field centre
  643. void CFieldMgr::FindLeftMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  644. {
  645. for (auto& poi : a_listFieldCentres)
  646. {
  647. if (poi.y == a_poi.y && poi.x < a_poi.x)
  648. {
  649. a_poi = poi;
  650. }
  651. }
  652. }
  653. // find top far side field centre
  654. void CFieldMgr::FindHeighestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  655. {
  656. for (auto& poi : a_listFieldCentres)
  657. {
  658. if (poi.x == a_poi.x && poi.y > a_poi.y)
  659. {
  660. a_poi = poi;
  661. }
  662. }
  663. }
  664. // find bottom far side field centre
  665. void CFieldMgr::FindLowestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  666. {
  667. for (auto& poi : a_listFieldCentres)
  668. {
  669. if (poi.x == a_poi.x && poi.y < a_poi.y)
  670. {
  671. a_poi = poi;
  672. }
  673. }
  674. }
  675. // check if this is a neighbor field centre
  676. BOOL CFieldMgr::IsNeighborFieldCentre(CPoint a_poiFieldCentre,
  677. CPoint a_poiCurrent,
  678. double a_dScanFieldSizeX,
  679. double a_dScanFieldSizeY,
  680. SORTING_DIRECTION& a_nDirection)
  681. {
  682. // x position of the tow field centres are the same, y positions have one field difference
  683. if (a_poiFieldCentre.x == a_poiCurrent.x && abs(a_poiFieldCentre.y - a_poiCurrent.y) == long(a_dScanFieldSizeY))
  684. {
  685. // test is above or below
  686. if (a_poiCurrent.y > a_poiFieldCentre.y)
  687. {
  688. // below
  689. a_nDirection = SORTING_DIRECTION::DOWN;
  690. }
  691. else
  692. {
  693. // above
  694. a_nDirection = SORTING_DIRECTION::UP;
  695. }
  696. // this is a neighbor field centre, return TRUE
  697. return TRUE;
  698. }
  699. // y position of the tow field centres are the same, x positions have one field difference
  700. else if (a_poiFieldCentre.y == a_poiCurrent.y && abs(a_poiFieldCentre.x - a_poiCurrent.x) == long(a_dScanFieldSizeX))
  701. {
  702. // test is on left or right
  703. if (a_poiCurrent.x > a_poiFieldCentre.x)
  704. {
  705. // on the left
  706. a_nDirection = SORTING_DIRECTION::LEFT;
  707. }
  708. else
  709. {
  710. // on the right
  711. a_nDirection = SORTING_DIRECTION::RIGHT;
  712. }
  713. // this is a neighbor field centre, return TRUE
  714. return TRUE;
  715. }
  716. // this is not a neighbor field centre, return FALSE
  717. return FALSE;
  718. }
  719. }