FieldMgr.cpp 22 KB

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