vision_master.h

00001
00002 /***************************************************************************
00003  *  vision_master.h - FireVision Vision Master
00004  *
00005  *  Created: Wed May 30 10:28:06 2007
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #ifndef __FIREVISION_FVUTILS_BASE_VISION_MASTER_H_
00025 #define __FIREVISION_FVUTILS_BASE_VISION_MASTER_H_
00026 
00027 #include <fvutils/color/colorspaces.h>
00028 #include <cams/control/control.h>
00029 #include <core/utils/refptr.h>
00030 #include <core/exceptions/software.h>
00031
00032 #include <typeinfo>
00033
00034 class Camera;
00035 namespace fawkes {
00036   class Thread;
00037   class TypeMismatchException;
00038 }
00039
00040 class VisionMaster
00041 {
00042  public:
00043   virtual ~VisionMaster();
00044
00045   virtual Camera *  register_for_camera(const char *camera_string,
00046                                         fawkes::Thread *thread,
00047                                         colorspace_t cspace = YUV422_PLANAR) = 0;
00048   virtual Camera *  register_for_raw_camera(const char *camera_string,
00049                                             fawkes::Thread *thread)          = 0;
00050   virtual void      unregister_thread(fawkes::Thread *thread)                = 0;
00051
00052   virtual CameraControl *acquire_camctrl(const char *cam_string)             = 0;
00053   virtual void           release_camctrl(CameraControl *cc)                  = 0;
00054
00055 
00056  /** Retrieve a typed camera control instance.
00057   * Like the non-template method this class will try to instantiate the camera
00058   * control based on the camera string (see there for details on the two possible
00059   * contents of the string). The camera control will be verified to be of the
00060   * desired type.
00061   * @param camera_string camera string of camera for the control or the argument
00062   * string for a new instance. See documentation of non-template method.
00063   * @return typed camera control instance
00064   * @exception TypeMismatchException thrown if requested camera control does not
00065   * match the requested type.
00066   */
00067   template <class CC>
00068     CC *
00069     acquire_camctrl(const char *camera_string);
00070 
00071  /** Retrieve a typed camera control instance.
00072   * Like the non-template method this class will try to instantiate the camera
00073   * control based on the camera string (see there for details on the two possible
00074   * contents of the string). The camera control will be verified to be of the
00075   * desired type.
00076   * Unlike the other template method you do not need to explicitly state the type
00077   * of the requested type as it is inferred based on the \p cc argument. You can
00078   * write something like this:
00079   * @code
00080   * CameraControlImage *cci = vision_master->acquire_camctrl("camstring...", cci);
00081   * @endcode
00082   * instead of
00083   * @code
00084   * CameraControlImage *cci = vision_master->acquire_camctrl<CameraControlImage>("camstring...");
00085   * @endcode
00086   * @param camera_string camera string of camera for the control or the argument
00087   * string for a new instance. See documentation of non-template method.
00088   * @param cc reference to pointer of camera control of the intended type, used
00089   * to automatically infer the template method. On successful return points to
00090   * the camera control instance
00091   * @return typed camera control instance (same as \p cc)
00092   * @exception TypeMismatchException thrown if requested camera control does not
00093   * match the requested type.
00094   */
00095   template <class CC>
00096     CC *
00097     acquire_camctrl(const char *camera_string, CC *&cc);
00098
00099 
00100   /** Get typed raw camera.
00101    * Like the non-template variant this method can be used to get access to
00102    * the raw camera implementation, without going through a proxy. See the other
00103    * method for risks and implication of using the raw device.
00104    * @param camera_string camera that can be used by CameraFactory to open a
00105    * camera.
00106    * @param thread thread to register for this camera
00107    */
00108   template <class CC>
00109     CC *
00110     register_for_raw_camera(const char *camera_string, fawkes::Thread *thread);
00111
00112
00113  protected:
00114   virtual CameraControl *acquire_camctrl(const char *cam_string,
00115                                          const std::type_info &typeinf) = 0;
00116
00117 };
00118
00119 template <class CC>
00120 CC *
00121 VisionMaster::acquire_camctrl(const char *camera_string, CC *&cc)
00122 {
00123   const std::type_info &t = typeid(CC);
00124   CameraControl *pcc = acquire_camctrl(camera_string, t);
00125   CC *tcc = dynamic_cast<CC *>(pcc);
00126   if (tcc) {
00127     if (cc) cc = tcc;
00128     return tcc;
00129   } else {
00130     release_camctrl(tcc);
00131     throw fawkes::TypeMismatchException("CameraControl defined by string does "
00132                                         "not match desired type");
00133   }
00134 }
00135
00136
00137 template <class CC>
00138 CC *
00139 VisionMaster::acquire_camctrl(const char *camera_string)
00140 {
00141   const std::type_info &t = typeid(CC);
00142   CameraControl *pcc = acquire_camctrl(camera_string, t);
00143   CC *tcc = dynamic_cast<CC *>(pcc);
00144   if (tcc) {
00145     return tcc;
00146   } else {
00147     release_camctrl(tcc);
00148     throw fawkes::TypeMismatchException("CameraControl defined by string does "
00149                                         "not match desired type");
00150   }
00151 }
00152
00153 template <class CC>
00154 CC *
00155 VisionMaster::register_for_raw_camera(const char *camera_string, fawkes::Thread *thread)
00156 {
00157   Camera *camera = register_for_raw_camera(camera_string, thread);
00158   CC *tcc = dynamic_cast<CC *>(camera);
00159   if (tcc) {
00160     return tcc;
00161   } else {
00162     unregister_thread(thread);
00163     throw fawkes::TypeMismatchException("Camera defined by string does "
00164                                         "not match desired type");
00165   }
00166 }
00167
00168 #endif