FieldMgr.cpp 25 KB

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