FieldMgr.cpp 25 KB

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