elliptical-arc.cpp revision 0f57fc4d1a4b661afa344eda781ac05485659cc2
/*
* SVG Elliptical Arc Class
*
* Authors:
* Marco Cecchetti <mrcekets at gmail.com>
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
* Copyright 2008-2009 Authors
*
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#include <cfloat>
#include <limits>
#include <memory>
namespace Geom
{
/**
* @class EllipticalArc
* @brief Elliptical arc curve
*
* Elliptical arc is a curve taking the shape of a section of an ellipse.
*
* The arc function has two forms: the regular one, mapping the unit interval to points
* on 2D plane (the linear domain), and a second form that maps some interval
* \f$A \subseteq [0,2\pi)\f$ to the same points (the angular domain). The interval \f$A\f$
* determines which part of the ellipse forms the arc. The arc is said to contain an angle
* if its angular domain includes that angle (and therefore it is defined for that angle).
*
* The angular domain considers each ellipse to be
* a rotated, scaled and translated unit circle: 0 corresponds to \f$(1,0)\f$ on the unit circle,
* \f$\pi/2\f$ corresponds to \f$(0,1)\f$, \f$\pi\f$ to \f$(-1,0)\f$ and \f$3\pi/2\f$
* to \f$(0,-1)\f$. After the angle is mapped to a point from a unit circle, the point is
* transformed using a matrix of this form
* \f[ M = \left[ \begin{array}{ccc}
r_X \cos(\theta) & -r_Y \sin(\theta) & 0 \\
r_X \sin(\theta) & r_Y \cos(\theta) & 0 \\
c_X & c_Y & 1 \end{array} \right] \f]
* where \f$r_X, r_Y\f$ are the X and Y rays of the ellipse, \f$\theta\f$ is its angle of rotation,
* and \f$c_X, c_Y\f$ the coordinates of the ellipse's center - thus mapping the angle
* to some point on the ellipse. Note that for example the point at angluar coordinate 0,
* the center and the point at angular coordinate \f$\pi/4\f$ do not necessarily
* create an angle of \f$\pi/4\f$ radians; it is only the case if both axes of the ellipse
* are of the same length (i.e. it is a circle).
*
* @image html ellipse-angular-coordinates.png "An illustration of the angular domain"
*
* Each arc is defined by five variables: The initial and final point, the ellipse's rays,
* and the ellipse's rotation. Each set of those parameters corresponds to four different arcs,
* with two of them larger than half an ellipse and two of them turning clockwise while traveling
* from initial to final point. The two flags disambiguate between them: "large arc flag" selects
* the bigger arc, while the "sweep flag" selects the clockwise arc.
*
* @image html elliptical-arc-flags.png "The four possible arcs and the meaning of flags"
*
* @ingroup Curves
*/
{
double extremes[4];
double arc_extremes[4];
arc_extremes[0] = initialPoint()[X];
for (unsigned i = 0; i < 4; ++i) {
if (containsAngle(extremes[i])) {
}
}
}
{
return ret;
}
{
if ( d == X ) {
+ center(X);
} else {
+ center(Y);
}
}
{
return ret;
}
{
if ( center(d) == v )
return sol;
}
{
{ "d == X; ray(X) == 0; "
"s = (v - center(X)) / ( -ray(Y) * std::sin(_rot_angle) ); "
"s should be contained in [-1,1]",
"d == X; ray(Y) == 0; "
"s = (v - center(X)) / ( ray(X) * std::cos(_rot_angle) ); "
"s should be contained in [-1,1]"
},
{ "d == Y; ray(X) == 0; "
"s = (v - center(X)) / ( ray(Y) * std::cos(_rot_angle) ); "
"s should be contained in [-1,1]",
"d == Y; ray(Y) == 0; "
"s = (v - center(X)) / ( ray(X) * std::sin(_rot_angle) ); "
"s should be contained in [-1,1]"
},
};
{
{
if ( initialPoint()[d] == v && finalPoint()[d] == v )
{
}
if ( (initialPoint()[d] < finalPoint()[d])
&& (initialPoint()[d] > v || finalPoint()[d] < v) )
{
return sol;
}
if ( (initialPoint()[d] > finalPoint()[d])
&& (finalPoint()[d] > v || initialPoint()[d] < v) )
{
return sol;
}
double ray_prj = 0.0;
switch(d)
{
case X:
switch(dim)
{
break;
break;
}
break;
case Y:
switch(dim)
{
break;
break;
}
break;
}
if ( s < -1 || s > 1 )
{
}
switch(dim)
{
case X:
{
if ( s < 0 ) s += 2*M_PI;
}
else
{
s = M_PI - s;
}
break;
case Y:
{
s = 2*M_PI - s;
}
break;
}
//std::cerr << "s = " << rad_to_deg(s);
s = map_to_01(s);
//std::cerr << " -> t: " << s << std::endl;
if ( !(s < 0 || s > 1) )
return sol;
}
}
//std::cerr << "a = " << a << std::endl;
//std::cerr << "b = " << b << std::endl;
//std::cerr << "c = " << c << std::endl;
if ( are_near(a,0) )
{
if ( !are_near(b,0) )
{
if ( s < 0 ) s += 2*M_PI;
}
}
else
{
double delta = b * b - a * c;
//std::cerr << "delta = " << delta << std::endl;
{
if ( s < 0 ) s += 2*M_PI;
}
else if ( delta > 0 )
{
if ( s < 0 ) s += 2*M_PI;
if ( s < 0 ) s += 2*M_PI;
}
}
{
//std::cerr << "s = " << rad_to_deg(sol[i]);
//std::cerr << " -> t: " << sol[i] << std::endl;
}
return arc_sol;
}
// D(E(t,C),t) = E(t+PI/2,O), where C is the ellipse center
// the derivative doesn't rotate the ellipse but there is a translation
// of the parameter t by an angle of PI/2 so the ellipse points are shifted
// of such an angle in the cw direction
{
{
}
{
}
return result;
}
{
finalAngle(), _sweep);
for ( unsigned int i = 0; i < m; ++i )
{
}
m = nn / 4;
for ( unsigned int i = 1; i < m; ++i )
{
for ( unsigned int j = 0; j < 4; ++j )
}
m = nn - 4 * m;
for ( unsigned int i = 0; i < m; ++i )
{
}
return result;
}
{
}
{
// fix input arguments
if (f < 0) f = 0;
if (f > 1) f = 1;
if (t < 0) t = 0;
if (t > 1) t = 1;
if ( are_near(f, t) )
{
return arc;
}
arc->_large_arc = false;
return arc;
}
// the arc is the same but traversed in the opposite direction
return rarc;
}
#ifdef HAVE_GSL // GSL is required for function "solve_reals"
{
{
THROW_RANGEERROR("[from,to] interval out of range");
}
{
return result;
}
{
{
{
}
else
{
}
}
else
{
{
}
else
{
}
}
return result;
}
{
{
}
// TODO: implement case r != 0
// Point np = ray(X) * unit_vector(r);
// std::vector<double> solX = roots(np[X],X);
// std::vector<double> solY = roots(np[Y],Y);
// double t;
// if ( are_near(solX[0], solY[0]) || are_near(solX[0], solY[1]))
// {
// t = solX[0];
// }
// else
// {
// t = solX[1];
// }
// if ( !(t < from || t > to) )
// {
// result.push_back(t);
// }
// else
// {
//
// }
}
// solve the equation <D(E(t),t)|E(t)-p> == 0
// that provides min and max distance points
// on the ellipse E wrt the point p
// after the substitutions:
// cos(t) = (1 - s^2) / (1 + s^2)
// sin(t) = 2t / (1 + s^2)
// where s = tan(t/2)
// we get a 4th degree equation in s
/*
* ry s^4 ((-cy + py) Cos[Phi] + (cx - px) Sin[Phi]) +
* ry ((cy - py) Cos[Phi] + (-cx + px) Sin[Phi]) +
* 2 s^3 (rx^2 - ry^2 + (-cx + px) rx Cos[Phi] + (-cy + py) rx Sin[Phi]) +
* 2 s (-rx^2 + ry^2 + (-cx + px) rx Cos[Phi] + (-cy + py) rx Sin[Phi])
*/
coeff[2] = 0;
// for ( unsigned int i = 0; i < 5; ++i )
// std::cerr << "c[" << i << "] = " << coeff[i] << std::endl;
// gsl_poly_complex_solve raises an error
// if the leading coefficient is zero
{
{
if ( sq > 0 )
{
}
}
}
else
{
}
{
}
// when s -> Infinity then <D(E)|E-p> -> 0 iff coeff[4] == 0
// so we add M_PI to the solutions being lim arctan(s) = PI when s->Infinity
{
}
double dsq;
{
if ( mindistsq1 > dsq )
{
mindistsq1 = dsq;
mi1 = i;
}
else if ( mindistsq2 > dsq )
{
mindistsq2 = dsq;
mi2 = i;
}
}
{
}
bool second_sol = false;
{
{
second_sol = true;
}
}
// we need to test extreme points too
if ( second_sol )
{
if ( mindistsq2 > dsq1 )
{
mindistsq2 = dsq1;
}
{
}
if ( mindistsq2 > dsq2 )
{
}
{
}
}
else
{
{
{
}
{
}
else
{
}
}
}
return result;
}
#endif
/*
* NOTE: this implementation follows Standard SVG 1.1 implementation guidelines
* for elliptical arc curves. See Appendix F.6.
*/
{
// TODO move this to SVGElipticalArc?
if (svg)
{
if ( initialPoint() == finalPoint() )
{
_center = initialPoint();
_large_arc = _sweep = false;
return;
}
{
_rays[Y] = 0;
_start_angle = 0;
_end_angle = M_PI;
_large_arc = false;
_sweep = false;
return;
}
}
else
{
{
{
_start_angle = _end_angle = 0;
_center = initialPoint();
return;
}
else
{
THROW_RANGEERROR("initial and final point are the same");
}
}
{ // but initialPoint != finalPoint
"there is no ellipse that satisfies the given constraints: "
"ray(X) == 0 && ray(Y) == 0 but initialPoint != finalPoint"
);
}
{
{
{
_start_angle = 0;
_end_angle = M_PI;
return;
}
{
_start_angle = M_PI;
_end_angle = 0;
return;
}
"there is no ellipse that satisfies the given constraints: "
"ray(Y) == 0 "
"and slope(initialPoint - finalPoint) != rotation_angle "
"and != rotation_angle + PI"
);
}
{
"there is no ellipse that satisfies the given constraints: "
"ray(Y) == 0 and distance(initialPoint, finalPoint) > 2*ray(X)"
);
}
else
{
"there is infinite ellipses that satisfy the given constraints: "
"ray(Y) == 0 and distance(initialPoint, finalPoint) < 2*ray(X)"
);
}
}
{
{
{
return;
}
{
return;
}
"there is no ellipse that satisfies the given constraints: "
"ray(X) == 0 "
"and slope(initialPoint - finalPoint) != rotation_angle + PI/2 "
"and != rotation_angle + (3/2)*PI"
);
}
{
"there is no ellipse that satisfies the given constraints: "
"ray(X) == 0 and distance(initialPoint, finalPoint) > 2*ray(Y)"
);
}
else
{
"there is infinite ellipses that satisfy the given constraints: "
"ray(X) == 0 and distance(initialPoint, finalPoint) < 2*ray(Y)"
);
}
}
}
m[1] = -m[1];
m[2] = -m[2];
Point p = (d / 2) * m;
Point c(0,0);
if (rad > 1)
{
rad -= 1;
}
{
}
else
{
"there is no ellipse that satisfies the given constraints"
);
}
Point v(1, 0);
}
{
// the interval of parametrization has to be [0,1]
// order = 4 seems to be enough to get a perfect looking elliptical arc
// ensure that endpoints remain exact
for ( int d = 0 ; d < 2 ; d++ ) {
arc[d][0][0] = initialPoint()[d];
}
return arc;
}
{
inner_point * m,
finalPoint() * m,
isSVGCompliant() );
}
{
finalAngle(), _sweep);
}
} // end namespace Geom
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :