📄 2dtracecontour.cpp
字号:
// 2DTraceContour.cpp: implementation of the Rx2DTraceContour class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "fusion.h"#include "2DTraceContour.h"#include <afxtempl.h>#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////Rx2DTraceContour::Rx2DTraceContour(){}Rx2DTraceContour::~Rx2DTraceContour(){}CPoint * Rx2DTraceContour::TraceContour(BYTE *pMap, int *pnPtCnt, int nWidth, int nHeight){ BYTE *pBoundMap = MakeBoundaryMap(pMap, nWidth, nHeight); int nImgSize = nWidth * nHeight; for(int i=0;i<nImgSize && pMap[i]==0;i++); // First Boundary Index Searching int nStartIdx = i; int nRef[4] = { -nWidth, 1, nWidth, -1}; int nNext[4] = { 1, nWidth, -1, -nWidth}; CArray<int, int> PtArray; PtArray.RemoveAll(); int nIdx = nStartIdx; PtArray.Add(nIdx); // Add to Contour - nIdx int nDir = 0; // 0 - right, 1 - down, 2 - left, 3 - up int nCnt = 0; do { nCnt++; if(nDir<0) nDir+=4; else if(nDir>3) nDir = nDir%4; int nNextIdx = nIdx + nNext[nDir]; int nOutIdx = nNextIdx + nRef[nDir]; if(pBoundMap[nNextIdx] != 0 && pBoundMap[nOutIdx] == 0){ // go PtArray.Add(nNextIdx); nIdx = nNextIdx; } else if(pBoundMap[nNextIdx] == 0 && pBoundMap[nOutIdx] == 0){ nDir ++; } else if(pBoundMap[nNextIdx] == 0 && pBoundMap[nOutIdx] != 0){ nDir --; PtArray.Add(nOutIdx); nIdx = nOutIdx; } else if(pBoundMap[nNextIdx] != 0 && pBoundMap[nOutIdx] != 0){ PtArray.Add(nNextIdx); PtArray.Add(nOutIdx); nIdx = nOutIdx; nDir--; } } while(nIdx!=nStartIdx || nCnt<3); GlobalFree(pBoundMap); int nSize = PtArray.GetSize(); CPoint *ptContour = new CPoint[nSize]; for(i=0;i<nSize;i++){ int nContourIdx = PtArray.GetAt(0); ptContour[i].x = nContourIdx%nWidth; ptContour[i].y = nContourIdx/nWidth; PtArray.RemoveAt(0); } *pnPtCnt = nSize; return ptContour;}BYTE * Rx2DTraceContour::MakeBoundaryMap(BYTE *pMap, int nWidth, int nHeight){ BYTE *pBoundMap = (BYTE *)GlobalAlloc(GMEM_FIXED, nWidth*nHeight); ZeroMemory(pBoundMap, nWidth*nHeight); int nNeighbor[4] = { 1, nWidth, -1, -nWidth}; for(int y = 1; y<nHeight-1;y++){ int yIdx = y*nWidth; for(int x = 1; x < nWidth-1;x++){ int nIdx = yIdx+x; if(pMap[nIdx]==0) continue; for(int n = 0; n<4; n++){ int nNeighborIdx = nIdx + nNeighbor[n]; if(pMap[nNeighborIdx] == 0){ pBoundMap[nIdx] = 1; break; } } } } return pBoundMap;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -