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-2001 Kevin Rosenberg
12 ** $Id: dlgreconstruct.cpp,v 1.21 2003/01/24 05:24:19 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 ******************************************************************************/
28 #include "wx/wxprec.h"
32 #include "wx/button.h"
33 #include "wx/stattext.h"
34 #include "wx/layout.h"
37 #include "wx/settings.h"
38 #include "wx/dcclient.h"
42 #include "dlgreconstruct.h"
47 static const int LAYOUT_X_MARGIN = 4;
48 static const int LAYOUT_Y_MARGIN = 4;
51 const int ReconstructDialog::ID_BTN_PAUSE = 19998;
52 const int ReconstructDialog::ID_BTN_STEP = 19999;
53 const int ReconstructDialog::MAX_IMAGE_X = 400;
54 const int ReconstructDialog::MAX_IMAGE_Y = 400;
57 BEGIN_EVENT_TABLE(ReconstructDialog, wxDialog)
58 EVT_BUTTON(wxID_CANCEL, ReconstructDialog::OnCancel)
59 EVT_BUTTON(ID_BTN_PAUSE, ReconstructDialog::OnPause)
60 EVT_BUTTON(ID_BTN_STEP, ReconstructDialog::OnStep)
61 EVT_CLOSE(ReconstructDialog::OnClose)
62 EVT_PAINT(ReconstructDialog::OnPaint)
65 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
68 ReconstructDialog::ReconstructDialog (Reconstructor& rReconstruct, const Projections& rProj,
69 ImageFile& rIF, const int iTrace, wxWindow *parent)
70 : wxDialog(parent, -1, "Reconstruction", wxDefaultPosition), m_rReconstructor(rReconstruct),
71 m_rProjections(rProj), m_rImageFile(rIF), m_pSGPDriver(NULL), m_pSGP(NULL),
72 m_iTrace(iTrace), m_pDC(NULL), m_btnAbort(0), m_btnPause(0), m_btnStep(0)
77 while ( m_parentTop && m_parentTop->GetParent() )
78 m_parentTop = m_parentTop->GetParent();
80 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
81 wxLayoutConstraints* c = new wxLayoutConstraints;
82 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
83 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
85 wxSize sizeBtn = wxButton::GetDefaultSize();
86 c->width.Absolute(sizeBtn.x);
87 c->height.Absolute(sizeBtn.y);
89 m_btnAbort->SetConstraints(c);
91 m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
92 wxLayoutConstraints* cPause = new wxLayoutConstraints;
93 cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
94 cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
95 cPause->width.Absolute(sizeBtn.x);
96 cPause->height.Absolute(sizeBtn.y);
97 m_btnPause->SetConstraints(cPause);
99 m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
100 wxLayoutConstraints* cStep = new wxLayoutConstraints;
101 cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
102 cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
103 cStep->width.Absolute(sizeBtn.x);
104 cStep->height.Absolute(sizeBtn.y);
105 m_btnStep->SetConstraints(cStep);
112 wxSize sizeDlg (m_nxGraph, m_nyGraph);
113 m_nxImage = m_rImageFile.nx();
114 if (m_nxImage > MAX_IMAGE_X)
115 m_nxImage = MAX_IMAGE_X;
116 m_nyImage = m_rImageFile.ny();
117 if (m_nyImage > MAX_IMAGE_Y)
118 m_nyImage = MAX_IMAGE_Y;
120 sizeDlg.x += m_nxImage;
121 sizeDlg.y = imax (sizeDlg.y, m_nyImage);
123 m_iClientX = sizeDlg.x;
124 m_iClientY = sizeDlg.y;
125 SetClientSize (sizeDlg);
127 Centre(wxCENTER_FRAME | wxBOTH);
130 m_parentTop->Enable(FALSE);
133 Enable(TRUE); // enable this window
135 m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
136 m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
138 this->GetClientSize(&x, &y);
139 m_pSGPDriver = new SGPDriver (m_pDC, x, y);
140 m_pSGP = new SGP (*m_pSGPDriver);
142 wxYield(); // Update the display
144 m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
148 ReconstructDialog::showView (int iViewNumber, bool bBackprojectView)
150 if ( iViewNumber < m_rProjections.nView() ) {
151 m_iLastView = iViewNumber;
152 ::wxYield(); // update the display
153 m_pSGP->eraseWindow();
154 m_btnPause->Refresh();
155 m_btnStep->Refresh();
156 m_btnAbort->Refresh();
158 char szProgress [256];
159 snprintf (szProgress, sizeof(szProgress), "Reconstructing View %d (%.1f%%)", iViewNumber, 100 * iViewNumber / static_cast<double>(m_rProjections.nView()));
160 m_pSGP->setViewport (0, 0, 1, 1);
161 m_pSGP->setWindow (0, 0, 1, 1);
162 m_pSGP->setTextColor (C_LTRED, -1);
163 m_pSGP->setTextPointSize (20.);
164 m_pSGP->moveAbs(0., m_pSGP->getCharHeight());
165 m_pSGP->drawText (szProgress);
166 m_pSGP->setTextPointSize (10.);
168 int iXDialog, iYDialog;
169 GetClientSize (&iXDialog, &iYDialog);
170 double dGraphWidth = (iXDialog - m_nxImage) / static_cast<double>(iXDialog);
172 m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP, bBackprojectView, dGraphWidth);
178 ImageFileArrayConst v = m_rImageFile.getArray();
179 int xBase = m_nxGraph;
181 if (m_nyGraph > m_nyImage)
182 yBase = (m_nyGraph - m_nyImage) / 2;
183 double minValue = v[0][0];
184 double maxValue = v[0][0];
185 for (int ix = 0; ix < m_nxImage; ix++) {
186 for (int iy = 0; iy < m_nyImage; iy++) {
187 double dPixel = v[ix][iy];
188 if (dPixel < minValue)
190 else if (dPixel > maxValue)
194 unsigned char* imageData = new unsigned char [m_nxImage * m_nyImage * 3];
195 double dScale = 255 / (maxValue - minValue);
196 for (int ix2 = 0; ix2 < m_nxImage; ix2++) {
197 for (int iy = 0; iy < m_nyImage; iy++) {
198 double dPixel = v[ix2][iy];
199 dPixel = (dPixel - minValue) * dScale;
200 int intensity = nearest<int>(dPixel);
201 intensity = clamp (intensity, 0, 255);
202 int baseAddr = ((m_nyImage - 1 - iy) * m_nxImage + ix2) * 3;
203 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
206 wxImage image (m_nxImage, m_nyImage, imageData, true);
207 wxBitmap bitmap (image);
208 m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
216 ReconstructDialog::reconstructView (int iViewNumber, bool bBackproject)
218 if (iViewNumber <= m_iLastView) // have already done this view
221 if (iViewNumber < m_rProjections.nView()) {
222 ::wxYield(); // update the display
223 showView (iViewNumber, bBackproject);
224 ::wxYield(); // update the display
225 if (m_iTrace >= Trace::TRACE_PLOT) {
229 m_state = Finished; // so that we return TRUE below and
230 } // that [Cancel] handler knew what to do
232 ::wxYield(); // update the display
233 return m_state != Cancelled;
239 void ReconstructDialog::OnCancel (wxCommandEvent& event)
241 if ( m_state == Finished ) {
242 // this means that the count down is already finished and we're being
243 // shown as a modal dialog - so just let the default handler do the job
246 // request to cancel was received, the next time Update() is called we
250 // update the button state immediately so that the user knows that the
251 // request has been noticed
252 m_btnAbort->Disable();
258 ReconstructDialog::OnPause (wxCommandEvent& event)
260 if ( m_state == Finished ) {
261 // this means that the count down is already finished and we're being
262 // shown as a modal dialog - so just let the default handler do the job
264 } else if (m_state == Continue) {
265 m_memoryDC.SelectObject (m_bitmap); // in memoryDC
266 m_pSGP->setDC (&m_memoryDC);
267 m_memoryDC.SetFont (*wxSWISS_FONT);
268 showView (m_iLastView, false);
270 m_btnPause->SetLabel (wxString("Resume"));
271 m_pSGP->setDC (m_pDC);
272 m_memoryDC.SelectObject(wxNullBitmap);
273 } else if (m_state == Paused) {
275 m_btnPause->SetLabel (wxString("Pause"));
281 ReconstructDialog::OnStep (wxCommandEvent& event)
283 if ( m_state == Finished ) {
285 } else if (m_state == Continue) {
286 m_memoryDC.SelectObject (m_bitmap); // in memoryDC
287 m_pSGP->setDC (&m_memoryDC);
288 m_memoryDC.SetFont (*wxSWISS_FONT);
289 showView (m_iLastView, false);
291 m_btnPause->SetLabel (wxString("Resume"));
292 m_pSGP->setDC (m_pDC);
293 m_memoryDC.SelectObject(wxNullBitmap);
295 } else if (m_state == Paused) {
296 m_memoryDC.SelectObject (m_bitmap); // in memoryDC
297 m_pSGP->setDC (&m_memoryDC);
298 m_memoryDC.SetFont (*wxSWISS_FONT);
299 reconstructView (m_iLastView + 1);
300 m_pSGP->setDC (m_pDC);
301 m_memoryDC.SelectObject(wxNullBitmap);
306 void ReconstructDialog::OnClose(wxCloseEvent& event)
308 if ( m_state == Uncancellable )
309 event.Veto(TRUE); // can't close this dialog
310 else if ( m_state == Finished )
311 event.Skip(); // let the default handler close the window as we already terminated
313 m_state = Cancelled; // next Update() will notice it
317 ReconstructDialog::OnPaint (wxPaintEvent& event)
319 wxPaintDC paintDC (this);
320 if (m_state == Paused) {
321 paintDC.DrawBitmap (m_bitmap, 0, 0, false);
326 /////////////////////////////////////////////////////
329 ReconstructDialog::~ReconstructDialog()
332 m_parentTop->Enable(TRUE);