1 /*****************************************************************************
5 ** Purpose: Top-level routines of 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: ctsim.cpp,v 1.28 2001/01/13 03:51:35 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 // For compilers that support precompilation, includes "wx/wx.h".
30 #include "wx/wxprec.h"
40 #if !wxUSE_DOC_VIEW_ARCHITECTURE
41 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
50 #if defined(HAVE_CONFIG_H)
54 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
64 static const char* rcsindent = "$Id: ctsim.cpp,v 1.28 2001/01/13 03:51:35 kevin Exp $";
66 struct option CTSimApp::ctsimOptions[] =
68 {"help", 0, 0, O_HELP},
69 {"version", 0, 0, O_VERSION},
73 IMPLEMENT_APP(CTSimApp)
76 : m_docManager(NULL), m_pFrame(NULL)
81 #ifdef HAVE_SYS_TIME_H
85 #ifdef HAVE_SYS_RESOURCE_H
86 #include <sys/resource.h>
92 #ifdef HAVE_SETPRIORITY
93 setpriority (PRIO_PROCESS, 0, 15); // set to low scheduling priority
96 g_bRunningWXWindows = true;
99 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
105 std::cout << rcsindent << std::endl;
107 std::cout << "Version: CTSIMVERSION" << std::endl;
108 #elif defined(VERSION)
109 std::cout << "Version: VERSION" << std::endl;
122 m_docManager = new wxDocManager;
124 new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
126 new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
128 new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
130 new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
132 //// Create the main frame window
133 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
135 SetTopWindow (m_pFrame);
136 m_pFrame->Centre(wxBOTH);
138 m_pFrame->Show(true);
140 for (int i = optind + 1; i <= argc; i++) {
141 wxString filename = argv [i - 1];
142 m_docManager->CreateDocument (filename, wxDOC_SILENT);
149 CTSimApp::usage(const char* program)
151 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
152 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
154 std::cout << " --version Display version\n";
155 std::cout << " --help Display this help message\n";
169 CTSimApp::getUntitledFilename()
171 static int untitledNumber = 1;
173 wxString filename ("Untitled");
174 filename << untitledNumber++;
180 // Top-level window for CTSim
183 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
185 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
187 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
189 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
192 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
193 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
194 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
195 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
196 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
197 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
198 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
199 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
200 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
201 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
202 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
203 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
204 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
205 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
206 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
207 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
208 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
209 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
210 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
211 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
212 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
213 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
214 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
215 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
216 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
217 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
223 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
224 : wxMDIParentFrame(NULL, id, title, pos, size, type), m_pLog(NULL)
226 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
227 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
230 m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
231 wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
233 SetStatusText ("Welcome to CTSim");
236 wxMenu *file_menu = new wxMenu;
238 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
239 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
240 file_menu->Append(wxID_OPEN, "&Open...");
242 file_menu->AppendSeparator();
243 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
245 // history of files visited
246 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
248 m_pWindowMenu = new wxMenu;
249 m_pWindowMenu->UpdateUI (this);
251 wxMenu* help_menu = new wxMenu;
252 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
253 help_menu->AppendSeparator();
254 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
256 wxMenuBar* menu_bar = new wxMenuBar;
258 menu_bar->Append(file_menu, "&File");
259 menu_bar->Append(m_pWindowMenu, "&Window");
260 menu_bar->Append(help_menu, "&Help");
262 SetMenuBar(menu_bar);
264 for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
265 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
266 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
267 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
270 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
271 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
272 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
273 m_iDefaultFilterXSize = 256;
274 m_iDefaultFilterYSize = 256;
275 m_dDefaultFilterParam = 1.;
276 m_dDefaultFilterBandwidth = 1.;
277 m_dDefaultFilterInputScale = 1.;
278 m_dDefaultFilterOutputScale = 1.;
283 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
285 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
290 #elif defined(VERSION)
291 msg << "Version: " << VERSION << "\n\n";
293 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
295 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
299 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
301 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
302 int dialogReturn = dialogPhantom.ShowModal();
303 if (dialogReturn == wxID_OK) {
304 wxString selection (dialogPhantom.getPhantom());
305 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
306 wxString filename = selection + ".phm";
307 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
308 theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
314 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
316 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
317 int dialogReturn = dialogFilter.ShowModal();
318 if (dialogReturn == wxID_OK) {
319 wxString strFilter (dialogFilter.getFilterName());
320 wxString strDomain (dialogFilter.getDomainName());
321 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
322 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
323 m_iDefaultFilterXSize = dialogFilter.getXSize();
324 m_iDefaultFilterYSize = dialogFilter.getYSize();
325 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
326 m_dDefaultFilterParam= dialogFilter.getFilterParam();
327 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
328 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
329 std::ostringstream os;
330 os << "Generate Filter=" << strFilter.c_str()
331 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
332 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
333 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
334 *theApp->getLog() << os.str().c_str() << "\n";
335 wxString filename = "untitled.if";
336 ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
338 sys_error (ERR_SEVERE, "Unable to create filter image");
341 ImageFile& rIF = pFilterDoc->getImageFile();
342 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
343 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
344 rIF.labelAdd (os.str().c_str());
345 if (theApp->getSetModifyNewDocs())
346 pFilterDoc->Modify (true);
347 pFilterDoc->UpdateAllViews();
348 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
353 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
355 const ImageFile& rIF = pIFDoc->getImageFile();
356 unsigned int nx = rIF.nx();
357 unsigned int ny = rIF.ny();
358 wxList& rListDocs = m_docManager->GetDocuments();
359 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
360 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
361 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
362 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
363 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
364 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
365 vecIF.push_back (pIFCompareDoc);
371 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
373 wxMessageBox("No help available, refer to man pages of command-line tools");
377 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
383 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
386 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
387 wxNode* pNode = rListDocs.GetFirst();
388 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
389 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
390 wxString strFilename = pDoc->GetFilename();
391 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
392 m_apWindowMenuData[iPos] = pDoc;
393 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
395 pNode = pNode->GetNext();
397 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
398 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
399 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
400 m_apWindowMenuData[i] = NULL;
406 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
408 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
409 wxString strFilename = pDoc->GetFilename();
410 const wxView* pView = pDoc->GetFirstView();
412 wxFrame* pFrame = pView->GetFrame();
419 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
420 { DoWindowMenu (0, event); }
422 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
423 { DoWindowMenu (1, event); }
425 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
426 { DoWindowMenu (2, event); }
428 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
429 { DoWindowMenu (3, event); }
431 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
432 { DoWindowMenu (4, event); }
434 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
435 { DoWindowMenu (5, event); }
437 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
438 { DoWindowMenu (6, event); }
440 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
441 { DoWindowMenu (7, event); }
443 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
444 { DoWindowMenu (8, event); }
446 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
447 { DoWindowMenu (9, event); }
449 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
450 { DoWindowMenu (10, event); }
452 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
453 { DoWindowMenu (11, event); }
455 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
456 { DoWindowMenu (12, event); }
458 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
459 { DoWindowMenu (13, event); }
461 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
462 { DoWindowMenu (14, event); }
464 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
465 { DoWindowMenu (15, event); }
467 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
468 { DoWindowMenu (16, event); }
470 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
471 { DoWindowMenu (17, event); }
473 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
474 { DoWindowMenu (18, event); }
476 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
477 { DoWindowMenu (19, event); }