1 #define __SP_ITEM_TRANSFORM_C__
3 /*
4 * Transforming single items
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * bulia byak <buliabyak@gmail.com>
10 * Johan Engelen <goejendaagh@zonnet.nl>
11 *
12 * Copyright (C) 1999-2008 authors
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #include <2geom/transforms.h>
18 #include "sp-item.h"
20 void
21 sp_item_rotate_rel(SPItem *item, Geom::Rotate const &rotation)
22 {
23 Geom::Point center = item->getCenter();
24 Geom::Translate const s(item->getCenter());
25 Geom::Matrix affine = Geom::Matrix(s).inverse() * Geom::Matrix(rotation) * Geom::Matrix(s);
27 // Rotate item.
28 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * (Geom::Matrix)affine);
29 // Use each item's own transform writer, consistent with sp_selection_apply_affine()
30 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
32 // Restore the center position (it's changed because the bbox center changed)
33 if (item->isCenterSet()) {
34 item->setCenter(center * affine);
35 item->updateRepr();
36 }
37 }
39 void
40 sp_item_scale_rel (SPItem *item, Geom::Scale const &scale)
41 {
42 Geom::OptRect bbox = sp_item_bbox_desktop(item);
43 if (bbox) {
44 Geom::Translate const s(bbox->midpoint()); // use getCenter?
45 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * s.inverse() * scale * s);
46 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
47 }
48 }
50 void
51 sp_item_skew_rel (SPItem *item, double skewX, double skewY)
52 {
53 Geom::Point center = item->getCenter();
54 Geom::Translate const s(item->getCenter());
56 Geom::Matrix const skew(1, skewY, skewX, 1, 0, 0);
57 Geom::Matrix affine = Geom::Matrix(s).inverse() * skew * Geom::Matrix(s);
59 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
60 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
62 // Restore the center position (it's changed because the bbox center changed)
63 if (item->isCenterSet()) {
64 item->setCenter(center * affine);
65 item->updateRepr();
66 }
67 }
69 void sp_item_move_rel(SPItem *item, Geom::Translate const &tr)
70 {
71 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * tr);
73 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
74 }
76 /*
77 ** Returns the matrix you need to apply to an object with given visual bbox and strokewidth to
78 scale/move it to the new visual bbox x0/y0/x1/y1. Takes into account the "scale stroke"
79 preference value passed to it. Has to solve a quadratic equation to make sure
80 the goal is met exactly and the stroke scaling is obeyed.
81 */
83 Geom::Matrix
84 get_scale_transform_with_stroke (Geom::Rect const &bbox_param, gdouble strokewidth, bool transform_stroke, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
85 {
86 Geom::Rect bbox (bbox_param);
88 Geom::Matrix p2o = Geom::Translate (-bbox.min());
89 Geom::Matrix o2n = Geom::Translate (x0, y0);
91 Geom::Matrix scale = Geom::Scale (1, 1); // scale component
92 Geom::Matrix unbudge = Geom::Translate (0, 0); // move component to compensate for the drift caused by stroke width change
94 gdouble w0 = bbox[Geom::X].extent(); // will return a value >= 0, as required further down the road
95 gdouble h0 = bbox[Geom::Y].extent();
96 gdouble w1 = x1 - x0; // can have any sign
97 gdouble h1 = y1 - y0;
98 gdouble r0 = strokewidth;
100 if (bbox.hasZeroArea()) {
101 Geom::Matrix move = Geom::Translate(x0 - bbox.min()[Geom::X], y0 - bbox.min()[Geom::Y]);
102 return (move); // cannot scale from empty boxes at all, so only translate
103 }
105 Geom::Matrix direct = Geom::Scale(w1 / w0, h1 / h0);
107 if (fabs(w0 - r0) < 1e-6 || fabs(h0 - r0) < 1e-6 || (!transform_stroke && (fabs(w1 - r0) < 1e-6 || fabs(h1 - r0) < 1e-6))) {
108 return (p2o * direct * o2n); // can't solve the equation: one of the dimensions is equal to stroke width, so return the straightforward scaler
109 }
111 int flip_x = (w1 > 0) ? 1 : -1;
112 int flip_y = (h1 > 0) ? 1 : -1;
114 // w1 and h1 will be negative when mirroring, but if so then e.g. w1-r0 won't make sense
115 // Therefore we will use the absolute values from this point on
116 w1 = fabs(w1);
117 h1 = fabs(h1);
118 r0 = fabs(r0);
119 // w0 and h0 will always be positive due to the definition extent()
121 gdouble ratio_x = (w1 - r0) / (w0 - r0);
122 gdouble ratio_y = (h1 - r0) / (h0 - r0);
124 Geom::Matrix direct_constant_r = Geom::Scale(flip_x * ratio_x, flip_y * ratio_y);
126 if (transform_stroke && r0 != 0 && r0 != NR_HUGE) { // there's stroke, and we need to scale it
127 // These coefficients are obtained from the assumption that scaling applies to the
128 // non-stroked "shape proper" and that stroke scale is scaled by the expansion of that
129 // matrix. We're trying to solve this equation:
130 // r1 = r0 * sqrt (((w1-r0)/(w0-r0))*((h1-r0)/(h0-r0)))
131 // The operant of the sqrt() must be positive, which is ensured by the fabs() a few lines above
132 gdouble A = -w0*h0 + r0*(w0 + h0);
133 gdouble B = -(w1 + h1) * r0*r0;
134 gdouble C = w1 * h1 * r0*r0;
135 if (B*B - 4*A*C > 0) {
136 gdouble r1 = fabs((-B - sqrt(B*B - 4*A*C))/(2*A));
137 //gdouble r2 = (-B + sqrt (B*B - 4*A*C))/(2*A);
138 //std::cout << "r0" << r0 << " r1" << r1 << " r2" << r2 << "\n";
139 //
140 // If w1 < 0 then the scale will be wrong if we just do
141 // gdouble scale_x = (w1 - r1)/(w0 - r0);
142 // Here we also need the absolute values of w0, w1, h0, h1, and r1
143 gdouble scale_x = (w1 - r1)/(w0 - r0);
144 gdouble scale_y = (h1 - r1)/(h0 - r0);
145 scale *= Geom::Scale(flip_x * scale_x, flip_y * scale_y);
146 unbudge *= Geom::Translate (-flip_x * 0.5 * (r0 * scale_x - r1), -flip_y * 0.5 * (r0 * scale_y - r1));
147 } else {
148 scale *= direct;
149 }
150 } else {
151 if (r0 == 0 || r0 == NR_HUGE) { // no stroke to scale
152 scale *= direct;
153 } else {// nonscaling strokewidth
154 scale *= direct_constant_r;
155 unbudge *= Geom::Translate (flip_x * 0.5 * r0 * (1 - ratio_x), flip_y * 0.5 * r0 * (1 - ratio_y));
156 }
157 }
159 return (p2o * scale * unbudge * o2n);
160 }
162 Geom::Rect
163 get_visual_bbox (Geom::OptRect const &initial_geom_bbox, Geom::Matrix const &abs_affine, gdouble const initial_strokewidth, bool const transform_stroke)
164 {
166 g_assert(initial_geom_bbox);
168 // Find the new geometric bounding box; Do this by transforming each corner of
169 // the initial geometric bounding box individually and fitting a new boundingbox
170 // around the transformerd corners
171 Geom::Point const p0 = Geom::Point(initial_geom_bbox->corner(0)) * abs_affine;
172 Geom::Rect new_geom_bbox(p0, p0);
173 for (unsigned i = 1 ; i < 4 ; i++) {
174 new_geom_bbox.expandTo(Geom::Point(initial_geom_bbox->corner(i)) * abs_affine);
175 }
177 Geom::Rect new_visual_bbox = new_geom_bbox;
178 if (initial_strokewidth > 0 && initial_strokewidth < NR_HUGE) {
179 if (transform_stroke) {
180 // scale stroke by: sqrt (((w1-r0)/(w0-r0))*((h1-r0)/(h0-r0))) (for visual bboxes, see get_scale_transform_with_stroke)
181 // equals scaling by: sqrt ((w1/w0)*(h1/h0)) for geometrical bboxes
182 // equals scaling by: sqrt (area1/area0) for geometrical bboxes
183 gdouble const new_strokewidth = initial_strokewidth * sqrt (new_geom_bbox.area() / initial_geom_bbox->area());
184 new_visual_bbox.expandBy(0.5 * new_strokewidth);
185 } else {
186 // Do not transform the stroke
187 new_visual_bbox.expandBy(0.5 * initial_strokewidth);
188 }
189 }
191 return new_visual_bbox;
192 }
194 /*
195 Local Variables:
196 mode:c++
197 c-file-style:"stroustrup"
198 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
199 indent-tabs-mode:nil
200 fill-column:99
201 End:
202 */
203 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :