Code

3D grid: When snapping, consider both left and right vertical lines next to the point...
[inkscape.git] / src / display / canvas-axonomgrid.cpp
1 #define CANVAS_AXONOMGRID_C
3 /*
4  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
5  */
7  /*
8   * Current limits are: one axis (y-axis) is always vertical. The other two
9   * axes are bound to a certain range of angles. The z-axis always has an angle
10   * smaller than 90 degrees (measured from horizontal, 0 degrees being a line extending
11   * to the right). The x-axis will always have an angle between 0 and 90 degrees.
12   * When I quickly think about it: all possibilities are probably covered this way. Eg.
13   * a z-axis with negative angle can be replaced with an x-axis, etc.
14   */
16  /*
17   * TODO:  LOTS LOTS LOTS. Clean up code. dirty as hell
18   * THIS FILE AND THE HEADER FILE NEED HUGE CLEANING UP. PLEASE DO NOT HESISTATE TO DO SO.
19   * For example: the line drawing code should not be here. There _must_ be a function somewhere else that can provide this functionality...
20   */
22 #include "sp-canvas-util.h"
23 #include "canvas-axonomgrid.h"
24 #include "util/mathfns.h" 
25 #include "display-forward.h"
26 #include <libnr/nr-pixops.h>
29 #include "canvas-grid.h"
30 #include "desktop-handles.h"
31 #include "helper/units.h"
32 #include "svg/svg-color.h"
33 #include "xml/node-event-vector.h"
34 #include "sp-object.h"
36 #include "sp-namedview.h"
37 #include "inkscape.h"
38 #include "desktop.h"
40 #include "document.h"
42 #define SAFE_SETPIXEL   //undefine this when it is certain that setpixel is never called with invalid params
44 #define DEFAULTGRIDCOLOR    0x0000FF20
45 #define DEFAULTGRIDEMPCOLOR 0x0000FF50
47 enum Dim3 { X=0, Y, Z };
49 #ifndef M_PI
50 # define M_PI 3.14159265358979323846
51 #endif
53 static double deg_to_rad(double deg) { return deg*M_PI/180.0;}
56 /**
57     \brief  This function renders a pixel on a particular buffer.
59     The topleft of the buffer equals
60                         ( rect.x0 , rect.y0 )  in screen coordinates
61                         ( 0 , 0 )  in setpixel coordinates
62     The bottomright of the buffer equals
63                         ( rect.x1 , rect,y1 )  in screen coordinates
64                         ( rect.x1 - rect.x0 , rect.y1 - rect.y0 )  in setpixel coordinates
65 */
66 static void
67 sp_caxonomgrid_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
68 {
69 #ifdef SAFE_SETPIXEL
70     if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
71 #endif
72         guint r, g, b, a;
73         r = NR_RGBA32_R (rgba);
74         g = NR_RGBA32_G (rgba);
75         b = NR_RGBA32_B (rgba);
76         a = NR_RGBA32_A (rgba);
77         guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
78         p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
79         p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
80         p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
81 #ifdef SAFE_SETPIXEL
82     }
83 #endif
84 }
86 /**
87     \brief  This function renders a line on a particular canvas buffer,
88             using Bresenham's line drawing function.
89             http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
90             Coordinates are interpreted as SCREENcoordinates
91 */
92 static void
93 sp_caxonomgrid_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
94 {
95     int dy = y1 - y0;
96     int dx = x1 - x0;
97     int stepx, stepy;
99     if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
100     if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
101     dy <<= 1;                                                  // dy is now 2*dy
102     dx <<= 1;                                                  // dx is now 2*dx
104     sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
105     if (dx > dy) {
106         int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
107         while (x0 != x1) {
108             if (fraction >= 0) {
109                 y0 += stepy;
110                 fraction -= dx;                                // same as fraction -= 2*dx
111             }
112             x0 += stepx;
113             fraction += dy;                                    // same as fraction -= 2*dy
114             sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
115         }
116     } else {
117         int fraction = dx - (dy >> 1);
118         while (y0 != y1) {
119             if (fraction >= 0) {
120                 x0 += stepx;
121                 fraction -= dy;
122             }
123             y0 += stepy;
124             fraction += dx;
125             sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
126         }
127     }
131 static void
132 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
134     if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
135         guint r, g, b, a;
136         gint y0, y1, y;
137         guchar *p;
138         r = NR_RGBA32_R(rgba);
139         g = NR_RGBA32_G (rgba);
140         b = NR_RGBA32_B (rgba);
141         a = NR_RGBA32_A (rgba);
142         y0 = MAX (buf->rect.y0, ys);
143         y1 = MIN (buf->rect.y1, ye + 1);
144         p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
145         for (y = y0; y < y1; y++) {
146             p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
147             p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
148             p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
149             p += buf->buf_rowstride;
150         }
151     }
154 namespace Inkscape {
157 /**
158 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP  TO QUICKLY GET RESULTS
160  * Helper function that attachs widgets in a 3xn table. The widgets come in an
161  * array that has two entries per table row. The two entries code for four
162  * possible cases: (0,0) means insert space in first column; (0, non-0) means
163  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
164  * (non-0, non-0) means two widgets in columns 2 and 3.
165 **/
166 #define SPACE_SIZE_X 15
167 #define SPACE_SIZE_Y 10
168 static inline void
169 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
171     for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
172         if (arr[i] && arr[i+1]) {
173             table.attach (const_cast<Gtk::Widget&>(*arr[i]),   1, 2, r, r+1,
174                           Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
175             table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
176                           Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
177         } else {
178             if (arr[i+1]) {
179                 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
180                               Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
181             } else if (arr[i]) {
182                 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
183                 label.set_alignment (0.0);
184                 table.attach (label, 0, 3, r, r+1,
185                               Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
186             } else {
187                 Gtk::HBox *space = manage (new Gtk::HBox);
188                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
189                 table.attach (*space, 0, 1, r, r+1,
190                               (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
191             }
192         }
193         ++r;
194     }
197 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
198     : CanvasGrid(nv, in_repr, in_doc, GRID_AXONOMETRIC), table(1, 1)
200     origin[NR::X] = origin[NR::Y] = 0.0;
201     color = DEFAULTGRIDCOLOR;
202     empcolor = DEFAULTGRIDEMPCOLOR;
203     empspacing = 5;
204     gridunit = &sp_unit_get_by_id(SP_UNIT_MM);
205     angle_deg[X] = angle_deg[Z] = 30;
206     angle_deg[Y] =0;
207     lengthy = sp_units_get_pixels(1.0, *(gridunit));
209     angle_rad[X] = deg_to_rad(angle_deg[X]);
210     tan_angle[X] = tan(angle_rad[X]);
211     angle_rad[Z] = deg_to_rad(angle_deg[Z]);
212     tan_angle[Z] = tan(angle_rad[Z]);
214     snapper = new CanvasAxonomGridSnapper(this, namedview, 0);
216     // initialize widgets:
217     vbox.set_border_width(2);
218     table.set_spacings(2);
219     vbox.pack_start(table, false, false, 0);
221     _rumg.init (_("Grid _units:"), "units", _wr, repr, doc);
222     _rsu_ox.init (_("_Origin X:"), _("X coordinate of grid origin"),
223                   "originx", _rumg, _wr, repr, doc);
224     _rsu_oy.init (_("O_rigin Y:"), _("Y coordinate of grid origin"),
225                   "originy", _rumg, _wr, repr, doc);
226     _rsu_sy.init (_("Spacing _Y:"), _("Base length of z-axis"),
227                   "spacingy", _rumg, _wr, repr, doc);
228     _rsu_ax.init (_("Angle X:"), _("Angle of x-axis"),
229                   "gridanglex", _rumg, _wr, repr, doc);
230     _rsu_az.init (_("Angle Z:"), _("Angle of z-axis"),
231                   "gridanglez", _rumg, _wr, repr, doc);
232     _rcp_gcol.init (_("Grid line _color:"), _("Grid line color"),
233                     _("Color of grid lines"), "color", "opacity", _wr, repr, doc);
234     _rcp_gmcol.init (_("Ma_jor grid line color:"), _("Major grid line color"),
235                      _("Color of the major (highlighted) grid lines"),
236                      "empcolor", "empopacity", _wr, repr, doc);
237     _rsi.init (_("_Major grid line every:"), _("lines"), "empspacing", _wr, repr, doc);
239     Gtk::Widget const *const widget_array[] = {
240         0,                  _rcbgrid._button,
241         _rumg._label,       _rumg._sel,
242         0,                  _rsu_ox.getSU(),
243         0,                  _rsu_oy.getSU(),
244         0,                  _rsu_sy.getSU(),
245         0,                  _rsu_ax.getSU(),
246         0,                  _rsu_az.getSU(),
247         _rcp_gcol._label,   _rcp_gcol._cp,
248         0,                  0,
249         _rcp_gmcol._label,  _rcp_gmcol._cp,
250         _rsi._label,        &_rsi._hbox,
251     };
253     attach_all (table, widget_array, sizeof(widget_array));
255     vbox.show();
257     if (repr) readRepr();
258     updateWidgets();
261 CanvasAxonomGrid::~CanvasAxonomGrid ()
263     if (snapper) delete snapper;
267 /* fixme: Collect all these length parsing methods and think common sane API */
269 static gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
271     if (!str) {
272         return FALSE;
273     }
275     gchar *u;
276     gdouble v = g_ascii_strtod(str, &u);
277     if (!u) {
278         return FALSE;
279     }
280     while (isspace(*u)) {
281         u += 1;
282     }
284     if (!*u) {
285         /* No unit specified - keep default */
286         *val = v;
287         return TRUE;
288     }
290     if (base & SP_UNIT_DEVICE) {
291         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
292             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
293             *val = v;
294             return TRUE;
295         }
296     }
298     if (base & SP_UNIT_ABSOLUTE) {
299         if (!strncmp(u, "pt", 2)) {
300             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
301         } else if (!strncmp(u, "mm", 2)) {
302             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
303         } else if (!strncmp(u, "cm", 2)) {
304             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
305         } else if (!strncmp(u, "m", 1)) {
306             *unit = &sp_unit_get_by_id(SP_UNIT_M);
307         } else if (!strncmp(u, "in", 2)) {
308             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
309         } else {
310             return FALSE;
311         }
312         *val = v;
313         return TRUE;
314     }
316     return FALSE;
319 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
321     if (!str) {
322         return FALSE;
323     }
325     gchar *u;
326     gdouble v = g_ascii_strtod(str, &u);
327     if (!u) {
328         return FALSE;
329     }
330     v = CLAMP(v, 0.0, 1.0);
332     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
334     return TRUE;
339 void
340 CanvasAxonomGrid::readRepr()
342     gchar const *value;
343     if ( (value = repr->attribute("originx")) ) {
344         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
345         origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
346     }
347     if ( (value = repr->attribute("originy")) ) {
348         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
349         origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
350     }
352     if ( (value = repr->attribute("spacingy")) ) {
353         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
354         lengthy = sp_units_get_pixels(lengthy, *(gridunit));
355         if (lengthy < 1.0) lengthy = 1.0;
356     }
358     if ( (value = repr->attribute("gridanglex")) ) {
359         angle_deg[X] = g_ascii_strtod(value, NULL);
360         if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
361         if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
362         angle_rad[X] = deg_to_rad(angle_deg[X]);
363         tan_angle[X] = tan(angle_rad[X]);
364     }
366     if ( (value = repr->attribute("gridanglez")) ) {
367         angle_deg[Z] = g_ascii_strtod(value, NULL);
368         if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
369         if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
370         angle_rad[Z] = deg_to_rad(angle_deg[Z]);
371         tan_angle[Z] = tan(angle_rad[Z]);
372     }
374     if ( (value = repr->attribute("color")) ) {
375         color = (color & 0xff) | sp_svg_read_color(value, color);
376     }
378     if ( (value = repr->attribute("empcolor")) ) {
379         empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
380     }
382     if ( (value = repr->attribute("opacity")) ) {
383         sp_nv_read_opacity(value, &color);
384     }
385     if ( (value = repr->attribute("empopacity")) ) {
386         sp_nv_read_opacity(value, &empcolor);
387     }
389     if ( (value = repr->attribute("empspacing")) ) {
390         empspacing = atoi(value);
391     }
393     if ( (value = repr->attribute("visible")) ) {
394         visible = (strcmp(value,"true") == 0);
395     }
397     if ( (value = repr->attribute("snap_enabled")) ) {
398         snap_enabled = (strcmp(value,"true") == 0);
399     }
401     for (GSList *l = canvasitems; l != NULL; l = l->next) {
402         sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
403     }
404     return;
407 /**
408  * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
409  */
410 void
411 CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
413     readRepr();
415     if ( ! (_wr.isUpdating()) )
416         updateWidgets();
422 Gtk::Widget &
423 CanvasAxonomGrid::getWidget()
425     return vbox;
429 /**
430  * Update dialog widgets from object's values.
431  */
432 void
433 CanvasAxonomGrid::updateWidgets()
435     if (_wr.isUpdating()) return;
437     _wr.setUpdating (true);
439     _rcb_visible.setActive(visible);
440     _rcb_snap_enabled.setActive(snap_enabled);
442     _rumg.setUnit (gridunit);
444     gdouble val;
445     val = origin[NR::X];
446     val = sp_pixels_get_units (val, *(gridunit));
447     _rsu_ox.setValue (val);
448     val = origin[NR::Y];
449     val = sp_pixels_get_units (val, *(gridunit));
450     _rsu_oy.setValue (val);
451     val = lengthy;
452     double gridy = sp_pixels_get_units (val, *(gridunit));
453     _rsu_sy.setValue (gridy);
455     _rsu_ax.setValue(angle_deg[X]);
456     _rsu_az.setValue(angle_deg[Z]);
458     _rcp_gcol.setRgba32 (color);
459     _rcp_gmcol.setRgba32 (empcolor);
460     _rsi.setValue (empspacing);
462     _wr.setUpdating (false);
464     return;
469 void
470 CanvasAxonomGrid::Update (NR::Matrix const &affine, unsigned int /*flags*/)
472     ow = origin * affine;
473     sw = NR::Point(fabs(affine[0]),fabs(affine[3]));
475     for(int dim = 0; dim < 2; dim++) {
476         gint scaling_factor = empspacing;
478         if (scaling_factor <= 1)
479             scaling_factor = 5;
481         scaled = FALSE;
482         int watchdog = 0;
483         while (  (sw[dim] < 8.0) & (watchdog < 100) ) {
484             scaled = TRUE;
485             sw[dim] *= scaling_factor;
486             // First pass, go up to the major line spacing, then
487             // keep increasing by two.
488             scaling_factor = 2;
489             watchdog++;
490         }
492     }
494     spacing_ylines = sw[NR::X] * lengthy  /(tan_angle[X] + tan_angle[Z]);
495     lyw            = sw[NR::Y] * lengthy;
496     lxw_x          = (lengthy / tan_angle[X]) * sw[NR::X];
497     lxw_z          = (lengthy / tan_angle[Z]) * sw[NR::X];
499     if (empspacing == 0) {
500         scaled = TRUE;
501     }
505 void
506 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
508     // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
509     // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
510     // bc = buffer patch coordinates
512     // tl = topleft ; br = bottomright
513     NR::Point buf_tl_gc;
514     NR::Point buf_br_gc;
515     buf_tl_gc[NR::X] = buf->rect.x0 - ow[NR::X];
516     buf_tl_gc[NR::Y] = buf->rect.y0 - ow[NR::Y];
517     buf_br_gc[NR::X] = buf->rect.x1 - ow[NR::X];
518     buf_br_gc[NR::Y] = buf->rect.y1 - ow[NR::Y];
520     gdouble x;
521     gdouble y;
523     // render the three separate line groups representing the main-axes
525     // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
526     gdouble const xintercept_y_bc = (buf_tl_gc[NR::X] * tan_angle[X]) - buf_tl_gc[NR::Y] ;
527     gdouble const xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
528     gint const xlinestart = (gint) Inkscape::round( (xstart_y_sc - buf->rect.x0*tan_angle[X] -ow[NR::Y]) / lyw );
529     gint xlinenum = xlinestart;
530     // lines starting on left side.
531     for (y = xstart_y_sc; y < buf->rect.y1; y += lyw, xlinenum++) {
532         gint const x0 = buf->rect.x0;
533         gint const y0 = (gint) Inkscape::round(y);
534         gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
535         gint const y1 = buf->rect.y1;
537         if (!scaled && (xlinenum % empspacing) == 0) {
538             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
539         } else {
540             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
541         }
542     }
543     // lines starting from top side
544     gdouble const xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
545     xlinenum = xlinestart-1;
546     for (x = xstart_x_sc; x < buf->rect.x1; x += lxw_x, xlinenum--) {
547         gint const y0 = buf->rect.y0;
548         gint const y1 = buf->rect.y1;
549         gint const x0 = (gint) Inkscape::round(x);
550         gint const x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
552         if (!scaled && (xlinenum % empspacing) == 0) {
553             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
554         } else {
555             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
556         }
557     }
559     // y-axis lines (vertical)
560     gdouble const ystart_x_sc = floor (buf_tl_gc[NR::X] / spacing_ylines) * spacing_ylines + ow[NR::X];
561     gint const  ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[NR::X]) / spacing_ylines);
562     gint ylinenum = ylinestart;
563     for (x = ystart_x_sc; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
564         gint const x0 = (gint) Inkscape::round(x);
566         if (!scaled && (ylinenum % empspacing) == 0) {
567             sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, empcolor);
568         } else {
569             sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
570         }
571     }
573     // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
574     gdouble const zintercept_y_bc = (buf_tl_gc[NR::X] * -tan_angle[Z]) - buf_tl_gc[NR::Y] ;
575     gdouble const zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
576     gint const  zlinestart = (gint) Inkscape::round( (zstart_y_sc + buf->rect.x0*tan_angle[X] - ow[NR::Y]) / lyw );
577     gint zlinenum = zlinestart;
578     // lines starting from left side
579     for (y = zstart_y_sc; y < buf->rect.y1; y += lyw, zlinenum++) {
580         gint const x0 = buf->rect.x0;
581         gint const y0 = (gint) Inkscape::round(y);
582         gint const x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
583         gint const y1 = buf->rect.y0;
585         if (!scaled && (zlinenum % empspacing) == 0) {
586             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
587         } else {
588             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
589         }
590     }
591     // draw lines from bottom-up
592     gdouble const zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
593     for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum++) {
594         gint const y0 = buf->rect.y1;
595         gint const y1 = buf->rect.y0;
596         gint const x0 = (gint) Inkscape::round(x);
597         gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
599         if (!scaled && (zlinenum % empspacing) == 0) {
600             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, empcolor);
601         } else {
602             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
603         }
604     }
607 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
609     this->grid = grid;
612 LineSnapper::LineList
613 CanvasAxonomGridSnapper::_getSnapLines(NR::Point const &p) const
615     LineList s;
617     if ( grid == NULL ) {
618         return s;
619     }
621     /* This is to make sure we snap to only visible grid lines */
622     double scaled_spacing = grid->spacing_ylines; // this is spacing of visible lines if screen pixels
624     // convert screen pixels to px
625     // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
626     if (SP_ACTIVE_DESKTOP) {
627         scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
628     }
630     NR::Coord rounded;
631     rounded = Inkscape::Util::round_to_nearest_multiple_plus(p[0], scaled_spacing, grid->origin[0]);
632     s.push_back(std::make_pair(NR::Dim2(0), rounded));
633     rounded = Inkscape::Util::round_to_lower_multiple_plus(p[0], scaled_spacing, grid->origin[0]);
634     s.push_back(std::make_pair(NR::Dim2(0), rounded));
636     return s;
639 void CanvasAxonomGridSnapper::_addSnappedLine(SnappedConstraints &sc, NR::Point const snapped_point, NR::Coord const snapped_distance, NR::Point const normal_to_line, NR::Point const point_on_line) const 
641     SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, normal_to_line, point_on_line);
642     sc.grid_lines.push_back(dummy);
647 }; // namespace Inkscape
650 /*
651   Local Variables:
652   mode:c++
653   c-file-style:"stroustrup"
654   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
655   indent-tabs-mode:nil
656   fill-column:99
657   End:
658 */
659 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :