Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
path.cpp
Go to the documentation of this file.
1/*
2 * Python bindings for lib2geom
3 *
4 * Copyright 2007 Aaron Spike <aaron@ekips.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
13 *
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
19 *
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
24 *
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
28 *
29 */
30
31#include <boost/python.hpp>
32#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
33
34#include "2geom/curve.h"
35#include "2geom/bezier-curve.h"
36#include "2geom/path.h"
37#include "2geom/pathvector.h"
39#include "helpers.h"
40
41#include "2geom/point.h"
42#include "2geom/rect.h"
43#include "2geom/d2.h"
44
45using namespace boost::python;
46
48{
49 unsigned size = p.size_default();
50 unsigned i = index;
51 if (index < 0)
52 {
53 i = index = size + index;
54 }
55 if ((index < 0) || (i > (size - 1))) {
56 PyErr_SetString(PyExc_IndexError, "index out of range");
57 boost::python::throw_error_already_set();
58 }
59 return p[i];
60}
61
62struct CurveWrap : Geom::Curve, wrapper<Geom::Curve>
63{
64 Geom::Point initialPoint() const {return this->get_override("initialPoint")();}
65 Geom::Point finalPoint() const {return this->get_override("finalPoint")();}
66 bool isDegenerate() const {return this->get_override("isDegenerate")();}
67 CurveWrap *duplicate() const {return this->get_override("duplicate")();}
68 Geom::Rect boundsFast() const {return this->get_override("boundsFast")();}
69 Geom::Rect boundsExact() const {return this->get_override("boundsExact")();}
70 virtual Geom::OptRect boundsLocal(Geom::OptInterval const &i, unsigned deg) const {return this->get_override("boundsLocal")(i,deg);}
71 std::vector<double> roots(double v, Geom::Dim2 d) const {return this->get_override("roots")(v,d);}
72
73 int winding(Geom::Point const &p) const {
74 if (override f = this->get_override("winding")) {
75 return f(p);
76 }
77 return Geom::Curve::winding(p);
78 }
79 int default_winding(Geom::Point p) const { return this->Geom::Curve::winding(p); }
80
81 Geom::Curve *portion(double f, double t) const { return this->get_override("portion")(f,t); }
82 Geom::Curve *reverse() const {
83 if (override f = this->get_override("reverse")) {
84 return f();
85 }
86 return Geom::Curve::reverse();
87 }
88 Geom::Curve *default_reverse() const { return this->Geom::Curve::reverse(); }
89
90 Geom::Curve *derivative() const { return this->get_override("derivative")(); }
91
92
93 Geom::Curve *transformed(Geom::Affine const &m) const { return this->get_override("transformed")(m); }
94
96 if (override f = this->get_override("pointAt")) {
97 return f(t);
98 }
99 return Geom::Curve::pointAt(t);
100 }
101 Geom::Point default_pointAt(Geom::Coord t) { return this->Geom::Curve::pointAt(t); }
102 std::vector<Geom::Point> pointAndDerivatives(Geom::Coord t, unsigned n) const {
103 return this->get_override("pointAndDerivatives")(t, n);
104 }
105
106 Geom::D2<Geom::SBasis> toSBasis() const {return this->get_override("sbasis")();}
107};
108
109
110/* pycairo stuff: */
111#ifdef HAVE_PYCAIRO
112
113#include "cairo-helpers.h"
114
115void py_cairo_curve(object cr, Geom::Curve const &c) {
117}
118void py_cairo_rectangle(object cr, Geom::Rect const &r) {
120}
121
125/*void py_cairo_path(object cr, Geom::Path const &p) {
126 cairo_path(cairo_t_from_object(cr), p);
127 }*/
128
129void py_cairo_path(object cr, Geom::Path const &p) {
131}
132
133void py_cairo_path(object cr, Geom::PathVector const &p) {
135}
136void py_cairo_path_stitches(object cr, Geom::Path const &p) {
138}
142void (*cp_1)(object, Geom::Path const &) = &py_cairo_path;
143void (*cp_2)(object, Geom::PathVector const &) = &py_cairo_path;
144
145void (*cps_1)(object, Geom::Path const &) = &py_cairo_path_stitches;
147
148
149void py_cairo_d2_sb(object cr, Geom::D2<Geom::SBasis> const &p) {
151}
152
156
160
161#endif // HAVE_PYCAIRO
162
163Geom::Point (Geom::Path::*path_pointAt_time)(Geom::Coord) const = &Geom::Path::pointAt;
164Geom::Coord (Geom::Path::*path_valueAt_time)(Geom::Coord, Geom::Dim2) const = &Geom::Path::valueAt;
165void (Geom::Path::*appendPortionTo_time)(Geom::Path &, Geom::Coord, Geom::Coord) const = &Geom::Path::appendPortionTo;
166//void (Geom::Path::*appendPortionTo_pos)(Geom::Path &, Geom::PathPosition const &, Geom::PathPosition const &, bool) const = &Geom::Path::appendPortionTo;
167
169{
170/* class_<CurveWrap, boost::noncopyable>("Curve")
171 .def("initalPoint", pure_virtual(&Geom::Curve::initialPoint))
172 .def("finalPoint", pure_virtual(&Geom::Curve::finalPoint))
173 .def("duplicate", pure_virtual(&Geom::Curve::duplicate), return_value_policy<manage_new_object>())
174 .def("boundsFast", pure_virtual(&Geom::Curve::boundsFast))
175 .def("boundsExact", pure_virtual(&Geom::Curve::boundsExact))
176 //.def("pointAt", &Geom::Curve::pointAt, &CurveWrap::default_pointAt)
177 //.def("winding", &Geom::Curve::winding, &CurveWrap::default_winding)
178 .def("pointAndDerivatives", pure_virtual(&Geom::Curve::pointAndDerivatives))
179 .def("toSBasis", pure_virtual(&Geom::Curve::toSBasis))
180 ;*/
181/* class_<Geom::LineSegment, bases<CurveWrap> >("LineSegment")
182 .def("points", &Geom::LineSegment::points)
183 ;
184 class_<Geom::QuadraticBezier, bases<CurveWrap> >("QuadraticBezier")
185 .def("points", &Geom::QuadraticBezier::points)
186 ;
187 class_<Geom::CubicBezier, bases<CurveWrap> >("CubicBezier")
188 .def("points", &Geom::CubicBezier::points)
189 ;*/
190 class_<Geom::Path>("Path")
191 .def("__getitem__", path_getitem, return_value_policy<copy_const_reference>()) //or return_internal_reference see http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#question1
192 .def("empty", &Geom::Path::empty)
193 .def("closed", &Geom::Path::closed)
194 .def("close", &Geom::Path::close)
195 .def("boundsFast", &Geom::Path::boundsFast)
196 .def("boundsExact", &Geom::Path::boundsExact)
197 .def("toPwSb", &Geom::Path::toPwSb)
198 .def(self * Geom::Affine())
199 .def(self *= Geom::Affine())
200 .def("pointAt", path_pointAt_time)
201 .def("valueAt", path_valueAt_time)
202 .def("__call__", path_pointAt_time)
203 .def("roots", &Geom::Path::roots)
204 //.def("allNearestTimes", &Geom::Path::allNearestTimes)
205 //.def("nearestTime", &Geom::Path::nearestTime)
206 .def("appendPortionTo", appendPortionTo_time)
207 //.def("portion", &Geom::Path::portion)
208 .def("reversed", &Geom::Path::reversed)
209 //.def("insert", &Geom::Path::insert)
210 .def("clear", &Geom::Path::clear)
211 //.def("erase", &Geom::Path::erase)
212 .def("erase_last", &Geom::Path::erase_last)
213 //.def("replace", &Geom::Path::replace)
214 .def("start", &Geom::Path::start)
215 .def("initialPoint", &Geom::Path::initialPoint)
216 .def("finalPoint", &Geom::Path::finalPoint)
217 //.def("append", &Geom::Path::append)
218 //.def("appendNew", &Geom::Path::appendNew)
219 ;
220 def("paths_to_pw",Geom::paths_to_pw);
221 class_<Geom::PathVector >("PathVector")
222 .def(vector_indexing_suite<Geom::PathVector >())
223 .def(self * Geom::Affine())
224 .def(self *= Geom::Affine())
225 .def("reversed", &Geom::PathVector::reversed)
226 .def("reverse", &Geom::PathVector::reverse)
227 .def("boundsFast", &Geom::PathVector::boundsFast)
228 .def("boundsExact", &Geom::PathVector::boundsExact)
229 ;
230 def("path_from_piecewise", Geom::path_from_piecewise);
231 def("path_from_sbasis", Geom::path_from_sbasis);
232 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
233
234#ifdef HAVE_PYCAIRO
235void cairo_move_to(cairo_t *cr, Geom::Point p1);
236 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
237void cairo_line_to(cairo_t *cr, Geom::Point p1);
238 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
240 def("cubicbezierpath_from_sbasis", Geom::cubicbezierpath_from_sbasis);
241
242 //def("cairo_curve", cairo_curve);
243 def("cairo_convex_hull", py_cairo_convex_hull);
244 def("cairo_path", cp_1);
245 def("cairo_path", cp_2);
246 def("cairo_path_stitches", cps_1);
247 def("cairo_path_stitches", cps_2);
248
249 def("cairo_d2_sb", py_cairo_d2_sb);
250 def("cairo_d2_pw_sb", py_cairo_d2_pw_sb);
251 def("cairo_pw_d2_sb", py_cairo_pw_d2_sb);
252#endif // HAVE_PYCAIRO
253}
254
255/*
256 Local Variables:
257 mode:c++
258 c-file-style:"stroustrup"
259 c-file-offsets:((innamespace . 0)(substatement-open . 0))
260 indent-tabs-mode:nil
261 c-brace-offset:0
262 fill-column:99
263 End:
264 vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
265*/
Abstract curve type.
Path - a sequence of contiguous curves.
Cartesian point / 2D vector and related operations.
void py_cairo_curve(object cr, Geom::Curve const &c)
Definition path.cpp:115
void(* cp_2)(object, Geom::PathVector const &)
Definition path.cpp:143
Geom::Point(Geom::Path::* path_pointAt_time)(Geom::Coord) const
Definition path.cpp:163
void py_cairo_path(object cr, Geom::Path const &p)
Definition path.cpp:129
void py_cairo_d2_sb(object cr, Geom::D2< Geom::SBasis > const &p)
Definition path.cpp:149
void(Geom::Path::* appendPortionTo_time)(Geom::Path &, Geom::Coord, Geom::Coord) const
Definition path.cpp:165
void(* cps_2)(object, Geom::PathVector const &)
Definition path.cpp:146
void wrap_path()
Definition path.cpp:168
Geom::Curve const & path_getitem(Geom::Path const &p, int index)
Definition path.cpp:47
void py_cairo_rectangle(object cr, Geom::Rect const &r)
Definition path.cpp:118
void py_cairo_path_stitches(object cr, Geom::Path const &p)
Definition path.cpp:136
void(* cps_1)(object, Geom::Path const &)
Definition path.cpp:145
void(* cp_1)(object, Geom::Path const &)
Definition path.cpp:142
void py_cairo_d2_pw_sb(object cr, Geom::D2< Geom::Piecewise< Geom::SBasis > > const &p)
Definition path.cpp:153
void py_cairo_convex_hull(object cr, Geom::ConvexHull const &r)
Definition path.cpp:122
void py_cairo_pw_d2_sb(object cr, Geom::Piecewise< Geom::D2< Geom::SBasis > > const &p)
Definition path.cpp:157
Geom::Coord(Geom::Path::* path_valueAt_time)(Geom::Coord, Geom::Dim2) const
Definition path.cpp:164
Bezier curve.
cairo_t * cairo_t_from_object(boost::python::object cr)
3x3 matrix representing an affine transformation.
Definition affine.h:70
Convex hull based on the Andrew's monotone chain algorithm.
Abstract continuous curve on a plane defined on [0,1].
Definition curve.h:78
virtual D2< SBasis > toSBasis() const =0
Convert the curve to a symmetric power basis polynomial.
virtual Point initialPoint() const =0
Retrieve the start of the curve.
virtual Point finalPoint() const =0
Retrieve the end of the curve.
virtual Rect boundsFast() const =0
Quickly compute the curve's approximate bounding box.
virtual Curve * reverse() const
Create a reversed version of this curve.
Definition curve.h:234
virtual Curve * transformed(Affine const &m) const
Create a curve transformed by an affine transformation.
Definition curve.h:209
virtual std::vector< Coord > roots(Coord v, Dim2 d) const =0
Computes time values at which the curve intersects an axis-aligned line.
virtual bool isDegenerate() const =0
Check whether the curve has exactly zero length.
virtual int winding(Point const &p) const
Compute the partial winding number of this curve.
Definition curve.cpp:61
virtual Rect boundsExact() const =0
Compute the curve's exact bounding box.
virtual Curve * derivative() const =0
Create a derivative of this curve.
virtual Curve * duplicate() const =0
Create an exact copy of this curve.
virtual Curve * portion(Coord a, Coord b) const =0
Create a curve that corresponds to a part of this curve.
virtual std::vector< Point > pointAndDerivatives(Coord t, unsigned n) const =0
Evaluate the curve and its derivatives.
virtual OptRect boundsLocal(OptInterval const &i, unsigned deg) const =0
virtual Point pointAt(Coord t) const
Evaluate the curve at a specified time value.
Definition curve.h:110
Adaptor that creates 2D functions from 1D ones.
Definition d2.h:55
Range of real numbers that can be empty.
Definition interval.h:199
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Sequence of subpaths.
Definition pathvector.h:122
OptRect boundsExact() const
OptRect boundsFast() const
PathVector reversed(bool reverse_paths=true) const
Get a new vector with reversed direction of paths.
void reverse(bool reverse_paths=true)
Reverse the direction of paths in the vector.
Sequence of contiguous curves, aka spline.
Definition path.h:353
bool closed() const
Check whether the path is closed.
Definition path.h:503
Point finalPoint() const
Get the last point in the path.
Definition path.h:709
void close(bool closed=true)
Set whether the path is closed.
Definition path.cpp:322
bool empty() const
Check whether path is empty.
Definition path.h:500
OptRect boundsExact() const
Get a tight-fitting bounding box.
Definition path.cpp:372
Piecewise< D2< SBasis > > toPwSb() const
Definition path.cpp:388
std::vector< PathTime > roots(Coord v, Dim2 d) const
Compute intersections with axis-aligned line.
Definition path.cpp:472
OptRect boundsFast() const
Get the approximate bounding box.
Definition path.cpp:348
void clear()
Remove all curves from the path.
Definition path.cpp:337
size_type size_default() const
Natural size of the path.
Definition path.h:486
Point initialPoint() const
Get the first point in the path.
Definition path.h:705
Path reversed() const
Obtain a reversed version of the current path.
Definition path.cpp:866
void erase_last()
Definition path.h:700
void start(Point const &p)
Definition path.cpp:426
Function defined as discrete pieces.
Definition piecewise.h:71
Two-dimensional point that doubles as a vector.
Definition point.h:66
Axis aligned, non-empty rectangle.
Definition rect.h:92
Path and its polyline approximation.
Definition Path.h:93
Geom::IntPoint size
double c[8][4]
Lifts one dimensional objects into 2D.
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
std::pair< size_t, size_t > deg(poly_vector_type const &p)
Definition implicit.h:80
size_t v
Various utility functions.
Definition affine.h:22
Path path_from_sbasis(D2< SBasis > const &B, double tol, bool only_cubicbeziers=false)
Make a path from a d2 sbasis.
Piecewise< D2< SBasis > > paths_to_pw(PathVector const &paths)
Definition path.cpp:1123
PathVector path_from_piecewise(Piecewise< D2< SBasis > > const &B, double tol, bool only_cubicbeziers=false)
Make a path from a d2 sbasis.
Path cubicbezierpath_from_sbasis(D2< SBasis > const &B, double tol)
int n
Definition spiro.cpp:57
void cairo_rectangle(cairo_t *cr, Geom::Rect const &r)
void cairo_line_to(cairo_t *cr, Geom::Point p1)
struct _cairo cairo_t
Definition path-cairo.h:16
void cairo_path(cairo_t *cr, Geom::Path const &p)
void cairo_curve(cairo_t *cr, Geom::Curve const &c)
void cairo_d2_pw_sb(cairo_t *cr, Geom::D2< Geom::Piecewise< Geom::SBasis > > const &p)
void cairo_convex_hull(cairo_t *cr, Geom::ConvexHull const &r)
void cairo_move_to(cairo_t *cr, Geom::Point p1)
void cairo_d2_sb(cairo_t *cr, Geom::D2< Geom::SBasis > const &p)
void cairo_curve_to(cairo_t *cr, Geom::Point p1, Geom::Point p2, Geom::Point p3)
void cairo_pw_d2_sb(cairo_t *cr, Geom::Piecewise< Geom::D2< Geom::SBasis > > const &p)
void cairo_path_stitches(cairo_t *cr, Geom::Path const &p)
PathVector - a sequence of subpaths.
Axis-aligned rectangle.
Conversion between SBasis and Bezier basis polynomials.
int index