www.pudn.com > RADAR-ALARM.rar > RadarGraph.cpp, change:2011-08-15,size:15765b


#include "StdAfx.h" 
#include "RadarGraph.h" 
#include <math.h> 
#include<stdlib.h>  
#include "ZoomGraphDlg.h" 
 
#define Angle_Num 0 
#define Bin_Num 0 
#define REF_MODE 0 
#define VEL_MODE 1 
#define VALUE_INVALID -999.		//实际值中的特殊标记,表示无有效观测数据 
#define VALUE_RANFOLD  999.		//实际值中的特殊标记,表示有距离模糊 
 
CRadarGraph::CRadarGraph(void) 
: RData(NULL) 
, m_RangeBinNum(0) 
, m_CenterPoint(0) 
, m_TotalXPoint(0) 
, m_TotalYPoint(0) 
, m_Radius(0) 
, m_GraphMode(1) 
, m_MaxLayerNum(21) 
, m_MaxBinNum(920) 
, m_ShowMaxRadius(230) 
, m_LayerIndex(0) 
, m_ShowMaxRadiusBin(0) 
, m_DataMaxRadius(0) 
, m_DataReadyFlag(false) 
, m_ZoomFlag(false) 
, m_lightflag(false) 
, m_GraphAlertFlag(0) 
, m_alertfilename(_T("")) 
{ 
} 
 
CRadarGraph::~CRadarGraph(void) 
{ 
} 
BEGIN_MESSAGE_MAP(CRadarGraph, CStatic) 
	ON_WM_PAINT() 
	ON_WM_LBUTTONDBLCLK() 
	ON_WM_TIMER() 
END_MESSAGE_MAP() 
 
void CRadarGraph::OnPaint() 
{ 
	CPaintDC dc(this); // device context for painting 
	// TODO: 在此处添加消息处理程序代码 
	// 不为绘图消息调用 CStatic::OnPaint() 
	 
	/*CReadRadarBaseData m_data; 
	m_data.ReadBaseData(_T("2009060311.01A")); 
	RData=m_data.RData; 
	VData=m_data.VData; 
	WData=m_data.WData; 
	InitPar(); 
	GetStaticInitInfo(); 
	CDC *pDC = GetDC(); 
	 
    DrawBackGround( pDC); 
	DrawBaseLine(pDC); 
	 
	DrawData(pDC); 
	DrawBaseLine(pDC);*/ 
	CDC *pDC = GetDC(); 
	Draw(pDC); 
	ReleaseDC(pDC); 
} 
 
void CRadarGraph::GetStaticInitInfo(void) 
{ 
	this->GetClientRect(m_FrameRect); 
	int width=m_FrameRect.Width(); 
	int lenth=m_FrameRect.Height(); 
	  if(width>=lenth) 
		  m_GraphRect.SetRect(0,0,lenth,lenth); 
	  else 
          m_GraphRect.SetRect(0,0,width,width); 
	  m_CenterPoint.SetPoint(m_GraphRect.Width()/2,m_GraphRect.Height()/2); 
	  m_TotalXPoint=m_GraphRect.Width(); 
	  m_TotalYPoint=m_GraphRect.Height(); 
	  m_Radius=m_GraphRect.Height()/2; 
 
} 
 
void CRadarGraph::DrawBaseLine(CDC* pDC) 
{ 
    
    CBrush Brush; 
	CPen pen(PS_SOLID,1,RGB(125,0,0)); 
	CPen *oldpen; 
	Brush.CreateStockObject(NULL_BRUSH); 
	pDC->SelectObject(&Brush); 
	oldpen=pDC->SelectObject(&pen); 
	CPoint pt1,pt2; 
	pt1.SetPoint(m_GraphRect.left,(m_GraphRect.bottom-m_GraphRect.top)/2+m_GraphRect.top); 
	pt2.SetPoint(m_GraphRect.right,(m_GraphRect.bottom-m_GraphRect.top)/2+m_GraphRect.top); 
	pDC->MoveTo(pt1); 
	pDC->LineTo(pt2); 
	pt1.SetPoint((m_GraphRect.right-m_GraphRect.left)/2+m_GraphRect.left,m_GraphRect.bottom); 
	pt2.SetPoint((m_GraphRect.right-m_GraphRect.left)/2+m_GraphRect.left,m_GraphRect.top); 
	pDC->MoveTo(pt1); 
	pDC->LineTo(pt2); 
 
	int r50=(m_GraphRect.right-m_GraphRect.left)*50/460; 
	for(int i=1;i<5;i++) 
	{ 
          	pDC->Ellipse(CRect(CPoint((m_GraphRect.right-m_GraphRect.left)/2+m_GraphRect.left-i*r50,(m_GraphRect.bottom-m_GraphRect.top)/2+m_GraphRect.top-i*r50),CPoint((m_GraphRect.right-m_GraphRect.left)/2+m_GraphRect.left+i*r50,(m_GraphRect.bottom-m_GraphRect.top)/2+m_GraphRect.top+i*r50))); 
	} 
     
           pDC->Ellipse(m_GraphRect); 
		   Brush.DeleteObject(); 
		   pen.DeleteObject(); 
		   oldpen->DeleteObject(); 
		    
 
} 
 
 
 
// 数据画点 
void CRadarGraph::DrawData(CDC* pDC) 
{ 
	for(int i=0;i<m_TotalXPoint;i++) 
	{ 
		for(int j=0;j<m_TotalYPoint;j++) 
		{ 
			if(m_Radius>=CalRadius(i,j)) 
			{ 
				float radius=CalRadius(i,j); 
				float angle=GetAngle(i,j); 
				float value=GetValue(radius,angle,m_LayerIndex,1); 
				if(value!=VALUE_INVALID) 
				{ 
					RGBST color=GetRGB(value); 
					pDC->SetPixel(i,j,RGB(color.r,color.g,color.b)); 
				} 
			} 
		} 
	} 
       
} 
 
// 单线性插值 
float CRadarGraph::SingleLinear(float StartV, float EndV, float StartAzi, float EndAzi, float Azi, float DefaultV) 
{ 
	if(StartV==DefaultV) 
		return DefaultV; 
	else if(EndV==DefaultV) 
		return DefaultV; 
	else//两值都有效 
	{ 
		if(StartAzi==EndAzi) 
			return (StartV+EndV)/2; 
		else 
			return (Azi-StartAzi)*(EndV-StartV)/(EndAzi-StartAzi)+StartV; 
	} 
} 
 
// 双线性插值 
float CRadarGraph::DoubleLinear(float NearStartV, float FarStartV, float NearEndV, float FarEndV, float StartAzi, float EndAzi, float StartBin, float EndBin, float Azi, float Bin,float DefaultV) 
{ 
	float StartV = SingleLinear(NearStartV,FarStartV,StartBin,EndBin,Bin,DefaultV); 
	float EndV = SingleLinear(NearEndV,FarEndV,StartBin,EndBin,Bin,DefaultV); 
	return SingleLinear(StartV,EndV,StartAzi,EndAzi,Azi,DefaultV); 
} 
 
// 方位角计算 
float CRadarGraph::GetAngle(float x, float y) 
{ 
	float ret; 
	float xoffset=x-m_CenterPoint.x; 
	float yoffset=y-m_CenterPoint.y; 
	if (xoffset==0){ 
		if (yoffset<=0) 
		 ret=0; 
		else 
         ret=180; 
	} 
    else if(yoffset==0) 
	{ 
 		if (xoffset>=0) 
		 ret=90; 
		else 
         ret=270; 
	} 
	else 
	{ 
		float angletmp = float(atan(fabs(xoffset/yoffset))*180/3.141593); 
		if(xoffset>0 && yoffset<0) 
			ret = angletmp; 
		else if(xoffset<0 && yoffset>0) 
			ret = 180+angletmp ; 
		else if(xoffset<0 && yoffset<0) 
			ret = 360-angletmp; 
		else  
			ret = 180-angletmp; 
	} 
    return ret; 
} 
 
