From cca66b9ec4e494c1d919bff0f71a820d8afab1fa Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:24:48 +0200 Subject: Adding upstream version 1.2.2. Signed-off-by: Daniel Baumann --- src/live_effects/lpe-sketch.cpp | 384 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 384 insertions(+) create mode 100644 src/live_effects/lpe-sketch.cpp (limited to 'src/live_effects/lpe-sketch.cpp') diff --git a/src/live_effects/lpe-sketch.cpp b/src/live_effects/lpe-sketch.cpp new file mode 100644 index 0000000..802f791 --- /dev/null +++ b/src/live_effects/lpe-sketch.cpp @@ -0,0 +1,384 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * LPE sketch effect implementation. + */ +/* Authors: + * Jean-Francois Barraud + * Johan Engelen + * + * Copyright (C) 2007 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "live_effects/lpe-sketch.h" + +// You might need to include other 2geom files. You can add them here: +#include <2geom/sbasis-math.h> +#include <2geom/bezier-to-sbasis.h> +#include <2geom/path-intersection.h> + +// TODO due to internal breakage in glibmm headers, this must be last: +#include + +namespace Inkscape { +namespace LivePathEffect { + +LPESketch::LPESketch(LivePathEffectObject *lpeobject) : + Effect(lpeobject), + // initialise your parameters here: + //testpointA(_("Test Point A"), _("Test A"), "ptA", &wr, this, Geom::Point(100,100)), + nbiter_approxstrokes(_("Strokes:"), _("Draw that many approximating strokes"), "nbiter_approxstrokes", &wr, this, 5), + strokelength(_("Max stroke length:"), + _("Maximum length of approximating strokes"), "strokelength", &wr, this, 100.), + strokelength_rdm(_("Stroke length variation:"), + _("Random variation of stroke length (relative to maximum length)"), "strokelength_rdm", &wr, this, .3), + strokeoverlap(_("Max. overlap:"), + _("How much successive strokes should overlap (relative to maximum length)"), "strokeoverlap", &wr, this, .3), + strokeoverlap_rdm(_("Overlap variation:"), + _("Random variation of overlap (relative to maximum overlap)"), "strokeoverlap_rdm", &wr, this, .3), + ends_tolerance(_("Max. end tolerance:"), + _("Maximum distance between ends of original and approximating paths (relative to maximum length)"), "ends_tolerance", &wr, this, .1), + parallel_offset(_("Average offset:"), + _("Average distance each stroke is away from the original path"), "parallel_offset", &wr, this, 5.), + tremble_size(_("Max. tremble:"), + _("Maximum tremble magnitude"), "tremble_size", &wr, this, 5.), + tremble_frequency(_("Tremble frequency:"), + _("Average number of tremble periods in a stroke"), "tremble_frequency", &wr, this, 1.) +#ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES + ,nbtangents(_("Construction lines:"), + _("How many construction lines (tangents) to draw"), "nbtangents", &wr, this, 5), + tgtscale(_("Scale:"), + _("Scale factor relating curvature and length of construction lines (try 5*offset)"), "tgtscale", &wr, this, 10.0), + tgtlength(_("Max. length:"), _("Maximum length of construction lines"), "tgtlength", &wr, this, 100.0), + tgtlength_rdm(_("Length variation:"), _("Random variation of the length of construction lines"), "tgtlength_rdm", &wr, this, .3), + tgt_places_rdmness(_("Placement randomness:"), _("0: evenly distributed construction lines, 1: purely random placement"), "tgt_places_rdmness", &wr, this, 1.) +#ifdef LPE_SKETCH_USE_CURVATURE + ,min_curvature(_("k_min:"), _("min curvature"), "k_min", &wr, this, 4.0) + ,max_curvature(_("k_max:"), _("max curvature"), "k_max", &wr, this, 1000.0) +#endif +#endif +{ + // register all your parameters here, so Inkscape knows which parameters this effect has: + //Add some comment in the UI: *warning* the precise output of this effect might change in future releases! + //convert to path if you want to keep exact output unchanged in future releases... + //registerParameter(&testpointA) ); + registerParameter(&nbiter_approxstrokes); + registerParameter(&strokelength); + registerParameter(&strokelength_rdm); + registerParameter(&strokeoverlap); + registerParameter(&strokeoverlap_rdm); + registerParameter(&ends_tolerance); + registerParameter(¶llel_offset); + registerParameter(&tremble_size); + registerParameter(&tremble_frequency); +#ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES + registerParameter(&nbtangents); + registerParameter(&tgt_places_rdmness); + registerParameter(&tgtscale); + registerParameter(&tgtlength); + registerParameter(&tgtlength_rdm); +#ifdef LPE_SKETCH_USE_CURVATURE + registerParameter(&min_curvature); + registerParameter(&max_curvature); +#endif +#endif + + nbiter_approxstrokes.param_make_integer(); + nbiter_approxstrokes.param_set_range(0, std::numeric_limits::max()); + strokelength.param_set_range(1, std::numeric_limits::max()); + strokelength.param_set_increments(1., 5.); + strokelength_rdm.param_set_range(0, 1.); + strokeoverlap.param_set_range(0, 1.); + strokeoverlap.param_set_increments(0.1, 0.30); + ends_tolerance.param_set_range(0., 1.); + parallel_offset.param_set_range(0, std::numeric_limits::max()); + tremble_frequency.param_set_range(0.01, 100.); + tremble_frequency.param_set_increments(.5, 1.5); + strokeoverlap_rdm.param_set_range(0, 1.); + +#ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES + nbtangents.param_make_integer(); + nbtangents.param_set_range(0, std::numeric_limits::max()); + tgtscale.param_set_range(0, std::numeric_limits::max()); + tgtscale.param_set_increments(.1, .5); + tgtlength.param_set_range(0, std::numeric_limits::max()); + tgtlength.param_set_increments(1., 5.); + tgtlength_rdm.param_set_range(0, 1.); + tgt_places_rdmness.param_set_range(0, 1.); + //this is not very smart, but required to avoid having lot of tangents stacked on short components. + //Note: we could specify a density instead of an absolute number, but this would be scale dependent. + concatenate_before_pwd2 = true; +#endif +} + +LPESketch::~LPESketch() += default; + +/* +Geom::Piecewise > +addLinearEnds (Geom::Piecewise > & m){ + using namespace Geom; + Piecewise > output; + Piecewise > start; + Piecewise > end; + double x,y,vx,vy; + + x = m.segs.front()[0].at0(); + y = m.segs.front()[1].at0(); + vx = m.segs.front()[0][1][0]+Tri(m.segs.front()[0][0]); + vy = m.segs.front()[1][1][0]+Tri(m.segs.front()[1][0]); + start = Piecewise >(D2(Linear (x-vx,x),Linear (y-vy,y))); + start.offsetDomain(m.cuts.front()-1.); + + x = m.segs.back()[0].at1(); + y = m.segs.back()[1].at1(); + vx = -m.segs.back()[0][1][1]+Tri(m.segs.back()[0][0]);; + vy = -m.segs.back()[1][1][1]+Tri(m.segs.back()[1][0]);; + end = Piecewise >(D2(Linear (x,x+vx),Linear (y,y+vy))); + //end.offsetDomain(m.cuts.back()); + + output = start; + output.concat(m); + output.concat(end); + return output; +} +*/ + + + +//This returns a random perturbation. Notice the domain is [s0,s0+first multiple of period>s1]... +Geom::Piecewise > +LPESketch::computePerturbation (double s0, double s1){ + using namespace Geom; + Piecewise >res; + + //global offset for this stroke. + double offsetX = 2*parallel_offset-parallel_offset.get_value(); + double offsetY = 2*parallel_offset-parallel_offset.get_value(); + Point A,dA,B,dB,offset = Point(offsetX,offsetY); + //start point A + for (unsigned dim=0; dim<2; dim++){ + A[dim] = offset[dim] + 2*tremble_size-tremble_size.get_value(); + dA[dim] = 2*tremble_size-tremble_size.get_value(); + } + //compute howmany deg 3 sbasis to concat according to frequency. + + unsigned count = unsigned((s1-s0)/strokelength*tremble_frequency)+1; + //unsigned count = unsigned((s1-s0)/tremble_frequency)+1; + + for (unsigned i=0; i perturb = D2(SBasis(2, Linear()), SBasis(2, Linear())); + for (unsigned dim=0; dim<2; dim++){ + B[dim] = offset[dim] + 2*tremble_size-tremble_size.get_value(); + perturb[dim][0] = Linear(A[dim],B[dim]); + dA[dim] = dA[dim]-B[dim]+A[dim]; + //avoid dividing by 0. Very short strokes will have ends parallel to the curve... + if ( s1-s0 > 1e-2) + dB[dim] = -(2*tremble_size-tremble_size.get_value())/(s0-s1)-B[dim]+A[dim]; + else + dB[dim] = -(2*tremble_size-tremble_size.get_value())-B[dim]+A[dim]; + perturb[dim][1] = Linear(dA[dim],dB[dim]); + } + dA = B-A-dB; + A = B; + //dA = B-A-dB; + res.concat(Piecewise >(perturb)); + } + res.setDomain(Interval(s0,s0+count*strokelength/tremble_frequency)); + //res.setDomain(Interval(s0,s0+count*tremble_frequency)); + return res; +} + + +// Main effect body... +Geom::Piecewise > +LPESketch::doEffect_pwd2 (Geom::Piecewise > const & pwd2_in) +{ + using namespace Geom; + //If the input path is empty, do nothing. + //Note: this happens when duplicating a 3d box... dunno why. + if (pwd2_in.size()==0) return pwd2_in; + + Piecewise > output; + + // some variables for futur use (for construction lines; compute arclength only once...) + // notations will be : t = path time, s = distance from start along the path. + Piecewise pathlength; + double total_length = 0; + + //TODO: split Construction Lines/Approximated Strokes into two separate effects? + + //----- Approximated Strokes. + std::vector > > pieces_in = split_at_discontinuities (pwd2_in); + + //work separately on each component. + for (auto piece : pieces_in){ + + Piecewise piecelength = arcLengthSb(piece,.1); + double piece_total_length = piecelength.segs.back().at1()-piecelength.segs.front().at0(); + pathlength.concat(piecelength + total_length); + total_length += piece_total_length; + + + //TODO: better check this on the Geom::Path. + bool closed = piece.segs.front().at0() == piece.segs.back().at1(); + if (closed){ + piece.concat(piece); + piecelength.concat(piecelength+piece_total_length); + } + + for (unsigned i = 0; ipiece_total_length - ends_tolerance.get_value()*strokelength) break; + if ( closed && s0>piece_total_length + s0_initial) break; + + std::vector times; + times = roots(piecelength-s0); + t0 = times.at(0);//there should be one and only one solution!! + + // pick a new end point (s1 = s0 + strokelength). + s1 = s0 + strokelength*(1-strokelength_rdm); + // don't let it go beyond the end of the original path. + // TODO/FIXME: this might result in short strokes near the end... + if (!closed && s1>piece_total_length-ends_tolerance.get_value()*strokelength){ + done = true; + //!!the root solver might miss s1==piece_total_length... + if (s1>piece_total_length){s1 = piece_total_length - ends_tolerance*strokelength-0.0001;} + } + if (closed && s1>piece_total_length + s0_initial){ + done = true; + if (closed && s1>2*piece_total_length){ + s1 = 2*piece_total_length - strokeoverlap*(1-strokeoverlap_rdm)*strokelength-0.0001; + } + } + times = roots(piecelength-s1); + if (times.empty()) break;//we should not be there. + t1 = times[0]; + + //pick a rdm perturbation, and collect the perturbed piece into output. + Piecewise > pwperturb = computePerturbation(s0-0.01,s1+0.01); + pwperturb = compose(pwperturb,portion(piecelength,t0,t1)); + + output.concat(portion(piece,t0,t1)+pwperturb); + + //step points: s0 = s1 - overlap. + //TODO: make sure this has to end? + s0 = s1 - strokeoverlap*(1-strokeoverlap_rdm)*(s1-s0); + } + } + } + +#ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES + + //----- Construction lines. + //TODO: choose places according to curvature?. + + //at this point we should have: + //pathlength = arcLengthSb(pwd2_in,.1); + //total_length = pathlength.segs.back().at1()-pathlength.segs.front().at0(); + Piecewise > m = pwd2_in; + Piecewise > v = derivative(pwd2_in); + Piecewise > a = derivative(v); + +#ifdef LPE_SKETCH_USE_CURVATURE + //---- curvature experiment...(enable + Piecewise k = curvature(pwd2_in); + OptInterval k_bnds = bounds_exact(abs(k)); + double k_min = k_bnds->min() + k_bnds->extent() * min_curvature; + double k_max = k_bnds->min() + k_bnds->extent() * max_curvature; + + Piecewise bump; + //SBasis bump_seg = SBasis( 2, Linear(0) ); + //bump_seg[1] = Linear( 4. ); + SBasis bump_seg = SBasis( 1, Linear(1) ); + bump.push_cut( k_bnds->min() - 1 ); + bump.push( Linear(0), k_min ); + bump.push(bump_seg,k_max); + bump.push( Linear(0), k_bnds->max()+1 ); + + Piecewise repartition = compose( bump, k ); + repartition = integral(repartition); + //------------------------------- +#endif + + for (unsigned i=0; i times; + times = roots(repartition - proba); + double t = times.at(0);//there should be one and only one solution! +#else + //double s = total_length * ( i + tgtlength_rdm ) / (nbtangents+1.); + double reg_place = total_length * ( i + .5) / ( nbtangents ); + double rdm_place = total_length * tgt_places_rdmness; + double s = ( 1.- tgt_places_rdmness.get_value() ) * reg_place + rdm_place ; + std::vector times; + times = roots(pathlength-s); + double t = times.at(0);//there should be one and only one solution! +#endif + Point m_t = m(t), v_t = v(t), a_t = a(t); + //Compute tgt length according to curvature (not exceeding tgtlength) so that + // dist to original curve ~ 4 * (parallel_offset+tremble_size). + //TODO: put this 4 as a parameter in the UI... + //TODO: what if with v=0? + double l = tgtlength*(1-tgtlength_rdm)/v_t.length(); + double r = std::pow(v_t.length(), 3) / cross(v_t, a_t); + r = sqrt((2*fabs(r)-tgtscale)*tgtscale)/v_t.length(); + l=(r tgt = D2(); + for (unsigned dim=0; dim<2; dim++){ + tgt[dim] = SBasis(Linear(m_t[dim]-v_t[dim]*l, m_t[dim]+v_t[dim]*l)); + } + output.concat(Piecewise >(tgt)); + } +#endif + + return output; +} + +void +LPESketch::doBeforeEffect (SPLPEItem const*/*lpeitem*/) +{ + //init random parameters. + parallel_offset.resetRandomizer(); + strokelength_rdm.resetRandomizer(); + strokeoverlap_rdm.resetRandomizer(); + ends_tolerance.resetRandomizer(); + tremble_size.resetRandomizer(); +#ifdef LPE_SKETCH_USE_CONSTRUCTION_LINES + tgtlength_rdm.resetRandomizer(); + tgt_places_rdmness.resetRandomizer(); +#endif +} + +/* ######################## */ + +} //namespace LivePathEffect (setq default-directory "c:/Documents And Settings/jf/Mes Documents/InkscapeSVN") +} /* 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 : -- cgit v1.2.3