Fawkes API  Fawkes Development Version
setup.cpp
1 
2 /***************************************************************************
3  * setup.cpp - OpenNI utility methods: setup routines
4  *
5  * Created: Thu Mar 24 10:23:27 2011
6  * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include <plugins/openni/utils/setup.h>
23 #include <config/config.h>
24 
25 namespace fawkes {
26  namespace openni {
27 #if 0 /* just to make Emacs auto-indent happy */
28  }
29 }
30 #endif
31 
32 /** Get resolution from configuration.
33  * This method reads the config values /plugins/openni/resolution and
34  * sets the width and height fields appropriately.
35  * @param config config to read values from
36  * @param width upon return contains configured width
37  * @param height upon return contains configured height
38  */
39 void get_resolution(fawkes::Configuration *config,
40  unsigned int &width, unsigned int &height)
41 {
42 
43  std::string cfg_resolution = config->get_string("/plugins/openni/resolution");
44 
45  XnResolution res = XN_RES_VGA;
46 
47  if (cfg_resolution == "QQVGA") {
48  res = XN_RES_QQVGA;
49  } else if (cfg_resolution == "CGA") {
50  res = XN_RES_CGA;
51  } else if (cfg_resolution == "QVGA") {
52  res = XN_RES_QVGA;
53  } else if (cfg_resolution == "VGA") {
54  res = XN_RES_VGA;
55  } else if (cfg_resolution == "SVGA") {
56  res = XN_RES_SVGA;
57  } else if (cfg_resolution == "XGA") {
58  res = XN_RES_XGA;
59  } else if (cfg_resolution == "720P") {
60  res = XN_RES_720P;
61  } else if (cfg_resolution == "SXGA") {
62  res = XN_RES_SXGA;
63  } else if (cfg_resolution == "UXGA") {
64  res = XN_RES_UXGA;
65  } else if (cfg_resolution == "1080P") {
66  res = XN_RES_1080P;
67  } else {
68  throw Exception("get_resolution(): Unknown resolution '%s'",
69  cfg_resolution.c_str());
70  }
71 
72  xn::Resolution resolution(res);
73  width = resolution.GetXResolution();
74  height = resolution.GetYResolution();
75 }
76 
77 
78 /** Setup a map generator from configuration.
79  * This method reads the config values /plugins/openni/resolution and
80  * /plugins/openni/fps and uses it to setup the map output of the given map
81  * generator.
82  * @param generator generator to setup
83  * @param config config to read values from
84  */
85 void
86 setup_map_generator(xn::MapGenerator &generator,
87  fawkes::Configuration *config)
88 {
89  unsigned int width = 0, height = 0;
90  get_resolution(config, width, height);
91  unsigned int cfg_fps = config->get_uint("/plugins/openni/fps");
92 
93  XnMapOutputMode output_mode;
94  output_mode.nXRes = width;
95  output_mode.nYRes = height;
96  output_mode.nFPS = cfg_fps;
97  XnStatus st;
98  if ((st = generator.SetMapOutputMode(output_mode)) != XN_STATUS_OK) {
99  throw Exception("OpenNI: failed to set map output mode: %s",
100  xnGetStatusString(st));
101  }
102 }
103 
104 
105 /** Setup alternate viewpoint for generator.
106  * This function checks if the @p gen generator supports @p target
107  * as its alternative viewpoint. If it is supported it is setup. If not,
108  * an exception is thrown.
109  * @param gen generator which to setup to the alternate viewpoint
110  * @param target generator whose frame to use as alternate viewpoint
111  */
112 void
113 setup_alternate_viewpoint(xn::Generator &gen, xn::Generator &target)
114 {
115  if (gen.GetAlternativeViewPointCap().IsViewPointAs(target)) {
116  // already setup
117  return;
118  }
119 
120  if (! gen.GetAlternativeViewPointCap().IsViewPointSupported(target)) {
121  throw Exception("Alternate viewpoint '%s' is not supported by %s",
122  target.GetName(), gen.GetName());
123  }
124 
125  XnStatus status = gen.GetAlternativeViewPointCap().SetViewPoint(target);
126 
127  if (status != XN_STATUS_OK) {
128  throw Exception("Setting alternate viewpoint '%s' by %s failed: %s",
129  target.GetName(), gen.GetName(), xnGetStatusString(status));
130  }
131 }
132 
133 
134 /** Setup synchronization of two generators.
135  * @param gen generator which to setup synchronization for
136  * @param target generator whose frame to use as synchronization source
137  */
138 void
139 setup_synchronization(xn::Generator &gen, xn::Generator &target)
140 {
141  if (gen.GetFrameSyncCap().IsFrameSyncedWith(target)) {
142  // already setup
143  return;
144  }
145  if (! gen.IsCapabilitySupported(XN_CAPABILITY_FRAME_SYNC)) {
146  throw Exception("Generator '%s' does not support frame synchronization",
147  gen.GetName());
148  }
149 
150  if (! gen.GetFrameSyncCap().CanFrameSyncWith(target)) {
151  throw Exception("Generator '%s' cannot synchronize with '%s'",
152  gen.GetName(), target.GetName());
153  }
154 
155  XnStatus status = gen.GetFrameSyncCap().FrameSyncWith(target);
156 
157  if (status != XN_STATUS_OK) {
158  throw Exception("Setting synchronization of '%s' with '%s' failed: %s",
159  target.GetName(), gen.GetName(), xnGetStatusString(status));
160  }
161 }
162 
163 /** Get information about device used by generator.
164  * @param gen generator whose input device to query
165  * @param upon return contains the USB vendor ID
166  * @param upon return contains the USB product ID
167  * @throw exception thrown if no matching device could be found
168  */
169 void
170 get_usb_info(xn::Generator &gen, unsigned short &vendor, unsigned short &product)
171 {
172  xn::NodeInfo node_info = gen.GetInfo();
173  xn::NodeInfoList &depnodes = node_info.GetNeededNodes();
174  for (xn::NodeInfoList::Iterator n = depnodes.Begin(); n != depnodes.End(); ++n) {
175  const XnProductionNodeDescription &pnd = (*n).GetDescription();
176 
177  if ((pnd.Type == XN_NODE_TYPE_DEVICE) &&
178  (strcmp(pnd.strVendor, "PrimeSense") == 0) &&
179  (strcmp(pnd.strName, "SensorV2") == 0) )
180  {
181  // it's the primesense device node and we can check for USB vendor/product
182  unsigned short int usb_vendor = 0, usb_product = 0;
183  unsigned char bus = 0, addr = 0;
184  if (sscanf((*n).GetCreationInfo(), "%04hx/%04hx@%hhu/%hhu",
185  &usb_vendor, &usb_product, &bus, &addr) == 4) {
186  //logger->log_debug(name(), "Detected USB device "
187  // "(vendor: %04hx product: %04hx bus: %hhu addr: %hhu)",
188  // vendor, product, bus, addr);
189  vendor = usb_vendor;
190  product = usb_product;
191  return;
192  }
193  }
194  }
195 
196  throw Exception("No matching device node found to retrieve USB info from");
197 }
198 
199 } // end namespace fawkes::openni
200 } // end namespace fawkes
Fawkes library namespace.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
Interface for configuration handling.
Definition: config.h:67
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.