FieldMgr.cpp 28 KB

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