www.pudn.com > vcframe.rar > matchDoc.cpp
// matchDoc.cpp : implementation of the CMatchDoc class // #include "stdafx.h" #include "match.h" #include "matchView.h" #include "matchDoc.h" #include "DlgPipei.h" #include "RightView.h" #include#include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMatchDoc IMPLEMENT_DYNCREATE(CMatchDoc, CDocument) BEGIN_MESSAGE_MAP(CMatchDoc, CDocument) //{{AFX_MSG_MAP(CMatchDoc) ON_COMMAND(ID_OPENIMAGE, OnOpenimage) ON_COMMAND(ID_CAIJI_JIESUAN, OnCaijiJiesuan) ON_COMMAND(ID_MATCHING, OnMatching) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMatchDoc construction/destruction CMatchDoc::CMatchDoc() { // TODO: add one-time construction code here // 默认背景色,灰色 m_refColorBKG = 0x00808080; // 初始化变量 m_hDIB = NULL; hDibL=NULL; hDibR=NULL; m_palDIB = NULL; m_sizeDoc = CSize(1,1); m_bIsInterior=FALSE; } CMatchDoc::~CMatchDoc() { // 判断DIB对象是否存在 if (m_hDIB != NULL) { // 清除DIB对象 ::GlobalFree((HGLOBAL) m_hDIB); } // 判断调色板是否存在 if (m_palDIB != NULL) { // 清除调色板 delete m_palDIB; } } BOOL CMatchDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMatchDoc serialization void CMatchDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here } } ///////////////////////////////////////////////////////////////////////////// // CMatchDoc diagnostics #ifdef _DEBUG void CMatchDoc::AssertValid() const { CDocument::AssertValid(); } void CMatchDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMatchDoc commands BOOL CMatchDoc::OnOpenDocument(LPCTSTR lpszPathName) { CFile file; CFileException fe; // 打开文件 if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe)) { // 失败 ReportSaveLoadException(lpszPathName, &fe, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); // 返回FALSE return FALSE; } DeleteContents(); // 更改光标形状 BeginWaitCursor(); // 尝试调用ReadDIBFile()读取图像 TRY { m_hDIB = ::ReadDIBFile(file); } CATCH (CFileException, eLoad) { // 读取失败 file.Abort(); // 恢复光标形状 EndWaitCursor(); // 报告失败 ReportSaveLoadException(lpszPathName, eLoad, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); // 设置DIB为空 m_hDIB = NULL; // 返回FALSE return FALSE; } END_CATCH // 初始化DIB InitDIBData(); // 恢复光标形状 EndWaitCursor(); // 判断读取文件是否成功 if (m_hDIB == NULL) { // 失败,可能非BMP格式 CString strMsg; strMsg = "读取图像时出错!可能是不支持该类型的图像文件!"; // 提示出错 MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK); // 返回FALSE return FALSE; } // 设置文件名称 SetPathName(lpszPathName); // 初始化胀标记为FALSE SetModifiedFlag(FALSE); // 返回TRUE return TRUE; } void CMatchDoc::InitDIBData() { // 初始化DIB对象 // 判断调色板是否为空 if (m_palDIB != NULL) { // 删除调色板对象 delete m_palDIB; // 重置调色板为空 m_palDIB = NULL; } // 如果DIB对象为空,直接返回 if (m_hDIB == NULL) { // 返回 return; } LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); // 判断图像是否过大 if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX) { ::GlobalUnlock((HGLOBAL) m_hDIB); // 释放DIB对象 ::GlobalFree((HGLOBAL) m_hDIB); // 设置DIB为空 m_hDIB = NULL; CString strMsg; strMsg = "BMP图像太大!"; // 提示用户 MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK); // 返回 return; } // 设置文档大小 m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB)); ::GlobalUnlock((HGLOBAL) m_hDIB); // 创建新调色板 m_palDIB = new CPalette; // 判断是否创建成功 if (m_palDIB == NULL) { // 失败,可能是内存不足 ::GlobalFree((HGLOBAL) m_hDIB); // 设置DIB对象为空 m_hDIB = NULL; // 返回 return; } // 调用CreateDIBPalette来创建调色板 if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL) { // 返回空,可能该DIB对象没有调色板 // 删除 delete m_palDIB; // 设置为空 m_palDIB = NULL; // 返回 return; } } void CMatchDoc::OnOpenimage() { CDlgPipei mydlg; if(mydlg.DoModal()==IDOK) { hDibL = mydlg.hDibL; hDibR = mydlg.hDibR; } else return; POSITION ps=GetFirstViewPosition(); CView* pView; CMatchView* pMatchView; CRightView* pRightView; while (ps != NULL) { pView = GetNextView(ps); CRuntimeClass* prt = pView->GetRuntimeClass(); if( lstrcmp( prt->m_lpszClassName, "CMatchView" ) == 0 ) { pMatchView=(CMatchView*)pView; } if(lstrcmp( prt->m_lpszClassName, "CRightView" ) == 0 ) pRightView=(CRightView*)pView; } pMatchView->hDib=hDibL; pRightView->hDib=hDibR; UpdateAllViews(NULL); } //相对定向迭代求解五个旋角 void CMatchDoc::OnCaijiJiesuan() { // TODO: Add your command handler code here CMatchView* pMatchView=NULL; CRightView* pRightView=NULL; POSITION pos = GetFirstViewPosition(); CView* pview; while (pos != NULL) { pview = GetNextView(pos); CRuntimeClass* prt = pview->GetRuntimeClass(); if(pview->IsKindOf(RUNTIME_CLASS(CMatchView))) { pMatchView=(CMatchView*)pview; } else if(pview->IsKindOf(RUNTIME_CLASS(CRightView))) { pRightView=(CRightView*)pview; } } int i,j,k; double xl[15],yl[15],zl[15],xr[15],yr[15],zr[15]; double x2[15],x1[15],y1[15],y2[15]; double *l_m; double *l_n; double *r_m; double *r_n; double sm; double f; double jiao[5],q[15],a[5][15]; double N[5][5],ATb_q[5]; CPoint pointl[15]; CPoint pointr[15]; double t_1,t_2,delt_w,k_10,k_20; double a1,a2,a3,b1,b2,b3,c1,c2,c3; double ar1,ar2,ar3,br1,br2,br3,cr1,cr2,cr3; l_m = pMatchView->lm; l_n = pMatchView->ln; r_m = pMatchView->rm; r_n = pMatchView->rn; sm = pMatchView->sm; f = pMatchView->f; //读入同名点 int n,m,s; CFileDialog m_dlg(TRUE,"*.txt",NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "txt文件(*.txt)|*.txt|所有文件(*.*)|*.*||",NULL); if(m_dlg.DoModal()==IDCANCEL) return; CString pathname = m_dlg.GetPathName(); FILE *stream; if( (stream = fopen( pathname, "rt" )) != NULL ) fscanf(stream ,"%d",&n); for(i=0;i t_1 ; t_2=pMatchView->t_2; delt_w=pMatchView->delt_w ; k_10=pMatchView->k_10 ; k_20=pMatchView->k_20 ; //迭代求解 int pp;pp=0; do { //左 a1 = cos(t_1)*cos(k_10); a2 = -cos(t_1)*sin(k_10); a3 = -sin(t_1); b1 = sin(k_10); b2 = cos(k_10); b3 = 0; c1 = sin(t_1)*cos(k_10); c2 = -sin(t_1)*sin(k_10); c3 = cos(t_1); for( i=0;i max) { max=N[i][k-1];rec=i; } exchange2=ATb_q[k-1];ATb_q[k-1]=ATb_q[rec];ATb_q[rec]=exchange2; for(i=0;i<5;i++) { exchange1[i]=N[k-1][i]; N[k-1][i]=N[rec][i];N[rec][i]=exchange1[i]; } } for(i=k;i<5;i++) { for(j=k;j<5;j++) { N[i][j] = N[i][j] - N[k-1][j]*N[i][k-1]/N[k-1][k-1]; } ATb_q[i] = ATb_q[i] - ATb_q[k-1]*N[i][k-1]/N[k-1][k-1]; } } jiao[4] =ATb_q[4]/N[4][4]; jiao[3] =(ATb_q[3]-N[3][4]*jiao[4])/N[3][3]; jiao[2] =(ATb_q[2]-N[2][4]*jiao[4]-N[2][3]*jiao[3])/N[2][2]; jiao[1] =(ATb_q[1]-N[1][4]*jiao[4]-N[1][3]*jiao[3]-N[1][2]*jiao[2])/N[1][1]; jiao[0] =(ATb_q[0]-N[0][4]*jiao[4]-N[0][3]*jiao[3]-N[0][2]*jiao[2]-N[0][1]*jiao[1])/N[0][0]; //进行改正 t_1=t_1 +jiao[0]; k_10=k_10+jiao[1]; delt_w=delt_w+jiao[2]; t_2 =t_2+jiao[3]; k_20=k_20+jiao[4]; pp++; if(pp>10)break; } while(fabs(jiao[0])>0.0001||fabs(jiao[1])>0.0001||fabs(jiao[2])>0.0001||fabs(jiao[3])>0.0001||fabs(jiao[4])>0.0001); pMatchView->t_1 = t_1; pMatchView->t_2 =t_2; pMatchView->delt_w =delt_w; pMatchView->k_10 =k_10; pMatchView->k_20 =k_20; _mkdir("E:\\IO"); CFile file; CFileException fe; char tmp[200]; file.Open("E:\\IO\\xiangdui.xd",CFile::modeCreate|CFile::modeWrite,&fe); j = sprintf(tmp,"%lf %lf %lf %lf %lf",t_1,k_10,delt_w,t_2,k_20); file.Write(tmp,j); file.Close(); } //匹配 void CMatchDoc::OnMatching() { // TODO: Add your command handler code here int yleft,yright,cha,lieleft,hangleft,lieright,hangright; int i,j,k,l,n,p,Ln,Col; BYTE g,gp; double Sgg,Sg; double Sgp,Sggp,Sgpgp; double ro; double max; int wid,squ; //LPSTR lpDIB; //lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDibL); CPoint pointR; CMatchView* pMatchView=NULL; CRightView* pRightView=NULL; POSITION pos = GetFirstViewPosition(); CView* pview; while (pos != NULL) { pview = GetNextView(pos); CRuntimeClass* prt = pview->GetRuntimeClass(); if(pview->IsKindOf(RUNTIME_CLASS(CMatchView))) { pMatchView=(CMatchView*)pview; } else if(pview->IsKindOf(RUNTIME_CLASS(CRightView))) { pRightView=(CRightView*)pview; } } pRightView->m_ArrayDot.RemoveAll(); pMatchView->m_ArrayDot.RemoveAll(); hDibL=pMatchView->hDib; hDibR=pRightView->hDib; LPSTR lpDIBBitsL = FindDIBBits((LPSTR) ::GlobalLock((HGLOBAL) hDibL)); LPSTR lpDIBBitsR = FindDIBBits((LPSTR) ::GlobalLock((HGLOBAL) hDibR)); /* yleft = pMatchView->y_left; yright = pMatchView->y_right; lieleft = pMatchView->lie_left; hangleft = pMatchView->hang_left; lieright = pMatchView->lie_right; hangright = pMatchView->hang_right; cha=int((yright-yleft)/100.0+1);*/ lieleft = 4556; hangleft = 4527; lieright = 4666; hangright = 4597; cha=214; LONG lLineBytes = WIDTHBYTES(lieleft * 8); LONG rLineBytes = WIDTHBYTES(lieright * 8); n=pMatchView->m_ArrayDot.GetSize(); wid=5;squ=pow((2*wid+1),2); //窗口大小 for(i=0;i m_ArrayDot.GetAt(i)).y; Ln=hangleft-(pMatchView->m_ArrayDot.GetAt(i)).y-1; Col=(pMatchView->m_ArrayDot.GetAt(i)).x; max=0;Sgg=0;Sg=0; for(j=-wid;j max) {max=fabs(ro);pointR.y=hangright-(Ln+cha+p)-1;pointR.x=l;} } // pRightView->(*(pointr+i))=*(pointR+i); //pRightView->pointr=pointR; } pRightView->m_ArrayDot.Add(pointR); } //右图画 pRightView->m=n; pRightView->flag=true; pRightView->Invalidate(FALSE); ::GlobalUnlock((HGLOBAL) hDibL); ::GlobalUnlock((HGLOBAL) hDibR); }