GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gui/python-bindings.cc Lines: 0 113 0.0 %
Date: 2024-04-14 11:13:22 Branches: 0 226 0.0 %

Line Branch Exec Source
1
// Copyright (c) 2019, LAAS-CNRS
2
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3
//
4
// This file is part of gepetto-viewer.
5
// gepetto-viewer is free software: you can redistribute it
6
// and/or modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation, either version
8
// 3 of the License, or (at your option) any later version.
9
//
10
// gepetto-viewer is distributed in the hope that it will be
11
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
// General Lesser Public License for more details.  You should have
14
// received a copy of the GNU Lesser General Public License along with
15
// gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16
17
#include "../../src/gui/python-bindings.hh"
18
19
#include <gepetto/viewer/windows-manager.h>
20
21
#include <boost/python.hpp>
22
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
23
#include <gepetto/gui/windows-manager.hh>
24
25
namespace bp = boost::python;
26
namespace gv = gepetto::viewer;
27
namespace gg = gepetto::gui;
28
using boost::noncopyable;
29
30
#define GV_DEF(func) .def(#func, &WindowsManager::func)
31
#define GV_DEF0(func, ret) \
32
  .def(#func, static_cast<ret (WindowsManager::*)()>(&WindowsManager::func))
33
#define GV_DEF1(func, ret, arg0) \
34
  .def(#func, static_cast<ret (WindowsManager::*)(arg0)>(&WindowsManager::func))
35
#define GV_DEF2(func, ret, arg0, arg1)                          \
36
  .def(#func, static_cast<ret (WindowsManager::*)(arg0, arg1)>( \
37
                  &WindowsManager::func))
38
#define GV_DEF3(func, ret, arg0, arg1, arg2)                          \
39
  .def(#func, static_cast<ret (WindowsManager::*)(arg0, arg1, arg2)>( \
40
                  &WindowsManager::func))
41
#define GV_DEF4(func, ret, arg0, arg1, arg2, arg3)                          \
42
  .def(#func, static_cast<ret (WindowsManager::*)(arg0, arg1, arg2, arg3)>( \
43
                  &WindowsManager::func))
44
45
struct to_python_converters {
46
  static PyObject* convert(const osgVector3& v) {
47
    return bp::incref(bp::make_tuple(v[0], v[1], v[2]).ptr());
48
  }
49
50
  static PyObject* convert(const osgQuat& v) {
51
    return bp::incref(bp::make_tuple(v[0], v[1], v[2], v[3]).ptr());
52
  }
53
54
  static PyObject* convert(const gv::Configuration& v) {
55
    return bp::incref(bp::make_tuple(v.position[0], v.position[1],
56
                                     v.position[2], v.quat[0], v.quat[1],
57
                                     v.quat[2], v.quat[3])
58
                          .ptr());
59
  }
60
};
61
62
template <typename V>
63
inline void set(V& v, int i, const double& d) {
64
  v[i] = (float)d;
65
}
66
inline void set(gv::Configuration& v, int i, const double& d) {
67
  if (i < 3)
68
    v.position[i] = (float)d;
69
  else
70
    v.quat[i - 3] = d;
71
}
72
73
template <typename V, int N>
74
struct from_python_converters {
75
  from_python_converters() {
76
    boost::python::converter::registry::push_back(&convertible, &construct,
77
                                                  boost::python::type_id<V>());
78
  }
79
80
  // Determine if obj_ptr can be converted in a QString
81
  static void* convertible(PyObject* obj_ptr) {
82
    if (!PyIter_Check(obj_ptr)) return 0;
83
    PyObject* iterator = PyObject_GetIter(obj_ptr);
84
    PyObject* item;
85
    for (int i = 0; i < N; ++i) {
86
      item = PyIter_Next(iterator);
87
      if (item == NULL) {
88
        Py_DECREF(iterator);
89
        return 0;
90
      }
91
      bool isFloat = PyFloat_Check(item);
92
      Py_DECREF(item);
93
      if (!isFloat) {
94
        Py_DECREF(iterator);
95
        return 0;
96
      }
97
    }
98
    item = PyIter_Next(iterator);
99
    Py_DECREF(iterator);
100
    if (item != NULL) {
101
      Py_DECREF(item);
102
      return 0;
103
    }
104
    return obj_ptr;
105
  }
106
107
  // Convert obj_ptr into a QString
108
  static void construct(
109
      PyObject* obj_ptr,
110
      boost::python::converter::rvalue_from_python_stage1_data* data) {
111
    // Grab pointer to memory into which to construct the new QString
112
    void* storage =
113
        ((boost::python::converter::rvalue_from_python_storage<V>*)data)
114
            ->storage.bytes;
115
116
    // in-place construct the new QString using the character data
117
    // extraced from the python object
118
    new (storage) V;
119
120
    V& value(*(V*)storage);
121
122
    PyObject* iterator = PyObject_GetIter(obj_ptr);
123
    for (int i = 0; i < N; ++i) {
124
      PyObject* item = PyIter_Next(iterator);
125
      set(value, i, PyFloat_AsDouble(item));
126
      Py_DECREF(item);
127
    }
128
    Py_DECREF(iterator);
129
130
    // Stash the memory chunk pointer for later use by boost.python
131
    data->convertible = storage;
132
  }
133
};
134
135
void exposeOSG() {
136
  bp::class_<std::vector<std::string> >("string_vector")
137
      .def(bp::vector_indexing_suite<std::vector<std::string> >());
138
  bp::to_python_converter<osgVector3, to_python_converters>();
139
  bp::to_python_converter<osgQuat, to_python_converters>();
140
  bp::to_python_converter<gv::Configuration, to_python_converters>();
141
142
  from_python_converters<osgVector3, 3>();
143
  from_python_converters<osgQuat, 4>();
144
  from_python_converters<gv::Configuration, 7>();
145
}
146
147
void exposeGV() {
148
  typedef gepetto::viewer::WindowsManager WindowsManager;
149
150
  bp::class_<WindowsManager, noncopyable>("WindowsManagerBase", bp::no_init) GV_DEF(
151
      getNodeList) GV_DEF(getGroupNodeList) GV_DEF(getSceneList) GV_DEF(getWindowList)
152
153
      GV_DEF(getWindowID)
154
155
          GV_DEF(createScene) GV_DEF(createSceneWithFloor) GV_DEF(
156
              addSceneToWindow)
157
158
              GV_DEF(attachCameraToNode) GV_DEF(detachCamera)
159
160
                  GV_DEF(nodeExists)
161
162
                      GV_DEF(addFloor) GV_DEF(addBox) GV_DEF(addCapsule) GV_DEF(resizeCapsule) GV_DEF(
163
                          addArrow) GV_DEF(resizeArrow) GV_DEF(addRod) GV_DEF(addMesh) GV_DEF(removeLightSources)
164
                          GV_DEF(addCone) GV_DEF(addCylinder) GV_DEF(addSphere) GV_DEF(
165
                              addLight) GV_DEF(addLine) GV_DEF(setLineStartPoint) GV_DEF(setLineEndPoint)
166
                              GV_DEF(setLineExtremalPoints) GV_DEF(addCurve) GV_DEF(
167
                                  setCurvePoints) GV_DEF(setCurveMode) GV_DEF(setCurvePointsSubset)
168
                                  GV_DEF(setCurveLineWidth) GV_DEF(addSquareFace) GV_DEF(
169
                                      setTexture) GV_DEF(addTriangleFace) GV_DEF(addXYZaxis)
170
171
                                      GV_DEF(createRoadmap) GV_DEF(
172
                                          addEdgeToRoadmap) GV_DEF(addNodeToRoadmap)
173
174
                                          GV_DEF2(
175
                                              addURDF, bool, const std::string&,
176
                                              const std::
177
                                                  string&) GV_DEF3(addURDF,
178
                                                                   bool,
179
                                                                   const std::
180
                                                                       string&,
181
                                                                   const std::
182
                                                                       string&,
183
                                                                   const std::
184
                                                                       string&)
185
                                              GV_DEF2(
186
                                                  addUrdfCollision, bool,
187
                                                  const std::string&,
188
                                                  const std::
189
                                                      string&) GV_DEF3(addUrdfCollision,
190
                                                                       bool,
191
                                                                       const std::
192
                                                                           string&,
193
                                                                       const std::
194
                                                                           string&,
195
                                                                       const std::
196
                                                                           string&)
197
                                                  GV_DEF3(
198
                                                      addUrdfObjects, void,
199
                                                      const std::string&,
200
                                                      const std::string&,
201
                                                      bool) GV_DEF4(addUrdfObjects,
202
                                                                    void,
203
                                                                    const std::
204
                                                                        string&,
205
                                                                    const std::
206
                                                                        string&,
207
                                                                    const std::
208
                                                                        string&,
209
                                                                    bool)
210
211
                                                      GV_DEF(createGroup) GV_DEF(
212
                                                          addToGroup)
213
                                                          GV_DEF(removeFromGroup) GV_DEF(
214
                                                              deleteNode)
215
216
                                                              GV_DEF(
217
                                                                  applyConfiguration)
218
                                                                  GV_DEF(
219
                                                                      applyConfigurations)
220
221
                                                                      GV_DEF(
222
                                                                          addLandmark)
223
                                                                          GV_DEF(
224
                                                                              deleteLandmark)
225
226
      // virtual Configuration getStaticTransform (const std::string& nodeName)
227
      // const;
228
      GV_DEF(setStaticTransform)
229
230
          GV_DEF(setVisibility) GV_DEF2(
231
              setScale, bool, const std::string&,
232
              const osgVector3&) GV_DEF2(setScale, bool, const std::string&,
233
                                         const float&) GV_DEF2(setScale, bool,
234
                                                               const std::
235
                                                                   string&,
236
                                                               const int&)
237
              GV_DEF(setColor) GV_DEF(setWireFrameMode) GV_DEF(setLightingMode) GV_DEF(
238
                  setHighlight) GV_DEF2(setAlpha, bool, const std::string&,
239
                                        const float&) GV_DEF2(setAlpha, bool,
240
                                                              const std::
241
                                                                  string&,
242
                                                              const int&)
243
244
                  GV_DEF(setCaptureTransform) GV_DEF(
245
                      captureTransformOnRefresh) GV_DEF(captureTransform)
246
                      GV_DEF(writeBlenderScript) GV_DEF(writeNodeFile) GV_DEF(
247
                          writeWindowFile) GV_DEF(setBackgroundColor1)
248
                          GV_DEF(setBackgroundColor2) GV_DEF(
249
                              getCameraTransform) GV_DEF(setCameraTransform)
250
251
                              GV_DEF(getPropertyNames) GV_DEF(getPropertyTypes)
252
253
                                  GV_DEF(getStringProperty) GV_DEF(
254
                                      setStringProperty) GV_DEF(getVector2Property)
255
                                      GV_DEF(setVector2Property) GV_DEF(
256
                                          getVector3Property)
257
                                          GV_DEF(setVector3Property) GV_DEF(
258
                                              getColorProperty)
259
                                              GV_DEF(setColorProperty) GV_DEF(
260
                                                  getFloatProperty)
261
                                                  GV_DEF(setFloatProperty) GV_DEF(
262
                                                      getBoolProperty)
263
                                                      GV_DEF(setBoolProperty)
264
                                                          GV_DEF(getIntProperty)
265
                                                              GV_DEF(
266
                                                                  setIntProperty)
267
268
      // WindowManagerPtr_t getWindowManager (const WindowID wid, bool
269
      // throwIfDoesntExist = false) const; GroupNodePtr_t getGroup (const
270
      // std::string groupName, bool throwIfDoesntExist = false) const;
271
      // NodePtr_t getNode (const std::string& nodeName, bool throwIfDoesntExist
272
      // = false) const; Configuration getNodeGlobalTransform(const std::string
273
      // nodeName) const;
274
      ;
275
}
276
277
void exposeGG() {
278
  typedef gepetto::gui::WindowsManager WindowsManager;
279
  using boost::noncopyable;
280
281
  bp::class_<WindowsManager, bp::bases<gv::WindowsManager>, noncopyable>(
282
      "WindowsManager",
283
      bp::no_init) GV_DEF(captureFrame) GV_DEF(startCapture) GV_DEF(stopCapture)
284
285
      GV_DEF(refresh)
286
287
          GV_DEF1(createWindow, WindowsManager::WindowID, const std::string&);
288
}
289
290
#undef GV_DEF
291
#undef GV_DEF0
292
#undef GV_DEF1
293
#undef GV_DEF2
294
#undef GV_DEF3
295
#undef GV_DEF4