Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / MFC

The Diffraction Grating Calculator

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
30 Oct 2010GPL38 min read 41.3K   678   6  
An MFC Windows program related to Concave Diffraction Gratings using VTK libraries.
// RestrictFPlaneDlg.cpp : implementation file

#include "stdafx.h"
#include "GratingCalculator.h"
#include "RestrictFPlaneDlg.h"

// CRestrictFPlaneDlg dialog

IMPLEMENT_DYNAMIC(CRestrictFPlaneDlg, CDialog)

CRestrictFPlaneDlg::CRestrictFPlaneDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRestrictFPlaneDlg::IDD, pParent)
{
   m_pFocalSpotObj = NULL;
}

CRestrictFPlaneDlg::~CRestrictFPlaneDlg()
{
}

void CRestrictFPlaneDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_EDIT_XMIN, m_edtXMin);
   DDX_Control(pDX, IDC_EDIT_XMAX, m_edtXMax);
   DDX_Control(pDX, IDC_EDIT_YMIN, m_edtYMin);
   DDX_Control(pDX, IDC_EDIT_YMAX, m_edtYMax);
   DDX_Control(pDX, IDC_SLIDER_XMIN, m_sliderXMin);
   DDX_Control(pDX, IDC_SLIDER_XMAX, m_sliderXMax);
   DDX_Control(pDX, IDC_SLIDER_YMIN, m_sliderYMin);
   DDX_Control(pDX, IDC_SLIDER_YMAX, m_sliderYMax);
}

BEGIN_MESSAGE_MAP(CRestrictFPlaneDlg, CDialog)
   ON_WM_HSCROLL()
END_MESSAGE_MAP()

// CRestrictFPlaneDlg message handlers

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

   if ( !m_pFocalSpotObj )
      return FALSE;

   ComputeBounds();

   CString szFormat;
   szFormat.Format(_T("%08f"),m_xmin);
   m_edtXMin.SetWindowText(szFormat);
   szFormat.Format(_T("%08f"),m_xmax);
   m_edtXMax.SetWindowText(szFormat);
   szFormat.Format(_T("%08f"),m_ymin);
   m_edtYMin.SetWindowText(szFormat);
   szFormat.Format(_T("%08f"),m_ymax);
   m_edtYMax.SetWindowText(szFormat);

   m_sliderXMin.SetRangeMin(0);
   m_sliderXMin.SetRangeMax(1000);
   m_sliderXMax.SetRangeMin(0);
   m_sliderXMax.SetRangeMax(1000);
   m_sliderYMin.SetRangeMin(0);
   m_sliderYMin.SetRangeMax(1000);
   m_sliderYMax.SetRangeMin(0);
   m_sliderYMax.SetRangeMax(1000);

   m_sliderXMax.SetPos(1000);
   m_sliderYMax.SetPos(1000);

   return TRUE;  // return TRUE unless you set the focus to a control
   // EXCEPTION: OCX Property Pages should return FALSE
}

void CRestrictFPlaneDlg::OnOK()
{
   CString szFormat;
   double xmin, xmax, ymin, ymax;
   m_edtXMin.GetWindowText(szFormat);
   xmin = _tstof(szFormat.GetBuffer()); szFormat.ReleaseBuffer();
   m_edtXMax.GetWindowText(szFormat);
   xmax = _tstof(szFormat.GetBuffer()); szFormat.ReleaseBuffer();
   m_edtYMin.GetWindowText(szFormat);
   ymin = _tstof(szFormat.GetBuffer()); szFormat.ReleaseBuffer();
   m_edtYMax.GetWindowText(szFormat);
   ymax = _tstof(szFormat.GetBuffer()); szFormat.ReleaseBuffer();

   if (( xmax <= xmin ) || ( ymax <= ymin ))
   {
      AfxMessageBox(_T("Illegal values."));
      return;
   }

   m_xmin = xmin;
   m_xmax = xmax;
   m_ymin = ymin;
   m_ymax = ymax;

   CDialog::OnOK();
}

void CRestrictFPlaneDlg::ComputeBounds()
{
   if ( !m_pFocalSpotObj )
      return;

   CAttribute_Generic *atX, *atY;
   atX = m_pFocalSpotObj->GetAttribute(FS_DENSITY_3D_X);
   atY = m_pFocalSpotObj->GetAttribute(FS_DENSITY_3D_Y);

   size_t N;
   atX->GetSize(N);

   m_xmin = 1.0e+33;
   m_ymin = 1.0e+33;
   m_xmax = 0.0;
   m_ymax = 0.0;

   double x, y;
   for (size_t i=0; i<N; i++)
   {
      atX->GetAt(x,i);
      atY->GetAt(y,i);
      if ( x <= m_xmin )
         m_xmin = x;
      if ( y <= m_ymin )
         m_ymin = y;
      if ( x >= m_xmax )
         m_xmax = x;
      if ( y >= m_ymax )
         m_ymax = y;
   }
}

void CRestrictFPlaneDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
   CSliderCtrl* pSlider = (CSliderCtrl*) pScrollBar;

   // Identify which scroll
   CSliderCtrl* pSliderXMin = &m_sliderXMin;
   CSliderCtrl* pSliderXMax = &m_sliderXMax;
   CSliderCtrl* pSliderYMin = &m_sliderYMin;
   CSliderCtrl* pSliderYMax = &m_sliderYMax;

   if (pSlider == pSliderXMin)
   {
      int iMin = m_sliderXMin.GetPos();
      CString szFormat;
      double  xmin = 0.0;
      xmin = m_xmin + ((double) iMin)/1000.0 * (m_xmax-m_xmin);
      szFormat.Format(_T("%08f"),xmin);
      m_edtXMin.SetWindowText(szFormat);
   }

   if (pSlider == pSliderXMax)
   {
      int iMax = m_sliderXMax.GetPos();
      CString szFormat;
      double  xmax = 0.0;
      xmax = m_xmin + ((double) iMax)/1000.0 * (m_xmax-m_xmin);
      szFormat.Format(_T("%08f"),xmax);
      m_edtXMax.SetWindowText(szFormat);
   }

   if (pSlider == pSliderYMin)
   {
      int iMin = m_sliderYMin.GetPos();
      CString szFormat;
      double  ymin = 0.0;
      ymin = m_ymin + ((double) iMin)/1000.0 * (m_ymax-m_ymin);
      szFormat.Format(_T("%08f"),ymin);
      m_edtYMin.SetWindowText(szFormat);
   }

   if (pSlider == pSliderYMax)
   {
      int iMax = m_sliderYMax.GetPos();
      CString szFormat;
      double  ymax = 0.0;
      ymax = m_ymin + ((double) iMax)/1000.0 * (m_ymax-m_ymin);
      szFormat.Format(_T("%08f"),ymax);
      m_edtYMax.SetWindowText(szFormat);
   }

   CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Italy Italy
Senior Software Developer in C/C++ and Oracle.
Ex-physicist holding a Ph.D. on x-ray lasers.

Comments and Discussions