//  计算点到中心的距离 
float CRadarGraph::CalRadius(int x, int y) 
{ 
	return sqrt((float)((x-m_CenterPoint.x)*(x-m_CenterPoint.x)+(y-m_CenterPoint.y)*(y-m_CenterPoint.y))); 
} 
 
 
void CRadarGraph::InitPar(void) 
{ 
	DataClrNum[0].r = 0; 
	DataClrNum[0].g = 0; 
	DataClrNum[0].b = 0; 
	DataClrNum[1].r = 0; 
	DataClrNum[1].g = 230; 
	DataClrNum[1].b = 255; 
	DataClrNum[2].r = 0; 
	DataClrNum[2].g = 150; 
	DataClrNum[2].b = 255; 
	DataClrNum[3].r = 0; 
	DataClrNum[3].g = 0; 
	DataClrNum[3].b = 255; 
	DataClrNum[4].r = 0; 
	DataClrNum[4].g = 255; 
	DataClrNum[4].b = 0; 
	DataClrNum[5].r = 0; 
	DataClrNum[5].g = 200; 
	DataClrNum[5].b = 0; 
	DataClrNum[6].r = 0; 
	DataClrNum[6].g = 128; 
	DataClrNum[6].b = 0; 
	DataClrNum[7].r = 255; 
	DataClrNum[7].g = 255; 
	DataClrNum[7].b = 0; 
	DataClrNum[8].r = 220; 
	DataClrNum[8].g = 180; 
	DataClrNum[8].b = 0; 
	DataClrNum[9].r = 255; 
	DataClrNum[9].g = 180; 
	DataClrNum[9].b = 0; 
	DataClrNum[10].r = 255; 
	DataClrNum[10].g = 0; 
	DataClrNum[10].b = 0; 
	DataClrNum[11].r = 170; 
	DataClrNum[11].g = 0; 
	DataClrNum[11].b = 0; 
	DataClrNum[12].r = 100; 
	DataClrNum[12].g = 0; 
	DataClrNum[12].b = 0; 
	DataClrNum[13].r = 255; 
	DataClrNum[13].g = 0; 
	DataClrNum[13].b = 255; 
	DataClrNum[14].r = 255; 
	DataClrNum[14].g = 180; 
	DataClrNum[14].b = 255; 
 
	Datathreshold[0] = -5; 
	Datathreshold[1] = 0; 
	Datathreshold[2] = 5; 
	Datathreshold[3] = 10; 
	Datathreshold[4] = 15; 
	Datathreshold[5] = 20; 
	Datathreshold[6] = 25; 
	Datathreshold[7] = 30; 
	Datathreshold[8] = 35; 
	Datathreshold[9] = 40; 
	Datathreshold[10] = 45; 
	Datathreshold[11] = 50; 
	Datathreshold[12] = 55; 
	Datathreshold[13] = 60; 
	Datathreshold[14] = 65; 
	 
 
	DataClrNumV[0].r = 126; 
	DataClrNumV[0].g = 244; 
	DataClrNumV[0].b = 254; 
	DataClrNumV[1].r = 0; 
	DataClrNumV[1].g = 224; 
	DataClrNumV[1].b = 254; 
	DataClrNumV[2].r = 0; 
	DataClrNumV[2].g = 176; 
	DataClrNumV[2].b = 176; 
	DataClrNumV[3].r = 0; 
	DataClrNumV[3].g = 254; 
	DataClrNumV[3].b = 0; 
	DataClrNumV[4].r = 0; 
	DataClrNumV[4].g = 196; 
	DataClrNumV[4].b = 0; 
	DataClrNumV[5].r = 0; 
	DataClrNumV[5].g = 128; 
	DataClrNumV[5].b = 0; 
	DataClrNumV[6].r = 254; 
	DataClrNumV[6].g = 254; 
	DataClrNumV[6].b = 254; 
	DataClrNumV[7].r = 252; 
	DataClrNumV[7].g = 252; 
	DataClrNumV[7].b = 252; 
	DataClrNumV[8].r = 254; 
	DataClrNumV[8].g = 0; 
	DataClrNumV[8].b = 0; 
	DataClrNumV[9].r = 254; 
	DataClrNumV[9].g = 88; 
	DataClrNumV[9].b = 88; 
	DataClrNumV[10].r = 254; 
	DataClrNumV[10].g = 176; 
	DataClrNumV[10].b = 176; 
	DataClrNumV[11].r = 254; 
	DataClrNumV[11].g = 124; 
	DataClrNumV[11].b = 0; 
	DataClrNumV[12].r = 254; 
	DataClrNumV[12].g = 210; 
	DataClrNumV[12].b = 0; 
	DataClrNumV[13].r = 254; 
	DataClrNumV[13].g = 254; 
	DataClrNumV[13].b = 0; 
 
 
    DatathresholdV[0] = -128; 
	DatathresholdV[1] = -27; 
	DatathresholdV[2] = -20; 
	DatathresholdV[3] = -15; 
	DatathresholdV[4] = -10; 
	DatathresholdV[5] = -5; 
	DatathresholdV[6] = -1; 
	DatathresholdV[7] = 0; 
	DatathresholdV[8] = 1; 
	DatathresholdV[9] = 5; 
	DatathresholdV[10] = 10; 
	DatathresholdV[11] = 15; 
	DatathresholdV[12] = 20; 
	DatathresholdV[13] = 27; 
     
  
	 
} 
 
