Code

New LPE: Mirror reflection
authorcilix42 <cilix42@users.sourceforge.net>
Tue, 10 Jun 2008 15:08:54 +0000 (15:08 +0000)
committercilix42 <cilix42@users.sourceforge.net>
Tue, 10 Jun 2008 15:08:54 +0000 (15:08 +0000)
src/live_effects/Makefile_insert
src/live_effects/effect.cpp
src/live_effects/effect.h
src/live_effects/lpe-mirror_reflect.cpp [new file with mode: 0644]
src/live_effects/lpe-mirror_reflect.h [new file with mode: 0644]

index ea49aa269a4334661aa70f333e3dc46b86c995f9..4f0d46e363485fe32fa5f5d524f6c95701975500 100644 (file)
@@ -50,6 +50,7 @@ live_effects_liblive_effects_a_SOURCES = \
        live_effects/lpe-circle_with_radius.cpp \
        live_effects/lpe-circle_with_radius.h   \
        live_effects/lpe-perspective_path.cpp   \
-       live_effects/lpe-perspective_path.h
-
+       live_effects/lpe-perspective_path.h             \
+       live_effects/lpe-mirror_reflect.cpp     \
+       live_effects/lpe-mirror_reflect.h
 
index c46a9dd2397dc864fc46f382a596866e4fcaa63a..0aaedab0a642a1233d8b56acab177e5b03df4c97 100644 (file)
@@ -50,6 +50,7 @@
 #include "live_effects/lpe-constructgrid.h"
 #include "live_effects/lpe-perp_bisector.h"
 #include "live_effects/lpe-tangent_to_curve.h"
+#include "live_effects/lpe-mirror_reflect.h"
 // end of includes
 
 #include "nodepath.h"
@@ -77,7 +78,8 @@ const Util::EnumData<EffectType> LPETypeData[INVALID_LPE] = {
     {ENVELOPE,              N_("Envelope Deformation"),  "envelope"},
     {CONSTRUCT_GRID,        N_("Construct grid"),        "construct_grid"},
     {PERP_BISECTOR, N_("Perpendicular bisector"), "perp_bisector"},
-    {TANGENT_TO_CURVE, N_("Tangent to curve"), "tangent_to_curve"}
+    {TANGENT_TO_CURVE, N_("Tangent to curve"), "tangent_to_curve"},
+    {MIRROR_REFLECT, N_("Mirror reflection"), "mirror_reflect"},
 };
 const Util::EnumDataConverter<EffectType> LPETypeConverter(LPETypeData, INVALID_LPE);
 
@@ -136,6 +138,9 @@ Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
         case TANGENT_TO_CURVE:
             neweffect = static_cast<Effect*> ( new LPETangentToCurve(lpeobj) );
             break;
+        case MIRROR_REFLECT:
+            neweffect = static_cast<Effect*> ( new LPEMirrorReflect(lpeobj) );
+            break;
         default:
             g_warning("LivePathEffect::Effect::New   called with invalid patheffect type (%d)", lpenr);
             neweffect = NULL;
index b78e931f92f2e4e2d04b58a1a330d3c67ed39224..665509fd118e048df75a8ec0d43e9bd189821755 100644 (file)
@@ -71,6 +71,7 @@ enum EffectType {
     CONSTRUCT_GRID,
     PERP_BISECTOR,
     TANGENT_TO_CURVE,
+    MIRROR_REFLECT,
     INVALID_LPE // This must be last
 };
 
