FieldMgr.cpp 28 KB

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