This advanced example illustrates multithreaded capturing.The sample code allows you to capture and process 3D frames in a time-efficient manner by employing all available threads.
#include <string>
#include <iostream>
#include <iomanip>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <boost/format.hpp>
using namespace boost::chrono;
namespace asdk {
};
using asdk::TRef;
using asdk::TArrayRef;
const int NumberOfFramesToCapture = 100;
int main( int argc, char **argv )
{
{
std::wcout << L"Enumerating scanners... ";
{
std::wcout << L"failed" << std::endl;
return 1;
}
std::wcout << L"done" << std::endl;
if( scannersList->getSize() == 0 )
{
std::wcout << L"No scanners are found" << std::endl;
return 2;
}
for( int i = 0; i < scannersArray.size(); i++ )
{
std::wcout << L"Scanner " << scannersArray[i].serial << L" is found" << std::endl;
std::wcout << L"Connecting to the scanner... ";
{
std::wcout << L"failed" << std::endl;
continue;
}
std::wcout << L"done" << std::endl;
break;
}
if( !scanner )
{
std::wcout << L"No scanner can be connected to" << std::endl;
return 3;
}
}
int framesAlreadyCaptured = scanner->getFrameNumber();
std::vector< TRef<asdk::IFrameMesh> > meshes;
meshes.resize( NumberOfFramesToCapture );
boost::mutex meshesProtection;
std::wcout << L"Capturing " << NumberOfFramesToCapture << " frames with " << boost::thread::hardware_concurrency() << L" threads" << std::endl;
high_resolution_clock::time_point startTime = high_resolution_clock::now();
boost::thread_group captureThreads;
for( unsigned i = 0; i < boost::thread::hardware_concurrency(); i++ )
{
captureThreads.create_thread( [&scanner, &meshes, &meshesProtection, framesAlreadyCaptured]
{
{
return;
}
while(true)
{
{
{
std::wcout << L"Capture error: frame capture timeout" << std::endl;
}
{
std::wcout << L"Capture error: frame corrupted" << std::endl;
}
{
std::wcout << L"Capture error: frame reconstruction failed" << std::endl;
}
{
std::wcout << L"Capture error: frame registration failed" << std::endl;
}
else
{
std::wcout << L"Capture error: unknown error" << std::endl;
}
continue;
}
int frameNumber = frame->getFrameNumber();
if( frameNumber >= NumberOfFramesToCapture + framesAlreadyCaptured )
{
return;
}
std::wcout << "frame " << std::setw( 4 ) << (frameNumber+1) << "\r";
{
std::wcout << L"Capture error: reconstruction failed for frame " << std::setw( 4 ) << (frameNumber+1) << std::endl;
continue;
}
std::wcout << "mesh " << std::setw( 4 ) << (frameNumber+1) << "\r";
std::wcout.flush();
boost::lock_guard<boost::mutex> guard( meshesProtection );
meshes[frameNumber - framesAlreadyCaptured] = mesh;
}
});
}
captureThreads.join_all();
high_resolution_clock::time_point stopTime = high_resolution_clock::now();
float timeDelta = duration_cast<milliseconds>( stopTime - startTime ).count() / 1000.f;
float fps = NumberOfFramesToCapture / timeDelta;
std::wcout << "\nfps = " << std::fixed << std::setw( 4 ) << std::setprecision( 2 ) << fps << std::endl;
int successfullyCapturedMeshes = 0;
for( int i = 0; i < NumberOfFramesToCapture; i++ )
{
if( meshes[i] )
{
successfullyCapturedMeshes++;
}
}
std::wcout << NumberOfFramesToCapture << " shots captured with " << successfullyCapturedMeshes << " meshes constructed." << std::endl;
scanner = NULL;
std::wcout << L"Scanner released" << std::endl;
return 0;
}