r584: 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.3 2001/02/25 16:21:36 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 #include "ct.h"
35 #include "ctsim.h"
36 #include "docs.h"
37 #include "views.h"
38 #include "threadproj.h"
39 #include "backgroundmgr.h"
40 #include "backgroundsupr.h"
41
42 #if defined(HAVE_CONFIG_H)
43 #include "config.h"
44 #endif
45
46
47
48 /////////////////////////////////////////////////////////////////////
49 //
50 // Class ProjectorSupervisorThread -- Thread for Background Supervisor
51 //
52 /////////////////////////////////////////////////////////////////////
53
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),
60   SupervisorThread()
61 {
62 }
63
64 wxThread::ExitCode
65 ProjectorSupervisorThread::Entry()
66 {
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());
69
70   projSupervisor.start();
71   while (! projSupervisor.isDone() && ! projSupervisor.fail()) {
72     Sleep(50);
73     Yield();
74   }
75   if (projSupervisor.fail())
76   {
77     wxString msg ("Error starting Projector supervisor: ");
78     msg += projSupervisor.getFailMessage().c_str();
79     msg += "\n";
80     wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
81     eventLog.SetString( msg );
82     wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
83   }
84
85   while (! projSupervisor.workersDeleted()) {
86     Sleep(50);
87     projSupervisor.ProcessPendingEvents();
88   }
89
90   return reinterpret_cast<wxThread::ExitCode>(0);
91 }
92
93 void
94 ProjectorSupervisorThread::OnExit()
95 {
96 }
97
98
99 /////////////////////////////////////////////////////////////////////
100 //
101 // Class ProjectorSupervisor -- A Background Supervisor
102 //
103 /////////////////////////////////////////////////////////////////////
104
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)
113 {
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);
116
117   m_vecpChildProjections.reserve (getNumWorkers());
118   for (unsigned int iThread = 0; iThread < getNumWorkers(); iThread++) {
119     m_vecpChildProjections[iThread] = new Projections (*m_pScanner);    
120   }
121
122
123
124 }
125
126 ProjectorSupervisor::~ProjectorSupervisor()
127 {
128   for (int i = 0; i < getNumWorkers(); i++) {
129       delete m_vecpChildProjections[i];
130       m_vecpChildProjections[i] = NULL;
131     }
132
133   delete m_pScanner;
134 }
135
136 BackgroundWorkerThread*
137 ProjectorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
138 {
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);
143
144    return pThread;
145 }
146
147 void
148 ProjectorSupervisor::onDone()
149 {
150   wxCriticalSection doneSection;
151   wxCriticalSectionLocker critsect (doneSection);
152
153   ProjectionFileDocument* pProjDoc = theApp->newProjectionDoc();
154   if (! pProjDoc) {
155     sys_error (ERR_SEVERE, "Unable to create projection file");
156     return;
157   }
158     
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);
168   }
169   *theApp->getLog() << m_pszLabel << "\n";
170   pProjections->setRemark (m_pszLabel);
171   pProjections->setCalcTime (getTimerEnd());
172
173   setDone();
174 }
175
176
177 Projections*
178 ProjectorSupervisor::getProjections()
179 {
180   Projections* pProjections = new Projections (*m_pScanner);
181
182   int iGlobalView = 0;
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);
192       iGlobalView++;
193     }
194   }
195
196   return (pProjections);
197 }
198
199
200 /////////////////////////////////////////////////////////////////////
201 //
202 // Class ProjectorWorker -- A worker thread
203 //
204 /////////////////////////////////////////////////////////////////////
205
206 void
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,
210  double dScanRatio)
211 {
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;
220 }
221
222 wxThread::ExitCode
223 ProjectorWorker::Entry ()
224 {
225   const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
226   bool bFail = m_pScanner->fail();
227   wxString failMsg;
228   if (bFail) {
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 );
234   }
235   else
236   {
237     wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
238     for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
239       if (TestDestroy()) {
240 #ifdef DEBUG
241         if (theApp->getVerboseLogging()) {
242           wxString msg;
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 ); 
247         }
248 #endif
249         break;
250       }
251       m_pScanner->collectProjections (*m_pProjections, rPhantom, iUnit + m_iStartUnit, 1, iUnit, Trace::TRACE_NONE);
252       wxPostEvent (m_pSupervisor, eventProgress);
253     }
254   }
255
256   if (bFail) {
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);
261   } else {
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);
265   }
266
267   while (! TestDestroy())
268     Sleep(100);
269
270   return reinterpret_cast<wxThread::ExitCode>(0);
271 }
272
273 void
274 ProjectorWorker::OnExit ()
275 {
276 }