1 /*
2 * Utilities for handling coordinate system transformations in filters
3 *
4 * Author:
5 * Niko Kiirala <niko@kiirala.com>
6 *
7 * Copyright (C) 2007 Niko Kiirala
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #include <glib.h>
14 #include "display/nr-filter-units.h"
15 #include "libnr/nr-matrix.h"
16 #include "libnr/nr-matrix-fns.h"
17 #include "libnr/nr-matrix-ops.h"
18 #include "libnr/nr-matrix-scale-ops.h"
19 #include "libnr/nr-rect.h"
20 #include "libnr/nr-rect-l.h"
21 #include "libnr/nr-scale.h"
22 #include "sp-filter-units.h"
24 namespace NR {
26 FilterUnits::FilterUnits() :
27 filterUnits(SP_FILTER_UNITS_OBJECTBOUNDINGBOX),
28 primitiveUnits(SP_FILTER_UNITS_USERSPACEONUSE),
29 resolution_x(-1), resolution_y(-1),
30 paraller_axis(false), automatic_resolution(true)
31 {}
33 FilterUnits::FilterUnits(SPFilterUnits const filterUnits, SPFilterUnits const primitiveUnits) :
34 filterUnits(filterUnits), primitiveUnits(primitiveUnits),
35 resolution_x(-1), resolution_y(-1),
36 paraller_axis(false), automatic_resolution(true)
37 {}
39 void FilterUnits::set_ctm(Matrix const &ctm) {
40 this->ctm = ctm;
41 }
43 void FilterUnits::set_resolution(double const x_res, double const y_res) {
44 g_assert(x_res > 0);
45 g_assert(y_res > 0);
47 resolution_x = x_res;
48 resolution_y = y_res;
49 }
51 void FilterUnits::set_item_bbox(Rect const &bbox) {
52 item_bbox = bbox;
53 }
55 void FilterUnits::set_filter_area(Rect const &area) {
56 filter_area = area;
57 }
59 void FilterUnits::set_paraller(bool const paraller) {
60 paraller_axis = paraller;
61 }
63 void FilterUnits::set_automatic_resolution(bool const automatic) {
64 automatic_resolution = automatic;
65 }
67 Matrix FilterUnits::get_matrix_user2pb() const {
68 g_assert(resolution_x > 0);
69 g_assert(resolution_y > 0);
71 Matrix u2pb = ctm;
73 if (paraller_axis || !automatic_resolution) {
74 u2pb[0] = resolution_x / (filter_area.max()[X] - filter_area.min()[X]);
75 u2pb[1] = 0;
76 u2pb[2] = 0;
77 u2pb[3] = resolution_y / (filter_area.max()[Y] - filter_area.min()[Y]);
78 u2pb[4] = ctm[4];
79 u2pb[5] = ctm[5];
80 }
82 return u2pb;
83 }
85 Matrix FilterUnits::get_matrix_units2pb(SPFilterUnits units) const {
86 if (units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
87 Matrix u2pb = get_matrix_user2pb();
88 Point origo(item_bbox.min());
89 origo *= u2pb;
90 Point i_end(item_bbox.max()[X], item_bbox.min()[Y]);
91 i_end *= u2pb;
92 Point j_end(item_bbox.min()[X], item_bbox.max()[Y]);
93 j_end *= u2pb;
95 double len_i = sqrt((origo[X] - i_end[X]) * (origo[X] - i_end[X])
96 + (origo[Y] - i_end[Y]) * (origo[Y] - i_end[Y]));
97 double len_j = sqrt((origo[X] - j_end[X]) * (origo[X] - j_end[X])
98 + (origo[Y] - j_end[Y]) * (origo[Y] - j_end[Y]));
100 /* TODO: make sure that user coordinate system (0,0) is in correct
101 * place in pixblock coordinates */
102 scale scaling(1.0 / len_i, 1.0 / len_j);
103 u2pb *= scaling;
104 return u2pb;
105 } else if (units == SP_FILTER_UNITS_USERSPACEONUSE) {
106 return get_matrix_user2pb();
107 } else {
108 g_warning("Error in NR::FilterUnits::get_matrix_units2pb: unrecognized unit type (%d)", units);
109 return Matrix();
110 }
111 }
113 Matrix FilterUnits::get_matrix_filterunits2pb() const {
114 return get_matrix_units2pb(filterUnits);
115 }
117 Matrix FilterUnits::get_matrix_primitiveunits2pb() const {
118 return get_matrix_units2pb(primitiveUnits);
119 }
121 Matrix FilterUnits::get_matrix_display2pb() const {
122 Matrix d2pb = ctm.inverse();
123 d2pb *= get_matrix_user2pb();
124 return d2pb;
125 }
127 Matrix FilterUnits::get_matrix_pb2display() const {
128 Matrix pb2d = get_matrix_user2pb().inverse();
129 pb2d *= ctm;
130 return pb2d;
131 }
133 Matrix FilterUnits::get_matrix_user2units(SPFilterUnits units) const {
134 if (units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
135 /* No need to worry about rotations: bounding box coordinates
136 * always have base vectors paraller with userspace coordinates */
137 Point min(item_bbox.min());
138 Point max(item_bbox.max());
139 double scale_x = 1.0 / (max[X] - min[X]);
140 double scale_y = 1.0 / (max[Y] - min[Y]);
141 return Matrix(scale_x, 0,
142 0, scale_y,
143 min[X] * scale_x, min[Y] * scale_y);
144 } else if (units == SP_FILTER_UNITS_USERSPACEONUSE) {
145 return identity();
146 } else {
147 g_warning("Error in NR::FilterUnits::get_matrix_user2units: unrecognized unit type (%d)", units);
148 return Matrix();
149 }
150 }
152 Matrix FilterUnits::get_matrix_user2filterunits() const {
153 return get_matrix_user2units(filterUnits);
154 }
156 Matrix FilterUnits::get_matrix_user2primitiveunits() const {
157 return get_matrix_user2units(primitiveUnits);
158 }
160 IRect FilterUnits::get_pixblock_filterarea_paraller() const {
161 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
162 Matrix u2pb = get_matrix_user2pb();
164 for (int i = 0 ; i < 4 ; i++) {
165 Point p = filter_area.corner(i);
166 p *= u2pb;
167 if (p[X] < min_x) min_x = (int)std::floor(p[X]);
168 if (p[X] > max_x) max_x = (int)std::ceil(p[X]);
169 if (p[Y] < min_y) min_y = (int)std::floor(p[Y]);
170 if (p[Y] > max_y) max_y = (int)std::ceil(p[Y]);
171 }
172 IRect ret(IPoint(min_x, min_y), IPoint(max_x, max_y));
173 return ret;
174 }
176 FilterUnits& FilterUnits::operator=(FilterUnits const &other) {
177 filterUnits = other.filterUnits;
178 primitiveUnits = other.primitiveUnits;
179 resolution_x = other.resolution_x;
180 resolution_y = other.resolution_y;
181 paraller_axis = other.paraller_axis;
182 automatic_resolution = other.automatic_resolution;
183 ctm = other.ctm;
184 item_bbox = other.item_bbox;
185 filter_area = other.filter_area;
186 return *this;
187 }
189 } // namespace NR
192 /*
193 Local Variables:
194 mode:c++
195 c-file-style:"stroustrup"
196 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
197 indent-tabs-mode:nil
198 fill-column:99
199 End:
200 */
201 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :