fb835ad8b5a9afdaf9d62489d3a9a628be796c69
[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.25 2001/01/02 16:02:13 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.25 2001/01/02 16:02:13 kevin Exp $";
65
66 class CTSimApp* theApp = NULL;
67
68 struct option CTSimApp::ctsimOptions[] = 
69 {
70   {"help", 0, 0, O_HELP},
71   {"version", 0, 0, O_VERSION},
72   {0, 0, 0, 0}
73 };
74
75 IMPLEMENT_APP(CTSimApp)
76
77 CTSimApp::CTSimApp()
78 : m_docManager(NULL), m_pFrame(NULL)
79 {
80   theApp = this;
81 }
82
83 #ifdef HAVE_SYS_TIME_H
84 #include <sys/time.h>
85 #endif
86
87 #ifdef HAVE_SYS_RESOURCE_H
88 #include <sys/resource.h>
89 #endif
90
91 bool
92 CTSimApp::OnInit()
93 {
94 #ifdef HAVE_SETPRIORITY
95   setpriority (PRIO_PROCESS, 0, 15);  // set to low scheduling priority
96 #endif
97   
98   // process options
99   while (1) {
100     int c = getopt_long (argc, argv, "", ctsimOptions, NULL);
101     if (c == -1)
102       break;
103     
104     switch (c) {
105     case O_VERSION:
106       std::cout << rcsindent << std::endl;
107 #ifdef CTSIMVERSION
108       std::cout << "Version: CTSIMVERSION" << std::endl;
109 #elif defined(VERSION)
110       std::cout << "Version: VERSION" << std::endl;
111 #endif
112       exit(0);
113     case O_HELP:
114     case '?':
115       usage (argv[0]);
116       exit (0);
117     default:
118       usage (argv[0]);
119       exit (1);
120     }
121   }
122   
123   m_docManager = new wxDocManager;
124   
125   new wxDocTemplate (m_docManager, "ImageFile", "*.if", "", "if", "ImageFile doc", "ImageFile View", CLASSINFO(ImageFileDocument), CLASSINFO(ImageFileView));
126   
127   new wxDocTemplate (m_docManager, "ProjectionFile", "*.pj", "", "pj", "ProjectionFile doc", "ProjectionFile View", CLASSINFO(ProjectionFileDocument), CLASSINFO(ProjectionFileView));
128   
129   new wxDocTemplate (m_docManager, "PhantomFile", "*.phm", "", "phm", "Phantom doc", "Phantom View", CLASSINFO(PhantomDocument), CLASSINFO(PhantomView));
130   
131   new wxDocTemplate (m_docManager, "PlotFile", "*.plt", "", "plt", "Plot doc", "Plot View", CLASSINFO(PlotFileDocument), CLASSINFO(PlotFileView));
132   
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);
135   
136   SetTopWindow (m_pFrame);
137   m_pFrame->Centre(wxBOTH);
138   
139   m_pFrame->Show(true);
140   
141   for (int i = optind + 1; i <= argc; i++) {
142     wxString filename = argv [i - 1];
143     m_docManager->CreateDocument (filename, wxDOC_SILENT);
144   }
145   
146   return true;
147 }
148
149 void
150 CTSimApp::usage(const char* program)
151 {
152   std::cout << "usage: " << fileBasename(program) << " [files-to-open...] [OPTIONS]\n";
153   std::cout << "Computed Tomography Simulator (Graphical Shell)\n";
154   std::cout << "\n";
155   std::cout << "  --version Display version\n";
156   std::cout << "  --help    Display this help message\n";
157 }
158
159 int
160 CTSimApp::OnExit()
161 {
162   delete m_docManager;
163 #ifdef HAVE_DMALLOC
164   dmalloc_shutdown();
165 #endif
166   return 0;
167 }
168
169 wxString
170 CTSimApp::getUntitledFilename()
171 {
172   static int untitledNumber = 1;
173   
174   wxString filename ("Untitled");
175   filename << untitledNumber++;
176   
177   return (filename);
178 }
179
180
181 // Top-level window for CTSim
182
183 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
184
185 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
186 EVT_MENU(MAINMENU_HELP_ABOUT, MainFrame::OnAbout)
187 EVT_MENU(MAINMENU_HELP_CONTENTS, MainFrame::OnHelpContents)
188 EVT_MENU(MAINMENU_FILE_CREATE_PHANTOM, MainFrame::OnCreatePhantom)
189 EVT_MENU(MAINMENU_FILE_CREATE_FILTER, MainFrame::OnCreateFilter)
190 EVT_MENU(MAINMENU_FILE_EXIT, MainFrame::OnExit)
191 EVT_MENU(MAINMENU_WINDOW_BASE, MainFrame::OnWindowMenu0)
192 EVT_MENU(MAINMENU_WINDOW_BASE+1, MainFrame::OnWindowMenu1)
193 EVT_MENU(MAINMENU_WINDOW_BASE+2, MainFrame::OnWindowMenu2)
194 EVT_MENU(MAINMENU_WINDOW_BASE+3, MainFrame::OnWindowMenu3)
195 EVT_MENU(MAINMENU_WINDOW_BASE+4, MainFrame::OnWindowMenu4)
196 EVT_MENU(MAINMENU_WINDOW_BASE+5, MainFrame::OnWindowMenu5)
197 EVT_MENU(MAINMENU_WINDOW_BASE+6, MainFrame::OnWindowMenu6)
198 EVT_MENU(MAINMENU_WINDOW_BASE+7, MainFrame::OnWindowMenu7)
199 EVT_MENU(MAINMENU_WINDOW_BASE+8, MainFrame::OnWindowMenu8)
200 EVT_MENU(MAINMENU_WINDOW_BASE+9, MainFrame::OnWindowMenu9)
201 EVT_MENU(MAINMENU_WINDOW_BASE+10, MainFrame::OnWindowMenu10)
202 EVT_MENU(MAINMENU_WINDOW_BASE+11, MainFrame::OnWindowMenu11)
203 EVT_MENU(MAINMENU_WINDOW_BASE+12, MainFrame::OnWindowMenu12)
204 EVT_MENU(MAINMENU_WINDOW_BASE+13, MainFrame::OnWindowMenu13)
205 EVT_MENU(MAINMENU_WINDOW_BASE+14, MainFrame::OnWindowMenu14)
206 EVT_MENU(MAINMENU_WINDOW_BASE+15, MainFrame::OnWindowMenu15)
207 EVT_MENU(MAINMENU_WINDOW_BASE+16, MainFrame::OnWindowMenu16)
208 EVT_MENU(MAINMENU_WINDOW_BASE+17, MainFrame::OnWindowMenu17)
209 EVT_MENU(MAINMENU_WINDOW_BASE+18, MainFrame::OnWindowMenu18)
210 EVT_MENU(MAINMENU_WINDOW_BASE+19, MainFrame::OnWindowMenu19)
211 EVT_UPDATE_UI_RANGE(MAINMENU_WINDOW_BASE, MAINMENU_WINDOW_BASE+20, MainFrame::OnUpdateUI)
212 END_EVENT_TABLE()
213
214
215
216 MainFrame::MainFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, const long type)
217 : wxDocParentFrame(manager, frame, id, title, pos, size, type), m_pLog(NULL)
218 {
219   m_pLog = new wxTextCtrl (this, -1, "Log Window\n", wxPoint(0, 250), wxSize(100,50), wxTE_MULTILINE | wxTE_READONLY);
220   wxLog::SetActiveTarget(new wxLogTextCtrl(m_pLog));
221   CreateStatusBar();
222   SetStatusText ("Welcome to CTSim");
223   
224   //// Make a menubar
225   wxMenu *file_menu = new wxMenu;
226   
227   file_menu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...");
228   file_menu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...");
229   file_menu->Append(wxID_OPEN, "&Open...");
230   
231   file_menu->AppendSeparator();
232   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
233   
234   //  history of files visited
235   m_docManager->FileHistoryUseMenu(file_menu);
236   
237   m_pWindowMenu = new wxMenu;
238   m_pWindowMenu->UpdateUI (this);
239   
240   wxMenu* help_menu = new wxMenu;
241   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents");
242   help_menu->AppendSeparator();
243   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
244   
245   wxMenuBar* menu_bar = new wxMenuBar;
246   
247   menu_bar->Append(file_menu, "&File");
248   menu_bar->Append(m_pWindowMenu, "&Window");
249   menu_bar->Append(help_menu, "&Help");
250   
251   SetMenuBar(menu_bar);
252   
253   for (int i = 0; i < MAX_WINDOW_MENUITEMS; i++) {
254     m_apWindowMenuItems[i] = new wxMenuItem (m_pWindowMenu, MAINMENU_WINDOW_BASE+i, wxString("<Empty>"));
255     m_pWindowMenu->Append (m_apWindowMenuItems[i]);
256     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
257   }
258   
259   m_iDefaultPhantomID = Phantom::PHM_HERMAN;
260   m_iDefaultFilterID = SignalFilter::FILTER_BANDLIMIT;
261   m_iDefaultFilterDomainID = SignalFilter::DOMAIN_FREQUENCY;
262   m_iDefaultFilterXSize = 256;
263   m_iDefaultFilterYSize = 256;
264   m_dDefaultFilterParam = 1.;
265   m_dDefaultFilterBandwidth = 1.;
266   m_dDefaultFilterInputScale = 1.;
267   m_dDefaultFilterOutputScale = 1.;
268   
269 }
270
271 void 
272 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
273 {
274   wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
275 #ifdef CTSIMVERSION
276   msg += "Version ";
277   msg += CTSIMVERSION;
278   msg += "\n\n";
279 #elif defined(VERSION)
280   msg << "Version: " <<  VERSION << "\n\n";
281 #endif
282   msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
283   
284   wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
285 }
286
287 void 
288 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
289 {
290   DialogGetPhantom dialogPhantom (this, m_iDefaultPhantomID);
291   int dialogReturn = dialogPhantom.ShowModal();
292   if (dialogReturn == wxID_OK) {
293     wxString selection (dialogPhantom.getPhantom());
294     *theApp->getLog() << "Selected phantom " << selection.c_str() << "\n";
295     wxString filename = selection + ".phm";
296     m_iDefaultPhantomID = Phantom::convertNameToPhantomID (selection.c_str());
297     theApp->getDocManager()->CreateDocument(filename, wxDOC_SILENT);
298   }
299   
300 }
301
302 void 
303 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
304 {
305   DialogGetFilterParameters dialogFilter (this, m_iDefaultFilterXSize, m_iDefaultFilterYSize, m_iDefaultFilterID, m_dDefaultFilterParam, m_dDefaultFilterBandwidth, m_iDefaultFilterDomainID, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
306   int dialogReturn = dialogFilter.ShowModal();
307   if (dialogReturn == wxID_OK) {
308     wxString strFilter (dialogFilter.getFilterName());
309     wxString strDomain (dialogFilter.getDomainName());
310     m_iDefaultFilterID = SignalFilter::convertFilterNameToID (strFilter.c_str());
311     m_iDefaultFilterDomainID = SignalFilter::convertDomainNameToID (strDomain.c_str());
312     m_iDefaultFilterXSize = dialogFilter.getXSize();
313     m_iDefaultFilterYSize = dialogFilter.getYSize();
314     m_dDefaultFilterBandwidth = dialogFilter.getBandwidth();
315     m_dDefaultFilterParam= dialogFilter.getFilterParam();
316     m_dDefaultFilterInputScale = dialogFilter.getInputScale();
317     m_dDefaultFilterOutputScale = dialogFilter.getOutputScale();
318     std::ostringstream os;
319     os << "Generate Filter=" << strFilter.c_str() 
320       << ", size=(" << static_cast<int>(m_iDefaultFilterXSize) << "," << static_cast<int>(m_iDefaultFilterYSize) 
321       << "), domain=" << strDomain.c_str() << ", filterParam=" << m_dDefaultFilterParam << ", bandwidth=" << m_dDefaultFilterBandwidth 
322       << ", inputScale=" << m_dDefaultFilterInputScale << ", outputScale=" << m_dDefaultFilterOutputScale;
323     *theApp->getLog() << os.str().c_str() << "\n";
324     wxString filename = "untitled.if";
325     ImageFileDocument* pFilterDoc = dynamic_cast<ImageFileDocument*>(theApp->getDocManager()->CreateDocument ("untitled.if", wxDOC_SILENT));
326     if (! pFilterDoc) {
327       sys_error (ERR_SEVERE, "Unable to create filter image");
328       return;
329     }
330     ImageFile& rIF = pFilterDoc->getImageFile();
331     rIF.setArraySize (m_iDefaultFilterXSize, m_iDefaultFilterYSize);
332     rIF.filterResponse (strDomain.c_str(), m_dDefaultFilterBandwidth, strFilter.c_str(), m_dDefaultFilterParam, m_dDefaultFilterInputScale, m_dDefaultFilterOutputScale);
333     rIF.labelAdd (os.str().c_str());
334     if (theApp->getSetModifyNewDocs())
335       pFilterDoc->Modify (true);
336     pFilterDoc->UpdateAllViews();
337     pFilterDoc->GetFirstView()->OnUpdate (NULL, NULL);
338   }
339 }
340
341 void
342 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
343 {
344   const ImageFile& rIF = pIFDoc->getImageFile();
345   unsigned int nx = rIF.nx();
346   unsigned int ny = rIF.ny();
347   wxList& rListDocs = m_docManager->GetDocuments();
348   for (wxNode* pNode = rListDocs.GetFirst(); pNode != NULL; pNode = pNode->GetNext()) {
349     wxDocument* pDoc = reinterpret_cast<wxDocument*>(pNode->GetData());
350     ImageFileDocument* pIFCompareDoc = dynamic_cast<ImageFileDocument*>(pDoc);
351     if (pIFCompareDoc && (pIFDoc != pIFCompareDoc)) {
352       const ImageFile& rCompareIF = pIFCompareDoc->getImageFile();
353       if (rCompareIF.nx() == nx && rCompareIF.ny() == ny)
354         vecIF.push_back (pIFCompareDoc);
355     }
356   }
357 }
358
359 void 
360 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
361 {
362   wxMessageBox("No help available, refer to man pages of command-line tools");
363 }
364
365 void 
366 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
367 {
368   Close(true);
369 }
370
371 void
372 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
373 {
374   int iPos = 0;
375   wxList& rListDocs = m_docManager->GetDocuments();
376   wxNode* pNode = rListDocs.GetFirst();
377   while (iPos < MAX_WINDOW_MENUITEMS && pNode != NULL) {
378     wxDocument* pDoc = static_cast<wxDocument*>(pNode->GetData());
379     wxString strFilename = pDoc->GetFilename();
380     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[iPos])->SetName (strFilename);
381     m_apWindowMenuData[iPos] = pDoc;
382     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+iPos, true);
383     iPos++;
384     pNode = pNode->GetNext();
385   }
386   for (int i = iPos; i < MAX_WINDOW_MENUITEMS; i++) {
387     m_pWindowMenu->Enable (MAINMENU_WINDOW_BASE+i, false);
388     static_cast<wxMenuItemBase*>(m_apWindowMenuItems[i])->SetName (wxString("<Empty>"));
389     m_apWindowMenuData[i] = NULL;
390   }
391   
392 }
393
394 void 
395 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
396 {
397   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
398     wxString strFilename = pDoc->GetFilename();
399     const wxView* pView = pDoc->GetFirstView();
400     if (pView) {
401       wxFrame* pFrame = pView->GetFrame();
402       pFrame->SetFocus();
403       pFrame->Raise();
404     }
405   }
406 }
407
408 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
409 { DoWindowMenu (0, event); }
410
411 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
412 { DoWindowMenu (1, event); }
413
414 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
415 { DoWindowMenu (2, event); }
416
417 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
418 { DoWindowMenu (3, event); }
419
420 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
421 { DoWindowMenu (4, event); }
422
423 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
424 { DoWindowMenu (5, event); }
425
426 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
427 { DoWindowMenu (6, event); }
428
429 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
430 { DoWindowMenu (7, event); }
431
432 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
433 { DoWindowMenu (8, event); }
434
435 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
436 { DoWindowMenu (9, event); }
437
438 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
439 { DoWindowMenu (10, event); }
440
441 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
442 { DoWindowMenu (11, event); }
443
444 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
445 { DoWindowMenu (12, event); }
446
447 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
448 { DoWindowMenu (13, event); }
449
450 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
451 { DoWindowMenu (14, event); }
452
453 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
454 { DoWindowMenu (15, event); }
455
456 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
457 { DoWindowMenu (16, event); }
458
459 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
460 { DoWindowMenu (17, event); }
461
462 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
463 { DoWindowMenu (18, event); }
464
465 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
466 { DoWindowMenu (19, event); }
467
468