Code

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