Project

General

Profile

Multithreading

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.

We decided to use the Decorator design pattern. Now is possible:

  • Divide implementation of multithreading from the image processing logic.
  • Apply multithreading on already created class with minimal modification.
  • Simple enabled/disabled multithreadig.
  • Simple testing new arEngine implementation.

How it's works

MultiThreadArEngine class contains two loops that are executed in parallel. Loading and display is separated from detection. Overloaded method start() starts both loops.

TODO Schema

Display Loop

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);
  }
}

Detection Loop

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);
  }
}

Create Multithreadig ArEngine

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.

    /// 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();