FieldMgr.cpp 25 KB

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