www.pudn.com > vcframe.rar > matchView.cpp
// matchView.cpp : implementation of the CMatchView class // #include "stdafx.h" #include "match.h" #include#include "matchDoc.h" #include "matchView.h" #include "mainfrm.h" #include #include #include "DIBAPI.h" #include "RightView.h" //#include "RightView.h" #define PI 3.1415926535 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMatchView IMPLEMENT_DYNCREATE(CMatchView, CScrollView) BEGIN_MESSAGE_MAP(CMatchView, CScrollView) //{{AFX_MSG_MAP(CMatchView) ON_COMMAND(ID_INTERIOR, OnInterior) ON_COMMAND(ID_INPUTWFWYS, OnInputwfwys) ON_COMMAND(ID_LEFT_CORRECT, OnLeftCorrect) ON_COMMAND(ID_RIGHT_CORRECT, OnRightCorrect) ON_WM_MOUSEWHEEL() ON_COMMAND(ID_INPUT_OBTAINEDXDFYS, OnInputObtainedxdfys) ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMatchView construction/destruction CMatchView::CMatchView() { // TODO: add construction code here flag=false; hDib=NULL; } CMatchView::~CMatchView() { } BOOL CMatchView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CScrollView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMatchView drawing void CMatchView::OnDraw(CDC* pDC) { // 显示等待光标 BeginWaitCursor(); // 获取文档 CMatchDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here // HDIB hDIB; // 获取DIB // hDIB = pDoc->hDibL; // 判断DIB是否为空 if (hDib != NULL) { LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDib); // 获取DIB宽度 int cxDIB = (int) ::DIBWidth(lpDIB); // 获取DIB高度 int cyDIB = (int) ::DIBHeight(lpDIB); ::GlobalUnlock((HGLOBAL) hDib); CSize sizeTotal(::DIBWidth(lpDIB), ::DIBHeight(lpDIB)); SetScrollSizes(MM_TEXT, sizeTotal); CRect rcDIB; rcDIB.top = rcDIB.left = 0; rcDIB.right = cxDIB; rcDIB.bottom = cyDIB; CRect rcDest; rcDest = rcDIB; // 输出DIB ::PaintDIB(pDC->m_hDC, &rcDest, hDib, &rcDIB, pDoc->GetDocPalette()); } CPen redPen; redPen.CreatePen(PS_SOLID,1,RGB(255,0,0)); CPen* pOldPen; pOldPen=pDC->SelectObject(&redPen); if(m_ArrayDot.GetSize()!=0) { CPoint point; for(int i=0;i MoveTo(point.x-10,point.y); pDC->LineTo(point.x+10,point.y); pDC->MoveTo(point.x,point.y-10); pDC->LineTo(point.x,point.y+10); // pDC->LineTo(point); } } pDC->SelectObject(pOldPen); // 恢复正常光标 EndWaitCursor(); } void CMatchView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); CSize sizeTotal; // TODO: calculate the total size of this view sizeTotal.cx =4450; sizeTotal.cy = 4440; SetScrollSizes(MM_TEXT, sizeTotal); } ///////////////////////////////////////////////////////////////////////////// // CMatchView printing BOOL CMatchView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMatchView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMatchView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMatchView diagnostics #ifdef _DEBUG void CMatchView::AssertValid() const { CScrollView::AssertValid(); } void CMatchView::Dump(CDumpContext& dc) const { CScrollView::Dump(dc); } CMatchDoc* CMatchView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatchDoc))); return (CMatchDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMatchView message handlers void CMatchView::OnInterior() { // TODO: Add your command handler code here CFileDialog m_dlg(TRUE,"*.aop",NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "IOP文件(*.iop)|*.iop|所有文件(*.*)|*.*||",this); if(m_dlg.DoModal()==IDCANCEL) return; CString pathname = m_dlg.GetPathName(); UpdateData(0); FILE *stream; if( (stream = fopen( pathname, "rt" )) != NULL ) fscanf(stream , "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",&lm[0],&lm[1],&lm[2],&ln[0],&ln[1],&ln[2] ,&rm[0],&rm[1],&rm[2],&rn[0],&rn[1],&rn[2],&f,&sm); fclose( stream ); } void CMatchView::OnInputwfwys() { // TODO: Add your command handler code here CFileDialog m_dlg(TRUE,"*.aop",NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "AOP文件(*.aop)|*.aop|所有文件(*.*)|*.*||",this); if(m_dlg.DoModal()==IDCANCEL) return; CString pathname = m_dlg.GetPathName(); UpdateData(0); FILE *stream; if( (stream = fopen( pathname, "rt" )) != NULL ) fscanf(stream , "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",&Ys1,&Xs1,&Zs1,&ax1,&w1,&k1,&Ys2,&Xs2,&Zs2,&ax2,&w2,&k2); fclose( stream ); delt_Xs = Xs2-Xs1; delt_Ys = Ys2-Ys1; delt_Zs = Zs2-Zs1; delt_ax = (ax2-ax1)*PI/60/180; delt_w = (w2-w1)*PI/60/180; delt_k = (k2-k1)*PI/60/180; JIXIAN_B = pow((delt_Xs*delt_Xs+delt_Ys*delt_Ys+delt_Zs*delt_Zs),0.5); tao_t = atan(delt_Ys/delt_Xs); wei_v = asin(delt_Zs/JIXIAN_B); t_1 = ax1*PI/60/180-wei_v; k_10 = k1*PI/60/180-tao_t; t_2 = ax2*PI/60/180-wei_v; k_20 = k2*PI/60/180-tao_t; } //输入已知的相对方位元素 void CMatchView::OnInputObtainedxdfys() { // TODO: Add your command handler code here } //左片核线排列 void CMatchView::OnLeftCorrect() { LONG i,j; //水平像片的像坐标 double u; double v; //原始像片的像坐标 double yuan_x; double yuan_y; //原始像片的可能不为整的行列号I,J double I_i; double J_j; // LONG I_last; LONG J_last; double delt_x; double delt_y; BYTE I11,I12,I21,I22; double IP; POINT po[2]; /* //旋转矩阵R5 a1 = 1; a2= -k_10; a3 = -t_1; b1 = k_10; b2 = 1; b3 = 0; c1 = t_1; c2 = 0; c3 = 1;*/ double ax,k; ax=t_1;k=k_10; a1 = cos(ax)*cos(k); a2 = -cos(ax)*sin(k); a3 = -sin(ax); b1 = sin(k); b2 = cos(k); b3 = 0; c1 = sin(ax)*cos(k); c2 = -sin(ax)*sin(k); c3 = cos(ax); CMatchDoc* pDoc = GetDocument(); //获取DIB HDIB hDIB = pDoc->hDibL; //LPCTSTR pathname = pDoc->GetPathName(); // 找到DIB图像象素起始位置 //指向原图像的指针 LPSTR lpDIB; lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB); LPSTR lpDIBBits = FindDIBBits(lpDIB); LONG height = ::DIBHeight(lpDIB); LONG width = ::DIBWidth(lpDIB); po[0].x = width; po[0].y = height; po[1].x =0; po[1].y =0; // 原图像每行的字节数 LONG lLineBytes; //最大外接矩形图像每行的字节数 LONG lLNewBytes; // 计算图像每行的字节数 lLineBytes = WIDTHBYTES(width * 8); //求最大外接矩形 rectangle(po,f,lm,ln); //计算最大外接矩形图像每行的字节数 lLNewBytes = WIDTHBYTES(po[0].x * 8); //创建新图像以保存核线影像 HDIB newDib = CreateGrayDIB(po[0].x,po[0].y); LPSTR lpNewDIBBits=FindDIBBits((LPSTR)::GlobalLock((HGLOBAL) newDib)); /* I11 = *(lpDIBBits+1000) ; CString str; str.Format("eehg:%d",I11); MessageBox(str);*/ for(i = 0; i < po[0].y; i++) { //v = (i - p.y/2.0 + 0.5)*sm; v = (i - po[1].y/100)*sm; for(j = 0; j < po[0].x; j++)//列 { u = (j - po[1].x/100)*sm; //公式没有问题 //反算到倾斜像片的像坐标 yuan_x = (-f)*(a1*u + b1*v - c1*f)/(a3*u + b3*v - c3*f); yuan_y = (-f)*(a2*u + b2*v - c2*f)/(a3*u + b3*v - c3*f); //对应的列 I_i = ( ln[2]*( yuan_x/sm - lm[0] ) - lm[2]*( yuan_y/sm - ln[0] ) ) / (lm[1]*ln[2] - ln[1]*lm[2]); //行 J_j = ( ln[1]*( yuan_x/sm - lm[0] ) - lm[1]*( yuan_y/sm - ln[0] ) ) / (lm[2]*ln[1] - ln[2]*lm[1]); I_last = int(I_i); J_last = int(J_j); delt_x = I_i - I_last; delt_y = J_j - J_last; if( (I_last >= 0) && (I_last <= (width-2)) && (J_last >= 0) && (J_last <= (height-2))) { I11 = *(lpDIBBits+ lLineBytes * J_last + I_last ); //J_last是行 I12 = *(lpDIBBits + lLineBytes * (J_last+1) + I_last); //I_last是列 I21 = *(lpDIBBits + lLineBytes * J_last + I_last+1); I22 = *(lpDIBBits + lLineBytes * (J_last+1) + I_last+1); IP = (1-delt_x)*(1-delt_y)*I11+(1-delt_x)*delt_y*I12+delt_x*(1-delt_y)*I21+delt_x*delt_y*I22; *(lpNewDIBBits + lLNewBytes * i+ j) = BYTE(IP+0.5); } else *(lpNewDIBBits + lLNewBytes * i+ j) = BYTE(255); } } /*BYTE bb; bb = *(lpDIBBits+4431+9) ; CString str; str.Format("eehg:%u",bb); MessageBox(str);*/ //写到文件 _mkdir("e:\\IO"); GlobalUnlock(newDib); CFile file; CFileException fe; file.Open("e:\\io\\left.bmp",CFile::modeCreate|CFile::modeWrite,&fe); //flag=true; SaveDIB(newDib, file); file.Close(); hDib=newDib; this->Invalidate(FALSE); // 释放内存 // GlobalFree(newDib); ::GlobalUnlock((HGLOBAL) hDIB); y_left=po[1].y; lie_left=po[0].x; hang_left=po[0].y; } //右片核线排列 void CMatchView::OnRightCorrect() { // TODO: Add your command handler code here LONG i,j; //水平像片的像坐标 double u; double v; //原始像片的像坐标 double yuan_x; double yuan_y; //原始像片的可能不为整的行列号I,J double I_i; double J_j; // LONG I_last; LONG J_last; double delt_x; double delt_y; BYTE I11,I12,I21,I22; double IP; POINT po[2]; /* //旋转矩阵R6 a1 = 1; a2= -k_20; a3 = -t_2; b1 = k_20; b2 = 1; b3 = -delt_w; c1 = t_2; c2 = delt_w; c3 = 1;*/ double ay,fai,kpie; ay=delt_w;fai=t_2;kpie=k_20; a1 = cos(fai)*cos(kpie); a2 = -cos(fai)*sin(kpie); a3 = -sin(fai); b1 = cos(ay)*sin(kpie)-sin(ay)*sin(fai)*cos(kpie); b2 = cos(ay)*cos(kpie)+sin(ay)*sin(fai)*sin(kpie); b3 = -sin(ay)*cos(fai); c1 = sin(ay)*sin(kpie)+cos(ay)*sin(fai)*cos(kpie); c2 = sin(ay)*cos(kpie)-cos(ay)*sin(fai)*sin(kpie); c3 = cos(ay)*cos(fai); CMatchDoc* pDoc = GetDocument(); //获取DIB HDIB hDIB = pDoc->hDibR; //LPCTSTR pathname = pDoc->GetPathName(); // 找到DIB图像象素起始位置 //指向原图像的指针 LPSTR lpDIB; lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB); LPSTR lpDIBBits = FindDIBBits(lpDIB); LONG height = ::DIBHeight(lpDIB); LONG width = ::DIBWidth(lpDIB); po[0].x = width; po[0].y = height; po[1].x =0; po[1].y =0; // 原图像每行的字节数 LONG lLineBytes; //最大外接矩形图像每行的字节数 LONG lLNewBytes; // 计算图像每行的字节数 lLineBytes = WIDTHBYTES(width * 8); //求最大外接矩形 rectangle(po,f,rm,rn); //计算最大外接矩形图像每行的字节数 lLNewBytes = WIDTHBYTES(po[0].x * 8); //创建新图像以保存核线影像 HDIB newDib = CreateGrayDIB(po[0].x,po[0].y); LPSTR lpNewDIBBits=FindDIBBits((LPSTR)::GlobalLock((HGLOBAL) newDib)); { for(i = 0; i < po[0].y; i++) { //v = (i - p.y/2.0 + 0.5)*sm; v = (i - po[1].y/100 )*sm; for(j = 0; j < po[0].x; j++)//列 { u = (j - po[1].x/100)*sm; //公式没有问题 //反算到倾斜像片的像坐标 yuan_x = (-f)*(a1*u + b1*v - c1*f)/(a3*u + b3*v - c3*f); yuan_y = (-f)*(a2*u + b2*v - c2*f)/(a3*u + b3*v - c3*f); //对应的列 I_i = ( rn[2]*( yuan_x/sm - rm[0] ) - rm[2]*( yuan_y/sm - rn[0] ) ) / (rm[1]*rn[2] - rn[1]*rm[2]); //行 J_j = ( rn[1]*( yuan_x/sm - rm[0] ) - rm[1]*( yuan_y/sm - rn[0] ) ) / (rm[2]*rn[1] - rn[2]*rm[1]); I_last = int(I_i); J_last = int(J_j); delt_x = I_i - I_last; delt_y = J_j - J_last; if( (I_last >= 0) && (I_last <= (width-2)) && (J_last >= 0) && (J_last <= (height-2))) { I11 = *(lpDIBBits+ lLineBytes * J_last + I_last ); //J_last是行 I12 = *(lpDIBBits + lLineBytes * (J_last+1) + I_last); //I_last是列 I21 = *(lpDIBBits + lLineBytes * J_last + I_last+1); I22 = *(lpDIBBits + lLineBytes * (J_last+1) + I_last+1); IP = (1-delt_x)*(1-delt_y)*I11+(1-delt_x)*delt_y*I12+delt_x*(1-delt_y)*I21+delt_x*delt_y*I22; *(lpNewDIBBits + lLNewBytes * i+ j) = BYTE(IP+0.5); } else *(lpNewDIBBits + lLNewBytes * i+ j) = BYTE(255); } } } //写到文件 _mkdir("e:\\IO"); GlobalUnlock(newDib); CFile file; CFileException fe; file.Open("e:\\io\\right.bmp",CFile::modeCreate|CFile::modeWrite,&fe); SaveDIB(newDib, file); file.Close(); // CMatchDoc* pDoc=GetDocument(); POSITION ps=pDoc->GetFirstViewPosition(); CView* pView; CRightView* pRightView; while (ps != NULL) { pView = pDoc->GetNextView(ps); CRuntimeClass* prt = pView->GetRuntimeClass(); if(lstrcmp( prt->m_lpszClassName, "CRightView" ) == 0 ) pRightView=(CRightView*)pView; } pRightView->hDib=newDib; pRightView->Invalidate(FALSE); // 释放内存 // GlobalFree(newDib); ::GlobalUnlock((HGLOBAL) hDIB); y_right=po[1].y; lie_right=po[0].x; hang_right=po[0].y; } //求最大外接矩形 void CMatchView::rectangle(POINT point[2],double f,double m[3],double n[3]) { double x[4]; double y[4]; double x_min; double x_max; double y_min; double y_max; double pointx[4]; double pointy[4]; double p_I[4]; double p_J[4]; int wid,hei,i,y_lie,y_hang; p_I[0] = 0; p_I[1] = point[0].x-1; p_I[2] = point[0].x-1; p_I[3] = 0; p_J[0] = point[0].y-1; p_J[1] = point[0].y-1; p_J[2] = 0; p_J[3] = 0; //p_I[0]=1747;p_J[0]=3533; for(i=0;i<4;i++) { pointx[i] =(m[0]+m[1]*p_I[i]+m[2]*p_J[i])*sm; pointy[i] =(n[0]+n[1]*p_I[i]+n[2]*p_J[i])*sm; } for(i = 0; i < 4; i++) { //x[i] = pointx[i]+(f+pointx[i]*pointx[i]/f)*t_1-pointy[i]*k_10; //y[i] = pointy[i]-pointx[i]*pointy[i]/f*t_1+pointx[i]*k_10; x[i] = -f*(a1*pointx[i] + a2*pointy[i] - a3*f)/(c1*pointx[i]+c2*pointy[i]-c3*f); y[i] = -f*(b1*pointx[i] + b2*pointy[i] - b3*f)/(c1*pointx[i]+c2*pointy[i]-c3*f); } if(x[0] x[2]) x_max = x[1]; else x_max = x[2]; if(y[2] y[1]) y_max = y[0]; else y_max = y[1]; //原点的行列号,为了精确放大了一百倍 y_lie=int(-x_min/sm*100); y_hang=int(-y_min/sm*100); //最大外接矩形的宽和高 wid = int((x_max -x_min+sm)/sm); hei = int((y_max -y_min+sm)/sm); point[0].x = wid; point[0].y = hei; point[1].x = y_lie; point[1].y = y_hang; } BOOL CMatchView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) { CPoint point = GetDeviceScrollPosition(); point.y = point.y - zDelta; ScrollToPosition(point); //Invalidate(FALSE); return CScrollView::OnMouseWheel(nFlags, zDelta, pt); } void CMatchView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default // if(m_ArrayDot.GetSize()!=0) CMatchDoc* pDoc=this->GetDocument(); // if(pDoc->flag=) { // CPoint point1=m_ArrayDot.GetAt(m_ArrayDot.GetSize()-1); CDC* pDC=this->GetDC(); CPen redPen; redPen.CreatePen(PS_SOLID,1,RGB(255,0,0)); CPen* pOldPen; pOldPen=pDC->SelectObject(&redPen); pDC->MoveTo(point.x-10,point.y); pDC->LineTo(point.x+10,point.y); pDC->MoveTo(point.x,point.y-10); pDC->LineTo(point.x,point.y+10); // pDC->MoveTo(point1); // pDC->LineTo(point); pDC->SelectObject(pOldPen); } CPoint Orign = GetDeviceScrollPosition(); point.x=Orign.x+point.x; point.y=Orign.y+point.y; m_ArrayDot.Add(point); CRightView* pRightView; POSITION pos = pDoc->GetFirstViewPosition(); CView* pview; while (pos != NULL) { pview = pDoc->GetNextView(pos); CRuntimeClass* prt = pview->GetRuntimeClass(); if(pview->IsKindOf(RUNTIME_CLASS(CRightView))) { pRightView=(CRightView*)pview; break; } } CPoint pointDest; pRightView->m_ArrayDot.Add(pointDest); CScrollView::OnLButtonDown(nFlags, point); }