r604: no message
[ctsim.git] / src / threadrecon.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          threadrecon.cpp
5 **   Purpose:       Threaded reconstruction 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: threadrecon.cpp,v 1.20 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
29 #include "wx/wxprec.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/wx.h"
33 #endif
34
35 #include "ct.h"
36 #include "ctsim.h"
37 #include "docs.h"
38 #include "views.h"
39 #include "threadrecon.h"
40 #include "backgroundmgr.h"
41 #include "backgroundsupr.h"
42
43 #ifdef HAVE_WXTHREADS
44
45
46
47
48 /////////////////////////////////////////////////////////////////////
49 //
50 // Class ReconstructorSupervisorThread -- Thread for Background Supervisor
51 //
52 /////////////////////////////////////////////////////////////////////
53
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),
61   SupervisorThread()
62 {
63 }
64
65 wxThread::ExitCode
66 ReconstructorSupervisorThread::Entry()
67 {
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());
71
72   reconSupervisor.start();
73   while (! reconSupervisor.workersDone() && ! reconSupervisor.fail() && ! reconSupervisor.cancelled()) {
74     Sleep(100);
75   }
76   if (reconSupervisor.fail())
77   {
78     wxString msg ("Error starting reconstructor supervisor: ");
79     msg += reconSupervisor.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   if (! reconSupervisor.cancelled())
86           reconSupervisor.onDone();
87   reconSupervisor.deleteWorkers();
88
89   while (! reconSupervisor.workersDeleted()) {
90     Sleep(50);
91   }
92
93   return reinterpret_cast<wxThread::ExitCode>(0);
94 }
95
96 void
97 ReconstructorSupervisorThread::OnExit()
98 {
99 }
100
101
102 /////////////////////////////////////////////////////////////////////
103 //
104 // Class ReconstructorSupervisor -- A Background Supervisor
105 //
106 /////////////////////////////////////////////////////////////////////
107
108 ReconstructorSupervisor::ReconstructorSupervisor (SupervisorThread* pThread, ProjectionFileView* pProjView, 
109   int iImageNX, int iImageNY, const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, 
110   int iZeropad, const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
111   const char* pszBackprojectName, const char* const pszLabel)
112     : m_pProjView(pProjView), m_pProjDoc(pProjView->GetDocument()), 
113       m_iImageNX(iImageNX), m_iImageNY(iImageNY), 
114       m_pszFilterName(pszFilterName), m_dFilterParam(dFilterParam), m_pszFilterMethod(pszFilterMethod),
115       m_iZeropad(iZeropad), m_pszFilterGenerationName(pszFilterGenerationName), m_pszInterpName(pszInterpName),
116       m_iInterpParam(iInterpParam), m_pszBackprojectName(pszBackprojectName), m_pszLabel(pszLabel), 
117       BackgroundSupervisor (pThread, pProjView->GetFrame(), pProjView->GetDocument(), "Reconstructing", pProjView->GetDocument()->getProjections().nView())
118 {
119   m_vecpChildImageFile.reserve (getNumWorkers());
120   for (int iThread = 0; iThread < getNumWorkers(); iThread++) {
121     m_vecpChildImageFile[iThread] = new ImageFile (m_iImageNX, m_iImageNY);    
122   }
123
124 }
125
126 ReconstructorSupervisor::~ReconstructorSupervisor()
127 {
128   for (int i = 0; i < getNumWorkers(); i++) {
129       delete m_vecpChildImageFile[i];
130       m_vecpChildImageFile[i] = NULL;
131     }
132 }
133
134 BackgroundWorkerThread*
135 ReconstructorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
136 {
137    ReconstructorWorker* pThread = new ReconstructorWorker (this, iThread, iStartUnit, iNumUnits);
138    pThread->SetParameters (m_pProjView, m_vecpChildImageFile[iThread], m_pszFilterName, m_dFilterParam, 
139      m_pszFilterMethod, m_iZeropad, m_pszFilterGenerationName, m_pszInterpName,
140      m_iInterpParam, m_pszBackprojectName);
141
142    return pThread;
143 }
144
145 void
146 ReconstructorSupervisor::onDone()
147 {
148   wxCriticalSection doneSection;
149   wxCriticalSectionLocker critsect (doneSection);
150
151   ImageFile* pImageFile = getImageFile();
152   pImageFile->labelAdd (m_pProjView->GetDocument()->getProjections().getLabel());
153   pImageFile->labelAdd (m_pszLabel, getTimerEnd());
154
155   wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
156   wxString msg (m_pszLabel);
157   msg += "\n";
158   eventLog.SetString( msg );
159   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
160
161   wxCommandEvent newImageEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_IMAGEFILE_EVENT);
162   newImageEvent.SetClientData (pImageFile);
163   wxPostEvent (theApp->getMainFrame(), newImageEvent);
164
165   setDone();
166 }
167
168
169 ImageFile*
170 ReconstructorSupervisor::getImageFile()
171 {
172   ImageFile* pImageFile = new ImageFile (m_iImageNX, m_iImageNY);
173   pImageFile->arrayDataClear();
174   ImageFileArray pArray = pImageFile->getArray();
175   
176   int i;
177   for (i = 0; i < getNumWorkers(); i++) {
178     ImageFileArrayConst pChildArray = m_vecpChildImageFile[i]->getArray();
179     for (int ix = 0; ix < m_iImageNX; ix++)
180       for (int iy = 0; iy < m_iImageNY; iy++)
181         pArray[ix][iy] += pChildArray[ix][iy];
182   }
183   
184   return (pImageFile);
185 }
186
187
188 /////////////////////////////////////////////////////////////////////
189 //
190 // Class ReconstructorWorker -- A worker thread
191 //
192 /////////////////////////////////////////////////////////////////////
193
194 void
195 ReconstructorWorker::SetParameters (ProjectionFileView* pProjView, ImageFile* pImageFile, 
196  const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, int iZeropad,
197  const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam, 
198  const char* pszBackprojectName)
199 {
200    m_pProjView = pProjView;
201    m_pImageFile = pImageFile;
202    m_pszFilterName = pszFilterName;
203    m_dFilterParam = dFilterParam; 
204    m_pszFilterMethod = pszFilterMethod;
205    m_iZeropad = iZeropad;
206    m_pszFilterGenerationName = pszFilterGenerationName;
207    m_pszInterpName = pszInterpName;
208    m_iInterpParam = iInterpParam;
209    m_pszBackprojectName = pszBackprojectName;
210 }
211
212 wxThread::ExitCode
213 ReconstructorWorker::Entry ()
214 {
215   Reconstructor* pReconstructor = new Reconstructor (m_pProjView->GetDocument()->getProjections(), 
216     *m_pImageFile, m_pszFilterName, m_dFilterParam, m_pszFilterMethod, m_iZeropad, 
217     m_pszFilterGenerationName, m_pszInterpName, m_iInterpParam, m_pszBackprojectName, Trace::TRACE_NONE);
218
219   bool bFail = pReconstructor->fail();
220   std::string failMsg;
221   if (bFail) {
222       failMsg = "Unable to make reconstructor: ";
223       failMsg += pReconstructor->failMessage().c_str();  
224       wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
225       event.SetString( failMsg.c_str() );
226       wxPostEvent( theApp->getMainFrame(), event );
227   }
228   else
229   {
230     wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
231     for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
232       if (TestDestroy()) {
233 #ifdef DEBUG
234         if (theApp->getVerboseLogging()) {
235           wxString msg;
236           msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);  
237           wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
238           event.SetString( msg );
239           wxPostEvent( theApp->getMainFrame(), event ); 
240         }
241 #endif
242         break;
243       }
244       pReconstructor->reconstructView (iUnit + m_iStartUnit, 1);
245       m_pSupervisor->onWorkerUnitTick();
246     }
247     pReconstructor->postProcessing();
248   }
249   delete pReconstructor;
250
251   if (bFail) {
252         m_pSupervisor->onWorkerFail (m_iThread, failMsg);
253   } else {
254         m_pSupervisor->onWorkerDone (m_iThread);
255   }
256
257   while (! TestDestroy())
258     Sleep(100);
259
260   return reinterpret_cast<wxThread::ExitCode>(0);
261 }
262
263 void
264 ReconstructorWorker::OnExit ()
265 {
266 }
267
268 #endif // HAVE_WXTHREADS