r573: no message
[ctsim.git] / src / threadrecon.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          threadrecon.cpp
5 **   Purpose:       Threaded reconstruction class
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.cpp,v 1.3 2001/02/22 18:22:40 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 #include "wx/wxprec.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/wx.h"
32 #endif
33
34 #include "ct.h"
35 #include "ctsim.h"
36 #include "docs.h"
37 #include "views.h"
38 #include "threadrecon.h"
39 #include "backgroundmgr.h"
40
41 #if defined(HAVE_CONFIG_H)
42 #include "config.h"
43 #endif
44
45
46 IMPLEMENT_DYNAMIC_CLASS(ThreadedReconstructor, BackgroundTask)
47 BEGIN_EVENT_TABLE(ThreadedReconstructor, BackgroundTask)
48 EVT_MENU(RECONSTRUCTION_THREAD_EVENT, ThreadedReconstructor::OnThreadEvent)
49 END_EVENT_TABLE()
50
51
52 enum {
53   RTHREAD_UNIT_COMPLETE = -1,
54     RTHREAD_THREAD_DONE = -2,
55     RTHREAD_THREAD_CANCELLED = -3,
56 };
57
58 ThreadedReconstructor::ThreadedReconstructor (ProjectionFileView* pProjView, 
59                                               int iImageNX, int iImageNY, const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, 
60                                               int iZeropad, const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
61                                               const char* pszBackprojectName, const char* const pszLabel)
62                                               : m_pProjView(pProjView), m_pDialogProgress(NULL), m_pGauge(NULL), m_bFail(false), m_iNumThreads(0), m_iImageNX(iImageNX), 
63                                               m_iImageNY(iImageNY), m_strLabel(pszLabel), m_pTimer(NULL), m_bCancelled(false), m_bCancelling(false), 
64                                               BackgroundTask()
65 {
66   m_iNumThreads = theApp->getNumberCPU();
67   //  ++m_iNumThreads;
68   m_iTotalViews = m_pProjView->GetDocument()->getProjections().nView();
69   int iBaseViews = m_iTotalViews / m_iNumThreads;
70   int iExtraViews = m_iTotalViews % m_iNumThreads;
71   
72   m_vecpChildImageFile.reserve (m_iNumThreads);
73   m_vecpReconstructor.reserve (m_iNumThreads);
74   m_vecpThread.reserve (m_iNumThreads);
75   
76   for (unsigned int iProc = 0; iProc < m_iNumThreads; iProc++) {
77     m_vecpChildImageFile[iProc] = new ImageFile (iImageNX, iImageNY);
78     m_vecpReconstructor[iProc] = new Reconstructor (m_pProjView->GetDocument()->getProjections(), *m_vecpChildImageFile[iProc],
79       pszFilterName, dFilterParam, pszFilterMethod, iZeropad, pszFilterGenerationName, 
80       pszInterpName, iInterpParam, pszBackprojectName, Trace::TRACE_NONE);
81     
82     int iStartView = iProc * iBaseViews;
83     int iNumViews = iBaseViews;
84     if (iProc < iExtraViews)
85       ++iNumViews;
86     m_vecpThread[iProc] = new ReconstructionThread (this, m_vecpReconstructor[iProc], iProc, iStartView, iNumViews);
87     if (m_vecpThread[iProc]->Create () != wxTHREAD_NO_ERROR) {
88       m_bFail = true;
89       break;
90     }
91   }
92   
93 }
94
95
96 bool
97 ThreadedReconstructor::start()
98 {
99   if (m_bFail)
100     return false;
101   
102   m_pProjView->GetDocument()->addReconstructor (this);
103   if (! theApp->getUseBackgroundTasks())
104     m_pDialogProgress = new wxProgressDialog (_T("Filtered Backprojection"), _T("Reconstruction Progress"), m_iTotalViews, m_pProjView->getFrame(), wxPD_CAN_ABORT | wxPD_AUTO_HIDE);
105   else
106     m_pGauge = theApp->getBackgroundManager()->addTask (this, m_iTotalViews, m_pProjView->GetFrame()->GetTitle());
107   
108   m_iRunning = m_iNumThreads;
109   m_iViewsDone = 0;
110   m_pTimer = new Timer;
111   
112   // starting all threads
113   for (int i = 0; i < m_iNumThreads; i++)
114     m_vecpThread[i]->Run();
115   
116   if (m_bCancelled)
117     return false;
118   
119   return true;
120 }
121
122 void
123 ThreadedReconstructor::cancel()
124 {
125   if (isDone() || m_bCancelled)
126     return;
127   m_bCancelled = true;
128   m_bCancelling = false;
129   cleanUp();
130 }
131
132 void
133 ThreadedReconstructor::cleanUp()
134 {
135   wxCriticalSection cleanSection;
136   cleanSection.Enter();
137   
138   for (int i = 0; i < m_iNumThreads; i++) 
139     if (m_vecpThread[i] && m_vecpThread[i]->IsRunning()) {
140       m_vecpThread[i]->Pause();
141       m_vecpThread[i]->Delete();
142     }
143     
144     for (i = 0; i < m_iNumThreads; i++) {
145       delete m_vecpChildImageFile[i];
146       m_vecpChildImageFile[i] = NULL;
147     }
148     for (i = 0; i < m_iNumThreads; i++) {
149       delete m_vecpReconstructor[i];
150       m_vecpReconstructor[i] = NULL;
151     }
152     
153     
154     m_iNumThreads = 0;
155     m_iRunning = 0;
156     delete m_pDialogProgress;
157     delete m_pTimer;
158     m_pDialogProgress = NULL;
159     m_pGauge = NULL;
160     setDone();
161     m_pProjView->GetDocument()->removeReconstructor (this);
162     theApp->getBackgroundManager()->taskDone (this);
163     
164     cleanSection.Leave();
165 }
166
167 void
168 ThreadedReconstructor::onDone()
169 {
170   wxCriticalSection doneSection;
171   doneSection.Enter();
172
173   m_pProjView->GetDocument()->removeReconstructor (this);
174   ImageFileDocument* pReconDoc = theApp->newImageDoc();
175   if (! pReconDoc) {
176     sys_error (ERR_SEVERE, "Unable to create image file");
177     doneSection.Leave();
178     return;
179   }
180   
181   for (int i = 0; i < m_iNumThreads; i++) {
182     delete m_vecpReconstructor[i];
183     m_vecpReconstructor[i] = NULL;
184   }
185   
186   ImageFile* pImageFile = getImageFile();
187   pReconDoc->setImageFile (pImageFile);
188   if (theApp->getAskDeleteNewDocs())
189     pReconDoc->Modify (true);
190   pReconDoc->UpdateAllViews (m_pProjView);
191   if (ImageFileView* rasterView = pReconDoc->getView()) {
192     rasterView->OnUpdate (rasterView, NULL);
193     rasterView->getFrame()->SetFocus();
194     rasterView->getFrame()->Show(true);
195   }
196   *theApp->getLog() << m_strLabel << "\n";
197   pImageFile->labelAdd (m_pProjView->GetDocument()->getProjections().getLabel());
198   pImageFile->labelAdd (m_strLabel.c_str(), m_pTimer->timerEnd());
199   
200   doneSection.Leave();
201   cleanUp();
202 }
203
204
205 void
206 ThreadedReconstructor::OnThreadEvent (wxCommandEvent& event)
207 {
208   if (isDone())
209     return;
210   wxCriticalSection eventSection;
211   eventSection.Enter();
212   if (isDone()) {
213     eventSection.Leave();
214     return;
215   }
216   
217   int iEventId = event.GetInt();
218   if (iEventId == RTHREAD_UNIT_COMPLETE) {
219     ++m_iViewsDone;
220     
221 #ifdef DEBUG
222     if (theApp->getVerboseLogging())
223       *theApp->getLog() << "Views done: " << static_cast<int>(m_iViewsDone) <<"\n";
224 #endif
225     
226     if (m_pDialogProgress)
227       m_bCancelling = ! m_pDialogProgress->Update (m_iViewsDone - 1);
228     else if (m_pGauge) {
229       m_pGauge->SetValue (m_iViewsDone - 1); 
230       eventSection.Leave();
231       m_bCancelling = theApp->getBackgroundManager()->isCancelling(this);
232     }
233     if (! isDone() && m_bCancelling) {
234       eventSection.Leave();
235       cancel();
236       return;
237     }
238   } 
239   else if (event.GetInt() >= 0) {
240     m_iRunning--;
241     m_vecpThread[event.GetInt()] = NULL;
242     *theApp->getLog() << "Thread finished. Remaining threads: " << m_iRunning << "\n";
243     if (m_iRunning <= 0) {
244       eventSection.Leave();
245       onDone();
246       return;
247     }
248   }
249   else
250     *theApp->getLog() << "Got event #" << iEventId << "\n";
251   
252   eventSection.Leave();
253 }
254
255 ImageFile*
256 ThreadedReconstructor::getImageFile()
257 {
258   ImageFile* pImageFile = new ImageFile (m_iImageNX, m_iImageNY);
259   pImageFile->arrayDataClear();
260   ImageFileArray pArray = pImageFile->getArray();
261   
262   int i;
263   for (i = 0; i < m_iNumThreads; i++) {
264     ImageFileArrayConst pChildArray = m_vecpChildImageFile[i]->getArray();
265     for (int ix = 0; ix < m_iImageNX; ix++)
266       for (int iy = 0; iy < m_iImageNY; iy++)
267         pArray[ix][iy] += pChildArray[ix][iy];
268   }
269   
270   return (pImageFile);
271 }
272
273 bool
274 ThreadedReconstructor::testDone()
275 {
276   return (m_iRunning <= 0 ? true : false);
277 }
278
279
280 ThreadedReconstructor::~ThreadedReconstructor()
281 {
282 }
283
284
285 ReconstructionThread::ReconstructionThread (ThreadedReconstructor* pSupervisor, 
286                                             Reconstructor* pReconstructor, int iThread, int iStartView, int iNumViews)
287                                             : m_pSupervisor(pSupervisor), m_pReconstructor(pReconstructor), 
288                                             m_iStartView(iStartView), m_iNumViews(iNumViews), m_iThread(iThread), 
289                                             wxThread(wxTHREAD_DETACHED)
290 {
291 }
292
293 wxThread::ExitCode
294 ReconstructionThread::Entry ()
295 {
296   wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, RECONSTRUCTION_THREAD_EVENT);
297   for (int iView = 0; iView < m_iNumViews; iView++) {
298     if (TestDestroy()) {
299       wxString msg;
300       msg.Printf("TestDestroy TRUE at view #%d\n", iView);  
301       wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
302       event.SetString( msg );
303       wxPostEvent( theApp->getMainFrame(), event ); // send in a thread-safe way
304       return reinterpret_cast<wxThread::ExitCode>(RTHREAD_THREAD_CANCELLED);
305     }
306     m_pReconstructor->reconstructView (iView + m_iStartView, 1);
307     eventProgress.SetInt (RTHREAD_UNIT_COMPLETE);
308     wxPostEvent (m_pSupervisor, eventProgress);
309   }
310   
311   eventProgress.SetInt (m_iThread); // Send back thread# that has finished
312   wxPostEvent (m_pSupervisor, eventProgress);
313   
314   return reinterpret_cast<wxThread::ExitCode>(0);
315 }
316
317 void
318 ReconstructionThread::OnExit ()
319 {
320 }