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