📄 delaunayview.cpp
字号:
if(m_Draw_what==DO_LINE)
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
else{
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
}
while(POS != NULL ){
DrawTri(m_pDoc->m_tri.GetAt(POS));
pTriangle=m_pDoc->m_tri.GetNext(POS);
}
}
void CDelaunayView::DrawPoints()
{
int max=m_pDoc->m_point.GetSize();
glPointSize(6.0);
for(int i=0;i<max;i++)
{
glColor4d(double(i)/double(max),1.0,0.0,0.0);
DrawPoint(i);
}
}
void CDelaunayView::OnCorlorMaterial()
{
CLightMaterial dlg;
dlg.m_colorRed=m_colorRed;
dlg.m_colorGreen=m_colorGreen;
dlg.m_colorBlue=m_colorBlue;
dlg.m_bMaskRed=m_bMaskRed;
dlg.m_bMaskGreen=m_bMaskGreen;
dlg.m_bMaskBlue=m_bMaskBlue;
dlg.m_matAmbientR=m_materialAmb[0];
dlg.m_matAmbientG=m_materialAmb[1];
dlg.m_matAmbientB=m_materialAmb[2];
dlg.m_matDiffuseR=m_materialDif[0];
dlg.m_matDiffuseG=m_materialDif[1];
dlg.m_matDiffuseB=m_materialDif[2];
dlg.m_matSpecularR=m_materialSpe[0];
dlg.m_matSpecularG=m_materialSpe[1];
dlg.m_matSpecularB=m_materialSpe[2];
dlg.m_matEmissionR=m_materialEmi[0];
dlg.m_matEmissionG=m_materialEmi[1];
dlg.m_matEmissionB=m_materialEmi[2];
dlg.m_matShininess=m_matshininess;
dlg.m_bMaterialOnOff=m_bMaterial;
int responeDlg=dlg.DoModal();
if(responeDlg==IDOK){
m_colorRed=dlg.m_colorRed;
m_colorGreen=dlg.m_colorGreen;
m_colorBlue=dlg.m_colorBlue;
m_bMaskRed=dlg.m_bMaskRed;
m_bMaskGreen=dlg.m_bMaskGreen;
m_bMaskBlue=dlg.m_bMaskBlue;
m_materialAmb[0]=dlg.m_matAmbientR;
m_materialAmb[1]=dlg.m_matAmbientG;
m_materialAmb[2]=dlg.m_matAmbientB;
m_materialDif[0]=dlg.m_matDiffuseR;
m_materialDif[1]=dlg.m_matDiffuseG;
m_materialDif[2]=dlg.m_matDiffuseB;
m_materialSpe[0]=dlg.m_matSpecularR;
m_materialSpe[1]=dlg.m_matSpecularG;
m_materialSpe[2]=dlg.m_matSpecularB;
m_materialEmi[0]=dlg.m_matEmissionR;
m_materialEmi[1]=dlg.m_matEmissionG;
m_materialEmi[2]=dlg.m_matEmissionB;
m_matshininess=dlg.m_matShininess;
m_bMaterial=dlg.m_bMaterialOnOff;
Invalidate();
}
}
void CDelaunayView::OnUpdateCorlorMaterial(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CDelaunayView::CreateLights()
{
glLightfv(GL_LIGHT0,GL_AMBIENT,m_lightAmb);
glLightfv(GL_LIGHT0,GL_DIFFUSE,m_lightDif);
glLightfv(GL_LIGHT0,GL_SPECULAR,m_lightSpe);
glLightfv(GL_LIGHT0,GL_POSITION,m_lightPos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void CDelaunayView::OnUpdateLight(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CDelaunayView::OnLight()
{
CLighting lightDlg;
lightDlg.m_lightAmbientR=m_lightAmb[0];
lightDlg.m_lightAmbientG=m_lightAmb[1];
lightDlg.m_lightAmbientB=m_lightAmb[2];
lightDlg.m_lightDiffuseR=m_lightDif[0];
lightDlg.m_lightDiffuseG=m_lightDif[1];
lightDlg.m_lightDiffuseB=m_lightDif[2];
lightDlg.m_lightSpecularR=m_lightSpe[0];
lightDlg.m_lightSpecularG=m_lightSpe[1];
lightDlg.m_lightSpecularB=m_lightSpe[2];
lightDlg.m_lightPositionX=m_lightPos[0];
lightDlg.m_lightPositionY=m_lightPos[1];
lightDlg.m_lightPositionZ=m_lightPos[2];
int responeDlg=lightDlg.DoModal();
if(responeDlg==IDOK){
m_lightAmb[0]=lightDlg.m_lightAmbientR;
m_lightAmb[1]=lightDlg.m_lightAmbientG;
m_lightAmb[2]=lightDlg.m_lightAmbientB;
m_lightDif[0]=lightDlg.m_lightDiffuseR;
m_lightDif[1]=lightDlg.m_lightDiffuseG;
m_lightDif[2]=lightDlg.m_lightDiffuseB;
m_lightSpe[0]=lightDlg.m_lightSpecularR;
m_lightSpe[1]=lightDlg.m_lightSpecularG;
m_lightSpe[2]=lightDlg.m_lightSpecularB;
m_lightPos[0]=lightDlg.m_lightPositionX;
m_lightPos[1]=lightDlg.m_lightPositionY;
m_lightPos[2]=lightDlg.m_lightPositionZ;
Invalidate();
}
}
void CDelaunayView::CreateMaterial()
{
glMaterialfv(GL_FRONT,GL_AMBIENT,m_materialAmb);
glMaterialfv(GL_FRONT,GL_DIFFUSE,m_materialDif);
glMaterialfv(GL_FRONT,GL_SPECULAR,m_materialSpe);
glMaterialfv(GL_FRONT,GL_EMISSION,m_materialEmi);
glMaterialf(GL_FRONT,GL_SHININESS,m_matshininess);
}
void CDelaunayView::OnButtonTwo()
{
m_Dimension=DO_DRAW2;
Invalidate();
}
void CDelaunayView::OnUpdateButtonTwo(CCmdUI* pCmdUI)
{
if(m_Dimension==DO_DRAW2)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CDelaunayView::OnButtonThree()
{
m_Dimension=DO_DRAW3;
Invalidate();
}
void CDelaunayView::OnUpdateButtonThree(CCmdUI* pCmdUI)
{
if(m_Dimension==DO_DRAW3)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CDelaunayView::HCT(CTriangle *temp, int p)
{//每次处理1/3三角形
m_hct[6].x=temp->m_x;//重心
m_hct[6].y=temp->m_y;
m_hct[6].z=temp->o;
if(p==3){
m_hct[0].x=m_pDoc->m_point[temp->m_p1]->m_x;//f1
m_hct[0].y=m_pDoc->m_point[temp->m_p1]->m_y;
m_hct[0].z=m_pDoc->m_point[temp->m_p1]->m_z;
m_hct[9].x=m_pDoc->m_point[temp->m_p2]->m_x;//f2
m_hct[9].y=m_pDoc->m_point[temp->m_p2]->m_y;
m_hct[9].z=m_pDoc->m_point[temp->m_p2]->m_z;
m_hct[1]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[6].x,m_hct[6].y);//c1
m_hct[1].z=temp->c1;//c1
m_hct[2]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[9].x,m_hct[9].y);//d31
m_hct[2].z=temp->d31;//d31
m_hct[4]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[0].x,m_hct[0].y);//b1
m_hct[4].z=temp->b1;//b1
m_hct[3]=BaryCenter(m_hct[0].x,m_hct[0].y,0,m_hct[9].x,m_hct[9].y,0,
m_hct[6].x,m_hct[6].y,0);//e3
m_hct[3].z=temp->e3;//e3
m_hct[5]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[9].x,m_hct[9].y);//b2
m_hct[5].z=temp->b2;//b2
m_hct[7]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[6].x,m_hct[6].y);//c2
m_hct[7].z=temp->c2;//c2
m_hct[8]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[0].x,m_hct[0].y);//d32
m_hct[8].z=temp->d32;//d32
}
if(p==2){
m_hct[0].x=m_pDoc->m_point[temp->m_p1]->m_x;//f1
m_hct[0].y=m_pDoc->m_point[temp->m_p1]->m_y;
m_hct[0].z=m_pDoc->m_point[temp->m_p1]->m_z;
m_hct[9].x=m_pDoc->m_point[temp->m_p3]->m_x;//f3
m_hct[9].y=m_pDoc->m_point[temp->m_p3]->m_y;
m_hct[9].z=m_pDoc->m_point[temp->m_p3]->m_z;
m_hct[1]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[6].x,m_hct[6].y);//c1
m_hct[1].z=temp->c1;//c1
m_hct[2]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[9].x,m_hct[9].y);//d21
m_hct[2].z=temp->d21;
m_hct[4]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[0].x,m_hct[0].y);//b1
m_hct[4].z=temp->b1;//b1
m_hct[3]=BaryCenter(m_hct[0].x,m_hct[0].y,0,m_hct[6].x,m_hct[6].y,0,
m_hct[9].x,m_hct[9].y,0);//e2
m_hct[3].z=temp->e2;//e2
m_hct[5]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[9].x,m_hct[9].y);//b3
m_hct[5].z=temp->b3;//b3
m_hct[7]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[6].x,m_hct[6].y);//c3
m_hct[7].z=temp->c3;//c3
m_hct[8]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[0].x,m_hct[0].y);//d23
m_hct[8].z=temp->d23;//d23
}
if(p==1){
m_hct[0].x=m_pDoc->m_point[temp->m_p2]->m_x;//f2
m_hct[0].y=m_pDoc->m_point[temp->m_p2]->m_y;
m_hct[0].z=m_pDoc->m_point[temp->m_p2]->m_z;
m_hct[9].x=m_pDoc->m_point[temp->m_p3]->m_x;//f3
m_hct[9].y=m_pDoc->m_point[temp->m_p3]->m_y;
m_hct[9].z=m_pDoc->m_point[temp->m_p3]->m_z;
m_hct[1]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[6].x,m_hct[6].y);//c2
m_hct[1].z=temp->c2;//c2
m_hct[2]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[9].x,m_hct[9].y);//d12
m_hct[2].z=temp->d12;//d12
m_hct[4]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[0].x,m_hct[0].y);//b2
m_hct[4].z=temp->b2;//b2
m_hct[3]=BaryCenter(m_hct[0].x,m_hct[0].y,0,m_hct[9].x,m_hct[9].y,0,
m_hct[6].x,m_hct[6].y,0);//e1
m_hct[3].z=temp->e1;//e1
m_hct[5]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[9].x,m_hct[9].y);//b3
m_hct[5].z=temp->b3;//b3
m_hct[7]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[6].x,m_hct[6].y);//c3
m_hct[7].z=temp->c3;//c3
m_hct[8]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[0].x,m_hct[0].y);//d13
m_hct[8].z=temp->d13;//d13
}
b[0][0][3]=m_hct[6].z;b[0][3][0]=m_hct[9].z;b[3][0][0]=m_hct[0].z;
b[0][1][2]=m_hct[5].z;b[0][2][1]=m_hct[7].z;
b[1][0][2]=m_hct[4].z;b[2][0][1]=m_hct[1].z;
b[2][1][0]=m_hct[2].z;b[1][2][0]=m_hct[8].z;b[1][1][1]=m_hct[3].z;
for(int i=1;i<9;i++){
if(i!=6){
m_hct[i].z=Bezier(m_hct[i]);//得到所有细分点的z值
}
}
}
POI CDelaunayView::BaryCenter(double x1, double y1,double z1,double x2, double y2,double z2,double x3, double y3,double z3)
{//平面上三角形重心
POI temp;
temp.x=(x1+x2+x3)/double(3);
temp.y=(y1+y2+y3)/double(3);
temp.z=(z1+z2+z3)/double(3);
return temp;
}
POI CDelaunayView::PointOnEdge_3(double x1, double y1, double x2, double y2)
{
POI temp;
temp.x=x1+(x2-x1)/double(3);
temp.y=y1+(y2-y1)/double(3);
return temp;
}
void CDelaunayView::OnButtonHct()
{
if(m_HCT==DO_HCT)
m_HCT=-1;
else
m_HCT=DO_HCT;
}
void CDelaunayView::OnUpdateButtonHct(CCmdUI* pCmdUI)
{
}
void CDelaunayView::OnButtonFill()
{
m_Draw_what=DO_FILL;
}
void CDelaunayView::OnUpdateButtonFill(CCmdUI* pCmdUI)
{
if(m_Draw_what==DO_FILL)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CDelaunayView::OnButtonLine()
{
m_Draw_what=DO_LINE;
}
void CDelaunayView::OnUpdateButtonLine(CCmdUI* pCmdUI)
{
if(m_Draw_what==DO_LINE)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
POI CDelaunayView::GetTriNormal(POI p1, POI p2, POI p3)
{ //得到三角片的单位法向
POI vector1,vector2,Normal;
/*POI p;
double s;
s=S(p1,p2,p3);
if(s<0){
p=p2;p2=p3;p3=p;
}*/
vector1.x=p2.x-p1.x;
vector1.y=p2.y-p1.y;
vector1.z=p2.z-p1.z;
vector2.x=p3.x-p1.x;
vector2.y=p3.y-p1.y;
vector2.z=p3.z-p1.z;
//get the normal n=a*b(外积)
Normal=m_pDoc->VectorProduct(vector1.x,vector1.y,vector1.z,vector2.x,vector2.y,vector2.z);
//单位化 normal
Normal=m_pDoc->Unitization(Normal);
return Normal;
}
double CDelaunayView::S(POI p1, POI p2, POI p3)
{ //求三角形面积,以右下角为原点时,s>0为逆时针(左转),
//s=0为三点重合
double s;
s=p1.x*p2.y+p2.x*p3.y+p1.y*p3.x-p2.y*p3.x-p1.y*p2.x-p1.x*p3.y;
s=s/2.0;
return s;
}
int CDelaunayView::Factorial(int n)
{//阶乘
int x=n;
if(n==0 ||n==1)
return 1;
else{
for(int i=1;i<n;i++){
x=x*(n-i);
}
}
return x;
}
double CDelaunayView::Power(double a, int e)
{
double x=1.0;
if(e==0){
return 1.0;
}
else{
if(a==0.0){
return 0.0;
}
for(int i=1;i<=e;i++){
x=x*a;
}
}
return x;
}
double CDelaunayView::Bezier(POI p)
{//s1,s2,s3 : 面积坐标
double s1,s2,s3,s;
s=S(m_hct[0],m_hct[9],m_hct[6]);
s1=S(p,m_hct[9],m_hct[6])/s;
s2=S(m_hct[0],p,m_hct[6])/s;
s3=S(m_hct[0],m_hct[9],p)/s;
if(fabs(s1)<0.000000000001) s1=0;
if(fabs(s2)<0.000000000001) s2=0;
if(fabs(s3)<0.000000000001) s3=0;
int i,j,k;
double B=0.0;
for(i=0;i<4;i++){
for(j=0;j<=(3-i);j++){
for(k=0;k<=(3-i-j);k++){
if((i+j+k)==3){
B=B+b[i][j][k]*double(6)/(Factorial(i)*Factorial(j)*Factorial(k))*
Power(s1,i)*Power(s2,j)*Power(s3,k);
}
}
}
}
return B;
}
void CDelaunayView::OnButtonWnag()
{
m_Draw_what=DO_WANG;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -