summaryrefslogtreecommitdiffstats
path: root/src/live_effects/lpe-offset.cpp
blob: ba71794764d887043a0233dd878f0a5015d7fa57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/** \file
 * LPE <offset> implementation
 */
/*
 * Authors:
 *   Maximilian Albert
 *
 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
 * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include <glibmm/i18n.h>

#include "live_effects/lpe-offset.h"
#include "sp-shape.h"
#include "display/curve.h"

#include <2geom/path.h>
#include <2geom/piecewise.h>
#include <2geom/sbasis-geometric.h>
#include <2geom/svg-elliptical-arc.h>
#include <2geom/transforms.h>

namespace Inkscape {
namespace LivePathEffect {

LPEOffset::LPEOffset(LivePathEffectObject *lpeobject) :
    Effect(lpeobject),
    offset_pt(_("Offset"), _("Handle to control the distance of the offset from the curve"), "offset_pt", &wr, this)
{
    show_orig_path = true;

    registerParameter(dynamic_cast<Parameter *>(&offset_pt));
}

LPEOffset::~LPEOffset()
{
}

void
LPEOffset::doOnApply(SPLPEItem const* lpeitem)
{
    Geom::Point offset = *(SP_SHAPE(lpeitem)->_curve->first_point());
    offset_pt.param_update_default(offset);
    offset_pt.param_setValue(offset,true);
}

static void append_half_circle(Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
                               Geom::Point const center, Geom::Point const &dir) {
    using namespace Geom;

    double r = L2(dir);
    SVGEllipticalArc cap(center + dir, r, r, angle_between(Point(1,0), dir), false, false, center - dir);
    Piecewise<D2<SBasis> > cap_pwd2(cap.toSBasis());
    pwd2.continuousConcat(cap_pwd2);
}

void
LPEOffset::doBeforeEffect (SPLPEItem const* lpeitem)
{
    SPLPEItem * item = const_cast<SPLPEItem*>(lpeitem);
    item->apply_to_clippath(item);
    item->apply_to_mask(item);
}

Geom::Piecewise<Geom::D2<Geom::SBasis> >
LPEOffset::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
{
    using namespace Geom;

    Piecewise<D2<SBasis> > output;

    double t = nearest_point(offset_pt, pwd2_in);
    Point A = pwd2_in.valueAt(t);
    double offset = L2(A - offset_pt);

    Piecewise<D2<SBasis> > der = unitVector(derivative(pwd2_in));
    Piecewise<D2<SBasis> > n   = rot90(der);

    output  = pwd2_in + n * offset;
    append_half_circle(output, pwd2_in.lastValue(), n.lastValue() * offset);
    output.continuousConcat(reverse(pwd2_in - n * offset));
    append_half_circle(output, pwd2_in.firstValue(), -n.firstValue() * offset);

    // TODO: here we should remove self-overlaps by applying the "union" boolop
    //       but we'd need to convert the path to a Shape, which is currently
    //       broken in 2geom, so we return the unaltered path

    return output;
}

} //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 :