elliptical-arc.cpp revision 69340304f32eac4d438c67b5e1f6bc2f0a05ea22
/*
* SVG Elliptical Arc Class
*
* Copyright 2008 Marco Cecchetti <mrcekets at gmail.com>
*
* 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 "elliptical-arc.h"
#include "bezier-curve.h"
#include "poly.h"
#include <cfloat>
#include <limits>
namespace Geom
{
{
arc_extremes[0] = initialPoint()[X];
if ( start_angle() < end_angle() )
{
if ( sweep_flag() )
{
{
{
}
}
}
else
{
{
{
}
}
}
}
else
{
if ( sweep_flag() )
{
{
{
}
}
}
else
{
{
{
}
}
}
}
}
{
if ( d > Y )
{
THROW_RANGEERROR("dimention out of range");
}
{
if ( center(d) == v )
return sol;
}
{
{ "d == X; ray(X) == 0; "
"s = (v - center(X)) / ( -ray(Y) * std::sin(rotation_angle()) ); "
"s should be contained in [-1,1]",
"d == X; ray(Y) == 0; "
"s = (v - center(X)) / ( ray(X) * std::cos(rotation_angle()) ); "
"s should be contained in [-1,1]"
},
{ "d == Y; ray(X) == 0; "
"s = (v - center(X)) / ( ray(Y) * std::cos(rotation_angle()) ); "
"s should be contained in [-1,1]",
"d == Y; ray(Y) == 0; "
"s = (v - center(X)) / ( ray(X) * std::sin(rotation_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;
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;
}
}
switch(d)
{
case X:
break;
case Y:
break;
}
//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;
// return SBasisCurve(toSBasis()).roots(v, d);
}
// D(E(t,C),t) = E(t+PI/2,O)
{
{
}
{
}
return result;
}
{
end_angle(), sweep_flag());
EllipticalArc ea(*this);
for ( unsigned int i = 0; i < m; ++i )
{
}
m = n / 4;
for ( unsigned int i = 1; i < m; ++i )
{
for ( unsigned int j = 0; j < 4; ++j )
}
m = n - 4 * m;
for ( unsigned int i = 0; i < m; ++i )
{
}
return result;
}
{
// the interval of parametrization has to be [0,1]
// order = 4 seems to be enough to get a perfect looking elliptical arc
// should it be choosen in function of the arc length anyway ?
// or maybe a user settable parameter: toSBasis(unsigned int order) ?
return arc;
}
{
if ( sweep_flag() )
if ( start_angle() < end_angle() )
else
else
if ( start_angle() > end_angle() )
else
}
{
if ( d == X )
{
+ center(X);
}
else if ( d == Y )
{
+ center(Y);
}
THROW_RANGEERROR("dimension parameter out of range");
}
{
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) )
{
}
if ( arc->m_start_angle < 0 )
if ( arc->m_end_angle < 0 )
arc->m_large_arc = false;
return arc;
}
// NOTE: doesn't work with 360 deg arcs
{
{
{
m_start_angle = m_end_angle = 0;
m_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"
);
}
{
{
{
m_start_angle = 0;
m_end_angle = M_PI;
return;
}
{
m_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)"
);
}
}
0, 0 );
double half_diff_angle;
{
// if |arg| is a little bit > 1 acos returns nan
half_diff_angle = 0;
else
{
"there is no ellipse that satisfies the given constraints"
);
// assert( -1 < arg && arg < 1 );
// if it fails
// => there is no ellipse that satisfies the given constraints
}
}
else
{
// if |arg| is a little bit > 1 asin returns nan
else
{
"there is no ellipse that satisfies the given constraints"
);
// assert( -1 < arg && arg < 1 );
// if it fails
// => there is no ellipse that satisfies the given constraints
}
}
if ( ( m_large_arc && half_diff_angle > 0 )
|| (!m_large_arc && half_diff_angle < 0 ) )
{
}
// 0 <= m_start_angle, m_end_angle < 2PI
if ( !sweep_flag() )
{
double angle = m_start_angle;
m_end_angle = angle;
}
}
{
if ( sweep_flag() )
{
return angle;
}
else
{
return angle;
}
}
{
end_angle(), sweep_flag());
}
{
{
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
{
}
// else
// {
// double sol[8];
// gsl_poly_complex_workspace * w = gsl_poly_complex_workspace_alloc(5);
// gsl_poly_complex_solve(coeff, 5, w, sol );
// gsl_poly_complex_workspace_free(w);
//
// for ( unsigned int i = 0; i < 4; ++i )
// {
// if ( sol[2*i+1] == 0 ) real_sol.push_back(sol[2*i]);
// }
// }
{
}
// 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;
}
} // 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:encoding=utf-8:textwidth=99 :