| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028 |
- #include "stdafx.h"
- #include "otsdataconst.h"
- #include "FieldMgr.h"
- #include "../OTSLog/COTSUtilityDllFunExport.h"
- namespace OTSIMGPROC {
- namespace {
- // fill the matrics with the spiral sequence number ,n*n is the largest fill number.
- // the row and col number should be odd number.
- void getSpiralMatrics(std::vector <std::vector <int>>& arrays,int row,int col)
- {
- int n = max(col, row);
- arrays.resize(n, std::vector<int>(n));
- int c = 0, i, j;
- int z = n * n;
- int ou = z;
- while (ou >= 1)
- {
- i = 0;
- j = 0;
- for (i += c, j += c; j < n - c; j++)//from left to right
- {
- if (ou > z) break;
- arrays[i][j] = ou--;
- }
- for (j--, i++; i < n - c; i++) //from top to bottom
- {
- if (ou > z) break;
- arrays[i][j] = ou--;
- }
- for (i--, j--; j >= c; j--)//from right to left
- {
- if (ou > z) break;
- arrays[i][j] = ou--;
- }
- for (j++, i--; i >= c + 1; i--)//from bottom to top
- {
- if (ou > z) break;
- arrays[i][j] = ou--;
- }
- c++;
- }
- // if col<>row then shift the matrics so that the smallest number is in the center of the row*col's matrics.
- if (row > col)
- {
- int offset = (row - col) / 2;
- for (int k = 0; k < col; k++)//move mat to left (row-col)/2 cols.
- {
- for (int m = 0; m < row; m++)
- {
- arrays[m][k] = arrays[m][k + offset];
- }
- }
- }
- else if (col > row)
- {
- int offset = (col - row) / 2;
- for (int k = 0; k < row; k++)//move mat to up (col-row)/2 cols.
- {
- for (int m = 0; m < col; m++)
- {
- arrays[k][m] = arrays[k+offset][m];
- }
- }
- }
- }
- void getZShapeMatrics(std::vector <std::vector <int>>& arrays, int row, int col)
- {
-
- arrays.resize(row, std::vector<int>(col));
-
-
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < col; j++)
- {
-
- arrays[i][j] = col *(row- i) + j+1;
-
-
- }
- }
- }
- void getUpDownMeanderMatrics(std::vector <std::vector <int>>& arrays, int row, int col)
- {
- arrays.resize(row, std::vector<int>(col));
- for (int i = 0; i <row; i++)
- {
- for (int j = 0; j < col; j++)
- {
- if (i % 2 == 0)
- {
- arrays[i][j] = col * (row-i) + j + 1;
- }
- else
- {
- arrays[i][j] = col *(row- i) + (col - j);
- }
- }
- }
- }
- }
- using namespace OTSDATA;
- // CFieldMgr
- CFieldMgr::CFieldMgr(int scanfieldsize,CSize a_ResolutionSize)
- {
- m_ScanFieldSize = scanfieldsize;
- m_ResolutionSize = a_ResolutionSize;
- m_pMeasureArea=nullptr;
- }
-
- CFieldMgr::~CFieldMgr()
- {
- }
- // initialization
- BOOL CFieldMgr::Init(CDomainPtr a_pMeasureArea, int a_FieldStartMode)
- {
- // input check
- ASSERT(a_pMeasureArea);
-
- // assign class member
- m_pMeasureArea = CDomainPtr(new CDomain(a_pMeasureArea.get()));
-
-
- m_fieldStartMode = a_FieldStartMode;
- return TRUE;
- }
- std::vector<CPoint> CFieldMgr::GetUnmeasuredFieldCentrePoints(std::vector<CPoint> a_listMeasuredFieldCentrePoints,int distributionMode)
- {
- std::vector<CPoint> allPoints = CalculateFieldCentrePoints(distributionMode);
- std::vector<CPoint> unmeasuredPoints;
- for(auto p:allPoints)
- if (!IsInMeasuredFieldList(p,a_listMeasuredFieldCentrePoints))
- {
- // add the field centre into the unmeasured field centre points list
- unmeasuredPoints.push_back(p);
- }
- return unmeasuredPoints;
- }
- std::vector<CPoint> CFieldMgr::GetFieldCentrePoints(int distributionMode)
- {
- auto m_listFieldCentrePoints = CalculateFieldCentrePoints(distributionMode);
- return m_listFieldCentrePoints;
- }
- int CFieldMgr::GetTotalFields(int distributionMode)
- {
- auto m_listFieldCentrePoints = CalculateFieldCentrePoints(distributionMode);
- return (int)m_listFieldCentrePoints.size();
- }
-
- // measure area
- void CFieldMgr::SetMeasureArea(CDomainPtr a_pMeasureArea)
- {
- // input check
- ASSERT(a_pMeasureArea);
- if (!a_pMeasureArea)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("SetMeasureArea: invalid measure area poiter."));
- return;
- }
- m_pMeasureArea = CDomainPtr(new CDomain(a_pMeasureArea.get()));
- }
- int CFieldMgr::GetEffectiveFieldWidth()
- {
- auto width= m_ScanFieldSize - 2*m_overlap;
- return width;
- }
- int CFieldMgr::GetEffectiveFieldHeight()
- {
-
- CSize ImageSizeByPixel = m_ResolutionSize;
- // scan field size (x, y)
- double pixelx = ImageSizeByPixel.cx;
- double pixely = ImageSizeByPixel.cy;
- double dScanFiledSizeX = m_ScanFieldSize;
- double dScanFiledSizeY = dScanFiledSizeX * pixely / pixelx;
- auto height= dScanFiledSizeY - 2*m_overlap;
- return height;
- }
- COTSFieldDataPtr CFieldMgr::FindNeighborField(const COTSFieldDataList a_flds, COTSFieldDataPtr a_centerField, SORTING_DIRECTION a_direction)
- {
- COTSFieldDataPtr fld;
- double pixelsize;
-
- for (auto f : a_flds)
- {
- SORTING_DIRECTION di;
- IsNeighborFieldCentre(f->GetPosition(), a_centerField->GetPosition(), GetEffectiveFieldWidth(), GetEffectiveFieldHeight(), di);
- if (di == a_direction)
- {
- return f;
- }
- }
- return fld;
- }
- bool CFieldMgr::FindNeighborField(const std::vector<CPoint> a_flds, CPoint a_centerField,CPoint& neighbor, SORTING_DIRECTION a_direction)
- {
-
-
- for (auto f : a_flds)
- {
- SORTING_DIRECTION di;
- IsNeighborFieldCentre(f, a_centerField, GetEffectiveFieldWidth(), GetEffectiveFieldHeight(), di);
- if (di == a_direction)
- {
- neighbor=f;
- return true;
- }
- }
- return false;
- }
- // protected
-
- // calculate field centre points list
- std::vector<CPoint> CFieldMgr::CalculateFieldCentrePoints(int distributionMode)
- {
- // field centre points list
- std::vector<CPoint> m_listFieldCentrePoints;
-
- // clean up
- m_listFieldCentrePoints.clear();
-
- // the measure domain rectangle
- CRect rectMeasureDomain = m_pMeasureArea->GetDomainRect();
- // the measure domain centre
- CPoint poiDomainCentre = rectMeasureDomain.CenterPoint();
- double effectiveWidth = GetEffectiveFieldWidth();
- double effectiveHeight = GetEffectiveFieldHeight();
- CSize sizeImage;
- sizeImage.cx = effectiveWidth;
- sizeImage.cy = effectiveHeight;
- // start mode
- OTS_GET_IMAGE_MODE nStartMode = (OTS_GET_IMAGE_MODE)m_fieldStartMode;
- rectMeasureDomain.NormalizeRect();
- int height = (double)rectMeasureDomain.Height();
- int width = (double)rectMeasureDomain.Width();
-
- // calculate total columns, rows and make sure the domain area be covered
- int nTotalCols = ceil((width / effectiveWidth));
- int nTotalRows = ceil(height / effectiveHeight);
- // calculate column on the left of the centre point
- int nLeftCols = nTotalCols / 2;
- int nRightCols = nLeftCols;
- // fields on top
- int nRowsOnTop = nTotalRows / 2;
- // sure total columns, rows are odd numbers
- nTotalCols = nLeftCols * 2 + 1;
- //nTotalRows = nTotalRows * 2 + 1;
- nTotalRows = nRowsOnTop * 2 + 1;
- // calculate left, right field column position (x only
- int nLeftMostColX = poiDomainCentre.x - nLeftCols * (effectiveWidth);
- int nUpMostRowY = poiDomainCentre.y - nRowsOnTop * (effectiveHeight);
-
- std::vector <std::vector <CPoint>> pointMatrics(nTotalRows, std::vector<CPoint>(nTotalCols));
- for (int i = 0; i < nTotalRows; i++)
- {
- for (int j = 0; j < nTotalCols; j++)
- {
- pointMatrics[i][j].x = nLeftMostColX + j * (effectiveWidth);
- pointMatrics[i][j].y = nUpMostRowY + i * (effectiveHeight);
- }
- }
- std::vector <std::vector <int>> sequenceMat; //construct an matrics map to the pointMatrics,but the content is the sequence number.
- switch (nStartMode)
- {
- case OTS_GET_IMAGE_MODE::SpiralSequnce:
- getSpiralMatrics(sequenceMat, nTotalRows,nTotalCols);
- break;
- case OTS_GET_IMAGE_MODE::SnakeSequnce :
- getUpDownMeanderMatrics(sequenceMat, nTotalRows, nTotalCols);
- break;
- case OTS_GET_IMAGE_MODE::ZShapeSequnce :
- getZShapeMatrics(sequenceMat, nTotalRows, nTotalCols);
- case OTS_GET_IMAGE_MODE::RANDOM :
-
- break;
- }
-
- std::map <int, CPoint> mapCenterPoint;
- for (int i = 0; i < nTotalRows; i++)
- {
- for (int j = 0; j < nTotalCols; j++)
- {
- int sequenceNum = sequenceMat[i][j];
- CPoint p = pointMatrics[i][j];
- mapCenterPoint[sequenceNum] = p;// sorting all the field center point by the sequence number.
- }
- }
- // judge if the measure area is polygon
- if (m_pMeasureArea->GetShape() == DOMAIN_SHAPE::POLYGON)
- {
- std::vector<CPoint> ptPolygon = m_pMeasureArea->GetPolygonPoint();
- for (auto itr : mapCenterPoint)
- {
-
- switch (distributionMode)
- {
- case 0://center in
- if (IsInPolygonMeasureArea(itr.second, sizeImage, ptPolygon))
- {
- m_listFieldCentrePoints.push_back(itr.second);
- }
- break;
- case 1://partly in ,all field will cover all the measure area
- if (IsPartlyInPolygonMeasureArea(itr.second, sizeImage, ptPolygon))
- {
- m_listFieldCentrePoints.push_back(itr.second);
- }
- break;
- case 2://totally in, field is totally in the measure area
- if (IsTotallyInPolygonMeasureArea(itr.second, sizeImage, ptPolygon))
- {
- m_listFieldCentrePoints.push_back(itr.second);
- }
- break;
- default:
- break;
- }
- }
- }
- else
- {
- for (auto itr : mapCenterPoint)
- {
- switch (distributionMode)
- {
- case 0://center in
- if (IsCenterInMeasureArea(itr.second, sizeImage))
- {
- m_listFieldCentrePoints.push_back(itr.second);
- }
- break;
- case 1://partly in ,all field will cover all the measure area
- if (IsPartlyInMeasureArea(itr.second, sizeImage))
- {
- m_listFieldCentrePoints.push_back(itr.second);
- }
- break;
- case 2://totally in, field is totally in the measure area
- if (IsTotallyInMeasureArea(itr.second, sizeImage))
- {
- m_listFieldCentrePoints.push_back(itr.second);
- }
- break;
- default:
- break;
- }
-
- }
- }
-
- return m_listFieldCentrePoints;
- }
- BOOL CFieldMgr::IsThisPointInMeasureArea(CPoint a_position)
- {
- // judge if the measure area is polygon
- if (m_pMeasureArea->GetShape() == DOMAIN_SHAPE::POLYGON)
- {
- std::vector<CPoint> ptPolygon = m_pMeasureArea->GetPolygonPoint();
-
-
- if (PtInPolygon(a_position, ptPolygon))
- {
- // centre in the measure domain area, return TRUE
- return TRUE;
- }
- else
- {
- return FALSE;
- }
-
- }
- else
- {
-
- if (m_pMeasureArea->PtInDomain(a_position))
- {
- // centre in the measure domain area, return TRUE
- return TRUE;
- }
- else
- {
- return FALSE;
- }
-
- }
-
- }
- // test if field is in or partly in the measure domain area
- BOOL CFieldMgr::IsInPolygonMeasureArea(CPoint a_poiField, CSize a_sizeImageSize, std::vector<CPoint> ptPolygon)
- {
- // check measure area parameter
- ASSERT(m_pMeasureArea);
- if (!m_pMeasureArea)
- {
- // shouldn't happen
- LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
- return FALSE;
- }
- // test field centre point first
- if (PtInPolygon(a_poiField, ptPolygon))
- {
- // centre in the measure domain area, return TRUE
- return TRUE;
- }
-
- // this field is not in the area at all, return FALSE.
- return FALSE;
- }
- BOOL CFieldMgr::IsPartlyInPolygonMeasureArea(CPoint a_poiField, CSize a_sizeImageSize, std::vector<CPoint> ptPolygon)
- {
- // check measure area parameter
- ASSERT(m_pMeasureArea);
- if (!m_pMeasureArea)
- {
- // shouldn't happen
- LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
- return FALSE;
- }
- // test field centre point first
- if (PtInPolygon(a_poiField, ptPolygon))
- {
- // centre in the measure domain area, return TRUE
- return TRUE;
- }
- // get measure field centre
- CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
- // move to left top postion.
- a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
- // rectangle of the field
- CRect rectFiled(a_poiField, a_sizeImageSize);
- // on the top left side, need to test the bottom right corner
- if (PtInPolygon(CPoint(rectFiled.right, rectFiled.top), ptPolygon))
- {
- return TRUE;
- }
- // on the bottom left side, need to test the top right corner
- if (PtInPolygon(rectFiled.BottomRight(), ptPolygon))
- {
- return TRUE;
- }
- // on the top left side, need to test the bottom right corner
- if (PtInPolygon(rectFiled.TopLeft(), ptPolygon))
- {
- return TRUE;
- }
- // on the bottom left side, need to test the top right corner
- if (PtInPolygon(CPoint(rectFiled.left, rectFiled.bottom), ptPolygon))
- {
- return TRUE;
- }
- // this field is not in the area at all, return FALSE.
- return FALSE;
- }
- BOOL CFieldMgr::IsTotallyInPolygonMeasureArea(CPoint a_poiField, CSize a_sizeImageSize, std::vector<CPoint> ptPolygon)
- {
- // check measure area parameter
- ASSERT(m_pMeasureArea);
- if (!m_pMeasureArea)
- {
- // shouldn't happen
- LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
- return FALSE;
- }
- // move to left top postion.
- a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
- // rectangle of the field
- CRect rectFiled(a_poiField, a_sizeImageSize);
- // on the top left side, need to test the bottom right corner
- if (!PtInPolygon(CPoint(rectFiled.right, rectFiled.top), ptPolygon))
- {
- return FALSE;
- }
- // on the bottom left side, need to test the top right corner
- if (!PtInPolygon(rectFiled.BottomRight(), ptPolygon))
- {
- return FALSE;
- }
- // on the top left side, need to test the bottom right corner
- if (!PtInPolygon(rectFiled.TopLeft(), ptPolygon))
- {
- return FALSE;
- }
- // on the bottom left side, need to test the top right corner
- if (!PtInPolygon(CPoint(rectFiled.left, rectFiled.bottom), ptPolygon))
- {
- return FALSE;
- }
-
- return TRUE;
- }
- //function:judge whether the point p is in the polygon ptPolygon
- //p is the particular point, ptPolygon is the collection of the vertex point
- BOOL CFieldMgr::PtInPolygon(CPoint p, std::vector<CPoint> ptPolygon)
- {
- int nCount = ptPolygon.size();
- // the polygon must have at least 3 points
- int nCross = 0;
- for (int i = 0; i < nCount; i++)
- {
- CPoint p1 = ptPolygon[i];
- CPoint p2 = ptPolygon[(i + 1) % nCount];// the line between the point p1 and p2
- if (p1.y == p2.y)
- continue;
- if (p.y < min(p1.y, p2.y))
- continue;
- if (p.y >= max(p1.y, p2.y))
- continue;
- // get the cross point (from the two point equation)
- double x = (double)(p.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
- //only count the cross point which is on the right side of the point p
- if (x > p.x)
- {
- nCross++;
- }
- }
- // the cross point number is even then the point is outside the polygon
- //the cross point number is odd then the point is inside the polygon
- if ((nCross % 2) == 1)
- {
- //true;
- return TRUE;
- }
- else
- {
- //false;
- return FALSE;
- }
- }
- // test if field is in or partly in the measure domain area
- BOOL CFieldMgr::IsCenterInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
- {
- // check measure area parameter
- ASSERT(m_pMeasureArea);
- if (!m_pMeasureArea)
- {
- // shouldn't happen
- LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
- return FALSE;
- }
- // test field centre point first
- if (m_pMeasureArea->PtInDomain(a_poiField))
- {
- // centre in the measure domain area, return TRUE
- return TRUE;
- }
-
- // this field is not in the area , return FALSE.
- return FALSE;
- }
- // test if field is in or partly in the measure domain area
- BOOL CFieldMgr::IsPartlyInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
- {
- // check measure area parameter
- ASSERT(m_pMeasureArea);
- if (!m_pMeasureArea)
- {
- // shouldn't happen
- LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
- return FALSE;
- }
- // test field centre point first
- if (m_pMeasureArea->PtInDomain(a_poiField))
- {
- // centre in the measure domain area, return TRUE
- return TRUE;
- }
- // get measure field centre
- //CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
- // move to left top postion.
- a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
- // rectangle of the field
- CRect rectFiled(a_poiField, a_sizeImageSize);
- if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.right, rectFiled.top)))
- {
- return true;
- }
- if (m_pMeasureArea->PtInDomain(rectFiled.BottomRight()))
- {
- return true;
- }
- if (m_pMeasureArea->PtInDomain(rectFiled.TopLeft()))
- {
- return true;
- }
- if (m_pMeasureArea->PtInDomain(CPoint(rectFiled.left, rectFiled.bottom)))
- {
- return true;
- }
- // check field position
-
- // this field is not in the area , return FALSE.
- return FALSE;
- }
- BOOL CFieldMgr::IsTotallyInMeasureArea(CPoint a_poiField, CSize a_sizeImageSize)
- {
- // check measure area parameter
- ASSERT(m_pMeasureArea);
- if (!m_pMeasureArea)
- {
- // shouldn't happen
- LogErrorTrace(__FILE__, __LINE__, _T("IsInDomainArea: invalid measure area parameter."));
- return FALSE;
- }
- // get measure field centre
- //CPoint poiMsrAreaCentre = m_pMeasureArea->GetDomainCenter();
- // move to left top postion.
- a_poiField -= CPoint(a_sizeImageSize.cx / 2, a_sizeImageSize.cy / 2);
- // rectangle of the field
- CRect rectFiled(a_poiField, a_sizeImageSize);
-
-
- if (!m_pMeasureArea->PtInDomain(CPoint(rectFiled.right, rectFiled.top)))
- {
- return false;
- }
-
- if (!m_pMeasureArea->PtInDomain(rectFiled.BottomRight()))
- {
- return false;
- }
-
- if (!m_pMeasureArea->PtInDomain(rectFiled.TopLeft()))
- {
- return false;
- }
-
-
- if (!m_pMeasureArea->PtInDomain(CPoint(rectFiled.left, rectFiled.bottom)))
- {
- return false;
- }
-
- return true;
- }
- // test if field is in the measured field centre points list
- BOOL CFieldMgr::IsInMeasuredFieldList(CPoint a_poiField, std::vector<CPoint> m_listHaveMeasuredFieldCentrePoints)
- {
-
- for (CPoint pnt : m_listHaveMeasuredFieldCentrePoints)
- {
- double scanHeight = (double)m_ScanFieldSize * ((double)m_ResolutionSize.cy / (double)m_ResolutionSize.cx);
- CPoint leftTop = CPoint(pnt.x - m_ScanFieldSize / 2, pnt.y + scanHeight / 2);
- CPoint rightBottom = CPoint(pnt.x + m_ScanFieldSize / 2, pnt.y - scanHeight / 2);
-
- COTSRect rec = COTSRect(leftTop, rightBottom);
- if (rec.PointInRect(a_poiField))
- {
- return true;
- }
- }
- // ok, return FALSE
- return FALSE;
- }
- // find the next field centre
- BOOL CFieldMgr::FindNeighborFieldCentre(const std::vector<CPoint>& a_listFieldCentres,
- double a_dScanFieldSizeX,
- double a_dScanFieldSizeY,
- CPoint a_poiCurrent,
- SORTING_DIRECTION& a_nDirection,
- CPoint& a_poiNeighbor)
- {
- // assume no neighbor
- BOOL bFind = FALSE;
- // go through the field centres list
- for (const CPoint& poiFieldCentre : a_listFieldCentres)
- {
- // test if this is a neighbor field centre
- SORTING_DIRECTION nDirection;
- if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
- {
- // we find a neighbor field centre
- // let see if this is neighbor we are looking for
- switch (a_nDirection)
- {
- // last move is left
- case SORTING_DIRECTION::LEFT:
- {
- // we are looking for DOWN neighbor
- if (nDirection == SORTING_DIRECTION::DOWN)
- {
- // we find a neighbor below, get out
- a_poiNeighbor = poiFieldCentre;
- a_nDirection = SORTING_DIRECTION::DOWN;
- return TRUE;
- }
- }
- break;
- // last move is down
- case SORTING_DIRECTION::DOWN:
- {
- // we are looking for RIGHT neighbor
- if (nDirection == SORTING_DIRECTION::RIGHT)
- {
- // we find a neighbor on the right, get out
- a_poiNeighbor = poiFieldCentre;
- a_nDirection = SORTING_DIRECTION::RIGHT;
- return TRUE;
- }
- }
- break;
- // last move is right
- case SORTING_DIRECTION::RIGHT:
- {
- // we are looking for UP neighbor
- if (nDirection == SORTING_DIRECTION::UP)
- {
- // we find a neighbor above
- a_poiNeighbor = poiFieldCentre;
- a_nDirection = SORTING_DIRECTION::UP;
- return TRUE;
- }
- }
- break;
- // last move is up
- case SORTING_DIRECTION::UP:
- {
- // we are looking for LEFT neighbor
- if (nDirection == SORTING_DIRECTION::LEFT)
- {
- // we find a neighbor on the left, get out
- a_poiNeighbor = poiFieldCentre;
- a_nDirection = SORTING_DIRECTION::LEFT;
- return TRUE;
- }
- }
- break;
- }
- }
- }
- for (const CPoint& poiFieldCentre : a_listFieldCentres)
- {
- // test if this is a neighbor field centre
- SORTING_DIRECTION nDirection;
- if (IsNeighborFieldCentre(poiFieldCentre, a_poiCurrent, a_dScanFieldSizeX, a_dScanFieldSizeY, nDirection))
- {
- // we find a neighbor field centre
- // let see if this is neighbor we are looking for
- switch (a_nDirection)
- {
- // last move is left
- case SORTING_DIRECTION::LEFT:
- {
- // we are looking for DOWN neighbor , but not found
- // or LEFT neighbor otherwise
- if (nDirection == SORTING_DIRECTION::LEFT)
- {
- // we find a neighbor on the left, continue looking
- a_poiNeighbor = poiFieldCentre;
- return TRUE;
- }
- }
- break;
- // last move is down
- case SORTING_DIRECTION::DOWN:
- {
- // we are looking for RIGHT neighbor, but not found
- // or DOWN neighbor otherwise
- if (nDirection == SORTING_DIRECTION::DOWN)
- {
- // we find a neighbor below, continue looking
- a_poiNeighbor = poiFieldCentre;
- return TRUE;
- }
- }
- break;
- // last move is right
- case SORTING_DIRECTION::RIGHT:
- {
- // we are looking for UP neighbor, but not found
- // or RIGHT neighbor, otherwise
- if (nDirection == SORTING_DIRECTION::RIGHT)
- {
- // we find a neighbor on the right, continue looking
- a_poiNeighbor = poiFieldCentre;
- return TRUE;
- }
- }
- break;
- // last move is up
- case SORTING_DIRECTION::UP:
- {
- // we are looking for LEFT neighbor, but not found
- // or UP neighbor, otherwise
- if (nDirection == SORTING_DIRECTION::UP)
- {
- // we find a neighbor above, continue looking
- a_poiNeighbor = poiFieldCentre;
- return TRUE;
- }
- }
- break;
- }
- }
- }
- // return find result
- return bFind;
- }
- // find field centre closest to measure domain point
- BOOL CFieldMgr::FindFieldCentreClosestMeasureDomainCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint a_poiMeasureDomain, CPoint& a_poi)
- {
- // distance ratio
- int nDisRadio = -1;
- for (const CPoint& poiFieldCentre : a_listFieldCentres)
- {
- // calculate current field centre distance ratio
- int nCurFiledDisRadio = (poiFieldCentre.x - a_poiMeasureDomain.x)*(poiFieldCentre.x - a_poiMeasureDomain.x) + (poiFieldCentre.y - a_poiMeasureDomain.y)*(poiFieldCentre.y - a_poiMeasureDomain.y);
- // pick one which more closer to centre
- if (nDisRadio > nCurFiledDisRadio || nDisRadio == -1)
- {
- a_poi = poiFieldCentre;
- nDisRadio = nCurFiledDisRadio;
- }
- }
- // nDisRadio != -1 means there still field centre in the a_listFieldCentres
- return nDisRadio != -1;
- }
- // find right far side field centre
- void CFieldMgr::FindRightMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
- {
- for (auto& poi : a_listFieldCentres)
- {
- if (poi.y == a_poi.y && poi.x > a_poi.x)
- {
- a_poi = poi;
- }
- }
- }
- // find left far side field centre
- void CFieldMgr::FindLeftMostFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
- {
- for (auto& poi : a_listFieldCentres)
- {
- if (poi.y == a_poi.y && poi.x < a_poi.x)
- {
- a_poi = poi;
- }
- }
- }
- // find top far side field centre
- void CFieldMgr::FindHeighestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
- {
- for (auto& poi : a_listFieldCentres)
- {
- if (poi.x == a_poi.x && poi.y > a_poi.y)
- {
- a_poi = poi;
- }
- }
- }
- // find bottom far side field centre
- void CFieldMgr::FindLowestFieldCentre(const std::vector<CPoint>& a_listFieldCentres, CPoint& a_poi)
- {
- for (auto& poi : a_listFieldCentres)
- {
- if (poi.x == a_poi.x && poi.y < a_poi.y)
- {
- a_poi = poi;
- }
- }
- }
- // check if this is a neighbor field centre
- BOOL CFieldMgr::IsNeighborFieldCentre(CPoint a_poiFieldCentre,
- CPoint a_poiCurrent,
- double a_dScanFieldSizeX,
- double a_dScanFieldSizeY,
- SORTING_DIRECTION& a_nDirection)
- {
- // x position of the tow field centres are the same, y positions have one field difference
- if (a_poiFieldCentre.x == a_poiCurrent.x && abs(a_poiFieldCentre.y - a_poiCurrent.y) == long(a_dScanFieldSizeY))
- {
- // test is above or below
- if (a_poiCurrent.y > a_poiFieldCentre.y)
- {
- // below
- a_nDirection = SORTING_DIRECTION::DOWN;
- }
- else
- {
- // above
- a_nDirection = SORTING_DIRECTION::UP;
- }
- // this is a neighbor field centre, return TRUE
- return TRUE;
- }
- // y position of the tow field centres are the same, x positions have one field difference
- else if (a_poiFieldCentre.y == a_poiCurrent.y && abs(a_poiFieldCentre.x - a_poiCurrent.x) == long(a_dScanFieldSizeX))
- {
- // test is on left or right
- if (a_poiCurrent.x > a_poiFieldCentre.x)
- {
- // on the left
- a_nDirection = SORTING_DIRECTION::LEFT;
- }
- else
- {
- // on the right
- a_nDirection = SORTING_DIRECTION::RIGHT;
- }
- // this is a neighbor field centre, return TRUE
- return TRUE;
- }
- // this is not a neighbor field centre, return FALSE
- return FALSE;
- }
-
- }
|