From b0c42c0dfcd02cc05126371948489a5a88b2e4b3 Mon Sep 17 00:00:00 2001 From: cilix42 Date: Tue, 10 Jun 2008 15:08:54 +0000 Subject: [PATCH] New LPE: Mirror reflection --- src/live_effects/Makefile_insert | 5 +- src/live_effects/effect.cpp | 7 +- src/live_effects/effect.h | 1 + src/live_effects/lpe-mirror_reflect.cpp | 102 ++++++++++++++++++++++++ src/live_effects/lpe-mirror_reflect.h | 47 +++++++++++ 5 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/live_effects/lpe-mirror_reflect.cpp create mode 100644 src/live_effects/lpe-mirror_reflect.h diff --git a/src/live_effects/Makefile_insert b/src/live_effects/Makefile_insert index ea49aa269..4f0d46e36 100644 --- a/src/live_effects/Makefile_insert +++ b/src/live_effects/Makefile_insert @@ -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 diff --git a/src/live_effects/effect.cpp b/src/live_effects/effect.cpp index c46a9dd23..0aaedab0a 100644 --- a/src/live_effects/effect.cpp +++ b/src/live_effects/effect.cpp @@ -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 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 LPETypeConverter(LPETypeData, INVALID_LPE); @@ -136,6 +138,9 @@ Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj) case TANGENT_TO_CURVE: neweffect = static_cast ( new LPETangentToCurve(lpeobj) ); break; + case MIRROR_REFLECT: + neweffect = static_cast ( new LPEMirrorReflect(lpeobj) ); + break; default: g_warning("LivePathEffect::Effect::New called with invalid patheffect type (%d)", lpenr); neweffect = NULL; diff --git a/src/live_effects/effect.h b/src/live_effects/effect.h index b78e931f9..665509fd1 100644 --- a/src/live_effects/effect.h +++ b/src/live_effects/effect.h @@ -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 index 000000000..438d012db --- /dev/null +++ b/src/live_effects/lpe-mirror_reflect.cpp @@ -0,0 +1,102 @@ +#define INKSCAPE_LPE_MIRROR_REFLECT_CPP +/** \file + * LPE implementation: mirrors a path with respect to a given line. + */ +/* + * Authors: + * Maximilian Albert + * Johan Engelen + * + * Copyright (C) Johan Engelen 2007 + * Copyright (C) Maximilin Albert 2008 + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "live_effects/lpe-mirror_reflect.h" +#include +#include +#include + +#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(&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 > rline = Piecewise >(D2(Linear(C[X], D[X]), Linear(C[Y], D[Y]))); + reflection_line.param_set_and_write_new_value(rline); +} + +std::vector +LPEMirrorReflect::doEffect_path (std::vector const & path_in) +{ + std::vector path_out; + + std::vector 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 index 000000000..79b1854f8 --- /dev/null +++ b/src/live_effects/lpe-mirror_reflect.h @@ -0,0 +1,47 @@ +#ifndef INKSCAPE_LPE_MIRROR_REFLECT_H +#define INKSCAPE_LPE_MIRROR_REFLECT_H + +/** \file + * LPE implementation: mirrors a path with respect to a given line. + */ +/* + * Authors: + * Maximilian Albert + * Johan Engelen + * + * Copyright (C) Johan Engelen 2007 + * Copyright (C) Maximilin Albert 2008 + * + * 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 doEffect_path (std::vector const & path_in); + +private: + PathParam reflection_line; + + LPEMirrorReflect(const LPEMirrorReflect&); + LPEMirrorReflect& operator=(const LPEMirrorReflect&); +}; + +} //namespace LivePathEffect +} //namespace Inkscape + +#endif -- 2.30.2