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.21 2001/03/05 17:26:46 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)
58 : 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), m_strLabel(pszLabel),
66 ReconstructorSupervisorThread::Entry()
68 ReconstructorSupervisor reconSupervisor (this, m_pProjView, m_iNX, m_iNY,
69 m_strFilterName.c_str(), m_dFilterParam, m_strFilterMethod.c_str(), m_iZeropad, m_strFilterGenerationName.c_str(),
70 m_strInterpName.c_str(), m_iInterpParam, m_strBackprojectName.c_str(), m_strLabel.c_str());
72 reconSupervisor.start();
73 while (! reconSupervisor.workersDone() && ! reconSupervisor.fail() && ! reconSupervisor.cancelled()) {
76 if (reconSupervisor.fail())
78 wxString msg ("Error starting reconstructor supervisor: ");
79 msg += reconSupervisor.getFailMessage().c_str();
81 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
82 eventLog.SetString( msg );
83 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
85 if (! reconSupervisor.cancelled())
86 reconSupervisor.onDone();
87 reconSupervisor.deleteWorkers();
89 return reinterpret_cast<wxThread::ExitCode>(0);
93 ReconstructorSupervisorThread::OnExit()
98 /////////////////////////////////////////////////////////////////////
100 // Class ReconstructorSupervisor -- A Background Supervisor
102 /////////////////////////////////////////////////////////////////////
104 ReconstructorSupervisor::ReconstructorSupervisor (SupervisorThread* pThread, ProjectionFileView* pProjView,
105 int iImageNX, int iImageNY, const char* pszFilterName, double dFilterParam, const char* pszFilterMethod,
106 int iZeropad, const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
107 const char* pszBackprojectName, const char* const pszLabel)
108 : m_pProjView(pProjView), m_pProjDoc(pProjView->GetDocument()),
109 m_iImageNX(iImageNX), m_iImageNY(iImageNY),
110 m_pszFilterName(pszFilterName), m_dFilterParam(dFilterParam), m_pszFilterMethod(pszFilterMethod),
111 m_iZeropad(iZeropad), m_pszFilterGenerationName(pszFilterGenerationName), m_pszInterpName(pszInterpName),
112 m_iInterpParam(iInterpParam), m_pszBackprojectName(pszBackprojectName), m_pszLabel(pszLabel),
113 BackgroundSupervisor (pThread, pProjView->GetFrame(), pProjView->GetDocument(), "Reconstructing", pProjView->GetDocument()->getProjections().nView())
115 m_vecpChildImageFile.reserve (getNumWorkers());
116 for (int iThread = 0; iThread < getNumWorkers(); iThread++) {
117 m_vecpChildImageFile[iThread] = new ImageFile (m_iImageNX, m_iImageNY);
122 ReconstructorSupervisor::~ReconstructorSupervisor()
124 for (int i = 0; i < getNumWorkers(); i++) {
125 delete m_vecpChildImageFile[i];
126 m_vecpChildImageFile[i] = NULL;
130 BackgroundWorkerThread*
131 ReconstructorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
133 ReconstructorWorker* pThread = new ReconstructorWorker (this, iThread, iStartUnit, iNumUnits);
134 pThread->SetParameters (m_pProjView, m_vecpChildImageFile[iThread], m_pszFilterName, m_dFilterParam,
135 m_pszFilterMethod, m_iZeropad, m_pszFilterGenerationName, m_pszInterpName,
136 m_iInterpParam, m_pszBackprojectName);
142 ReconstructorSupervisor::onDone()
144 wxCriticalSection doneSection;
145 wxCriticalSectionLocker critsect (doneSection);
147 ImageFile* pImageFile = getImageFile();
148 pImageFile->labelAdd (m_pProjView->GetDocument()->getProjections().getLabel());
149 pImageFile->labelAdd (m_pszLabel, getTimerEnd());
151 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
152 wxString msg (m_pszLabel);
154 eventLog.SetString( msg );
155 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
157 wxCommandEvent newImageEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_IMAGEFILE_EVENT);
158 newImageEvent.SetClientData (pImageFile);
159 wxPostEvent (theApp->getMainFrame(), newImageEvent);
166 ReconstructorSupervisor::getImageFile()
168 ImageFile* pImageFile = new ImageFile (m_iImageNX, m_iImageNY);
169 pImageFile->arrayDataClear();
170 ImageFileArray pArray = pImageFile->getArray();
173 for (i = 0; i < getNumWorkers(); i++) {
174 ImageFileArrayConst pChildArray = m_vecpChildImageFile[i]->getArray();
175 for (int ix = 0; ix < m_iImageNX; ix++)
176 for (int iy = 0; iy < m_iImageNY; iy++)
177 pArray[ix][iy] += pChildArray[ix][iy];
184 /////////////////////////////////////////////////////////////////////
186 // Class ReconstructorWorker -- A worker thread
188 /////////////////////////////////////////////////////////////////////
191 ReconstructorWorker::SetParameters (ProjectionFileView* pProjView, ImageFile* pImageFile,
192 const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, int iZeropad,
193 const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
194 const char* pszBackprojectName)
196 m_pProjView = pProjView;
197 m_pImageFile = pImageFile;
198 m_pszFilterName = pszFilterName;
199 m_dFilterParam = dFilterParam;
200 m_pszFilterMethod = pszFilterMethod;
201 m_iZeropad = iZeropad;
202 m_pszFilterGenerationName = pszFilterGenerationName;
203 m_pszInterpName = pszInterpName;
204 m_iInterpParam = iInterpParam;
205 m_pszBackprojectName = pszBackprojectName;
209 ReconstructorWorker::Entry ()
211 Reconstructor* pReconstructor = new Reconstructor (m_pProjView->GetDocument()->getProjections(),
212 *m_pImageFile, m_pszFilterName, m_dFilterParam, m_pszFilterMethod, m_iZeropad,
213 m_pszFilterGenerationName, m_pszInterpName, m_iInterpParam, m_pszBackprojectName, Trace::TRACE_NONE);
215 bool bFail = pReconstructor->fail();
218 failMsg = "Unable to make reconstructor: ";
219 failMsg += pReconstructor->failMessage().c_str();
220 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
221 event.SetString( failMsg.c_str() );
222 wxPostEvent( theApp->getMainFrame(), event );
226 wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
227 for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
230 if (theApp->getVerboseLogging()) {
232 msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);
233 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
234 event.SetString( msg );
235 wxPostEvent( theApp->getMainFrame(), event );
240 pReconstructor->reconstructView (iUnit + m_iStartUnit, 1);
241 m_pSupervisor->onWorkerUnitTick();
243 pReconstructor->postProcessing();
245 delete pReconstructor;
248 m_pSupervisor->onWorkerFail (m_iThread, failMsg);
250 m_pSupervisor->onWorkerDone (m_iThread);
253 while (! TestDestroy())
256 return reinterpret_cast<wxThread::ExitCode>(0);
260 ReconstructorWorker::OnExit ()
264 #endif // HAVE_WXTHREADS