最近地圖專案中使用遞回取瓦片地圖,當地圖層級高且下一級沒有圖片時,遞回的效率變得相當慢,需要卡很久才能反應過來,考慮對代碼做優化,嘗試改成迭代,發現并沒有那么容易,有大神有這方面經驗的可以指導一下嗎?不勝感激!僅有的100分奉上,謝謝!!
以下是原始碼,為方便閱讀,省去了一些無關緊要的東西:
void CDrawImageDrawer::drawImages(int iHdc,double dLeft,double dTop,double dRight,double dBottom,int iZoom)
{
if(iZoom < 0)
{
return;
}
IImageSource* pImageSource = m_imageSourceMap[m_curImageSource];
if(pImageSource->_bNeedCheckSource)
{
pImageSource->_bNeedCheckSource = false;
checkImageSourceIsExsit();
}
if(!pImageSource->_bExsitImageSource)
{
return;
}
MapServerDiskImageSource* pMapServerDiskImageSource = 0;
IImageSource::IMAGESOURCE_TYPE imageType = pImageSource->getType();
switch(imageType)
{
case IImageSource::IMAGESOURCE_TYPE_MapServerDisk:
{
pMapServerDiskImageSource = (MapServerDiskImageSource*)pImageSource;
break;
}
}
// 繪制
int minX = long2tilex(dLeft,iZoom);
int minY = lat2tiley(dTop,iZoom);
int maxX = long2tilex(dRight,iZoom);
int maxY = lat2tiley(dBottom,iZoom);
for(int i = minX;i <= maxX; ++i)
{
for(int j = minY;j <= maxY; ++j)
{
double dImageLeft = tilex2long(i,iZoom);
double dImageTop = tiley2lat(j,iZoom);
double dImageRight = tilex2long(i+1,iZoom);
double dImageBottom = tiley2lat(j+1,iZoom);
// 使用當前層圖片繪制
bool bIsValid = false;
if(pMapServerDiskImageSource)
{
// 使用記憶體位圖來繪制
if(m_memoryImage[iZoom].isValid())
{
int iLDest = 0;
int iRDest = 0;
int iTDest = 0;
int iBDest = 0;
IGF_Display* pDisplay = m_pMain->GetDisplayPtr();
IGIS_SpatialReference* pSpatialReferenceBJ54 = m_pSpatialReferenceFactory->GetPredefinedSpatialReferenceByIndex(GIS_BJ54_SPATIALREFERENCE_INDEX);
pDisplay->WorldToClient((int)(dImageLeft*m_xyScale),(int)(dImageTop*m_xyScale),&iLDest,&iTDest,pSpatialReferenceBJ54);
pDisplay->WorldToClient((int)(dImageRight*m_xyScale),(int)(dImageBottom*m_xyScale),&iRDest,&iBDest,pSpatialReferenceBJ54);
pSpatialReferenceBJ54->Release();
bIsValid = m_memoryImage[iZoom].drawMemoryToScreen(iHdc,iZoom,i,j,iLDest,iTDest,iRDest,iBDest);
}
// 從檔案來繪制
if(!bIsValid)
{
//if(tileIsInRangeEx(i,j,iZoom) || pMapServerDiskImageSource->_levelRange.empty())
{
char imagePath[512] = {0};
sprintf_s(imagePath,"%s",pMapServerDiskImageSource->_imageDirectory.c_str());
bIsValid = drawImageFromFileEx(iHdc,imagePath,dImageLeft,dImageTop,dImageRight,dImageBottom,pMapServerDiskImageSource->_bImageEncrypt,pMapServerDiskImageSource->_dSaturation,iZoom,i,j);
}
}
}
if(iZoom-1 < 0)
{
continue;
}
// 使用上一層圖片繪制
if(!bIsValid)
{
/*if(iZoom == m_iRealZoom &&
m_memoryImage[iZoom-1].isValid())*/
if(m_memoryImage[iZoom-1].isValid())
{
/*----------- ----------- -----------
| 8 | | 1 | | 7 |
| ------------------------- |
| | | | | | | |
------|---- ----------- ----|------
| |
------|---- ----|------ ----------
| | | 記憶體位圖區域 | | | | |
| 3 | | | | 4 | | 9 |
| | | | | | | |
------|---- ----|------ ----------
------|---- ----------- ----|------
| ------------------------- |
| 6 | | 2 | | 5 |
| | | | | |
----------- ----------- -----------*/
// 使用上一層記憶體位圖來繪制
drawImageFromMemory(iHdc,dImageLeft,dImageTop,dImageRight,dImageBottom,iZoom - 1);
double dLeftEx = 0;
double dTopEx = 0;
double dRightEx = 0;
double dBottomEx = 0;
m_memoryImage[iZoom-1].getZXYValidRange(dLeftEx,dTopEx,dRightEx,dBottomEx);
bool bLTInRect = ptInRect(dImageLeft,dImageTop,dLeftEx,dTopEx,dRightEx,dBottomEx);
bool bRTInRect = ptInRect(dImageRight,dImageTop,dLeftEx,dTopEx,dRightEx,dBottomEx);
bool bLBInRect = ptInRect(dImageLeft,dImageBottom,dLeftEx,dTopEx,dRightEx,dBottomEx);
bool bRBInRect = ptInRect(dImageRight,dImageBottom,dLeftEx,dTopEx,dRightEx,dBottomEx);
//1. 左下角兩點和右下角兩點在
if(bLBInRect && bRBInRect && !bLTInRect && !bRTInRect)
{
drawImages(iHdc,dImageLeft,dImageTop,dImageRight,m_dTop,(iZoom - 1));
}
//2. 左上角兩點和右上角兩點在
else if(bLTInRect && bRTInRect && !bLBInRect && !bRBInRect)
{
drawImages(iHdc,dImageLeft,m_dBottom,dImageRight,dImageBottom,(iZoom - 1));
}
// 。。。
}
else
{
drawImages(iHdc,dImageLeft,dImageTop,dImageRight,dImageBottom,(iZoom - 1));
}
}
}
}
}
uj5u.com熱心網友回復:
http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/68649.html
標籤:圖形處理/算法
上一篇:如何使自己維護的lib/dll不再需要區分debug與release?
下一篇:C++簡單演算法求解
