www.pudn.com > RADAR-ALARM.rar > RadarAlert.cpp, change:2011-08-07,size:6114b


#include "StdAfx.h" 
#include "RadarAlert.h" 
#include <math.h> 
 
 
 
CRadarAlert::CRadarAlert(void) 
: m_BinContinualFlag(false) 
, m_AziContinualFlag(false) 
, m_MaxBinNum(0) 
, m_MaxElNum(0) 
, m_Value(NULL) 
, m_Threshold(30) 
, m_AlertBinNum(0) 
, m_AlertAziNum(0) 
, m_AreaThreshold(0) 
, m_ElIndex(0) 
, m_TotalAreaAlertFlag(false) 
, m_AlertTotalArea(0) 
, m_AlertRangeBin(460) 
, m_AlertAreaFileName(_T("alert")) 
, m_SingleAreaThreshold(0) 
{ 
} 
 
CRadarAlert::~CRadarAlert(void) 
{ 
} 
 
float CRadarAlert::GetValue(int El,int Azi,int Bin) 
{ 
	return m_Value[El*360*m_MaxBinNum+Azi*m_MaxBinNum+Bin]; 
} 
 
// 获取大于预警门限的区间 
void CRadarAlert::GetAlertSection(int AlertLenth, int Threshold,int Elindex) 
{ 
	unsigned short int nCount;//长度计数值 
	nCount=0; 
	unsigned short int BeginBinIndex; 
	unsigned short int EndBinIndex; 
	int num;//线段数量 
	AlertSection section; 
	num=0; 
    float value; 
		for(int j=0;j<360;j++) 
		{ 
			 
			nCount=0; 
			for(int k=0;k<m_AlertRangeBin;k++) 
			{    
				value=fabs(GetValue( Elindex,j,k)); 
				if(value>=Threshold&&value!=999&&value!=-999) 
				{ 
                    nCount++; 
 
				} 
				else 
				{    
					 
					EndBinIndex=k; 
					if(nCount>=AlertLenth) 
					{ 
						num++; 
						BeginBinIndex=EndBinIndex-nCount; 
						section.Azi=j; 
						section.StartBin=BeginBinIndex; 
						section.EndBin=EndBinIndex; 
						section.areaflag=false; 
						section.AreaIndex=0; 
						m_vect.push_back(section); 
						nCount=0; 
 
					} 
				} 
			} 
			 
		} 
	 
} 
 
void CRadarAlert::test(void) 
{ 
    FILE* fp; 
	fp=_tfopen(_T("test.txt"),_T("w")); 
	GetAlertSection(2,40,0); 
	 int out; 
	 out=m_vect.size(); 
	 for(int i=0;i<m_vect.size();i++) 
	 { 
		 fprintf(fp,"%d %d %d %d %d %d\n",i,m_vect[i].Azi,m_vect[i].StartBin,m_vect[i].EndBin,m_vect[i].areaflag,m_vect[i].AreaIndex); 
	 } 
	 fclose(fp); 
	 GetAlertArea(); 
	 MessageBox(NULL,_T("ok"),NULL,MB_OK); 
 
 
} 
 
// 两个警告线段是否有重合 
bool CRadarAlert::Match(int startbin1, int endbin1, int startbin2, int endbin2) 
{ 
	if(endbin1<=endbin2&&endbin1>=startbin2) 
		return true; 
	else if(startbin1>=startbin2&&startbin1<=endbin2) 
		return true; 
	else 
	return false; 
} 
 
 
 
