lpe-powerstroke.cpp revision 414e1596bb9a3aa3293fa8f98f91442c97f8f984
/**
* @file
* PowerStroke LPE implementation. Creates curves with modifiable stroke width.
*/
/* Authors:
* Johan Engelen <j.b.c.engelen@alumnus.utwente.nl>
*
* Copyright (C) 2010-2012 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "live_effects/lpe-powerstroke.h"
#include "live_effects/lpe-powerstroke-interpolators.h"
#include "sp-shape.h"
#include "style.h"
#include <math.h>
#include "spiro.h"
namespace Geom {
// should all be moved to 2geom at some point
/** Find the point where two straight lines cross.
*/
{
}
}
{
}
/**
* document this!
* very quick: this finds the ellipse with minimum eccentricity
passing through point P and Q, with tangent PO at P and QO at Q
*/
{
Point p = P - O;
Point q = Q - O;
double cross = p[Y]*q[X] - p[X]*q[Y];
double a = -q[Y]/cross;
double b = q[X]/cross;
double c = (O[X]*q[Y] - O[Y]*q[X])/cross;
double d = p[Y]/cross;
double e = -p[X]/cross;
double f = (-O[X]*p[Y] + O[Y]*p[X])/cross;
// Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0
double A = (a*d*K+d*d+a*a);
double B = (a*e*K+b*d*K+2*d*e+2*a*b);
double C = (b*e*K+e*e+b*b);
double D = (a*f*K+c*d*K+2*d*f-2*d+2*a*c-2*a);
double E = (b*f*K+c*e*K+2*e*f-2*e+2*b*c-2*b);
double F = c*f*K+f*f-2*f+c*c-2*c+1;
return Ellipse(A, B, C, D, E, F);
}
/**
* Refer to: Weisstein, Eric W. "Circle-Circle Intersection."
From MathWorld--A Wolfram Web Resource.
*
* @return 0 if no intersection
* @return 1 if one circle is contained in the other
* @return 2 if intersections are found (they are written to p0 and p1)
*/
{
/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
/* Determine the straight-line distance between the centers. */
double d = L2(D);
/* Check for solvability. */
{
/* no solution. circles do not intersect. */
return 0;
}
{
/* no solution. one circle is contained in the other */
return 1;
}
/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.
*/
/* Determine the distance from point 0 to point 2. */
/* Determine the coordinates of point 2. */
/* Determine the distance from point 2 to either of the
* intersection points.
*/
/* Now determine the offsets of the intersection points from
* point 2.
*/
/* Determine the absolute intersection points. */
return 2;
}
/**
* Find circle that touches inside of the curve, with radius matching the curvature, at time value \c t.
* Because this method internally uses unitTangentAt, t should be smaller than 1.0 (see unitTangentAt).
*/
{
//Piecewise<SBasis> k = curvature(curve, tol);
}
}
double curv = k(t); // note that this value is signed
}
} // namespace Geom
namespace Inkscape {
namespace LivePathEffect {
};
static const Util::EnumDataConverter<unsigned> InterpolatorTypeConverter(InterpolatorTypeData, sizeof(InterpolatorTypeData)/sizeof(*InterpolatorTypeData));
enum LineCapType {
};
};
static const Util::EnumDataConverter<unsigned> LineCapTypeConverter(LineCapTypeData, sizeof(LineCapTypeData)/sizeof(*LineCapTypeData));
enum LineJoinType {
};
#ifdef LPE_ENABLE_TEST_EFFECTS
#endif
};
static const Util::EnumDataConverter<unsigned> LineJoinTypeConverter(LineJoinTypeData, sizeof(LineJoinTypeData)/sizeof(*LineJoinTypeData));
sort_points(_("Sort points"), _("Sort offset points according to their time value along the curve"), "sort_points", &wr, this, true),
interpolator_type(_("Interpolator type:"), _("Determines which kind of interpolator will be used to interpolate between stroke width along the path"), "interpolator_type", InterpolatorTypeConverter, &wr, this, Geom::Interpolate::INTERP_CUBICBEZIER_JOHAN),
interpolator_beta(_("Smoothness:"), _("Sets the smoothness for the CubicBezierJohan interpolator; 0 = linear interpolation, 1 = smooth"), "interpolator_beta", &wr, this, 0.2),
start_linecap_type(_("Start cap:"), _("Determines the shape of the path's start"), "start_linecap_type", LineCapTypeConverter, &wr, this, LINECAP_ROUND),
linejoin_type(_("Join:"), _("Determines the shape of the path's corners"), "linejoin_type", LineJoinTypeConverter, &wr, this, LINEJOIN_ROUND),
miter_limit(_("Miter limit:"), _("Maximum length of the miter (in units of stroke width)"), "miter_limit", &wr, this, 4.),
end_linecap_type(_("End cap:"), _("Determines the shape of the path's end"), "end_linecap_type", LineCapTypeConverter, &wr, this, LINECAP_ROUND)
{
show_orig_path = true;
/// @todo offset_points are initialized with empty path, is that bug-save?
interpolator_beta.addSlider(true);
}
{
}
void
{
if (SP_IS_SHAPE(lpeitem)) {
} else {
}
}
} else {
g_warning("LPE Powerstroke can only be applied to shapes (not groups).");
}
}
void
{
}
}
{
}
static Geom::Path path_from_piecewise_fix_cusps( Geom::Piecewise<Geom::D2<Geom::SBasis> > const & B,
double miter_limit,
bool /*forward_direction*/,
{
/* per definition, each discontinuity should be fixed with a join-ending, as defined by linejoin_type
*/
if (B.size() == 0) {
}
unsigned prev_i = 0;
for (unsigned i=1; i < B.size(); i++) {
// if segment is degenerate, skip it
// the degeneracy/constancy test had to be loosened (eps > 1e-5)
if (B[i].isConstant(1e-4)) {
continue;
}
{ // discontinuity found, so fix it :-)
if (on_outside) {
// we are on the outside: add some type of join!
switch (jointype) {
case LINEJOIN_ROUND: {
/* for constant width paths, the rounding is a circular arc (rx == ry),
for non-constant width paths, the rounding can be done with an ellipse but is hard and ambiguous.
The elliptical arc should go through the discontinuity's start and end points (of course!)
and also should match the discontinuity tangents at those start and end points.
To resolve the ambiguity, the elliptical arc with minimal eccentricity should be chosen.
A 2Geom method was created to do exactly this :)
*/
if (!O) {
// no center found, i.e. 180 degrees round
break;
}
try {
}
catch (Geom::LogicalError &e) {
// 2geom did not find a fitting ellipse, this happens for weird thick paths :)
// do bevel, and break
break;
}
// check if ellipse.ray is within 'sane' range.
{
// do bevel, and break
break;
}
break;
}
case LINEJOIN_EXTRP_MITER: {
// empty crossing: default to bevel
} else {
// check size of miter
// miter too big: default to bevel
} else {
}
}
break;
}
case LINEJOIN_EXTRP_MITER_ARC: {
if (solutions == 2) {
// points[0] is bad, choose points[1]
// points[1] is bad, choose points[0]
} else {
// both points are good, choose nearest
}
if (arc0) {
delete arc0;
}
if (arc1) {
delete arc1;
}
break;
} else {
// fall back to miter
if (p) {
// check size of miter
// miter OK
}
}
}
/*else if (solutions == 1) { // one circle is inside the other
// don't know what to do: default to bevel
pb.lineTo(B[i].at0());
} else { // no intersections
// don't know what to do: default to bevel
pb.lineTo(B[i].at0());
} */
break;
}
case LINEJOIN_MITER: {
if (p) {
// check size of miter
// miter OK
}
}
break;
}
case LINEJOIN_SPIRO: {
break;
}
case LINEJOIN_BEVEL:
default:
break;
}
} else {
// we are on inside of corner!
// empty crossing or too many crossings: default to bevel
} else {
// :-) quick hack:
}
}
}
} else {
}
prev_i = i;
}
}
{
using namespace Geom;
return path_out;
}
// for now, only regard first subpath and ignore the rest
return path_out;
}
if (sort_points) {
}
// add extra points for interpolation between first and last point
} else {
// add width data for first and last point on the path
// depending on cap type, these first and last points have width zero or take the width from the closest width point.
}
// create stroke path where points (x,y) := (t, offset)
Geom::Interpolate::Interpolator *interpolator = Geom::Interpolate::Interpolator::create(static_cast<Geom::Interpolate::InterpolatorType>(interpolator_type.get_value()));
if (Geom::Interpolate::CubicBezierJohan *johan = dynamic_cast<Geom::Interpolate::CubicBezierJohan*>(interpolator)) {
}
delete interpolator;
// find time values for which x lies outside path domain
// and only take portion of x and y that lies within those time values
}
Geom::Path fixed_path = path_from_piecewise_fix_cusps( pwd2_out, y, jointype, miter_limit, LPE_CONVERSION_TOLERANCE);
Geom::Path fixed_mirrorpath = path_from_piecewise_fix_cusps( mirrorpath, reverse(y), jointype, miter_limit, LPE_CONVERSION_TOLERANCE);
fixed_path.close(true);
fixed_mirrorpath.close(true);
} else {
// add linecaps...
switch (end_linecap) {
case LINECAP_ZERO_WIDTH:
// do nothing
break;
case LINECAP_PEAK:
{
break;
}
case LINECAP_SQUARE:
{
break;
}
case LINECAP_BUTT:
{
break;
}
case LINECAP_ROUND:
default:
{
fixed_path.appendNew<SVGEllipticalArc>( radius1, radius1, M_PI/2., false, y.lastValue() < 0, mirrorpath.firstValue() );
break;
}
}
switch (start_linecap) {
case LINECAP_ZERO_WIDTH:
// do nothing
break;
case LINECAP_PEAK:
{
break;
}
case LINECAP_SQUARE:
{
break;
}
case LINECAP_BUTT:
{
break;
}
case LINECAP_ROUND:
default:
{
fixed_path.appendNew<SVGEllipticalArc>( radius2, radius2, M_PI/2., false, y.firstValue() < 0, pwd2_out.firstValue() );
break;
}
}
fixed_path.close(true);
}
return path_out;
}
/* ######################## */
} //namespace LivePathEffect
} /* namespace Inkscape */
/*
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 :