⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delaunayview.cpp

📁 daunay三角网格生成 相当好的一个算法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
				   glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
				   glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);		
    			
				   normal=GetTriNormal(m_hct[2],m_hct[3],m_hct[8]);
				   glNormal3d(normal.x,normal.y,normal.z);
				   glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z); 	
				   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
				   glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
    			glEnd();
			}
			else
			{
			   glBegin(GL_TRIANGLES);//anticlock		   
			   normal=GetTriNormal(m_hct[0],m_hct[2],m_hct[1]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[0].x,m_hct[0].y,m_hct[0].z); 	    	   
			   glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
			   glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
			   
			   normal=GetTriNormal(m_hct[1],m_hct[2],m_hct[3]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
			   glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
			   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);

			   normal=GetTriNormal(m_hct[1],m_hct[3],m_hct[4]);
			   glNormal3d(normal.x,normal.y,normal.z);		   
			   glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
			   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
			   glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);

			   normal=GetTriNormal(m_hct[4],m_hct[3],m_hct[5]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);
			   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
			   glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
			   
			   normal=GetTriNormal(m_hct[4],m_hct[5],m_hct[6]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);	   
			   glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
    		   glVertex3d(m_hct[6].x,m_hct[6].y,m_hct[6].z);

			   normal=GetTriNormal(m_hct[7],m_hct[8],m_hct[9]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z); 	
    		   glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
			   glVertex3d(m_hct[9].x,m_hct[9].y,m_hct[9].z);

			   normal=GetTriNormal(m_hct[3],m_hct[8],m_hct[7]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);		
			   glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
			   glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);

			   normal=GetTriNormal(m_hct[3],m_hct[7],m_hct[5]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
			   glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);
			   glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);		
    		
			   normal=GetTriNormal(m_hct[3],m_hct[2],m_hct[8]);
			   glNormal3d(normal.x,normal.y,normal.z);
			   glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z); 	
			   glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
			   glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
    			glEnd();
			}//if else
		}//for*/
	}//if
	if(m_Dimension==DO_DRAW2 && m_HCT==DO_HCT)
	{
		for(i=1;i<=3;i++)
		{
			HCT(tri,i);
			glColor4d(0.0,0.0,1.0,0.5);	
			glBegin(GL_TRIANGLES);//anticlock
			   glVertex3d(m_hct[0].x,m_hct[0].y,0); 	    	   
			   glVertex3d(m_hct[1].x,m_hct[1].y,0);
			   glVertex3d(m_hct[2].x,m_hct[2].y,0); 
			
			   glVertex3d(m_hct[1].x,m_hct[1].y,0); 
			   glVertex3d(m_hct[2].x,m_hct[2].y,0); 
			   glVertex3d(m_hct[3].x,m_hct[3].y,0); 

			   glVertex3d(m_hct[3].x,m_hct[3].y,0); 
			   glVertex3d(m_hct[1].x,m_hct[1].y,0); 
			   glVertex3d(m_hct[4].x,m_hct[4].y,0); 

			   glVertex3d(m_hct[3].x,m_hct[3].y,0); 
			   glVertex3d(m_hct[4].x,m_hct[4].y,0); 
			   glVertex3d(m_hct[5].x,m_hct[5].y,0); 

			   glVertex3d(m_hct[4].x,m_hct[4].y,0); 
			   glVertex3d(m_hct[6].x,m_hct[6].y,0); 
    		   glVertex3d(m_hct[5].x,m_hct[5].y,0); 

			   glVertex3d(m_hct[7].x,m_hct[7].y,0);  	
    		   glVertex3d(m_hct[8].x,m_hct[8].y,0); 
			   glVertex3d(m_hct[9].x,m_hct[9].y,0); 

			   glVertex3d(m_hct[8].x,m_hct[8].y,0); 	
			   glVertex3d(m_hct[3].x,m_hct[3].y,0); 
			   glVertex3d(m_hct[7].x,m_hct[7].y,0); 
 
			   glVertex3d(m_hct[3].x,m_hct[3].y,0); 
			   glVertex3d(m_hct[5].x,m_hct[5].y,0); 
			   glVertex3d(m_hct[7].x,m_hct[7].y,0); 
    
			   glVertex3d(m_hct[2].x,m_hct[2].y,0); 
			   glVertex3d(m_hct[3].x,m_hct[3].y,0); 
			   glVertex3d(m_hct[8].x,m_hct[8].y,0); 
    		glEnd();
		}
	}
}

