Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
matplotlib/src/py_adaptors.h at main · fedora-python/matplotlib · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
fedora-python
/
matplotlib
Public
forked from
matplotlib/matplotlib
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
6
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
matplotlib
/
src
/
py_adaptors.h
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
229 lines (184 loc) · 5.6 KB
main
Breadcrumbs
matplotlib
/
src
/
py_adaptors.h
Copy path
Top
File metadata and controls
Code
Blame
229 lines (184 loc) · 5.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
-*- 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
<
pybind11/pybind11.h
>
#
include
<
pybind11/numpy.h
>
#
include
"
agg_basics.h
"
namespace
py
=
pybind11;
namespace
mpl
{
/*
***********************************************************
* mpl::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.
*/
py::
array_t
<
double
> m_vertices;
py::
array_t
<
uint8_t
> 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_iterator(
0
),
m_total_vertices(
0
),
m_should_simplify(
false
),
m_simplify_threshold(
1.0
/
9.0
)
{
}
inline
PathIterator
(py::object vertices, py::object codes,
bool
should_simplify,
double
simplify_threshold)
: m_iterator(
0
)
{
set
(vertices, codes, should_simplify, simplify_threshold);
}
inline
PathIterator
(py::object vertices, py::object codes)
: m_iterator(
0
)
{
set
(vertices, codes);
}
inline
PathIterator
(
const
PathIterator &other)
{
m_vertices = other.
m_vertices
;
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
;
}
inline
void
set
(py::object vertices, py::object codes,
bool
should_simplify,
double
simplify_threshold)
{
m_should_simplify = should_simplify;
m_simplify_threshold = simplify_threshold;
m_vertices = vertices.
cast
<py::
array_t
<
double
>>();
if
(m_vertices.
ndim
() !=
2
|| m_vertices.
shape
(
1
) !=
2
) {
throw
py::value_error
(
"
Invalid vertices array
"
);
}
m_total_vertices = m_vertices.
shape
(
0
);
m_codes.
release
().
dec_ref
();
if
(!codes.
is_none
()) {
m_codes = codes.
cast
<py::
array_t
<
uint8_t
>>();
if
(m_codes.
ndim
() !=
1
|| m_codes.
shape
(
0
) != m_total_vertices) {
throw
py::value_error
(
"
Invalid codes array
"
);
}
}
m_iterator =
0
;
}
inline
void
set
(py::object vertices, py::object codes)
{
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++;
*x = *m_vertices.
data
(idx,
0
);
*y = *m_vertices.
data
(idx,
1
);
if
(m_codes) {
return
*m_codes.
data
(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
bool
(m_codes);
}
inline
void
*
get_id
()
{
return
(
void
*)m_vertices.
ptr
();
}
};
class
PathGenerator
{
py::sequence m_paths;
Py_ssize_t m_npaths;
public:
typedef
PathIterator path_iterator;
PathGenerator
() : m_npaths(
0
) {}
void
set
(py::object obj)
{
m_paths = obj.
cast
<py::sequence>();
m_npaths = m_paths.
size
();
}
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;
auto
item = m_paths[i % m_npaths];
path = item.
cast
<path_iterator>();
return
path;
}
};
}
namespace
PYBIND11_NAMESPACE
{
namespace
detail
{
template
<>
struct
type_caster
<mpl::PathIterator> {
public:
PYBIND11_TYPE_CASTER
(mpl::PathIterator, const_name(
"
PathIterator
"
));
bool
load
(handle src,
bool
) {
if
(src.
is_none
()) {
return
true
;
}
py::object vertices = src.
attr
(
"
vertices
"
);
py::object codes = src.
attr
(
"
codes
"
);
auto
should_simplify = src.
attr
(
"
should_simplify
"
).
cast
<
bool
>();
auto
simplify_threshold = src.
attr
(
"
simplify_threshold
"
).
cast
<
double
>();
value.
set
(vertices, codes, should_simplify, simplify_threshold);
return
true
;
}
};
template
<>
struct
type_caster
<mpl::PathGenerator> {
public:
PYBIND11_TYPE_CASTER
(mpl::PathGenerator, const_name(
"
PathGenerator
"
));
bool
load
(handle src,
bool
) {
value.
set
(py::reinterpret_borrow<py::object>(src));
return
true
;
}
};
}}
//
namespace PYBIND11_NAMESPACE::detail
#
endif
You can’t perform that action at this time.