www.pudn.com > DEM_TDS.rar > DEMdata.cpp, change:2007-07-12,size:11454b


// DEMdata.cpp: implementation of the DEMdata class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "DEM.h" 
#include "DEMdata.h" 
#include "OpenGLText.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
DEMdata::DEMdata() 
{ 
 data=NULL; 
 color=NULL; 
 DataMark=""; 
 ValueType=""; 
 mapcolor="linescolor"; 
 cr1=RGB(255,255,255); 
 cr2=RGB(0,0,0); 
 arrow_size=0.01; 
 arrow_dis=0.05; 
} 
 
DEMdata::~DEMdata() 
{ 
 DeleteDEM(); 
} 
 
void DEMdata::DeleteDEM() 
{ 
 for(int i=0;i<Row;i++){if(data[i])delete [] data[i];data[i]=NULL;} 
 if(data)delete [] data;data=NULL; 
 for(i=0;i<Row;i++){if(color[i])delete [] color[i];color[i]=NULL;} 
 if(color)delete []color;color=NULL; 
} 
 
 
bool DEMdata::ReadDEMdata(ifstream &is) 
{ 
	ReadHead(is); 
	Readdata(is); 
	is.close(); 
	matchcolor(); 
	return true; 
} 
 
void DEMdata::ReadHead(ifstream &is) 
{ 
	bool newdata; 
	char linedata[100]; 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	CString getdata=linedata; 
	if(getdata.Find("DataMark")!=-1)newdata=true; 
	else newdata=false; 
 
	is.seekg(0,ios::beg); 
	if(newdata)is.getline(linedata,sizeof(linedata)-1-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	DataMark=linedata; 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&Version); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	Unit=is.get(); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	else is.getline(linedata,sizeof(linedata)-1,'\n'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&Alpha); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&Compress); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&X0); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&Y0); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&DX); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%f",&DY); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%d",&Row); 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%d",&Col); 
 
	if(newdata) 
	{ 
		is.getline(linedata,sizeof(linedata)-1,':'); 
		is.getline(linedata,sizeof(linedata)-1,'\n'); 
		ValueType=linedata; 
	} 
 
	if(newdata)is.getline(linedata,sizeof(linedata)-1,':'); 
	is.getline(linedata,sizeof(linedata)-1,'\n'); 
	sscanf(linedata,"%d",&Hzoom); 
	SizeX=Col-1;SizeY=Row-1; 
} 
 
void DEMdata::Readdata(ifstream &is) 
{ 
	char linedata[10]; 
 
//	data=new int[Row][Col]; 
 
	data=new int*[Row]; 
	for(int i=0;i<Row;i++)data[i]=new int[Col]; 
 
	for(i=0;i<Row;i++) 
		for(int j=0;j<Col;j++) 
		{ 
			is.getline(linedata,sizeof(linedata)-1,' '); 
			if(sscanf(linedata,"%d",&data[i][j])!=1)j--; 
		} 
 
} 
 
int DEMdata::GetSizeX() 
{ 
return Col-1; 
} 
 
int DEMdata::GetSizeY() 
{ 
return Row-1; 
} 
 
void DEMdata::GetSizeZ(int &minH,int &maxH,int &sizeH) 
{ 
	minH=data[0][0];maxH=minH; 
for(int i=0;i<Row;i++) 
	for(int j=0;j<Col;j++) 
	{ 
		if(data[i][j]<minH)minH=data[i][j]; 
		else if(data[i][j]>maxH)maxH=data[i][j]; 
	} 
	sizeH=maxH-minH; 
} 
 
void DEMdata::SetH() 
{ 
	GetSizeZ(this->MinH,this->MaxH,this->SizeH); 
} 
 
void DEMdata::DrawDEM(CDC *pDC) 
{ 
	for(int i=0;i<Row-1;i++) 
		for(int j=0;j<Col-1;j++) 
		{ 
			CPoint point(j,SizeY-1-i); 
			pDC->SetPixel(point,GetAveColor(i,j)); 
		} 
} 
 
