1 /*****************************************************************************
4 ** Name: threadproj.cpp
5 ** Purpose: Threaded projection 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
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 ******************************************************************************/
28 #include "wx/wxprec.h"
34 #if defined(HAVE_CONFIG_H)
42 #include "threadproj.h"
43 #include "backgroundmgr.h"
44 #include "backgroundsupr.h"
51 /////////////////////////////////////////////////////////////////////
53 // Class ProjectorSupervisorThread -- Thread for Background Supervisor
55 /////////////////////////////////////////////////////////////////////
57 ProjectorSupervisorThread::ProjectorSupervisorThread (PhantomFileView* pProjView, int iNDet, int iNView, int iOffsetView,
58 const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dCenterDetectorLength,
59 double dViewRatio, double dScanRatio, const char* const pszLabel)
60 : SupervisorThread(), m_pPhantomView(pProjView), m_iNDet(iNDet), m_iNView(iNView), m_iOffsetView(iOffsetView), m_strGeometry(pszGeometry),
61 m_iNSample(iNSample), m_dRotation(dRotation), m_dFocalLength(dFocalLength), m_dCenterDetectorLength(dCenterDetectorLength),
62 m_dViewRatio(dViewRatio), m_dScanRatio(dScanRatio), m_strLabel(pszLabel)
67 ProjectorSupervisorThread::Entry()
69 ProjectorSupervisor projSupervisor (this, m_pPhantomView, m_iNDet, m_iNView, m_iOffsetView,
70 m_strGeometry.c_str(), m_iNSample, m_dRotation, m_dFocalLength, m_dCenterDetectorLength, m_dViewRatio, m_dScanRatio, m_strLabel.c_str());
72 projSupervisor.start();
73 while (! projSupervisor.workersDone() && ! projSupervisor.fail() && ! projSupervisor.cancelled()) {
76 if (projSupervisor.fail())
78 wxString msg ("Error starting Projector supervisor: ");
79 msg += projSupervisor.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
86 if (! projSupervisor.cancelled())
87 projSupervisor.onDone();
88 projSupervisor.deleteWorkers();
90 return static_cast<wxThread::ExitCode>(0);
94 ProjectorSupervisorThread::OnExit()
99 /////////////////////////////////////////////////////////////////////
101 // Class ProjectorSupervisor -- A Background Supervisor
103 /////////////////////////////////////////////////////////////////////
105 ProjectorSupervisor::ProjectorSupervisor (SupervisorThread* pThread, PhantomFileView* pPhantomView, int iNDet, int iNView, int iOffsetView,
106 const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dCenterDetectorLength,
107 double dViewRatio, double dScanRatio, const char* const pszLabel)
108 : BackgroundSupervisor (pThread, pPhantomView->GetFrame(), pPhantomView->GetDocument(), "Projecting", iNView),
109 m_pPhantomView(pPhantomView), m_pPhantomDoc(pPhantomView->GetDocument()),
110 m_iNDet(iNDet), m_iNView(iNView), m_iOffsetView(iOffsetView), m_pszGeometry(pszGeometry), m_iNSample(iNSample),
111 m_dRotation(dRotation), m_dFocalLength(dFocalLength), m_dCenterDetectorLength(dCenterDetectorLength),
112 m_dViewRatio(dViewRatio), m_dScanRatio(dScanRatio), m_pszLabel(pszLabel)
114 m_pScanner = new Scanner (m_pPhantomDoc->getPhantom(), m_pszGeometry, m_iNDet,
115 m_iNView, m_iOffsetView, m_iNSample, m_dRotation, m_dFocalLength, m_dCenterDetectorLength, m_dViewRatio, m_dScanRatio);
117 m_vecpChildProjections.reserve (getNumWorkers());
118 for (int iThread = 0; iThread < getNumWorkers(); iThread++) {
119 m_vecpChildProjections[iThread] = new Projections (*m_pScanner);
126 ProjectorSupervisor::~ProjectorSupervisor()
128 for (int i = 0; i < getNumWorkers(); i++) {
129 delete m_vecpChildProjections[i];
130 m_vecpChildProjections[i] = NULL;
136 BackgroundWorkerThread*
137 ProjectorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
139 ProjectorWorker* pThread = new ProjectorWorker (this, iThread, iStartUnit, iNumUnits);
140 m_vecpChildProjections[iThread]->setNView (iNumUnits);
141 pThread->SetParameters (m_pPhantomView, m_vecpChildProjections[iThread], m_pScanner, m_iNDet, m_iNView, m_iOffsetView,
142 m_pszGeometry, m_iNSample, m_dRotation, m_dFocalLength, m_dCenterDetectorLength, m_dViewRatio, m_dScanRatio);
148 ProjectorSupervisor::onDone()
150 wxCriticalSection doneSection;
151 wxCriticalSectionLocker critsect (doneSection);
153 Projections* pProjections = getProjections();
154 pProjections->setRemark (m_pszLabel);
155 pProjections->setCalcTime (getTimerEnd());
157 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
158 wxString msg (m_pszLabel);
160 eventLog.SetString( msg );
161 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
163 wxCommandEvent newProjEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_PROJECTIONFILE_EVENT);
164 newProjEvent.SetClientData (pProjections);
165 wxPostEvent (theApp->getMainFrame(), newProjEvent);
172 ProjectorSupervisor::getProjections()
174 Projections* pProjections = new Projections (*m_pScanner);
177 size_t detArraySize = pProjections->nDet() * sizeof (DetectorValue);
178 for (int iw = 0; iw < getNumWorkers(); iw++) {
179 for (int iView = 0; iView < m_vecpChildProjections[iw]->nView(); iView++) {
180 DetectorArray& childDetArray = m_vecpChildProjections[iw]->getDetectorArray(iView);
181 DetectorArray& globalDetArray = pProjections->getDetectorArray(iGlobalView++);
182 globalDetArray.setViewAngle (childDetArray.viewAngle());
183 DetectorValue* childDetval = childDetArray.detValues();
184 DetectorValue* globalDetval = globalDetArray.detValues();
185 memcpy (globalDetval, childDetval, detArraySize);
189 return (pProjections);
193 /////////////////////////////////////////////////////////////////////
195 // Class ProjectorWorker -- A worker thread
197 /////////////////////////////////////////////////////////////////////
200 ProjectorWorker::SetParameters (PhantomFileView* pPhantomView, Projections* pProjections, Scanner* pScanner,
201 int iNDet, int iView, int iOffsetView,
202 const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dCenterDetectorLength,
203 double dViewRatio, double dScanRatio)
205 m_pScanner = pScanner;
206 m_pPhantomView = pPhantomView;
207 m_pProjections = pProjections;
208 m_pszGeometry = pszGeometry;
209 m_iNSample = iNSample;
210 m_dFocalLength = dFocalLength;
211 m_dCenterDetectorLength = dCenterDetectorLength;
212 m_dViewRatio = dViewRatio;
213 m_dScanRatio = dScanRatio;
217 ProjectorWorker::Entry ()
219 const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
220 bool bFail = m_pScanner->fail();
223 failMsg = "Unable to make Projector: ";
224 failMsg += m_pScanner->failMessage().c_str();
225 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
226 event.SetString( failMsg.c_str() );
227 wxPostEvent( theApp->getMainFrame(), event );
231 wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
232 for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
235 if (theApp->getVerboseLogging()) {
237 msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);
238 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
239 event.SetString( msg );
240 wxPostEvent( theApp->getMainFrame(), event );
245 m_pScanner->collectProjections (*m_pProjections, rPhantom, iUnit + m_iStartUnit, 1,
246 m_iOffsetView, iUnit, Trace::TRACE_NONE);
247 m_pSupervisor->onWorkerUnitTick();
252 m_pSupervisor->onWorkerFail (m_iThread, failMsg);
254 m_pSupervisor->onWorkerDone (m_iThread);
257 while (! TestDestroy())
260 return reinterpret_cast<wxThread::ExitCode>(0);
264 ProjectorWorker::OnExit ()
268 #endif // HAVE_WXTHREADS