// 获得报警区域 
void CRadarAlert::GetAlertArea() 
{    
 
    vector<AlertSection> m_vecttemp1; 
	vector<AlertSection> m_vecttemp360; 
    bool bFind; 
    int areaindex=0; 
	for(int i=0;i<m_vect.size();i++) 
	{   	 
		if(m_vect[i].areaflag==false) 
		{ 
			bFind=false; 
			for(int j=0;j<m_vect.size();j++) 
			{ 
				if(m_vect[j].areaflag==true) 
				{    
					bool match=Match(m_vect[i].StartBin,m_vect[i].EndBin,m_vect[j].StartBin,m_vect[j].EndBin); 
                   // bool aziflag=((abs(m_vect[i].Azi-m_vect[j].Azi)||abs(m_vect[i].Azi-m_vect[j].Azi)+360||abs(m_vect[i].Azi-m_vect[j].Azi)-360))==1; 
					 bool aziflag=(abs(m_vect[i].Azi-m_vect[j].Azi)==1)||(abs(m_vect[i].Azi-m_vect[j].Azi+360)==1)||(abs(m_vect[i].Azi-m_vect[j].Azi-360)==1); 
					if(match&&aziflag) 
					{ 
						m_vect[i].AreaIndex=m_vect[j].AreaIndex; 
						bFind=true; 
					} 
				} 
			} 
			if(bFind==false) 
			{ 
				areaindex++; 
				m_vect[i].AreaIndex=areaindex; 
			} 
 
			 
		}	 
		m_vect[i].areaflag=true; 
 
	} 
	for(int i=0;i<m_vect.size();i++) 
	{ 
		if(m_vect[i].Azi==1) 
		{ 
             m_vecttemp1.push_back(m_vect[i]); 
		} 
		if(m_vect[i].Azi==359) 
		{ 
             m_vecttemp360.push_back(m_vect[i]); 
		} 
 
	} 
	for(int i=0;i<m_vecttemp360.size();i++) 
	{ 
		for(int j=0;j<m_vecttemp1.size();j++) 
		{ 
		   bool match=Match(m_vecttemp360[i].StartBin,m_vecttemp360[i].EndBin,m_vecttemp1[j].StartBin,m_vecttemp1[j].EndBin); 
           if(match) 
		   { 
			 for(int k=0;k<m_vect.size();k++) 
			 { 
				 if(m_vect[k].AreaIndex==m_vecttemp360[i].AreaIndex) 
					 m_vect[k].AreaIndex=m_vecttemp1[j].AreaIndex; 
			 } 
		   } 
		} 
	} 
     
 
	 
	int minazi=999; 
	int maxazi=-999; 
	int minbin=999; 
	int maxbin=-999; 
	int area=0; 
	int index=0; 
 
    for(int i=1;i<areaindex+1;i++) 
	{ 
		for(int j=0;j<m_vect.size();j++) 
		{ 
			if(m_vect[j].AreaIndex==i) 
			{ 
				if(m_vect[j].Azi<=minazi) minazi=m_vect[j].Azi; 
				if(m_vect[j].Azi>=maxazi) maxazi=m_vect[j].Azi; 
				if(m_vect[j].StartBin<=minbin) minbin=m_vect[j].StartBin; 
				if(m_vect[j].EndBin>=maxbin) maxbin=m_vect[j].EndBin; 
				area=area+(m_vect[j].EndBin-m_vect[j].StartBin); 
				index=i; 
				 
			} 
 
		} 
		AreaInfo info; 
		info.MaxAzi=maxazi; 
		info.MinAzi=minazi; 
		info.MaxBin=maxbin; 
		info.MinBin=minbin; 
		info.Area=area; 
		info.index=index; 
		m_vectarea.push_back(info); 
		area=0; 
		minazi=999; 
	    maxazi=-999; 
	    minbin=999; 
	    maxbin=-999; 
		 
	} 
 
	 
} 
 
 
void CRadarAlert::CalibrationA(int areathreshold, int azithreshold) 
{ 
     vector<AreaInfo>::iterator itr;  
	for(itr = m_vectarea.begin();itr!=m_vectarea.end();) 
	 { 
		 if((*itr).Area<areathreshold||((*itr).MaxAzi-(*itr).MinAzi+1)<(azithreshold)) 
		 { 
			 itr=m_vectarea.erase(itr); 
		 } 
		 else 
			 ++itr; 
 
	 } 
 
	{ 
	 CFile file; 
			file.Open(this->m_AlertAreaFileName,CFile::modeWrite|CFile::modeCreate); 
			for(int i=0;i<m_vectarea.size();i++) 
		{ 
			for(int j=0;j<m_vect.size();j++) 
			{ 
				 
	           
			   if(m_vectarea[i].index==m_vect[j].AreaIndex) 
			   { 
				   file.Write(&(m_vect[j].AreaIndex),4); 
				   file.Write(&(m_vect[j].Azi),4); 
				   file.Write(&(m_vect[j].StartBin),4); 
				   file.Write(&(m_vect[j].EndBin),4); 
			   } 
 
			} 
		} 
		file.Close(); 
	} 
	 
 
} 
 
// 获取报警总面积 
int CRadarAlert::GetTotalArea(void) 
{ 
	int totalarea=0; 
	for(int i=0;i<m_vectarea.size();i++) 
	 { 
		 totalarea=m_vectarea[i].Area+totalarea; 
	 } 
	return totalarea; 
} 
 
// 总面积报警 
bool CRadarAlert::AlertTotalArea(int ThresholdArea) 
{ 
	if(GetTotalArea()>=ThresholdArea) 
		return true; 
	else  
		return false; 
} 
 
// 报警总过程 
void CRadarAlert::AlertProcess(void) 
{ 
     GetAlertSection(m_AlertBinNum,m_Threshold,m_ElIndex); 
	 GetAlertArea(); 
	 this->CalibrationA(this->m_SingleAreaThreshold,m_AlertAziNum); 
	 m_AlertTotalArea=GetTotalArea(); 
 
 
}