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