-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcvisualmodule.cpp
More file actions
181 lines (155 loc) · 3.92 KB
/
cvisualmodule.cpp
File metadata and controls
181 lines (155 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// This file takes roughly 115 MB RAM to compile.
// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// Copyright (c) 2003, 2004 by Jonathan Brandmeyer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.
#include <stdexcept>
#include <exception>
#include <iostream>
#include <boost/python/exception_translator.hpp>
#include <boost/python/module.hpp>
#include <boost/python/numeric.hpp>
#include <boost/python/def.hpp>
#define PY_ARRAY_UNIQUE_SYMBOL visual_PyArrayHandle
//#include <numpy/arrayobject.h>
#include "util/rate.hpp"
#include "util/errors.hpp"
#include "python/num_util.hpp"
#include "python/gil.hpp"
// Python 2/3 compatibility
#ifndef PyString_Check
#define PyString_Check PyUnicode_Check
#endif
namespace cvisual {
void wrap_display_kernel();
void wrap_primitive();
void wrap_rgba();
void wrap_vector();
void wrap_arrayobjects();
void
translate_std_out_of_range( std::out_of_range e)
{
PyErr_SetString( PyExc_IndexError, e.what());
}
void
translate_std_invalid_argument( std::invalid_argument e)
{
PyErr_SetString( PyExc_ValueError, e.what());
}
void
translate_std_runtime_error( std::runtime_error e)
{
PyErr_SetString( PyExc_RuntimeError, e.what());
}
void
py_rate( double freq)
{
python::gil_release R;
rate(freq);
}
namespace py = boost::python;
struct double_from_int
{
double_from_int()
{
py::converter::registry::push_back(
&convertible,
&construct,
py::type_id<double>());
}
static void* convertible( PyObject* obj)
{
PyObject* newobj = PyNumber_Float(obj);
if (!PyString_Check(obj) && newobj) {
Py_DECREF(newobj);
return obj;
} else {
if (newobj) {
Py_DECREF(newobj);
}
PyErr_Clear();
return 0;
}
}
static void construct(
PyObject* _obj,
py::converter::rvalue_from_python_stage1_data* data)
{
PyObject* newobj = PyNumber_Float(_obj);
double* storage = (double*)(
(py::converter::rvalue_from_python_storage<double>*)
data)->storage.bytes;
*storage = py::extract<double>(newobj);
Py_DECREF(newobj);
data->convertible = storage;
}
};
struct float_from_int
{
float_from_int()
{
py::converter::registry::push_back(
&convertible,
&construct,
py::type_id<float>());
}
static void* convertible( PyObject* obj)
{
PyObject* newobj = PyNumber_Float(obj);
if (!PyString_Check(obj) && newobj) {
Py_DECREF(newobj);
return obj;
} else {
if (newobj) {
Py_DECREF(newobj);
}
PyErr_Clear();
return 0;
}
}
static void construct(
PyObject* _obj,
py::converter::rvalue_from_python_stage1_data* data)
{
PyObject* newobj = PyNumber_Float(_obj);
float* storage = (float*)(
(py::converter::rvalue_from_python_storage<float>*)
data)->storage.bytes;
*storage = py::extract<float>(newobj);
Py_DECREF(newobj);
data->convertible = storage;
}
};
BOOST_PYTHON_MODULE( cvisual)
{
VPYTHON_NOTE( "Importing cvisual from vpython-core2.");
using namespace boost::python;
numeric::array::set_module_and_type( "numpy", "ndarray");
#if __GNUG__
#if __GNUC__ == 3
#if __GNUCMINOR__ >= 1 && __GNUCMINOR__ < 4
std::set_terminate( __gnu_cxx::__verbose_terminate_handler);
#endif
#endif
#endif
// Initialize the Python thread system.
PyEval_InitThreads();
// A subset of the python standard exceptions may be thrown from visual
register_exception_translator<std::out_of_range>(
&translate_std_out_of_range);
register_exception_translator<std::invalid_argument>(
&translate_std_invalid_argument);
register_exception_translator<std::runtime_error>(
&translate_std_runtime_error);
def( "rate", py_rate, "rate(arg) -> Limits the execution rate of a loop to arg"
" iterations per second.");
double_from_int();
float_from_int();
wrap_vector();
wrap_rgba();
wrap_display_kernel();
wrap_primitive();
wrap_arrayobjects();
python::init_numpy(); // initialize numpy
}
} // !namespace cvisual