Code

LPE STACKING!
[inkscape.git] / src / live_effects / lpe-tangent_to_curve.cpp
1 #define INKSCAPE_LPE_TANGENT_TO_CURVE_CPP\r
2 /** \file\r
3  * Implementation of tangent-to-curve LPE.\r
4  */\r
5 \r
6 /*\r
7  * Authors:\r
8  *   Johan Engelen\r
9  *   Maximilian Albert\r
10  *\r
11  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
12  * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>\r
13  *\r
14  * Released under GNU GPL, read the file 'COPYING' for more information\r
15  */\r
16 \r
17 #include "live_effects/lpe-tangent_to_curve.h"\r
18 // FIXME: The following are only needed to convert the path's SPCurve* to pwd2.\r
19 //        There must be a more convenient way to achieve this.\r
20 #include "sp-path.h"\r
21 #include "display/curve.h"\r
22 #include "libnr/n-art-bpath-2geom.h"\r
23 \r
24 #include <2geom/path.h>\r
25 #include <2geom/transforms.h>\r
26 \r
27 namespace Inkscape {\r
28 namespace LivePathEffect {\r
29 \r
30 /* FIXME: We should make these member functions of LPETangentToCurve.\r
31           Is there an easy way to register member functions with knotholder?\r
32     KNOWN BUG: Because of the above, this effect does not work well when in an LPE stack\r
33 */\r
34 NR::Point attach_pt_get(SPItem *item) {\r
35     Inkscape::LivePathEffect::LPETangentToCurve *lpe =\r
36         dynamic_cast<Inkscape::LivePathEffect::LPETangentToCurve *> (sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)));\r
37 \r
38     if (lpe)\r
39         return lpe->ptA;\r
40     else\r
41         return NR::Point(0,0);\r
42 }\r
43 \r
44 void attach_pt_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state) {\r
45     Inkscape::LivePathEffect::LPETangentToCurve *lpe =\r
46         dynamic_cast<Inkscape::LivePathEffect::LPETangentToCurve *> (sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)));\r
47 \r
48     if (!lpe)\r
49         return;\r
50 \r
51     using namespace Geom;\r
52 \r
53     // FIXME: There must be a better way of converting the path's SPCurve* to pwd2.\r
54     SPCurve *curve = sp_path_get_curve_for_edit (SP_PATH(item));\r
55     const NArtBpath *bpath = curve->get_bpath();\r
56     Piecewise<D2<SBasis> > pwd2;\r
57     std::vector<Geom::Path> pathv = BPath_to_2GeomPath(bpath);\r
58     for (unsigned int i=0; i < pathv.size(); i++) {\r
59         pwd2.concat(pathv[i].toPwSb());\r
60     }\r
61 \r
62     double t0 = nearest_point(p.to_2geom(), pwd2);\r
63     lpe->t_attach.param_set_value(t0);\r
64 \r
65     // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.\r
66     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), true, true);\r
67 }\r
68 \r
69 NR::Point left_end_get(SPItem *item) {\r
70     Inkscape::LivePathEffect::LPETangentToCurve *lpe =\r
71         dynamic_cast<Inkscape::LivePathEffect::LPETangentToCurve *> (sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)));\r
72 \r
73     if (lpe)\r
74         return lpe->C;\r
75     else\r
76         return NR::Point(0,0);\r
77 }\r
78 \r
79 NR::Point right_end_get(SPItem *item) {\r
80     Inkscape::LivePathEffect::LPETangentToCurve *lpe =\r
81         dynamic_cast<Inkscape::LivePathEffect::LPETangentToCurve *> (sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)));\r
82 \r
83     if (lpe)\r
84         return lpe->D;\r
85     else\r
86         return NR::Point(0,0);\r
87 }\r
88 \r
89 void left_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state) {\r
90     Inkscape::LivePathEffect::LPETangentToCurve *lpe =\r
91         dynamic_cast<Inkscape::LivePathEffect::LPETangentToCurve *> (sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)));\r
92 \r
93     if (!lpe)\r
94         return;\r
95 \r
96     double lambda = Geom::nearest_point(p.to_2geom(), lpe->ptA, lpe->derivA);\r
97     lpe->length_left.param_set_value(-lambda);\r
98 \r
99     // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.\r
100     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), true, true);\r
101 }\r
102 \r
103 void right_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state) {\r
104     Inkscape::LivePathEffect::LPETangentToCurve *lpe =\r
105         dynamic_cast<Inkscape::LivePathEffect::LPETangentToCurve *> (sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)));\r
106 \r
107     if (!lpe)\r
108         return;\r
109 \r
110     double lambda = Geom::nearest_point(p.to_2geom(), lpe->ptA, lpe->derivA);\r
111     lpe->length_right.param_set_value(lambda);\r
112 \r
113     // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.\r
114     sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), true, true);\r
115 }\r
116 \r
117 LPETangentToCurve::LPETangentToCurve(LivePathEffectObject *lpeobject) :\r
118     Effect(lpeobject),\r
119     t_attach(_("Location along curve"), _("Location of the point of attachment along the curve (between 0.0 and number-of-segments)"), "t_attach", &wr, this, 0.5),\r
120     length_left(_("Length left"), _("Specifies the left end of the tangent"), "length-left", &wr, this, 150),\r
121     length_right(_("Length right"), _("Specifies the right end of the tangent"), "length-right", &wr, this, 150),\r
122     angle(_("Angle"), _("Additional angle between tangent and curve"), "angle", &wr, this, 0.0)\r
123 {\r
124     registerParameter( dynamic_cast<Parameter *>(&t_attach) );\r
125     registerParameter( dynamic_cast<Parameter *>(&length_left) );\r
126     registerParameter( dynamic_cast<Parameter *>(&length_right) );\r
127     registerParameter( dynamic_cast<Parameter *>(&angle) );\r
128     registerKnotHolderHandle(attach_pt_set, attach_pt_get);\r
129     registerKnotHolderHandle(left_end_set, left_end_get);\r
130     registerKnotHolderHandle(right_end_set, right_end_get);\r
131 }\r
132 \r
133 LPETangentToCurve::~LPETangentToCurve()\r
134 {\r
135 }\r
136 \r
137 Geom::Piecewise<Geom::D2<Geom::SBasis> >\r
138 LPETangentToCurve::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)\r
139 {\r
140     using namespace Geom;\r
141     Piecewise<D2<SBasis> > output;\r
142 \r
143     ptA = pwd2_in.valueAt(t_attach);\r
144     derivA = unit_vector(derivative(pwd2_in).valueAt(t_attach));\r
145 \r
146     // TODO: Why are positive angles measured clockwise, not counterclockwise?\r
147     Geom::Rotate rot(Geom::Rotate::from_degrees(-angle));\r
148     derivA = derivA * rot;\r
149 \r
150     C = ptA - derivA * length_left;\r
151     D = ptA + derivA * length_right;\r
152 \r
153     output = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));\r
154 \r
155     return output;\r
156 }\r
157 \r
158 } //namespace LivePathEffect\r
159 } /* namespace Inkscape */\r
160 \r
161 /*\r
162   Local Variables:\r
163   mode:c++\r
164   c-file-style:"stroustrup"\r
165   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
166   indent-tabs-mode:nil\r
167   fill-column:99\r
168   End:\r
169 */\r
170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r