From 928e639d18577bb51d5f577867f338ae416e7042 Mon Sep 17 00:00:00 2001
From: Angel Santamaria-Navarro <asantamaria@iri.upc.edu>
Date: Thu, 15 Dec 2016 11:22:56 +0100
Subject: [PATCH] prepared to work with external device manager to have several
 cameras

---
 src/examples/mvbluefox3_test.cpp | 125 ++++++++++++++++++++++++-------
 src/mvbluefox3.cpp               |  70 +++--------------
 src/mvbluefox3.h                 |  13 +---
 3 files changed, 110 insertions(+), 98 deletions(-)

diff --git a/src/examples/mvbluefox3_test.cpp b/src/examples/mvbluefox3_test.cpp
index 19db022..6bf7122 100644
--- a/src/examples/mvbluefox3_test.cpp
+++ b/src/examples/mvbluefox3_test.cpp
@@ -1,7 +1,8 @@
 #include "mvbluefox3.h"
-#include <boost/timer.hpp>
 #include <unistd.h>
 
+#include <sstream>
+
 #ifdef HAVE_OPENCV_H
 #include <opencv2/core/version.hpp>
 #if CV_MAJOR_VERSION == 3
@@ -9,7 +10,7 @@
 #endif
 #endif
 
-#define WINDOW_NAME "mvBlueFOX3 TEST"
+typedef bool( *SUPPORTED_DEVICE_CHECK )( const mvIMPACT::acquire::Device* const );
 
 mvIMPACT::acquire::DeviceManager devMgr_;            // Device Manager.
 
@@ -20,52 +21,122 @@ std::string GetSerialFromUser(void)
   return getDeviceFromUserInput(devMgr)->serial.read();
 }
 
