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.30 2001/01/17 06:38: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 // 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.30 2001/01/17 06:38:06 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));
125 new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
126 new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
127 new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
129 //// Create the main frame window
130 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
132 SetTopWindow (m_pFrame);
133 m_pFrame->Centre(wxBOTH);
135 m_pFrame->Show(true);
137 for (int i = optind + 1; i <= argc; i++) {
138 wxString filename = argv [i - 1];
139 m_docManager->CreateDocument (filename, wxDOC_SILENT);
142 setIconForFrame (m_pFrame);
147 #include "./ctsim.xpm"
149 CTSimApp::setIconForFrame(wxFrame* pFrame)
151 wxIcon iconApp (ctsim16_xpm);
154 pFrame->SetIcon (iconApp);
158 CTSimApp::usage(const char* program)
160 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
161 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
163 std::cout << " --version Display version\n";
164 std::cout << " --help Display this help message\n";
178 CTSimApp::getUntitledFilename()
180 static int untitledNumber = 1;
182 wxString filename ("Untitled");
183 filename << untitledNumber++;
189 // Top-level window for CTSim
192 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
194 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
196 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
198 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
201 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
202 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
203 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
204 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
205 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
206 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
207 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
208 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
209 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
210 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
211 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
212 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
213 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
214 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
215 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
216 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
217 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
218 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
219 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
220 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
221 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
222 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
223 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
224 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
225 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
226 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
232 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
233 : wxMDIParentFrame(NULL, id, title, pos, size, type), m_pLog(NULL)
235 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
236 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
239 m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
240 wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
242 SetStatusText ("Welcome to CTSim");
245 wxMenu *file_menu = new wxMenu;
247 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
248 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
249 file_menu->Append(wxID_OPEN, "&Open...");
251 file_menu->AppendSeparator();
252 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
254 // history of files visited
255 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
257 m_pWindowMenu = new wxMenu;
258 m_pWindowMenu->UpdateUI (this);
260 wxMenu* help_menu = new wxMenu;
261 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
262 help_menu->AppendSeparator();
263 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
265 wxMenuBar* menu_bar = new wxMenuBar;
267 menu_bar->Append(file_menu, "&File");
268 menu_bar->Append(m_pWindowMenu, "&Window");
269 menu_bar->Append(help_menu, "&Help");
271 SetMenuBar(menu_bar);
273 for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
274 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
275 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
276 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
279 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
280 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
281 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
282 m_iDefaultFilterXSize = 256;
283 m_iDefaultFilterYSize = 256;
284 m_dDefaultFilterParam = 1.;
285 m_dDefaultFilterBandwidth = 1.;
286 m_dDefaultFilterInputScale = 1.;
287 m_dDefaultFilterOutputScale = 1.;
292 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
294 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
299 #elif defined(VERSION)
300 msg << "Version: " << VERSION << "\n\n";
302 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
304 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
308 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
310 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
311 int dialogReturn = dialogPhantom.ShowModal();
312 if (dialogReturn == wxID_OK) {
313 wxString selection (dialogPhantom.getPhantom());
314 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
315 wxString filename = selection + ".phm";
316 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
317 theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
323 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
325 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
326 int dialogReturn = dialogFilter.ShowModal();
327 if (dialogReturn == wxID_OK) {
328 wxString strFilter (dialogFilter.getFilterName());
329 wxString strDomain (dialogFilter.getDomainName());
330 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
331 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
332 m_iDefaultFilterXSize = dialogFilter.getXSize();
333 m_iDefaultFilterYSize = dialogFilter.getYSize();
334 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
335 m_dDefaultFilterParam= dialogFilter.getFilterParam();
336 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
337 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
338 std::ostringstream os;
339 os << "Generate Filter=" << strFilter.c_str()
340 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
341 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
342 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
343 *theApp->getLog() << os.str().c_str() << "\n";
344 wxString filename = "untitled.if";
345 ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
347 sys_error (ERR_SEVERE, "Unable to create filter image");
350 ImageFile& rIF = pFilterDoc->getImageFile();
351 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
352 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
353 rIF.labelAdd (os.str().c_str());
354 if (theApp->getSetModifyNewDocs())
355 pFilterDoc->Modify (true);
356 pFilterDoc->UpdateAllViews();
357 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
362 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
364 const ImageFile& rIF = pIFDoc->getImageFile();
365 unsigned int nx = rIF.nx();
366 unsigned int ny = rIF.ny();
367 wxList& rListDocs = m_docManager->GetDocuments();
368 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
369 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
370 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
371 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
372 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
373 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
374 vecIF.push_back (pIFCompareDoc);
380 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
382 wxMessageBox("No help available, refer to man pages of command-line tools");
386 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
392 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
395 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
396 wxNode* pNode = rListDocs.GetFirst();
397 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
398 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
399 wxString strFilename = pDoc->GetFilename();
400 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
401 m_apWindowMenuData[iPos] = pDoc;
402 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
404 pNode = pNode->GetNext();
406 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
407 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
408 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
409 m_apWindowMenuData[i] = NULL;
415 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
417 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
418 wxString strFilename = pDoc->GetFilename();
419 const wxView* pView = pDoc->GetFirstView();
421 wxFrame* pFrame = pView->GetFrame();
428 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
429 { DoWindowMenu (0, event); }
431 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
432 { DoWindowMenu (1, event); }
434 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
435 { DoWindowMenu (2, event); }
437 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
438 { DoWindowMenu (3, event); }
440 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
441 { DoWindowMenu (4, event); }
443 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
444 { DoWindowMenu (5, event); }
446 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
447 { DoWindowMenu (6, event); }
449 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
450 { DoWindowMenu (7, event); }
452 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
453 { DoWindowMenu (8, event); }
455 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
456 { DoWindowMenu (9, event); }
458 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
459 { DoWindowMenu (10, event); }
461 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
462 { DoWindowMenu (11, event); }
464 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
465 { DoWindowMenu (12, event); }
467 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
468 { DoWindowMenu (13, event); }
470 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
471 { DoWindowMenu (14, event); }
473 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
474 { DoWindowMenu (15, event); }
476 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
477 { DoWindowMenu (16, event); }
479 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
480 { DoWindowMenu (17, event); }
482 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
483 { DoWindowMenu (18, event); }
485 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
486 { DoWindowMenu (19, event); }