Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
conic.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2009 Nathan Hurst <njh@njhurst.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it either under the terms of the GNU Lesser General Public
6 * License version 2.1 as published by the Free Software Foundation
7 * (the "LGPL") or, at your option, under the terms of the Mozilla
8 * Public License Version 1.1 (the "MPL"). If you do not alter this
9 * notice, a recipient may use your version of this file under either
10 * the MPL or the LGPL.
11 *
12 * You should have received a copy of the LGPL along with this library
13 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 * You should have received a copy of the MPL along with this library
16 * in the file COPYING-MPL-1.1
17 *
18 * The contents of this file are subject to the Mozilla Public License
19 * Version 1.1 (the "License"); you may not use this file except in
20 * compliance with the License. You may obtain a copy of the License at
21 * http://www.mozilla.org/MPL/
22 *
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
24 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
25 * the specific language governing rights and limitations.
26 *
27 */
28
29#include <boost/python.hpp>
30#include <boost/python/implicit.hpp>
31#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
32#include <optional>
33
34#include "py2geom.h"
35#include "helpers.h"
36
37#include "2geom/point.h"
38#include "2geom/line.h"
39#include "2geom/conicsec.h"
40
41using namespace boost::python;
42
43// helpers for point
44static tuple xAx_to_tuple(Geom::xAx const& a)
45{
46 return make_tuple(a.c[0], a.c[1], a.c[2], a.c[3], a.c[4], a.c[5]);
47}
48
49static Geom::xAx tuple_to_xAx(boost::python::tuple const& t)
50{
51 return Geom::xAx(extract<double>(t[0]),
52 extract<double>(t[1]),
53 extract<double>(t[2]),
54 extract<double>(t[3]),
55 extract<double>(t[4]),
56 extract<double>(t[5])
57 );
58}
59
60static std::vector<double> xax_roots1(Geom::xAx const & xax, Geom::Point const &a, Geom::Point const &b) { return xax.roots(a,b); }
61static std::vector<double> xax_roots2(Geom::xAx const & xax, Geom::Line const &l) { return xax.roots(l); }
63 Geom::SBasis const & x,
64 Geom::SBasis const & y,
65 Geom::SBasis const & w
66 ) {
67 return xax.evaluate_at(x, y, w);
68}
69
70static Geom::SBasis xy_eval_at(Geom::xAx const & xax,
71 Geom::SBasis const & x,
72 Geom::SBasis const & y
73 ) {
74 return xax.evaluate_at(x, y);
75}
76
78 return rq.toCubic().toSBasis();
79}
80
82 return rq.toCubic(l).toSBasis();
83}
84
85static std::vector<Geom::Point> wrap_rq_to_cubic_l(Geom::RatQuad const & rq, double l) {
86 return rq.toCubic(l).controlPoints();
87}
88
89static std::vector<Geom::Point> wrap_rq_to_cubic(Geom::RatQuad const & rq) {
90 return wrap_rq_to_cubic_l(rq, rq.lambda());
91}
92
93static tuple wrap_rq_split(Geom::RatQuad const & rq) {
94 Geom::RatQuad a, b;
95 rq.split(a, b);
96 return make_tuple(a, b);
97}
98
99static object wrap_xax_to_curve(Geom::xAx const & xax, Geom::Rect const & r) {
100 std::optional<Geom::RatQuad> oc = xax.toCurve(r);
101 return oc?object(*oc):object();
102}
103
104
105
107 //conicsec.h
108 def("intersect", (std::vector<Geom::Point> (*)(Geom::xAx const &, Geom::xAx const &))Geom::intersect);
109
110 class_<Geom::xAx>("xAx", init<>())
111 .def(init<double, double, double, double, double, double>())
112 .def(init<Geom::xAx const&>())
113 .def_readonly("c", &Geom::xAx::c)
114 .def("tuple", xAx_to_tuple)
115
116 .def("from_tuple", tuple_to_xAx)
117 .staticmethod("from_tuple")
118 .def("fromPoint", Geom::xAx::fromPoint)
119 .staticmethod("fromPoint")
120 .def("fromPoints", Geom::xAx::fromPoints)
121 .staticmethod("fromPoints")
122 .def("fromLine", (Geom::xAx (*)(Geom::Line l))Geom::xAx::fromLine)
123 .staticmethod("fromLine")
124 .def(self_ns::str(self))
125 .def("valueAt", &Geom::xAx::valueAt)
126
127 .def("implicit_form_coefficients", &Geom::xAx::implicit_form_coefficients)
128
129 .def("isDegenerate", &Geom::xAx::isDegenerate)
130 .def("roots", &xax_roots1)
131 .def("roots", &xax_roots2)
132 .def("extrema", &Geom::xAx::extrema)
133 .def("gradient", &Geom::xAx::gradient)
134 .def("crossings", &Geom::xAx::crossings)
135 .def("evaluate_at", &xy_eval_at)
136 .def("evaluate_at", &homo_eval_at)
137 .def("toCurve", &wrap_xax_to_curve)
138 .def(self - self)
139 .def(self + float())
140 .def(self * float())
141 ;
142
143 class_<Geom::RatQuad>("RatQuad", init<>())
144 .def(init<Geom::Point, Geom::Point, Geom::Point, double>())
145 .def_readonly("P", &Geom::RatQuad::P)
146 .def_readonly("w", &Geom::RatQuad::w)
147 .def_readonly("lam", &Geom::RatQuad::lambda)
148 //.def(self_ns::str(self))
149 .def("at0", &Geom::RatQuad::at0)
150 .def("at1", &Geom::RatQuad::at1)
151 .def("pointAt", &Geom::RatQuad::pointAt)
152
153 .def("toCubic", &wrap_rq_to_cubic)
154 .def("toCubic", &wrap_rq_to_cubic_l)
155 .def("toCubicSBasis", &wrap_rq_to_cubic_sb)
156 .def("toCubicSBasis", &wrap_rq_to_cubic_sb_l)
157
158 .def("split", &wrap_rq_split)
159 .def("hermite", &Geom::RatQuad::hermite)
160 .def("homogeneous", &Geom::RatQuad::homogeneous)
161 .def("fromPointsTangents", &Geom::RatQuad::fromPointsTangents)
162 .staticmethod("fromPointsTangents")
163 ;
164 implicitly_convertible<Geom::Point,tuple>();
165};
166
167/*
168 Local Variables:
169 mode:c++
170 c-file-style:"stroustrup"
171 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
172 indent-tabs-mode:nil
173 fill-column:99
174 End:
175*/
176// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
D2< SBasis > toSBasis() const override
Convert the curve to a symmetric power basis polynomial.
std::vector< Point > controlPoints() const
Get the control points.
Adaptor that creates 2D functions from 1D ones.
Definition d2.h:55
Infinite line on a plane.
Definition line.h:53
Two-dimensional point that doubles as a vector.
Definition point.h:66
void split(RatQuad &a, RatQuad &b) const
Definition conicsec.cpp:175
D2< SBasis > hermite() const
Definition conicsec.cpp:185
Point at0() const
Definition conicsec.h:87
static RatQuad fromPointsTangents(Point P0, Point dP0, Point P, Point P2, Point dP2)
Definition conicsec.cpp:115
double lambda() const
Definition conicsec.cpp:111
CubicBezier toCubic() const
Definition conicsec.cpp:156
Point pointAt(double t) const
Definition conicsec.cpp:167
Point at1() const
Definition conicsec.h:88
Point P[3]
A curve of the form B02*A + B12*B*w + B22*C/(B02 + B12*w + B22) These curves can exactly represent a ...
Definition conicsec.h:68
std::vector< SBasis > homogeneous() const
Definition conicsec.cpp:197
Axis aligned, non-empty rectangle.
Definition rect.h:92
Polynomial in symmetric power basis.
Definition sbasis.h:70
std::vector< double > implicit_form_coefficients() const
Definition conicsec.h:252
static xAx fromPoints(std::vector< Point > const &pts)
Definition conicsec.cpp:428
static xAx fromPoint(Point p)
Definition conicsec.cpp:409
Point gradient(Point p) const
Definition conicsec.cpp:459
double c[6]
Definition conicsec.h:101
std::vector< double > roots(Point d, Point o) const
Definition conicsec.cpp:565
static xAx fromLine(Point n, double d)
Definition conicsec.cpp:417
double valueAt(Point P) const
Definition conicsec.cpp:450
bool isDegenerate() const
Definition conicsec.h:332
Interval extrema(Rect r) const
Definition conicsec.cpp:648
T evaluate_at(T x, T y) const
Definition conicsec.h:246
std::optional< RatQuad > toCurve(Rect const &bnd) const
Definition conicsec.cpp:511
std::vector< Point > crossings(Rect r) const
Definition conicsec.cpp:497
const double w
Definition conic-4.cpp:19
static tuple xAx_to_tuple(Geom::xAx const &a)
Definition conic.cpp:44
static object wrap_xax_to_curve(Geom::xAx const &xax, Geom::Rect const &r)
Definition conic.cpp:99
static std::vector< double > xax_roots1(Geom::xAx const &xax, Geom::Point const &a, Geom::Point const &b)
Definition conic.cpp:60
static std::vector< Geom::Point > wrap_rq_to_cubic(Geom::RatQuad const &rq)
Definition conic.cpp:89
static Geom::SBasis xy_eval_at(Geom::xAx const &xax, Geom::SBasis const &x, Geom::SBasis const &y)
Definition conic.cpp:70
static Geom::xAx tuple_to_xAx(boost::python::tuple const &t)
Definition conic.cpp:49
static Geom::D2< Geom::SBasis > wrap_rq_to_cubic_sb_l(Geom::RatQuad const &rq, double l)
Definition conic.cpp:81
static std::vector< double > xax_roots2(Geom::xAx const &xax, Geom::Line const &l)
Definition conic.cpp:61
static std::vector< Geom::Point > wrap_rq_to_cubic_l(Geom::RatQuad const &rq, double l)
Definition conic.cpp:85
static Geom::SBasis homo_eval_at(Geom::xAx const &xax, Geom::SBasis const &x, Geom::SBasis const &y, Geom::SBasis const &w)
Definition conic.cpp:62
static Geom::D2< Geom::SBasis > wrap_rq_to_cubic_sb(Geom::RatQuad const &rq)
Definition conic.cpp:77
void wrap_conic()
Definition conic.cpp:106
static tuple wrap_rq_split(Geom::RatQuad const &rq)
Definition conic.cpp:93
Conic Section.
Infinite straight line.
std::vector< Point > intersect(const xAx &C1, const xAx &C2)
Definition conicsec.cpp:361