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