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