48 struct ellipse_equation;
85 unsigned int _total_samples,
89 bool bound_exceeded(
unsigned int k, detail::ellipse_equation
const & ee,
90 double e1x,
double e1y,
double e2 );
92 bool check_bound(
double A,
double B,
double C,
double D,
double E,
double F);
96 bool make_elliptiarc();
98 void print_bound_error(
unsigned int k)
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;
117 const NL::Vector & coeff = fitter.result();
119 if ( !check_bound(1, coeff[0], coeff[1], coeff[2], coeff[3], coeff[4]) )
121 if ( !(make_elliptiarc()) )
return false;
128 Piecewise<D2<SBasis> > dcurve;
129 NL::LFMEllipse model;
131 NL::least_squeares_fitter<NL::LFMEllipse> fitter;
138 double tolerance, tol_at_extr, tol_at_center, angle_tol;
139 Point initial_point, final_point;
143 std::vector<Point> p;
144 double dist_err, dist_bound, angle_err;
157struct ellipse_equation
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)
164 double operator()(
double x,
double y)
const
167 return (A * x + B * y + D) * x + (C * y +
E) * y + F;
170 double operator()(
Point const& p)
const
172 return (*
this)(p[
X], p[
Y]);
177 Point n( 2 * A * x + B * y + D, 2 * C * y + B * x + E );
186 double A, B, C,
D,
E, F;
192make_elliptical_arc( EllipticalArc& _ea,
194 unsigned int _total_samples,
196 : ea(_ea),
curve(_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)
212bound_exceeded(
unsigned int k, detail::ellipse_equation
const & ee,
213 double e1x,
double e1y,
double e2 )
215 dist_err = std::fabs( ee(p[k]) );
216 dist_bound = std::fabs( e1x * p[k][X] + e1y * p[k][Y] + e2 );
220 angle_err = std::fabs(
dot( dcurve(k/partitions), ee.normal(p[k]) ) );
222 return ( dist_err > dist_bound || angle_err > angle_tol );
231check_bound(
double A,
double B,
double C,
double D,
double E,
double F)
233 detail::ellipse_equation ee(A, B, C,
D, E, F);
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))
241 print_bound_error(0);
244 if (bound_exceeded(0, ee, e1x, e1y, e2))
246 print_bound_error(last);
251 e1x = (2*A + B) * tolerance;
253 e1y = (B + 2*C) * tolerance;
255 e2 = ((
D +
E) + (A + B + C) * tolerance) * tolerance;
261 for (
unsigned int k = 1; k < last; ++k )
263 if ( bound_exceeded(k, ee, e1x, e1y, e2) )
265 print_bound_error(k);
279void make_elliptical_arc::fit()
281 for (
unsigned int k = 0; k <
N; ++k)
283 p[k] =
curve( k / partitions );
288 NL::Vector z(
N, 0.0);
292bool make_elliptical_arc::make_elliptiarc()
294 const NL::Vector & coeff = fitter.result();
298 e.setCoefficients(1, coeff[0], coeff[1], coeff[2], coeff[3], coeff[4]);
300 catch(LogicalError
const &exc)
307 std::unique_ptr<EllipticalArc> arc( e.arc(initial_point, inner_point, final_point) );
312 tol_at_center * std::min(e.ray(X),e.ray(Y))
324 double tolerance,
unsigned num_samples)
Various utility functions.
pair< double, double > Point
Various trigoniometric helper functions.
Adaptor that creates 2D functions from 1D ones.
void normal(std::vector< Point > &N, std::vector< Point > const &B)
Various utility functions.
bool arc_from_sbasis(EllipticalArc &ea, D2< SBasis > const &in, double tolerance, unsigned num_samples)
bool make_elliptical_arc(EllipticalArc &ea, Point const ¢re, Point const &initial, Point const &final, Point const &inner)
Bezier derivative(Bezier const &a)
Point unit_vector(Point const &a)
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
Piecewise< D2< SBasis > > unitVector(D2< SBasis > const &vect, double tol=.01, unsigned order=3)
Symmetric power basis curve.
void dot(Cairo::RefPtr< Cairo::Context > &cr, double x, double y)