sp-spiral.cpp revision fdc44da6a01ea7605271feed15df61005ddd0219
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * <sodipodi:spiral> implementation
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Mitsuru Oka <oka326@parkcity.ne.jp>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Lauris Kaplinski <lauris@kaplinski.com>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Copyright (C) 1999-2002 Lauris Kaplinski
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Copyright (C) 2000-2001 Ximian, Inc.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Released under GNU GPL, read the file 'COPYING' for more information
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic void sp_spiral_class_init (SPSpiralClass *klass);
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic void sp_spiral_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic Inkscape::XML::Node *sp_spiral_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic void sp_spiral_set (SPObject *object, unsigned int key, const gchar *value);
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic void sp_spiral_update (SPObject *object, SPCtx *ctx, guint flags);
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshstatic gchar * sp_spiral_description (SPItem * item);
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshstatic void sp_spiral_snappoints(SPItem const *item, SnapPointsIter p);
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic NR::Point sp_spiral_get_tangent (SPSpiral const *spiral, gdouble t);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh * Register SPSpiral class and return its type number.
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh spiral_type = g_type_register_static (SP_TYPE_SHAPE, "SPSpiral", &spiral_info, (GTypeFlags)0);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * SPSpiral vtable initialization.
a4d12a5147f3d1d6b568a326e39ef5dca384248dmiklosh parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * Callback for SPSpiral object initialization.
f9ab06d037336cf8796b54c94a119f40eda79a46Kris * Virtual build: set spiral properties from corresponding repr.
f9ab06d037336cf8796b54c94a119f40eda79a46Krissp_spiral_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
75b857d473541532819bd791105cb352c9a43214buliabyak ((SPObjectClass *) parent_class)->build (object, document, repr);
be17b6e6b80af045ef40c635ea563ebd987269b8tavmjong-free sp_object_read_attr (object, "sodipodi:expansion");
be17b6e6b80af045ef40c635ea563ebd987269b8tavmjong-free sp_object_read_attr (object, "sodipodi:revolution");
75b857d473541532819bd791105cb352c9a43214buliabyak sp_object_read_attr (object, "sodipodi:argument");
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh * Virtual write: write spiral attributes to corresponding repr.
7ddc475794d846646261f6b12015d44ac361d070Kris De Gussemsp_spiral_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* Fixme: we may replace these attributes by
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * sodipodi:spiral="cx cy exp revo rad arg t0"
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:cx", spiral->cx);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:cy", spiral->cy);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:expansion", spiral->exp);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:revolution", spiral->revo);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:radius", spiral->rad);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:argument", spiral->arg);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh sp_repr_set_svg_double(repr, "sodipodi:t0", spiral->t0);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // make sure the curve is rebuilt with all up-to-date parameters
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh //Duplicate the path
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh //Nulls might be possible if this called iteratively
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh //g_warning("sp_spiral_write(): No path to copy\n");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh //g_warning("sp_spiral_write(): No path to copy\n");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ((SPObjectClass *) (parent_class))->write (object, repr, flags | SP_SHAPE_WRITE_PATH);
1667116521643e2475184b048e0abb77a2aa9735miklosh * Virtual set: change spiral object attribute.
1667116521643e2475184b048e0abb77a2aa9735mikloshsp_spiral_set (SPObject *object, unsigned int key, const gchar *value)
1667116521643e2475184b048e0abb77a2aa9735miklosh /// \todo fixme: we should really collect updates
1667116521643e2475184b048e0abb77a2aa9735miklosh if (!sp_svg_length_read_computed_absolute (value, &spiral->cx)) {
1667116521643e2475184b048e0abb77a2aa9735miklosh object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh if (!sp_svg_length_read_computed_absolute (value, &spiral->cy)) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh * FIXME: check that value looks like a (finite)
68664e00e2372534b4df2fdc5f54f836bafece18miklosh * number. Create a routine that uses strtod, and
68664e00e2372534b4df2fdc5f54f836bafece18miklosh * accepts a default value (if strtod finds an error).
68664e00e2372534b4df2fdc5f54f836bafece18miklosh * N.B. atof/sscanf/strtod consider "nan" and "inf"
13c1b2b6c3fc75e657813da1865541253a2b1dceapenner * to be valid numbers.
1c03744513f6db77018fe703507fe04011c20b09Krzysztof Kosiński spiral->exp = g_ascii_strtod (value, NULL);
1c03744513f6db77018fe703507fe04011c20b09Krzysztof Kosiński spiral->exp = CLAMP (spiral->exp, 0.0, 1000.0);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (!sp_svg_length_read_computed_absolute (value, &spiral->rad)) {
d9a7c806ee7f408ddb61ff4f233c9d96111ee2b5johanengelen object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
d9a7c806ee7f408ddb61ff4f233c9d96111ee2b5johanengelen * FIXME: We still need some bounds on arg, for
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * numerical reasons. E.g., we don't want inf or NaN,
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * nor near-infinite numbers. I'm inclined to take
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * modulo 2*pi. If so, then change the knot editors,
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * which use atan2 - revo*2*pi, which typically
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh * results in very negative arg.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Have shared constants for the allowable bounds for
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * attributes. There was a bug here where we used -1.0
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * as the minimum (which leads to NaN via, e.g.,
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * pow(-1.0, 0.5); see sp_spiral_get_xy for
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * requirements.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ((SPObjectClass *) parent_class)->set (object, key, value);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Virtual update callback.
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshsp_spiral_update (SPObject *object, SPCtx *ctx, guint flags)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ((SPObjectClass *) parent_class)->update (object, ctx, flags);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Return textual description of spiral.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // TRANSLATORS: since turn count isn't an integer, please adjust the
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // string as needed to deal with an localized plural forms.
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh return g_strdup_printf (_("<b>Spiral</b> with %3f turns"), SP_SPIRAL(item)->revo);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Fit beziers together to spiral and draw it.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \pre dstep \> 0.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \pre is_unit_vector(*hat1).
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * \post is_unit_vector(*hat2).
1ac9ddf51315777a429c1083a3fee7eba89f400dKrzysztof Kosiński#define BEZIER_LENGTH (BEZIER_SIZE * FITTING_MAX_BEZIERS)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh for (d = *t, i = 0; i <= SAMPLE_SIZE; d += dstep, i++) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* Avoid useless adjacent dups. (Otherwise we can have all of darray filled with
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh the same value, which upsets chord_length_parameterize.) */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if ((i != 0)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh && (d < 1.0)) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /** We mustn't increase dstep for subsequent values of
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * i: for large spiral.exp values, rate of growth
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * increases very rapidly.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Get the function itself to decide what value of d
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * to use next: ensure that we move at least 0.25 *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * stroke width, for example. The derivative (as used
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * for get_tangent before normalization) would be
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * useful for estimating the appropriate d value. Or
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * perhaps just start with a small dstep and scale by
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * some small number until we move >= 0.25 *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * stroke_width. Must revert to the original dstep
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * value for next iteration to avoid the problem
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * mentioned above.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* == t + (SAMPLE_SIZE - 1) * dstep, in absence of dups. */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * We should use better algorithm to specify maximum error.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh depth = sp_bezier_fit_cubic_full (bezier, NULL, darray, SAMPLE_SIZE,
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_assert(depth * BEZIER_SIZE <= gint(G_N_ELEMENTS(bezier)));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_print ("[%s] depth=%d, dstep=%g, t0=%g, t=%g, arg=%g\n",
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh debug_state, depth, dstep, spiral->t0, *t, spiral->arg);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SP_OBJECT (spiral)->requestModified(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_print ("cx=%g, cy=%g, exp=%g, revo=%g, rad=%g, arg=%g, t0=%g\n",
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh /* Initial moveto. */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_curve_moveto(c, sp_spiral_get_xy(spiral, spiral->t0));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh NR::Point hat1 = sp_spiral_get_tangent (spiral, spiral->t0);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_spiral_fit_and_draw (spiral, c, dstep, darray, hat1, hat2, &t);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_spiral_fit_and_draw (spiral, c, (1.0 - t)/(SAMPLE_SIZE - 1.0),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_shape_set_curve_insync ((SPShape *) spiral, c, TRUE);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Set spiral properties and update display.
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * Consider applying CLAMP or adding in-bounds assertions for
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * some of these parameters.
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * Virtual snappoints callback.
3686c32a570c3df738a09b34e85fc5d6bd50d020mikloshstatic void sp_spiral_snappoints(SPItem const *item, SnapPointsIter p)
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh ((SPItemClass *) parent_class)->snappoints (item, p);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * Return one of the points on the spiral.
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * \param t specifies how far along the spiral.
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * \pre \a t in [0.0, 2.03]. (It doesn't make sense for t to be much more
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * than 1.0, though some callers go slightly beyond 1.0 for curve-fitting
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * purposes.)
3686c32a570c3df738a09b34e85fc5d6bd50d020mikloshNR::Point sp_spiral_get_xy (SPSpiral const *spiral, gdouble t)
1db439af43130c9695dbbb661e893d56006bb072miklosh /* Otherwise we get NaN for t==0. */
1db439af43130c9695dbbb661e893d56006bb072miklosh /* Anything much more results in infinities. Even allowing 1000 is somewhat overkill. */
1db439af43130c9695dbbb661e893d56006bb072miklosh /* Any callers passing -ve t will have a bug for non-integral values of exp. */
1db439af43130c9695dbbb661e893d56006bb072miklosh double const rad = spiral->rad * pow(t, (double) spiral->exp);
1db439af43130c9695dbbb661e893d56006bb072miklosh double const arg = 2.0 * M_PI * spiral->revo * t + spiral->arg;
1db439af43130c9695dbbb661e893d56006bb072miklosh * Returns the derivative of sp_spiral_get_xy with respect to t,
1db439af43130c9695dbbb661e893d56006bb072miklosh * scaled to a unit vector.
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * \pre spiral != 0.
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \pre 0 \<= t.
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \pre p != NULL.
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * \post is_unit_vector(*p).
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshsp_spiral_get_tangent (SPSpiral const *spiral, gdouble t)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* See above for comments on these assertions. */
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh double const t_scaled = 2.0 * M_PI * spiral->revo * t;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Check that this isn't being too hopeful of the hypot
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * function. E.g. test with numbers around 2**-1070
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * (denormalized numbers), preferably on a few different
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * platforms. However, njh says that the usual implementation
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * does handle both very big and very small numbers.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* ret = spiral->exp * (c, s) + t_scaled * (-s, c);
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen alternatively ret = (spiral->exp, t_scaled) * (( c, s),
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh (-s, c)).*/
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh /* ret should already be approximately normalized: the
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh matrix ((c, -s), (s, c)) is orthogonal (it just
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh rotates by arg), and unrotated has been normalized,
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh so ret is already of unit length other than numerical
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh error in the above matrix multiplication. */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * I haven't checked how important it is for ret to be very
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * near unit length; we could get rid of the below.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* Proof that ret length is non-zero: see above. (Should be near 1.) */
1c03744513f6db77018fe703507fe04011c20b09Krzysztof Kosiński * Compute rad and/or arg for point on spiral.
f9325af537ca5517eb50ef95f432a3204616f6b3apennersp_spiral_get_polar (SPSpiral const *spiral, gdouble t, gdouble *rad, gdouble *arg)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Return true if spiral has properties that make it invalid.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Local Variables:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c-file-style:"stroustrup"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh indent-tabs-mode:nil
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh fill-column:99
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :