r198: *** 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-2000 Kevin Rosenberg
11 **
12 **  $Id: dlgreconstruct.cpp,v 1.5 2000/09/07 14:29:05 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 #ifndef WX_PRECOMP
40     #include "wx/utils.h"
41     #include "wx/frame.h"
42     #include "wx/button.h"
43     #include "wx/stattext.h"
44     #include "wx/layout.h"
45     #include "wx/event.h"
46     #include "wx/intl.h"
47     #include "wx/settings.h"
48     #include "wx/dcclient.h"
49     #include "wx/timer.h"
50     #include "wx/image.h"
51 #endif
52
53 #include "dlgreconstruct.h"
54 #include <algorithm>
55 #include "ct.h"
56
57
58 static const int LAYOUT_X_MARGIN = 4;
59 static const int LAYOUT_Y_MARGIN = 4;
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, 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)
74 {
75     m_state = Continue;
76     m_iLastView = -1;
77     m_parentTop = parent;
78     while ( m_parentTop && m_parentTop->GetParent() )
79         m_parentTop = m_parentTop->GetParent();
80     
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);
85
86     wxSize sizeBtn = wxButton::GetDefaultSize();
87     c->width.Absolute(sizeBtn.x);
88     c->height.Absolute(sizeBtn.y);
89
90     m_btnAbort->SetConstraints(c);
91
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);
99
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);
107
108     SetAutoLayout(TRUE);
109     Layout();
110
111     m_nxGraph = 500;
112     m_nyGraph = 500;
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;
120
121     sizeDlg.x += m_nxImage;
122     sizeDlg.y = max (sizeDlg.y, m_nyImage);
123
124     m_iClientX = sizeDlg.x;
125     m_iClientY = sizeDlg.y;
126     SetClientSize (sizeDlg);
127
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);
132     int x, y;
133     this->GetClientSize(&x, &y);
134     m_pSGPDriver = new SGPDriver (dynamic_cast<wxDC*>(m_pDC), x, y);
135     m_pSGP = new SGP (*m_pSGPDriver);
136
137     Centre(wxCENTER_FRAME | wxBOTH);
138
139     if ( m_parentTop )
140       m_parentTop->Enable(FALSE);
141
142     Show(TRUE);
143     Enable(TRUE); // enable this window
144
145     wxYield();     // Update the display
146
147     m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
148 #ifdef __WXMAC__
149     MacUpdateImmediately();
150 #endif
151 }
152
153 void
154 ReconstructDialog::showView (int iViewNumber)
155 {
156   if ( iViewNumber < m_rProjections.nView() ) {
157     m_iLastView = iViewNumber;
158     m_pSGP->eraseWindow();
159
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);
170
171     m_pSGP->setViewport (0.0, 0.1, 0.66, 1.);
172     m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP);
173
174     ImageFileArrayConst v = m_rImageFile.getArray();
175     int xBase = m_nxGraph;
176     int yBase = 0;
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)
185                 minValue = dPixel;
186             else if (dPixel > maxValue)
187                 maxValue = dPixel;
188         }
189     }
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;
200         }
201     }
202     wxImage image (m_nxImage, m_nyImage, imageData, true);
203     wxBitmap bitmap = image.ConvertToBitmap();
204     m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
205     delete imageData;
206
207     Refresh();
208   }
209 }
210
211 bool
212 ReconstructDialog::reconstructView (int iViewNumber)
213 {
214   if (iViewNumber <= m_iLastView)  // have already done this view
215     return true;
216
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) {
222       ::wxUsleep(250);
223     }
224   } else {
225     m_state = Finished;    // so that we return TRUE below and 
226   }                        // that [Cancel] handler knew what to do
227   
228 #ifdef __WXMAC__
229   MacUpdateImmediately();
230 #endif
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 {
265       if (m_state == Continue) {
266         m_state = Paused;
267         m_btnPause->SetLabel (wxString("Resume"));
268       } else if (m_state == Paused) {
269         m_state = Continue;
270         m_btnPause->SetLabel (wxString("Pause"));
271       }
272   }
273 }
274
275 void 
276 ReconstructDialog::OnStep (wxCommandEvent& event)
277 {
278   if ( m_state == Finished ) {
279     event.Skip();
280   } else {
281     if (m_state == Continue) {
282       m_state = Paused;
283       m_btnPause->SetLabel (wxString("Resume"));
284     } else if (m_state == Paused) {
285       reconstructView (m_iLastView + 1);
286     }
287   }
288 }
289
290 void ReconstructDialog::OnClose(wxCloseEvent& event)
291 {
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
296     else
297       m_state = Cancelled;          // next Update() will notice it
298 }
299
300 void
301 ReconstructDialog::OnPaint (wxPaintEvent& event)
302 {
303   wxPaintDC paintDC (this);
304   paintDC.DrawBitmap(m_bitmap, 0, 0, false);
305 }
306
307
308 /////////////////////////////////////////////////////
309 // destruction
310
311 ReconstructDialog::~ReconstructDialog()
312 {
313   if ( m_parentTop )
314     m_parentTop->Enable(TRUE);
315
316   delete m_pSGP;
317   delete m_pSGPDriver;
318   delete m_pDC;
319 }
320