COLORREF DEMdata::GetAveColor(int i, int j) 
{ 
	int R,G,B; 
	R=(GetRValue(color[i][j])+GetRValue(color[i][j])+GetRValue(color[i][j])+GetRValue(color[i][j]))/4; 
	G=(GetGValue(color[i][j])+GetGValue(color[i][j])+GetGValue(color[i][j])+GetGValue(color[i][j]))/4; 
	B=(GetBValue(color[i][j])+GetBValue(color[i][j])+GetBValue(color[i][j])+GetBValue(color[i][j]))/4; 
	return RGB(R,G,B); 
} 
 
COLORREF DEMdata::Get2ColorGraduateWithH(float aveH,COLORREF cr1,COLORREF cr2) 
{ 
  int   r1   =   GetRValue(cr1);    
  int   g1   =   GetGValue(cr1);    
  int   b1   =   GetBValue(cr1);    
     
  int   r2   =   GetRValue(cr2);    
  int   g2   =   GetGValue(cr2);    
  int   b2   =   GetBValue(cr2);    
 
  float dr=(float)(r2-r1)/(float)SizeH; 
  float dg=(float)(g2-g1)/(float)SizeH; 
  float db=(float)(b2-b1)/(float)SizeH; 
 
  int r=r1+(int)(dr*(aveH-(float)MinH)); 
  int g=g1+(int)(dg*(aveH-(float)MinH)); 
  int b=b1+(int)(db*(aveH-(float)MinH)); 
  return RGB(r,g,b); 
} 
 
 
COLORREF DEMdata::Get2ColorGraduateWithH(int linecode, COLORREF cr1, COLORREF cr2) 
{ 
  if(linecode>1020)return RGB(255,255,255); 
  int   r1   =   GetRValue(cr1);    
  int   g1   =   GetGValue(cr1);    
  int   b1   =   GetBValue(cr1);    
     
  int   r2   =   GetRValue(cr2);    
  int   g2   =   GetGValue(cr2);    
  int   b2   =   GetBValue(cr2);    
 
  float dr=(float)(r2-r1)/(float)1020; 
  float dg=(float)(g2-g1)/(float)1020; 
  float db=(float)(b2-b1)/(float)1020; 
 
  int r=r1+(int)(dr*(float)linecode); 
  int g=g1+(int)(dg*(float)linecode); 
  int b=b1+(int)(db*(float)linecode); 
  return RGB(r,g,b); 
} 
 
 
COLORREF DEMdata::GetLinesColorWithH(float aveH) 
{ 
 int linecode=(int)((float)1020*(aveH-(float)MinH)/(float)SizeH); 
  return GetLinesColor(linecode); 
} 
 
COLORREF DEMdata::GetLinesColor(int linecode) 
{ 
 if(linecode<=115)return RGB(0,0,140+linecode); 
  else if(linecode<=370)return RGB(0,linecode-115,255); 
  else if(linecode<=625)return RGB(linecode-370,255,625-linecode); 
  else if(linecode<=880)return RGB(255,880-linecode,0); 
  else return RGB(1135-linecode,0,0); 
} 
 
void DEMdata::matchcolor() 
{ 
	if(mapcolor=="spring"){cr1=RGB(255,0,255);cr2=RGB(255,255,0);} 
	else if(mapcolor=="summer"){cr1=RGB(0,130,90);cr2=RGB(255,255,99);} 
	else if(mapcolor=="fall"){cr1=RGB(255,255,0);cr2=RGB(255,0,0);} 
	else if(mapcolor=="winter"){cr1=RGB(0,0,255);cr2=RGB(0,255,132);} 
	else if(mapcolor=="blackwhite"){cr1=RGB(255,255,255);cr2=RGB(0,0,0);} 
	else if(mapcolor=="linescolor"){cr1=RGB(0,0,255);cr2=RGB(255,0,0);} 
 
	color=new COLORREF*[Row]; 
	for(int i=0;i<Row;i++)color[i]=new COLORREF[Col]; 
	for(i=0;i<Row;i++) 
		for(int j=0;j<Col;j++) 
		{ 
			if(mapcolor=="linescolor")color[i][j]=GetLinesColorWithH((float)data[i][j]); 
			else color[i][j]=Get2ColorGraduateWithH((float)data[i][j],cr1,cr2); 
		} 
} 
 
 
COLORREF DEMdata::GetLengendColor(int linecode) 
{ 
  	if(mapcolor=="linescolor")return GetLinesColor(linecode); 
	else return Get2ColorGraduateWithH(linecode,cr1,cr2); 
 
} 
 
