1 /*
2 * Horizontal/vertical but can also be angled line
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * Johan Engelen
7 * Maximilian Albert <maximilian.albert@gmail.com>
8 *
9 * Copyright (C) 2000-2002 Lauris Kaplinski
10 * Copyright (C) 2007 Johan Engelen
11 * Copyright (C) 2009 Maximilian Albert
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
17 #include <libnr/nr-pixops.h>
18 #include <2geom/transforms.h>
19 #include "sp-canvas-util.h"
20 #include "sp-ctrlpoint.h"
21 #include "guideline.h"
23 static void sp_guideline_class_init(SPGuideLineClass *c);
24 static void sp_guideline_init(SPGuideLine *guideline);
25 static void sp_guideline_destroy(GtkObject *object);
27 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
28 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
30 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
32 static void sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba);
34 static SPCanvasItemClass *parent_class;
36 GType sp_guideline_get_type()
37 {
38 static GType guideline_type = 0;
40 if (!guideline_type) {
41 static GTypeInfo const guideline_info = {
42 sizeof (SPGuideLineClass),
43 NULL, NULL,
44 (GClassInitFunc) sp_guideline_class_init,
45 NULL, NULL,
46 sizeof (SPGuideLine),
47 16,
48 (GInstanceInitFunc) sp_guideline_init,
49 NULL,
50 };
52 guideline_type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPGuideLine", &guideline_info, (GTypeFlags) 0);
53 }
55 return guideline_type;
56 }
58 static void sp_guideline_class_init(SPGuideLineClass *c)
59 {
60 parent_class = (SPCanvasItemClass*) g_type_class_peek_parent(c);
62 GtkObjectClass *object_class = (GtkObjectClass *) c;
63 object_class->destroy = sp_guideline_destroy;
65 SPCanvasItemClass *item_class = (SPCanvasItemClass *) c;
66 item_class->update = sp_guideline_update;
67 item_class->render = sp_guideline_render;
68 item_class->point = sp_guideline_point;
69 }
71 static void sp_guideline_init(SPGuideLine *gl)
72 {
73 gl->rgba = 0x0000ff7f;
75 gl->normal_to_line = Geom::Point(0,1);
76 gl->angle = 3.14159265358979323846/2;
77 gl->point_on_line = Geom::Point(0,0);
78 gl->sensitive = 0;
80 gl->origin = NULL;
81 }
83 static void sp_guideline_destroy(GtkObject *object)
84 {
85 g_return_if_fail (object != NULL);
86 g_return_if_fail (SP_IS_GUIDELINE (object));
87 //g_return_if_fail (SP_GUIDELINE(object)->origin != NULL);
88 //g_return_if_fail (SP_IS_CTRLPOINT(SP_GUIDELINE(object)->origin));
90 if (SP_GUIDELINE(object)->origin != NULL && SP_IS_CTRLPOINT(SP_GUIDELINE(object)->origin)) {
91 gtk_object_destroy(GTK_OBJECT(SP_GUIDELINE(object)->origin));
92 } else {
93 // FIXME: This branch shouldn't be reached (although it seems to be harmless).
94 //g_error("Why can it be that gl->origin is not a valid SPCtrlPoint?\n");
95 }
97 GTK_OBJECT_CLASS(parent_class)->destroy(object);
98 }
100 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
101 {
102 SPGuideLine const *gl = SP_GUIDELINE (item);
104 sp_canvas_prepare_buffer(buf);
106 unsigned int const r = NR_RGBA32_R (gl->rgba);
107 unsigned int const g = NR_RGBA32_G (gl->rgba);
108 unsigned int const b = NR_RGBA32_B (gl->rgba);
109 unsigned int const a = NR_RGBA32_A (gl->rgba);
111 if (gl->is_vertical()) {
112 int position = (int) Inkscape::round(gl->point_on_line[Geom::X]);
113 if (position < buf->rect.x0 || position >= buf->rect.x1) {
114 return;
115 }
117 int p0 = buf->rect.y0;
118 int p1 = buf->rect.y1;
119 int step = buf->buf_rowstride;
120 unsigned char *d = buf->buf + 4 * (position - buf->rect.x0);
122 for (int p = p0; p < p1; p++) {
123 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
124 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
125 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
126 d += step;
127 }
128 } else if (gl->is_horizontal()) {
129 int position = (int) Inkscape::round(gl->point_on_line[Geom::Y]);
130 if (position < buf->rect.y0 || position >= buf->rect.y1) {
131 return;
132 }
134 int p0 = buf->rect.x0;
135 int p1 = buf->rect.x1;
136 int step = 4;
137 unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
139 for (int p = p0; p < p1; p++) {
140 d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
141 d[1] = NR_COMPOSEN11_1111(g, a, d[1]);
142 d[2] = NR_COMPOSEN11_1111(b, a, d[2]);
143 d += step;
144 }
145 } else {
146 // render angled line, once intersection has been detected, draw from there.
147 Geom::Point parallel_to_line( gl->normal_to_line[Geom::Y],
148 /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]);
150 //try to intersect with left vertical of rect
151 double y_intersect_left = (buf->rect.x0 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
152 if ( (y_intersect_left >= buf->rect.y0) && (y_intersect_left <= buf->rect.y1) ) {
153 // intersects with left vertical!
154 double y_intersect_right = (buf->rect.x1 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
155 sp_guideline_drawline (buf, buf->rect.x0, static_cast<gint>(round(y_intersect_left)), buf->rect.x1, static_cast<gint>(round(y_intersect_right)), gl->rgba);
156 return;
157 }
159 //try to intersect with right vertical of rect
160 double y_intersect_right = (buf->rect.x1 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
161 if ( (y_intersect_right >= buf->rect.y0) && (y_intersect_right <= buf->rect.y1) ) {
162 // intersects with right vertical!
163 sp_guideline_drawline (buf, buf->rect.x1, static_cast<gint>(round(y_intersect_right)), buf->rect.x0, static_cast<gint>(round(y_intersect_left)), gl->rgba);
164 return;
165 }
167 //try to intersect with top horizontal of rect
168 double x_intersect_top = (buf->rect.y0 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
169 if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
170 // intersects with top horizontal!
171 double x_intersect_bottom = (buf->rect.y1 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
172 sp_guideline_drawline (buf, static_cast<gint>(round(x_intersect_top)), buf->rect.y0, static_cast<gint>(round(x_intersect_bottom)), buf->rect.y1, gl->rgba);
173 return;
174 }
176 //try to intersect with bottom horizontal of rect
177 double x_intersect_bottom = (buf->rect.y1 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
178 if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
179 // intersects with bottom horizontal!
180 sp_guideline_drawline (buf, static_cast<gint>(round(x_intersect_bottom)), buf->rect.y1, static_cast<gint>(round(x_intersect_top)), buf->rect.y0, gl->rgba);
181 return;
182 }
183 }
184 }
186 static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
187 {
188 SPGuideLine *gl = SP_GUIDELINE(item);
190 if (((SPCanvasItemClass *) parent_class)->update) {
191 ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
192 }
194 gl->point_on_line[Geom::X] = affine[4];
195 gl->point_on_line[Geom::Y] = affine[5];
197 sp_ctrlpoint_set_coords(gl->origin, gl->point_on_line * affine.inverse());
198 sp_canvas_item_request_update(SP_CANVAS_ITEM (gl->origin));
200 if (gl->is_horizontal()) {
201 sp_canvas_update_bbox (item, -1000000, (int) Inkscape::round(gl->point_on_line[Geom::Y]), 1000000, (int) Inkscape::round(gl->point_on_line[Geom::Y] + 1));
202 } else if (gl->is_vertical()) {
203 sp_canvas_update_bbox (item, (int) Inkscape::round(gl->point_on_line[Geom::X]), -1000000, (int) Inkscape::round(gl->point_on_line[Geom::X] + 1), 1000000);
204 } else {
205 sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
206 }
207 }
209 // Returns 0.0 if point is on the guideline
210 static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
211 {
212 SPGuideLine *gl = SP_GUIDELINE (item);
214 if (!gl->sensitive) {
215 return NR_HUGE;
216 }
218 *actual_item = item;
220 Geom::Point vec(gl->normal_to_line[Geom::X], - gl->normal_to_line[Geom::Y]);
221 double distance = Geom::dot((p - gl->point_on_line), vec);
222 return MAX(fabs(distance)-1, 0);
223 }
225 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
226 {
227 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
228 SPCanvasItem *origin = sp_canvas_item_new(parent, SP_TYPE_CTRLPOINT, NULL);
230 SPGuideLine *gl = SP_GUIDELINE(item);
231 SPCtrlPoint *cp = SP_CTRLPOINT(origin);
232 gl->origin = cp;
234 normal.normalize();
235 gl->normal_to_line = normal;
236 gl->angle = tan( -gl->normal_to_line[Geom::X] / gl->normal_to_line[Geom::Y]);
237 sp_guideline_set_position(gl, point_on_line);
239 sp_ctrlpoint_set_coords(cp, point_on_line);
241 return item;
242 }
244 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
245 {
246 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl), Geom::Matrix(Geom::Translate(point_on_line)));
247 sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl->origin), Geom::Matrix(Geom::Translate(point_on_line)));
248 }
250 void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
251 {
252 gl->normal_to_line = normal_to_line;
253 gl->angle = tan( -normal_to_line[Geom::X] / normal_to_line[Geom::Y]);
255 sp_canvas_item_request_update(SP_CANVAS_ITEM (gl));
256 }
258 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
259 {
260 gl->rgba = rgba;
261 sp_ctrlpoint_set_color(gl->origin, rgba);
263 sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
264 }
266 void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
267 {
268 gl->sensitive = sensitive;
269 }
271 void sp_guideline_delete(SPGuideLine *gl)
272 {
273 //gtk_object_destroy(GTK_OBJECT(gl->origin));
274 gtk_object_destroy(GTK_OBJECT(gl));
275 }
277 //##########################################################
278 // Line rendering
279 #define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
281 /**
282 \brief This function renders a pixel on a particular buffer.
284 The topleft of the buffer equals
285 ( rect.x0 , rect.y0 ) in screen coordinates
286 ( 0 , 0 ) in setpixel coordinates
287 The bottomright of the buffer equals
288 ( rect.x1 , rect,y1 ) in screen coordinates
289 ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
290 */
291 static void
292 sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
293 {
294 #ifdef SAFE_SETPIXEL
295 if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
296 #endif
297 guint r, g, b, a;
298 r = NR_RGBA32_R (rgba);
299 g = NR_RGBA32_G (rgba);
300 b = NR_RGBA32_B (rgba);
301 a = NR_RGBA32_A (rgba);
302 guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
303 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
304 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
305 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
306 #ifdef SAFE_SETPIXEL
307 }
308 #endif
309 }
311 /**
312 \brief This function renders a line on a particular canvas buffer,
313 using Bresenham's line drawing function.
314 http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
315 Coordinates are interpreted as SCREENcoordinates
316 */
317 static void
318 sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
319 {
320 int dy = y1 - y0;
321 int dx = x1 - x0;
322 int stepx, stepy;
324 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
325 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
326 dy <<= 1; // dy is now 2*dy
327 dx <<= 1; // dx is now 2*dx
329 sp_guideline_setpixel(buf, x0, y0, rgba);
330 if (dx > dy) {
331 int fraction = dy - (dx >> 1); // same as 2*dy - dx
332 while (x0 != x1) {
333 if (fraction >= 0) {
334 y0 += stepy;
335 fraction -= dx; // same as fraction -= 2*dx
336 }
337 x0 += stepx;
338 fraction += dy; // same as fraction -= 2*dy
339 sp_guideline_setpixel(buf, x0, y0, rgba);
340 }
341 } else {
342 int fraction = dx - (dy >> 1);
343 while (y0 != y1) {
344 if (fraction >= 0) {
345 x0 += stepx;
346 fraction -= dy;
347 }
348 y0 += stepy;
349 fraction += dx;
350 sp_guideline_setpixel(buf, x0, y0, rgba);
351 }
352 }
353 }
355 /*
356 Local Variables:
357 mode:c++
358 c-file-style:"stroustrup"
359 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
360 indent-tabs-mode:nil
361 fill-column:99
362 End:
363 */
364 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :