FieldMgr.cpp 25 KB

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