r301: *** empty log message ***
[ctsim.git] / src / views.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          view.cpp
5 **   Purpose:       View & Canvas routines for CTSim program
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: views.cpp,v 1.30 2000/12/18 09:31:26 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 // For compilers that support precompilation, includes "wx/wx.h".
29 #include "wx/wxprec.h"
30
31 #ifdef __BORLANDC__
32 #pragma hdrstop
33 #endif
34
35 #ifndef WX_PRECOMP
36 #include "wx/wx.h"
37 #endif
38
39 #if !wxUSE_DOC_VIEW_ARCHITECTURE
40 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
41 #endif
42
43 #include "wx/image.h"
44 #include "wx/progdlg.h"
45
46 #include "ct.h"
47 #include "ctsim.h"
48 #include "docs.h"
49 #include "views.h"
50 #include "dialogs.h"
51 #include "dlgprojections.h"
52 #include "dlgreconstruct.h"
53 #include "backprojectors.h"
54 #include "reconstruct.h"
55 #include "timer.h"
56 \r
57 #if defined(MSVC) || HAVE_SSTREAM\r
58 #include <sstream>\r
59 #else\r
60 #include <sstream_subst>\r
61 #endif\r
62 \r
63
64 // ImageFileCanvas
65
66 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
67 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
68 END_EVENT_TABLE()
69
70
71 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
72 : wxScrolledWindow(frame, -1, pos, size, style)
73 {
74     m_pView = v;
75 }
76
77 void 
78 ImageFileCanvas::OnDraw(wxDC& dc)
79 {
80     if (m_pView)
81         m_pView->OnDraw(& dc);
82 }
83
84 void 
85 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
86 {
87     if (! m_pView)
88         return;
89     
90     wxClientDC dc(this);
91     PrepareDC(dc);
92     
93     wxPoint pt(event.GetLogicalPosition(dc));
94         
95     if (event.LeftIsDown()) {
96                 const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
97                 ImageFileArrayConst v = rIF.getArray();
98                 int nx = rIF.nx();
99                 int ny = rIF.ny();
100                 
101                 if (pt.x >= 0 && pt.x < nx && pt.y >= 0 && pt.y < ny) {
102                         std::ostringstream os;
103                         os << "Image value (" << pt.x << "," << pt.y << ") = " << v[pt.x][ny - 1 - pt.y] << "\n";
104                         *theApp->getLog() << os.str().c_str();
105                 } else
106                         *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << pt.y << ")\n";
107                 
108     }
109 }
110
111
112 // ImageFileView
113
114 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
115
116 BEGIN_EVENT_TABLE(ImageFileView, wxView)
117 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
118 EVT_MENU(IFMENU_VIEW_SCALE_MINMAX, ImageFileView::OnScaleMinMax)
119 EVT_MENU(IFMENU_VIEW_SCALE_AUTO, ImageFileView::OnScaleAuto)
120 END_EVENT_TABLE()
121
122 ImageFileView::ImageFileView(void) 
123 : wxView(), m_canvas(NULL), m_frame(NULL), m_bMinSpecified(false), m_bMaxSpecified(false)
124 {
125 }
126
127 ImageFileView::~ImageFileView(void)
128 {
129 }
130
131 void
132 ImageFileView::OnProperties (wxCommandEvent& event)
133 {
134         double min, max, mean, mode, median, stddev;
135         const ImageFile& rIF = GetDocument()->getImageFile();
136         if (rIF.nx() == 0 || rIF.ny() == 0)
137                 *theApp->getLog() << "Properties: empty imagefile\n";
138         else {
139                 const std::string& rFilename = rIF.getFilename();
140                 rIF.statistics (min, max, mean, mode, median, stddev);
141                 std::ostringstream os;
142                 os << "file: " << rFilename << "\nmin: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmedian: "<<median<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
143                 *theApp->getLog() << os.str().c_str();
144                 wxMessageDialog dialogMsg (m_frame, os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
145                 dialogMsg.ShowModal();
146         }
147 }
148
149 void 
150 ImageFileView::OnScaleAuto (wxCommandEvent& event)
151 {
152     const ImageFile& rIF = GetDocument()->getImageFile();
153     DialogAutoScaleParameters dialogAutoScale (m_frame, rIF, m_dAutoScaleFactor);
154     int iRetVal = dialogAutoScale.ShowModal();
155     if (iRetVal == wxID_OK) {
156                 m_bMinSpecified = true;
157                 m_bMaxSpecified = true;
158                 double dMin, dMax;
159                 dialogAutoScale.getMinMax (&dMin, &dMax);
160                 m_dMinPixel = dMin;
161                 m_dMaxPixel = dMax;
162                 m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
163                 OnUpdate (this, NULL);
164     }
165 }
166
167 void 
168 ImageFileView::OnScaleMinMax (wxCommandEvent& event)
169 {
170     const ImageFile& rIF = GetDocument()->getImageFile();
171     double min, max;
172     if (! m_bMinSpecified && ! m_bMaxSpecified)
173                 rIF.getMinMax (min, max);
174         
175     if (m_bMinSpecified)
176                 min = m_dMinPixel;
177     if (m_bMaxSpecified)
178                 max = m_dMaxPixel;
179         
180     DialogGetImageMinMax dialogMinMax (m_frame, rIF, min, max);
181     int retVal = dialogMinMax.ShowModal();
182     if (retVal == wxID_OK) {
183                 m_bMinSpecified = true;
184                 m_bMaxSpecified = true;
185                 m_dMinPixel = dialogMinMax.getMinimum();
186                 m_dMaxPixel = dialogMinMax.getMaximum();
187                 OnUpdate (this, NULL);
188     }
189 }
190
191
192 ImageFileCanvas* 
193 ImageFileView::CreateCanvas (wxView *view, wxFrame *parent)
194 {
195     ImageFileCanvas* pCanvas;
196     int width, height;
197     parent->GetClientSize(&width, &height);
198     
199     pCanvas = new ImageFileCanvas (dynamic_cast<ImageFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
200     
201     pCanvas->SetScrollbars(20, 20, 50, 50);
202     pCanvas->SetBackgroundColour(*wxWHITE);
203     pCanvas->Clear();
204     
205     return pCanvas;
206 }
207
208 wxFrame*
209 ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
210 {
211     wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
212     
213     wxMenu *file_menu = new wxMenu;
214     
215     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
216     file_menu->Append(wxID_OPEN, "&Open...");
217     file_menu->Append(wxID_SAVE, "&Save");
218     file_menu->Append(wxID_SAVEAS, "Save &As...");
219     file_menu->Append(wxID_CLOSE, "&Close");
220     
221     file_menu->AppendSeparator();
222     file_menu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
223         
224     file_menu->AppendSeparator();
225     file_menu->Append(wxID_PRINT, "&Print...");
226     file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
227     file_menu->Append(wxID_PREVIEW, "Print Pre&view");
228     
229     wxMenu *view_menu = new wxMenu;
230     view_menu->Append(IFMENU_VIEW_SCALE_MINMAX, "Display Scale &Set...");
231     view_menu->Append(IFMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...");
232     
233     wxMenu *help_menu = new wxMenu;
234     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
235     
236     wxMenuBar *menu_bar = new wxMenuBar;
237     
238     menu_bar->Append(file_menu, "&File");
239     menu_bar->Append(view_menu, "&View");
240     menu_bar->Append(help_menu, "&Help");
241     
242     subframe->SetMenuBar(menu_bar);
243     
244     subframe->Centre(wxBOTH);
245     
246     return subframe;
247 }
248
249
250 bool 
251 ImageFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
252 {
253     m_frame = CreateChildFrame(doc, this);
254     SetFrame (m_frame);
255     
256     m_bMinSpecified = false;
257     m_bMaxSpecified = false;
258     m_dAutoScaleFactor = 1.;
259         
260     int width, height;
261     m_frame->GetClientSize (&width, &height);
262     m_frame->SetTitle("ImageFileView");
263     m_canvas = CreateCanvas (this, m_frame);
264         
265     int x, y;  // X requires a forced resize
266     m_frame->GetSize(&x, &y);
267     m_frame->SetSize(-1, -1, x, y);
268         m_frame->SetFocus();\r
269         m_frame->Show(true);
270     Activate(true);
271     
272     return true;
273 }
274
275 void 
276 ImageFileView::OnDraw (wxDC* dc)
277 {
278         if (m_bitmap.Ok())
279                 dc->DrawBitmap(m_bitmap, 0, 0, false);
280 }
281
282
283 void 
284 ImageFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
285 {
286     const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
287     ImageFileArrayConst v = rIF.getArray();
288     int nx = rIF.nx();
289     int ny = rIF.ny();
290     if (v != NULL && nx != 0 && ny != 0) {
291                 if (! m_bMinSpecified || ! m_bMaxSpecified) {
292                         double min, max;
293                         rIF.getMinMax (min, max);
294                         if (! m_bMinSpecified)
295                                 m_dMinPixel = min;
296                         if (! m_bMaxSpecified)
297                                 m_dMaxPixel = max;
298                 }
299                 double scaleWidth = m_dMaxPixel - m_dMinPixel;
300                 
301                 unsigned char* imageData = new unsigned char [nx * ny * 3];
302                 for (int ix = 0; ix < nx; ix++) {
303                         for (int iy = 0; iy < ny; iy++) {
304                                 double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
305                                 int intensity = static_cast<int>(scaleValue + 0.5);
306                                 intensity = clamp (intensity, 0, 255);
307                                 int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
308                                 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
309                         }
310                 }
311                 wxImage image (nx, ny, imageData, true);
312                 m_bitmap = image.ConvertToBitmap();
313                 delete imageData;
314                 int xSize = nx;
315                 int ySize = ny;
316                 xSize = clamp (xSize, 0, 800);
317                 ySize = clamp (ySize, 0, 800);
318                 m_frame->SetClientSize (xSize, ySize);
319                 m_canvas->SetScrollbars(20, 20, nx/20, ny/20);
320                 m_canvas->SetBackgroundColour(*wxWHITE);
321     } 
322         
323     if (m_canvas)
324                 m_canvas->Refresh();
325 }
326
327 bool 
328 ImageFileView::OnClose (bool deleteWindow)
329 {
330     if (!GetDocument()->Close())
331         return false;
332         
333     m_canvas->Clear();
334     m_canvas->m_pView = NULL;
335     m_canvas = NULL;
336     wxString s(theApp->GetAppName());
337     if (m_frame)
338                 m_frame->SetTitle(s);
339     SetFrame(NULL);
340         
341     Activate(false);
342     
343     if (deleteWindow) {
344         delete m_frame;
345         return true;
346     }
347     return true;
348 }
349
350
351
352 // PhantomCanvas
353
354 PhantomCanvas::PhantomCanvas (PhantomView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
355 : wxScrolledWindow(frame, -1, pos, size, style)
356 {
357     m_pView = v;
358 }
359
360 void 
361 PhantomCanvas::OnDraw(wxDC& dc)
362 {
363     if (m_pView)
364         m_pView->OnDraw(& dc);
365 }
366
367
368 // PhantomView
369
370 IMPLEMENT_DYNAMIC_CLASS(PhantomView, wxView)
371
372 BEGIN_EVENT_TABLE(PhantomView, wxView)
373 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomView::OnProperties)
374 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomView::OnRasterize)
375 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomView::OnProjections)
376 END_EVENT_TABLE()
377
378 PhantomView::PhantomView(void) 
379 : wxView(), m_canvas(NULL), m_frame(NULL)
380 {
381     m_iDefaultNDet = 367;
382     m_iDefaultNView = 320;
383     m_iDefaultNSample = 2;
384     m_dDefaultRotation = 2;
385     m_dDefaultFocalLength = 2;
386     m_dDefaultFieldOfView = 1;
387     m_iDefaultGeometry = Scanner::GEOMETRY_PARALLEL;
388     m_iDefaultTrace = Trace::TRACE_NONE;
389 }
390
391 PhantomView::~PhantomView(void)
392 {
393 }
394
395 void
396 PhantomView::OnProperties (wxCommandEvent& event)
397 {
398         const int idPhantom = GetDocument()->getPhantomID();
399         const wxString& namePhantom = GetDocument()->getPhantomName();
400         std::ostringstream os;
401         os << "Phantom " << namePhantom.c_str() << " (" << idPhantom << ")\n";
402         *theApp->getLog() << os.str().c_str();
403 #if DEBUG
404         const Phantom& rPhantom = GetDocument()->getPhantom();
405         rPhantom.print();
406 #endif
407 }
408
409
410 void
411 PhantomView::OnProjections (wxCommandEvent& event)
412 {
413         DialogGetProjectionParameters dialogProjection (m_frame, m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultFieldOfView, m_iDefaultGeometry, m_iDefaultTrace);
414         int retVal = dialogProjection.ShowModal();
415         if (retVal == wxID_OK) {
416                 m_iDefaultNDet = dialogProjection.getNDet();
417                 m_iDefaultNView = dialogProjection.getNView();
418                 m_iDefaultNSample = dialogProjection.getNSamples();
419                 m_iDefaultTrace = dialogProjection.getTrace();
420                 m_dDefaultRotation = dialogProjection.getRotAngle();
421                 m_dDefaultFocalLength = dialogProjection.getFocalLengthRatio();
422                 m_dDefaultFieldOfView = dialogProjection.getFieldOfViewRatio();
423                 wxString sGeometry = dialogProjection.getGeometry();
424                 m_iDefaultGeometry = Scanner::convertGeometryNameToID (sGeometry.c_str());
425                 
426                 if (m_iDefaultNDet > 0 && m_iDefaultNView > 0 && sGeometry != "") {
427                         const Phantom& rPhantom = GetDocument()->getPhantom();
428                         ProjectionFileDocument* pProjectionDoc = dynamic_cast<ProjectionFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.pj", wxDOC_SILENT));
429                         Projections& rProj = pProjectionDoc->getProjections();
430                         Scanner theScanner (rPhantom, sGeometry.c_str(), m_iDefaultNDet, m_iDefaultNView, m_iDefaultNSample, m_dDefaultRotation, m_dDefaultFocalLength, m_dDefaultFieldOfView);
431                         if (theScanner.fail()) {
432                                 *theApp->getLog() << "Failed making scanner: " << theScanner.failMessage().c_str() << "\n";
433                                 return;
434                         }
435                         rProj.initFromScanner (theScanner);
436                         m_dDefaultRotation /= PI;  // convert back to PI units
437                         
438                         if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
439                                 ProjectionsDialog dialogProjections (theScanner, rProj, rPhantom, m_iDefaultTrace, dynamic_cast<wxWindow*>(m_frame));
440                                 for (int iView = 0; iView < rProj.nView(); iView++) {
441                                         ::wxYield();
442                                         ::wxYield();
443                                         if (dialogProjections.isCancelled() || ! dialogProjections.projectView (iView)) {
444                                                 pProjectionDoc->DeleteAllViews();
445                                                 return;
446                                         }
447                                         ::wxYield();
448                                         ::wxYield();
449                                         while (dialogProjections.isPaused()) {
450                                                 ::wxYield();
451                                                 ::wxUsleep(50);
452                                         }
453                                 }
454                         } else {
455                                 wxProgressDialog dlgProgress (wxString("Projection"), wxString("Projection Progress"), rProj.nView() + 1, m_frame, wxPD_CAN_ABORT);
456                                 for (int i = 0; i < rProj.nView(); i++) {
457                                         theScanner.collectProjections (rProj, rPhantom, i, 1, true, m_iDefaultTrace);
458                                         if (! dlgProgress.Update (i+1)) {
459                                                 pProjectionDoc->DeleteAllViews();
460                                                 return;
461                                         }
462                                 }
463                         }
464                         
465                         std::ostringstream os;
466                         os << "Projections for " << rPhantom.name() << ": nDet=" << m_iDefaultNDet << ", nView=" << m_iDefaultNView << ", nSamples=" << m_iDefaultNSample << ", RotAngle=" << m_dDefaultRotation << ", FocalLengthRatio=" << m_dDefaultFocalLength << ", FieldOfViewRatio=" << m_dDefaultFieldOfView << ", Geometry=" << sGeometry.c_str();
467                         rProj.setRemark (os.str());
468                         *theApp->getLog() << os.str().c_str() << "\n";
469                         
470                         m_frame->Lower();
471                         ::wxYield();
472                         ProjectionFileView* projView = dynamic_cast<ProjectionFileView*>(pProjectionDoc->GetFirstView());\r
473                         if (projView) {\r
474                                 projView->getFrame()->SetFocus();\r
475                                 projView->OnUpdate (projView, NULL);\r
476                         }\r
477                         if (wxView* pView = pProjectionDoc->GetFirstView()) {
478                                 if (wxFrame* pFrame = pView->GetFrame()) {
479                                         pFrame->SetFocus();
480                                         pFrame->Raise();
481                                 }
482                                 theApp->getDocManager()->ActivateView (pView, true, false);
483                         }
484                         ::wxYield();
485                         pProjectionDoc->Modify(true);
486                         pProjectionDoc->UpdateAllViews(this);
487                 }
488         }
489 }
490
491
492 void
493 PhantomView::OnRasterize (wxCommandEvent& event)
494 {
495         DialogGetRasterParameters dialogRaster (m_frame, 256, 256, 1);
496         int retVal = dialogRaster.ShowModal();
497         if (retVal == wxID_OK) {
498                 int xSize = dialogRaster.getXSize();
499                 int ySize = dialogRaster.getYSize();
500                 int nSamples = dialogRaster.getNSamples();
501                 if (nSamples < 1)
502                         nSamples = 1;
503                 if (xSize > 0 && ySize > 0) {
504                         const Phantom& rPhantom = GetDocument()->getPhantom();
505                         ImageFileDocument* pRasterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
506                         ImageFile& imageFile = pRasterDoc->getImageFile();
507                         
508                         imageFile.setArraySize (xSize, ySize);
509                         wxProgressDialog dlgProgress (wxString("Rasterize"), wxString("Rasterization Progress"), imageFile.nx() + 1, m_frame, wxPD_CAN_ABORT);
510                         for (unsigned int i = 0; i < imageFile.nx(); i++) {
511                                 rPhantom.convertToImagefile (imageFile, nSamples, Trace::TRACE_NONE, i, 1, true);
512                                 if (! dlgProgress.Update(i+1)) {
513                                         pRasterDoc->DeleteAllViews();
514                                         return;
515                                 }
516                         }
517                         pRasterDoc->Modify(true);
518                         pRasterDoc->UpdateAllViews(this);
519                         ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pRasterDoc->GetFirstView());\r
520                         if (rasterView) {\r
521                                 rasterView->getFrame()->SetFocus();\r
522                                 rasterView->OnUpdate (rasterView, NULL);\r
523                         }\r
524                         
525                         std::ostringstream os;
526                         os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << xSize << ", YSize=" << ySize << ", nSamples=" << nSamples << "\n";
527                         *theApp->getLog() << os.str().c_str();
528                 }\r
529         }
530 }
531
532
533 PhantomCanvas* 
534 PhantomView::CreateCanvas (wxView *view, wxFrame *parent)
535 {
536     PhantomCanvas* pCanvas;
537     int width, height;
538     parent->GetClientSize(&width, &height);
539     
540     pCanvas = new PhantomCanvas (dynamic_cast<PhantomView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
541     
542     pCanvas->SetBackgroundColour(*wxWHITE);
543     pCanvas->Clear();
544     
545     return pCanvas;
546 }
547
548 wxFrame*
549 PhantomView::CreateChildFrame(wxDocument *doc, wxView *view)
550 {
551     wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
552     
553     wxMenu *file_menu = new wxMenu;
554     
555     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
556     file_menu->Append(wxID_OPEN, "&Open...");
557     file_menu->Append(wxID_CLOSE, "&Close");
558     
559     file_menu->AppendSeparator();
560     file_menu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
561         
562     file_menu->AppendSeparator();
563     file_menu->Append(wxID_PRINT, "&Print...");
564     file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
565     file_menu->Append(wxID_PREVIEW, "Print Pre&view");
566     
567     wxMenu *process_menu = new wxMenu;
568     process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...");
569     process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...");
570         
571     wxMenu *help_menu = new wxMenu;
572     help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
573     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
574     
575     wxMenuBar *menu_bar = new wxMenuBar;
576     
577     menu_bar->Append(file_menu, "&File");
578     menu_bar->Append(process_menu, "&Process");
579     menu_bar->Append(help_menu, "&Help");
580     
581     subframe->SetMenuBar(menu_bar);
582     
583     subframe->Centre(wxBOTH);
584     
585     return subframe;
586 }
587
588
589 bool 
590 PhantomView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
591 {
592     m_frame = CreateChildFrame(doc, this);
593     SetFrame(m_frame);
594         
595     int width, height;
596     m_frame->GetClientSize(&width, &height);
597     m_frame->SetTitle("PhantomView");
598     m_canvas = CreateCanvas(this, m_frame);
599         
600 #ifdef __X__
601     int x, y;  // X requires a forced resize
602     m_frame->GetSize(&x, &y);
603     m_frame->SetSize(-1, -1, x, y);
604 #endif
605         
606     m_frame->Show(true);
607     Activate(true);
608         
609     return true;
610 }
611
612
613 void 
614 PhantomView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
615 {
616     if (m_canvas)
617                 m_canvas->Refresh();
618 }
619
620 bool 
621 PhantomView::OnClose (bool deleteWindow)
622 {
623     if (!GetDocument()->Close())
624         return false;
625         
626     m_canvas->Clear();
627     m_canvas->m_pView = NULL;
628     m_canvas = NULL;
629     wxString s(wxTheApp->GetAppName());
630     if (m_frame)
631                 m_frame->SetTitle(s);
632     SetFrame(NULL);
633         
634     Activate(false);
635     
636     if (deleteWindow) {
637         delete m_frame;
638         return true;
639     }
640     return true;
641 }
642
643 void
644 PhantomView::OnDraw (wxDC* dc)
645 {
646         int xsize, ysize;
647         m_canvas->GetClientSize (&xsize, &ysize);
648         SGPDriver driver (dc, xsize, ysize);
649         SGP sgp (driver);
650         const Phantom& rPhantom = GetDocument()->getPhantom();
651         sgp.setColor (C_RED);
652         rPhantom.show (sgp);
653 }
654
655 // ProjectionCanvas
656
657 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
658 : wxScrolledWindow(frame, -1, pos, size, style)
659 {
660     m_pView = v;
661 }
662
663 void 
664 ProjectionFileCanvas::OnDraw(wxDC& dc)
665 {
666     if (m_pView)
667         m_pView->OnDraw(& dc);
668 }
669
670 // ProjectionFileView
671
672 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
673
674 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
675 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
676 EVT_MENU(PJMENU_PROCESS_RECONSTRUCT, ProjectionFileView::OnReconstruct)
677 END_EVENT_TABLE()
678
679 ProjectionFileView::ProjectionFileView(void) 
680 : wxView(), m_canvas(NULL), m_frame(NULL)
681 {
682     m_iDefaultNX = 256;
683     m_iDefaultNY = 256;
684         m_iDefaultFilter = SignalFilter::FILTER_ABS_BANDLIMIT;
685         m_dDefaultFilterParam = 1.;
686 #if HAVE_FFTW
687         m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_RFFTW;
688         m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_INVERSE_FOURIER;
689 #else
690         m_iDefaultFilterMethod = ProcessSignal::FILTER_METHOD_CONVOLUTION;
691         m_iDefaultFilterGeneration = ProcessSignal::FILTER_GENERATION_DIRECT;
692 #endif
693         m_iDefaultZeropad = 1;
694         m_iDefaultBackprojector = Backprojector::BPROJ_IDIFF3;
695         m_iDefaultInterpolation = Backprojector::INTERP_LINEAR;
696         m_iDefaultInterpParam = 1;
697         m_iDefaultTrace = Trace::TRACE_NONE;
698 }
699
700 ProjectionFileView::~ProjectionFileView(void)
701 {
702 }
703
704 void
705 ProjectionFileView::OnProperties (wxCommandEvent& event)
706 {
707         const Projections& rProj = GetDocument()->getProjections();
708         std::ostringstream os;
709         rProj.printScanInfo(os);
710         *theApp->getLog() << os.str().c_str();
711         wxMessageDialog dialogMsg (m_frame, os.str().c_str(), "Projection File Properties", wxOK | wxICON_INFORMATION);
712         dialogMsg.ShowModal();
713 }
714
715
716 void
717 ProjectionFileView::OnReconstruct (wxCommandEvent& event)
718 {
719         DialogGetReconstructionParameters dialogReconstruction (m_frame, m_iDefaultNX, m_iDefaultNY, m_iDefaultFilter, m_dDefaultFilterParam, m_iDefaultFilterMethod, m_iDefaultFilterGeneration, m_iDefaultZeropad, m_iDefaultInterpolation, m_iDefaultInterpParam, m_iDefaultBackprojector, m_iDefaultTrace);
720         
721         int retVal = dialogReconstruction.ShowModal();
722         if (retVal == wxID_OK) {
723                 m_iDefaultNX = dialogReconstruction.getXSize();
724                 m_iDefaultNY = dialogReconstruction.getYSize();
725                 wxString optFilterName = dialogReconstruction.getFilterName();
726                 m_iDefaultFilter = SignalFilter::convertFilterNameToID (optFilterName.c_str());
727                 m_dDefaultFilterParam = dialogReconstruction.getFilterParam();
728                 wxString optFilterMethodName = dialogReconstruction.getFilterMethodName();
729                 m_iDefaultFilterMethod = ProcessSignal::convertFilterMethodNameToID(optFilterMethodName.c_str());
730                 m_iDefaultZeropad = dialogReconstruction.getZeropad();
731                 wxString optFilterGenerationName = dialogReconstruction.getFilterGenerationName();
732                 m_iDefaultFilterGeneration = ProcessSignal::convertFilterGenerationNameToID (optFilterGenerationName.c_str());
733                 wxString optInterpName = dialogReconstruction.getInterpName();
734                 m_iDefaultInterpolation = Backprojector::convertInterpNameToID (optInterpName.c_str());
735                 m_iDefaultInterpParam = dialogReconstruction.getInterpParam();
736                 wxString optBackprojectName = dialogReconstruction.getBackprojectName();
737                 m_iDefaultBackprojector = Backprojector::convertBackprojectNameToID (optBackprojectName.c_str());
738                 m_iDefaultTrace = dialogReconstruction.getTrace();
739                 if (m_iDefaultNX > 0 && m_iDefaultNY > 0) {
740                         ImageFileDocument* pReconDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
741                         ImageFile& imageFile = pReconDoc->getImageFile();
742                         const Projections& rProj = GetDocument()->getProjections();
743                         imageFile.setArraySize (m_iDefaultNX, m_iDefaultNY);
744                         
745                         if (m_iDefaultFilterMethod != ProcessSignal::FILTER_METHOD_CONVOLUTION && m_iDefaultFilterGeneration == ProcessSignal::FILTER_GENERATION_DIRECT && rProj.geometry() != Scanner::GEOMETRY_PARALLEL) {
746                                 wxMessageBox ("Sorry!\nCurrently, frequency-based filtering with direct filter generation is not support for geometries other than parallel.\nAborting command.", "Not Supported", wxOK | wxICON_WARNING, m_frame);
747                                 return;
748                         }
749 #if 0
750                         SGPDriver* pSGPDriver = NULL;
751                         SGP* pSGP = NULL;
752                         wxMemoryDC* pDCPlot = NULL;
753                         wxBitmap bitmap;
754                         if (m_iDefaultTrace >= Trace::TRACE_PLOT) {
755                                 bitmap.Create (500, 500);
756                                 pDCPlot = new wxMemoryDC;
757                                 pDCPlot->SelectObject (bitmap);
758                                 pSGPDriver = new SGPDriver (dynamic_cast<wxDC*>pDCPlot, 500, 500);
759                                 pSGP = new SGP (*pSGPDriver);
760                         }
761                         Reconstructor* pReconstruct = new Reconstructor (rProj, imageFile, optFilterName.c_str(), m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace, pSGP);
762                         delete pSGP;
763 #else
764                         Reconstructor* pReconstruct = new Reconstructor (rProj, imageFile, optFilterName.c_str(), m_dDefaultFilterParam, optFilterMethodName.c_str(), m_iDefaultZeropad, optFilterGenerationName.c_str(), optInterpName.c_str(), m_iDefaultInterpParam, optBackprojectName.c_str(), m_iDefaultTrace);
765 #endif
766                         \r
767                         Timer timerRecon;
768                         if (m_iDefaultTrace > Trace::TRACE_CONSOLE) {
769                                 ReconstructDialog* pDlgReconstruct = new ReconstructDialog (*pReconstruct, rProj, imageFile, m_iDefaultTrace, m_frame);
770                                 for (int iView = 0; iView < rProj.nView(); iView++) {
771                                         ::wxYield();
772                                         ::wxYield();
773                                         if (pDlgReconstruct->isCancelled() || ! pDlgReconstruct->reconstructView (iView)) {
774                                                 delete pDlgReconstruct;
775                                                 delete pReconstruct;
776                                                 pReconDoc->DeleteAllViews();
777                                                 return;
778                                         }
779                                         ::wxYield();
780                                         ::wxYield();
781                                         while (pDlgReconstruct->isPaused()) {
782                                                 ::wxYield();
783                                                 ::wxUsleep(50);
784                                         }
785                                 }
786                                 delete pDlgReconstruct;
787                         } else {
788                                 wxProgressDialog dlgProgress (wxString("Reconstruction"), wxString("Reconstruction Progress"), rProj.nView() + 1, m_frame, wxPD_CAN_ABORT);
789                                 for (int i = 0; i < rProj.nView(); i++) {
790                                         pReconstruct->reconstructView (i, 1);
791                                         if (! dlgProgress.Update(i + 1)) {
792                                                 delete pReconstruct;
793                                                 pReconDoc->DeleteAllViews();
794                                                 return;
795                                         }
796                                 }
797                         }
798                         delete pReconstruct;
799                         pReconDoc->Modify(true);
800                         pReconDoc->UpdateAllViews(this);
801                         ImageFileView* rasterView = dynamic_cast<ImageFileView*>(pReconDoc->GetFirstView());\r
802                         if (rasterView) {\r
803                                 rasterView->getFrame()->SetFocus();\r
804                                 rasterView->OnUpdate (rasterView, NULL);\r
805                         }\r
806                         std::ostringstream os;
807                         os << "Reconstruct " << rProj.getFilename() << ": xSize=" << m_iDefaultNX << ", ySize=" << m_iDefaultNY << ", Filter=" << optFilterName.c_str() << ", FilterParam=" << m_dDefaultFilterParam << ", FilterMethod=" << optFilterMethodName.c_str() << ", FilterGeneration=" << optFilterGenerationName.c_str() << ", Zeropad=" << m_iDefaultZeropad << ", Interpolation=" << optInterpName.c_str() << ", InterpolationParam=" << m_iDefaultInterpParam << ", Backprojection=" << optBackprojectName.c_str();
808                         *theApp->getLog() << os.str().c_str() << "\n";
809                         imageFile.labelAdd (rProj.getLabel());
810                         imageFile.labelAdd (Array2dFileLabel::L_HISTORY, os.str().c_str(), timerRecon.timerEnd());
811                 }
812         }
813 }
814
815
816 ProjectionFileCanvas* 
817 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
818 {
819     ProjectionFileCanvas* pCanvas;
820     int width, height;
821     parent->GetClientSize(&width, &height);
822     
823     pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
824     
825     pCanvas->SetScrollbars(20, 20, 50, 50);
826     pCanvas->SetBackgroundColour(*wxWHITE);
827     pCanvas->Clear();
828     
829     return pCanvas;
830 }
831
832 wxFrame*
833 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
834 {
835     wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
836     
837     wxMenu *file_menu = new wxMenu;
838     
839     file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
840     file_menu->Append(wxID_OPEN, "&Open...");
841     file_menu->Append(wxID_SAVE, "&Save");
842     file_menu->Append(wxID_SAVEAS, "Save &As...");
843     file_menu->Append(wxID_CLOSE, "&Close");
844     
845     file_menu->AppendSeparator();
846     file_menu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
847         
848     file_menu->AppendSeparator();
849     file_menu->Append(wxID_PRINT, "&Print...");
850     file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
851     file_menu->Append(wxID_PREVIEW, "Print Pre&view");
852     
853     wxMenu *process_menu = new wxMenu;
854     process_menu->Append(PJMENU_PROCESS_RECONSTRUCT, "R&econstruct...");
855         
856     wxMenu *help_menu = new wxMenu;
857     help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
858     help_menu->AppendSeparator();
859     help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
860     
861     wxMenuBar *menu_bar = new wxMenuBar;
862     
863     menu_bar->Append(file_menu, "&File");
864     menu_bar->Append(process_menu, "&Process");
865     menu_bar->Append(help_menu, "&Help");
866     
867     subframe->SetMenuBar(menu_bar);
868     
869     subframe->Centre(wxBOTH);
870     
871     return subframe;
872 }
873
874
875 bool 
876 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
877 {
878     m_frame = CreateChildFrame(doc, this);
879     SetFrame(m_frame);
880     
881     int width, height;
882     m_frame->GetClientSize(&width, &height);
883     m_frame->SetTitle("ProjectionFileView");
884     m_canvas = CreateCanvas(this, m_frame);
885         
886 #ifdef __X__
887     int x, y;  // X requires a forced resize
888     m_frame->GetSize(&x, &y);
889     m_frame->SetSize(-1, -1, x, y);
890 #endif
891         
892     m_frame->Show(true);
893     Activate(true);
894     
895     return true;
896 }
897
898 void 
899 ProjectionFileView::OnDraw (wxDC* dc)
900 {
901     if (m_bitmap.Ok())
902                 dc->DrawBitmap (m_bitmap, 0, 0, false);
903 }
904
905
906 void 
907 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
908 {
909     const Projections& rProj = GetDocument()->getProjections();
910     const int nDet = rProj.nDet();
911     const int nView = rProj.nView();
912     if (nDet != 0 && nView != 0) {
913                 const DetectorArray& detarray = rProj.getDetectorArray(0);
914                 const DetectorValue* detval = detarray.detValues();
915                 double min = detval[0];
916                 double max = detval[0];
917                 for (int iy = 0; iy < nView; iy++) {
918                         const DetectorArray& detarray = rProj.getDetectorArray(iy);
919                         const DetectorValue* detval = detarray.detValues();
920                         for (int ix = 0; ix < nDet; ix++) {
921                                 if (min > detval[ix])
922                                         min = detval[ix];
923                                 else if (max < detval[ix])
924                                         max = detval[ix];
925                         }
926                 }
927                 
928                 unsigned char* imageData = new unsigned char [nDet * nView * 3];
929                 double scale = (max - min) / 255;
930                 for (int iy2 = 0; iy2 < nView; iy2++) {
931                         const DetectorArray& detarray = rProj.getDetectorArray (iy2);
932                         const DetectorValue* detval = detarray.detValues();
933                         for (int ix = 0; ix < nDet; ix++) {
934                                 int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
935                                 intensity = clamp(intensity, 0, 255);
936                                 int baseAddr = (iy2 * nDet + ix) * 3;
937                                 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
938                         }
939                 }
940                 wxImage image (nDet, nView, imageData, true);
941                 m_bitmap = image.ConvertToBitmap();
942                 delete imageData;
943                 int xSize = nDet;
944                 int ySize = nView;
945                 xSize = clamp (xSize, 0, 800);
946                 ySize = clamp (ySize, 0, 800);
947                 m_frame->SetClientSize (xSize, ySize);
948                 m_canvas->SetScrollbars (20, 20, nDet/20, nView/20);
949     }
950         
951     if (m_canvas)
952                 m_canvas->Refresh();
953 }
954
955 bool 
956 ProjectionFileView::OnClose (bool deleteWindow)
957 {
958     if (!GetDocument()->Close())
959         return false;
960         
961     m_canvas->Clear();
962     m_canvas->m_pView = NULL;
963     m_canvas = NULL;
964     wxString s(wxTheApp->GetAppName());
965     if (m_frame)
966                 m_frame->SetTitle(s);
967     SetFrame(NULL);
968         
969     Activate(false);
970     
971     if (deleteWindow) {
972         delete m_frame;
973         return true;
974     }
975     return true;
976 }
977