diff --git a/src/live_effects/lpe-mirror_reflect.cpp b/src/live_effects/lpe-mirror_reflect.cpp
new file mode 100644 (file)
index 0000000..438d012
--- /dev/null
@@ -0,0 +1,102 @@
+#define INKSCAPE_LPE_MIRROR_REFLECT_CPP
+/** \file
+ * LPE <mirror_reflection> implementation: mirrors a path with respect to a given line.
+ */
+/*
+ * Authors:
+ *   Maximilian Albert
+ *   Johan Engelen
+ *
+ * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
+ * Copyright (C) Maximilin Albert 2008 <maximilian.albert@gmail.com>
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#include "live_effects/lpe-mirror_reflect.h"
+#include <sp-path.h>
+#include <display/curve.h>
+#include <svg/path-string.h>
+
+#include <2geom/path.h>
+#include <2geom/transforms.h>
+#include <2geom/matrix.h>
+
+namespace Inkscape {
+namespace LivePathEffect {
+
+LPEMirrorReflect::LPEMirrorReflect(LivePathEffectObject *lpeobject) :
+    Effect(lpeobject),
+    reflection_line(_("Reflection line"), _("Line which serves as 'mirror' for the reflection"), "reflection_line", &wr, this, "M0,0 L100,100")
+{
+    registerParameter( dynamic_cast<Parameter *>(&reflection_line) );
+}
+
+LPEMirrorReflect::~LPEMirrorReflect()
+{
+
+}
+
+void
+LPEMirrorReflect::doOnApply (SPLPEItem *lpeitem)
+{
+    using namespace Geom;
+
+    SPCurve *curve = sp_path_get_curve_for_edit (SP_PATH(lpeitem));
+    Point A(curve->first_point().to_2geom());
+    Point B(curve->last_point().to_2geom());
+
+    Point M = (2*A + B)/3; // some point between A and B (a bit closer to A)
+    Point perp_dir = unit_vector((B - A).ccw());
+    
+    Point C(M[X], M[Y] + 150);
+    Point D(M[X], M[Y] - 150);
+
+    Piecewise<D2<SBasis> > rline = Piecewise<D2<SBasis> >(D2<SBasis>(Linear(C[X], D[X]), Linear(C[Y], D[Y])));
+    reflection_line.param_set_and_write_new_value(rline);
+}
+
+std::vector<Geom::Path>
+LPEMirrorReflect::doEffect_path (std::vector<Geom::Path> const & path_in)
+{
+    std::vector<Geom::Path> path_out;
+
+    std::vector<Geom::Path> mline(reflection_line.get_pathvector());
+    Geom::Point A(mline.front().initialPoint());
+    Geom::Point B(mline.back().finalPoint());
+
+    Geom::Matrix m1(1.0, 0.0, 0.0, 1.0, A[0], A[1]);
+    double hyp = Geom::distance(A, B);
+    double c = (B[0] - A[0]) / hyp; // cos(alpha)
+    double s = (B[1] - A[1]) / hyp; // sin(alpha)
+
+    Geom::Matrix m2(c, -s, s, c, 0.0, 0.0);
+    Geom::Matrix sca(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
+
+    Geom::Matrix m = m1.inverse() * m2;
+    m = m * sca;
+    m = m * m2.inverse();
+    m = m * m1;
+
+    for (int i = 0; i < path_in.size(); ++i) {
+        path_out.push_back(path_in[i] * m);
+    }
+
+    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 :
diff --git a/src/live_effects/lpe-mirror_reflect.h b/src/live_effects/lpe-mirror_reflect.h
new file mode 100644 (file)
index 0000000..79b1854
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef INKSCAPE_LPE_MIRROR_REFLECT_H
+#define INKSCAPE_LPE_MIRROR_REFLECT_H
+
+/** \file
+ * LPE <mirror_reflection> implementation: mirrors a path with respect to a given line.
+ */
+/*
+ * Authors:
+ *   Maximilian Albert
+ *   Johan Engelen
+ *
+ * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
+ * Copyright (C) Maximilin Albert 2008 <maximilian.albert@gmail.com>
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+#include "live_effects/effect.h"
+#include "live_effects/parameter/parameter.h"
+#include "live_effects/parameter/point.h"
+#include "live_effects/parameter/path.h"
+
+namespace Inkscape {
+namespace LivePathEffect {
+
+class LPEMirrorReflect : public Effect {
+public:
+    LPEMirrorReflect(LivePathEffectObject *lpeobject);
+    virtual ~LPEMirrorReflect();
+
+    virtual void doOnApply (SPLPEItem *lpeitem);
+
+    virtual LPEPathFlashType pathFlashType() { return PERMANENT_FLASH; }
+
+    virtual std::vector<Geom::Path> doEffect_path (std::vector<Geom::Path> const & path_in);
+
+private:
+    PathParam reflection_line;
+
+    LPEMirrorReflect(const LPEMirrorReflect&);
+    LPEMirrorReflect& operator=(const LPEMirrorReflect&);
+};
+
+} //namespace LivePathEffect
+} //namespace Inkscape
+
+#endif