r355: Polar conversions of projections
[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.26 2001/01/06 15:33:15 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.26 2001/01/06 15:33:15 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 #if CTSIM_MDI
184 IMPLEMENT_CLASS(MainFrame, wxMDIParentFrame)
185
186 BEGIN_EVENT_TABLE(MainFrame, wxMDIParentFrame)
187 #else
188 IMPLEMENT_CLASS(MainFrame, wxDocParentFrame)
189
190 BEGIN_EVENT_TABLE(MainFrame, wxDocParentFrame)
191 #endif
192
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)
219 END_EVENT_TABLE()
220
221
222
223 #if CTSIM_MDI
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)
226 #else
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)
229 #endif
230 {
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));
233   CreateStatusBar();
234   SetStatusText ("Welcome to CTSim");
235   
236   //// Make a menubar
237   wxMenu *file_menu = new wxMenu;
238   
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...");
242   
243   file_menu->AppendSeparator();
244   file_menu->Append(MAINMENU_FILE_EXIT, "E&xit");
245   
246   //  history of files visited
247   theApp->getDocManager()->FileHistoryUseMenu(file_menu);
248   
249   m_pWindowMenu = new wxMenu;
250   m_pWindowMenu->UpdateUI (this);
251   
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");
256   
257   wxMenuBar* menu_bar = new wxMenuBar;
258   
259   menu_bar->Append(file_menu, "&File");
260   menu_bar->Append(m_pWindowMenu, "&Window");
261   menu_bar->Append(help_menu, "&Help");
262   
263   SetMenuBar(menu_bar);
264   
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);
269   }
270   
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.;
280   
281 }
282
283 void 
284 MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
285 {
286   wxString msg = "CTSim\nThe Open Source Computed Tomography Simulator\n";
287 #ifdef CTSIMVERSION
288   msg += "Version ";
289   msg += CTSIMVERSION;
290   msg += "\n\n";
291 #elif defined(VERSION)
292   msg << "Version: " <<  VERSION << "\n\n";
293 #endif
294   msg += "Author: Kevin Rosenberg <kevin@rosenberg.net>\nUsage: ctsim [files-to-open..] [--help]";
295   
296   wxMessageBox(msg, "About CTSim", wxOK | wxICON_INFORMATION, this);
297 }
298
299 void 
300 MainFrame::OnCreatePhantom(wxCommandEvent& WXUNUSED(event))
301 {
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);
310   }
311   
312 }
313
314 void 
315 MainFrame::OnCreateFilter (wxCommandEvent& WXUNUSED(event))
316 {
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));
338     if (! pFilterDoc) {
339       sys_error (ERR_SEVERE, "Unable to create filter image");
340       return;
341     }
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);
350   }
351 }
352
353 void
354 CTSimApp::getCompatibleImages (const ImageFileDocument* pIFDoc, std::vector<ImageFileDocument*>& vecIF)
355 {
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);
367     }
368   }
369 }
370
371 void 
372 MainFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event) )
373 {
374   wxMessageBox("No help available, refer to man pages of command-line tools");
375 }
376
377 void 
378 MainFrame::OnExit (wxCommandEvent& WXUNUSED(event) )
379 {
380   Close(true);
381 }
382
383 void
384 MainFrame::OnUpdateUI (wxUpdateUIEvent& rEvent)
385 {
386   int iPos = 0;
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);
395     iPos++;
396     pNode = pNode->GetNext();
397   }
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;
402   }
403   
404 }
405
406 void 
407 MainFrame::DoWindowMenu (int iMenuPosition, wxCommandEvent& event)
408 {
409   if (wxDocument* pDoc = m_apWindowMenuData [iMenuPosition]) {
410     wxString strFilename = pDoc->GetFilename();
411     const wxView* pView = pDoc->GetFirstView();
412     if (pView) {
413       wxFrame* pFrame = pView->GetFrame();
414       pFrame->SetFocus();
415       pFrame->Raise();
416     }
417   }
418 }
419
420 void MainFrame::OnWindowMenu0 (wxCommandEvent& event)
421 { DoWindowMenu (0, event); }
422
423 void MainFrame::OnWindowMenu1 (wxCommandEvent& event)
424 { DoWindowMenu (1, event); }
425
426 void MainFrame::OnWindowMenu2 (wxCommandEvent& event)
427 { DoWindowMenu (2, event); }
428
429 void MainFrame::OnWindowMenu3 (wxCommandEvent& event)
430 { DoWindowMenu (3, event); }
431
432 void MainFrame::OnWindowMenu4 (wxCommandEvent& event)
433 { DoWindowMenu (4, event); }
434
435 void MainFrame::OnWindowMenu5 (wxCommandEvent& event)
436 { DoWindowMenu (5, event); }
437
438 void MainFrame::OnWindowMenu6 (wxCommandEvent& event)
439 { DoWindowMenu (6, event); }
440
441 void MainFrame::OnWindowMenu7 (wxCommandEvent& event)
442 { DoWindowMenu (7, event); }
443
444 void MainFrame::OnWindowMenu8 (wxCommandEvent& event)
445 { DoWindowMenu (8, event); }
446
447 void MainFrame::OnWindowMenu9 (wxCommandEvent& event)
448 { DoWindowMenu (9, event); }
449
450 void MainFrame::OnWindowMenu10 (wxCommandEvent& event)
451 { DoWindowMenu (10, event); }
452
453 void MainFrame::OnWindowMenu11 (wxCommandEvent& event)
454 { DoWindowMenu (11, event); }
455
456 void MainFrame::OnWindowMenu12 (wxCommandEvent& event)
457 { DoWindowMenu (12, event); }
458
459 void MainFrame::OnWindowMenu13 (wxCommandEvent& event)
460 { DoWindowMenu (13, event); }
461
462 void MainFrame::OnWindowMenu14 (wxCommandEvent& event)
463 { DoWindowMenu (14, event); }
464
465 void MainFrame::OnWindowMenu15 (wxCommandEvent& event)
466 { DoWindowMenu (15, event); }
467
468 void MainFrame::OnWindowMenu16 (wxCommandEvent& event)
469 { DoWindowMenu (16, event); }
470
471 void MainFrame::OnWindowMenu17 (wxCommandEvent& event)
472 { DoWindowMenu (17, event); }
473
474 void MainFrame::OnWindowMenu18 (wxCommandEvent& event)
475 { DoWindowMenu (18, event); }
476
477 void MainFrame::OnWindowMenu19 (wxCommandEvent& event)
478 { DoWindowMenu (19, event); }
479
480