Fawkes API  Fawkes Development Version
setup.cpp
00001 
00002 /***************************************************************************
00003  *  setup.cpp - OpenNI utility methods: setup routines
00004  *
00005  *  Created: Thu Mar 24 10:23:27 2011
00006  *  Copyright  2006-2011  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.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <plugins/openni/utils/setup.h>
00024 #include <config/config.h>
00025 
00026 namespace fawkes {
00027   namespace openni {
00028 #if 0 /* just to make Emacs auto-indent happy */
00029   }
00030 }
00031 #endif
00032 
00033 /** Get resolution from configuration.
00034  * This method reads the config values /plugins/openni/resolution and
00035  * sets the width and height fields appropriately.
00036  * @param config config to read values from
00037  * @param width upon return contains configured width
00038  * @param height upon return contains configured height
00039  */
00040 void get_resolution(fawkes::Configuration *config,
00041                     unsigned int &width, unsigned int &height)
00042 {
00043   
00044   std::string  cfg_resolution = config->get_string("/plugins/openni/resolution");
00045 
00046   XnResolution res = XN_RES_VGA;
00047 
00048   if (cfg_resolution == "QQVGA") {
00049     res = XN_RES_QQVGA;
00050   } else if (cfg_resolution == "CGA") {
00051     res = XN_RES_CGA;
00052   } else if (cfg_resolution == "QVGA") {
00053     res = XN_RES_QVGA;
00054   } else if (cfg_resolution == "VGA") {
00055     res = XN_RES_VGA;
00056   } else if (cfg_resolution == "SVGA") {
00057     res = XN_RES_SVGA;
00058   } else if (cfg_resolution == "XGA") {
00059     res = XN_RES_XGA;
00060   } else if (cfg_resolution == "720P") {
00061     res = XN_RES_720P;
00062   } else if (cfg_resolution == "SXGA") {
00063     res = XN_RES_SXGA;
00064   } else if (cfg_resolution == "UXGA") {
00065     res = XN_RES_UXGA;
00066   } else if (cfg_resolution == "1080P") {
00067     res = XN_RES_1080P;
00068   } else {
00069     throw Exception("get_resolution(): Unknown resolution '%s'",
00070                     cfg_resolution.c_str());
00071   }
00072 
00073   xn::Resolution resolution(res);
00074   width  = resolution.GetXResolution();
00075   height = resolution.GetYResolution();
00076 }
00077 
00078 
00079 /** Setup a map generator from configuration.
00080  * This method reads the config values /plugins/openni/resolution and
00081  * /plugins/openni/fps and uses it to setup the map output of the given map
00082  * generator.
00083  * @param generator generator to setup
00084  * @param config config to read values from
00085  */
00086 void
00087 setup_map_generator(xn::MapGenerator &generator,
00088                     fawkes::Configuration *config)
00089 {
00090   unsigned int width = 0, height = 0;
00091   get_resolution(config, width, height);
00092   unsigned int cfg_fps  = config->get_uint("/plugins/openni/fps");
00093 
00094   XnMapOutputMode output_mode;
00095   output_mode.nXRes = width;
00096   output_mode.nYRes = height;
00097   output_mode.nFPS  = cfg_fps;
00098   XnStatus st;
00099   if ((st = generator.SetMapOutputMode(output_mode)) != XN_STATUS_OK) {
00100     throw Exception("OpenNI: failed to set map output mode: %s",
00101                     xnGetStatusString(st));
00102   }
00103 }
00104 
00105 
00106 /** Setup alternate viewpoint for generator.
00107  * This function checks if the @p gen generator supports @p target
00108  * as its alternative viewpoint. If it is supported it is setup. If not,
00109  * an exception is thrown.
00110  * @param gen generator which to setup to the alternate viewpoint
00111  * @param target generator whose frame to use as alternate viewpoint
00112  */
00113 void
00114 setup_alternate_viewpoint(xn::Generator &gen, xn::Generator &target)
00115 {
00116   if (gen.GetAlternativeViewPointCap().IsViewPointAs(target)) {
00117     // already setup
00118     return;
00119   }
00120 
00121   if (! gen.GetAlternativeViewPointCap().IsViewPointSupported(target)) {
00122     throw Exception("Alternate viewpoint '%s' is not supported by %s",
00123                     target.GetName(), gen.GetName());
00124   }
00125 
00126   XnStatus status = gen.GetAlternativeViewPointCap().SetViewPoint(target);
00127 
00128   if (status != XN_STATUS_OK) {
00129     throw Exception("Setting alternate viewpoint '%s' by %s failed: %s",
00130                     target.GetName(), gen.GetName(), xnGetStatusString(status));
00131   }
00132 }
00133 
00134 
00135 /** Setup synchronization of two generators.
00136  * @param gen generator which to setup synchronization for
00137  * @param target generator whose frame to use as synchronization source
00138  */
00139 void
00140 setup_synchronization(xn::Generator &gen, xn::Generator &target)
00141 {
00142   if (gen.GetFrameSyncCap().IsFrameSyncedWith(target)) {
00143     // already setup
00144     return;
00145   }
00146   if (! gen.IsCapabilitySupported(XN_CAPABILITY_FRAME_SYNC)) {
00147     throw Exception("Generator '%s' does not support frame synchronization",
00148                     gen.GetName());
00149   }
00150 
00151   if (! gen.GetFrameSyncCap().CanFrameSyncWith(target)) {
00152     throw Exception("Generator '%s' cannot synchronize with '%s'",
00153                     gen.GetName(), target.GetName());
00154   }
00155 
00156   XnStatus status = gen.GetFrameSyncCap().FrameSyncWith(target);
00157 
00158   if (status != XN_STATUS_OK) {
00159     throw Exception("Setting synchronization of '%s' with '%s' failed: %s",
00160                     target.GetName(), gen.GetName(), xnGetStatusString(status));
00161   }
00162 }
00163 
00164 } // end namespace fawkes::openni
00165 } // end namespace fawkes