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 <config/config.h>
23 #include <plugins/openni/utils/setup.h>
24 
25 namespace fawkes {
26 namespace openni {
27 
28 /** Get resolution from configuration.
29  * This method reads the config values /plugins/openni/resolution and
30  * sets the width and height fields appropriately.
31  * @param config config to read values from
32  * @param width upon return contains configured width
33  * @param height upon return contains configured height
34  */
35 void
36 get_resolution(fawkes::Configuration *config, unsigned int &width, unsigned int &height)
37 {
38  std::string cfg_resolution = config->get_string("/plugins/openni/resolution");
39 
40  XnResolution res = XN_RES_VGA;
41 
42  if (cfg_resolution == "QQVGA") {
43  res = XN_RES_QQVGA;
44  } else if (cfg_resolution == "CGA") {
45  res = XN_RES_CGA;
46  } else if (cfg_resolution == "QVGA") {
47  res = XN_RES_QVGA;
48  } else if (cfg_resolution == "VGA") {
49  res = XN_RES_VGA;
50  } else if (cfg_resolution == "SVGA") {
51  res = XN_RES_SVGA;
52  } else if (cfg_resolution == "XGA") {
53  res = XN_RES_XGA;
54  } else if (cfg_resolution == "720P") {
55  res = XN_RES_720P;
56  } else if (cfg_resolution == "SXGA") {
57  res = XN_RES_SXGA;
58  } else if (cfg_resolution == "UXGA") {
59  res = XN_RES_UXGA;
60  } else if (cfg_resolution == "1080P") {
61  res = XN_RES_1080P;
62  } else {
63  throw Exception("get_resolution(): Unknown resolution '%s'", cfg_resolution.c_str());
64  }
65 
66  xn::Resolution resolution(res);
67  width = resolution.GetXResolution();
68  height = resolution.GetYResolution();
69 }
70 
71 /** Setup a map generator from configuration.
72  * This method reads the config values /plugins/openni/resolution and
73  * /plugins/openni/fps and uses it to setup the map output of the given map
74  * generator.
75  * @param generator generator to setup
76  * @param config config to read values from
77  */
78 void
79 setup_map_generator(xn::MapGenerator &generator, fawkes::Configuration *config)
80 {
81  unsigned int width = 0, height = 0;
82  get_resolution(config, width, height);
83  unsigned int cfg_fps = config->get_uint("/plugins/openni/fps");
84 
85  XnMapOutputMode output_mode;
86  output_mode.nXRes = width;
87  output_mode.nYRes = height;
88  output_mode.nFPS = cfg_fps;
89  XnStatus st;
90  if ((st = generator.SetMapOutputMode(output_mode)) != XN_STATUS_OK) {
91  throw Exception("OpenNI: failed to set map output mode: %s", xnGetStatusString(st));
92  }
93 }
94 
95 /** Setup alternate viewpoint for generator.
96  * This function checks if the @p gen generator supports @p target
97  * as its alternative viewpoint. If it is supported it is setup. If not,
98  * an exception is thrown.
99  * @param gen generator which to setup to the alternate viewpoint
100  * @param target generator whose frame to use as alternate viewpoint
101  */
102 void
103 setup_alternate_viewpoint(xn::Generator &gen, xn::Generator &target)
104 {
105  if (gen.GetAlternativeViewPointCap().IsViewPointAs(target)) {
106  // already setup
107  return;
108  }
109 
110  if (!gen.GetAlternativeViewPointCap().IsViewPointSupported(target)) {
111  throw Exception("Alternate viewpoint '%s' is not supported by %s",
112  target.GetName(),
113  gen.GetName());
114  }
115 
116  XnStatus status = gen.GetAlternativeViewPointCap().SetViewPoint(target);
117 
118  if (status != XN_STATUS_OK) {
119  throw Exception("Setting alternate viewpoint '%s' by %s failed: %s",
120  target.GetName(),
121  gen.GetName(),
122  xnGetStatusString(status));
123  }
124 }
125 
126 /** Setup synchronization of two generators.
127  * @param gen generator which to setup synchronization for
128  * @param target generator whose frame to use as synchronization source
129  */
130 void
131 setup_synchronization(xn::Generator &gen, xn::Generator &target)
132 {
133  if (gen.GetFrameSyncCap().IsFrameSyncedWith(target)) {
134  // already setup
135  return;
136  }
137  if (!gen.IsCapabilitySupported(XN_CAPABILITY_FRAME_SYNC)) {
138  throw Exception("Generator '%s' does not support frame synchronization", gen.GetName());
139  }
140 
141  if (!gen.GetFrameSyncCap().CanFrameSyncWith(target)) {
142  throw Exception("Generator '%s' cannot synchronize with '%s'", gen.GetName(), target.GetName());
143  }
144 
145  XnStatus status = gen.GetFrameSyncCap().FrameSyncWith(target);
146 
147  if (status != XN_STATUS_OK) {
148  throw Exception("Setting synchronization of '%s' with '%s' failed: %s",
149  target.GetName(),
150  gen.GetName(),
151  xnGetStatusString(status));
152  }
153 }
154 
155 /** Get information about device used by generator.
156  * @param gen generator whose input device to query
157  * @param upon return contains the USB vendor ID
158  * @param upon return contains the USB product ID
159  * @throw exception thrown if no matching device could be found
160  */
161 void
162 get_usb_info(xn::Generator &gen, unsigned short &vendor, unsigned short &product)
163 {
164  xn::NodeInfo node_info = gen.GetInfo();
165  xn::NodeInfoList &depnodes = node_info.GetNeededNodes();
166  for (xn::NodeInfoList::Iterator n = depnodes.Begin(); n != depnodes.End(); ++n) {
167  const XnProductionNodeDescription &pnd = (*n).GetDescription();
168 
169  if ((pnd.Type == XN_NODE_TYPE_DEVICE) && (strcmp(pnd.strVendor, "PrimeSense") == 0)
170  && (strcmp(pnd.strName, "SensorV2") == 0)) {
171  // it's the primesense device node and we can check for USB vendor/product
172  unsigned short int usb_vendor = 0, usb_product = 0;
173  unsigned char bus = 0, addr = 0;
174  if (sscanf(
175  (*n).GetCreationInfo(), "%04hx/%04hx@%hhu/%hhu", &usb_vendor, &usb_product, &bus, &addr)
176  == 4) {
177  //logger->log_debug(name(), "Detected USB device "
178  // "(vendor: %04hx product: %04hx bus: %hhu addr: %hhu)",
179  // vendor, product, bus, addr);
180  vendor = usb_vendor;
181  product = usb_product;
182  return;
183  }
184  }
185  }
186 
187  throw Exception("No matching device node found to retrieve USB info from");
188 }
189 
190 } // namespace openni
191 } // end namespace fawkes
Interface for configuration handling.
Definition: config.h:68
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Fawkes library namespace.