<h1>Multithreading</h1>
<p>Multithrading allows smoother rendering the scene. It creates a smaller difference between scaned and augmented reality. But the virtual objects can lag behind the marks. Delays in display the virtual objects is better than tearing in the video.</p>
<p>We decided to use the Decorator design pattern. Now is possible:</p>
<ul>
<li>Divide implementation of multithreading from the image processing logic.</li>
<li>Apply multithreading on already created class with minimal modification.</li>
<li>Simple enabled/disabled multithreadig.</li>
<li>Simple testing new arEngine implementation.</li>
</ul>
<h2 id="Howitsworks">How it's works</h2>
<p>MultiThreadArEngine class contains two loops that are executed in parallel. Loading and display is separated from detection. Overloaded method <tt>start()</tt> starts both loops.</p>
<p><tt>TODO Schema</tt></p>
<h3 id="DisplayLoop">Display Loop</h3>
<div style="margin-left:1.75em;">
<pre>
void Creator::MultiThreadArEngine::displayLoop()
{
cv::Mat loadedImage;
while(m_isRunning) {
//load new image
loadedImage = getNextImage();
// thread-save store new image
boost::mutex::scoped_lock lock(m_imageMutex);
m_loadedImage = loadedImage;
lock.unlock();
// display scene
displayScene(loadedImage, m_foundMarkers);
}
}
</pre>
</div>
<h3 id="DetectionLoop">Detection Loop</h3>
<div style="margin-left:1.75em;">
<pre>
void Creator::MultiThreadArEngine::displayLoop()
{
cv::Mat loadedImage;
while(m_isRunning) {
//load new image
loadedImage = getNextImage();
// thread-save store new image
boost::mutex::scoped_lock lock(m_imageMutex);
m_loadedImage = loadedImage;
lock.unlock();
// display scene
displayScene(loadedImage, m_foundMarkers);
}
}
</pre>
</div>
<h2 id="CreateMultithreadigArEngine">Create Multithreadig ArEngine</h2>
<p>Creating multithreaded engine is very simple. A special method created by engine configuration. Created engine is wrapped in a multi-threaded envelope in the end.</p>
<div style="margin-left:1.75em;">
<pre>
/// First, we create a class that is able to produce AR Engine
Creator::DesktopArEngineDirector* engineCreator = new Creator::DesktopArEngineDirector();
Creator::ArEngineInterface* arEngine
= engineCreator->createMultiThreadArEngine(Creator::ConfigFileType_PlainText,
"/etc/settings.golay",
"/etc/markers.golay");
/// Finally, the infinite loop is started...
arEngine->start();
</pre>
</div>