r11859: Canonicalize whitespace
[ctsim.git] / src / dlgreconstruct.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          dlgreconstruct.cpp
5 **   Purpose:       Reconstruction Animation Dialog
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  August 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id$
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "wx/wxprec.h"
29
30 #include "wx/utils.h"
31 #include "wx/frame.h"
32 #include "wx/button.h"
33 #include "wx/stattext.h"
34 #include "wx/layout.h"
35 #include "wx/event.h"
36 #include "wx/intl.h"
37 #include "wx/settings.h"
38 #include "wx/dcclient.h"
39 #include "wx/timer.h"
40 #include "wx/image.h"
41
42 #include "dlgreconstruct.h"
43 #include <algorithm>
44 #include "ct.h"
45
46
47 static const int LAYOUT_X_MARGIN = 4;
48 static const int LAYOUT_Y_MARGIN = 4;
49
50
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;
55
56
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)
63 END_EVENT_TABLE()
64
65 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
66
67
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)
73 {
74     m_state = Continue;
75     m_iLastView = -1;
76     m_parentTop = parent;
77     while ( m_parentTop && m_parentTop->GetParent() )
78         m_parentTop = m_parentTop->GetParent();
79
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);
84
85     wxSize sizeBtn = wxButton::GetDefaultSize();
86     c->width.Absolute(sizeBtn.x);
87     c->height.Absolute(sizeBtn.y);
88
89     m_btnAbort->SetConstraints(c);
90
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);
98
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);
106
107     SetAutoLayout(TRUE);
108     Layout();
109
110     m_nxGraph = 500;
111     m_nyGraph = 500;
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;
119
120     sizeDlg.x += m_nxImage;
121     sizeDlg.y = imax (sizeDlg.y, m_nyImage);
122
123     m_iClientX = sizeDlg.x;
124     m_iClientY = sizeDlg.y;
125     SetClientSize (sizeDlg);
126
127     Centre(wxCENTER_FRAME | wxBOTH);
128
129     if ( m_parentTop )
130                 m_parentTop->Enable(FALSE);
131
132     Show(TRUE);
133     Enable(TRUE); // enable this window
134
135     m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
136     m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
137     int x, y;
138     this->GetClientSize(&x, &y);
139     m_pSGPDriver = new SGPDriver (m_pDC, x, y);
140     m_pSGP = new SGP (*m_pSGPDriver);
141
142     wxYield();     // Update the display
143
144     m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
145 }
146
147 void
148 ReconstructDialog::showView (int iViewNumber, bool bBackprojectView)
149 {
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();
157
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.);
167
168     int iXDialog, iYDialog;
169     GetClientSize (&iXDialog, &iYDialog);
170     double dGraphWidth = (iXDialog - m_nxImage) / static_cast<double>(iXDialog);
171
172                 m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP, bBackprojectView, dGraphWidth);
173     ::wxYield();
174 ::wxYield();
175 ::wxYield();
176 ::wxYield();
177
178                 ImageFileArrayConst v = m_rImageFile.getArray();
179                 int xBase = m_nxGraph;
180                 int yBase = 0;
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)
189                                         minValue = dPixel;
190                                 else if (dPixel > maxValue)
191                                         maxValue = dPixel;
192                         }
193                 }
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;
204                         }
205                 }
206                 wxImage image (m_nxImage, m_nyImage, imageData, true);
207                 wxBitmap bitmap (image);
208                 m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
209                 delete imageData;
210         }
211
212   ::wxYield();
213 }
214
215 bool
216 ReconstructDialog::reconstructView (int iViewNumber, bool bBackproject)
217 {
218         if (iViewNumber <= m_iLastView)  // have already done this view
219                 return true;
220
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) {
226                         ::wxUsleep(250);
227                 }
228         } else {
229                 m_state = Finished;    // so that we return TRUE below and
230         }                        // that [Cancel] handler knew what to do
231
232         ::wxYield();        // update the display
233         return m_state != Cancelled;
234 }
235
236
237 // EVENT HANDLERS
238
239 void ReconstructDialog::OnCancel (wxCommandEvent& event)
240 {
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
244                 event.Skip();
245         } else {
246                 // request to cancel was received, the next time Update() is called we
247                 // will handle it
248                 m_state = Cancelled;
249
250                 // update the button state immediately so that the user knows that the
251                 // request has been noticed
252                 m_btnAbort->Disable();
253         }
254 }
255
256
257 void
258 ReconstructDialog::OnPause (wxCommandEvent& event)
259 {
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
263                 event.Skip();
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);
269                 m_state = Paused;
270                 m_btnPause->SetLabel (wxString("Resume"));
271                 m_pSGP->setDC (m_pDC);
272                 m_memoryDC.SelectObject(wxNullBitmap);
273         } else if (m_state == Paused) {
274                 m_state = Continue;
275                 m_btnPause->SetLabel (wxString("Pause"));
276         }
277 }
278
279
280 void
281 ReconstructDialog::OnStep (wxCommandEvent& event)
282 {
283         if ( m_state == Finished ) {
284                 event.Skip();
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);
290                 m_state = Paused;
291                 m_btnPause->SetLabel (wxString("Resume"));
292                 m_pSGP->setDC (m_pDC);
293                 m_memoryDC.SelectObject(wxNullBitmap);
294                 Refresh();
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);
302                 Refresh();
303         }
304 }
305
306 void ReconstructDialog::OnClose(wxCloseEvent& event)
307 {
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
312     else
313                 m_state = Cancelled;          // next Update() will notice it
314 }
315
316 void
317 ReconstructDialog::OnPaint (wxPaintEvent& event)
318 {
319         wxPaintDC paintDC (this);
320         if (m_state == Paused) {
321                 paintDC.DrawBitmap (m_bitmap, 0, 0, false);
322         }
323 }
324
325
326 /////////////////////////////////////////////////////
327 // destruction
328
329 ReconstructDialog::~ReconstructDialog()
330 {
331         if ( m_parentTop )
332                 m_parentTop->Enable(TRUE);
333
334         delete m_pSGP;
335         delete m_pSGPDriver;
336         delete m_pDC;
337 }
338