⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opticaldlg.cpp

📁 根据菲涅耳衍射原理编写的光学衍射模拟程序
💻 CPP
字号:
// OpticalDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Optical.h"
#include "OpticalDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define PI 3.1415926
/////////////////////////////////////////////////////////////////////////////
// COpticalDlg dialog

COpticalDlg::COpticalDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COpticalDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COpticalDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	bon=FALSE;
}

void COpticalDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COpticalDlg)
	DDX_Control(pDX, IDC_SRCS2, s_src2);
	DDX_Control(pDX, IDC_SLIDER1, m_rate);
	DDX_Control(pDX, IDC_PROGRESS1, m_step);
	DDX_Control(pDX, IDC_OBJS, s_obj);
	DDX_Control(pDX, IDC_SRCS, s_src);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COpticalDlg, CDialog)
	//{{AFX_MSG_MAP(COpticalDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1)
	ON_BN_CLICKED(IDB_TRAN, OnTran)
	ON_BN_CLICKED(IDB_RETRAN, OnRetran)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COpticalDlg message handlers

BOOL COpticalDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	RECT r;
	s_obj.GetClientRect(&r);
	w_obj.CreateWnd(&s_obj,r);

	s_src.GetClientRect(&r);
	w_src1.CreateWnd(&s_src,r,TRUE);

	s_src2.GetClientRect(&r);
	w_src2.CreateWnd(&s_src2,r);

	m_step.SetRange(0,100);
	m_rate.SetRange(1,400);
	m_rate.SetPos(100);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void COpticalDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COpticalDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COpticalDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	w_obj.SetFocus();
	int rate=m_rate.GetPos();
	this->w_obj.ReDrawDC(rate);
	this->w_src2.ReDrawDC(rate);
	*pResult = 0;
}

UINT WINAPI _ThTransProc(LPVOID pParam)
{
	COpticalDlg *pdlg=(COpticalDlg *)pParam;

	pdlg->m_rate.EnableWindow(FALSE);

	if (pdlg->sta==1)
		pdlg->OnTrans(&pdlg->w_src1,&pdlg->w_obj);
	else pdlg->OnTrans(&pdlg->w_obj,&pdlg->w_src2);

	pdlg->m_rate.EnableWindow(TRUE);
	pdlg->m_step.SetPos(0);

	return 0;
}

void COpticalDlg::OnTran() 
{
	// TODO: Add your control notification handler code here
	if (bon)
	{
		bon=FALSE;
		return;
	}

	sta=1;
	DWORD nTID = 0;
	HANDLE m_hThread;
	m_step.SetPos(0);
	m_hThread=CreateThread( NULL, 0,(LPTHREAD_START_ROUTINE)_ThTransProc,(LPVOID)this, CREATE_SUSPENDED,&nTID);
	if (m_hThread)
	{
		w_obj.SetFocus();
		ResumeThread(m_hThread);
		bon=TRUE;
	}
	
}

void COpticalDlg::OnRetran() 
{
	// TODO: Add your control notification handler code here
	if (bon)
	{
		bon=FALSE;
		return;
	}

	sta=-1;
	DWORD nTID = 0;
	HANDLE m_hThread;
	m_step.SetPos(0);
	m_hThread=CreateThread( NULL, 0,(LPTHREAD_START_ROUTINE)_ThTransProc,(LPVOID)this, CREATE_SUSPENDED,&nTID);
	if (m_hThread)
	{
		w_obj.SetFocus();
		ResumeThread(m_hThread);
		bon=TRUE;
	}
	
}

void COpticalDlg::OnTrans(CObjwnd *pwsrc, CObjwnd *pwobj)
{
	int w=pwsrc->ww/2;
	int h=pwsrc->hh/2;

	int ww=pwobj->ww/2;
	int hh=pwobj->hh/2;

	DWORD f=40;
	float t=1.5;
	float k=2;
	if (sta==-1) k=0.8;

	DWORD rate=m_rate.GetPos();

	int x,y,xx,yy,i,step=0;
	float r,R,I,R1,I1,rsin,rcos;
	DWORD rgbE=0;

	for (yy=-hh;yy<hh;yy++)
	{
		for (xx=-ww;xx<ww;xx++)
		{
			R=0;I=0;
			for (y=-h;y<h;y++)
				for (x=-w;x<w;x++)
				{
					if (!bon) return;

					i=x+w+(y+h)*w*2;
					R1=k*pwsrc->opix[i].re;
					I1=k*pwsrc->opix[i].im;
					if (R1<1 && R1>-1 && I1<1 && I1>-1) continue;
//					r=sqrt(f*f+(xx-x)*(xx-x)+(yy-y)*(yy-y));
//					k=(float)(x*x+y*y)/2/f;
//					R+=(float)rgbE*cos(2*PI*(r-k)/t)/r;
//					I+=(float)rgbE*sin(2*PI*(r-k)/t)/r;
					r=2*PI*(x*xx+y*yy)/f/t;
					rcos=cos(sta*r);
					rsin=sin(sta*r);
					R+=R1*rcos+I1*rsin;
					I+=I1*rcos+R1*rsin;

				}
			i=xx+ww+2*(yy+hh)*ww;
			//E1=E1/l/t;
	//		if (sta==0)
			pwobj->opix[i].re=R/(f*t);//*sinc2(PI*xx/f/t)*sinc2(PI*yy/f/t);
			pwobj->opix[i].im=I/(f*t);//*sinc2(PI*xx/f/t)*sinc2(PI*yy/f/t);
	//		else pwobj->opix[i]=(R*R+I*I)/(l*l)/1000000;


			rgbE=(pwobj->opix[i].re*pwobj->opix[i].re+pwobj->opix[i].im*pwobj->opix[i].im)*rate/100;
			if (rgbE>255) rgbE=255;
			pwobj->pmdc->SetPixel(xx+ww,yy+hh,RGB(rgbE,0,0));

			step++;
			m_step.SetPos(step*25/(ww*hh));
		}
		pwobj->Invalidate();
	}

	bon=FALSE;
}


float COpticalDlg::sinc2(float a)
{
	if (a<0.1 && a>-0.1)
		return 1;

	float res;
	res=sin(a)/a;
	res*=res;
	return res;
}

void COpticalDlg::ResetOrder(Complex *A, int N)
{
	int NV2=N/2;
	int I,J,K=0;

	Complex T;
	for (I=1,J=1;I<N;I++)
	{ 
		if(I<J)
		{
			T=A[J-1];
			A[J-1]=A[I-1];
			A[I-1]=T;
		} 
		K=NV2;
		while(K<J)
		{ 
			J-=K;
			K/=2;
		} 
		J+=K;
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -