1 /*****************************************************************************
5 ** Purpose: View & Canvas routines for CTSim program
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: July 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: views.cpp,v 1.4 2000/07/18 14:51:06 kevin Exp $
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.
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.
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 ******************************************************************************/
29 // #pragma implementation
32 // For compilers that support precompilation, includes "wx/wx.h".
33 #include "wx/wxprec.h"
43 #if !wxUSE_DOC_VIEW_ARCHITECTURE
44 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
58 BEGIN_EVENT_TABLE(ImageFileCanvas, wxScrolledWindow)
59 EVT_MOUSE_EVENTS(ImageFileCanvas::OnMouseEvent)
63 ImageFileCanvas::ImageFileCanvas (ImageFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
64 : wxScrolledWindow(frame, -1, pos, size, style)
70 ImageFileCanvas::OnDraw(wxDC& dc)
73 m_pView->OnDraw(& dc);
77 ImageFileCanvas::OnMouseEvent(wxMouseEvent& event)
85 wxPoint pt(event.GetLogicalPosition(dc));
87 if (event.LeftIsDown()) {
88 const ImageFile& rIF = m_pView->GetDocument()->getImageFile();
89 ImageFileArrayConst v = rIF.getArray();
93 if (pt.x >= 0 && pt.x < nx && pt.y >= 0 & pt.y < ny) {
95 os << "Image value (" << pt.x << "," << pt.y << ") = " << v[pt.x][pt.y] << "\n";
96 *theApp->getLog() << os.str().c_str();
98 *theApp->getLog() << "Mouse out of image range (" << pt.x << "," << pt.y << ")\n";
106 IMPLEMENT_DYNAMIC_CLASS(ImageFileView, wxView)
108 BEGIN_EVENT_TABLE(ImageFileView, wxView)
109 EVT_MENU(IFMENU_FILE_PROPERTIES, ImageFileView::OnProperties)
110 EVT_MENU(IFMENU_VIEW_WINDOW_MINMAX, ImageFileView::OnWindowMinMax)
111 EVT_MENU(IFMENU_VIEW_WINDOW_AUTO, ImageFileView::OnWindowAuto)
114 ImageFileView::ImageFileView(void)
115 : wxView(), m_canvas(NULL), m_frame(NULL), m_bMinSpecified(false), m_bMaxSpecified(false)
119 ImageFileView::~ImageFileView(void)
124 ImageFileView::OnProperties (wxCommandEvent& event)
126 double min, max, mean, mode, median, stddev;
127 const ImageFile& rIF = GetDocument()->getImageFile();
128 if (rIF.nx() == 0 || rIF.ny() == 0)
129 *theApp->getLog() << "Properties: empty imagefile\n";
131 const string& rFilename = rIF.getFilename();
132 rIF.statistics (min, max, mean, mode, median, stddev);
134 os << "file: " << rFilename << "\nmin: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
135 *theApp->getLog() << os.str().c_str();
138 ostringstream osSize;
139 m_frame->GetSize (&xSize, &ySize);
140 osSize << "Frame size: (" << xSize << "," << ySize << ")\n";
141 m_frame->GetClientSize (&xSize, &ySize);
142 osSize << "Frame Client size: (" << xSize << "," << ySize << ")\n";
143 m_canvas->GetSize (&xSize, &ySize);
144 osSize << "Canvas size: (" << xSize << "," << ySize << ")\n";
145 *theApp->getLog() << osSize.str().c_str();
150 ImageFileView::OnWindowAuto (wxCommandEvent& event)
152 *theApp->getLog() << "ImageFile: Window Auto\n";
156 ImageFileView::OnWindowMinMax (wxCommandEvent& event)
158 const ImageFile& rIF = GetDocument()->getImageFile();
160 if (! m_bMinSpecified && ! m_bMaxSpecified)
161 rIF.getMinMax (min, max);
168 DialogGetImageMinMax dialogMinMax (m_frame, rIF, min, max);
169 int retVal = dialogMinMax.ShowModal();
170 if (retVal == wxID_OK) {
171 m_bMinSpecified = true;
172 m_bMaxSpecified = true;
173 m_dMinPixel = dialogMinMax.getMinimum();
174 m_dMaxPixel = dialogMinMax.getMaximum();
175 OnUpdate (this, NULL);
181 ImageFileView::CreateCanvas (wxView *view, wxFrame *parent)
183 ImageFileCanvas* pCanvas;
185 parent->GetClientSize(&width, &height);
187 pCanvas = new ImageFileCanvas (dynamic_cast<ImageFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
189 pCanvas->SetScrollbars(20, 20, 50, 50);
190 pCanvas->SetBackgroundColour(*wxWHITE);
197 ImageFileView::CreateChildFrame(wxDocument *doc, wxView *view)
199 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "ImageFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
201 wxMenu *file_menu = new wxMenu;
203 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "&Create Phantom...");
204 file_menu->Append(wxID_OPEN, "&Open...");
205 file_menu->Append(wxID_CLOSE, "&Close");
206 file_menu->Append(wxID_SAVE, "&Save");
207 file_menu->Append(wxID_SAVEAS, "Save &As...");
209 file_menu->AppendSeparator();
210 file_menu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
212 file_menu->AppendSeparator();
213 file_menu->Append(wxID_PRINT, "&Print...");
214 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
215 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
217 wxMenu *view_menu = new wxMenu;
218 view_menu->Append(IFMENU_VIEW_WINDOW_MINMAX, "&Set Window Min/Max");
219 view_menu->Append(IFMENU_VIEW_WINDOW_AUTO, "&Auto Window...");
221 wxMenu *help_menu = new wxMenu;
222 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
224 wxMenuBar *menu_bar = new wxMenuBar;
226 menu_bar->Append(file_menu, "&File");
227 menu_bar->Append(view_menu, "&View");
228 menu_bar->Append(help_menu, "&Help");
230 subframe->SetMenuBar(menu_bar);
232 subframe->Centre(wxBOTH);
239 ImageFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
241 m_frame = CreateChildFrame(doc, this);
243 m_bMinSpecified = false;
244 m_bMaxSpecified = false;
247 m_frame->GetClientSize(&width, &height);
248 m_frame->SetTitle("ImageFileView");
249 m_canvas = CreateCanvas(this, m_frame);
252 int x, y; // X requires a forced resize
253 m_frame->GetSize(&x, &y);
254 m_frame->SetSize(-1, -1, x, y);
264 ImageFileView::OnDraw (wxDC* dc)
267 dc->DrawBitmap(m_bitmap, 0, 0, false);
272 ImageFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
274 const ImageFile& rIF = dynamic_cast<ImageFileDocument*>(GetDocument())->getImageFile();
275 ImageFileArrayConst v = rIF.getArray();
278 if (v != NULL && nx != 0 && ny != 0) {
279 if (! m_bMinSpecified || ! m_bMaxSpecified) {
281 rIF.getMinMax (min, max);
282 if (! m_bMinSpecified)
284 if (! m_bMaxSpecified)
287 double scaleWidth = m_dMaxPixel - m_dMinPixel;
289 unsigned char* imageData = new unsigned char [nx * ny * 3];
290 for (int ix = 0; ix < nx; ix++) {
291 for (int iy = 0; iy < ny; iy++) {
292 double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
293 int intensity = static_cast<int>(scaleValue + 0.5);
294 intensity = clamp (intensity, 0, 255);
295 int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
296 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
299 wxImage image (ny, nx, imageData, true);
300 m_bitmap = image.ConvertToBitmap();
304 xSize = clamp (xSize, 0, 800);
305 ySize = clamp (ySize, 0, 800);
306 m_frame->SetClientSize (xSize, ySize);
307 m_canvas->SetScrollbars(20, 20, nx/20, ny/20);
308 m_canvas->SetBackgroundColour(*wxWHITE);
316 ImageFileView::OnClose (bool deleteWindow)
318 if (!GetDocument()->Close())
322 m_canvas->m_pView = NULL;
324 wxString s(theApp->GetAppName());
326 m_frame->SetTitle(s);
342 PhantomCanvas::PhantomCanvas (PhantomView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
343 : wxPanel(frame, -1, pos, size, style)
349 PhantomCanvas::OnDraw(wxDC& dc)
352 m_pView->OnDraw(& dc);
357 IMPLEMENT_DYNAMIC_CLASS(PhantomView, wxView)
359 BEGIN_EVENT_TABLE(PhantomView, wxView)
360 EVT_MENU(PHMMENU_FILE_PROPERTIES, PhantomView::OnProperties)
361 EVT_MENU(PHMMENU_PROCESS_RASTERIZE, PhantomView::OnRasterize)
362 EVT_MENU(PHMMENU_PROCESS_PROJECTIONS, PhantomView::OnProjections)
365 PhantomView::PhantomView(void)
366 : wxView(), m_canvas(NULL), m_frame(NULL)
370 PhantomView::~PhantomView(void)
375 PhantomView::OnProperties (wxCommandEvent& event)
377 const Phantom::PhantomID idPhantom = GetDocument()->getPhantomID();
378 const string& namePhantom = GetDocument()->getPhantomName();
380 os << "Phantom " << namePhantom << " (" << static_cast<int>(idPhantom) << ")\n";
381 *theApp->getLog() << os.str().c_str();
383 const Phantom& rPhantom = GetDocument()->getPhantom();
390 PhantomView::OnProjections (wxCommandEvent& event)
392 DialogGetProjectionParameters dialogProjection (m_frame, 367, 320, 1, 1., Scanner::GEOMETRY_PARALLEL_STR);
393 int retVal = dialogProjection.ShowModal();
394 if (retVal == wxID_OK) {
395 int nDet = dialogProjection.getNDet();
396 int nView = dialogProjection.getNView();
397 int nSamples = dialogProjection.getNSamples();
398 double dRotAngle = dialogProjection.getRotAngle();
399 string sGeometry = dialogProjection.getGeometry();
400 if (nDet > 0 && nView > 0 && sGeometry != "") {
401 const Phantom& rPhantom = GetDocument()->getPhantom();
402 ProjectionFileDocument* pProjectionDoc = dynamic_cast<ProjectionFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.pj", wxDOC_SILENT));
403 Projections& rProj = pProjectionDoc->getProjections();
404 Scanner theScanner (rPhantom, sGeometry.c_str(), nDet, nView, nSamples, dRotAngle);
405 rProj.initFromScanner (theScanner);
406 theScanner.collectProjections (rProj, rPhantom, 0, TRACE_NONE);
407 pProjectionDoc->Modify(true);
408 pProjectionDoc->UpdateAllViews(this);
415 PhantomView::OnRasterize (wxCommandEvent& event)
417 DialogGetRasterParameters dialogRaster (m_frame, 256, 256, 1);
418 int retVal = dialogRaster.ShowModal();
419 if (retVal == wxID_OK) {
420 int xSize = dialogRaster.getXSize();
421 int ySize = dialogRaster.getYSize();
422 int nSamples = dialogRaster.getNSamples();
425 if (xSize > 0 && ySize > 0) {
426 const Phantom& rPhantom = GetDocument()->getPhantom();
427 ImageFileDocument* pRasterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
428 ImageFile& imageFile = pRasterDoc->getImageFile();
430 imageFile.setArraySize (xSize, ySize);
431 rPhantom.convertToImagefile (imageFile, nSamples, TRACE_NONE);
432 pRasterDoc->Modify(true);
433 pRasterDoc->UpdateAllViews(this);
436 os << "Rasterize Phantom " << rPhantom.name() << ": XSize=" << xSize << ", YSize=" << ySize << ", nSamples=" << nSamples << "\n";
437 *theApp->getLog() << os.str().c_str();
444 PhantomView::CreateCanvas (wxView *view, wxFrame *parent)
446 PhantomCanvas* pCanvas;
448 parent->GetClientSize(&width, &height);
450 pCanvas = new PhantomCanvas (dynamic_cast<PhantomView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
452 pCanvas->SetBackgroundColour(*wxWHITE);
459 PhantomView::CreateChildFrame(wxDocument *doc, wxView *view)
461 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Phantom Frame", wxPoint(10, 10), wxSize(256, 256), wxDEFAULT_FRAME_STYLE);
463 wxMenu *file_menu = new wxMenu;
465 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "&Create Phantom...");
466 file_menu->Append(wxID_OPEN, "&Open...");
467 file_menu->Append(wxID_CLOSE, "&Close");
469 file_menu->AppendSeparator();
470 file_menu->Append(PHMMENU_FILE_PROPERTIES, "P&roperties");
472 file_menu->AppendSeparator();
473 file_menu->Append(wxID_PRINT, "&Print...");
474 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
475 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
477 wxMenu *process_menu = new wxMenu;
478 process_menu->Append(PHMMENU_PROCESS_RASTERIZE, "&Rasterize...");
479 process_menu->Append(PHMMENU_PROCESS_PROJECTIONS, "&Projections...");
481 wxMenu *help_menu = new wxMenu;
482 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
483 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
485 wxMenuBar *menu_bar = new wxMenuBar;
487 menu_bar->Append(file_menu, "&File");
488 menu_bar->Append(process_menu, "&Process");
489 menu_bar->Append(help_menu, "&Help");
491 subframe->SetMenuBar(menu_bar);
493 subframe->Centre(wxBOTH);
500 PhantomView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
502 m_frame = CreateChildFrame(doc, this);
505 m_frame->GetClientSize(&width, &height);
506 m_frame->SetTitle("PhantomView");
507 m_canvas = CreateCanvas(this, m_frame);
510 int x, y; // X requires a forced resize
511 m_frame->GetSize(&x, &y);
512 m_frame->SetSize(-1, -1, x, y);
522 PhantomView::OnDraw (wxDC* dc)
524 // if (m_bitmap.Ok())
525 // dc->DrawBitmap (m_bitmap, 0, 0, false);
530 PhantomView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
537 PhantomView::OnClose (bool deleteWindow)
539 if (!GetDocument()->Close())
543 m_canvas->m_pView = NULL;
545 wxString s(wxTheApp->GetAppName());
547 m_frame->SetTitle(s);
562 ProjectionFileCanvas::ProjectionFileCanvas (ProjectionFileView* v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style)
563 : wxScrolledWindow(frame, -1, pos, size, style)
569 ProjectionFileCanvas::OnDraw(wxDC& dc)
572 m_pView->OnDraw(& dc);
575 // ProjectionFileView
577 IMPLEMENT_DYNAMIC_CLASS(ProjectionFileView, wxView)
579 BEGIN_EVENT_TABLE(ProjectionFileView, wxView)
580 EVT_MENU(PJMENU_FILE_PROPERTIES, ProjectionFileView::OnProperties)
581 EVT_MENU(PJMENU_PROCESS_RECONSTRUCT, ProjectionFileView::OnReconstruct)
584 ProjectionFileView::ProjectionFileView(void)
585 : wxView(), m_canvas(NULL), m_frame(NULL)
589 ProjectionFileView::~ProjectionFileView(void)
594 ProjectionFileView::OnProperties (wxCommandEvent& event)
596 const Projections& rProj = GetDocument()->getProjections();
598 os << "ProjectionFile " << rProj.getFilename() << ": Number of Detectors = " << rProj.nDet() << ", Number of Views = " << rProj.nView() << "\n";
599 *theApp->getLog() << os.str().c_str();
604 ProjectionFileView::OnReconstruct (wxCommandEvent& event)
606 ImageFileDocument* pReconDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument("untitled.if", wxDOC_SILENT));
607 ImageFile& imageFile = pReconDoc->getImageFile();
608 const Projections& rProj = GetDocument()->getProjections();
609 string optFilterName = "abs_bandlimit";
610 double optFilterParam = 1.;
611 string optFilterMethodName = "convolution";
613 string optInterpName = "linear";
614 int optInterpParam = 1;
615 string optBackprojectName = "idiff3";
616 imageFile.setArraySize (256, 256);
617 rProj.reconstruct (imageFile, optFilterName.c_str(), optFilterParam, optFilterMethodName.c_str(), optZeropad, optInterpName.c_str(), optInterpParam, optBackprojectName.c_str(), TRACE_NONE);
618 pReconDoc->Modify(true);
619 pReconDoc->UpdateAllViews(this);
623 ProjectionFileCanvas*
624 ProjectionFileView::CreateCanvas (wxView *view, wxFrame *parent)
626 ProjectionFileCanvas* pCanvas;
628 parent->GetClientSize(&width, &height);
630 pCanvas = new ProjectionFileCanvas (dynamic_cast<ProjectionFileView*>(view), parent, wxPoint(0, 0), wxSize(width, height), 0);
632 pCanvas->SetScrollbars(20, 20, 50, 50);
633 pCanvas->SetBackgroundColour(*wxWHITE);
640 ProjectionFileView::CreateChildFrame(wxDocument *doc, wxView *view)
642 wxDocChildFrame *subframe = new wxDocChildFrame(doc, view, theApp->getMainFrame(), -1, "Projection Frame", wxPoint(10, 10), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
644 wxMenu *file_menu = new wxMenu;
646 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "&Create Phantom...");
647 file_menu->Append(wxID_OPEN, "&Open...");
648 file_menu->Append(wxID_CLOSE, "&Close");
650 file_menu->AppendSeparator();
651 file_menu->Append(PJMENU_FILE_PROPERTIES, "P&roperties");
653 file_menu->AppendSeparator();
654 file_menu->Append(wxID_PRINT, "&Print...");
655 file_menu->Append(wxID_PRINT_SETUP, "Print &Setup...");
656 file_menu->Append(wxID_PREVIEW, "Print Pre&view");
658 wxMenu *process_menu = new wxMenu;
659 process_menu->Append(PJMENU_PROCESS_RECONSTRUCT, "R&econstruct...");
661 wxMenu *help_menu = new wxMenu;
662 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
663 help_menu->AppendSeparator();
664 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
666 wxMenuBar *menu_bar = new wxMenuBar;
668 menu_bar->Append(file_menu, "&File");
669 menu_bar->Append(process_menu, "&Process");
670 menu_bar->Append(help_menu, "&Help");
672 subframe->SetMenuBar(menu_bar);
674 subframe->Centre(wxBOTH);
681 ProjectionFileView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
683 m_frame = CreateChildFrame(doc, this);
686 m_frame->GetClientSize(&width, &height);
687 m_frame->SetTitle("ProjectionFileView");
688 m_canvas = CreateCanvas(this, m_frame);
691 int x, y; // X requires a forced resize
692 m_frame->GetSize(&x, &y);
693 m_frame->SetSize(-1, -1, x, y);
703 ProjectionFileView::OnDraw (wxDC* dc)
706 dc->DrawBitmap (m_bitmap, 0, 0, false);
711 ProjectionFileView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
713 const Projections& rProj = GetDocument()->getProjections();
714 const int nDet = rProj.nDet();
715 const int nView = rProj.nView();
716 if (nDet != 0 && nView != 0) {
717 const DetectorArray& detarray = rProj.getDetectorArray(0);
718 const DetectorValue* detval = detarray.detValues();
719 double min = detval[0];
720 double max = detval[0];
721 for (int iy = 0; iy < nView; iy++) {
722 const DetectorArray& detarray = rProj.getDetectorArray(iy);
723 const DetectorValue* detval = detarray.detValues();
724 for (int ix = 0; ix < nDet; ix++) {
725 if (min > detval[ix])
727 else if (max < detval[ix])
732 unsigned char* imageData = new unsigned char [nDet * nView * 3];
733 double scale = (max - min) / 255;
734 for (int iy = 0; iy < nView; iy++) {
735 const DetectorArray& detarray = rProj.getDetectorArray(iy);
736 const DetectorValue* detval = detarray.detValues();
737 for (int ix = 0; ix < nDet; ix++) {
738 int intensity = static_cast<int>(((detval[ix] - min) / scale) + 0.5);
739 intensity = clamp(intensity, 0, 255);
740 int baseAddr = (iy * nDet + ix) * 3;
741 imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
744 wxImage image (nDet, nView, imageData, true);
745 m_bitmap = image.ConvertToBitmap();
749 xSize = clamp (xSize, 0, 800);
750 ySize = clamp (ySize, 0, 800);
751 m_frame->SetClientSize (xSize, ySize);
752 m_canvas->SetScrollbars (20, 20, nDet/20, nView/20);
760 ProjectionFileView::OnClose (bool deleteWindow)
762 if (!GetDocument()->Close())
766 m_canvas->m_pView = NULL;
768 wxString s(wxTheApp->GetAppName());
770 m_frame->SetTitle(s);