1 /*****************************************************************************
4 ** Name: threadrecon.cpp
5 ** Purpose: Threaded reconstruction class
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: February 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2001 Kevin Rosenberg
12 ** $Id: threadrecon.cpp,v 1.25 2001/03/13 08:24:41 kevin Exp $
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.
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.
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 ******************************************************************************/
29 #include "wx/wxprec.h"
39 #include "threadrecon.h"
40 #include "backgroundmgr.h"
41 #include "backgroundsupr.h"
48 /////////////////////////////////////////////////////////////////////
50 // Class ReconstructorSupervisorThread -- Thread for Background Supervisor
52 /////////////////////////////////////////////////////////////////////
54 ReconstructorSupervisorThread::ReconstructorSupervisorThread (ProjectionFileView* pProjView, int iNX, int iNY,
55 const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, int iZeropad,
56 const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
57 const char* pszBackprojectName, const char* const pszLabel, ReconstructionROI* pROI, bool bRebinToParallel)
58 : SupervisorThread(), m_pProjView(pProjView), m_iNX(iNX), m_iNY(iNY), m_strFilterName(pszFilterName), m_dFilterParam(dFilterParam),
59 m_strFilterMethod(pszFilterMethod), m_iZeropad(iZeropad), m_strFilterGenerationName(pszFilterGenerationName),
60 m_strInterpName(pszInterpName), m_iInterpParam(iInterpParam), m_strBackprojectName(pszBackprojectName),
61 m_strLabel(pszLabel), m_reconROI(*pROI), m_bRebinToParallel(bRebinToParallel)
66 ReconstructorSupervisorThread::Entry()
68 Projections* pProj = &m_pProjView->GetDocument()->getProjections();
70 if (m_bRebinToParallel)
71 pProj = pProj->interpolateToParallel();
73 ReconstructorSupervisor reconSupervisor (this, pProj, m_pProjView, m_iNX, m_iNY,
74 m_strFilterName.c_str(), m_dFilterParam, m_strFilterMethod.c_str(), m_iZeropad, m_strFilterGenerationName.c_str(),
75 m_strInterpName.c_str(), m_iInterpParam, m_strBackprojectName.c_str(), m_strLabel.c_str(), &m_reconROI);
77 reconSupervisor.start();
78 while (! reconSupervisor.workersDone() && ! reconSupervisor.fail() && ! reconSupervisor.cancelled()) {
81 if (reconSupervisor.fail())
83 wxString msg ("Error starting reconstructor supervisor: ");
84 msg += reconSupervisor.getFailMessage().c_str();
86 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
87 eventLog.SetString( msg );
88 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
90 if (! reconSupervisor.cancelled())
91 reconSupervisor.onDone();
92 reconSupervisor.deleteWorkers();
94 if (m_bRebinToParallel)
97 return static_cast<wxThread::ExitCode>(0);
101 ReconstructorSupervisorThread::OnExit()
106 /////////////////////////////////////////////////////////////////////
108 // Class ReconstructorSupervisor -- A Background Supervisor
110 /////////////////////////////////////////////////////////////////////
112 ReconstructorSupervisor::ReconstructorSupervisor (SupervisorThread* pThread, Projections* pProj,
113 ProjectionFileView* pProjView, int iImageNX, int iImageNY, const char* pszFilterName, double dFilterParam,
114 const char* pszFilterMethod, int iZeropad, const char* pszFilterGenerationName,
115 const char* pszInterpName, int iInterpParam, const char* pszBackprojectName, const char* const pszLabel,
116 ReconstructionROI* pROI)
117 : BackgroundSupervisor (pThread, pProjView->GetFrame(), pProjView->GetDocument(), "Reconstructing", pProjView->GetDocument()->getProjections().nView()),
118 m_pProj(pProj), m_pProjView(pProjView), m_pProjDoc(pProjView->GetDocument()),
119 m_iImageNX(iImageNX), m_iImageNY(iImageNY),
120 m_pszFilterName(pszFilterName), m_dFilterParam(dFilterParam), m_pszFilterMethod(pszFilterMethod),
121 m_iZeropad(iZeropad), m_pszFilterGenerationName(pszFilterGenerationName), m_pszInterpName(pszInterpName),
122 m_iInterpParam(iInterpParam), m_pszBackprojectName(pszBackprojectName), m_pszLabel(pszLabel),
125 m_vecpChildImageFile.reserve (getNumWorkers());
126 for (int iThread = 0; iThread < getNumWorkers(); iThread++) {
127 m_vecpChildImageFile[iThread] = new ImageFile (m_iImageNX, m_iImageNY);
132 ReconstructorSupervisor::~ReconstructorSupervisor()
134 for (int i = 0; i < getNumWorkers(); i++) {
135 delete m_vecpChildImageFile[i];
136 m_vecpChildImageFile[i] = NULL;
140 BackgroundWorkerThread*
141 ReconstructorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
143 ReconstructorWorker* pThread = new ReconstructorWorker (this, iThread, iStartUnit, iNumUnits);
144 pThread->SetParameters (m_pProj, m_pProjView, m_vecpChildImageFile[iThread], m_pszFilterName,
145 m_dFilterParam, m_pszFilterMethod, m_iZeropad, m_pszFilterGenerationName, m_pszInterpName,
146 m_iInterpParam, m_pszBackprojectName, m_pReconROI);
152 ReconstructorSupervisor::onDone()
154 wxCriticalSection doneSection;
155 wxCriticalSectionLocker critsect (doneSection);
157 ImageFile* pImageFile = getImageFile();
158 pImageFile->labelAdd (m_pProj->getLabel());
159 pImageFile->labelAdd (m_pszLabel, getTimerEnd());
161 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
162 wxString msg (m_pszLabel);
164 eventLog.SetString( msg );
165 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
167 wxCommandEvent newImageEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_IMAGEFILE_EVENT);
168 newImageEvent.SetClientData (pImageFile);
169 wxPostEvent (theApp->getMainFrame(), newImageEvent);
176 ReconstructorSupervisor::getImageFile()
178 ImageFile* pImageFile = new ImageFile (m_iImageNX, m_iImageNY);
179 pImageFile->arrayDataClear();
180 ImageFileArray pArray = pImageFile->getArray();
183 for (i = 0; i < getNumWorkers(); i++) {
184 ImageFileArrayConst pChildArray = m_vecpChildImageFile[i]->getArray();
185 for (int ix = 0; ix < m_iImageNX; ix++)
186 for (int iy = 0; iy < m_iImageNY; iy++)
187 pArray[ix][iy] += pChildArray[ix][iy];
194 /////////////////////////////////////////////////////////////////////
196 // Class ReconstructorWorker -- A worker thread
198 /////////////////////////////////////////////////////////////////////
201 ReconstructorWorker::SetParameters (const Projections* pProj, ProjectionFileView* pProjView, ImageFile* pImageFile,
202 const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, int iZeropad,
203 const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
204 const char* pszBackprojectName, ReconstructionROI* pROI)
207 m_pProjView = pProjView;
208 m_pImageFile = pImageFile;
209 m_pszFilterName = pszFilterName;
210 m_dFilterParam = dFilterParam;
211 m_pszFilterMethod = pszFilterMethod;
212 m_iZeropad = iZeropad;
213 m_pszFilterGenerationName = pszFilterGenerationName;
214 m_pszInterpName = pszInterpName;
215 m_iInterpParam = iInterpParam;
216 m_pszBackprojectName = pszBackprojectName;
221 ReconstructorWorker::Entry ()
223 Reconstructor* pReconstructor = new Reconstructor (*m_pProj, *m_pImageFile, m_pszFilterName,
224 m_dFilterParam, m_pszFilterMethod, m_iZeropad, m_pszFilterGenerationName, m_pszInterpName,
225 m_iInterpParam, m_pszBackprojectName, Trace::TRACE_NONE, m_pReconROI, false);
227 bool bFail = pReconstructor->fail();
230 failMsg = "Unable to make reconstructor: ";
231 failMsg += pReconstructor->failMessage().c_str();
232 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
233 event.SetString( failMsg.c_str() );
234 wxPostEvent( theApp->getMainFrame(), event );
238 wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
239 for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
242 if (theApp->getVerboseLogging()) {
244 msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);
245 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
246 event.SetString( msg );
247 wxPostEvent( theApp->getMainFrame(), event );
252 pReconstructor->reconstructView (iUnit + m_iStartUnit, 1);
253 m_pSupervisor->onWorkerUnitTick();
255 pReconstructor->postProcessing();
257 delete pReconstructor;
260 m_pSupervisor->onWorkerFail (m_iThread, failMsg);
262 m_pSupervisor->onWorkerDone (m_iThread);
265 while (! TestDestroy())
268 return reinterpret_cast<wxThread::ExitCode>(0);
272 ReconstructorWorker::OnExit ()
276 #endif // HAVE_WXTHREADS