r604: no message
[ctsim.git] / src / threadproj.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          threadproj.cpp
5 **   Purpose:       Threaded projection 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: threadproj.cpp,v 1.13 2001/03/04 22:30:20 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 #if defined(HAVE_CONFIG_H)
35 #include "config.h"
36 #endif
37
38 #include "ct.h"
39 #include "ctsim.h"
40 #include "docs.h"
41 #include "views.h"
42 #include "threadproj.h"
43 #include "backgroundmgr.h"
44 #include "backgroundsupr.h"
45
46 #ifdef HAVE_WXTHREADS
47
48
49
50
51 /////////////////////////////////////////////////////////////////////
52 //
53 // Class ProjectorSupervisorThread -- Thread for Background Supervisor
54 //
55 /////////////////////////////////////////////////////////////////////
56
57 ProjectorSupervisorThread::ProjectorSupervisorThread (PhantomFileView* pProjView, int iNDet, int iNView, 
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_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)
63 {
64 }
65
66 wxThread::ExitCode
67 ProjectorSupervisorThread::Entry()
68 {
69   ProjectorSupervisor projSupervisor (this, m_pPhantomView, m_iNDet, m_iNView, 
70    m_strGeometry.c_str(), m_iNSample, m_dRotation, m_dFocalLength, m_dCenterDetectorLength, m_dViewRatio, m_dScanRatio, m_strLabel.c_str());
71
72   projSupervisor.start();
73   while (! projSupervisor.workersDone() && ! projSupervisor.fail() && ! projSupervisor.cancelled()) {
74     Sleep(100);
75   }
76   if (projSupervisor.fail())
77   {
78     wxString msg ("Error starting Projector supervisor: ");
79     msg += projSupervisor.getFailMessage().c_str();
80     msg += "\n";
81     wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
82     eventLog.SetString( msg );
83     wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
84   }
85
86   if (! projSupervisor.cancelled())
87           projSupervisor.onDone();
88   projSupervisor.deleteWorkers();
89
90   while (! projSupervisor.workersDeleted()) {
91     Sleep(50);
92   }
93
94   return reinterpret_cast<wxThread::ExitCode>(0);
95 }
96
97 void
98 ProjectorSupervisorThread::OnExit()
99 {
100 }
101
102
103 /////////////////////////////////////////////////////////////////////
104 //
105 // Class ProjectorSupervisor -- A Background Supervisor
106 //
107 /////////////////////////////////////////////////////////////////////
108
109 ProjectorSupervisor::ProjectorSupervisor (SupervisorThread* pThread, PhantomFileView* pPhantomView, int iNDet, int iNView, 
110    const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dCenterDetectorLength,
111    double dViewRatio, double dScanRatio, const char* const pszLabel)
112     : BackgroundSupervisor (pThread, pPhantomView->GetFrame(), pPhantomView->GetDocument(), "Projecting", iNView),
113       m_pPhantomView(pPhantomView), m_pPhantomDoc(pPhantomView->GetDocument()), 
114       m_iNDet(iNDet), m_iNView(iNView), m_pszGeometry(pszGeometry), m_iNSample(iNSample), 
115       m_dRotation(dRotation), m_dFocalLength(dFocalLength), m_dCenterDetectorLength(dCenterDetectorLength), 
116       m_dViewRatio(dViewRatio), m_dScanRatio(dScanRatio), m_pszLabel(pszLabel)
117 {
118   m_pScanner = new Scanner (m_pPhantomDoc->getPhantom(), m_pszGeometry, m_iNDet,
119                   m_iNView, m_iNSample, m_dRotation, m_dFocalLength, m_dCenterDetectorLength, m_dViewRatio, m_dScanRatio);
120
121   m_vecpChildProjections.reserve (getNumWorkers());
122   for (int iThread = 0; iThread < getNumWorkers(); iThread++) {
123     m_vecpChildProjections[iThread] = new Projections (*m_pScanner);    
124   }
125
126
127
128 }
129
130 ProjectorSupervisor::~ProjectorSupervisor()
131 {
132   for (int i = 0; i < getNumWorkers(); i++) {
133       delete m_vecpChildProjections[i];
134       m_vecpChildProjections[i] = NULL;
135     }
136
137   delete m_pScanner;
138 }
139
140 BackgroundWorkerThread*
141 ProjectorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
142 {
143    ProjectorWorker* pThread = new ProjectorWorker (this, iThread, iStartUnit, iNumUnits);
144    m_vecpChildProjections[iThread]->setNView (iNumUnits);
145    pThread->SetParameters (m_pPhantomView, m_vecpChildProjections[iThread], m_pScanner, m_iNDet, m_iNView, 
146      m_pszGeometry, m_iNSample, m_dRotation, m_dFocalLength, m_dCenterDetectorLength, m_dViewRatio, m_dScanRatio);
147
148    return pThread;
149 }
150
151 void
152 ProjectorSupervisor::onDone()
153 {
154   wxCriticalSection doneSection;
155   wxCriticalSectionLocker critsect (doneSection);
156
157   Projections* pProjections = getProjections();
158   pProjections->setRemark (m_pszLabel);
159   pProjections->setCalcTime (getTimerEnd());
160
161   wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
162   wxString msg (m_pszLabel);
163   msg += "\n";
164   eventLog.SetString( msg );
165   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
166
167   wxCommandEvent newProjEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_PROJECTIONFILE_EVENT);
168   newProjEvent.SetClientData (pProjections);
169   wxPostEvent (theApp->getMainFrame(), newProjEvent);
170   
171   setDone();
172 }
173
174
175 Projections*
176 ProjectorSupervisor::getProjections()
177 {
178   Projections* pProjections = new Projections (*m_pScanner);
179
180   int iGlobalView = 0;
181   size_t detArraySize = pProjections->nDet() * sizeof (DetectorValue);
182   for (int iw = 0; iw < getNumWorkers(); iw++) {
183     for (int iView = 0; iView < m_vecpChildProjections[iw]->nView(); iView++) {
184       DetectorArray& childDetArray = m_vecpChildProjections[iw]->getDetectorArray(iView);
185       DetectorArray& globalDetArray = pProjections->getDetectorArray(iGlobalView++);
186       globalDetArray.setViewAngle (childDetArray.viewAngle());
187       DetectorValue* childDetval = childDetArray.detValues();
188       DetectorValue* globalDetval = globalDetArray.detValues();
189       memcpy (globalDetval, childDetval, detArraySize);
190     }
191   }
192
193   return (pProjections);
194 }
195
196
197 /////////////////////////////////////////////////////////////////////
198 //
199 // Class ProjectorWorker -- A worker thread
200 //
201 /////////////////////////////////////////////////////////////////////
202
203 void
204 ProjectorWorker::SetParameters (PhantomFileView* pPhantomView, Projections* pProjections, Scanner* pScanner,
205  int iNDet, int iView,
206  const char* pszGeometry, int iNSample, double dRotation, double dFocalLength, double dCenterDetectorLength,
207  double dViewRatio, double dScanRatio)
208 {
209    m_pScanner = pScanner;
210    m_pPhantomView = pPhantomView;
211    m_pProjections = pProjections;
212    m_pszGeometry = pszGeometry;
213    m_iNSample = iNSample;
214    m_dFocalLength = dFocalLength;
215    m_dCenterDetectorLength = dCenterDetectorLength;
216    m_dViewRatio = dViewRatio;
217    m_dScanRatio = dScanRatio;
218 }
219
220 wxThread::ExitCode
221 ProjectorWorker::Entry ()
222 {
223   const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
224   bool bFail = m_pScanner->fail();
225   std::string failMsg;
226   if (bFail) {
227       failMsg = "Unable to make Projector: ";
228       failMsg += m_pScanner->failMessage().c_str();  
229       wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
230       event.SetString( failMsg.c_str() );
231       wxPostEvent( theApp->getMainFrame(), event );
232   }
233   else
234   {
235     wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
236     for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
237       if (TestDestroy()) {
238 #ifdef DEBUG
239         if (theApp->getVerboseLogging()) {
240           wxString msg;
241           msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);  
242           wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
243           event.SetString( msg );
244           wxPostEvent( theApp->getMainFrame(), event ); 
245         }
246 #endif
247         break;
248       }
249       m_pScanner->collectProjections (*m_pProjections, rPhantom, iUnit + m_iStartUnit, 1, iUnit, Trace::TRACE_NONE);
250       m_pSupervisor->onWorkerUnitTick();
251     }
252   }
253
254   if (bFail) {
255     m_pSupervisor->onWorkerFail (m_iThread, failMsg);
256   } else {
257     m_pSupervisor->onWorkerDone (m_iThread);
258   }
259
260   while (! TestDestroy())
261     Sleep(100);
262
263   return reinterpret_cast<wxThread::ExitCode>(0);
264 }
265
266 void
267 ProjectorWorker::OnExit ()
268 {
269 }
270
271 #endif // HAVE_WXTHREADS