FieldMgr.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 (m_pMeasureArea->GetShape() == DOMAIN_SHAPE::POLYGON)
  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. BOOL CFieldMgr::IsThisPointInMeasureArea(CPoint a_position)
  289. {
  290. // 判断当前样品获取帧图信息的测量区域为多边形
  291. if (m_pMeasureArea->GetShape() == DOMAIN_SHAPE::POLYGON)
  292. {
  293. std::vector<CPoint> ptPolygon = m_pMeasureArea->GetPolygonPoint();
  294. if (PtInPolygon(a_position, ptPolygon))
  295. {
  296. // centre in the measure domain area, return TRUE
  297. return TRUE;
  298. }
  299. else
  300. {
  301. return FALSE;
  302. }
  303. }
  304. else
  305. {
  306. if (m_pMeasureArea->PtInDomain(a_position))
  307. {
  308. // centre in the measure domain area, return TRUE
  309. return TRUE;
  310. }
  311. else
  312. {
  313. return FALSE;
  314. }
  315. }
  316. }
  317. // test if field is in or partly in the measure domain area
  318. BOOL CFieldMgr::IsInPolygonMeasureArea(CPoint a_poiField, CSize a_sizeImageSize, std::vector<CPoint> ptPolygon)
  319. {
  320. // check measure area parameter
  321. ASSERT(m_pMeasureArea);
  322. if (!m_pMeasureArea)
  323. {
  324. // shouldn't happen
  325. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  326. return FALSE;
  327. }
  328. // test field centre point first
  329. if (PtInPolygon(a_poiField, ptPolygon))
  330. {
  331. // centre in the measure domain area, return TRUE
  332. return TRUE;
  333. }
  334. // get measure field centre
  335. CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  336. // move to left top postion.
  337. a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  338. // rectangle of the field
  339. CRect rectFiled(a_poiField, a_sizeImageSize);
  340. // // on the top left side, need to test the bottom right corner
  341. if (PtInPolygon(CPoint(rectFiled.right, rectFiled.top), ptPolygon))
  342. {
  343. return TRUE;
  344. }
  345. // // on the bottom left side, need to test the top right corner
  346. if (PtInPolygon(rectFiled.BottomRight(), ptPolygon))
  347. {
  348. return TRUE;
  349. }
  350. // // on the top left side, need to test the bottom right corner
  351. if (PtInPolygon(rectFiled.TopLeft(), ptPolygon))
  352. {
  353. return TRUE;
  354. }
  355. // // on the bottom left side, need to test the top right corner
  356. if (PtInPolygon(CPoint(rectFiled.left, rectFiled.bottom), ptPolygon))
  357. {
  358. return TRUE;
  359. }
  360. // this field is not in the area at all, return FALSE.
  361. return FALSE;
  362. }
  363. //作用:判断点是否在多边形内
  364. //p指目标点, ptPolygon指多边形的点集合, nCount指多边形的边数
  365. BOOL CFieldMgr::PtInPolygon(CPoint p, std::vector<CPoint> ptPolygon)
  366. {
  367. int nCount = ptPolygon.size();
  368. // 交点个数
  369. int nCross = 0;
  370. for (int i = 0; i < nCount; i++)
  371. {
  372. CPoint p1 = ptPolygon[i];
  373. CPoint p2 = ptPolygon[(i + 1) % nCount];// 点P1与P2形成连线
  374. if (p1.y == p2.y)
  375. continue;
  376. if (p.y < min(p1.y, p2.y))
  377. continue;
  378. if (p.y >= max(p1.y, p2.y))
  379. continue;
  380. // 求交点的x坐标(由直线两点式方程转化而来)
  381. double x = (double)(p.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
  382. // 只统计p1p2与p向右射线的交点
  383. if (x > p.x)
  384. {
  385. nCross++;
  386. }
  387. }
  388. // 交点为偶数,点在多边形之外
  389. // 交点为奇数,点在多边形之内
  390. if ((nCross % 2) == 1)
  391. {
  392. //true;
  393. return TRUE;
  394. }
  395. else
  396. {
  397. //false;
  398. return FALSE;
  399. }
  400. }
  401. // test if field is in or partly in the measure domain area
  402. BOOL CFieldMgr::IsInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
  403. {
  404. // check measure area parameter
  405. ASSERT(m_pMeasureArea);
  406. if (!m_pMeasureArea)
  407. {
  408. // shouldn't happen
  409. LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
  410. return FALSE;
  411. }
  412. // test field centre point first
  413. if (m_pMeasureArea->PtInDomain(a_poiField))
  414. {
  415. // centre in the measure domain area, return TRUE
  416. return TRUE;
  417. }
  418. // get measure field centre
  419. CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
  420. // move to left top postion.
  421. a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
  422. // rectangle of the field
  423. CRect rectFiled(a_poiField, a_sizeImageSize);
  424. // check field position
  425. if (rectFiled.left <= poiMsrAreaCentre.x && rectFiled.right >= poiMsrAreaCentre.x)
  426. {
  427. // centre column field or centre field
  428. return TRUE;
  429. }
  430. else if (rectFiled.top <= poiMsrAreaCentre.y && rectFiled.bottom >= poiMsrAreaCentre.y)
  431. {
  432. // centre row field?
  433. return TRUE;
  434. }
  435. else if ( rectFiled.right <= poiMsrAreaCentre.x)
  436. {
  437. // on the left side
  438. //up
  439. if (rectFiled.top >= poiMsrAreaCentre.y)
  440. {
  441. // on the top left side, need to test the bottom right corner
  442. if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.right, rectFiled.top)))
  443. {
  444. return TRUE;
  445. }
  446. }
  447. else if(rectFiled.bottom <= poiMsrAreaCentre.y) //down//
  448. {
  449. // on the bottom left side, need to test the top right corner
  450. if (m_pMeasureArea->PtInDomain(rectFiled.BottomRight()))
  451. {
  452. return TRUE;
  453. }
  454. }
  455. }
  456. else if(rectFiled.left >= poiMsrAreaCentre.x)
  457. {
  458. // on the right side
  459. //up
  460. if (rectFiled.top >= poiMsrAreaCentre.y)
  461. {
  462. // on the top left side, need to test the bottom right corner
  463. if (m_pMeasureArea->PtInDomain(rectFiled.TopLeft()))
  464. {
  465. return TRUE;
  466. }
  467. }
  468. else if (rectFiled.bottom <= poiMsrAreaCentre.y) //down//
  469. {
  470. // on the bottom left side, need to test the top right corner
  471. if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.left, rectFiled.bottom)))
  472. {
  473. return TRUE;
  474. }
  475. }
  476. }
  477. // this field is not in the area at all, return FALSE.
  478. return FALSE;
  479. }
  480. // test if field is in the measured field centre points list
  481. BOOL CFieldMgr::IsInMeasuredFieldList(CPoint a_poiField, std::vector<CPoint> m_listHaveMeasuredFieldCentrePoints)
  482. {
  483. for (CPoint pnt : m_listHaveMeasuredFieldCentrePoints)
  484. {
  485. double scanHeight = (double)m_ScanFieldSize * ((double)m_ResolutionSize.cy / (double)m_ResolutionSize.cx);
  486. CPoint leftTop = CPoint(pnt.x - m_ScanFieldSize / 2, pnt.y + scanHeight / 2);
  487. CPoint rightBottom = CPoint(pnt.x + m_ScanFieldSize / 2, pnt.y - scanHeight / 2);
  488. COTSRect rec = COTSRect(leftTop, rightBottom);
  489. if (rec.PointInRect(a_poiField))
  490. {
  491. return true;
  492. }
  493. }
  494. // ok, return FALSE
  495. return FALSE;
  496. }
  497. // find the next field centre
  498. BOOL CFieldMgr::FindNeighborFieldCentre(const std::vector<CPoint>& a_listFieldCentres,
  499. double a_dScanFieldSizeX,
  500. double a_dScanFieldSizeY,
  501. CPoint a_poiCurrent,
  502. SORTING_DIRECTION& a_nDirection,
  503. CPoint& a_poiNeighbor)
  504. {
  505. // assume no neighbor
  506. BOOL bFind = FALSE;
  507. // go through the field centres list
  508. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  509. {
  510. // test if this is a neighbor field centre
  511. SORTING_DIRECTION nDirection;
  512. if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
  513. {
  514. // we find a neighbor field centre
  515. // let see if this is neighbor we are looking for
  516. switch (a_nDirection)
  517. {
  518. // last move is left
  519. case SORTING_DIRECTION::LEFT:
  520. {
  521. // we are looking for DOWN neighbor
  522. if (nDirection == SORTING_DIRECTION::DOWN)
  523. {
  524. // we find a neighbor below, get out
  525. a_poiNeighbor = poiFieldCentre;
  526. a_nDirection = SORTING_DIRECTION::DOWN;
  527. return TRUE;
  528. }
  529. }
  530. break;
  531. // last move is down
  532. case SORTING_DIRECTION::DOWN:
  533. {
  534. // we are looking for RIGHT neighbor
  535. if (nDirection == SORTING_DIRECTION::RIGHT)
  536. {
  537. // we find a neighbor on the right, get out
  538. a_poiNeighbor = poiFieldCentre;
  539. a_nDirection = SORTING_DIRECTION::RIGHT;
  540. return TRUE;
  541. }
  542. }
  543. break;
  544. // last move is right
  545. case SORTING_DIRECTION::RIGHT:
  546. {
  547. // we are looking for UP neighbor
  548. if (nDirection == SORTING_DIRECTION::UP)
  549. {
  550. // we find a neighbor above
  551. a_poiNeighbor = poiFieldCentre;
  552. a_nDirection = SORTING_DIRECTION::UP;
  553. return TRUE;
  554. }
  555. }
  556. break;
  557. // last move is up
  558. case SORTING_DIRECTION::UP:
  559. {
  560. // we are looking for LEFT neighbor
  561. if (nDirection == SORTING_DIRECTION::LEFT)
  562. {
  563. // we find a neighbor on the left, get out
  564. a_poiNeighbor = poiFieldCentre;
  565. a_nDirection = SORTING_DIRECTION::LEFT;
  566. return TRUE;
  567. }
  568. }
  569. break;
  570. }
  571. }
  572. }
  573. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  574. {
  575. // test if this is a neighbor field centre
  576. SORTING_DIRECTION nDirection;
  577. if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
  578. {
  579. // we find a neighbor field centre
  580. // let see if this is neighbor we are looking for
  581. switch (a_nDirection)
  582. {
  583. // last move is left
  584. case SORTING_DIRECTION::LEFT:
  585. {
  586. // we are looking for DOWN neighbor , but not found
  587. // or LEFT neighbor otherwise
  588. if (nDirection == SORTING_DIRECTION::LEFT)
  589. {
  590. // we find a neighbor on the left, continue looking
  591. a_poiNeighbor = poiFieldCentre;
  592. return TRUE;
  593. }
  594. }
  595. break;
  596. // last move is down
  597. case SORTING_DIRECTION::DOWN:
  598. {
  599. // we are looking for RIGHT neighbor, but not found
  600. // or DOWN neighbor otherwise
  601. if (nDirection == SORTING_DIRECTION::DOWN)
  602. {
  603. // we find a neighbor below, continue looking
  604. a_poiNeighbor = poiFieldCentre;
  605. return TRUE;
  606. }
  607. }
  608. break;
  609. // last move is right
  610. case SORTING_DIRECTION::RIGHT:
  611. {
  612. // we are looking for UP neighbor, but not found
  613. // or RIGHT neighbor, otherwise
  614. if (nDirection == SORTING_DIRECTION::RIGHT)
  615. {
  616. // we find a neighbor on the right, continue looking
  617. a_poiNeighbor = poiFieldCentre;
  618. return TRUE;
  619. }
  620. }
  621. break;
  622. // last move is up
  623. case SORTING_DIRECTION::UP:
  624. {
  625. // we are looking for LEFT neighbor, but not found
  626. // or UP neighbor, otherwise
  627. if (nDirection == SORTING_DIRECTION::UP)
  628. {
  629. // we find a neighbor above, continue looking
  630. a_poiNeighbor = poiFieldCentre;
  631. return TRUE;
  632. }
  633. }
  634. break;
  635. }
  636. }
  637. }
  638. // return find result
  639. return bFind;
  640. }
  641. // find field centre closest to measure domain point
  642. BOOL CFieldMgr::FindFieldCentreClosestMeasureDomainCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint a_poiMeasureDomain, CPoint& a_poi)
  643. {
  644. // distance ratio
  645. int nDisRadio = -1;
  646. for (const CPoint& poiFieldCentre : a_listFieldCentres)
  647. {
  648. // calculate current field centre distance ratio
  649. int nCurFiledDisRadio = (poiFieldCentre.x - a_poiMeasureDomain.x)*(poiFieldCentre.x - a_poiMeasureDomain.x) + (poiFieldCentre.y - a_poiMeasureDomain.y)*(poiFieldCentre.y - a_poiMeasureDomain.y);
  650. // pick one which more closer to centre
  651. if (nDisRadio > nCurFiledDisRadio || nDisRadio == -1)
  652. {
  653. a_poi = poiFieldCentre;
  654. nDisRadio = nCurFiledDisRadio;
  655. }
  656. }
  657. // nDisRadio != -1 means there still field centre in the a_listFieldCentres
  658. return nDisRadio != -1;
  659. }
  660. // find right far side field centre
  661. void CFieldMgr::FindRightMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  662. {
  663. for (auto& poi : a_listFieldCentres)
  664. {
  665. if (poi.y == a_poi.y && poi.x > a_poi.x)
  666. {
  667. a_poi = poi;
  668. }
  669. }
  670. }
  671. // find left far side field centre
  672. void CFieldMgr::FindLeftMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  673. {
  674. for (auto& poi : a_listFieldCentres)
  675. {
  676. if (poi.y == a_poi.y && poi.x < a_poi.x)
  677. {
  678. a_poi = poi;
  679. }
  680. }
  681. }
  682. // find top far side field centre
  683. void CFieldMgr::FindHeighestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  684. {
  685. for (auto& poi : a_listFieldCentres)
  686. {
  687. if (poi.x == a_poi.x && poi.y > a_poi.y)
  688. {
  689. a_poi = poi;
  690. }
  691. }
  692. }
  693. // find bottom far side field centre
  694. void CFieldMgr::FindLowestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
  695. {
  696. for (auto& poi : a_listFieldCentres)
  697. {
  698. if (poi.x == a_poi.x && poi.y < a_poi.y)
  699. {
  700. a_poi = poi;
  701. }
  702. }
  703. }
  704. // check if this is a neighbor field centre
  705. BOOL CFieldMgr::IsNeighborFieldCentre(CPoint a_poiFieldCentre,
  706. CPoint a_poiCurrent,
  707. double a_dScanFieldSizeX,
  708. double a_dScanFieldSizeY,
  709. SORTING_DIRECTION& a_nDirection)
  710. {
  711. // x position of the tow field centres are the same, y positions have one field difference
  712. if (a_poiFieldCentre.x == a_poiCurrent.x && abs(a_poiFieldCentre.y - a_poiCurrent.y) == long(a_dScanFieldSizeY))
  713. {
  714. // test is above or below
  715. if (a_poiCurrent.y > a_poiFieldCentre.y)
  716. {
  717. // below
  718. a_nDirection = SORTING_DIRECTION::DOWN;
  719. }
  720. else
  721. {
  722. // above
  723. a_nDirection = SORTING_DIRECTION::UP;
  724. }
  725. // this is a neighbor field centre, return TRUE
  726. return TRUE;
  727. }
  728. // y position of the tow field centres are the same, x positions have one field difference
  729. else if (a_poiFieldCentre.y == a_poiCurrent.y && abs(a_poiFieldCentre.x - a_poiCurrent.x) == long(a_dScanFieldSizeX))
  730. {
  731. // test is on left or right
  732. if (a_poiCurrent.x > a_poiFieldCentre.x)
  733. {
  734. // on the left
  735. a_nDirection = SORTING_DIRECTION::LEFT;
  736. }
  737. else
  738. {
  739. // on the right
  740. a_nDirection = SORTING_DIRECTION::RIGHT;
  741. }
  742. // this is a neighbor field centre, return TRUE
  743. return TRUE;
  744. }
  745. // this is not a neighbor field centre, return FALSE
  746. return FALSE;
  747. }
  748. }