// 根据强度值计算RGB值 
RGBST CRadarGraph::GetRGB(float data) 
{ 
	int nClrInx=0; 
	if(m_GraphMode==REF_MODE) 
	{ 
			while(data>Datathreshold[nClrInx]&& nClrInx<14) 
 					{ 
 						nClrInx++; 
 					} 
			return DataClrNum[nClrInx]; 
	} 
	if(m_GraphMode==VEL_MODE) 
	{ 
        if(data==VALUE_RANFOLD) 
		{ 
			RGBST valueinvalidate; 
			    valueinvalidate.r=124; 
				valueinvalidate.g=0; 
				valueinvalidate.b=124; 
				return valueinvalidate; 
		} 
		while(data>DatathresholdV[nClrInx]&& nClrInx<13) 
					{ 
						nClrInx++; 
					} 
		 return  DataClrNumV[nClrInx]; 
	} 
} 
 
void CRadarGraph::DrawBackGround(CDC* pDC) 
{ 
	CBrush BGBrush;//底色涂黑 
    CBrush*OldBrush; 
    BGBrush.CreateSolidBrush(RGB(0,0,0)); 
    OldBrush=pDC->SelectObject(&BGBrush); 
    pDC->Rectangle(&m_FrameRect); 
	pDC->SelectObject(&OldBrush); 
	BGBrush.DeleteObject(); 
	OldBrush->DeleteObject(); 
	 
} 
 
float CRadarGraph::GetValue(float Radius,float Angle,int LayerNum,int CalType) 
{ 
	m_ShowMaxRadiusBin=m_ShowMaxRadius*m_MaxBinNum/m_DataMaxRadius; 
	if(CalType==0) 
	{ 
		int angle=(int)Angle; 
		int radius=(int)(Radius/m_Radius*m_ShowMaxRadiusBin); 
		if(m_GraphMode==REF_MODE) 
		  { 
		    return RData[LayerNum*360*m_MaxBinNum+angle*m_MaxBinNum+radius]; 
		  } 
        if(m_GraphMode==VEL_MODE) 
		  { 
		    return VData[LayerNum*360*m_MaxBinNum+angle*m_MaxBinNum+radius]; 
		  } 
	} 
	if(CalType==1) 
	{ 
		int angle=(int)Angle; 
		int radius=(int)(Radius/m_Radius*m_ShowMaxRadiusBin); 
		int startazi=angle; 
		int endazi; 
		if (startazi==359) 
		    endazi=0; 
		else 
			endazi=startazi+1; 
		int nStartBin=radius; 
		int nEndBin; 
		if(nStartBin>=m_MaxBinNum-1) 
					nEndBin = nStartBin; 
				else 
					nEndBin = nStartBin+1; 
		float NearStartV,FarStartV,NearEndV,FarEndV; 
		if(m_GraphMode==REF_MODE) 
		  { 
			NearStartV = RData[LayerNum*360*m_MaxBinNum+startazi*m_MaxBinNum+nStartBin]; 
			FarStartV = RData[LayerNum*360*m_MaxBinNum+startazi*m_MaxBinNum+nEndBin]; 
			NearEndV = RData[LayerNum*360*m_MaxBinNum+endazi*m_MaxBinNum+nStartBin]; 
			FarEndV = RData[LayerNum*360*m_MaxBinNum+endazi*m_MaxBinNum+nEndBin]; 
			float TempV = DoubleLinear(NearStartV,FarStartV,NearStartV,FarStartV, 
					startazi,endazi,(float)nStartBin,(float)nEndBin,startazi,nStartBin,(float)VALUE_INVALID); 
			return TempV; 
		  } 
		if(m_GraphMode==VEL_MODE) 
		  { 
			NearStartV = VData[LayerNum*360*m_MaxBinNum+startazi*m_MaxBinNum+nStartBin]; 
			FarStartV = VData[LayerNum*360*m_MaxBinNum+startazi*m_MaxBinNum+nEndBin]; 
			NearEndV = VData[LayerNum*360*m_MaxBinNum+endazi*m_MaxBinNum+nStartBin]; 
			FarEndV = VData[LayerNum*360*m_MaxBinNum+endazi*m_MaxBinNum+nEndBin]; 
			float TempV; 
			if(NearStartV==VALUE_RANFOLD||FarStartV==VALUE_RANFOLD||NearEndV==VALUE_RANFOLD||FarEndV==VALUE_RANFOLD) 
				TempV = VALUE_RANFOLD; 
			else 
				TempV = DoubleLinear(NearStartV,FarStartV,NearStartV,FarStartV, 
					startazi,endazi,(float)nStartBin,(float)nEndBin,startazi,nStartBin,(float)VALUE_INVALID); 
			return TempV; 
		  } 
 
	} 
 
} 
 
