r577: no message
[ctsim.git] / src / threadrecon.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          threadrecon.h
5 **   Purpose:       Header file for thread reconstructions
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  February 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: threadrecon.h,v 1.7 2001/02/23 21:58:32 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 #ifndef _THREADRECON_H
29 #define _THREADRECON_H
30
31 #include <vector>
32 #include <wx/thread.h>
33 #include <wx/progdlg.h>
34 #include "timer.h"
35
36 // This thread creates a SupervisorTask event handler object
37 // The thread is detached and terminates when SupervisorTask terminates
38 class SupervisorTaskThread : public wxThread {
39 private:
40
41 public:
42   SupervisorTaskThread();
43
44   virtual wxThread::ExitCode Entry() = 0;
45
46   // called when the thread exits - whether it terminates normally or is stopped with Delete()
47   virtual void OnExit();
48 };
49
50
51 class ReconstructorSupervisorThread : public SupervisorTaskThread {
52 private:
53 public:
54 };
55
56
57 // Pure virtual class for BackgroundSupervisor that can communication
58 // with BackgroundManager via messages
59
60 class BackgroundSupervisor : public wxEvtHandler {
61 private:
62   bool m_bDone;
63
64 public:
65
66   enum {
67     MSG_BACKGROUND_SUPERVISOR_ADD = 7500, // sends to BackgroundManager and Document
68     MSG_BACKGROUND_SUPERVISOR_REMOVE = 7501, // sends to BackgroundManager and Document
69     MSG_BACKGROUND_SUPERVISOR_UNIT_TICK = 7502,  // sends to BackgroundManager for progress bars
70     MSG_BACKGROUND_SUPERVISOR_CANCEL = 7503,   // *sent* to Supervisor to cancel process
71
72     MSG_WORKER_THREAD_UNIT_TICK = 7504,
73     MSG_WORKER_THREAD_DONE = 7505,
74     MSG_WORKER_THREAD_FAIL = 7506,   // sent by workers when they fail
75   };
76
77   BackgroundSupervisor()
78     : m_bDone(false), wxEvtHandler()
79   {}
80
81   virtual ~BackgroundSupervisor()
82   {}
83
84   virtual bool start() = 0;
85   virtual bool testDone() = 0;
86   virtual void OnWorkerFail(wxCommandEvent& event) = 0;
87   virtual void OnWorkerUnitTick(wxCommandEvent& event) = 0;
88   virtual void OnWorkerDone(wxCommandEvent& event) = 0;
89   virtual void OnCancel(wxCommandEvent& event) = 0;
90
91   bool isDone() const {return m_bDone;}
92   void setDone() { m_bDone = true; }
93
94   static void cancelSupervisor (BackgroundSupervisor* pSupervisor);
95 };
96
97 class Reconstructor;
98 class ImageFile;
99 class ProjectionFileDocument;
100 class ReconstructorWorker;
101 class ProjectionFileView;
102
103
104 class ReconstructorSupervisor : public BackgroundSupervisor {
105 private:
106   DECLARE_DYNAMIC_CLASS(ReconstructorSupervisor)
107   wxCriticalSection m_critsectThreadContainer;
108
109   std::vector<ImageFile*> m_vecpChildImageFile;
110   typedef std::vector<ReconstructorWorker*> ThreadContainer;
111   ThreadContainer m_vecpThread;
112   ProjectionFileDocument* m_pProjDoc;
113   ProjectionFileView* m_pProjView;
114   wxProgressDialog* m_pDialogProgress;
115     
116   volatile bool m_bFail;
117   int m_iNumThreads;
118   const int m_iImageNX;
119   const int m_iImageNY;
120   volatile int m_iRunning;
121   volatile unsigned int m_iViewsDone;
122   volatile unsigned int m_iTotalViews;
123   wxString m_strLabel;
124   Timer* m_pTimer;
125   bool m_bCancelled;
126   bool m_bCancelling;
127
128 public:
129    ReconstructorSupervisor (ProjectionFileView* pProjView, 
130    int iNX, int iNY, const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, 
131    int iZeropad, const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
132    const char* pszBackprojectName, const char* const pszLabel);
133
134    ReconstructorSupervisor ()
135      : m_bFail(true), m_iNumThreads(0), m_iImageNX(0), m_iImageNY(0), m_iTotalViews(0), 
136      BackgroundSupervisor()
137    {}
138
139    ~ReconstructorSupervisor ();
140
141   void onDone();
142   bool start();
143   bool fail() const {return m_bFail;}
144   bool testDone();
145   void cleanUp();
146   virtual void OnWorkerFail(wxCommandEvent& event);
147   virtual void OnWorkerUnitTick(wxCommandEvent& event);
148   virtual void OnWorkerDone(wxCommandEvent& event);
149   virtual void OnCancel(wxCommandEvent& event);
150
151   ImageFile* getImageFile();
152
153   DECLARE_EVENT_TABLE()
154 };
155
156
157
158 class ReconstructorWorker : public wxThread {
159 private:
160   ReconstructorSupervisor* m_pSupervisor;
161   int m_iStartView;
162   int m_iNumViews;
163   int m_iThread;
164   ProjectionFileView* m_pProjView;
165   ImageFile* m_pImageFile;
166   const std::string m_strFilterName;
167   const double m_dFilterParam;
168   const std::string m_strFilterMethod;
169   const int m_iZeropad;
170   const std::string m_strFilterGenerationName;
171   const std::string m_strInterpName;
172   const int m_iInterpParam;
173   const std::string m_strBackprojectName;
174
175 public:
176   ReconstructorWorker (ReconstructorSupervisor* pSupervisor, 
177     ProjectionFileView* pProjFile, ImageFile* pImageFile, 
178     int iThread, int iStartView, int iNumViews,
179    const char* const pszFilterName, double dFilterParam, const char* const pszFilterMethod, 
180    int iZeropad, const char* const pszFilterGenerationName, const char* const pszInterpName, int iInterpParam,
181    const char* pszBackprojectName);
182
183   virtual wxThread::ExitCode Entry();      // thread execution starts here
184
185   // called when the thread exits - whether it terminates normally or is stopped with Delete()
186   virtual void OnExit();
187 };
188
189
190 #endif
191