DelaunayView.cpp
上传用户:azhong891
上传日期:2013-06-04
资源大小:197k
文件大小:36k
源码类别:

GIS编程

开发平台:

Visual C++

  1. // DelaunayView.cpp : implementation of the CDelaunayView class
  2. //
  3. #include "stdafx.h"
  4. #include "Delaunay.h"
  5. #include "mainfrm.h"
  6. #include "pointpos.h"
  7. #include "triangle.h"
  8. #include "DelaunayDoc.h"
  9. #include "DelaunayView.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CDelaunayView
  17. IMPLEMENT_DYNCREATE(CDelaunayView, CView)
  18. BEGIN_MESSAGE_MAP(CDelaunayView, CView)
  19. //{{AFX_MSG_MAP(CDelaunayView)
  20. ON_WM_SIZE()
  21. ON_WM_CREATE()
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_MOUSEMOVE()
  24. ON_WM_DESTROY()
  25. ON_WM_KEYDOWN()
  26. ON_COMMAND(IDM_CORLOR, OnCorlorMaterial)
  27. ON_UPDATE_COMMAND_UI(IDM_CORLOR, OnUpdateCorlorMaterial)
  28. ON_UPDATE_COMMAND_UI(IDM_LIGHT, OnUpdateLight)
  29. ON_COMMAND(IDM_LIGHT, OnLight)
  30. ON_COMMAND(ID_BUTTON_TWO, OnButtonTwo)
  31. ON_UPDATE_COMMAND_UI(ID_BUTTON_TWO, OnUpdateButtonTwo)
  32. ON_COMMAND(ID_BUTTON_THREE, OnButtonThree)
  33. ON_UPDATE_COMMAND_UI(ID_BUTTON_THREE, OnUpdateButtonThree)
  34. ON_COMMAND(ID_BUTTON_HCT, OnButtonHct)
  35. ON_UPDATE_COMMAND_UI(ID_BUTTON_HCT, OnUpdateButtonHct)
  36. ON_COMMAND(ID_BUTTON_FILL, OnButtonFill)
  37. ON_UPDATE_COMMAND_UI(ID_BUTTON_FILL, OnUpdateButtonFill)
  38. ON_COMMAND(ID_BUTTON_LINE, OnButtonLine)
  39. ON_UPDATE_COMMAND_UI(ID_BUTTON_LINE, OnUpdateButtonLine)
  40. ON_COMMAND(ID_BUTTON_WNAG, OnButtonWnag)
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CDelaunayView construction/destruction
  45. CDelaunayView::CDelaunayView()
  46. {
  47. // TODO: add construction code here
  48. }
  49. CDelaunayView::~CDelaunayView()
  50. {
  51. }
  52. BOOL CDelaunayView::PreCreateWindow(CREATESTRUCT& cs)
  53. {
  54. // TODO: Modify the Window class or styles here by modifying
  55. //  the CREATESTRUCT cs
  56.     cs.style |=WS_CLIPCHILDREN |WS_CLIPSIBLINGS;
  57. return CView::PreCreateWindow(cs);
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CDelaunayView drawing
  61. void CDelaunayView::OnDraw(CDC* pDC)
  62. {
  63. CDelaunayDoc* pDoc = GetDocument();
  64. ASSERT_VALID(pDoc);
  65. DrawScene();
  66. SwapBuffers(pDC->m_hDC);
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CDelaunayView diagnostics
  70. #ifdef _DEBUG
  71. void CDelaunayView::AssertValid() const
  72. {
  73. CView::AssertValid();
  74. }
  75. void CDelaunayView::Dump(CDumpContext& dc) const
  76. {
  77. CView::Dump(dc);
  78. }
  79. CDelaunayDoc* CDelaunayView::GetDocument() // non-debug version is inline
  80. {
  81. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDelaunayDoc)));
  82. return (CDelaunayDoc*)m_pDocument;
  83. }
  84. #endif //_DEBUG
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CDelaunayView message handlers
  87. void CDelaunayView::OnSize(UINT nType, int cx, int cy) 
  88. {
  89. CView::OnSize(nType, cx, cy);
  90. if(cy>0)
  91. {
  92. m_oldRect.right=cx;
  93. m_oldRect.bottom=cy;
  94. glMatrixMode(GL_PROJECTION);
  95. glLoadIdentity();
  96. glFrustum(-2.0,2.0,-2.0,2.0,0.0,20.0);
  97. glLoadIdentity();//????不能不要,事关显示
  98. //gluPerspective(40.0f,(GLdouble)cx/cy,1.0f,50.0f);
  99. glMatrixMode(GL_MODELVIEW);
  100. glViewport(0,0,cx,cy);
  101. }
  102. }
  103. int CDelaunayView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  104. {
  105. if (CView::OnCreate(lpCreateStruct) == -1)
  106. return -1;
  107. InitOpenGl();
  108. m_pDoc=this->GetDocument();
  109. m_ox=50;m_oy=50;m_len=400;
  110. m_Dimension=DO_DRAW2;
  111. //m_HCT=DO_NO_HCT;
  112. m_Draw_what=DO_LINE;
  113. //OpenGl/////////////////////////////
  114. m_translate_x=-1.0;
  115. m_translate_y=-1.0;
  116. m_translate_z=0;
  117. m_rotate_angle=0;
  118. m_gl_x=1.0;m_gl_y=1.0;m_gl_z=0.0;
  119. eye.x=4.0;eye.y=5.0;eye.z=6.0;
  120. m_colorRed=float(0.2);m_colorGreen=float(0.5);m_colorBlue=float(0.8);
  121. m_bMaskRed=FALSE;m_bMaskGreen=FALSE;m_bMaskBlue=FALSE;
  122. m_materialAmb[0]=0.2;m_materialAmb[1]=0.2;m_materialAmb[2]=0.2;m_materialAmb[3]=1.0;
  123. m_materialDif[0]=0.8;m_materialDif[1]=0.8;m_materialDif[2]=0.8;m_materialDif[3]=1.0;
  124. m_materialSpe[0]=0.0;m_materialSpe[1]=0.0;m_materialSpe[2]=0.0;m_materialSpe[3]=1.0;
  125. m_materialEmi[0]=0.0;m_materialEmi[1]=0.0;m_materialEmi[2]=0.0;m_materialEmi[3]=1.0;
  126. m_matshininess=0.0f;
  127. m_lightAmb[0]=0.2;m_lightAmb[1]=0.2;m_lightAmb[2]=0.2;m_lightAmb[3]=1.0;
  128. m_lightDif[0]=1.0;m_lightDif[1]=1.0;m_lightDif[2]=0.3;m_lightDif[3]=1.0;
  129. m_lightSpe[0]=1.0;m_lightSpe[1]=1.0;m_lightSpe[2]=1.0;m_lightSpe[3]=1.0;
  130. m_lightPos[0]=1.0;m_lightPos[1]=1.0;m_lightPos[2]=1.0;m_lightPos[3]=0.0;
  131. //OpenGl/////////////////////////////
  132. return 0;
  133. }
  134. void CDelaunayView::OnLButtonDown(UINT nFlags, CPoint point) 
  135. {////////////////////////////////////////////
  136. ASSERT(m_pDoc!=NULL);
  137. CPoint pos=point;
  138. int m_plen;//当前节点个数
  139. double s;
  140. //ScreenToClient(&pos);
  141. if(m_pDoc->m_DoWhat==DO_ADD)
  142. {
  143. //'x' and 'y' are that to be stored in 'm_point'
  144. double x,y;
  145. x=double(pos.x)/double(m_oldRect.right);
  146. y=double((m_oldRect.bottom-pos.y))/double(m_oldRect.bottom);
  147. //form the original convexity when 'm_plen' is smaller than 3
  148. //the original convexity is a triangle
  149. m_plen=m_pDoc->m_point.GetSize();//==1????
  150. switch(m_plen) 
  151. {
  152. case 0 :
  153. m_pDoc->AddPoint(x,y);
  154. m_pDoc->m_con.SetAtGrow(0,0);
  155.                         break;
  156. case 1 :
  157. if(m_pDoc->m_point[0]->m_x==x &&
  158.    m_pDoc->m_point[0]->m_y==y)
  159. {
  160.   AfxMessageBox("The two point not suitable");
  161.                       break;
  162. //please 输入节点,跳出
  163. }
  164. m_pDoc->AddPoint(x,y);
  165. m_pDoc->m_con.SetAtGrow(1,1);
  166.                         break;
  167.                     case 2 ://确保凸包顶点逆时针存储
  168. m_pDoc->AddPoint(x,y);
  169. s=m_pDoc->S(0,1,2);
  170. if(s==0)
  171. {
  172. AfxMessageBox("please 输入节点 again,因三点共线,跳出");
  173. m_pDoc->m_point.RemoveAt(2,1);
  174.                           //  m_pDoc->m_n.RemoveAt(2,1);
  175. }
  176. m_plen=m_pDoc->m_point.GetSize();
  177. m_pDoc->m_con.SetAtGrow(m_plen-1,m_plen-1);                    
  178. if(s<0) 
  179. {
  180. m_pDoc->m_con[0]=1;
  181. m_pDoc->m_con[1]=0;                          
  182. }
  183. CTriangle* pTriangle;
  184. pTriangle=new CTriangle(m_pDoc->m_con[0],
  185.       m_pDoc->m_con[1],m_pDoc->m_con[2]);
  186. m_pDoc->Center(pTriangle);
  187. m_pDoc->BaryCenter(pTriangle);
  188. m_pDoc->m_tri.AddHead(pTriangle);                   
  189.                         break;
  190.                 default :
  191. m_pDoc->AddPoint(x,y);
  192.                         m_plen=m_pDoc->m_point.GetSize();
  193.                         int i=DelTriList(x,y,m_plen-1);
  194. if(i==POS_ERROR){
  195. int k=m_pDoc->m_point.GetSize();
  196. m_pDoc->m_point.RemoveAt(k-1,1);
  197. }
  198.                         break;
  199. }
  200. Invalidate();
  201. }
  202. ////////////////////////////////////////////
  203. CView::OnLButtonDown(nFlags, point);
  204. }
  205. void CDelaunayView::OnMouseMove(UINT nFlags, CPoint point) 
  206. {
  207. // TODO: Add your message handler code here and/or call default
  208. // CClientDC dc(this);
  209. double x,y;
  210. double a,b;
  211. a=double(point.x);
  212. b=double(m_oldRect.right);
  213. x=a/b;
  214. y=double((m_oldRect.bottom-point.y))/double(m_oldRect.bottom);
  215.     CString str1;
  216. str1.Empty();
  217. int i=m_pDoc->m_DoWhat;
  218.     str1.Format("%f;%f;%d;%d;%d;%d",x,y,i,m_Dimension,m_HCT,m_Draw_what);
  219. CMainFrame *pFram=(CMainFrame *)(AfxGetApp()->GetMainWnd());
  220. pFram->m_wndStatusBar.SetPaneText(0,str1);
  221. CView::OnMouseMove(nFlags, point);
  222. }
  223. array CDelaunayView::Wher(CPointPos *pos)
  224. {
  225. //Points and Convexity
  226. /*return Value:a.a
  227. POS_IN=1,a point  belong a triangle or convexity
  228. POS_ON=2,a point belong a circle or on the edge of convexity
  229. POS_OUT=0,a point out of a circle or convexity
  230. a.b , a.c: record the point's mark in m_con */
  231. //1. get a ponit in convexity:p
  232. array a;
  233. int max=m_pDoc->m_con.GetSize();
  234. int z=max/2;
  235. CPointPos *p1=new CPointPos(m_pDoc->m_point[m_pDoc->m_con[0]]->m_x,
  236. m_pDoc->m_point[m_pDoc->m_con[0]]->m_y);
  237.     CPointPos *p2=new CPointPos(m_pDoc->m_point[m_pDoc->m_con[z]]->m_x,
  238. m_pDoc->m_point[m_pDoc->m_con[z]]->m_y);
  239.     CPointPos *p3=new CPointPos(m_pDoc->m_point[m_pDoc->m_con[max-1]]->m_x,
  240. m_pDoc->m_point[m_pDoc->m_con[max-1]]->m_y);
  241. CPointPos *p=new CPointPos((p1->m_x+p2->m_x+p3->m_x)/3,
  242.          (p1->m_y+p2->m_y+p3->m_y)/3);
  243. p3->m_x=double(pos->m_x);
  244. p3->m_y=double(pos->m_y);
  245. //遍寻Convexity
  246. double s1,s2,s;
  247. for(int i=0;i<max-1;i++)
  248. {
  249. p1->m_x=m_pDoc->m_point[m_pDoc->m_con[i]]->m_x;
  250.     p1->m_y=m_pDoc->m_point[m_pDoc->m_con[i]]->m_y;
  251.       p2->m_x=m_pDoc->m_point[m_pDoc->m_con[i+1]]->m_x;
  252.     p2->m_y=m_pDoc->m_point[m_pDoc->m_con[i+1]]->m_y;
  253.         
  254. s1=m_pDoc->S(p3,p,p1);
  255. s2=m_pDoc->S(p3,p,p2);
  256. if(s1>0 && s2<0)//the point in the 扇形 con[i],con[i+1] and p formed
  257. {
  258.   a.b=i;
  259.   a.c=i+1;
  260.           s=m_pDoc->S(p1,p2,p3);
  261.   if(s>0)
  262.   {
  263.   a.a=1;//POS_IN  
  264.   }
  265.           if(s==0)
  266.   {
  267.   a.a=2;//POS_ON
  268.   }
  269.   if(s<0)
  270.   {
  271.   a.a=0;//POS_OUT
  272.   }
  273.   return a;
  274. }         
  275. }
  276.     p1->m_x=m_pDoc->m_point[m_pDoc->m_con[i]]->m_x;
  277. p1->m_y=m_pDoc->m_point[m_pDoc->m_con[i]]->m_y;
  278.     p2->m_x=m_pDoc->m_point[m_pDoc->m_con[0]]->m_x;
  279. p2->m_y=m_pDoc->m_point[m_pDoc->m_con[0]]->m_y;
  280. s1=m_pDoc->S(p3,p,p1);
  281. s2=m_pDoc->S(p3,p,p2);
  282. if(s1>0 && s2<0)
  283. {
  284.           a.b=i;
  285.   a.c=0;
  286.           s=m_pDoc->S(p1,p2,p3);
  287.   if(s>0)
  288.   {
  289.   a.a=1;//POS_IN  
  290.   }
  291.           if(s==0)
  292.   {
  293.   a.a=2;//POS_ON
  294.   }
  295.   if(s<0)
  296.   {
  297.   a.a=0;//POS_OUT
  298.   }
  299.   return a;
  300. }           
  301. }
  302. void CDelaunayView::DrawTri(CTriangle *tri)
  303. {
  304. int i;
  305. //POI normal;
  306. int p1,p2,p3;
  307. p1=tri->m_p1;
  308. p2=tri->m_p2;
  309. p3=tri->m_p3;
  310. GLfloat fLineWidth[2];
  311. glGetFloatv(GL_LINE_WIDTH_RANGE,fLineWidth);
  312. if(m_Dimension==DO_DRAW2 && m_HCT!=DO_HCT)
  313. {
  314. glColor4d(0.0,0.0,1.0,0.5);
  315.     glBegin(GL_TRIANGLES);
  316. glVertex3d(m_pDoc->m_point[p1]->m_x,m_pDoc->m_point[p1]->m_y,0.0); 
  317.      glVertex3d(m_pDoc->m_point[p2]->m_x,m_pDoc->m_point[p2]->m_y,0.0);
  318.      glVertex3d(m_pDoc->m_point[p3]->m_x,m_pDoc->m_point[p3]->m_y,0.0);
  319.      glEnd();
  320. }
  321. if(m_Dimension==DO_DRAW3 && m_HCT!=DO_HCT)
  322. {
  323. glPushMatrix();
  324. //glScaled(2,2,1.5);
  325. glColor3f(1.0f,0.0f,0.0f);         
  326. glBegin(GL_TRIANGLES);
  327. glVertex3d(m_pDoc->m_point[p1]->m_x,m_pDoc->m_point[p1]->m_y,m_pDoc->m_point[p1]->m_z); 
  328.      glVertex3d(m_pDoc->m_point[p2]->m_x,m_pDoc->m_point[p2]->m_y,m_pDoc->m_point[p2]->m_z);
  329.      glVertex3d(m_pDoc->m_point[p3]->m_x,m_pDoc->m_point[p3]->m_y,m_pDoc->m_point[p3]->m_z);
  330.      glEnd();
  331. glPopMatrix();
  332. }
  333. if(m_Dimension==DO_DRAW3 && m_HCT==DO_HCT)
  334. {
  335. m_pDoc->DrawTri(tri->m_p1,tri->m_p2,tri);
  336. m_pDoc->DrawTri(tri->m_p2,tri->m_p3,tri);
  337. m_pDoc->DrawTri(tri->m_p3,tri->m_p1,tri);
  338. /*for(i=1;i<=3;i++)
  339. {
  340. HCT(tri,i);
  341. glColor4d(0.0,1.0,0.0,0.5);
  342. if(i==2)
  343. {
  344. glBegin(GL_TRIANGLES);//anticlock    
  345.    normal=GetTriNormal(m_hct[0],m_hct[1],m_hct[2]);
  346.    glNormal3d(normal.x,normal.y,normal.z);
  347.    glVertex3d(m_hct[0].x,m_hct[0].y,m_hct[0].z);          
  348.    glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
  349.    glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
  350.    
  351.    normal=GetTriNormal(m_hct[1],m_hct[3],m_hct[2]);
  352.    glNormal3d(normal.x,normal.y,normal.z);
  353.    glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
  354.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  355.    glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
  356.    normal=GetTriNormal(m_hct[3],m_hct[1],m_hct[4]);
  357.    glNormal3d(normal.x,normal.y,normal.z);    
  358.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  359.    glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
  360.    glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);
  361.    normal=GetTriNormal(m_hct[3],m_hct[4],m_hct[5]);
  362.    glNormal3d(normal.x,normal.y,normal.z);
  363.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  364.    glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);
  365.    glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
  366.    
  367.    normal=GetTriNormal(m_hct[4],m_hct[6],m_hct[5]);
  368.    glNormal3d(normal.x,normal.y,normal.z);
  369.    glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);    
  370.    glVertex3d(m_hct[6].x,m_hct[6].y,m_hct[6].z);
  371.         glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
  372.    normal=GetTriNormal(m_hct[8],m_hct[7],m_hct[9]);
  373.    glNormal3d(normal.x,normal.y,normal.z);
  374.    glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z); 
  375.         glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);
  376.    glVertex3d(m_hct[9].x,m_hct[9].y,m_hct[9].z);
  377.    normal=GetTriNormal(m_hct[8],m_hct[3],m_hct[7]);
  378.    glNormal3d(normal.x,normal.y,normal.z);
  379.    glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
  380.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  381.    glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);
  382.    normal=GetTriNormal(m_hct[3],m_hct[5],m_hct[7]);
  383.    glNormal3d(normal.x,normal.y,normal.z);
  384.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  385.    glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
  386.    glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);
  387.     
  388.    normal=GetTriNormal(m_hct[2],m_hct[3],m_hct[8]);
  389.    glNormal3d(normal.x,normal.y,normal.z);
  390.    glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z); 
  391.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  392.    glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
  393.      glEnd();
  394. }
  395. else
  396. {
  397.    glBegin(GL_TRIANGLES);//anticlock    
  398.    normal=GetTriNormal(m_hct[0],m_hct[2],m_hct[1]);
  399.    glNormal3d(normal.x,normal.y,normal.z);
  400.    glVertex3d(m_hct[0].x,m_hct[0].y,m_hct[0].z);          
  401.    glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
  402.    glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
  403.    
  404.    normal=GetTriNormal(m_hct[1],m_hct[2],m_hct[3]);
  405.    glNormal3d(normal.x,normal.y,normal.z);
  406.    glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
  407.    glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
  408.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  409.    normal=GetTriNormal(m_hct[1],m_hct[3],m_hct[4]);
  410.    glNormal3d(normal.x,normal.y,normal.z);    
  411.    glVertex3d(m_hct[1].x,m_hct[1].y,m_hct[1].z);
  412.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  413.    glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);
  414.    normal=GetTriNormal(m_hct[4],m_hct[3],m_hct[5]);
  415.    glNormal3d(normal.x,normal.y,normal.z);
  416.    glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);
  417.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  418.    glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
  419.    
  420.    normal=GetTriNormal(m_hct[4],m_hct[5],m_hct[6]);
  421.    glNormal3d(normal.x,normal.y,normal.z);
  422.    glVertex3d(m_hct[4].x,m_hct[4].y,m_hct[4].z);    
  423.    glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
  424.         glVertex3d(m_hct[6].x,m_hct[6].y,m_hct[6].z);
  425.    normal=GetTriNormal(m_hct[7],m_hct[8],m_hct[9]);
  426.    glNormal3d(normal.x,normal.y,normal.z);
  427.    glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z); 
  428.         glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
  429.    glVertex3d(m_hct[9].x,m_hct[9].y,m_hct[9].z);
  430.    normal=GetTriNormal(m_hct[3],m_hct[8],m_hct[7]);
  431.    glNormal3d(normal.x,normal.y,normal.z);
  432.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  433.    glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
  434.    glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);
  435.    normal=GetTriNormal(m_hct[3],m_hct[7],m_hct[5]);
  436.    glNormal3d(normal.x,normal.y,normal.z);
  437.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z);
  438.    glVertex3d(m_hct[7].x,m_hct[7].y,m_hct[7].z);
  439.    glVertex3d(m_hct[5].x,m_hct[5].y,m_hct[5].z);
  440.     
  441.    normal=GetTriNormal(m_hct[3],m_hct[2],m_hct[8]);
  442.    glNormal3d(normal.x,normal.y,normal.z);
  443.    glVertex3d(m_hct[3].x,m_hct[3].y,m_hct[3].z); 
  444.    glVertex3d(m_hct[2].x,m_hct[2].y,m_hct[2].z);
  445.    glVertex3d(m_hct[8].x,m_hct[8].y,m_hct[8].z);
  446.      glEnd();
  447. }//if else
  448. }//for*/
  449. }//if
  450. if(m_Dimension==DO_DRAW2 && m_HCT==DO_HCT)
  451. {
  452. for(i=1;i<=3;i++)
  453. {
  454. HCT(tri,i);
  455. glColor4d(0.0,0.0,1.0,0.5);
  456. glBegin(GL_TRIANGLES);//anticlock
  457.    glVertex3d(m_hct[0].x,m_hct[0].y,0);          
  458.    glVertex3d(m_hct[1].x,m_hct[1].y,0);
  459.    glVertex3d(m_hct[2].x,m_hct[2].y,0); 
  460.    glVertex3d(m_hct[1].x,m_hct[1].y,0); 
  461.    glVertex3d(m_hct[2].x,m_hct[2].y,0); 
  462.    glVertex3d(m_hct[3].x,m_hct[3].y,0); 
  463.    glVertex3d(m_hct[3].x,m_hct[3].y,0); 
  464.    glVertex3d(m_hct[1].x,m_hct[1].y,0); 
  465.    glVertex3d(m_hct[4].x,m_hct[4].y,0); 
  466.    glVertex3d(m_hct[3].x,m_hct[3].y,0); 
  467.    glVertex3d(m_hct[4].x,m_hct[4].y,0); 
  468.    glVertex3d(m_hct[5].x,m_hct[5].y,0); 
  469.    glVertex3d(m_hct[4].x,m_hct[4].y,0); 
  470.    glVertex3d(m_hct[6].x,m_hct[6].y,0); 
  471.         glVertex3d(m_hct[5].x,m_hct[5].y,0); 
  472.    glVertex3d(m_hct[7].x,m_hct[7].y,0);  
  473.         glVertex3d(m_hct[8].x,m_hct[8].y,0); 
  474.    glVertex3d(m_hct[9].x,m_hct[9].y,0); 
  475.    glVertex3d(m_hct[8].x,m_hct[8].y,0); 
  476.    glVertex3d(m_hct[3].x,m_hct[3].y,0); 
  477.    glVertex3d(m_hct[7].x,m_hct[7].y,0); 
  478.  
  479.    glVertex3d(m_hct[3].x,m_hct[3].y,0); 
  480.    glVertex3d(m_hct[5].x,m_hct[5].y,0); 
  481.    glVertex3d(m_hct[7].x,m_hct[7].y,0); 
  482.     
  483.    glVertex3d(m_hct[2].x,m_hct[2].y,0); 
  484.    glVertex3d(m_hct[3].x,m_hct[3].y,0); 
  485.    glVertex3d(m_hct[8].x,m_hct[8].y,0); 
  486.      glEnd();
  487. }
  488. }
  489. }
  490. int CDelaunayView::DelTriList(double x, double y,int p)
  491. {//record the position of triangles which are to be deleted
  492. //'x' and 'y' are the coordinates of the insert point
  493. //'p' is the mark or th insert point in 'm_point'
  494. CPointPos *point=new CPointPos(x,y);
  495. int j=0,i=0,k=0,max=0;
  496. array a;
  497. // GetInitEdges : ransack each trangle to record it's edges
  498. //when the piont belong it's circle
  499.     k=m_pDoc->GetInitEdges(x,y,p);// the returned value 'k/2' is the number of cirecle the point belonged??
  500. if(k==POS_ERROR){
  501. return POS_ERROR;
  502. }
  503. //默认:插入点属于某个外接圆,且属于凸包
  504.           //judge a point belong to the convexity or not
  505.   a=Wher(point);
  506.   /*return Value:a.a
  507. POS_IN=1,a point  belong a triangle or convexity
  508. POS_ON=2,a point belong a circle or on the edge of convexity
  509. POS_OUT=0,a point out of a circle or convexity
  510. a.b , a.c: record the point's mark in m_con */
  511.    max=m_pDoc->m_con.GetSize();
  512.   if(0==a.a && k>=2)//插入点属于某个外接圆,但不属于凸包
  513.   {  //delete an edge "belong to" the inserted polygon,the edge is also 
  514.  //belong  to the border of the convexity
  515.  if (max==3){
  516. int t=m_pDoc->m_edge.GetSize();       
  517. for(j=0;j<t;j++){
  518. if((m_pDoc->m_edge[j]->m_p1==m_pDoc->m_con[a.b]) & 
  519.    (m_pDoc->m_edge[j]->m_p2==m_pDoc->m_con[a.c]))
  520.    {
  521.    m_pDoc->m_edge.RemoveAt(j,1);
  522.    m_pDoc->m_con.InsertAt(a.c,p);
  523.    break;
  524.    }
  525. }
  526.  }
  527.  else
  528.  {
  529.     CWordArray con_index;//save the point's mark can be saw on the con
  530.     for(i=0;i<max-1;i++)
  531. {
  532.    int t=m_pDoc->m_edge.GetSize();       
  533.    for(j=0;j<t;j++){
  534.   if((m_pDoc->m_edge[j]->m_p1==m_pDoc->m_con[i]) & 
  535.    (m_pDoc->m_edge[j]->m_p2==m_pDoc->m_con[i+1]))
  536.   {
  537.   bool y=m_pDoc->DelEdgeOrNot(m_pDoc->m_edge[j]->m_p1,m_pDoc->m_edge[j]->m_p2,p);
  538.                           if(y){
  539.               m_pDoc->m_edge.RemoveAt(j,1);
  540.               con_index.Add(i);
  541.               con_index.Add(i+1);    
  542.   break;
  543.   }
  544.   }
  545.    }
  546. }//end for i
  547.                 int t=m_pDoc->m_edge.GetSize();       
  548.     for(j=0;j<t;j++){       
  549.    if((m_pDoc->m_edge[j]->m_p1==m_pDoc->m_con[max-1]) && 
  550.    (m_pDoc->m_edge[j]->m_p2==m_pDoc->m_con[0]))
  551.    {
  552.     bool y=m_pDoc->DelEdgeOrNot(m_pDoc->m_edge[j]->m_p1,m_pDoc->m_edge[j]->m_p2,p);
  553.                         if(y){
  554.               m_pDoc->m_edge.RemoveAt(j,1);
  555.               con_index.Add(max-1);
  556.               con_index.Add(0);    
  557.   break;
  558. }     
  559.    }
  560. }
  561. t=con_index.GetSize();
  562.     m_pDoc->EditCon(con_index[t-1],con_index[0],p);
  563.              }
  564.   }
  565.   if(k==0){//插入点不属于任意一个外接圆,显然也不属于凸包
  566.   CBorder * m_border;
  567.   m_border=new CBorder(m_pDoc->m_con[a.b],m_pDoc->m_con[a.c]);
  568.   m_pDoc->m_edge.Add(m_border);
  569.   m_pDoc->EditCon(a.c,a.b,p);
  570.   }
  571.    //Delete triangles that have been marked
  572.   m_pDoc->DelTriMarked();
  573.   //add new triangles
  574.       m_pDoc->AddTriangle(p);
  575.   max=m_pDoc->m_index.GetSize();
  576.   m_pDoc->m_index.RemoveAt(0,max);//RemoveAll : no use ???
  577.   m_pDoc->m_edge.RemoveAll();
  578. }
  579. void CDelaunayView::OnDestroy() 
  580. {
  581. HGLRC hrc;
  582. hrc=::wglGetCurrentContext();
  583. ::wglMakeCurrent(NULL,NULL);
  584. if(m_pDC)
  585. delete m_pDC;
  586. CView::OnDestroy();
  587. }
  588. void CDelaunayView::DrawScene()
  589. {
  590. glClearColor(1.0f,1.0f,1.0f,1.0f);//setup backcolor
  591. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  592.     //GL_COLOR_BUFFER_BIT 用当前设置的backcolor 第一次清除 color buffer
  593. // GL_DEPTH_BUFFER_BIT清除 depth buffer,以便进行depth test and 消除隐藏面
  594. glClearDepth(1.0f);
  595. glDepthFunc(GL_LESS);
  596. glEnable(GL_DEPTH_TEST);
  597. if(m_bMaterial){
  598. CreateMaterial();
  599. CreateLights();
  600. }
  601. glColorMask(!m_bMaskRed,!m_bMaskGreen,!m_bMaskBlue,GL_TRUE);
  602. glColor3f(m_colorRed,m_colorGreen,m_colorBlue);
  603. glMatrixMode(GL_MODELVIEW);
  604. glLoadIdentity();
  605. glTranslated(m_translate_x,m_translate_y,m_translate_z);
  606. if(m_rotate_angle!=0.0){
  607. glRotated(m_rotate_angle,m_gl_x,m_gl_y,m_gl_z);
  608. }
  609. glScaled(2,2,0);
  610. // glScaled((cx-2*m_ox)/cx,(cy-2*m_oy)/cy,0.0);
  611. if(m_Dimension==DO_DRAW3){
  612. // glPushMatrix();
  613.     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
  614. // glPopMatrix();
  615. }
  616. glBegin(GL_LINE_STRIP);
  617.            glVertex3d(0.0,0.0,0.0);
  618.    glVertex3d(0.0,1.0,0.0);
  619.    glVertex3d(1.0,1.0,0.0);
  620.    glVertex3d(1.0,0.0,0.0);
  621.    glVertex3d(0.0,0.0,0.0);
  622.     glEnd(); 
  623. glPushMatrix();
  624. if(m_Dimension==DO_DRAW2)
  625. DrawPoints();
  626. if(m_Draw_what==DO_WANG && m_Dimension==DO_DRAW3)
  627. m_pDoc->Wang();
  628. else
  629.         DrawTris();
  630. glPopMatrix();
  631. glTranslated(0.5,0.5,0.0);
  632. // glPushMatrix();
  633. // auxSolidSphere(0.5);
  634. // glPopMatrix();
  635. glDisable(GL_LIGHTING);
  636. glDisable(GL_LIGHT0);
  637. glDisable(GL_DEPTH_TEST);
  638. // glFinish();
  639. }
  640. void CDelaunayView::InitOpenGl()
  641. {
  642. PIXELFORMATDESCRIPTOR pfd;
  643.     int n;
  644. HGLRC hrc;
  645. m_pDC=new CClientDC(this);
  646. ASSERT(m_pDC!=NULL);
  647. if(!bSetupPixelFormat()) return;
  648. n=::GetPixelFormat(m_pDC->GetSafeHdc());
  649. ::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);
  650. hrc=wglCreateContext(m_pDC->GetSafeHdc());
  651. //create a instance of RC: hrc
  652. wglMakeCurrent(m_pDC->GetSafeHdc(),hrc);
  653. //make hrc a current Rendering context of a thread
  654. GetClientRect(&m_oldRect);//get size of ClientRect
  655. }
  656. BOOL CDelaunayView::bSetupPixelFormat()
  657. {
  658. static PIXELFORMATDESCRIPTOR pfd={
  659. sizeof(PIXELFORMATDESCRIPTOR),
  660. 1,
  661. PFD_DRAW_TO_WINDOW |
  662. PFD_SUPPORT_OPENGL |
  663. PFD_DOUBLEBUFFER,
  664. PFD_TYPE_RGBA,
  665. 24,//24位真彩
  666. 0,0,0,0,0,0,
  667. 0,
  668. 0,
  669. 0,
  670. 0,0,0,0,
  671. 32,//32-bit z-buffer,可从物体中消除隐藏面
  672. 0,
  673. 0,
  674. PFD_MAIN_PLANE,//选择主层面
  675. 0,
  676. 0,0,0
  677. };
  678. int pixelformat;
  679. if((pixelformat=ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd))==0)
  680. {
  681. MessageBox("ChoosePixelFormat failed");
  682. return FALSE;
  683. }
  684. if(SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)
  685. {
  686. MessageBox("SetPixelFormat failed");
  687. return FALSE;
  688. }
  689. return TRUE;
  690. //CClientDC clientdc(this);
  691. //int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
  692. //BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
  693. //hglrc=wglCreateContext(clientdc.m_hDC);
  694. }
  695. void CDelaunayView::DrawPoint(int i)
  696. {
  697. double x,y,z;
  698. x=m_pDoc->m_point[i]->m_x;
  699. y=m_pDoc->m_point[i]->m_y;
  700. z=m_pDoc->m_point[i]->m_z;
  701.     glBegin(GL_POINTS);
  702. glVertex3d(m_pDoc->m_point[i]->m_x,m_pDoc->m_point[i]->m_y,0.0);
  703.     glEnd();
  704. }
  705. void CDelaunayView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  706. {
  707. switch(nChar)
  708. {
  709. case VK_LEFT:
  710. m_translate_x=m_translate_x-0.2;
  711. break;
  712. case VK_RIGHT:
  713.     m_translate_x=m_translate_x+0.2;
  714. break;
  715. case VK_UP:
  716. m_translate_y=m_translate_y+0.1;
  717.         break;
  718. case VK_DOWN:
  719. m_translate_y=m_translate_y-0.2;
  720. break;
  721. case 88://x
  722. if(m_gl_x==0.0){ 
  723.             m_gl_x=0.1;
  724. }
  725. else{
  726. m_gl_x=0.0;
  727. }
  728. break;
  729. case 89://y
  730. if(m_gl_y==0.0){ 
  731.             m_gl_y=0.1;
  732. }
  733. else{
  734. m_gl_y=0.0;
  735. }
  736. break;
  737. case 90://z
  738. if(m_gl_z==0.0){ 
  739.             m_gl_z=0.1;
  740. }
  741. else{
  742. m_gl_z=0.0;
  743. }
  744. break;
  745. case 100://小键盘的left
  746. m_rotate_angle=m_rotate_angle-10.0;
  747. break;
  748. case 102://小键盘的right
  749. m_rotate_angle=m_rotate_angle+10.0;
  750. break;
  751. case 104://小键盘的up
  752. m_rotate_angle=m_rotate_angle+90.0;
  753. break;
  754. case 98://小键盘的down
  755. m_rotate_angle=m_rotate_angle-90.0;
  756. break;
  757. case 65://a
  758. eye.x=eye.x+1.0;
  759. break;
  760.     case 83://s
  761. eye.y=eye.y+1.0;
  762. break;
  763.     case 68://d
  764. eye.z=eye.z+1.0;
  765. break;
  766. default:
  767. break;
  768. }
  769. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  770.     Invalidate();
  771. }
  772. void CDelaunayView::DrawTris()
  773. {
  774. POSITION POS;
  775. CTriangle* pTriangle;
  776. POS = m_pDoc->m_tri.GetHeadPosition();
  777. if(m_Draw_what==DO_LINE)
  778. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  779.     else{ 
  780.     glPolygonMode(GL_FRONT,GL_FILL);
  781.     glPolygonMode(GL_BACK,GL_FILL);
  782. }
  783. while(POS != NULL ){
  784.   DrawTri(m_pDoc->m_tri.GetAt(POS));
  785.       pTriangle=m_pDoc->m_tri.GetNext(POS);
  786. }   
  787. }
  788. void CDelaunayView::DrawPoints()
  789. {
  790. int max=m_pDoc->m_point.GetSize();
  791. glPointSize(6.0);
  792. for(int i=0;i<max;i++)
  793. {
  794.    glColor4d(double(i)/double(max),1.0,0.0,0.0);
  795.    DrawPoint(i);
  796. }
  797. }
  798. void CDelaunayView::OnCorlorMaterial() 
  799. {
  800.    CLightMaterial dlg;
  801.    dlg.m_colorRed=m_colorRed;
  802.    dlg.m_colorGreen=m_colorGreen;
  803.    dlg.m_colorBlue=m_colorBlue;
  804.    dlg.m_bMaskRed=m_bMaskRed;
  805.    dlg.m_bMaskGreen=m_bMaskGreen;
  806.    dlg.m_bMaskBlue=m_bMaskBlue;
  807.    dlg.m_matAmbientR=m_materialAmb[0];
  808.    dlg.m_matAmbientG=m_materialAmb[1];
  809.    dlg.m_matAmbientB=m_materialAmb[2];
  810.    dlg.m_matDiffuseR=m_materialDif[0];
  811.    dlg.m_matDiffuseG=m_materialDif[1];
  812.    dlg.m_matDiffuseB=m_materialDif[2];
  813.    dlg.m_matSpecularR=m_materialSpe[0];
  814.    dlg.m_matSpecularG=m_materialSpe[1];
  815.    dlg.m_matSpecularB=m_materialSpe[2];
  816.    dlg.m_matEmissionR=m_materialEmi[0];
  817.    dlg.m_matEmissionG=m_materialEmi[1];
  818.    dlg.m_matEmissionB=m_materialEmi[2];
  819.    dlg.m_matShininess=m_matshininess;
  820.    dlg.m_bMaterialOnOff=m_bMaterial;
  821.    int responeDlg=dlg.DoModal();
  822.    if(responeDlg==IDOK){
  823.    m_colorRed=dlg.m_colorRed;
  824.        m_colorGreen=dlg.m_colorGreen;
  825.        m_colorBlue=dlg.m_colorBlue;
  826.        m_bMaskRed=dlg.m_bMaskRed;
  827.        m_bMaskGreen=dlg.m_bMaskGreen;
  828.        m_bMaskBlue=dlg.m_bMaskBlue;
  829.    m_materialAmb[0]=dlg.m_matAmbientR;
  830.        m_materialAmb[1]=dlg.m_matAmbientG;
  831.        m_materialAmb[2]=dlg.m_matAmbientB;
  832.        m_materialDif[0]=dlg.m_matDiffuseR;
  833.        m_materialDif[1]=dlg.m_matDiffuseG;
  834.        m_materialDif[2]=dlg.m_matDiffuseB;
  835.        m_materialSpe[0]=dlg.m_matSpecularR;
  836.        m_materialSpe[1]=dlg.m_matSpecularG;
  837.    m_materialSpe[2]=dlg.m_matSpecularB;
  838.        m_materialEmi[0]=dlg.m_matEmissionR;
  839.        m_materialEmi[1]=dlg.m_matEmissionG;
  840.        m_materialEmi[2]=dlg.m_matEmissionB;
  841.        m_matshininess=dlg.m_matShininess;
  842.        m_bMaterial=dlg.m_bMaterialOnOff;
  843.    Invalidate();
  844.    }
  845. }
  846. void CDelaunayView::OnUpdateCorlorMaterial(CCmdUI* pCmdUI) 
  847. {
  848. // TODO: Add your command update UI handler code here
  849. }
  850. void CDelaunayView::CreateLights()
  851. {
  852. glLightfv(GL_LIGHT0,GL_AMBIENT,m_lightAmb);
  853. glLightfv(GL_LIGHT0,GL_DIFFUSE,m_lightDif);
  854. glLightfv(GL_LIGHT0,GL_SPECULAR,m_lightSpe);
  855. glLightfv(GL_LIGHT0,GL_POSITION,m_lightPos);
  856. glEnable(GL_LIGHTING);
  857. glEnable(GL_LIGHT0);
  858. }
  859. void CDelaunayView::OnUpdateLight(CCmdUI* pCmdUI) 
  860. {
  861. // TODO: Add your command update UI handler code here
  862. }
  863. void CDelaunayView::OnLight() 
  864. {
  865. CLighting lightDlg;
  866. lightDlg.m_lightAmbientR=m_lightAmb[0];
  867. lightDlg.m_lightAmbientG=m_lightAmb[1];
  868. lightDlg.m_lightAmbientB=m_lightAmb[2];
  869. lightDlg.m_lightDiffuseR=m_lightDif[0];
  870. lightDlg.m_lightDiffuseG=m_lightDif[1];
  871. lightDlg.m_lightDiffuseB=m_lightDif[2];
  872. lightDlg.m_lightSpecularR=m_lightSpe[0];
  873. lightDlg.m_lightSpecularG=m_lightSpe[1];
  874. lightDlg.m_lightSpecularB=m_lightSpe[2];
  875. lightDlg.m_lightPositionX=m_lightPos[0];
  876. lightDlg.m_lightPositionY=m_lightPos[1];
  877. lightDlg.m_lightPositionZ=m_lightPos[2];
  878. int responeDlg=lightDlg.DoModal();
  879. if(responeDlg==IDOK){
  880. m_lightAmb[0]=lightDlg.m_lightAmbientR;
  881. m_lightAmb[1]=lightDlg.m_lightAmbientG;
  882. m_lightAmb[2]=lightDlg.m_lightAmbientB;
  883.         m_lightDif[0]=lightDlg.m_lightDiffuseR;
  884. m_lightDif[1]=lightDlg.m_lightDiffuseG;
  885. m_lightDif[2]=lightDlg.m_lightDiffuseB;
  886.         m_lightSpe[0]=lightDlg.m_lightSpecularR;
  887. m_lightSpe[1]=lightDlg.m_lightSpecularG;
  888. m_lightSpe[2]=lightDlg.m_lightSpecularB;
  889. m_lightPos[0]=lightDlg.m_lightPositionX;
  890. m_lightPos[1]=lightDlg.m_lightPositionY;
  891. m_lightPos[2]=lightDlg.m_lightPositionZ;
  892. Invalidate();
  893. }
  894. }
  895. void CDelaunayView::CreateMaterial()
  896. {
  897. glMaterialfv(GL_FRONT,GL_AMBIENT,m_materialAmb);
  898. glMaterialfv(GL_FRONT,GL_DIFFUSE,m_materialDif);
  899. glMaterialfv(GL_FRONT,GL_SPECULAR,m_materialSpe);
  900. glMaterialfv(GL_FRONT,GL_EMISSION,m_materialEmi);
  901. glMaterialf(GL_FRONT,GL_SHININESS,m_matshininess);
  902. }
  903. void CDelaunayView::OnButtonTwo() 
  904. {
  905. m_Dimension=DO_DRAW2;
  906. Invalidate();
  907. }
  908. void CDelaunayView::OnUpdateButtonTwo(CCmdUI* pCmdUI) 
  909. {
  910. if(m_Dimension==DO_DRAW2)
  911. pCmdUI->SetCheck(1);
  912. else 
  913. pCmdUI->SetCheck(0);
  914. }
  915. void CDelaunayView::OnButtonThree() 
  916. {
  917. m_Dimension=DO_DRAW3;
  918. Invalidate();
  919. }
  920. void CDelaunayView::OnUpdateButtonThree(CCmdUI* pCmdUI) 
  921. {
  922.     if(m_Dimension==DO_DRAW3)
  923. pCmdUI->SetCheck(1);
  924. else 
  925. pCmdUI->SetCheck(0);
  926. }
  927. void CDelaunayView::HCT(CTriangle *temp, int p)
  928. {//每次处理1/3三角形
  929. m_hct[6].x=temp->m_x;//重心
  930. m_hct[6].y=temp->m_y;
  931. m_hct[6].z=temp->o;
  932. if(p==3){
  933.     m_hct[0].x=m_pDoc->m_point[temp->m_p1]->m_x;//f1
  934.     m_hct[0].y=m_pDoc->m_point[temp->m_p1]->m_y;
  935.     m_hct[0].z=m_pDoc->m_point[temp->m_p1]->m_z;
  936. m_hct[9].x=m_pDoc->m_point[temp->m_p2]->m_x;//f2
  937.     m_hct[9].y=m_pDoc->m_point[temp->m_p2]->m_y;
  938.     m_hct[9].z=m_pDoc->m_point[temp->m_p2]->m_z;
  939.     m_hct[1]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[6].x,m_hct[6].y);//c1
  940.     m_hct[1].z=temp->c1;//c1
  941.     m_hct[2]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[9].x,m_hct[9].y);//d31
  942.         m_hct[2].z=temp->d31;//d31
  943.         m_hct[4]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[0].x,m_hct[0].y);//b1
  944.         m_hct[4].z=temp->b1;//b1
  945.         m_hct[3]=BaryCenter(m_hct[0].x,m_hct[0].y,0,m_hct[9].x,m_hct[9].y,0,
  946.  m_hct[6].x,m_hct[6].y,0);//e3
  947.         m_hct[3].z=temp->e3;//e3
  948.         m_hct[5]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[9].x,m_hct[9].y);//b2
  949.         m_hct[5].z=temp->b2;//b2
  950. m_hct[7]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[6].x,m_hct[6].y);//c2
  951.         m_hct[7].z=temp->c2;//c2
  952. m_hct[8]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[0].x,m_hct[0].y);//d32
  953.         m_hct[8].z=temp->d32;//d32
  954. }
  955. if(p==2){
  956.     m_hct[0].x=m_pDoc->m_point[temp->m_p1]->m_x;//f1
  957.     m_hct[0].y=m_pDoc->m_point[temp->m_p1]->m_y;
  958.     m_hct[0].z=m_pDoc->m_point[temp->m_p1]->m_z;
  959. m_hct[9].x=m_pDoc->m_point[temp->m_p3]->m_x;//f3
  960.     m_hct[9].y=m_pDoc->m_point[temp->m_p3]->m_y;
  961.     m_hct[9].z=m_pDoc->m_point[temp->m_p3]->m_z;
  962.     m_hct[1]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[6].x,m_hct[6].y);//c1
  963.     m_hct[1].z=temp->c1;//c1
  964.     m_hct[2]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[9].x,m_hct[9].y);//d21
  965.         m_hct[2].z=temp->d21;
  966.        
  967. m_hct[4]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[0].x,m_hct[0].y);//b1
  968.         m_hct[4].z=temp->b1;//b1
  969.         m_hct[3]=BaryCenter(m_hct[0].x,m_hct[0].y,0,m_hct[6].x,m_hct[6].y,0,
  970.  m_hct[9].x,m_hct[9].y,0);//e2
  971.         m_hct[3].z=temp->e2;//e2
  972.        
  973. m_hct[5]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[9].x,m_hct[9].y);//b3
  974.         m_hct[5].z=temp->b3;//b3
  975. m_hct[7]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[6].x,m_hct[6].y);//c3
  976.         m_hct[7].z=temp->c3;//c3
  977. m_hct[8]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[0].x,m_hct[0].y);//d23
  978.         m_hct[8].z=temp->d23;//d23
  979. }
  980. if(p==1){
  981.     m_hct[0].x=m_pDoc->m_point[temp->m_p2]->m_x;//f2
  982.     m_hct[0].y=m_pDoc->m_point[temp->m_p2]->m_y;
  983.     m_hct[0].z=m_pDoc->m_point[temp->m_p2]->m_z;
  984. m_hct[9].x=m_pDoc->m_point[temp->m_p3]->m_x;//f3
  985.     m_hct[9].y=m_pDoc->m_point[temp->m_p3]->m_y;
  986.     m_hct[9].z=m_pDoc->m_point[temp->m_p3]->m_z;
  987.     m_hct[1]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[6].x,m_hct[6].y);//c2
  988.     m_hct[1].z=temp->c2;//c2
  989.     m_hct[2]=PointOnEdge_3(m_hct[0].x,m_hct[0].y,m_hct[9].x,m_hct[9].y);//d12
  990.         m_hct[2].z=temp->d12;//d12
  991.        
  992. m_hct[4]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[0].x,m_hct[0].y);//b2
  993.         m_hct[4].z=temp->b2;//b2
  994.         m_hct[3]=BaryCenter(m_hct[0].x,m_hct[0].y,0,m_hct[9].x,m_hct[9].y,0,
  995.  m_hct[6].x,m_hct[6].y,0);//e1
  996.         m_hct[3].z=temp->e1;//e1
  997.         
  998. m_hct[5]=PointOnEdge_3(m_hct[6].x,m_hct[6].y,m_hct[9].x,m_hct[9].y);//b3
  999.         m_hct[5].z=temp->b3;//b3
  1000. m_hct[7]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[6].x,m_hct[6].y);//c3
  1001.         m_hct[7].z=temp->c3;//c3
  1002. m_hct[8]=PointOnEdge_3(m_hct[9].x,m_hct[9].y,m_hct[0].x,m_hct[0].y);//d13
  1003.         m_hct[8].z=temp->d13;//d13
  1004. }
  1005. b[0][0][3]=m_hct[6].z;b[0][3][0]=m_hct[9].z;b[3][0][0]=m_hct[0].z;
  1006. b[0][1][2]=m_hct[5].z;b[0][2][1]=m_hct[7].z;
  1007. b[1][0][2]=m_hct[4].z;b[2][0][1]=m_hct[1].z;
  1008. b[2][1][0]=m_hct[2].z;b[1][2][0]=m_hct[8].z;b[1][1][1]=m_hct[3].z;
  1009.     
  1010. for(int i=1;i<9;i++){
  1011. if(i!=6){
  1012. m_hct[i].z=Bezier(m_hct[i]);//得到所有细分点的z值
  1013. }
  1014. }
  1015. }
  1016. POI CDelaunayView::BaryCenter(double x1, double y1,double z1,double x2, double y2,double z2,double x3, double y3,double z3)
  1017. {//平面上三角形重心
  1018. POI temp;
  1019.     temp.x=(x1+x2+x3)/double(3);
  1020. temp.y=(y1+y2+y3)/double(3);
  1021. temp.z=(z1+z2+z3)/double(3);
  1022.     return temp;
  1023. }
  1024. POI CDelaunayView::PointOnEdge_3(double x1, double y1, double x2, double y2)
  1025. {
  1026. POI temp;
  1027. temp.x=x1+(x2-x1)/double(3);
  1028. temp.y=y1+(y2-y1)/double(3);
  1029. return temp;
  1030. }
  1031. void CDelaunayView::OnButtonHct() 
  1032. {
  1033. if(m_HCT==DO_HCT)
  1034.     m_HCT=-1;
  1035. else
  1036. m_HCT=DO_HCT;
  1037. }
  1038. void CDelaunayView::OnUpdateButtonHct(CCmdUI* pCmdUI) 
  1039. {
  1040.     
  1041. }
  1042. void CDelaunayView::OnButtonFill() 
  1043. {
  1044.     m_Draw_what=DO_FILL;
  1045. }
  1046. void CDelaunayView::OnUpdateButtonFill(CCmdUI* pCmdUI) 
  1047. {
  1048. if(m_Draw_what==DO_FILL)
  1049. pCmdUI->SetCheck(1);
  1050. else 
  1051. pCmdUI->SetCheck(0);
  1052. }
  1053. void CDelaunayView::OnButtonLine() 
  1054. {
  1055. m_Draw_what=DO_LINE;
  1056. }
  1057. void CDelaunayView::OnUpdateButtonLine(CCmdUI* pCmdUI) 
  1058. {
  1059.     if(m_Draw_what==DO_LINE)
  1060. pCmdUI->SetCheck(1);
  1061. else 
  1062. pCmdUI->SetCheck(0);
  1063. }
  1064. POI CDelaunayView::GetTriNormal(POI p1, POI p2, POI p3)
  1065. { //得到三角片的单位法向
  1066. POI vector1,vector2,Normal;
  1067.     /*POI p;
  1068. double s;
  1069. s=S(p1,p2,p3);
  1070. if(s<0){
  1071. p=p2;p2=p3;p3=p;
  1072. }*/
  1073. vector1.x=p2.x-p1.x;
  1074. vector1.y=p2.y-p1.y;
  1075. vector1.z=p2.z-p1.z;
  1076. vector2.x=p3.x-p1.x;
  1077. vector2.y=p3.y-p1.y;
  1078. vector2.z=p3.z-p1.z;
  1079.     //get the normal n=a*b(外积)
  1080. Normal=m_pDoc->VectorProduct(vector1.x,vector1.y,vector1.z,vector2.x,vector2.y,vector2.z);
  1081. //单位化 normal
  1082. Normal=m_pDoc->Unitization(Normal);
  1083.     return Normal;
  1084. }
  1085. double CDelaunayView::S(POI p1, POI p2, POI p3)
  1086. { //求三角形面积,以右下角为原点时,s>0为逆时针(左转),
  1087.    //s=0为三点重合
  1088.    double s;
  1089.    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;
  1090.    s=s/2.0;
  1091.    return s;
  1092. }
  1093. int CDelaunayView::Factorial(int n)
  1094. {//阶乘
  1095. int x=n;
  1096. if(n==0 ||n==1) 
  1097. return 1;
  1098. else{
  1099. for(int i=1;i<n;i++){
  1100. x=x*(n-i);
  1101. }
  1102. }
  1103.     return x;
  1104. }
  1105. double CDelaunayView::Power(double a, int e)
  1106. {
  1107. double x=1.0;
  1108. if(e==0){
  1109. return 1.0;
  1110. }
  1111. else{
  1112. if(a==0.0){
  1113. return 0.0;
  1114. }
  1115.         for(int i=1;i<=e;i++){
  1116. x=x*a;
  1117. }
  1118. }
  1119.     return x;
  1120. }
  1121. double CDelaunayView::Bezier(POI p)
  1122. {//s1,s2,s3 : 面积坐标
  1123. double s1,s2,s3,s;
  1124. s=S(m_hct[0],m_hct[9],m_hct[6]);
  1125. s1=S(p,m_hct[9],m_hct[6])/s;
  1126. s2=S(m_hct[0],p,m_hct[6])/s;
  1127. s3=S(m_hct[0],m_hct[9],p)/s;
  1128. if(fabs(s1)<0.000000000001) s1=0;
  1129. if(fabs(s2)<0.000000000001) s2=0;
  1130. if(fabs(s3)<0.000000000001) s3=0;
  1131. int i,j,k;
  1132. double B=0.0;
  1133. for(i=0;i<4;i++){
  1134. for(j=0;j<=(3-i);j++){
  1135. for(k=0;k<=(3-i-j);k++){
  1136. if((i+j+k)==3){
  1137. B=B+b[i][j][k]*double(6)/(Factorial(i)*Factorial(j)*Factorial(k))*
  1138. Power(s1,i)*Power(s2,j)*Power(s3,k);
  1139. }
  1140. }
  1141. }
  1142. }
  1143. return B;
  1144. }
  1145. void CDelaunayView::OnButtonWnag() 
  1146. {
  1147.     m_Draw_what=DO_WANG;
  1148. }