[vtkusers] one thread for reading, one thread for rendering

ochampao ochampao at hotmail.com
Mon May 21 18:24:00 EDT 2018


Hi,

An approach that I am uing in my own applications with Qt/VTK, is to have a
Worker class that calls the Update() method of vtkAlgorithm in a separate
thread. Using this approach Qt's main thread is not blocked while waiting
for the reader to read the data. 

Below I've included a method that reads from a folder multiple DICOM files
and returns a vtkImageData object for rendering. The reader is setup as
necessary in the main thread but the vtkDICOMImageReader::Update() method is
called by the Worker object in a separate thread. While the data are loaded,
the user sees a progress dialog.

Hope this helps,

Panos.

// ============================================
// = Method for loading data
// ============================================
vtkSmartPointer<vtkImageData> loadDicom(const char* filename) const
{
	vtkNew<vtkDICOMImageReader> reader;
	reader->FileLowerLeftOn();
	reader->SetDirectoryName(filename);
	reader->UpdateInformation();

	QProgressDialog progress(
		QString("Reading, please wait..."),
		QString("Close"), 0, 0);
	progress.show();
	qApp->processEvents(); 
	Worker thread(reader);  // calls Update()
	thread.start();
	while (!thread.isDone())
		qApp->processEvents();
	thread.quit(); // Stop thread's event loop.
	progress.hide();

	return reader->GetOutput();
}

// ============================================
// = Worker class for calling vtkAlgorithm::Update() in separate thread
// ============================================
#ifndef WORKER_H
#define WORKER_H

#include <QThread>
#include <vtkAlgorithm.h>

class Worker : public QThread
{
	Q_OBJECT

public:
	Worker(vtkAlgorithm *f) :
		QThread(),
		filter(f),
		done(false) { }

	bool isDone() { return this->done; }

protected:
	void run()
	{
		filter->Update(); // Read image.
		done = true;
	}

private:
	volatile bool done;
	vtkAlgorithm *filter;
};

#endif // !WORKER_H



--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list