| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** @file | ||
| 2 | * @brief Fitting elliptical arc to SBasis | ||
| 3 | * | ||
| 4 | * This file contains the implementation of the function arc_from_sbasis. | ||
| 5 | *//* | ||
| 6 | * Copyright 2008 Marco Cecchetti <mrcekets at gmail.com> | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it either under the terms of the GNU Lesser General Public | ||
| 10 | * License version 2.1 as published by the Free Software Foundation | ||
| 11 | * (the "LGPL") or, at your option, under the terms of the Mozilla | ||
| 12 | * Public License Version 1.1 (the "MPL"). If you do not alter this | ||
| 13 | * notice, a recipient may use your version of this file under either | ||
| 14 | * the MPL or the LGPL. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the LGPL along with this library | ||
| 17 | * in the file COPYING-LGPL-2.1; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 19 | * You should have received a copy of the MPL along with this library | ||
| 20 | * in the file COPYING-MPL-1.1 | ||
| 21 | * | ||
| 22 | * The contents of this file are subject to the Mozilla Public License | ||
| 23 | * Version 1.1 (the "License"); you may not use this file except in | ||
| 24 | * compliance with the License. You may obtain a copy of the License at | ||
| 25 | * http://www.mozilla.org/MPL/ | ||
| 26 | * | ||
| 27 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY | ||
| 28 | * OF ANY KIND, either express or implied. See the LGPL or the MPL for | ||
| 29 | * the specific language governing rights and limitations. | ||
| 30 | */ | ||
| 31 | |||
| 32 | #include <2geom/curve.h> | ||
| 33 | #include <2geom/angle.h> | ||
| 34 | #include <2geom/utils.h> | ||
| 35 | #include <2geom/bezier-curve.h> | ||
| 36 | #include <2geom/elliptical-arc.h> | ||
| 37 | #include <2geom/sbasis-curve.h> // for non-native methods | ||
| 38 | #include <2geom/numeric/vector.h> | ||
| 39 | #include <2geom/numeric/fitting-tool.h> | ||
| 40 | #include <2geom/numeric/fitting-model.h> | ||
| 41 | #include <algorithm> | ||
| 42 | |||
| 43 | namespace Geom { | ||
| 44 | |||
| 45 | // forward declaration | ||
| 46 | namespace detail | ||
| 47 | { | ||
| 48 | struct ellipse_equation; | ||
| 49 | } | ||
| 50 | |||
| 51 | /* | ||
| 52 | * make_elliptical_arc | ||
| 53 | * | ||
| 54 | * convert a parametric polynomial curve given in symmetric power basis form | ||
| 55 | * into an EllipticalArc type; in order to be successful the input curve | ||
| 56 | * has to look like an actual elliptical arc even if a certain tolerance | ||
| 57 | * is allowed through an ad-hoc parameter. | ||
| 58 | * The conversion is performed through an interpolation on a certain amount of | ||
| 59 | * sample points computed on the input curve; | ||
| 60 | * the interpolation computes the coefficients of the general implicit equation | ||
| 61 | * of an ellipse (A*X^2 + B*XY + C*Y^2 + D*X + E*Y + F = 0), then from the | ||
| 62 | * implicit equation we compute the parametric form. | ||
| 63 | * | ||
| 64 | */ | ||
| 65 | class make_elliptical_arc | ||
| 66 | { | ||
| 67 | public: | ||
| 68 | typedef D2<SBasis> curve_type; | ||
| 69 | |||
| 70 | /* | ||
| 71 | * constructor | ||
| 72 | * | ||
| 73 | * it doesn't execute the conversion but set the input and output parameters | ||
| 74 | * | ||
| 75 | * _ea: the output EllipticalArc that will be generated; | ||
| 76 | * _curve: the input curve to be converted; | ||
| 77 | * _total_samples: the amount of sample points to be taken | ||
| 78 | * on the input curve for performing the conversion | ||
| 79 | * _tolerance: how much likelihood is required between the input curve | ||
| 80 | * and the generated elliptical arc; the smaller it is the | ||
| 81 | * the tolerance the higher it is the likelihood. | ||
| 82 | */ | ||
| 83 | make_elliptical_arc( EllipticalArc& _ea, | ||
| 84 | curve_type const& _curve, | ||
| 85 | unsigned int _total_samples, | ||
| 86 | double _tolerance ); | ||
| 87 | |||
| 88 | private: | ||
| 89 | bool bound_exceeded( unsigned int k, detail::ellipse_equation const & ee, | ||
| 90 | double e1x, double e1y, double e2 ); | ||
| 91 | |||
| 92 | bool check_bound(double A, double B, double C, double D, double E, double F); | ||
| 93 | |||
| 94 | void fit(); | ||
| 95 | |||
| 96 | bool make_elliptiarc(); | ||
| 97 | |||
| 98 | ✗ | void print_bound_error(unsigned int k) | |
| 99 | { | ||
| 100 | std::cerr | ||
| 101 | ✗ | << "tolerance error" << std::endl | |
| 102 | ✗ | << "at point: " << k << std::endl | |
| 103 | ✗ | << "error value: "<< dist_err << std::endl | |
| 104 | ✗ | << "bound: " << dist_bound << std::endl | |
| 105 | ✗ | << "angle error: " << angle_err | |
| 106 | ✗ | << " (" << angle_tol << ")" << std::endl; | |
| 107 | ✗ | } | |
| 108 | |||
| 109 | public: | ||
| 110 | /* | ||
| 111 | * perform the actual conversion | ||
| 112 | * return true if the conversion is successful, false on the contrary | ||
| 113 | */ | ||
| 114 | ✗ | bool operator()() | |
| 115 | { | ||
| 116 | // initialize the reference | ||
| 117 | ✗ | const NL::Vector & coeff = fitter.result(); | |
| 118 | ✗ | fit(); | |
| 119 | ✗ | if ( !check_bound(1, coeff[0], coeff[1], coeff[2], coeff[3], coeff[4]) ) | |
| 120 | ✗ | return false; | |
| 121 | ✗ | if ( !(make_elliptiarc()) ) return false; | |
| 122 | ✗ | return true; | |
| 123 | } | ||
| 124 | |||
| 125 | private: | ||
| 126 | EllipticalArc& ea; // output elliptical arc | ||
| 127 | const curve_type & curve; // input curve | ||
| 128 | Piecewise<D2<SBasis> > dcurve; // derivative of the input curve | ||
| 129 | NL::LFMEllipse model; // model used for fitting | ||
| 130 | // perform the actual fitting task | ||
| 131 | NL::least_squeares_fitter<NL::LFMEllipse> fitter; | ||
| 132 | // tolerance: the user-defined tolerance parameter; | ||
| 133 | // tol_at_extr: the tolerance at end-points automatically computed | ||
| 134 | // on the value of "tolerance", and usually more strict; | ||
| 135 | // tol_at_center: tolerance at the center of the ellipse | ||
| 136 | // angle_tol: tolerance for the angle btw the input curve tangent | ||
| 137 | // versor and the ellipse normal versor at the sample points | ||
| 138 | double tolerance, tol_at_extr, tol_at_center, angle_tol; | ||
| 139 | Point initial_point, final_point; // initial and final end-points | ||
| 140 | unsigned int N; // total samples | ||
| 141 | unsigned int last; // N-1 | ||
| 142 | double partitions; // N-1 | ||
| 143 | std::vector<Point> p; // sample points | ||
| 144 | double dist_err, dist_bound, angle_err; | ||
| 145 | }; | ||
| 146 | |||
| 147 | namespace detail | ||
| 148 | { | ||
| 149 | /* | ||
| 150 | * ellipse_equation | ||
| 151 | * | ||
| 152 | * this is an helper struct, it provides two routines: | ||
| 153 | * the first one evaluates the implicit form of an ellipse on a given point | ||
| 154 | * the second one computes the normal versor at a given point of an ellipse | ||
| 155 | * in implicit form | ||
| 156 | */ | ||
| 157 | struct ellipse_equation | ||
| 158 | { | ||
| 159 | ✗ | ellipse_equation(double a, double b, double c, double d, double e, double f) | |
| 160 | ✗ | : A(a), B(b), C(c), D(d), E(e), F(f) | |
| 161 | { | ||
| 162 | ✗ | } | |
| 163 | |||
| 164 | ✗ | double operator()(double x, double y) const | |
| 165 | { | ||
| 166 | // A * x * x + B * x * y + C * y * y + D * x + E * y + F; | ||
| 167 | ✗ | return (A * x + B * y + D) * x + (C * y + E) * y + F; | |
| 168 | } | ||
| 169 | |||
| 170 | ✗ | double operator()(Point const& p) const | |
| 171 | { | ||
| 172 | ✗ | return (*this)(p[X], p[Y]); | |
| 173 | } | ||
| 174 | |||
| 175 | ✗ | Point normal(double x, double y) const | |
| 176 | { | ||
| 177 | ✗ | Point n( 2 * A * x + B * y + D, 2 * C * y + B * x + E ); | |
| 178 | ✗ | return unit_vector(n); | |
| 179 | } | ||
| 180 | |||
| 181 | ✗ | Point normal(Point const& p) const | |
| 182 | { | ||
| 183 | ✗ | return normal(p[X], p[Y]); | |
| 184 | } | ||
| 185 | |||
| 186 | double A, B, C, D, E, F; | ||
| 187 | }; | ||
| 188 | |||
| 189 | } // end namespace detail | ||
| 190 | |||
| 191 | ✗ | make_elliptical_arc:: | |
| 192 | make_elliptical_arc( EllipticalArc& _ea, | ||
| 193 | curve_type const& _curve, | ||
| 194 | unsigned int _total_samples, | ||
| 195 | ✗ | double _tolerance ) | |
| 196 | ✗ | : ea(_ea), curve(_curve), | |
| 197 | ✗ | dcurve( unitVector(derivative(curve)) ), | |
| 198 | ✗ | model(), fitter(model, _total_samples), | |
| 199 | ✗ | tolerance(_tolerance), tol_at_extr(tolerance/2), | |
| 200 | ✗ | tol_at_center(0.1), angle_tol(0.1), | |
| 201 | ✗ | initial_point(curve.at0()), final_point(curve.at1()), | |
| 202 | ✗ | N(_total_samples), last(N-1), partitions(N-1), p(N) | |
| 203 | { | ||
| 204 | ✗ | } | |
| 205 | |||
| 206 | /* | ||
| 207 | * check that the coefficients computed by the fit method satisfy | ||
| 208 | * the tolerance parameters at the k-th sample point | ||
| 209 | */ | ||
| 210 | bool | ||
| 211 | ✗ | make_elliptical_arc:: | |
| 212 | bound_exceeded( unsigned int k, detail::ellipse_equation const & ee, | ||
| 213 | double e1x, double e1y, double e2 ) | ||
| 214 | { | ||
| 215 | ✗ | dist_err = std::fabs( ee(p[k]) ); | |
| 216 | ✗ | dist_bound = std::fabs( e1x * p[k][X] + e1y * p[k][Y] + e2 ); | |
| 217 | // check that the angle btw the tangent versor to the input curve | ||
| 218 | // and the normal versor of the elliptical arc, both evaluate | ||
| 219 | // at the k-th sample point, are really othogonal | ||
| 220 | ✗ | angle_err = std::fabs( dot( dcurve(k/partitions), ee.normal(p[k]) ) ); | |
| 221 | //angle_err *= angle_err; | ||
| 222 | ✗ | return ( dist_err > dist_bound || angle_err > angle_tol ); | |
| 223 | } | ||
| 224 | |||
| 225 | /* | ||
| 226 | * check that the coefficients computed by the fit method satisfy | ||
| 227 | * the tolerance parameters at each sample point | ||
| 228 | */ | ||
| 229 | bool | ||
| 230 | ✗ | make_elliptical_arc:: | |
| 231 | check_bound(double A, double B, double C, double D, double E, double F) | ||
| 232 | { | ||
| 233 | ✗ | detail::ellipse_equation ee(A, B, C, D, E, F); | |
| 234 | |||
| 235 | // check error magnitude at the end-points | ||
| 236 | ✗ | double e1x = (2*A + B) * tol_at_extr; | |
| 237 | ✗ | double e1y = (B + 2*C) * tol_at_extr; | |
| 238 | ✗ | double e2 = ((D + E) + (A + B + C) * tol_at_extr) * tol_at_extr; | |
| 239 | ✗ | if (bound_exceeded(0, ee, e1x, e1y, e2)) | |
| 240 | { | ||
| 241 | ✗ | print_bound_error(0); | |
| 242 | ✗ | return false; | |
| 243 | } | ||
| 244 | ✗ | if (bound_exceeded(0, ee, e1x, e1y, e2)) | |
| 245 | { | ||
| 246 | ✗ | print_bound_error(last); | |
| 247 | ✗ | return false; | |
| 248 | } | ||
| 249 | |||
| 250 | // e1x = derivative((ee(x,y), x) | x->tolerance, y->tolerance | ||
| 251 | ✗ | e1x = (2*A + B) * tolerance; | |
| 252 | // e1y = derivative((ee(x,y), y) | x->tolerance, y->tolerance | ||
| 253 | ✗ | e1y = (B + 2*C) * tolerance; | |
| 254 | // e2 = ee(tolerance, tolerance) - F; | ||
| 255 | ✗ | e2 = ((D + E) + (A + B + C) * tolerance) * tolerance; | |
| 256 | // std::cerr << "e1x = " << e1x << std::endl; | ||
| 257 | // std::cerr << "e1y = " << e1y << std::endl; | ||
| 258 | // std::cerr << "e2 = " << e2 << std::endl; | ||
| 259 | |||
| 260 | // check error magnitude at sample points | ||
| 261 | ✗ | for ( unsigned int k = 1; k < last; ++k ) | |
| 262 | { | ||
| 263 | ✗ | if ( bound_exceeded(k, ee, e1x, e1y, e2) ) | |
| 264 | { | ||
| 265 | ✗ | print_bound_error(k); | |
| 266 | ✗ | return false; | |
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | ✗ | return true; | |
| 271 | } | ||
| 272 | |||
| 273 | /* | ||
| 274 | * fit | ||
| 275 | * | ||
| 276 | * supply the samples to the fitter and compute | ||
| 277 | * the ellipse implicit equation coefficients | ||
| 278 | */ | ||
| 279 | ✗ | void make_elliptical_arc::fit() | |
| 280 | { | ||
| 281 | ✗ | for (unsigned int k = 0; k < N; ++k) | |
| 282 | { | ||
| 283 | ✗ | p[k] = curve( k / partitions ); | |
| 284 | ✗ | fitter.append(p[k]); | |
| 285 | } | ||
| 286 | ✗ | fitter.update(); | |
| 287 | |||
| 288 | ✗ | NL::Vector z(N, 0.0); | |
| 289 | ✗ | fitter.result(z); | |
| 290 | ✗ | } | |
| 291 | |||
| 292 | ✗ | bool make_elliptical_arc::make_elliptiarc() | |
| 293 | { | ||
| 294 | ✗ | const NL::Vector & coeff = fitter.result(); | |
| 295 | ✗ | Ellipse e; | |
| 296 | try | ||
| 297 | { | ||
| 298 | ✗ | e.setCoefficients(1, coeff[0], coeff[1], coeff[2], coeff[3], coeff[4]); | |
| 299 | } | ||
| 300 | ✗ | catch(LogicalError const &exc) | |
| 301 | { | ||
| 302 | ✗ | return false; | |
| 303 | ✗ | } | |
| 304 | |||
| 305 | ✗ | Point inner_point = curve(0.5); | |
| 306 | |||
| 307 | ✗ | std::unique_ptr<EllipticalArc> arc( e.arc(initial_point, inner_point, final_point) ); | |
| 308 | ✗ | ea = *arc; | |
| 309 | |||
| 310 | ✗ | if ( !are_near( e.center(), | |
| 311 | ✗ | ea.center(), | |
| 312 | ✗ | tol_at_center * std::min(e.ray(X),e.ray(Y)) | |
| 313 | ) | ||
| 314 | ) | ||
| 315 | { | ||
| 316 | ✗ | return false; | |
| 317 | } | ||
| 318 | ✗ | return true; | |
| 319 | ✗ | } | |
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | ✗ | bool arc_from_sbasis(EllipticalArc &ea, D2<SBasis> const &in, | |
| 324 | double tolerance, unsigned num_samples) | ||
| 325 | { | ||
| 326 | ✗ | make_elliptical_arc convert(ea, in, num_samples, tolerance); | |
| 327 | ✗ | return convert(); | |
| 328 | ✗ | } | |
| 329 | |||
| 330 | } // end namespace Geom | ||
| 331 | |||
| 332 | /* | ||
| 333 | Local Variables: | ||
| 334 | mode:c++ | ||
| 335 | c-file-style:"stroustrup" | ||
| 336 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 337 | indent-tabs-mode:nil | ||
| 338 | fill-column:99 | ||
| 339 | End: | ||
| 340 | */ | ||
| 341 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 342 |