void DEMdata::DrawAxis() 
{ 
	GLdouble sx=1.0/((GLdouble)SizeX*DX),sy=1.0/((GLdouble)SizeY*DY); 
	s=sx<sy?sx:sy; 
	Com_3D=sx<sy?SizeX:SizeY; 
 
	::glNewList(1,GL_COMPILE); 
	glShadeModel(GL_SMOOTH); 
	// 绘制三个坐标轴 
	glBegin(GL_LINES); 
		// 绘制红色的x轴 
		glColor3f(1.f,0.f,0.f); 
		glVertex3d(0.0,0.0,0.0); 
		glVertex3d(s*SizeX*DX+arrow_dis,0.0,0.0); 
		glVertex3d(s*SizeX*DX+arrow_dis,0.0,0.0); 
		glVertex3d(s*SizeX*DX+arrow_dis-arrow_size,arrow_size,0.0); 
		glVertex3d(s*SizeX*DX+arrow_dis,0.0,0.0); 
		glVertex3d(s*SizeX*DX+arrow_dis-arrow_size,-arrow_size,0.0); 
		// 绘制绿色的y轴 
		glColor3f(0.f,1.f,0.f); 
		glVertex3d(0.0,0.0,0.0); 
		glVertex3d(0.0,s*SizeY*DY+arrow_dis,0.0); 
		glVertex3d(0.0,s*SizeY*DY+arrow_dis,0.0); 
		glVertex3d(arrow_size,s*SizeY*DY+arrow_dis-arrow_size,0.0); 
		glVertex3d(0.0,s*SizeY*DY+arrow_dis,0.0); 
		glVertex3d(-arrow_size,s*SizeY*DY+arrow_dis-arrow_size,0.0); 
		// 绘制蓝色的z轴 
		glColor3f(0.f,0.f,1.f); 
		glVertex3d(0.0,0.0,0.0); 
		glVertex3d(0.0,0.0,SizeH/Hzoom*s+arrow_dis); 
		glVertex3d(0.0,0.0,SizeH/Hzoom*s+arrow_dis); 
		glVertex3d(0.0,arrow_size,SizeH/Hzoom*s+arrow_dis-arrow_size); 
		glVertex3d(0.0,0.0,SizeH/Hzoom*s+arrow_dis); 
		glVertex3d(0.0,-arrow_size,SizeH/Hzoom*s+arrow_dis-arrow_size); 
	glEnd(); 
	::glEndList(); 
} 
 
void DEMdata::Draw3DSurf() 
{ 
	::glNewList(4,GL_COMPILE); 
	glShadeModel(GL_SMOOTH); 
 
	for(int i=0;i<Row-1;i++) 
		for(int j=0;j<Col-1;j++) 
		{ 
		glBegin(GL_POLYGON); 
//		glNormal3d(0.0,1.0,1.0); 
		glColor3ub(GetRValue(color[i][j]),GetGValue(color[i][j]),GetBValue(color[i][j])); 
		glVertex3d(j*s*DX,i*s*DY,data[i][j]*s/Hzoom); 
		glVertex3d(j*s*DX,(i+1)*s*DY,data[i+1][j]*s/Hzoom); 
		glVertex3d((j+1)*s*DX,(i+1)*s*DY,data[i+1][j+1]*s/Hzoom); 
		glVertex3d((j+1)*s*DX,i*s*DY,data[i][j+1]*s/Hzoom); 
		glEnd(); 
		} 
	::glEndList(); 
} 
 
	 
 