// 设置基数据层数和一根径向上库数 
void CRadarGraph::SetBinNumCutNum(int BinNum, int CutNum,int DataMaxRadius) 
{ 
	m_MaxBinNum=BinNum; 
	m_MaxLayerNum=CutNum; 
	m_DataMaxRadius=DataMaxRadius; 
 
} 
 
void CRadarGraph::Draw(CDC* pDC) 
{ 
	if(m_GraphMode==REF_MODE) 
    { 
       SetBinNumCutNum(460,20,460); 
	} 
	if(m_GraphMode==VEL_MODE) 
	{ 
       SetBinNumCutNum(920,20,230); 
	} 
 
	InitPar(); 
	GetStaticInitInfo(); 
 
	 
    DrawBackGround( pDC); 
	DrawBaseLine(pDC); 
   if(m_DataReadyFlag==true) 
	{ 
	 
		DrawData(pDC); 
		DrawBaseLine(pDC); 
	 
	} 
   
} 
 
void CRadarGraph::OnLButtonDblClk(UINT nFlags, CPoint point) 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
 
	//CStatic::OnLButtonDblClk(nFlags, point); 
	if(m_ZoomFlag==true) 
	{ 
	CZoomGraphDlg dlg; 
	dlg.m_graph.m_MaxBinNum=this->m_MaxBinNum; 
	dlg.m_graph.m_MaxLayerNum=m_MaxLayerNum; 
	dlg.m_graph.RData=RData; 
	dlg.m_graph.VData=VData; 
	dlg.m_graph.m_GraphMode=m_GraphMode; 
	dlg.m_graph.m_ShowMaxRadius=m_ShowMaxRadius; 
	dlg.m_graph.m_DataReadyFlag=m_DataReadyFlag; 
	dlg.m_graph.m_LayerIndex=m_LayerIndex; 
	 
	 
	dlg.DoModal(); 
	} 
	 
} 
 
void CRadarGraph::AlertZone(CDC* pDC,int azi,int startbin, int endbin,bool flag) 
{      
	CBrush *oldbrush; 
	CPen *pOldPn; 
     if(flag==0) 
	 { 
		 CBrush brush(RGB(255,255,255)); 
		 oldbrush=pDC->SelectObject(&brush); 
		 CPen pPen; 
		 pPen.CreatePen(PS_SOLID,2,RGB(255,255,255)); 
		 pOldPn=pDC->SelectObject(&pPen); 
		 int x,y; 
		  for(int i=startbin;i<=endbin;i++) 
		  { 
			   coverse(azi, i, &x, &y); 
			   if(x>=0&&x<=this->m_TotalXPoint&&y>=0&&y<=this->m_TotalYPoint) 
			   { 
				   pDC->Ellipse(x-3,y-3,x+3,y+3); 
				   // pDC->SetPixel(x,y,RGB(255,255,255)); 
			   } 
		  } 
		  pDC->SelectObject(oldbrush); 
		  pDC->SelectObject( pOldPn); 
		  oldbrush->DeleteObject(); 
		  pOldPn->DeleteObject(); 
		  brush.DeleteObject(); 
		  pPen.DeleteObject(); 
 
 
		   
 
	 } 
	 else 
	 { 
         CBrush brush(RGB(255,0,0)); 
		 oldbrush=pDC->SelectObject(&brush); 
		 CPen pPen; 
		  
		 pPen.CreatePen(PS_SOLID,2,RGB(255,0,0)); 
		 pOldPn=pDC->SelectObject(&pPen); 
		 int x,y; 
		  for(int i=startbin;i<=endbin;i++) 
		  { 
			   coverse(azi, i, &x, &y); 
			   if(x>=0&&x<=this->m_TotalXPoint&&y>=0&&y<=this->m_TotalYPoint) 
			   { 
				   pDC->Ellipse(x-3,y-3,x+3,y+3); 
				   // pDC->SetPixel(x,y,RGB(255,255,255)); 
			   } 
		  } 
		  pDC->SelectObject(oldbrush); 
		  pDC->SelectObject( pOldPn); 
		  oldbrush->DeleteObject(); 
		  pOldPn->DeleteObject(); 
		  brush.DeleteObject(); 
		  pPen.DeleteObject(); 
 
	 } 
 
 
 
	 
} 
 
