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
12 ** $Id: threadproj.cpp,v 1.3 2001/02/25 16:21:36 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 ******************************************************************************/
28 #include "wx/wxprec.h"
38 #include "threadproj.h"
39 #include "backgroundmgr.h"
40 #include "backgroundsupr.h"
42 #if defined(HAVE_CONFIG_H)
48 /////////////////////////////////////////////////////////////////////
50 // Class ProjectorSupervisorThread -- Thread for Background Supervisor
52 /////////////////////////////////////////////////////////////////////
54 ProjectorSupervisorThread::ProjectorSupervisorThread (PhantomFileView* pProjView, int iNDet, int iNView,
55 const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dViewRatio,
56 double dScanRatio, const char* const pszLabel)
57 : m_pPhantomView(pProjView), m_iNDet(iNDet), m_iNView(iNView), m_strGeometry(pszGeometry),
58 m_iNSample(iNSample), m_dRotation(dRotation), m_dFocalLength(dFocalLength), m_dViewRatio(dViewRatio),
59 m_dScanRatio(dScanRatio), m_strLabel(pszLabel),
65 ProjectorSupervisorThread::Entry()
67 ProjectorSupervisor projSupervisor (m_pPhantomView, m_iNDet, m_iNView,
68 m_strGeometry.c_str(), m_iNSample, m_dRotation, m_dFocalLength, m_dViewRatio, m_dScanRatio, m_strLabel.c_str());
70 projSupervisor.start();
71 while (! projSupervisor.isDone() && ! projSupervisor.fail()) {
75 if (projSupervisor.fail())
77 wxString msg ("Error starting Projector supervisor: ");
78 msg += projSupervisor.getFailMessage().c_str();
80 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
81 eventLog.SetString( msg );
82 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
85 while (! projSupervisor.workersDeleted()) {
87 projSupervisor.ProcessPendingEvents();
90 return reinterpret_cast<wxThread::ExitCode>(0);
94 ProjectorSupervisorThread::OnExit()
99 /////////////////////////////////////////////////////////////////////
101 // Class ProjectorSupervisor -- A Background Supervisor
103 /////////////////////////////////////////////////////////////////////
105 ProjectorSupervisor::ProjectorSupervisor (PhantomFileView* pPhantomView, int iNDet, int iNView,
106 const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dViewRatio,
107 double dScanRatio, const char* const pszLabel)
108 : m_pPhantomView(pPhantomView), m_pPhantomDoc(pPhantomView->GetDocument()),
109 m_iNDet(iNDet), m_iNView(iNView),
110 m_pszGeometry(pszGeometry), m_iNSample(iNSample), m_dRotation(dRotation), m_dFocalLength(dFocalLength),
111 m_dViewRatio(dViewRatio), m_dScanRatio(dScanRatio), m_pszLabel(pszLabel),
112 BackgroundSupervisor (pPhantomView->GetFrame(), pPhantomView->GetDocument(), "Projecting", iNView)
114 m_pScanner = new Scanner (m_pPhantomDoc->getPhantom(), m_pszGeometry, m_iNDet,
115 m_iNView, m_iNSample, m_dRotation, m_dFocalLength, m_dViewRatio, m_dScanRatio);
117 m_vecpChildProjections.reserve (getNumWorkers());
118 for (unsigned 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,
142 m_pszGeometry, m_iNSample, m_dRotation, m_dFocalLength, m_dViewRatio, m_dScanRatio);
148 ProjectorSupervisor::onDone()
150 wxCriticalSection doneSection;
151 wxCriticalSectionLocker critsect (doneSection);
153 ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
155 sys_error (ERR_SEVERE, "Unable to create projection file");
159 Projections* pProjections = getProjections();
160 pProjDoc->setProjections (pProjections);
161 if (theApp->getAskDeleteNewDocs())
162 pProjDoc->Modify (true);
163 pProjDoc->UpdateAllViews (NULL);
164 if (ProjectionFileView* projView = pProjDoc->getView()) {
165 projView->OnUpdate (projView, NULL);
166 projView->getFrame()->SetFocus();
167 projView->getFrame()->Show(true);
169 *theApp->getLog() << m_pszLabel << "\n";
170 pProjections->setRemark (m_pszLabel);
171 pProjections->setCalcTime (getTimerEnd());
178 ProjectorSupervisor::getProjections()
180 Projections* pProjections = new Projections (*m_pScanner);
183 size_t detArraySize = pProjections->nDet() * sizeof (DetectorValue);
184 for (int iw = 0; iw < getNumWorkers(); iw++) {
185 for (int iView = 0; iView < m_vecpChildProjections[iw]->nView(); iView++) {
186 DetectorArray& childDetArray = m_vecpChildProjections[iw]->getDetectorArray(iView);
187 DetectorArray& globalDetArray = pProjections->getDetectorArray(iGlobalView);
188 globalDetArray.setViewAngle (childDetArray.viewAngle());
189 DetectorValue* childDetval = childDetArray.detValues();
190 DetectorValue* globalDetval = globalDetArray.detValues();
191 memcpy (globalDetval, childDetval, detArraySize);
196 return (pProjections);
200 /////////////////////////////////////////////////////////////////////
202 // Class ProjectorWorker -- A worker thread
204 /////////////////////////////////////////////////////////////////////
207 ProjectorWorker::SetParameters (PhantomFileView* pPhantomView, Projections* pProjections, Scanner* pScanner,
208 int iNDet, int iView,
209 const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dViewRatio,
212 m_pScanner = pScanner;
213 m_pPhantomView = pPhantomView;
214 m_pProjections = pProjections;
215 m_pszGeometry = pszGeometry;
216 m_iNSample = iNSample;
217 m_dFocalLength = dFocalLength;
218 m_dViewRatio = dViewRatio;
219 m_dScanRatio = dScanRatio;
223 ProjectorWorker::Entry ()
225 const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
226 bool bFail = m_pScanner->fail();
229 failMsg = "Unable to make Projector: ";
230 failMsg += m_pScanner->failMessage().c_str();
231 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
232 event.SetString( failMsg );
233 wxPostEvent( theApp->getMainFrame(), event );
237 wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
238 for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
241 if (theApp->getVerboseLogging()) {
243 msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);
244 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
245 event.SetString( msg );
246 wxPostEvent( theApp->getMainFrame(), event );
251 m_pScanner->collectProjections (*m_pProjections, rPhantom, iUnit + m_iStartUnit, 1, iUnit, Trace::TRACE_NONE);
252 wxPostEvent (m_pSupervisor, eventProgress);
257 wxCommandEvent eventFail (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_FAIL);
258 eventFail.SetInt (m_iThread); // Send back thread# that has finished
259 eventFail.SetString (failMsg);
260 wxPostEvent (m_pSupervisor, eventFail);
262 wxCommandEvent eventDone (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_DONE);
263 eventDone.SetInt (m_iThread); // Send back thread# that has finished
264 wxPostEvent (m_pSupervisor, eventDone);
267 while (! TestDestroy())
270 return reinterpret_cast<wxThread::ExitCode>(0);
274 ProjectorWorker::OnExit ()