void DEMdata::Draw3DMesh() 
{ 
	::glNewList(5,GL_COMPILE); 
	glShadeModel(GL_SMOOTH); 
	glBegin(GL_LINES); 
	for(int i=0;i<Row-1;i++) 
		for(int j=0;j<Col-1;j++) 
		{ 
		glColor3ub(GetRValue(color[i][j]),GetGValue(color[i][j]),GetBValue(color[i][j])); 
		glVertex3d(j*s*DX,i*s*DY,data[i][j]*s/Hzoom); 
		glColor3ub(GetRValue(color[i+1][j]),GetGValue(color[i+1][j]),GetBValue(color[i+1][j])); 
		glVertex3d(j*s*DX,(i+1)*s*DY,data[i+1][j]*s/Hzoom); 
		glColor3ub(GetRValue(color[i][j]),GetGValue(color[i][j]),GetBValue(color[i][j])); 
		glVertex3d(j*s*DX,i*s*DY,data[i][j]*s/Hzoom); 
		glColor3ub(GetRValue(color[i][j+1]),GetGValue(color[i][j+1]),GetBValue(color[i][j+1])); 
		glVertex3d((j+1)*s*DX,i*s*DY,data[i][j+1]*s/Hzoom); 
		} 
	for(i=0;i<Row-1;i++) 
		{ 
		glColor3ub(GetRValue(color[i][Col-1]),GetGValue(color[i][Col-1]),GetBValue(color[i][Col-1])); 
		glVertex3d((Col-1)*s*DX,i*s*DY,data[i][Col-1]*s/Hzoom); 
		glColor3ub(GetRValue(color[i+1][Col-1]),GetGValue(color[i+1][Col-1]),GetBValue(color[i+1][Col-1])); 
		glVertex3d((Col-1)*s*DX,(i+1)*s*DY,data[i+1][Col-1]*s/Hzoom); 
		} 
	for(int j=0;j<Col-1;j++) 
		{ 
		glColor3ub(GetRValue(color[Row-1][j]),GetGValue(color[Row-1][j]),GetBValue(color[Row-1][j])); 
		glVertex3d(j*s*DX,(Row-1)*s*DY,data[(Row-1)][j]*s/Hzoom); 
		glColor3ub(GetRValue(color[Row-1][j+1]),GetGValue(color[Row-1][j+1]),GetBValue(color[Row-1][j+1])); 
		glVertex3d((j+1)*s*DX,(Row-1)*s*DY,data[(Row-1)][j+1]*s/Hzoom); 
		} 
	glEnd(); 
	::glEndList(); 
} 
 
void DEMdata::Draw3DSkirt() 
{ 
	::glNewList(6,GL_COMPILE); 
	glShadeModel(GL_SMOOTH); 
	glBegin(GL_LINES); 
	for(int i=0;i<Row-1;i++) 
		{ 
		glColor3ub(GetRValue(color[i][Col-1]),GetGValue(color[i][Col-1]),GetBValue(color[i][Col-1])); 
		glVertex3d((Col-1)*s*DX,i*s*DY,data[i][Col-1]*s/Hzoom); 
		glVertex3d((Col-1)*s*DX,i*s*DY,0); 
		} 
	for(int j=0;j<Col-1;j++) 
		{ 
		glColor3ub(GetRValue(color[Row-1][j]),GetGValue(color[Row-1][j]),GetBValue(color[Row-1][j])); 
		glVertex3d(j*s*DX,(Row-1)*s*DY,data[(Row-1)][j]*s/Hzoom); 
		glVertex3d(j*s*DX,(Row-1)*s*DY,0); 
		} 
	for(i=0;i<Row-1;i++) 
		{ 
		glColor3ub(GetRValue(color[i][0]),GetGValue(color[i][0]),GetBValue(color[i][0])); 
		glVertex3d(0,i*s*DY,data[i][0]*s/Hzoom); 
		glVertex3d(0,i*s*DY,0); 
		} 
	for(j=0;j<Col-1;j++) 
		{ 
		glColor3ub(GetRValue(color[0][j]),GetGValue(color[0][j]),GetBValue(color[0][j])); 
		glVertex3d(j*s*DX,0,data[0][j]*s/Hzoom); 
		glVertex3d(j*s*DX,0,0); 
		} 
	glEnd(); 
	::glEndList(); 
}