r411: no message
[ctsim.git] / src / ctsim.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ctsim.cpp
5 **   Purpose:       Top-level routines of 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: ctsim.cpp,v 1.30 2001/01/17 06:38:06 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
29 // For compilers that support precompilation, includes "wx/wx.h".
30 #include "wx/wxprec.h"
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #ifndef WX_PRECOMP
37 #include "wx/wx.h"
38 #endif
39
40 #if !wxUSE_DOC_VIEW_ARCHITECTURE
41 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
42 #endif
43
44 #include "ct.h"
45 #include "ctsim.h"
46 #include "docs.h"
47 #include "views.h"
48 #include "dialogs.h"
49
50 #if defined(HAVE_CONFIG_H)
51 #include "config.h"
52 #endif
53
54 #if defined(HAVE_GETOPT_H) || defined(HAVE_GETOPT_LONG)
55 #ifdef MSVC
56 #define __STDC__ 1
57 #endif
58 #include "getopt.h"
59 #ifdef MSVC
60 #undef __STDC__
61 #endif
62 #endif
63
64 static const char* rcsindent = "$Id: ctsim.cpp,v 1.30 2001/01/17 06:38:06 kevin Exp $";
65
66 struct option CTSimApp::ctsimOptions[] = 
67 {
68   {"help", 0, 0, O_HELP},
69   {"version", 0, 0, O_VERSION},
70   {0, 0, 0, 0}
71 };
72
73 IMPLEMENT_APP(CTSimApp)
74
75 CTSimApp::CTSimApp()
76 : m_docManager(NULL), m_pFrame(NULL)
77 {
78   theApp = this;
79 }
80
81 #ifdef HAVE_SYS_TIME_H
82 #include <sys/time.h>
83 #endif
84
85 #ifdef HAVE_SYS_RESOURCE_H
86 #include <sys/resource.h>
87 #endif
88
89 bool
90 CTSimApp::OnInit()
91 {
92 #ifdef HAVE_SETPRIORITY
93   setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
94 #endif
95
96   g_bRunningWXWindows = true;
97   // process options
98   while (1) {
99     int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
100     if (c == -1)
101       break;
102     
103     switch (c) {
104     case O_VERSION:
105       std::cout << rcsindent << std::endl;
106 #ifdef CTSIMVERSION
107       std::cout << "Version: CTSIMVERSION" << std::endl;
108 #elif defined(VERSION)
109       std::cout << "Version: VERSION" << std::endl;
110 #endif
111       exit(0);
112     case O_HELP:
113     case '?':
114       usage (argv[0]);
115       exit (0);
116     default:
117       usage (argv[0]);
118       exit (1);
119     }
120   }
121   
122   m_docManager = new wxDocManager;
123   
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));
128   
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);
131
132   SetTopWindow (m_pFrame);
133   m_pFrame->Centre(wxBOTH);
134   
135   m_pFrame->Show(true);
136   
137   for (int i = optind + 1; i <= argc; i++) {
138     wxString filename = argv [i - 1];
139     m_docManager->CreateDocument (filename, wxDOC_SILENT);
140   }
141
142   setIconForFrame (m_pFrame);
143
144   return true;
145 }
146
147 #include "./ctsim.xpm"
148 void
149 CTSimApp::setIconForFrame(wxFrame* pFrame)
150 {
151   wxIcon iconApp (ctsim16_xpm);
152
153   if (iconApp.Ok())
154     pFrame->SetIcon (iconApp);
155 }
156
157 void
158 CTSimApp::usage(const char* program)
159 {
160   std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
161   std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
162   std::cout << "\n";
163   std::cout << "  --version Display version\n";
164   std::cout << "  --help    Display this help message\n";
165 }
166
167 int
168 CTSimApp::OnExit()
169 {
170   delete m_docManager;
171 #ifdef HAVE_DMALLOC
172   dmalloc_shutdown();
173 #endif
174   return 0;
175 }
176
177 wxString
178 CTSimApp::getUntitledFilename()
179 {
180   static int untitledNumber = 1;
181   
182   wxString filename ("Untitled");
183   filename << untitledNumber++;
184   
185   return (filename);
186 }
187
188
189 // Top-level window for CTSim
190
191 #if CTSIM_MDI
192 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
193
194 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
195 #else
196 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
197
198 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
199 #endif
200
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)
227 END_EVENT_TABLE()
228
229
230
231 #if CTSIM_MDI
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)
234 #else
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)
237 #endif
238 {
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));
241   CreateStatusBar();
242   SetStatusText ("Welcome to CTSim");
243   
244   //// Make a menubar
245   wxMenu *file_menu = new wxMenu;
246   
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...");
250   
251   file_menu->AppendSeparator();
252   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
253   
254   //  history of files visited
255   theApp->getDocManager()->FileHistoryUseMenu(file_menu);
256   
257   m_pWindowMenu = new wxMenu;
258   m_pWindowMenu->UpdateUI (this);
259   
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");
264   
265   wxMenuBar* menu_bar = new wxMenuBar;
266   
267   menu_bar->Append(file_menu, "&File");
268   menu_bar->Append(m_pWindowMenu, "&Window");
269   menu_bar->Append(help_menu, "&Help");
270   
271   SetMenuBar(menu_bar);
272   
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);
277   }
278   
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.;
288   
289 }
290
291 void 
292 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
293 {
294   wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
295 #ifdef CTSIMVERSION
296   msg += "Version ";
297   msg += CTSIMVERSION;
298   msg += "\n\n";
299 #elif defined(VERSION)
300   msg << "Version: " <<  VERSION << "\n\n";
301 #endif
302   msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
303   
304   wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
305 }
306
307 void 
308 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
309 {
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);
318   }
319   
320 }
321
322 void 
323 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
324 {
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));
346     if (! pFilterDoc) {
347       sys_error (ERR_SEVERE, "Unable to create filter image");
348       return;
349     }
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);
358   }
359 }
360
361 void
362 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
363 {
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);
375     }
376   }
377 }
378
379 void 
380 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
381 {
382   wxMessageBox("No help available, refer to man pages of command-line tools");
383 }
384
385 void 
386 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
387 {
388   Close(true);
389 }
390
391 void
392 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
393 {
394   int iPos = 0;
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);
403     iPos++;
404     pNode = pNode->GetNext();
405   }
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;
410   }
411   
412 }
413
414 void 
415 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
416 {
417   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
418     wxString strFilename = pDoc->GetFilename();
419     const wxView* pView = pDoc->GetFirstView();
420     if (pView) {
421       wxFrame* pFrame = pView->GetFrame();
422       pFrame->SetFocus();
423       pFrame->Raise();
424     }
425   }
426 }
427
428 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
429 { DoWindowMenu (0, event); }
430
431 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
432 { DoWindowMenu (1, event); }
433
434 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
435 { DoWindowMenu (2, event); }
436
437 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
438 { DoWindowMenu (3, event); }
439
440 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
441 { DoWindowMenu (4, event); }
442
443 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
444 { DoWindowMenu (5, event); }
445
446 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
447 { DoWindowMenu (6, event); }
448
449 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
450 { DoWindowMenu (7, event); }
451
452 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
453 { DoWindowMenu (8, event); }
454
455 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
456 { DoWindowMenu (9, event); }
457
458 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
459 { DoWindowMenu (10, event); }
460
461 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
462 { DoWindowMenu (11, event); }
463
464 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
465 { DoWindowMenu (12, event); }
466
467 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
468 { DoWindowMenu (13, event); }
469
470 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
471 { DoWindowMenu (14, event); }
472
473 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
474 { DoWindowMenu (15, event); }
475
476 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
477 { DoWindowMenu (16, event); }
478
479 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
480 { DoWindowMenu (17, event); }
481
482 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
483 { DoWindowMenu (18, event); }
484
485 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
486 { DoWindowMenu (19, event); }
487
488