+std::string AvailableDevices(SUPPORTED_DEVICE_CHECK pSupportedDeviceCheckFn) 
+{
+  mvIMPACT::acquire::DeviceManager devMgr; // Device Manager.
+  std::ostringstream devices;
+  devices << "Available device(s): ";
+  const unsigned int devCnt = devMgr.deviceCount();
+  if (devCnt == 0)
+  {
+    devices << "0.";
+  }
+  else
+  {
+    for( unsigned int i = 0; i < devCnt; i++ )
+    {
+      Device* pDev = devMgr_[i];
+      if( pDev )
+      {
+        if( !pSupportedDeviceCheckFn || pSupportedDeviceCheckFn( pDev ) )
+        {
+          devices << " \n  - " << pDev->product.read() << ": " << pDev->serial.read();
+        }
+      }
+    }
+  }
+  return devices.str();
+}
+
 int main(int argc, char *argv[])
 {
-  CMvbluefox3::CMvbluefox3* cam_ptr = NULL;
+  int num_cams = 2;
+
+  std::vector<std::string> serial;
+  serial.resize(num_cams);
+  serial[0] = "F0300141";
+  serial[1] = "F0300123";
 
+#if defined(HAVE_OPENCV_H) && CV_MAJOR_VERSION == 3
+  std::vector<std::string> window_name;
+  window_name.resize(num_cams);
+  window_name[0] = serial[0];
+  window_name[1] = serial[1];
+#endif
+
+  std::vector<CMvbluefox3::CMvbluefox3*> cam_ptr;
+  cam_ptr.resize(num_cams);
+  for (int ii = 0; ii < num_cams; ++ii)
+  {
+    cam_ptr[ii] = NULL;
+
+    if( !serial[ii].empty() )
+    {
+      std::cout << "[mvBlueFOX3]: Trying to open device with serial: " << serial[ii] << std::endl;
+
+      if (devMgr_.getDeviceBySerial(serial[ii]) == 0)
+      {
+        std::cout << "No device found! Unable to continue. " << std::endl;
+      }
+      else
+      {
+        // create an interface to the found device 
+        cam_ptr[ii] = new CMvbluefox3::CMvbluefox3(devMgr_.getDeviceBySerial(serial[ii]));
+      }
+    }
+    else
+      std::cout << "Device cannot be found because serial number is not specified." << std::endl;
+  }
+  
   try {
-    // cam_ptr = new CMvbluefox3::CMvbluefox3(GetSerialFromUser());
-    cam_ptr = new CMvbluefox3::CMvbluefox3("F0300141",devMgr_);
 
-    CMvbluefox3::CParams params; //default parameters.
-    cam_ptr->SetConfig(params);
+    for (int ii = 0; ii < num_cams; ++ii)
+    {
+      CMvbluefox3::CParams params; //default parameters.
+      cam_ptr[ii]->SetConfig(params);
+    }
 
 #if defined(HAVE_OPENCV_H) && CV_MAJOR_VERSION == 3
     std::cout << "[Camera test]: Acquiring images." << std::endl;
 
-    cv::namedWindow( WINDOW_NAME, cv::WINDOW_NORMAL );
-    
-    boost::timer t;
-    double duration;
+    for (int ii = 0; ii < num_cams; ++ii)
+      cv::namedWindow( window_name[ii], cv::WINDOW_NORMAL );
 
     while (true) 
     {
-      t.restart();
-      cv::Mat image;
-      cam_ptr->GetImageCV(image);
-      cv::imshow( WINDOW_NAME, image );
+      for (int ii = 0; ii < num_cams; ++ii)
+      {
+        cv::Mat image;
+        cam_ptr[ii]->GetImageCV(image);
+        cv::imshow( window_name[ii], image );
+      }
       if(cv::waitKey(30) >= 0) break;
-      std::cout << '\r' << "fps:" << 1.0/t.elapsed() << std::flush;
     }
-    std::cout << std::endl;
 
-    cv::destroyAllWindows();
+    cv::destroyAllWindows();    
 #else
-    char *image=NULL;
-    cam_ptr->GetImage(&image);
-    std::cout << "[Camera test]: Frame acquired successfully." 
-    << "OpenCV 3 is required and not installed, thus visualization is avoided." << std::endl;
-    delete [] image;
+
+    for (int ii = 0; ii < num_cams; ++ii)
+    {
+      char *image=NULL;
+      cam_ptr[ii]->GetImage(&image);
+      std::cout << "[Camera test]: CAM: " << serial[ii] << ". Frame acquired successfully." << "OpenCV 3 is required and not installed, thus visualization is avoided." << std::endl;
+      delete [] image;
+    }
+
 #endif
+
     std::cout << "[Camera test]: Finished successfully." << std::endl;
   }catch (CMvbluefox3::CmvBlueFOX3Exception& e) {
     std::cout << e.what() << std::endl;
   }
-
-  if (cam_ptr != NULL) 
+  
+  for (int ii = 0; ii < num_cams; ++ii)
   {
-    cam_ptr = NULL;
-    delete [] cam_ptr;
+    if (cam_ptr[ii] != NULL) 
+    {
+      cam_ptr[ii] = NULL;
+      delete [] cam_ptr[ii];
+    }
   }
 }
