r557: no message
[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: dlgreconstruct.cpp,v 1.17 2001/02/20 04:48:45 kevin Exp $
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 #ifdef __GNUG__
29 #pragma implementation "dlgreconstruct.h"
30 #endif
31
32 // For compilers that support precompilation, includes "wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #include "wx/utils.h"
40 #include "wx/frame.h"
41 #include "wx/button.h"
42 #include "wx/stattext.h"
43 #include "wx/layout.h"
44 #include "wx/event.h"
45 #include "wx/intl.h"
46 #include "wx/settings.h"
47 #include "wx/dcclient.h"
48 #include "wx/timer.h"
49 #include "wx/image.h"
50
51 #include "dlgreconstruct.h"
52 #include <algorithm>
53 #include "ct.h"
54
55
56 static const int LAYOUT_X_MARGIN = 4;
57 static const int LAYOUT_Y_MARGIN = 4;
58
59
60 const int ReconstructDialog::ID_BTN_PAUSE = 19998;
61 const int ReconstructDialog::ID_BTN_STEP = 19999;
62 const int ReconstructDialog::MAX_IMAGE_X = 400;
63 const int ReconstructDialog::MAX_IMAGE_Y = 400;
64
65
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)
72 END_EVENT_TABLE()
73
74 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
75
76
77 ReconstructDialog::ReconstructDialog (Reconstructor& rReconstruct, const Projections& rProj, 
78                                       ImageFile& rIF, const int iTrace, wxWindow *parent)
79 : wxDialog(parent, -1, "Reconstruction", wxDefaultPosition), m_rReconstructor(rReconstruct), 
80            m_rProjections(rProj), m_rImageFile(rIF), m_pSGPDriver(NULL), m_pSGP(NULL), 
81   m_iTrace(iTrace), m_pDC(NULL), m_btnAbort(0), m_btnPause(0), m_btnStep(0)
82 {
83     m_state = Continue;
84     m_iLastView = -1;
85     m_parentTop = parent;
86     while ( m_parentTop && m_parentTop->GetParent() )
87         m_parentTop = m_parentTop->GetParent();
88     
89     m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
90     wxLayoutConstraints* c = new wxLayoutConstraints;
91     c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
92     c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
93         
94     wxSize sizeBtn = wxButton::GetDefaultSize();
95     c->width.Absolute(sizeBtn.x);
96     c->height.Absolute(sizeBtn.y);
97         
98     m_btnAbort->SetConstraints(c);
99         
100     m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
101     wxLayoutConstraints* cPause = new wxLayoutConstraints;
102     cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
103     cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
104     cPause->width.Absolute(sizeBtn.x);
105     cPause->height.Absolute(sizeBtn.y);
106     m_btnPause->SetConstraints(cPause);
107         
108     m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
109     wxLayoutConstraints* cStep = new wxLayoutConstraints;
110     cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
111     cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
112     cStep->width.Absolute(sizeBtn.x);
113     cStep->height.Absolute(sizeBtn.y);
114     m_btnStep->SetConstraints(cStep);
115         
116     SetAutoLayout(TRUE);
117     Layout();
118         
119     m_nxGraph = 500;
120     m_nyGraph = 500;
121     wxSize sizeDlg (m_nxGraph, m_nyGraph);
122     m_nxImage = m_rImageFile.nx();
123     if (m_nxImage > MAX_IMAGE_X)
124                    m_nxImage = MAX_IMAGE_X;
125     m_nyImage = m_rImageFile.ny();
126     if (m_nyImage > MAX_IMAGE_Y)
127                    m_nyImage = MAX_IMAGE_Y;
128         
129     sizeDlg.x += m_nxImage;
130     sizeDlg.y = max (sizeDlg.y, m_nyImage);
131         
132     m_iClientX = sizeDlg.x;
133     m_iClientY = sizeDlg.y;
134     SetClientSize (sizeDlg);
135         
136     Centre(wxCENTER_FRAME | wxBOTH);
137         
138     if ( m_parentTop )
139                 m_parentTop->Enable(FALSE);
140         
141     Show(TRUE);
142     Enable(TRUE); // enable this window
143         
144     m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
145     m_pDC = dynamic_cast<wxDC*> (new wxClientDC (this));
146     int x, y;
147     this->GetClientSize(&x, &y);
148     m_pSGPDriver = new SGPDriver (m_pDC, x, y);
149     m_pSGP = new SGP (*m_pSGPDriver);
150         
151     wxYield();     // Update the display
152         
153     m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
154 }
155
156 void
157 ReconstructDialog::showView (int iViewNumber, bool bBackprojectView)
158 {
159         if ( iViewNumber < m_rProjections.nView() ) {
160                 m_iLastView = iViewNumber;
161                 ::wxYield();        // update the display
162                 m_pSGP->eraseWindow();
163                 m_btnPause->Refresh();
164                 m_btnStep->Refresh();
165                 m_btnAbort->Refresh();
166
167                 char szProgress [256];
168                 snprintf (szProgress, sizeof(szProgress), "Reconstructing View %d (%.1f%%)", iViewNumber, 100 * iViewNumber / static_cast<double>(m_rProjections.nView()));
169                 m_pSGP->setViewport (0, 0, 1, 1);
170                 m_pSGP->setWindow (0, 0, 1, 1);
171                 m_pSGP->setTextColor (C_LTRED, -1);
172                 m_pSGP->setTextPointSize (20.);
173                 m_pSGP->moveAbs(0., m_pSGP->getCharHeight());
174                 m_pSGP->drawText (szProgress);
175                 m_pSGP->setTextPointSize (10.);
176                 
177     int iXDialog, iYDialog;
178     GetClientSize (&iXDialog, &iYDialog);
179     double dGraphWidth = (iXDialog - m_nxImage) / static_cast<double>(iXDialog);
180
181                 m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP, bBackprojectView, dGraphWidth);
182     ::wxYield();
183 ::wxYield();
184 ::wxYield();
185 ::wxYield();
186
187                 ImageFileArrayConst v = m_rImageFile.getArray();
188                 int xBase = m_nxGraph;
189                 int yBase = 0;
190                 if (m_nyGraph > m_nyImage)
191                         yBase = (m_nyGraph - m_nyImage) / 2;
192                 double minValue = v[0][0];
193                 double maxValue = v[0][0];
194                 for (int ix = 0; ix < m_nxImage; ix++) {
195                         for (int iy = 0; iy < m_nyImage; iy++) {
196                                 double dPixel = v[ix][iy];
197                                 if (dPixel < minValue)
198                                         minValue = dPixel;
199                                 else if (dPixel > maxValue)
200                                         maxValue = dPixel;
201                         }
202                 }
203                 unsigned char* imageData = new unsigned char [m_nxImage * m_nyImage * 3];
204                 double dScale = 255 / (maxValue - minValue);
205                 for (int ix2 = 0; ix2 < m_nxImage; ix2++) {
206                         for (int iy = 0; iy < m_nyImage; iy++) {
207                                 double dPixel = v[ix2][iy];
208                                 dPixel = (dPixel - minValue) * dScale;
209                                 int intensity = nearest<int>(dPixel);
210                                 intensity = clamp (intensity, 0, 255);
211                                 int baseAddr = ((m_nyImage - 1 - iy) * m_nxImage + ix2) * 3;
212                                 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
213                         }
214                 }
215                 wxImage image (m_nxImage, m_nyImage, imageData, true);
216                 wxBitmap bitmap = image.ConvertToBitmap();
217                 m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
218                 delete imageData;
219         }
220
221   ::wxYield();
222 }
223
224 bool
225 ReconstructDialog::reconstructView (int iViewNumber, bool bBackproject)
226 {
227         if (iViewNumber <= m_iLastView)  // have already done this view
228                 return true;
229         
230         if (iViewNumber < m_rProjections.nView()) {
231                 ::wxYield();        // update the display
232                 showView (iViewNumber, bBackproject);
233                 ::wxYield();        // update the display
234                 if (m_iTrace >= Trace::TRACE_PLOT) {
235                         ::wxUsleep(250);
236                 }
237         } else {
238                 m_state = Finished;    // so that we return TRUE below and 
239         }                        // that [Cancel] handler knew what to do
240         
241         ::wxYield();        // update the display
242         return m_state != Cancelled;
243 }
244
245
246 // EVENT HANDLERS
247
248 void ReconstructDialog::OnCancel (wxCommandEvent& event)
249 {
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
253                 event.Skip();
254         } else {
255                 // request to cancel was received, the next time Update() is called we
256                 // will handle it
257                 m_state = Cancelled;
258                 
259                 // update the button state immediately so that the user knows that the
260                 // request has been noticed
261                 m_btnAbort->Disable();
262         }
263 }
264
265
266 void 
267 ReconstructDialog::OnPause (wxCommandEvent& event)
268 {
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
272                 event.Skip();
273         } else if (m_state == Continue) {
274                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
275                 m_pSGP->setDC (&m_memoryDC);
276                 m_memoryDC.SetFont (*wxSWISS_FONT);
277                 showView (m_iLastView, false);
278                 m_state = Paused;
279                 m_btnPause->SetLabel (wxString("Resume"));
280                 m_pSGP->setDC (m_pDC);
281                 m_memoryDC.SelectObject(wxNullBitmap);
282         } else if (m_state == Paused) {
283                 m_state = Continue;
284                 m_btnPause->SetLabel (wxString("Pause"));
285         }
286 }
287
288
289 void 
290 ReconstructDialog::OnStep (wxCommandEvent& event)
291 {
292         if ( m_state == Finished ) {
293                 event.Skip();
294         } else if (m_state == Continue) {
295                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
296                 m_pSGP->setDC (&m_memoryDC);
297                 m_memoryDC.SetFont (*wxSWISS_FONT);
298                 showView (m_iLastView, false);
299                 m_state = Paused;
300                 m_btnPause->SetLabel (wxString("Resume"));
301                 m_pSGP->setDC (m_pDC);
302                 m_memoryDC.SelectObject(wxNullBitmap);
303                 Refresh();
304         } else if (m_state == Paused) {
305                 m_memoryDC.SelectObject (m_bitmap);       // in memoryDC
306                 m_pSGP->setDC (&m_memoryDC);
307                 m_memoryDC.SetFont (*wxSWISS_FONT);
308                 reconstructView (m_iLastView + 1);
309                 m_pSGP->setDC (m_pDC);
310                 m_memoryDC.SelectObject(wxNullBitmap);
311                 Refresh();
312         }
313 }
314
315 void ReconstructDialog::OnClose(wxCloseEvent& event)
316 {
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
321     else
322                 m_state = Cancelled;          // next Update() will notice it
323 }
324
325 void
326 ReconstructDialog::OnPaint (wxPaintEvent& event)
327 {
328         wxPaintDC paintDC (this);
329         if (m_state == Paused) {
330                 paintDC.DrawBitmap (m_bitmap, 0, 0, false);
331         }
332 }
333
334
335 /////////////////////////////////////////////////////
336 // destruction
337
338 ReconstructDialog::~ReconstructDialog()
339 {
340         if ( m_parentTop )
341                 m_parentTop->Enable(TRUE);
342         
343         delete m_pSGP;
344         delete m_pSGPDriver;
345         delete m_pDC;
346 }
347