FieldMgr.cpp 22 KB

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