1 /** \file
2 * Implementation of sp_item_notify_moveto().
3 */
5 #include <sp-item.h>
6 #include <libnr/nr-matrix-translate-ops.h>
7 #include <sp-guide.h>
8 #include <sp-item-rm-unsatisfied-cns.h>
9 using std::vector;
12 /**
13 * Called by sp_guide_moveto to indicate that the guide line corresponding to g has been moved, and
14 * that consequently this item should move with it.
15 *
16 * \pre exist [cn in item.constraints] g eq cn.g.
17 */
18 void sp_item_notify_moveto(SPItem &item, SPGuide const &mv_g, int const snappoint_ix,
19 double const position, bool const commit)
20 {
21 g_return_if_fail(SP_IS_ITEM(&item));
22 g_return_if_fail( unsigned(snappoint_ix) < 8 );
23 NR::Point const dir( mv_g.normal );
24 double const dir_lensq(dot(dir, dir));
25 g_return_if_fail( dir_lensq != 0 );
27 vector<NR::Point> snappoints;
28 sp_item_snappoints(&item, SnapPointsIter(snappoints));
29 g_return_if_fail( snappoint_ix < int(snappoints.size()) );
31 double const pos0 = dot(dir, snappoints[snappoint_ix]);
32 /// \todo effic: skip if mv_g is already satisfied.
34 /* Translate along dir to make dot(dir, snappoints(item)[snappoint_ix]) == position. */
36 /* Calculation:
37 dot(dir, snappoints[snappoint_ix] + s * dir) = position.
38 dot(dir, snappoints[snappoint_ix]) + dot(dir, s * dir) = position.
39 pos0 + s * dot(dir, dir) = position.
40 s * lensq(dir) = position - pos0.
41 s = (position - pos0) / dot(dir, dir). */
42 NR::translate const tr( ( position - pos0 )
43 * ( dir / dir_lensq ) );
44 sp_item_set_i2d_affine(&item, sp_item_i2d_affine(&item) * tr);
45 /// \todo Reget snappoints, check satisfied.
47 if (commit) {
48 /// \todo Consider maintaining a set of dirty items.
50 /* Commit repr. */
51 {
52 sp_item_write_transform(&item, SP_OBJECT_REPR(&item), item.transform);
53 }
55 sp_item_rm_unsatisfied_cns(item);
56 #if 0 /* nyi */
57 move_cn_to_front(mv_g, snappoint_ix, item.constraints);
58 /** \note If the guideline is connected to multiple snappoints of
59 * this item, then keeping those cns in order requires that the
60 * guide send notifications in order of increasing importance.
61 */
62 #endif
63 }
64 }
67 /*
68 Local Variables:
69 mode:c++
70 c-file-style:"stroustrup"
71 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
72 indent-tabs-mode:nil
73 fill-column:99
74 End:
75 */
76 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :