File size: 5,893 Bytes
714e7c4 | 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | /* -*- mode: c++; c-basic-offset: 4 -*- */
#ifndef MPL_PY_ADAPTORS_H
#define MPL_PY_ADAPTORS_H
#define PY_SSIZE_T_CLEAN
/***************************************************************************
* This module contains a number of C++ classes that adapt Python data
* structures to C++ and Agg-friendly interfaces.
*/
#include <Python.h>
#include "numpy/arrayobject.h"
#include "py_exceptions.h"
extern "C" {
int convert_path(PyObject *obj, void *pathp);
}
namespace py
{
/************************************************************
* py::PathIterator acts as a bridge between Numpy and Agg. Given a
* pair of Numpy arrays, vertices and codes, it iterates over
* those vertices and codes, using the standard Agg vertex source
* interface:
*
* unsigned vertex(double* x, double* y)
*/
class PathIterator
{
/* We hold references to the Python objects, not just the
underlying data arrays, so that Python reference counting
can work.
*/
PyArrayObject *m_vertices;
PyArrayObject *m_codes;
unsigned m_iterator;
unsigned m_total_vertices;
/* This class doesn't actually do any simplification, but we
store the value here, since it is obtained from the Python
object.
*/
bool m_should_simplify;
double m_simplify_threshold;
public:
inline PathIterator()
: m_vertices(NULL),
m_codes(NULL),
m_iterator(0),
m_total_vertices(0),
m_should_simplify(false),
m_simplify_threshold(1.0 / 9.0)
{
}
inline PathIterator(PyObject *vertices,
PyObject *codes,
bool should_simplify,
double simplify_threshold)
: m_vertices(NULL), m_codes(NULL), m_iterator(0)
{
if (!set(vertices, codes, should_simplify, simplify_threshold))
throw py::exception();
}
inline PathIterator(PyObject *vertices, PyObject *codes)
: m_vertices(NULL), m_codes(NULL), m_iterator(0)
{
if (!set(vertices, codes))
throw py::exception();
}
inline PathIterator(const PathIterator &other)
{
Py_XINCREF(other.m_vertices);
m_vertices = other.m_vertices;
Py_XINCREF(other.m_codes);
m_codes = other.m_codes;
m_iterator = 0;
m_total_vertices = other.m_total_vertices;
m_should_simplify = other.m_should_simplify;
m_simplify_threshold = other.m_simplify_threshold;
}
~PathIterator()
{
Py_XDECREF(m_vertices);
Py_XDECREF(m_codes);
}
inline int
set(PyObject *vertices, PyObject *codes, bool should_simplify, double simplify_threshold)
{
m_should_simplify = should_simplify;
m_simplify_threshold = simplify_threshold;
Py_XDECREF(m_vertices);
m_vertices = (PyArrayObject *)PyArray_FromObject(vertices, NPY_DOUBLE, 2, 2);
if (!m_vertices || PyArray_DIM(m_vertices, 1) != 2) {
PyErr_SetString(PyExc_ValueError, "Invalid vertices array");
return 0;
}
Py_XDECREF(m_codes);
m_codes = NULL;
if (codes != NULL && codes != Py_None) {
m_codes = (PyArrayObject *)PyArray_FromObject(codes, NPY_UINT8, 1, 1);
if (!m_codes || PyArray_DIM(m_codes, 0) != PyArray_DIM(m_vertices, 0)) {
PyErr_SetString(PyExc_ValueError, "Invalid codes array");
return 0;
}
}
m_total_vertices = (unsigned)PyArray_DIM(m_vertices, 0);
m_iterator = 0;
return 1;
}
inline int set(PyObject *vertices, PyObject *codes)
{
return set(vertices, codes, false, 0.0);
}
inline unsigned vertex(double *x, double *y)
{
if (m_iterator >= m_total_vertices) {
*x = 0.0;
*y = 0.0;
return agg::path_cmd_stop;
}
const size_t idx = m_iterator++;
char *pair = (char *)PyArray_GETPTR2(m_vertices, idx, 0);
*x = *(double *)pair;
*y = *(double *)(pair + PyArray_STRIDE(m_vertices, 1));
if (m_codes != NULL) {
return (unsigned)(*(char *)PyArray_GETPTR1(m_codes, idx));
} else {
return idx == 0 ? agg::path_cmd_move_to : agg::path_cmd_line_to;
}
}
inline void rewind(unsigned path_id)
{
m_iterator = path_id;
}
inline unsigned total_vertices() const
{
return m_total_vertices;
}
inline bool should_simplify() const
{
return m_should_simplify;
}
inline double simplify_threshold() const
{
return m_simplify_threshold;
}
inline bool has_codes() const
{
return m_codes != NULL;
}
inline void *get_id()
{
return (void *)m_vertices;
}
};
class PathGenerator
{
PyObject *m_paths;
Py_ssize_t m_npaths;
public:
typedef PathIterator path_iterator;
PathGenerator() : m_paths(NULL), m_npaths(0) {}
~PathGenerator()
{
Py_XDECREF(m_paths);
}
int set(PyObject *obj)
{
if (!PySequence_Check(obj)) {
return 0;
}
Py_XDECREF(m_paths);
m_paths = obj;
Py_INCREF(m_paths);
m_npaths = PySequence_Size(m_paths);
return 1;
}
Py_ssize_t num_paths() const
{
return m_npaths;
}
Py_ssize_t size() const
{
return m_npaths;
}
path_iterator operator()(size_t i)
{
path_iterator path;
PyObject *item;
item = PySequence_GetItem(m_paths, i % m_npaths);
if (item == NULL) {
throw py::exception();
}
if (!convert_path(item, &path)) {
Py_DECREF(item);
throw py::exception();
}
Py_DECREF(item);
return path;
}
};
}
#endif
|