r259: MSVC modifications
[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.6 2000/12/16 03:29:02 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 \r
61 \r
62 const int ReconstructDialog::ID_BTN_PAUSE = 19998;\r
63 const int ReconstructDialog::ID_BTN_STEP = 19999;\r
64 const int ReconstructDialog::MAX_IMAGE_X = 400;\r
65 const int ReconstructDialog::MAX_IMAGE_Y = 400;\r
66 \r
67
68 BEGIN_EVENT_TABLE(ReconstructDialog, wxDialog)
69    EVT_BUTTON(wxID_CANCEL, ReconstructDialog::OnCancel)
70    EVT_BUTTON(ID_BTN_PAUSE, ReconstructDialog::OnPause)
71    EVT_BUTTON(ID_BTN_STEP, ReconstructDialog::OnStep)
72    EVT_CLOSE(ReconstructDialog::OnClose)
73    EVT_PAINT(ReconstructDialog::OnPaint)
74 END_EVENT_TABLE()
75
76 IMPLEMENT_CLASS(ReconstructDialog, wxDialog)
77
78
79 ReconstructDialog::ReconstructDialog (Reconstructor& rReconstruct, const Projections& rProj, ImageFile& rIF, const int iTrace, wxWindow *parent)
80     : 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)
81 {
82     m_state = Continue;
83     m_iLastView = -1;
84     m_parentTop = parent;
85     while ( m_parentTop && m_parentTop->GetParent() )
86         m_parentTop = m_parentTop->GetParent();
87     
88     m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
89     wxLayoutConstraints* c = new wxLayoutConstraints;
90     c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
91     c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
92
93     wxSize sizeBtn = wxButton::GetDefaultSize();
94     c->width.Absolute(sizeBtn.x);
95     c->height.Absolute(sizeBtn.y);
96
97     m_btnAbort->SetConstraints(c);
98
99     m_btnPause = new wxButton (this, ID_BTN_PAUSE, wxString("Pause"));
100     wxLayoutConstraints* cPause = new wxLayoutConstraints;
101     cPause->right.SameAs(this, wxRight, 3*LAYOUT_X_MARGIN + sizeBtn.x);
102     cPause->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
103     cPause->width.Absolute(sizeBtn.x);
104     cPause->height.Absolute(sizeBtn.y);
105     m_btnPause->SetConstraints(cPause);
106
107     m_btnStep = new wxButton (this, ID_BTN_STEP, wxString("Step"));
108     wxLayoutConstraints* cStep = new wxLayoutConstraints;
109     cStep->right.SameAs(this, wxRight, 5*LAYOUT_X_MARGIN + sizeBtn.x * 2);
110     cStep->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
111     cStep->width.Absolute(sizeBtn.x);
112     cStep->height.Absolute(sizeBtn.y);
113     m_btnStep->SetConstraints(cStep);
114
115     SetAutoLayout(TRUE);
116     Layout();
117
118     m_nxGraph = 500;
119     m_nyGraph = 500;
120     wxSize sizeDlg (m_nxGraph, m_nyGraph);
121     m_nxImage = m_rImageFile.nx();
122     if (m_nxImage > MAX_IMAGE_X)
123       m_nxImage = MAX_IMAGE_X;
124     m_nyImage = m_rImageFile.ny();
125     if (m_nyImage > MAX_IMAGE_Y)
126       m_nyImage = MAX_IMAGE_Y;
127
128     sizeDlg.x += m_nxImage;
129     sizeDlg.y = max (sizeDlg.y, m_nyImage);
130
131     m_iClientX = sizeDlg.x;
132     m_iClientY = sizeDlg.y;
133     SetClientSize (sizeDlg);
134
135     m_bitmap.Create (m_iClientX, m_iClientY); // save a copy of screen
136     m_pDC = new wxMemoryDC;
137     m_pDC->SelectObject (m_bitmap);       // in memoryDC
138     m_pDC->SetFont (*wxSWISS_FONT);
139     int x, y;
140     this->GetClientSize(&x, &y);
141     m_pSGPDriver = new SGPDriver (dynamic_cast<wxDC*>(m_pDC), x, y);
142     m_pSGP = new SGP (*m_pSGPDriver);
143
144     Centre(wxCENTER_FRAME | wxBOTH);
145
146     if ( m_parentTop )
147       m_parentTop->Enable(FALSE);
148
149     Show(TRUE);
150     Enable(TRUE); // enable this window
151
152     wxYield();     // Update the display
153
154     m_pSGPDriver->idWX()->SetFont(*wxSWISS_FONT);
155 #ifdef __WXMAC__
156     MacUpdateImmediately();
157 #endif
158 }
159
160 void
161 ReconstructDialog::showView (int iViewNumber)
162 {
163   if ( iViewNumber < m_rProjections.nView() ) {
164     m_iLastView = iViewNumber;
165     m_pSGP->eraseWindow();
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     double dCharHeight = m_pSGP->getCharHeight();
173     m_pSGP->setTextSize (dCharHeight * 2);
174     m_pSGP->moveAbs(0., m_pSGP->getCharHeight());
175     m_pSGP->drawText (szProgress);
176     m_pSGP->setTextSize (dCharHeight);
177
178     m_pSGP->setViewport (0.0, 0.1, 0.66, 1.);
179     m_rReconstructor.reconstructView (iViewNumber, 1, m_pSGP);
180
181     ImageFileArrayConst v = m_rImageFile.getArray();
182     int xBase = m_nxGraph;
183     int yBase = 0;
184     if (m_nyGraph > m_nyImage)
185         yBase = (m_nyGraph - m_nyImage) / 2;
186     double minValue = v[0][0];
187     double maxValue = v[0][0];
188     for (int ix = 0; ix < m_nxImage; ix++) {
189         for (int iy = 0; iy < m_nyImage; iy++) {
190             double dPixel = v[ix][iy];
191             if (dPixel < minValue)
192                 minValue = dPixel;
193             else if (dPixel > maxValue)
194                 maxValue = dPixel;
195         }
196     }
197     unsigned char* imageData = new unsigned char [m_nxImage * m_nyImage * 3];
198     double dScale = 255 / (maxValue - minValue);
199     for (int ix2 = 0; ix2 < m_nxImage; ix2++) {
200         for (int iy = 0; iy < m_nyImage; iy++) {
201             double dPixel = v[ix2][iy];
202             dPixel = (dPixel - minValue) * dScale;
203             int intensity = nearest<int>(dPixel);
204             intensity = clamp (intensity, 0, 255);
205             int baseAddr = ((m_nyImage - 1 - iy) * m_nxImage + ix2) * 3;
206             imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
207         }
208     }
209     wxImage image (m_nxImage, m_nyImage, imageData, true);
210     wxBitmap bitmap = image.ConvertToBitmap();
211     m_pSGP->getDriver().idWX()->DrawBitmap(bitmap, xBase, yBase, false);
212     delete imageData;
213
214     Refresh();
215   }
216 }
217
218 bool
219 ReconstructDialog::reconstructView (int iViewNumber)
220 {
221   if (iViewNumber <= m_iLastView)  // have already done this view
222     return true;
223
224   if (iViewNumber < m_rProjections.nView()) {
225     ::wxYield();        // update the display
226     showView (iViewNumber);
227     ::wxYield();        // update the display
228     if (m_iTrace >= Trace::TRACE_PLOT) {
229       ::wxUsleep(250);
230     }
231   } else {
232     m_state = Finished;    // so that we return TRUE below and 
233   }                        // that [Cancel] handler knew what to do
234   
235 #ifdef __WXMAC__
236   MacUpdateImmediately();
237 #endif
238
239   ::wxYield();        // update the display
240   return m_state != Cancelled;
241 }
242
243
244 // EVENT HANDLERS
245
246 void ReconstructDialog::OnCancel (wxCommandEvent& event)
247 {
248   if ( m_state == Finished ) {
249     // this means that the count down is already finished and we're being
250     // shown as a modal dialog - so just let the default handler do the job
251     event.Skip();
252   } else {
253     // request to cancel was received, the next time Update() is called we
254     // will handle it
255     m_state = Cancelled;
256
257     // update the button state immediately so that the user knows that the
258     // request has been noticed
259     m_btnAbort->Disable();
260   }
261 }
262
263
264 void 
265 ReconstructDialog::OnPause (wxCommandEvent& event)
266 {
267   if ( m_state == Finished ) {
268     // this means that the count down is already finished and we're being
269     // shown as a modal dialog - so just let the default handler do the job
270     event.Skip();
271   } else {
272       if (m_state == Continue) {
273         m_state = Paused;
274         m_btnPause->SetLabel (wxString("Resume"));
275       } else if (m_state == Paused) {
276         m_state = Continue;
277         m_btnPause->SetLabel (wxString("Pause"));
278       }
279   }
280 }
281
282 void 
283 ReconstructDialog::OnStep (wxCommandEvent& event)
284 {
285   if ( m_state == Finished ) {
286     event.Skip();
287   } else {
288     if (m_state == Continue) {
289       m_state = Paused;
290       m_btnPause->SetLabel (wxString("Resume"));
291     } else if (m_state == Paused) {
292       reconstructView (m_iLastView + 1);
293     }
294   }
295 }
296
297 void ReconstructDialog::OnClose(wxCloseEvent& event)
298 {
299     if ( m_state == Uncancellable )
300       event.Veto(TRUE);    // can't close this dialog
301     else if ( m_state == Finished )
302       event.Skip(); // let the default handler close the window as we already terminated
303     else
304       m_state = Cancelled;          // next Update() will notice it
305 }
306
307 void
308 ReconstructDialog::OnPaint (wxPaintEvent& event)
309 {
310   wxPaintDC paintDC (this);
311   paintDC.DrawBitmap(m_bitmap, 0, 0, false);
312 }
313
314
315 /////////////////////////////////////////////////////
316 // destruction
317
318 ReconstructDialog::~ReconstructDialog()
319 {
320   if ( m_parentTop )
321     m_parentTop->Enable(TRUE);
322
323   delete m_pSGP;
324   delete m_pSGPDriver;
325   delete m_pDC;
326 }
327