FieldMgr.cpp 27 KB

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