lpe-powerstroke.cpp revision 9a9c71675941e564965469decd2f30df9cb68245
/**
* @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 "sp-paint-server.h"
#include "svg/svg-color.h"
#include "desktop-style.h"
#include "svg/css-ostringstream.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);
}
/**
* 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 {
{Geom::Interpolate::INTERP_CENTRIPETAL_CATMULLROM, N_("Centripetal Catmull-Rom"), "CentripetalCatmullRom"}
};
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 {
};
// {LINEJOIN_EXTRP_MITER, N_("Extrapolated"), "extrapolated"}, // disabled because doesn't work well
};
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),
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_BUTT),
linejoin_type(_("Join:"), _("Determines the shape of the path's corners"), "linejoin_type", LineJoinTypeConverter, &wr, this, LINEJOIN_EXTRP_MITER_ARC),
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_BUTT)
{
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)) {
if (server) {
str += "url(#";
str += ")";
}
gchar c[64];
sp_svg_write_color (c, sizeof(c), lpeitem->style->stroke.value.color.toRGBA32(SP_SCALE24_TO_FLOAT(lpeitem->style->stroke_opacity.value)));
} else {
}
} else {
}
item->updateRepr();
} else {
}
}
}
} else {
g_warning("LPE Powerstroke can only be applied to shapes (not groups).");
}
}
{
if (SP_IS_SHAPE(lpeitem)) {
if (server) {
str += "url(#";
str += ")";
}
char c[64] = {0};
sp_svg_write_color (c, sizeof(c), lpeitem->style->fill.value.color.toRGBA32(SP_SCALE24_TO_FLOAT(lpeitem->style->fill_opacity.value)));
} else {
}
item->updateRepr();
}
}
void
{
}
}
{
}
static Geom::Path path_from_piecewise_fix_cusps( Geom::Piecewise<Geom::D2<Geom::SBasis> > const & B,
double miter_limit,
{
/* per definition, each discontinuity should be fixed with a join-ending, as defined by linejoin_type
*/
if (B.size() == 0) {
}
pb.setStitching(true);
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: {
// Extrapolate using the curvature at the end of the path segments to join
// points[0] is bad, choose points[1]
} else if ( dot(tang2, solutions[1].point() - B[i].at0()) > 0 ) { // points[0] could be good, now check points[1]
// points[1] is bad, choose points[0]
} else {
// both points are good, choose nearest
sol = ( distanceSq(B[i].at0(), solutions[0].point()) < distanceSq(B[i].at0(), solutions[1].point()) ) ?
}
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.
}
// do the interpolation in a coordinate system that is more alike to the on-canvas knots,
// instead of the heavily compressed coordinate system of (segment_no offset, Y) in which the knots are stored
}
// 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)) {
}
if (Geom::Interpolate::CubicBezierSmooth *smooth = dynamic_cast<Geom::Interpolate::CubicBezierSmooth*>(interpolator)) {
}
delete interpolator;
// apply the inverse knot-xcoord scaling that was applied before the interpolation
// 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<EllipticalArc>( 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<EllipticalArc>( 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 :