www.pudn.com > Depthimage.rar > DepthimageView.cpp
// DepthimageView.cpp : implementation of the CDepthimageView class
//
#include "stdafx.h"
#include "Depthimage.h"
#include "DepthimageDoc.h"
#include "DepthimageView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDepthimageView
IMPLEMENT_DYNCREATE(CDepthimageView, CView)
BEGIN_MESSAGE_MAP(CDepthimageView, CView)
//{{AFX_MSG_MAP(CDepthimageView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
ON_COMMAND(IDC_SHENDU, OnDepth)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDepthimageView construction/destruction
CDepthimageView::CDepthimageView()
{
// TODO: add construction code here
}
CDepthimageView::~CDepthimageView()
{
}
BOOL CDepthimageView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDepthimageView drawing
void CDepthimageView::OnDraw(CDC* pDC)
{
CDepthimageDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
double x,y;
pDC;
pDC->SetViewportOrg(210,210);//设置视口
pDC->MoveTo(-215,0);
pDC->LineTo(215,0);
pDC->MoveTo(0,-215);
pDC->LineTo(0,215);
pDC->TextOut(0,0,"坐标原点");
// TODO: add draw code for native data here
{
for(x=2*xmin;x<2*xmax;x++)// 行
{
for(y=2*ymin;y<2*ymax;y++) //列
{
int depth;
depth=(int)(pDoc->mdepth[(int(x)+int(2*xmax))*int(2*xmax-2*xmin)+int(y)+int(2*ymax)]);
pDC->SetPixel(int(x),int(y),RGB(depth,depth,depth));
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CDepthimageView diagnostics
#ifdef _DEBUG
void CDepthimageView::AssertValid() const
{
CView::AssertValid();
}
void CDepthimageView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDepthimageDoc* CDepthimageView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDepthimageDoc)));
return (CDepthimageDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDepthimageView message handlers
void CDepthimageView::OnDepth()
{
// TODO: Add your command handler code here
CDepthimageDoc* pDoc = GetDocument();
double AA,BB,CC;
/////////////////////////////////////////////////
RECT rect;
this->GetClientRect(&rect);
m_a=0;
m_b=0;
m_c=-255;
m_r=255;
m_f=255;
//得到显示区域
xmin=float((m_f*(m_a-m_r)))/float((m_f-m_c));
xmax=float((m_f*(m_a+m_r)))/float(m_f-m_c);
ymin=float((m_f*(m_b-m_r)))/float(m_f-m_c);
ymax=float(m_f*(m_b+m_r))/float(m_f-m_c);
//量化
dtx=double((xmax-xmin))/double(2*m_r);
dty=double((ymax-ymin))/double(2*m_r);
if(pDoc->mdepth!=NULL)
{
delete[] pDoc->mdepth;
pDoc->mdepth=NULL;
}
pDoc->mdepth=new unsigned char[1262144];
if(pDoc->mdepth==NULL)
{
::AfxMessageBox("内存分配错误");
return;
}
::memset(pDoc->mdepth,(BYTE)0,1262144);
//////// ////////////////////////////////////
//CTest1Doc* pDoc = GetDocument();
//////////////////////////////////////////////////////////////////////////
BeginWaitCursor();
int depth;
double x,y;
//计算深度
for(x=2*xmin;x<2*xmax;x+=dtx)
{
for(y=2*ymin;y<2*ymax;y+=dty)
{
AA=(x*x+y*y+m_f*m_f)/(m_f*m_f);
BB=(m_c*m_f+x*x+y*y)/m_f;
CC=x*x+y*y+m_c*m_c-m_r*m_r;
if((BB*BB-AA*CC)<0)
{depth=0;
continue;}
if((BB+sqrt(BB*BB-AA*CC))<0)
depth=int((BB+sqrt(BB*BB-AA*CC))/AA-m_c);
else
depth=int((BB-sqrt(BB*BB-AA*CC))/AA-m_c);
depth=int(depth/dtx);
pDoc->mdepth[(int(x)+int(2*xmax))*int(2*xmax-2*xmin)+int(y)+int(2*ymax)]=BYTE(depth);
}
}
EndWaitCursor();
this->Invalidate(true);
}