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.5 2000/09/07 14:29:05 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"
42 #include "wx/button.h"
43 #include "wx/stattext.h"
44 #include "wx/layout.h"
47 #include "wx/settings.h"
48 #include "wx/dcclient.h"
53 #include "dlgreconstruct.h"
58 static const int LAYOUT_X_MARGIN = 4;
59 static const int LAYOUT_Y_MARGIN = 4;
61 BEGIN_EVENT_TABLE(ReconstructDialog, wxDialog)
62 EVT_BUTTON(wxID_CANCEL, ReconstructDialog::OnCancel)
63 EVT_BUTTON(ID_BTN_PAUSE, ReconstructDialog::OnPause)
64 EVT_BUTTON(ID_BTN_STEP, ReconstructDialog::OnStep)
65 EVT_CLOSE(ReconstructDialog::OnClose)
66 EVT_PAINT(ReconstructDialog::OnPaint)
69 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
72 ReconstructDialog::ReconstructDialog (Reconstructor& rReconstruct, const Projections& rProj, ImageFile& rIF, const int iTrace, wxWindow *parent)
73 : 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)
78 while ( m_parentTop && m_parentTop->GetParent() )
79 m_parentTop = m_parentTop->GetParent();
81 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
82 wxLayoutConstraints* c = new wxLayoutConstraints;
83 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
84 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
86 wxSize sizeBtn = wxButton::GetDefaultSize();
87 c->width.Absolute(sizeBtn.x);
88 c->height.Absolute(sizeBtn.y);
90 m_btnAbort->SetConstraints(c);
92 m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
93 wxLayoutConstraints* cPause = new wxLayoutConstraints;
94 cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
95 cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
96 cPause->width.Absolute(sizeBtn.x);
97 cPause->height.Absolute(sizeBtn.y);
98 m_btnPause->SetConstraints(cPause);
100 m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
101 wxLayoutConstraints* cStep = new wxLayoutConstraints;
102 cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
103 cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
104 cStep->width.Absolute(sizeBtn.x);
105 cStep->height.Absolute(sizeBtn.y);
106 m_btnStep->SetConstraints(cStep);
113 wxSize sizeDlg (m_nxGraph, m_nyGraph);
114 m_nxImage = m_rImageFile.nx();
115 if (m_nxImage > MAX_IMAGE_X)
116 m_nxImage = MAX_IMAGE_X;
117 m_nyImage = m_rImageFile.ny();
118 if (m_nyImage > MAX_IMAGE_Y)
119 m_nyImage = MAX_IMAGE_Y;
121 sizeDlg.x += m_nxImage;
122 sizeDlg.y = max (sizeDlg.y, m_nyImage);
124 m_iClientX = sizeDlg.x;
125 m_iClientY = sizeDlg.y;
126 SetClientSize (sizeDlg);
128 m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
129 m_pDC = new wxMemoryDC;
130 m_pDC->SelectObject (m_bitmap); // in memoryDC
131 m_pDC->SetFont (*wxSWISS_FONT);
133 this->GetClientSize(&x, &y);
134 m_pSGPDriver = new SGPDriver (dynamic_cast<wxDC*>(m_pDC), x, y);
135 m_pSGP = new SGP (*m_pSGPDriver);
137 Centre(wxCENTER_FRAME | wxBOTH);
140 m_parentTop->Enable(FALSE);
143 Enable(TRUE); // enable this window
145 wxYield(); // Update the display
147 m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
149 MacUpdateImmediately();
154 ReconstructDialog::showView (int iViewNumber)
156 if ( iViewNumber < m_rProjections.nView() ) {
157 m_iLastView = iViewNumber;
158 m_pSGP->eraseWindow();
160 char szProgress [256];
161 snprintf (szProgress, sizeof(szProgress), "Reconstructing View %d (%.1f%%)", iViewNumber, 100 * iViewNumber / static_cast<double>(m_rProjections.nView()));
162 m_pSGP->setViewport (0, 0, 1, 1);
163 m_pSGP->setWindow (0, 0, 1, 1);
164 m_pSGP->setTextColor (C_LTRED, -1);
165 double dCharHeight = m_pSGP->getCharHeight();
166 m_pSGP->setTextSize (dCharHeight * 2);
167 m_pSGP->moveAbs(0., m_pSGP->getCharHeight());
168 m_pSGP->drawText (szProgress);
169 m_pSGP->setTextSize (dCharHeight);
171 m_pSGP->setViewport (0.0, 0.1, 0.66, 1.);
172 m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP);
174 ImageFileArrayConst v = m_rImageFile.getArray();
175 int xBase = m_nxGraph;
177 if (m_nyGraph > m_nyImage)
178 yBase = (m_nyGraph - m_nyImage) / 2;
179 double minValue = v[0][0];
180 double maxValue = v[0][0];
181 for (int ix = 0; ix < m_nxImage; ix++) {
182 for (int iy = 0; iy < m_nyImage; iy++) {
183 double dPixel = v[ix][iy];
184 if (dPixel < minValue)
186 else if (dPixel > maxValue)
190 unsigned char* imageData = new unsigned char [m_nxImage * m_nyImage * 3];
191 double dScale = 255 / (maxValue - minValue);
192 for (int ix = 0; ix < m_nxImage; ix++) {
193 for (int iy = 0; iy < m_nyImage; iy++) {
194 double dPixel = v[ix][iy];
195 dPixel = (dPixel - minValue) * dScale;
196 int intensity = nearest<int>(dPixel);
197 intensity = clamp (intensity, 0, 255);
198 int baseAddr = ((m_nyImage - 1 - iy) * m_nxImage + ix) * 3;
199 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
202 wxImage image (m_nxImage, m_nyImage, imageData, true);
203 wxBitmap bitmap = image.ConvertToBitmap();
204 m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
212 ReconstructDialog::reconstructView (int iViewNumber)
214 if (iViewNumber <= m_iLastView) // have already done this view
217 if (iViewNumber < m_rProjections.nView()) {
218 ::wxYield(); // update the display
219 showView (iViewNumber);
220 ::wxYield(); // update the display
221 if (m_iTrace >= Trace::TRACE_PLOT) {
225 m_state = Finished; // so that we return TRUE below and
226 } // that [Cancel] handler knew what to do
229 MacUpdateImmediately();
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
265 if (m_state == Continue) {
267 m_btnPause->SetLabel (wxString("Resume"));
268 } else if (m_state == Paused) {
270 m_btnPause->SetLabel (wxString("Pause"));
276 ReconstructDialog::OnStep (wxCommandEvent& event)
278 if ( m_state == Finished ) {
281 if (m_state == Continue) {
283 m_btnPause->SetLabel (wxString("Resume"));
284 } else if (m_state == Paused) {
285 reconstructView (m_iLastView + 1);
290 void ReconstructDialog::OnClose(wxCloseEvent& event)
292 if ( m_state == Uncancellable )
293 event.Veto(TRUE); // can't close this dialog
294 else if ( m_state == Finished )
295 event.Skip(); // let the default handler close the window as we already terminated
297 m_state = Cancelled; // next Update() will notice it
301 ReconstructDialog::OnPaint (wxPaintEvent& event)
303 wxPaintDC paintDC (this);
304 paintDC.DrawBitmap(m_bitmap, 0, 0, false);
308 /////////////////////////////////////////////////////
311 ReconstructDialog::~ReconstructDialog()
314 m_parentTop->Enable(TRUE);