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