diff --git a/src/mvbluefox3.cpp b/src/mvbluefox3.cpp
index 2fcb41e..bac72dc 100644
--- a/src/mvbluefox3.cpp
+++ b/src/mvbluefox3.cpp
@@ -17,40 +17,18 @@
 
 namespace CMvbluefox3 {
 
-CMvbluefox3::CMvbluefox3(const std::string &serial, const mvIMPACT::acquire::DeviceManager &dMan): devMgr_(dMan)
+CMvbluefox3::CMvbluefox3(const mvIMPACT::acquire::Device *device)
 {
-  if( !serial.empty() )
-  {
-    std::cout << "[mvBlueFOX3]: Trying to open device with serial: " << serial << std::endl;
-
-    // if (this->devMgr_.getDeviceBySerial(serial) == 0)
-    if (devMgr_.getDeviceBySerial(serial) == 0)
-    {
-      throw CmvBlueFOX3Exception(_HERE_, "No device found! Unable to continue. " + AvailableDevices());
-    }
-    else
-    {
-      // create an interface to the found device 
-      // this->device_ = this->devMgr_[0];
-      this->device_ = devMgr_[0];
-
-      // Initialize other vars
-      this->timeout_ms_ = -1;
-      this->linepitch_ = 8;
-      this->depth_ = 8;
-      this->bytes_per_pixel_ = 1;
+  // Initialize vars
+  this->device_ = new mvIMPACT::acquire::Device(*device);
+  this->timeout_ms_ = -1;
+  this->linepitch_ = 8;
+  this->depth_ = 8;
+  this->bytes_per_pixel_ = 1;
 
-      // displayPropertyData(this->device_->defaultRequestCount);
-      // WriteProperty(this->device_->defaultRequestCount,1);
-
-      // open device
-      OpenDevice(serial);
-    }
-  }
-  else
-    throw CmvBlueFOX3Exception(_HERE_, "Device cannot be found because serial number is not specified.");
+  OpenDevice();
 }
- 
+
 CMvbluefox3::~CMvbluefox3()
 {
   // Close device if necessary.  
@@ -69,35 +47,7 @@ CMvbluefox3::~CMvbluefox3()
   DelPtr(this->stats_);
 }
 
-std::string CMvbluefox3::AvailableDevices(SUPPORTED_DEVICE_CHECK pSupportedDeviceCheckFn) const 
-{
-  std::ostringstream devices;
-  devices << "Available device(s): ";
-  // const unsigned int devCnt = this->devMgr_.deviceCount();
-  const unsigned int devCnt = devMgr_.deviceCount();
-  if (devCnt == 0)
-  {
-    devices << "0.";
-  }
-  else
-  {
-    for( unsigned int i = 0; i < devCnt; i++ )
-    {
-      // Device* pDev = this->devMgr_[i];
-      Device* pDev = devMgr_[i];
-      if( pDev )
-      {
-        if( !pSupportedDeviceCheckFn || pSupportedDeviceCheckFn( pDev ) )
-        {
-          devices << " \n  - " << pDev->product.read() << ": " << pDev->serial.read();
-        }
-      }
-    }
-  }
-  return devices.str();
-}
-
-void CMvbluefox3::OpenDevice(const std::string &serial)
+void CMvbluefox3::OpenDevice(void)
 {
   // Open the device
   try { this->device_->open(); }
diff --git a/src/mvbluefox3.h b/src/mvbluefox3.h
index 62daedb..a00cfe5 100644
--- a/src/mvbluefox3.h
+++ b/src/mvbluefox3.h
@@ -13,8 +13,6 @@ namespace cv { class Mat; }
 
 namespace CMvbluefox3 {
 
-typedef bool( *SUPPORTED_DEVICE_CHECK )( const mvIMPACT::acquire::Device* const );
-
 /** 
  * \brief Camera parameters
  *
@@ -173,19 +171,12 @@ class CMvbluefox3
       }
     }
 
-    /**
-     * \brief Available Device
-     *
-     *  Returns a string with currently available devices.
-     */
-    std::string AvailableDevices(SUPPORTED_DEVICE_CHECK pSupportedDeviceCheckFn = 0) const; 
-
     /**
      * \brief Open device
      *
      *  Open device.
      */
-    void OpenDevice(const std::string &serial);
+    void OpenDevice(void);
 
     /**
      * \brief Get image request format
@@ -366,7 +357,7 @@ class CMvbluefox3
      *
      * Class constructor.
      */
-    CMvbluefox3(const std::string &serial, const mvIMPACT::acquire::DeviceManager &dMan);
+    CMvbluefox3(const mvIMPACT::acquire::Device *device);
 
     /**
      * \brief Class destructor
-- 
GitLab