// 画预警区域 
void CRadarGraph::DrawAlertArea(CDC* pDC,CString filename,bool flag) 
{ 
     
	CFile file; 
	bool flag1=file.Open(filename,CFile::modeRead); 
		if(flag1==true&&file.GetLength()!=0) 
		{ 
			int index; 
			int startbin; 
			int endbin; 
			int azi; 
			int n1,n2,n3,n4; 
			while(true) 
			{ 
				n1=file.Read(&index,4); 
				n2=file.Read(&azi,4); 
				n3=file.Read(&startbin,4); 
				n4=file.Read(&endbin,4); 
				if(n1<4||n2<4||n3<4||n4<4) 
					break; 
                AlertZone( pDC, azi, startbin,  endbin,flag); 
			} 
            file.Close(); 
		} 
		 
		 
} 
 
void CRadarGraph::coverse(int azi, int bin, int* x, int* y) 
{ 
	m_ShowMaxRadiusBin=m_ShowMaxRadius*m_MaxBinNum/m_DataMaxRadius; 
	int radius=(int)(bin*m_Radius/m_ShowMaxRadiusBin); 
	int i,j; 
	if(0<=azi||azi<90) 
	{ 
		float jiao=azi*3.141593/180; 
		*x=(int)(radius*sin(jiao))+this->m_CenterPoint.x; 
		*y=this->m_CenterPoint.y-(int)(radius*cos(jiao)); 
	} 
	if(90<=azi||azi<180) 
	{ 
		float jiao=(azi-90)*3.141593/180; 
		*x=(int)(radius*cos(jiao))+this->m_CenterPoint.x; 
		*y=this->m_CenterPoint.y+(int)(radius*sin(jiao)); 
	} 
	if(180<=azi||azi<270) 
	{ 
		float jiao=(azi-180)*3.141593/180; 
		*x=this->m_CenterPoint.x-(int)(radius*sin(jiao)); 
		*y=this->m_CenterPoint.y+(int)(radius*cos(jiao)); 
	} 
	if(270<=azi||azi<360) 
	{ 
		float jiao=(azi-270)*3.141593/180; 
		*x=this->m_CenterPoint.x-(int)(radius*cos(jiao)); 
		*y=this->m_CenterPoint.y-(int)(radius*sin(jiao)); 
	} 
} 
 
 
void CRadarGraph::GraphLightAlert() 
{ 
	if(this->m_GraphAlertFlag==true) 
	SetTimer(1,500,NULL); 
	else 
	KillTimer(1); 
} 
 
 
void CRadarGraph::OnTimer(UINT_PTR nIDEvent) 
{ 
	// TODO: 在此添加消息处理程序代码和/或调用默认值 
 
	//CStatic::OnTimer(nIDEvent); 
	CDC *pDC = GetDC(); 
	switch(nIDEvent) 
	{ 
		case 1: 
		{ 
			m_lightflag=!m_lightflag; 
			 
	        DrawAlertArea(pDC,m_alertfilename,m_lightflag); 
 
			break; 
			 
		} 
	} 
	ReleaseDC(pDC); 
	 
}