?? contourdrawer.cpp
字號:
// ContourDrawer.cpp: implementation of the CContourDrawer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ContourGenerator.h"
#include "Contour.h"
#include "ContourDrawer.h"
#include "ClrFileDialog.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CContourDrawer::CContourDrawer(CContour* pContour)
{
ASSERT(pContour!=NULL);
m_pContourOwner = pContour;
m_ContourObjSets.RemoveAll();
m_ScaleRatio = 1.0f;
m_TranslateX = 0.0f;
m_TranslateY = 0.0f;
m_ColorTable.SetValueRange(m_pContourOwner->GetDataOwner()->GetDataInfo().zMin,
m_pContourOwner->GetDataOwner()->GetDataInfo().zMax);
}
CContourDrawer::~CContourDrawer()
{
RemoveAllContourObjs();
}
void CContourDrawer::RemoveAllContourObjs()
{
int nSize = m_ContourObjSets.GetSize();
for(int i=0; i<nSize; i++)
{
delete m_ContourObjSets[i];
}
m_ContourObjSets.RemoveAll();
}
void CContourDrawer::CreateContourObjs()
{//根據m_pContourOwner中的追蹤出的等值線曲線,構造ContourObj對象
TRACE0("CContourDrawer::CreateContourObjs()\n");
ASSERT(m_pContourOwner!=NULL);
RemoveAllContourObjs(); //創建之前先清除
CGridDataInfo& dataInfo = m_pContourOwner->GetDataOwner()->GetDataInfo();
CGeoRect gridBorder(dataInfo.xMin,dataInfo.yMin,
dataInfo.xMax,dataInfo.yMax);
float value;
CCurveList* pCurveList;
int numVals = m_pContourOwner->GetNumberOfValues();
for(int i=0; i<numVals; i++)
{
pCurveList = m_pContourOwner->GetContours().GetAt(i);
ASSERT( pCurveList!=NULL );
value = m_pContourOwner->GetValue(i);
POSITION pos = pCurveList->GetHeadPosition();
CCurve* pCtrLine = NULL;
CContourObj* pCntrObj = NULL;
while (pos!=NULL)
{
pCtrLine = pCurveList->GetNext(pos);
ASSERT(pCtrLine->GetSize()>=2); //一條等值線曲線至少有2個點
//創建ContourObj對象,并存入鏈表中
pCntrObj = new CContourObj(pCtrLine,value);
ASSERT(pCntrObj!=NULL);
m_ContourObjSets.Add(pCntrObj);
}
}
}
//---計算縮放比例和平移量---
void CContourDrawer::CalcScaleRatioAndTranslateXY(CDC* pDC,const CRect& drawRect)
{
int oldMapMode = pDC->GetMapMode();
pDC->SetMapMode(MM_LOENGLISH); //使y軸向上
CRect rt(drawRect);
pDC->DPtoLP(&rt);
CGridDataInfo& dataInfo = m_pContourOwner->GetDataOwner()->GetDataInfo();
CGeoRect mapRect(dataInfo.xMin,dataInfo.yMin,dataInfo.xMax,dataInfo.yMax);
int rtW = rt.Width();
int rtH = abs(rt.Height());
float width = mapRect.Width();
float height = mapRect.Height();
m_ScaleRatio = (width/rtW > height/rtH) ? width/rtW : height/rtH ; //取比例大的
mapRect.Scale(m_ScaleRatio); //計算比例變換后的平移量
m_TranslateX = rt.CenterPoint().x - mapRect.CenterPoint().x; //x方向平移量
m_TranslateY = rt.CenterPoint().y - mapRect.CenterPoint().y; //y方向平移量
pDC->SetMapMode(oldMapMode);
TRACE3("ratio=%f,Movex=%f,Movey=%f\n",m_ScaleRatio,m_TranslateX,m_TranslateY);
}
void CContourDrawer::DoDraw(CDC* pDC, const CRect& drawRect)
{
//把等值線區域居中顯示在繪制矩形drawRect中
ASSERT(m_pContourOwner!=NULL);
//1.如果Contour重新成生了,則就重新創建Contour繪制對象
if(m_pContourOwner->IsGeneratingAgain())
CreateContourObjs();
//2.在繪制之前必須先計算好縮放比例和平移量
CalcScaleRatioAndTranslateXY(pDC,drawRect);
//3.先畫網格邊框
DrawGridBorder(pDC);
//4.再畫等值線
DrawContours(pDC);
}
//繪制網格邊框
void CContourDrawer::DrawGridBorder(CDC* pDC)
{
ASSERT( m_pContourOwner!=NULL );
int oldMapMode = pDC->GetMapMode();
pDC->SetMapMode(MM_LOENGLISH);
CPen greenPen;
greenPen.CreatePen(PS_SOLID,1,RGB(0,255,0));
CPen* oldPen = pDC->SelectObject(&greenPen);
CGridDataInfo& dataInfo = m_pContourOwner->GetDataOwner()->GetDataInfo();
CGeoRect mapRect(dataInfo.xMin,dataInfo.yMin,dataInfo.xMax,dataInfo.yMax);
mapRect.Scale(m_ScaleRatio);
mapRect.Translate(m_TranslateX,m_TranslateY);
pDC->MoveTo(int(mapRect.left), int(mapRect.top));
pDC->LineTo(int(mapRect.right),int(mapRect.top));
pDC->LineTo(int(mapRect.right),int(mapRect.bottom));
pDC->LineTo(int(mapRect.left), int(mapRect.bottom));
pDC->LineTo(int(mapRect.left), int(mapRect.top));
pDC->SelectObject(oldPen);
pDC->SetMapMode(oldMapMode);
}
void CContourDrawer::DrawContours(CDC* pDC)
{//繪制Contour對象,在繪制之前,必須做好繪制順序的排序(調用SortDrawOrder函數)
ASSERT( m_pContourOwner!=NULL );
ASSERT( m_ScaleRatio!=0 );
int oldMapMode = pDC->GetMapMode();
pDC->SetMapMode(MM_LOENGLISH);
CContourObj* pContourObj = NULL;
int nSize = m_ContourObjSets.GetSize();
for(int i=0; i<nSize; i++)
{
pContourObj = m_ContourObjSets[i];
pContourObj->Draw(pDC,m_ColorTable,
m_ScaleRatio,m_TranslateX,m_TranslateY);
}
pDC->SetMapMode(oldMapMode);
}
void CContourDrawer::ModifyColorTable()
{
CClrFileDialog Dlg(
TRUE, // TRUE for FileOpen, FALSE for FileSaveAs
"clr",
"*.clr", //LPCTSTR lpszFileName = NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ENABLETEMPLATE,
"Data Files (*.clr) | *.clr ||",
NULL);
if(Dlg.DoModal()!=IDOK)
return;
m_ColorTable.LoadColorTable(Dlg.GetPathName());
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -