Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <plugins/openni/utils/setup.h>
24 #include <config/config.h>
25 
26 namespace fawkes {
27  namespace openni {
28 #if 0 /* just to make Emacs auto-indent happy */
29  }
30 }
31 #endif
32 
33 /** Get resolution from configuration.
34  * This method reads the config values /plugins/openni/resolution and
35  * sets the width and height fields appropriately.
36  * @param config config to read values from
37  * @param width upon return contains configured width
38  * @param height upon return contains configured height
39  */
40 void get_resolution(fawkes::Configuration *config,
41  unsigned int &width, unsigned int &height)
42 {
43 
44  std::string cfg_resolution = config->get_string("/plugins/openni/resolution");
45 
46  XnResolution res = XN_RES_VGA;
47 
48  if (cfg_resolution == "QQVGA") {
49  res = XN_RES_QQVGA;
50  } else if (cfg_resolution == "CGA") {
51  res = XN_RES_CGA;
52  } else if (cfg_resolution == "QVGA") {
53  res = XN_RES_QVGA;
54  } else if (cfg_resolution == "VGA") {
55  res = XN_RES_VGA;
56  } else if (cfg_resolution == "SVGA") {
57  res = XN_RES_SVGA;
58  } else if (cfg_resolution == "XGA") {
59  res = XN_RES_XGA;
60  } else if (cfg_resolution == "720P") {
61  res = XN_RES_720P;
62  } else if (cfg_resolution == "SXGA") {
63  res = XN_RES_SXGA;
64  } else if (cfg_resolution == "UXGA") {
65  res = XN_RES_UXGA;
66  } else if (cfg_resolution == "1080P") {
67  res = XN_RES_1080P;
68  } else {
69  throw Exception("get_resolution(): Unknown resolution '%s'",
70  cfg_resolution.c_str());
71  }
72 
73  xn::Resolution resolution(res);
74  width = resolution.GetXResolution();
75  height = resolution.GetYResolution();
76 }
77 
78 
79 /** Setup a map generator from configuration.
80  * This method reads the config values /plugins/openni/resolution and
81  * /plugins/openni/fps and uses it to setup the map output of the given map
82  * generator.
83  * @param generator generator to setup
84  * @param config config to read values from
85  */
86 void
87 setup_map_generator(xn::MapGenerator &generator,
88  fawkes::Configuration *config)
89 {
90  unsigned int width = 0, height = 0;
91  get_resolution(config, width, height);
92  unsigned int cfg_fps = config->get_uint("/plugins/openni/fps");
93 
94  XnMapOutputMode output_mode;
95  output_mode.nXRes = width;
96  output_mode.nYRes = height;
97  output_mode.nFPS = cfg_fps;
98  XnStatus st;
99  if ((st = generator.SetMapOutputMode(output_mode)) != XN_STATUS_OK) {
100  throw Exception("OpenNI: failed to set map output mode: %s",
101  xnGetStatusString(st));
102  }
103 }
104 
105 
106 /** Setup alternate viewpoint for generator.
107  * This function checks if the @p gen generator supports @p target
108  * as its alternative viewpoint. If it is supported it is setup. If not,
109  * an exception is thrown.
110  * @param gen generator which to setup to the alternate viewpoint
111  * @param target generator whose frame to use as alternate viewpoint
112  */
113 void
114 setup_alternate_viewpoint(xn::Generator &gen, xn::Generator &target)
115 {
116  if (gen.GetAlternativeViewPointCap().IsViewPointAs(target)) {
117  // already setup
118  return;
119  }
120 
121  if (! gen.GetAlternativeViewPointCap().IsViewPointSupported(target)) {
122  throw Exception("Alternate viewpoint '%s' is not supported by %s",
123  target.GetName(), gen.GetName());
124  }
125 
126  XnStatus status = gen.GetAlternativeViewPointCap().SetViewPoint(target);
127 
128  if (status != XN_STATUS_OK) {
129  throw Exception("Setting alternate viewpoint '%s' by %s failed: %s",
130  target.GetName(), gen.GetName(), xnGetStatusString(status));
131  }
132 }
133 
134 
135 /** Setup synchronization of two generators.
136  * @param gen generator which to setup synchronization for
137  * @param target generator whose frame to use as synchronization source
138  */
139 void
140 setup_synchronization(xn::Generator &gen, xn::Generator &target)
141 {
142  if (gen.GetFrameSyncCap().IsFrameSyncedWith(target)) {
143  // already setup
144  return;
145  }
146  if (! gen.IsCapabilitySupported(XN_CAPABILITY_FRAME_SYNC)) {
147  throw Exception("Generator '%s' does not support frame synchronization",
148  gen.GetName());
149  }
150 
151  if (! gen.GetFrameSyncCap().CanFrameSyncWith(target)) {
152  throw Exception("Generator '%s' cannot synchronize with '%s'",
153  gen.GetName(), target.GetName());
154  }
155 
156  XnStatus status = gen.GetFrameSyncCap().FrameSyncWith(target);
157 
158  if (status != XN_STATUS_OK) {
159  throw Exception("Setting synchronization of '%s' with '%s' failed: %s",
160  target.GetName(), gen.GetName(), xnGetStatusString(status));
161  }
162 }
163 
164 } // end namespace fawkes::openni
165 } // end namespace fawkes
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:63
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.