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.26 2001/01/06 15:33:15 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.26 2001/01/06 15:33:15 kevin Exp $";
66 class CTSimApp* theApp = NULL;
68 struct option CTSimApp::ctsimOptions[] =
70 {"help", 0, 0, O_HELP},
71 {"version", 0, 0, O_VERSION},
75 IMPLEMENT_APP(CTSimApp)
78 : m_docManager(NULL), m_pFrame(NULL)
83 #ifdef HAVE_SYS_TIME_H
87 #ifdef HAVE_SYS_RESOURCE_H
88 #include <sys/resource.h>
94 #ifdef HAVE_SETPRIORITY
95 setpriority (PRIO_PROCESS, 0, 15); // set to low scheduling priority
100 int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
106 std::cout << rcsindent << std::endl;
108 std::cout << "Version: CTSIMVERSION" << std::endl;
109 #elif defined(VERSION)
110 std::cout << "Version: VERSION" << std::endl;
123 m_docManager = new wxDocManager;
125 new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
127 new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
129 new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
131 new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
133 //// Create the main frame window
134 m_pFrame = new MainFrame(m_docManager, (wxFrame *) NULL, -1, "CTSim", wxPoint(0, 0), wxSize(500, 400), wxDEFAULT_FRAME_STYLE);
136 SetTopWindow (m_pFrame);
137 m_pFrame->Centre(wxBOTH);
139 m_pFrame->Show(true);
141 for (int i = optind + 1; i <= argc; i++) {
142 wxString filename = argv [i - 1];
143 m_docManager->CreateDocument (filename, wxDOC_SILENT);
150 CTSimApp::usage(const char* program)
152 std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
153 std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
155 std::cout << " --version Display version\n";
156 std::cout << " --help Display this help message\n";
170 CTSimApp::getUntitledFilename()
172 static int untitledNumber = 1;
174 wxString filename ("Untitled");
175 filename << untitledNumber++;
181 // Top-level window for CTSim
184 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
186 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
188 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
190 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
193 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
194 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
195 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
196 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
197 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
198 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
199 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
200 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
201 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
202 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
203 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
204 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
205 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
206 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
207 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
208 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
209 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
210 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
211 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
212 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
213 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
214 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
215 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
216 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
217 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
218 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
224 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
225 : wxMDIParentFrame(NULL, id, title, pos, size, type), m_pLog(NULL)
227 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
228 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
231 m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
232 wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
234 SetStatusText ("Welcome to CTSim");
237 wxMenu *file_menu = new wxMenu;
239 file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
240 file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
241 file_menu->Append(wxID_OPEN, "&Open...");
243 file_menu->AppendSeparator();
244 file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
246 // history of files visited
247 theApp->getDocManager()->FileHistoryUseMenu(file_menu);
249 m_pWindowMenu = new wxMenu;
250 m_pWindowMenu->UpdateUI (this);
252 wxMenu* help_menu = new wxMenu;
253 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
254 help_menu->AppendSeparator();
255 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
257 wxMenuBar* menu_bar = new wxMenuBar;
259 menu_bar->Append(file_menu, "&File");
260 menu_bar->Append(m_pWindowMenu, "&Window");
261 menu_bar->Append(help_menu, "&Help");
263 SetMenuBar(menu_bar);
265 for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
266 m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
267 m_pWindowMenu->Append (m_apWindowMenuItems[i]);
268 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
271 m_iDefaultPhantomID = Phantom::PHM_HERMAN;
272 m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
273 m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
274 m_iDefaultFilterXSize = 256;
275 m_iDefaultFilterYSize = 256;
276 m_dDefaultFilterParam = 1.;
277 m_dDefaultFilterBandwidth = 1.;
278 m_dDefaultFilterInputScale = 1.;
279 m_dDefaultFilterOutputScale = 1.;
284 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
286 wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
291 #elif defined(VERSION)
292 msg << "Version: " << VERSION << "\n\n";
294 msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
296 wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
300 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
302 DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
303 int dialogReturn = dialogPhantom.ShowModal();
304 if (dialogReturn == wxID_OK) {
305 wxString selection (dialogPhantom.getPhantom());
306 *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
307 wxString filename = selection + ".phm";
308 m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
309 theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
315 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
317 DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
318 int dialogReturn = dialogFilter.ShowModal();
319 if (dialogReturn == wxID_OK) {
320 wxString strFilter (dialogFilter.getFilterName());
321 wxString strDomain (dialogFilter.getDomainName());
322 m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
323 m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
324 m_iDefaultFilterXSize = dialogFilter.getXSize();
325 m_iDefaultFilterYSize = dialogFilter.getYSize();
326 m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
327 m_dDefaultFilterParam= dialogFilter.getFilterParam();
328 m_dDefaultFilterInputScale = dialogFilter.getInputScale();
329 m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
330 std::ostringstream os;
331 os << "Generate Filter=" << strFilter.c_str()
332 << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize)
333 << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth
334 << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
335 *theApp->getLog() << os.str().c_str() << "\n";
336 wxString filename = "untitled.if";
337 ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
339 sys_error (ERR_SEVERE, "Unable to create filter image");
342 ImageFile& rIF = pFilterDoc->getImageFile();
343 rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
344 rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
345 rIF.labelAdd (os.str().c_str());
346 if (theApp->getSetModifyNewDocs())
347 pFilterDoc->Modify (true);
348 pFilterDoc->UpdateAllViews();
349 pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
354 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
356 const ImageFile& rIF = pIFDoc->getImageFile();
357 unsigned int nx = rIF.nx();
358 unsigned int ny = rIF.ny();
359 wxList& rListDocs = m_docManager->GetDocuments();
360 for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
361 wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
362 ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
363 if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
364 const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
365 if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
366 vecIF.push_back (pIFCompareDoc);
372 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
374 wxMessageBox("No help available, refer to man pages of command-line tools");
378 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
384 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
387 wxList& rListDocs = theApp->getDocManager()->GetDocuments();
388 wxNode* pNode = rListDocs.GetFirst();
389 while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
390 wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
391 wxString strFilename = pDoc->GetFilename();
392 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
393 m_apWindowMenuData[iPos] = pDoc;
394 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
396 pNode = pNode->GetNext();
398 for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
399 m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
400 static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
401 m_apWindowMenuData[i] = NULL;
407 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
409 if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
410 wxString strFilename = pDoc->GetFilename();
411 const wxView* pView = pDoc->GetFirstView();
413 wxFrame* pFrame = pView->GetFrame();
420 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
421 { DoWindowMenu (0, event); }
423 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
424 { DoWindowMenu (1, event); }
426 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
427 { DoWindowMenu (2, event); }
429 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
430 { DoWindowMenu (3, event); }
432 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
433 { DoWindowMenu (4, event); }
435 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
436 { DoWindowMenu (5, event); }
438 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
439 { DoWindowMenu (6, event); }
441 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
442 { DoWindowMenu (7, event); }
444 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
445 { DoWindowMenu (8, event); }
447 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
448 { DoWindowMenu (9, event); }
450 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
451 { DoWindowMenu (10, event); }
453 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
454 { DoWindowMenu (11, event); }
456 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
457 { DoWindowMenu (12, event); }
459 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
460 { DoWindowMenu (13, event); }
462 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
463 { DoWindowMenu (14, event); }
465 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
466 { DoWindowMenu (15, event); }
468 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
469 { DoWindowMenu (16, event); }
471 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
472 { DoWindowMenu (17, event); }
474 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
475 { DoWindowMenu (18, event); }
477 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
478 { DoWindowMenu (19, event); }