r601: 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.18 2001/03/04 04:16: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.isDone() && ! reconSupervisor.fail()) {
74     Sleep(100);
75     Yield();
76   }
77   if (reconSupervisor.fail())
78   {
79     wxString msg ("Error starting reconstructor supervisor: ");
80     msg += reconSupervisor.getFailMessage().c_str();
81     msg += "\n";
82     wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
83     eventLog.SetString( msg );
84     wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
85   }
86
87   while (! reconSupervisor.workersDeleted()) {
88     Sleep(50);
89     reconSupervisor.ProcessPendingEvents();
90   }
91
92   return reinterpret_cast<wxThread::ExitCode>(0);
93 }
94
95 void
96 ReconstructorSupervisorThread::OnExit()
97 {
98 }
99
100
101 /////////////////////////////////////////////////////////////////////
102 //
103 // Class ReconstructorSupervisor -- A Background Supervisor
104 //
105 /////////////////////////////////////////////////////////////////////
106
107 ReconstructorSupervisor::ReconstructorSupervisor (SupervisorThread* pThread, ProjectionFileView* pProjView, 
108   int iImageNX, int iImageNY, const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, 
109   int iZeropad, const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam,
110   const char* pszBackprojectName, const char* const pszLabel)
111     : m_pProjView(pProjView), m_pProjDoc(pProjView->GetDocument()), 
112       m_iImageNX(iImageNX), m_iImageNY(iImageNY), 
113       m_pszFilterName(pszFilterName), m_dFilterParam(dFilterParam), m_pszFilterMethod(pszFilterMethod),
114       m_iZeropad(iZeropad), m_pszFilterGenerationName(pszFilterGenerationName), m_pszInterpName(pszInterpName),
115       m_iInterpParam(iInterpParam), m_pszBackprojectName(pszBackprojectName), m_pszLabel(pszLabel), 
116       BackgroundSupervisor (pThread, pProjView->GetFrame(), pProjView->GetDocument(), "Reconstructing", pProjView->GetDocument()->getProjections().nView())
117 {
118   m_vecpChildImageFile.reserve (getNumWorkers());
119   for (unsigned int iThread = 0; iThread < getNumWorkers(); iThread++) {
120     m_vecpChildImageFile[iThread] = new ImageFile (m_iImageNX, m_iImageNY);    
121   }
122
123 }
124
125 ReconstructorSupervisor::~ReconstructorSupervisor()
126 {
127   for (int i = 0; i < getNumWorkers(); i++) {
128       delete m_vecpChildImageFile[i];
129       m_vecpChildImageFile[i] = NULL;
130     }
131 }
132
133 BackgroundWorkerThread*
134 ReconstructorSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
135 {
136    ReconstructorWorker* pThread = new ReconstructorWorker (this, iThread, iStartUnit, iNumUnits);
137    pThread->SetParameters (m_pProjView, m_vecpChildImageFile[iThread], m_pszFilterName, m_dFilterParam, 
138      m_pszFilterMethod, m_iZeropad, m_pszFilterGenerationName, m_pszInterpName,
139      m_iInterpParam, m_pszBackprojectName);
140
141    return pThread;
142 }
143
144 void
145 ReconstructorSupervisor::onDone()
146 {
147   wxCriticalSection doneSection;
148   wxCriticalSectionLocker critsect (doneSection);
149
150   ImageFile* pImageFile = getImageFile();
151   pImageFile->labelAdd (m_pProjView->GetDocument()->getProjections().getLabel());
152   pImageFile->labelAdd (m_pszLabel, getTimerEnd());
153
154   wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
155   wxString msg (m_pszLabel);
156   msg += "\n";
157   eventLog.SetString( msg );
158   wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
159
160   wxCommandEvent newImageEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_IMAGEFILE_EVENT);
161   newImageEvent.SetClientData (pImageFile);
162   wxPostEvent (theApp->getMainFrame(), newImageEvent);
163
164   setDone();
165 }
166
167
168 ImageFile*
169 ReconstructorSupervisor::getImageFile()
170 {
171   ImageFile* pImageFile = new ImageFile (m_iImageNX, m_iImageNY);
172   pImageFile->arrayDataClear();
173   ImageFileArray pArray = pImageFile->getArray();
174   
175   int i;
176   for (i = 0; i < getNumWorkers(); i++) {
177     ImageFileArrayConst pChildArray = m_vecpChildImageFile[i]->getArray();
178     for (int ix = 0; ix < m_iImageNX; ix++)
179       for (int iy = 0; iy < m_iImageNY; iy++)
180         pArray[ix][iy] += pChildArray[ix][iy];
181   }
182   
183   return (pImageFile);
184 }
185
186
187 /////////////////////////////////////////////////////////////////////
188 //
189 // Class ReconstructorWorker -- A worker thread
190 //
191 /////////////////////////////////////////////////////////////////////
192
193 void
194 ReconstructorWorker::SetParameters (ProjectionFileView* pProjView, ImageFile* pImageFile, 
195  const char* pszFilterName, double dFilterParam, const char* pszFilterMethod, int iZeropad,
196  const char* pszFilterGenerationName, const char* pszInterpName, int iInterpParam, 
197  const char* pszBackprojectName)
198 {
199    m_pProjView = pProjView;
200    m_pImageFile = pImageFile;
201    m_pszFilterName = pszFilterName;
202    m_dFilterParam = dFilterParam; 
203    m_pszFilterMethod = pszFilterMethod;
204    m_iZeropad = iZeropad;
205    m_pszFilterGenerationName = pszFilterGenerationName;
206    m_pszInterpName = pszInterpName;
207    m_iInterpParam = iInterpParam;
208    m_pszBackprojectName = pszBackprojectName;
209 }
210
211 wxThread::ExitCode
212 ReconstructorWorker::Entry ()
213 {
214   Reconstructor* pReconstructor = new Reconstructor (m_pProjView->GetDocument()->getProjections(), 
215     *m_pImageFile, m_pszFilterName, m_dFilterParam, m_pszFilterMethod, m_iZeropad, 
216     m_pszFilterGenerationName, m_pszInterpName, m_iInterpParam, m_pszBackprojectName, Trace::TRACE_NONE);
217
218   bool bFail = pReconstructor->fail();
219   wxString failMsg;
220   if (bFail) {
221       failMsg = "Unable to make reconstructor: ";
222       failMsg += pReconstructor->failMessage().c_str();  
223       wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
224       event.SetString( failMsg );
225       wxPostEvent( theApp->getMainFrame(), event );
226   }
227   else
228   {
229     wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
230     for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
231       if (TestDestroy()) {
232 #ifdef DEBUG
233         if (theApp->getVerboseLogging()) {
234           wxString msg;
235           msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);  
236           wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
237           event.SetString( msg );
238           wxPostEvent( theApp->getMainFrame(), event ); 
239         }
240 #endif
241         break;
242       }
243       pReconstructor->reconstructView (iUnit + m_iStartUnit, 1);
244       m_pSupervisor->AddPendingEvent (eventProgress);
245     }
246     pReconstructor->postProcessing();
247   }
248   delete pReconstructor;
249
250   if (bFail) {
251     wxCommandEvent eventFail (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_FAIL);
252     eventFail.SetInt (m_iThread); // Send back thread# that has finished
253     eventFail.SetString (failMsg);
254     wxPostEvent (m_pSupervisor, eventFail);
255   } else {
256     wxCommandEvent eventDone (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_DONE);
257     eventDone.SetInt (m_iThread); // Send back thread# that has finished
258     wxPostEvent (m_pSupervisor, eventDone);
259   }
260
261   while (! TestDestroy())
262     Sleep(100);
263
264   return reinterpret_cast<wxThread::ExitCode>(0);
265 }
266
267 void
268 ReconstructorWorker::OnExit ()
269 {
270 }
271
272 #endif // HAVE_WXTHREADS