ControlPoint3DDlg.cpp
上传用户:zj_jiuzhou
上传日期:2022-06-20
资源大小:1859k
文件大小:9k
源码类别:

3D图形编程

开发平台:

Visual C++

  1. // ControlPoint3DDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ControlPoint3D.h"
  5. #include "ControlPoint3DDlg.h"
  6. #include "InputPoint.h"
  7. #include "stdlib.h"
  8. #include "math.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define  PI 3.141592653
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. //{{AFX_DATA(CAboutDlg)
  23. enum { IDD = IDD_ABOUTBOX };
  24. //}}AFX_DATA
  25. // ClassWizard generated virtual function overrides
  26. //{{AFX_VIRTUAL(CAboutDlg)
  27. protected:
  28. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  29. //}}AFX_VIRTUAL
  30. // Implementation
  31. protected:
  32. //{{AFX_MSG(CAboutDlg)
  33. //}}AFX_MSG
  34. DECLARE_MESSAGE_MAP()
  35. };
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAboutDlg)
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  48. //{{AFX_MSG_MAP(CAboutDlg)
  49. // No message handlers
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CControlPoint3DDlg dialog
  54. CControlPoint3DDlg::CControlPoint3DDlg(CWnd* pParent /*=NULL*/)
  55. : CDialog(CControlPoint3DDlg::IDD, pParent)
  56. {
  57. //{{AFX_DATA_INIT(CControlPoint3DDlg)
  58. m_strPath = _T("");
  59. //}}AFX_DATA_INIT
  60. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. m_Sab=0;
  63. bCal = false;
  64. }
  65. void CControlPoint3DDlg::DoDataExchange(CDataExchange* pDX)
  66. {
  67. CDialog::DoDataExchange(pDX);
  68. //{{AFX_DATA_MAP(CControlPoint3DDlg)
  69. DDX_Control(pDX, IDC_LIST_INFO, m_ListContl_Info);
  70. DDX_Text(pDX, IDC_EDIT_PATH, m_strPath);
  71. //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(CControlPoint3DDlg, CDialog)
  74. //{{AFX_MSG_MAP(CControlPoint3DDlg)
  75. ON_WM_SYSCOMMAND()
  76. ON_WM_PAINT()
  77. ON_WM_QUERYDRAGICON()
  78. ON_BN_CLICKED(IDC_BUTN_READ, OnButnRead)
  79. ON_BN_CLICKED(IDC_BUTN_EXPORT, OnButnExport)
  80. ON_BN_CLICKED(IDC_BUTN_CAL, OnButnCal)
  81. //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CControlPoint3DDlg message handlers
  85. BOOL CControlPoint3DDlg::OnInitDialog()
  86. {
  87. CDialog::OnInitDialog();
  88. // Add "About..." menu item to system menu.
  89. // IDM_ABOUTBOX must be in the system command range.
  90. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  91. ASSERT(IDM_ABOUTBOX < 0xF000);
  92. CMenu* pSysMenu = GetSystemMenu(FALSE);
  93. if (pSysMenu != NULL)
  94. {
  95. CString strAboutMenu;
  96. strAboutMenu.LoadString(IDS_ABOUTBOX);
  97. if (!strAboutMenu.IsEmpty())
  98. {
  99. pSysMenu->AppendMenu(MF_SEPARATOR);
  100. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  101. }
  102. }
  103. // Set the icon for this dialog.  The framework does this automatically
  104. //  when the application's main window is not a dialog
  105. SetIcon(m_hIcon, TRUE); // Set big icon
  106. SetIcon(m_hIcon, FALSE); // Set small icon
  107. // TODO: Add extra initialization here
  108. //初始化列表
  109. m_ListContl_Info.InsertColumn(0," 点号",LVCFMT_LEFT,50,0);
  110.     m_ListContl_Info.InsertColumn(1,"       X",LVCFMT_LEFT,100,0);
  111.     m_ListContl_Info.InsertColumn(2,"       Y",LVCFMT_LEFT,100,0);
  112. m_ListContl_Info.InsertColumn(3,"       Z",LVCFMT_LEFT,100,0);
  113. return TRUE;  // return TRUE  unless you set the focus to a control
  114. }
  115. void CControlPoint3DDlg::OnSysCommand(UINT nID, LPARAM lParam)
  116. {
  117. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  118. {
  119. CAboutDlg dlgAbout;
  120. dlgAbout.DoModal();
  121. }
  122. else
  123. {
  124. CDialog::OnSysCommand(nID, lParam);
  125. }
  126. }
  127. // If you add a minimize button to your dialog, you will need the code below
  128. //  to draw the icon.  For MFC applications using the document/view model,
  129. //  this is automatically done for you by the framework.
  130. void CControlPoint3DDlg::OnPaint() 
  131. {
  132. if (IsIconic())
  133. {
  134. CPaintDC dc(this); // device context for painting
  135. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  136. // Center icon in client rectangle
  137. int cxIcon = GetSystemMetrics(SM_CXICON);
  138. int cyIcon = GetSystemMetrics(SM_CYICON);
  139. CRect rect;
  140. GetClientRect(&rect);
  141. int x = (rect.Width() - cxIcon + 1) / 2;
  142. int y = (rect.Height() - cyIcon + 1) / 2;
  143. // Draw the icon
  144. dc.DrawIcon(x, y, m_hIcon);
  145. }
  146. else
  147. {
  148. CDialog::OnPaint();
  149. }
  150. }
  151. // The system calls this to obtain the cursor to display while the user drags
  152. //  the minimized window.
  153. HCURSOR CControlPoint3DDlg::OnQueryDragIcon()
  154. {
  155. return (HCURSOR) m_hIcon;
  156. }
  157. void CControlPoint3DDlg::OnOK() 
  158. {
  159. // TODO: Add extra validation here
  160. m_inPointList.RemoveAll();
  161. m_strPath=" ";
  162. m_ListContl_Info.DeleteAllItems();
  163. SetDlgItemText(IDC_STATIC_TIPS,"");
  164. UpdateData(FALSE);
  165. //CDialog::OnOK();
  166. }
  167. void CControlPoint3DDlg::OnButnRead() 
  168. {
  169. // TODO: Add your control notification handler code here
  170. CFileDialog openFile(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"文本文件(*.txt)|*.txt||",NULL);
  171. if (openFile.DoModal()==IDCANCEL)
  172. return ;
  173. m_strPath = openFile.GetPathName();
  174. UpdateData(false);
  175. //read data
  176. CInputPoint *pPoint;
  177. FILE * pInFile;
  178. pInFile = fopen(m_strPath,"r");
  179. if (pInFile!=NULL)
  180. {
  181. //第一行基线长度
  182. fscanf(pInFile,"%lf",&m_Sab);
  183. //第二行以后每行一个点的测量纪录
  184. while(!feof(pInFile))
  185. {
  186.             int nOrderNum =0 ;//point order number
  187. double LeftHorizontalAngle = 0.0;//左站水平角   
  188. double LeftZenithdistance = 0.0;//左站天顶距
  189. double RightHorizontalAngle = 0.0;//右站水平角
  190.         double RightZenithdistance = 0.0;//右站天顶距
  191. fscanf(pInFile,"%d%lf%lf%lf%lf",&nOrderNum,&LeftHorizontalAngle,&LeftZenithdistance,
  192. &RightHorizontalAngle,&RightZenithdistance);
  193. pPoint = new CInputPoint();
  194. pPoint->m_nOrderNum = nOrderNum;
  195. pPoint->m_LeftHorizontalAngle = 2*PI - ConvertAngleToRadian(LeftHorizontalAngle);
  196. pPoint->m_LeftZenithdistance = PI/2 - ConvertAngleToRadian(LeftZenithdistance);
  197. pPoint->m_RightHorizontalAngle = PI/2 - ConvertAngleToRadian(RightHorizontalAngle);
  198. pPoint->m_RightZenithdistance = ConvertAngleToRadian(RightZenithdistance);
  199.     m_inPointList.AddTail(pPoint);
  200. }
  201. fclose(pInFile);
  202. }
  203. else
  204. {
  205. MessageBox("Can't open the file");
  206. }
  207. }
  208. void CControlPoint3DDlg::OnButnExport() 
  209. {
  210. // TODO: Add your control notification handler code here
  211. //输出控制点三维坐标
  212. if (m_strPath=="")
  213. {
  214. MessageBox("请先导入坐标文件!");
  215. return;
  216. }
  217. if (!bCal)
  218. {
  219. MessageBox("请先点击计算!");
  220. return; 
  221. }
  222.     CString strIndir = m_strPath.Left(m_strPath.ReverseFind('\'));//m_strPath.GetLength() -
  223. CString temp = "结果保存在:"+ strIndir + "\ControlPoint3D.txt";
  224. SetDlgItemText(IDC_STATIC_TIPS,temp);
  225. strIndir+="\ControlPoint3D.txt";
  226. FILE * fp_out;
  227. fp_out=fopen(strIndir,"w");
  228. fprintf(fp_out,"点号 X坐标 Y坐标 Z坐标n");
  229. CInputPoint * pPoint  = new CInputPoint();
  230. POSITION ps = m_inPointList.GetHeadPosition();
  231. for (int i=0;i<m_inPointList.GetCount()-1;i++)
  232. {
  233. pPoint = m_inPointList.GetNext(ps);
  234. fprintf(fp_out,"%d %lf %lf %lfn",pPoint->m_nOrderNum,pPoint->X,pPoint->Y,pPoint->Z);
  235. }
  236. fclose(fp_out);
  237. }
  238. void CControlPoint3DDlg::OnButnCal() 
  239. {
  240. // TODO: Add your control notification handler code here
  241. //清空列表中的内容
  242. m_ListContl_Info.DeleteAllItems();
  243. int PointNum = m_inPointList.GetCount();
  244. CInputPoint * pPoint  = new CInputPoint();
  245. POSITION ps = m_inPointList.GetHeadPosition();
  246. CString tempConvert;
  247. //将结果写入表中 
  248. for (int i=0;i<PointNum-1;i++)
  249. {
  250. pPoint = m_inPointList.GetNext(ps);
  251. //计算结果
  252. pPoint->Y = CalCordination(m_Sab,pPoint->m_LeftHorizontalAngle,pPoint->m_RightHorizontalAngle,&pPoint->X);
  253. pPoint->Z = CalElevation(0,0,pPoint->m_LeftZenithdistance,pPoint->m_RightZenithdistance,pPoint->X,pPoint->Y,m_Sab);
  254. //结果写入表中
  255. m_ListContl_Info.InsertItem(i,"");
  256. tempConvert.Format("%d",pPoint->m_nOrderNum);
  257. m_ListContl_Info.SetItemText(i,0,tempConvert);
  258. tempConvert.Format("%.5f",pPoint->X);
  259.         m_ListContl_Info.SetItemText(i,1,tempConvert);
  260. tempConvert.Format("%.5f",pPoint->Y);
  261.         m_ListContl_Info.SetItemText(i,2,tempConvert);
  262. tempConvert.Format("%.5f",pPoint->Z);
  263.         m_ListContl_Info.SetItemText(i,3,tempConvert);
  264. }
  265.     SetDlgItemText(IDC_STATIC_TIPS,"计算时以A为原点,n AB连线为Y轴,nA、B两点的高程均为0");
  266. bCal =true;
  267. }
  268. double CControlPoint3DDlg::CalCordination(double Sab, double lefth, double Righth, double *X)
  269. {
  270.      *X = Sab/(1/tan(lefth) + 1/tan(Righth));
  271.  return Sab/(1/tan(lefth) + 1/tan(Righth)) / tan(lefth);
  272. }
  273. double CControlPoint3DDlg::CalElevation(double Ha, double Hb,double AngleA,double AngleB, double X, double Y, double Sab)
  274. {
  275.     double h1 = Ha + sqrt(X*X + Y* Y);
  276. h1 *= cos(AngleA);
  277. double h2 = Hb + sqrt( X*X+(Y-Sab)*(Y-Sab));
  278. h2 *=cos(AngleB);
  279. return (h1+h2)/2;
  280. }
  281. double CControlPoint3DDlg::ConvertAngleToRadian(double Angle)
  282. {
  283.     int a,b,c;
  284. a=(int)Angle;
  285. b=int((Angle-a)*100);
  286. c=int(((Angle-a)*100-b)*100);
  287. return (a+b/60.0+c/3600.0)*PI/180;
  288. }