/** @file
* @brief Ellipse shape
*//*
* Authors:
* Marco Cecchetti <mrcekets at gmail.com>
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
*
* Copyright 2008-2014 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.
*/
namespace Geom {
, _angle(0)
{}
{
if (den == 0) {
THROW_RANGEERROR("den == 0, while computing ellipse centre");
}
// evaluate the a coefficient of the ellipse equation in normal form
// E(x,y) = a*(x-cx)^2 + b*(x-cx)*(y-cy) + c*(y-cy)^2 = 1
// where b = a*B , c = a*C, (cx,cy) == centre
- F;
//evaluate ellipse rotation angle
// evaluate the length of the ellipse rays
if (den == 0) {
THROW_RANGEERROR("den == 0, while computing 'rx' coefficient");
}
if (rx2 < 0) {
THROW_RANGEERROR("rx2 < 0, while computing 'rx' coefficient");
}
if (den == 0) {
THROW_RANGEERROR("den == 0, while computing 'ry' coefficient");
}
if (ry2 < 0) {
THROW_RANGEERROR("ry2 < 0, while computing 'rx' coefficient");
}
// the solution is not unique so we choose always the ellipse
// with a rotation angle between 0 and PI/2
}
{
return p;
}
{
return ret;
}
{
THROW_RANGEERROR("a degenerate ellipse doesn't have an inverse unit circle transform");
}
return ret;
}
{
Point a(0, 0), b(0, 0);
a[d] = -1;
b[d] = 1;
LineSegment ls(a, b);
return ls;
}
{
Point a(0, 0), b(0, 0);
LineSegment ls(a, b);
return ls;
}
{
for (unsigned d = 0; d < 2; ++d) {
}
return result;
}
{
return c;
}
{
THROW_RANGEERROR("a degenerate ellipse doesn't have an implicit form");
}
- 1;
}
{
if (sz < 5) {
THROW_RANGEERROR("fitting error: too few points passed");
}
}
}
{
// This is resistant to degenerate ellipses:
// both flags evaluate to false in that case.
bool large_arc_flag = false;
bool sweep_flag = false;
// Determination of large arc flag:
// large_arc is false when the inner point is on the same side
// of the center---initial point line as the final point, AND
// is on the same side of the center---final point line as the
// initial point.
// Additionally, large_arc is always false when we have exactly
// 1/2 of an arc, i.e. the cross product of the center -> initial point
// and center -> final point vectors is zero.
// Negating the above leads to the condition for large_arc being true.
{
large_arc_flag = true;
}
//cross(-iv, fv) && large_arc_flag
// Determination of sweep flag:
// For clarity, let's assume that Y grows up. Then the cross product
// is positive for points on the left side of a vector and negative
// on the right side of a vector.
//
// cross(?, v) > 0
// o------------------->
// cross(?, v) < 0
//
// If the arc is small (large_arc_flag is false) and the final point
// is on the right side of the vector initial point -> center,
// we have to go in the direction of increasing angles
// (counter-clockwise) and the sweep flag is true.
// If the arc is large, the opposite is true, since we have to reach
// the final point going the long way - in the other direction.
// We can express this observation as:
// cross(_center - ip, fp - _center) < 0 xor large_arc flag
// This is equal to:
// cross(-iv, fv) < 0 xor large_arc flag
// But cross(-iv, fv) is equal to cross(fv, iv) due to antisymmetry
// of the cross product, so we end up with the condition below.
if ((ifcp < 0) ^ large_arc_flag) {
sweep_flag = true;
}
return ret_arc;
}
{
_center *= r;
return *this;
}
{
double angle;
if (am[0] != 0) {
} else if (am[1] != 0) {
} else {
}
_rays[Y] = 0;
return *this;
}
0, 0 );
q = invm * q ;
q *= invm;
return *this;
}
{
return result;
}
{
_angle = 0;
return;
}
if (_angle < 0) {
}
}
}
{
p *= unitCircleTransform();
return p;
}
{
if ( d == X ) {
+ center(X);
} else {
+ center(Y);
}
}
{
// degenerate ellipse is basically a reparametrized line segment
if (ray(X) != 0) {
} else if (ray(Y) != 0) {
} else {
return 0;
}
}
}
{
p *= unitCircleTransform().withoutTranslation();
p.normalize();
return p;
}
{
}
{
// TODO intersect with line segment.
return result;
}
// Ax^2 + Bxy + Cy^2 + Dx + Ey + F
Coord A, B, C, D, E, F;
coefficients(A, B, C, D, E, F);
// generic case
Coord a, b, c;
line.coefficients(a, b, c);
// y = -a/b x - c/b
Coord q = -a/b;
Coord r = -c/b;
// substitute that into the ellipse equation, making it quadratic in x
Coord I = A + B*q + C*q*q; // x^2 terms
Coord K = C*r*r + E*r + F; // x^0 terms
}
} else {
Coord q = -b/a;
Coord r = -c/a;
Coord I = A*q*q + B*q + C;
Coord J = A*2*q*r + B*r + D*q + E;
Coord K = A*r*r + D*r + F;
}
}
return result;
}
{
// we simply re-use the procedure for lines and filter out
// results where the line time value is outside of the unit interval.
return result;
}
{
// handle degenerate cases first
}
// intersection of two ellipses can be solved analytically.
Coord A, B, C, D, E, F;
Coord a, b, c, d, e, f;
// NOTE: the order of coefficients is different to match the convention in the PDF above
// Ax^2 + Bx^2 + Cx + Dy + Exy + F
this->coefficients(A, E, B, C, D, F);
other.coefficients(a, e, b, c, d, f);
// Assume that Q is the ellipse equation given by uppercase letters
// and R is the equation given by lowercase ones. An intersection exists when
// there is a coefficient mu such that
// mu Q + R = 0
//
// This can be written in the following way:
//
// | ff cc/2 dd/2 | |1|
// mu Q + R = [1 x y] | cc/2 aa ee/2 | |x| = 0
// | dd/2 ee/2 bb | |y|
//
// where aa = mu A + a and so on. The determinant can be explicitly written out,
// giving an equation which is cubic in mu and can be solved analytically.
Coord I, J, K, L;
I = (-E*E*F + 4*A*B*F + C*D*E - A*D*D - B*C*C) / 4;
J = -((E*E - 4*A*B) * f + (2*E*F - C*D) * e + (2*A*D - C*E) * d +
(2*B*C - D*E) * c + (C*C - 4*A*F) * b + (D*D - 4*B*F) * a) / 4;
K = -((e*e - 4*a*b) * F + (2*e*f - c*d) * E + (2*a*d - c*e) * D +
(2*b*c - d*e) * C + (c*c - 4*a*f) * B + (d*d - 4*b*f) * A) / 4;
L = (-e*e*f + 4*a*b*f + c*d*e - a*d*d - b*c*c) / 4;
// Now that we have solved for mu, we need to check whether the conic
// determined by mu Q + R is reducible to a product of two lines. If it's not,
// it means that there are no intersections. If it is, the intersections of these
// lines with the original ellipses (if there are any) give the coordinates
// of intersections.
// Prefer middle root if there are three.
// Out of three possible pairs of lines that go through four points of intersection
// of two ellipses, this corresponds to cross-lines. These intersect the ellipses
// at less shallow angles than the other two options.
}
if (delta < 0) continue;
break;
}
// if no suitable mu was found, there are no intersections
if (aa != 0) {
} else if (bb != 0) {
Coord q = 0;
} else {
}
// intersect with the obtained lines and report intersections
}
}
}
return result;
}
{
Coord A, B, C, D, E, F;
coefficients(A, B, C, D, E, F);
Bezier x = A*b[X]*b[X] + B*b[X]*b[Y] + C*b[Y]*b[Y] + D*b[X] + E*b[Y] + F;
for (unsigned i = 0; i < r.size(); ++i) {
}
return result;
}
{
Ellipse a = this->canonicalForm();
return true;
}
{
// We want to know whether no point on ellipse a is further than precision
// from the corresponding point on ellipse b. To check this, we compute
// the four extreme points at the end of each ray for each ellipse
// and check whether they are sufficiently close.
// First, we need to correct the angles on the ellipses, so that they are
// no further than M_PI/4 apart. This can always be done by rotating
// and exchanging axes.
}
}
// Do the actual comparison by computing four points on each ellipse.
for (unsigned i = 0; i < 4; ++i) {
return false;
}
return true;
}
{
return out;
}
} // 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 :