1 /*****************************************************************************
4 ** Name: dlgreconstruct.cpp
5 ** Purpose: Reconstruction Animation Dialog
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: August 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: dlgreconstruct.cpp,v 1.8 2000/12/17 22:30:34 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
29 #pragma implementation "dlgreconstruct.h"
32 // For compilers that support precompilation, includes "wx.h".
33 #include "wx/wxprec.h"
41 #include "wx/button.h"
42 #include "wx/stattext.h"
43 #include "wx/layout.h"
46 #include "wx/settings.h"
47 #include "wx/dcclient.h"
51 #include "dlgreconstruct.h"
56 static const int LAYOUT_X_MARGIN = 4;
57 static const int LAYOUT_Y_MARGIN = 4;
60 const int ReconstructDialog::ID_BTN_PAUSE = 19998;
\r
61 const int ReconstructDialog::ID_BTN_STEP = 19999;
\r
62 const int ReconstructDialog::MAX_IMAGE_X = 400;
\r
63 const int ReconstructDialog::MAX_IMAGE_Y = 400;
\r
66 BEGIN_EVENT_TABLE(ReconstructDialog, wxDialog)
67 EVT_BUTTON(wxID_CANCEL, ReconstructDialog::OnCancel)
68 EVT_BUTTON(ID_BTN_PAUSE, ReconstructDialog::OnPause)
69 EVT_BUTTON(ID_BTN_STEP, ReconstructDialog::OnStep)
70 EVT_CLOSE(ReconstructDialog::OnClose)
71 EVT_PAINT(ReconstructDialog::OnPaint)
74 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
77 ReconstructDialog::ReconstructDialog (Reconstructor& rReconstruct, const Projections& rProj, ImageFile& rIF, const int iTrace, wxWindow *parent)
78 : wxDialog(parent, -1, "Reconstruction", wxDefaultPosition), m_rReconstructor(rReconstruct), m_rProjections(rProj), m_rImageFile(rIF), m_pSGPDriver(NULL), m_pSGP(NULL), m_iTrace(iTrace), m_pDC(NULL), m_btnAbort(0), m_btnPause(0), m_btnStep(0)
83 while ( m_parentTop && m_parentTop->GetParent() )
84 m_parentTop = m_parentTop->GetParent();
86 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
87 wxLayoutConstraints* c = new wxLayoutConstraints;
88 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
89 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
91 wxSize sizeBtn = wxButton::GetDefaultSize();
92 c->width.Absolute(sizeBtn.x);
93 c->height.Absolute(sizeBtn.y);
95 m_btnAbort->SetConstraints(c);
97 m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
98 wxLayoutConstraints* cPause = new wxLayoutConstraints;
99 cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
100 cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
101 cPause->width.Absolute(sizeBtn.x);
102 cPause->height.Absolute(sizeBtn.y);
103 m_btnPause->SetConstraints(cPause);
105 m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
106 wxLayoutConstraints* cStep = new wxLayoutConstraints;
107 cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
108 cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
109 cStep->width.Absolute(sizeBtn.x);
110 cStep->height.Absolute(sizeBtn.y);
111 m_btnStep->SetConstraints(cStep);
118 wxSize sizeDlg (m_nxGraph, m_nyGraph);
119 m_nxImage = m_rImageFile.nx();
120 if (m_nxImage > MAX_IMAGE_X)
121 m_nxImage = MAX_IMAGE_X;
122 m_nyImage = m_rImageFile.ny();
123 if (m_nyImage > MAX_IMAGE_Y)
124 m_nyImage = MAX_IMAGE_Y;
126 sizeDlg.x += m_nxImage;
127 sizeDlg.y = max (sizeDlg.y, m_nyImage);
129 m_iClientX = sizeDlg.x;
130 m_iClientY = sizeDlg.y;
131 SetClientSize (sizeDlg);
133 Centre(wxCENTER_FRAME | wxBOTH);
\r
136 m_parentTop->Enable(FALSE);
\r
139 Enable(TRUE); // enable this window
\r
141 m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
143 this->GetClientSize(&x, &y);
\r
145 m_pDC = new wxMemoryDC;
146 m_pDC->SelectObject (m_bitmap); // in memoryDC
147 m_pSGPDriver = new SGPDriver (dynamic_cast<wxDC*>(m_pDC), x, y);
\r
149 m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
\r
150 m_pSGPDriver = new SGPDriver (m_pDC, x, y);
\r
152 m_pSGP = new SGP (*m_pSGPDriver);
154 wxYield(); // Update the display
156 m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
158 MacUpdateImmediately();
163 ReconstructDialog::showView (int iViewNumber, bool bBackprojectView)
165 if ( iViewNumber < m_rProjections.nView() ) {
166 m_iLastView = iViewNumber;
167 m_pSGP->eraseWindow();
169 char szProgress [256];
170 snprintf (szProgress, sizeof(szProgress), "Reconstructing View %d (%.1f%%)", iViewNumber, 100 * iViewNumber / static_cast<double>(m_rProjections.nView()));
171 m_pSGP->setViewport (0, 0, 1, 1);
172 m_pSGP->setWindow (0, 0, 1, 1);
173 m_pSGP->setTextColor (C_LTRED, -1);
174 double dCharHeight = m_pSGP->getCharHeight();
175 m_pSGP->setTextSize (dCharHeight * 2);
176 m_pSGP->moveAbs(0., m_pSGP->getCharHeight());
177 m_pSGP->drawText (szProgress);
178 m_pSGP->setTextSize (dCharHeight);
180 m_pSGP->setViewport (0.0, 0.1, 0.66, 1.);
181 m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP, bBackprojectView);
183 ImageFileArrayConst v = m_rImageFile.getArray();
184 int xBase = m_nxGraph;
186 if (m_nyGraph > m_nyImage)
187 yBase = (m_nyGraph - m_nyImage) / 2;
188 double minValue = v[0][0];
189 double maxValue = v[0][0];
190 for (int ix = 0; ix < m_nxImage; ix++) {
191 for (int iy = 0; iy < m_nyImage; iy++) {
192 double dPixel = v[ix][iy];
193 if (dPixel < minValue)
195 else if (dPixel > maxValue)
199 unsigned char* imageData = new unsigned char [m_nxImage * m_nyImage * 3];
200 double dScale = 255 / (maxValue - minValue);
201 for (int ix2 = 0; ix2 < m_nxImage; ix2++) {
202 for (int iy = 0; iy < m_nyImage; iy++) {
203 double dPixel = v[ix2][iy];
204 dPixel = (dPixel - minValue) * dScale;
205 int intensity = nearest<int>(dPixel);
206 intensity = clamp (intensity, 0, 255);
207 int baseAddr = ((m_nyImage - 1 - iy) * m_nxImage + ix2) * 3;
208 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
211 wxImage image (m_nxImage, m_nyImage, imageData, true);
212 wxBitmap bitmap = image.ConvertToBitmap();
213 m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
221 ReconstructDialog::reconstructView (int iViewNumber)
223 if (iViewNumber <= m_iLastView) // have already done this view
226 if (iViewNumber < m_rProjections.nView()) {
227 ::wxYield(); // update the display
228 showView (iViewNumber, true);
229 ::wxYield(); // update the display
230 if (m_iTrace >= Trace::TRACE_PLOT) {
234 m_state = Finished; // so that we return TRUE below and
235 } // that [Cancel] handler knew what to do
238 MacUpdateImmediately();
241 ::wxYield(); // update the display
242 return m_state != Cancelled;
248 void ReconstructDialog::OnCancel (wxCommandEvent& event)
250 if ( m_state == Finished ) {
251 // this means that the count down is already finished and we're being
252 // shown as a modal dialog - so just let the default handler do the job
255 // request to cancel was received, the next time Update() is called we
259 // update the button state immediately so that the user knows that the
260 // request has been noticed
261 m_btnAbort->Disable();
267 ReconstructDialog::OnPause (wxCommandEvent& event)
269 if ( m_state == Finished ) {
270 // this means that the count down is already finished and we're being
271 // shown as a modal dialog - so just let the default handler do the job
274 if (m_state == Continue) {
\r
276 m_memoryDC.SelectObject (m_bitmap); // in memoryDC
\r
277 m_pSGP->setDC (&m_memoryDC);
\r
278 m_memoryDC.SetFont (*wxSWISS_FONT);
\r
279 showView (m_iLastView, false);
\r
280 m_pSGP->setDC (m_pDC);
\r
281 m_memoryDC.SelectObject(wxNullBitmap);
\r
284 m_btnPause->SetLabel (wxString("Resume"));
285 } else if (m_state == Paused) {
287 m_btnPause->SetLabel (wxString("Pause"));
293 ReconstructDialog::OnStep (wxCommandEvent& event)
295 if ( m_state == Finished ) {
298 if (m_state == Continue) {
\r
300 m_memoryDC.SelectObject (m_bitmap); // in memoryDC
\r
301 m_pSGP->setDC (&m_memoryDC);
\r
302 m_memoryDC.SetFont (*wxSWISS_FONT);
\r
303 m_rReconstructor.reconstructView (m_iLastView, 1, m_pSGP, false);
\r
304 m_pSGP->setDC (m_pDC);
\r
305 m_memoryDC.SelectObject(wxNullBitmap);
\r
308 m_btnPause->SetLabel (wxString("Resume"));
309 } else if (m_state == Paused) {
310 reconstructView (m_iLastView + 1);
315 void ReconstructDialog::OnClose(wxCloseEvent& event)
317 if ( m_state == Uncancellable )
318 event.Veto(TRUE); // can't close this dialog
319 else if ( m_state == Finished )
320 event.Skip(); // let the default handler close the window as we already terminated
322 m_state = Cancelled; // next Update() will notice it
326 ReconstructDialog::OnPaint (wxPaintEvent& event)
328 wxPaintDC paintDC (this);
\r
329 if (m_state == Paused)
330 paintDC.DrawBitmap(m_bitmap, 0, 0, false);
334 /////////////////////////////////////////////////////
337 ReconstructDialog::~ReconstructDialog()
340 m_parentTop->Enable(TRUE);