1 /*****************************************************************************
4 ** Name: threadraster.cpp
5 ** Purpose: Threaded rasterizer class
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: February 2001
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2001 Kevin Rosenberg
12 ** $Id: threadraster.cpp,v 1.8 2001/03/04 22:30:20 kevin Exp $
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.
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.
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 ******************************************************************************/
29 #if defined(HAVE_CONFIG_H)
33 #include "wx/wxprec.h"
48 #include "threadraster.h"
49 #include "backgroundmgr.h"
50 #include "backgroundsupr.h"
53 /////////////////////////////////////////////////////////////////////
55 // Class RasterizerSupervisorThread -- Thread for Background Supervisor
57 /////////////////////////////////////////////////////////////////////
59 RasterizerSupervisorThread::RasterizerSupervisorThread (PhantomFileView* pProjView, int iNX, int iNY,
60 int iNSample, double dViewRatio, const char* const pszLabel)
61 : SupervisorThread(), m_pPhantomView(pProjView), m_iNX(iNX), m_iNY(iNY), m_iNSample(iNSample), m_dViewRatio(dViewRatio), m_strLabel(pszLabel)
66 RasterizerSupervisorThread::Entry()
68 RasterizerSupervisor* pRasterSupervisor = new RasterizerSupervisor (this, m_pPhantomView, m_iNX, m_iNY, m_iNSample, m_dViewRatio, m_strLabel.c_str());
70 pRasterSupervisor->start();
72 while (! pRasterSupervisor->workersDone() && ! pRasterSupervisor->fail() && ! pRasterSupervisor->cancelled()) {
76 if (pRasterSupervisor->fail())
78 wxString msg ("Error starting Rasterizer supervisor: ");
79 msg += pRasterSupervisor->getFailMessage().c_str();
81 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
82 eventLog.SetString( msg );
83 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
86 if (! pRasterSupervisor->cancelled())
87 pRasterSupervisor->onDone();
88 pRasterSupervisor->deleteWorkers();
89 while (! pRasterSupervisor->workersDeleted()) {
92 delete pRasterSupervisor;
94 return reinterpret_cast<wxThread::ExitCode>(0);
98 RasterizerSupervisorThread::OnExit()
103 /////////////////////////////////////////////////////////////////////
105 // Class RasterizerSupervisor -- A Background Supervisor
107 /////////////////////////////////////////////////////////////////////
109 RasterizerSupervisor::RasterizerSupervisor (SupervisorThread* pThread, PhantomFileView* pPhantomView, int iNX, int iNY,
110 int iNSample, double dViewRatio, const char* const pszLabel)
111 : BackgroundSupervisor (pThread, pPhantomView->GetFrame(), pPhantomView->GetDocument(), "Rasterizing", iNX),
112 m_pPhantomView(pPhantomView), m_pPhantomDoc(pPhantomView->GetDocument()),
113 m_iNX(iNX), m_iNY(iNY), m_iNSample(iNSample), m_dViewRatio(dViewRatio), m_pszLabel(pszLabel)
115 m_vecpChildImageFiles.reserve (getNumWorkers());
116 for (int iThread = 0; iThread < getNumWorkers(); iThread++) {
117 m_vecpChildImageFiles[iThread] = new ImageFile;
121 RasterizerSupervisor::~RasterizerSupervisor()
123 for (int i = 0; i < getNumWorkers(); i++)
124 delete m_vecpChildImageFiles[i];
127 BackgroundWorkerThread*
128 RasterizerSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
130 RasterizerWorker* pThread = new RasterizerWorker (this, iThread, iStartUnit, iNumUnits);
131 m_vecpChildImageFiles[iThread]->setArraySize (iNumUnits, m_iNY);
132 pThread->SetParameters (m_pPhantomView, m_vecpChildImageFiles[iThread], m_iNX, m_iNY, m_iNSample, m_dViewRatio);
138 RasterizerSupervisor::onDone()
140 wxCriticalSection doneSection;
141 wxCriticalSectionLocker critsect (doneSection);
143 ImageFile* pImageFile = getImageFile();
144 pImageFile->labelAdd (m_pszLabel, getTimerEnd());
146 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
147 wxString msg (m_pszLabel);
149 eventLog.SetString( msg );
150 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
152 wxCommandEvent newImageEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_IMAGEFILE_EVENT);
153 newImageEvent.SetClientData (pImageFile);
154 wxPostEvent (theApp->getMainFrame(), newImageEvent);
161 RasterizerSupervisor::getImageFile()
163 ImageFile* pImageFile = new ImageFile (m_iNX, m_iNY);
165 size_t iColSize = sizeof(ImageFileValue) * m_iNY;
167 ImageFileArray globalArray = pImageFile->getArray();
169 for (int iw = 0; iw < getNumWorkers(); iw++) {
170 ImageFileArray childArray = m_vecpChildImageFiles[iw]->getArray();
171 for (unsigned int iCol = 0; iCol < m_vecpChildImageFiles[iw]->nx(); iCol++) {
172 memcpy (globalArray[iGlobalCol++], childArray[iCol], iColSize);
179 /////////////////////////////////////////////////////////////////////
181 // Class RasterizerWorker -- A worker thread
183 /////////////////////////////////////////////////////////////////////
186 RasterizerWorker::SetParameters (PhantomFileView* pPhantomView, ImageFile* pImageFile, int iNX, int iNY, int iNSample, double dViewRatio)
188 m_pImageFile = pImageFile;
191 m_pPhantomView = pPhantomView;
192 m_iNSample = iNSample;
193 m_dViewRatio = dViewRatio;
197 RasterizerWorker::Entry ()
199 const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
200 wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
201 for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
204 if (theApp->getVerboseLogging()) {
206 msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);
207 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
208 event.SetString( msg );
209 wxPostEvent( theApp->getMainFrame(), event );
214 rPhantom.convertToImagefile (*m_pImageFile, m_iNX, m_dViewRatio, m_iNSample, Trace::TRACE_NONE, iUnit + m_iStartUnit, 1, iUnit);
216 m_pSupervisor->onWorkerUnitTick();
219 m_pSupervisor->onWorkerDone (m_iThread);
221 while (! TestDestroy())
224 return reinterpret_cast<wxThread::ExitCode>(0);
228 RasterizerWorker::OnExit ()
232 #endif // HAVE_WXTHREADS