FieldMgr.cpp 26 KB

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