int CDelaunayView::DelTriList(double x, double y,int p)
{//record the position of triangles which are to be deleted
	//'x' and 'y' are the coordinates of the insert point
	//'p' is the mark or th insert point in 'm_point'
		
		CPointPos *point=new CPointPos(x,y);
		int j=0,i=0,k=0,max=0;
		array a;
		// GetInitEdges : ransack each trangle to record it's edges
		//when the piont belong it's circle
	    k=m_pDoc->GetInitEdges(x,y,p);// the returned value 'k/2' is the number of cirecle the point belonged??
		if(k==POS_ERROR){
			return POS_ERROR;
		}
		//默认:插入点属于某个外接圆,且属于凸包
          //judge a point belong to the convexity or not
		  a=Wher(point);
		  /*return Value:a.a
	POS_IN=1,a point  belong a triangle or convexity
	POS_ON=2,a point belong a circle or on the edge of convexity
	POS_OUT=0,a point out of a circle or convexity
	a.b , a.c: record the point's mark in m_con */
		   max=m_pDoc->m_con.GetSize();
		  if(0==a.a && k>=2)//插入点属于某个外接圆,但不属于凸包
		  {  //delete an edge "belong to" the inserted polygon,the edge is also 
			 //belong  to the border of the convexity
			 if (max==3){
				int t=m_pDoc->m_edge.GetSize();			      
				for(j=0;j<t;j++){
					if((m_pDoc->m_edge[j]->m_p1==m_pDoc->m_con[a.b]) & 
					   (m_pDoc->m_edge[j]->m_p2==m_pDoc->m_con[a.c]))
				   {
					   m_pDoc->m_edge.RemoveAt(j,1);
					   m_pDoc->m_con.InsertAt(a.c,p);
					   break;
				   }
				}
			 }
			 else
			 {
			    CWordArray con_index;//save the point's mark can be saw on the con	
			    for(i=0;i<max-1;i++)
				{
				   int t=m_pDoc->m_edge.GetSize();			      
				   for(j=0;j<t;j++){
					  if((m_pDoc->m_edge[j]->m_p1==m_pDoc->m_con[i]) & 
					   (m_pDoc->m_edge[j]->m_p2==m_pDoc->m_con[i+1]))
					  {
						  bool y=m_pDoc->DelEdgeOrNot(m_pDoc->m_edge[j]->m_p1,m_pDoc->m_edge[j]->m_p2,p);
                          if(y){
					              m_pDoc->m_edge.RemoveAt(j,1);
					              con_index.Add(i);
					              con_index.Add(i+1);					   
								  break;
						  }
					  }
				   }
				}//end for i
                int t=m_pDoc->m_edge.GetSize();			      
			    for(j=0;j<t;j++){       
				   if((m_pDoc->m_edge[j]->m_p1==m_pDoc->m_con[max-1]) && 
					   (m_pDoc->m_edge[j]->m_p2==m_pDoc->m_con[0]))
				   {
					    bool y=m_pDoc->DelEdgeOrNot(m_pDoc->m_edge[j]->m_p1,m_pDoc->m_edge[j]->m_p2,p);
                        if(y){
					              m_pDoc->m_edge.RemoveAt(j,1);
					              con_index.Add(max-1);
					              con_index.Add(0);					   
								  break;
						}					 					  
				   }			
				}
				t=con_index.GetSize();
			    m_pDoc->EditCon(con_index[t-1],con_index[0],p);				
             }	
		  }
		  if(k==0){//插入点不属于任意一个外接圆,显然也不属于凸包
			  CBorder * m_border;
			  m_border=new CBorder(m_pDoc->m_con[a.b],m_pDoc->m_con[a.c]);
			  m_pDoc->m_edge.Add(m_border);
			  m_pDoc->EditCon(a.c,a.b,p);
		  }	
		   //Delete triangles that have been marked
		  m_pDoc->DelTriMarked();
		  //add new triangles
	      m_pDoc->AddTriangle(p);
		  max=m_pDoc->m_index.GetSize();
		  m_pDoc->m_index.RemoveAt(0,max);//RemoveAll : no use ???
		  m_pDoc->m_edge.RemoveAll();
}

void CDelaunayView::OnDestroy() 
{
	HGLRC hrc;
	hrc=::wglGetCurrentContext();
	::wglMakeCurrent(NULL,NULL);
	if(m_pDC)
		delete m_pDC;
	CView::OnDestroy();
}

void CDelaunayView::DrawScene()
{	
	glClearColor(1.0f,1.0f,1.0f,1.0f);//setup backcolor
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //GL_COLOR_BUFFER_BIT 用当前设置的backcolor 第一次清除 color buffer
	// GL_DEPTH_BUFFER_BIT清除 depth buffer,以便进行depth test and 消除隐藏面
	glClearDepth(1.0f);
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);
	if(m_bMaterial){
		CreateMaterial();
		CreateLights();
	}
	glColorMask(!m_bMaskRed,!m_bMaskGreen,!m_bMaskBlue,GL_TRUE);
	glColor3f(m_colorRed,m_colorGreen,m_colorBlue);
	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();
	glTranslated(m_translate_x,m_translate_y,m_translate_z);	
	if(m_rotate_angle!=0.0){
		glRotated(m_rotate_angle,m_gl_x,m_gl_y,m_gl_z);
	}
	glScaled(2,2,0);
