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.1 2001/02/27 03:59:30 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 ******************************************************************************/
28 #include "wx/wxprec.h"
38 #include "threadraster.h"
39 #include "backgroundmgr.h"
40 #include "backgroundsupr.h"
42 #if defined(HAVE_CONFIG_H)
48 /////////////////////////////////////////////////////////////////////
50 // Class RasterizerSupervisorThread -- Thread for Background Supervisor
52 /////////////////////////////////////////////////////////////////////
54 RasterizerSupervisorThread::RasterizerSupervisorThread (PhantomFileView* pProjView, int iNX, int iNY,
55 int iNSample, double dViewRatio, const char* const pszLabel)
56 : m_pPhantomView(pProjView), m_iNX(iNX), m_iNY(iNY), m_iNSample(iNSample), m_dViewRatio(dViewRatio), m_strLabel(pszLabel),
62 RasterizerSupervisorThread::Entry()
64 RasterizerSupervisor rasterSupervisor (this, m_pPhantomView, m_iNX, m_iNY, m_iNSample, m_dViewRatio, m_strLabel.c_str());
66 rasterSupervisor.start();
67 while (! rasterSupervisor.isDone() && ! rasterSupervisor.fail()) {
71 if (rasterSupervisor.fail())
73 wxString msg ("Error starting Rasterizer supervisor: ");
74 msg += rasterSupervisor.getFailMessage().c_str();
76 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
77 eventLog.SetString( msg );
78 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
81 while (! rasterSupervisor.workersDeleted()) {
83 rasterSupervisor.ProcessPendingEvents();
86 return reinterpret_cast<wxThread::ExitCode>(0);
90 RasterizerSupervisorThread::OnExit()
95 /////////////////////////////////////////////////////////////////////
97 // Class RasterizerSupervisor -- A Background Supervisor
99 /////////////////////////////////////////////////////////////////////
101 RasterizerSupervisor::RasterizerSupervisor (SupervisorThread* pThread, PhantomFileView* pPhantomView, int iNX, int iNY,
102 int iNSample, double dViewRatio, const char* const pszLabel)
103 : m_pPhantomView(pPhantomView), m_pPhantomDoc(pPhantomView->GetDocument()),
104 m_iNX(iNX), m_iNY(iNY), m_iNSample(iNSample), m_dViewRatio(dViewRatio), m_pszLabel(pszLabel),
105 BackgroundSupervisor (pThread, pPhantomView->GetFrame(), pPhantomView->GetDocument(), "Rasterizing", iNX)
107 m_vecpChildImageFiles.reserve (getNumWorkers());
108 for (unsigned int iThread = 0; iThread < getNumWorkers(); iThread++) {
109 m_vecpChildImageFiles[iThread] = new ImageFile;
116 RasterizerSupervisor::~RasterizerSupervisor()
118 for (int i = 0; i < getNumWorkers(); i++)
119 delete m_vecpChildImageFiles[i];
122 BackgroundWorkerThread*
123 RasterizerSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
125 RasterizerWorker* pThread = new RasterizerWorker (this, iThread, iStartUnit, iNumUnits);
126 m_vecpChildImageFiles[iThread]->setArraySize (iNumUnits, m_iNY);
127 pThread->SetParameters (m_pPhantomView, m_vecpChildImageFiles[iThread], m_iNX, m_iNY, m_iNSample, m_dViewRatio);
133 RasterizerSupervisor::onDone()
135 wxCriticalSection doneSection;
136 wxCriticalSectionLocker critsect (doneSection);
138 ImageFileDocument* pImageDoc = theApp->newImageDoc();
140 sys_error (ERR_SEVERE, "Unable to create image file");
144 ImageFile* pImageFile = getImageFile();
145 pImageDoc->setImageFile (pImageFile);
146 if (theApp->getAskDeleteNewDocs())
147 pImageDoc->Modify (true);
148 pImageDoc->UpdateAllViews (NULL);
149 if (ImageFileView* imageView = pImageDoc->getView()) {
150 imageView->OnUpdate (imageView, NULL);
151 imageView->getFrame()->SetFocus();
152 imageView->getFrame()->Show(true);
154 *theApp->getLog() << m_pszLabel << "\n";
155 pImageFile->labelAdd (m_pszLabel, getTimerEnd());
162 RasterizerSupervisor::getImageFile()
164 ImageFile* pImageFile = new ImageFile (m_iNX, m_iNY);
166 size_t iColSize = sizeof(ImageFileValue) * m_iNY;
168 ImageFileArray globalArray = pImageFile->getArray();
170 for (int iw = 0; iw < getNumWorkers(); iw++) {
171 ImageFileArray childArray = m_vecpChildImageFiles[iw]->getArray();
172 for (int iCol = 0; iCol < m_vecpChildImageFiles[iw]->nx(); iCol++) {
173 memcpy (globalArray[iGlobalCol++], childArray[iCol], iColSize);
180 /////////////////////////////////////////////////////////////////////
182 // Class RasterizerWorker -- A worker thread
184 /////////////////////////////////////////////////////////////////////
187 RasterizerWorker::SetParameters (PhantomFileView* pPhantomView, ImageFile* pImageFile, int iNX, int iNY, int iNSample, double dViewRatio)
189 m_pImageFile = pImageFile;
192 m_pPhantomView = pPhantomView;
193 m_iNSample = iNSample;
194 m_dViewRatio = dViewRatio;
198 RasterizerWorker::Entry ()
200 const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
201 wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
202 for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
205 if (theApp->getVerboseLogging()) {
207 msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);
208 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
209 event.SetString( msg );
210 wxPostEvent( theApp->getMainFrame(), event );
215 rPhantom.convertToImagefile (*m_pImageFile, m_iNX, m_dViewRatio, m_iNSample, Trace::TRACE_NONE,
216 iUnit + m_iStartUnit, 1, iUnit);
217 wxPostEvent (m_pSupervisor, eventProgress);
219 wxCommandEvent eventDone (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_DONE);
220 eventDone.SetInt (m_iThread); // Send back thread# that has finished
221 wxPostEvent (m_pSupervisor, eventDone);
223 while (! TestDestroy())
226 return reinterpret_cast<wxThread::ExitCode>(0);
230 RasterizerWorker::OnExit ()