//	glScaled((cx-2*m_ox)/cx,(cy-2*m_oy)/cy,0.0);
	if(m_Dimension==DO_DRAW3){
	//	glPushMatrix();
	    gluLookAt(10.0,11.0,12.0,0.0,0.0,0.0,0.0,0.0,1.0);//eye.x,eye.y,eye.z
		
	//	glPopMatrix();
	}
	glBegin(GL_LINE_STRIP);
           glVertex3d(0.0,0.0,0.0);
		   glVertex3d(0.0,1.0,0.0);
		   glVertex3d(1.0,1.0,0.0);
		   glVertex3d(1.0,0.0,0.0);
		   glVertex3d(0.0,0.0,0.0);
    glEnd(); 
	glPushMatrix();
	if(m_Dimension==DO_DRAW2)
		DrawPoints();
	if(m_Draw_what==DO_WANG && m_Dimension==DO_DRAW3)
		m_pDoc->Wang();
	else	
        DrawTris();
	glPopMatrix();
	glTranslated(0.5,0.5,0.0);

//	glPushMatrix();

//	auxSolidSphere(0.5);

//	glPopMatrix();

	glDisable(GL_LIGHTING);
	glDisable(GL_LIGHT0);
	glDisable(GL_DEPTH_TEST);
//	glFinish();
}

void CDelaunayView::InitOpenGl()
{
	PIXELFORMATDESCRIPTOR pfd;
    int n;
	HGLRC hrc;
	m_pDC=new CClientDC(this);
	ASSERT(m_pDC!=NULL);
	if(!bSetupPixelFormat()) return;
	n=::GetPixelFormat(m_pDC->GetSafeHdc());
	::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);
	hrc=wglCreateContext(m_pDC->GetSafeHdc());
	//create a instance of RC: hrc
	wglMakeCurrent(m_pDC->GetSafeHdc(),hrc);
	//make hrc a current Rendering context of a thread
	GetClientRect(&m_oldRect);//get size of ClientRect
}

BOOL CDelaunayView::bSetupPixelFormat()
{
	static PIXELFORMATDESCRIPTOR pfd={
		sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW |
			PFD_SUPPORT_OPENGL |
			PFD_DOUBLEBUFFER,
			PFD_TYPE_RGBA,
			24,//24位真彩
			0,0,0,0,0,0,
			0,
			0,
			0,
			0,0,0,0,
			32,//32-bit z-buffer,可从物体中消除隐藏面
			0,
			0,
			PFD_MAIN_PLANE,//选择主层面
			0,
			0,0,0
	};
	int pixelformat;
	
	if((pixelformat=ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd))==0)
	{
		MessageBox("ChoosePixelFormat failed");
		return FALSE;
	}
		
	if(SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)
	{
		MessageBox("SetPixelFormat failed");
		return FALSE;
	}
		
	return TRUE;
	//CClientDC clientdc(this);
	//int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
	//BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
	//hglrc=wglCreateContext(clientdc.m_hDC);
}



void CDelaunayView::DrawPoint(int i)
{
	double x,y,z;
	x=m_pDoc->m_point[i]->m_x;
	y=m_pDoc->m_point[i]->m_y;
	z=m_pDoc->m_point[i]->m_z;
    glBegin(GL_POINTS);
	glVertex3d(m_pDoc->m_point[i]->m_x,m_pDoc->m_point[i]->m_y,0.0);
    glEnd();
}

void CDelaunayView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	switch(nChar)
	{
	case VK_LEFT:
		m_translate_x=m_translate_x-0.2;
		break;
	case VK_RIGHT:
	    m_translate_x=m_translate_x+0.2;
		break;
	case VK_UP:
		m_translate_y=m_translate_y+0.1;
        break;
	case VK_DOWN:
		m_translate_y=m_translate_y-0.2;
		break;
	case 88://x
		if(m_gl_x==0.0){ 
            m_gl_x=0.1;
		}
		else{
			m_gl_x=0.0;
		}
		break;
	case 89://y
		if(m_gl_y==0.0){ 
            m_gl_y=0.1;
		}
		else{
			m_gl_y=0.0;
		}
		break;
	case 90://z
		if(m_gl_z==0.0){ 
            m_gl_z=0.1;
		}
		else{
			m_gl_z=0.0;
		}
		break;
	case 100://小键盘的left
		m_rotate_angle=m_rotate_angle-10.0;
		break;
	case 102://小键盘的right
		m_rotate_angle=m_rotate_angle+10.0;
		break;
	case 104://小键盘的up
		m_rotate_angle=m_rotate_angle+90.0;
		break;
	case 98://小键盘的down
		m_rotate_angle=m_rotate_angle-90.0;
		break;
	case 65://a
		eye.x=eye.x+1.0;
		break;
    case 83://s
		eye.y=eye.y+1.0;
		break;
    case 68://d
		eye.z=eye.z+1.0;
		break;
	default:
		break;
	}	
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
    Invalidate();
}

void CDelaunayView::DrawTris()
{
	POSITION POS;
	CTriangle* pTriangle;
	POS = m_pDoc->m_tri.GetHeadPosition();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -