Code

update for 32 instead of 24 bpp canvas buf
[inkscape.git] / src / display / canvas-axonomgrid.cpp
1 #define CANVAS_AXONOMGRID_C
3 /*
4  * Copyright (C) 2006-2008 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   */
14  /*
15   * TODO:
16   * THIS FILE AND THE HEADER FILE NEED CLEANING UP. PLEASE DO NOT HESISTATE TO DO SO.
17   * For example: the line drawing code should not be here. There _must_ be a function somewhere else that can provide this functionality...
18   */
20 #include "sp-canvas-util.h"
21 #include "canvas-axonomgrid.h"
22 #include "util/mathfns.h" 
23 #include "2geom/geom.h"
24 #include "display-forward.h"
25 #include <libnr/nr-pixops.h>
27 #include "canvas-grid.h"
28 #include "desktop-handles.h"
29 #include "helper/units.h"
30 #include "svg/svg-color.h"
31 #include "xml/node-event-vector.h"
32 #include "sp-object.h"
34 #include "sp-namedview.h"
35 #include "inkscape.h"
36 #include "desktop.h"
38 #include "document.h"
39 #include "prefs-utils.h"
41 #define SAFE_SETPIXEL   //undefine this when it is certain that setpixel is never called with invalid params
43 enum Dim3 { X=0, Y, Z };
45 #ifndef M_PI
46 # define M_PI 3.14159265358979323846
47 #endif
49 static double deg_to_rad(double deg) { return deg*M_PI/180.0;}
52 /**
53     \brief  This function renders a pixel on a particular buffer.
55     The topleft of the buffer equals
56                         ( rect.x0 , rect.y0 )  in screen coordinates
57                         ( 0 , 0 )  in setpixel coordinates
58     The bottomright of the buffer equals
59                         ( rect.x1 , rect,y1 )  in screen coordinates
60                         ( rect.x1 - rect.x0 , rect.y1 - rect.y0 )  in setpixel coordinates
61 */
62 static void
63 sp_caxonomgrid_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
64 {
65 #ifdef SAFE_SETPIXEL
66     if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
67 #endif
68         guint r, g, b, a;
69         r = NR_RGBA32_R (rgba);
70         g = NR_RGBA32_G (rgba);
71         b = NR_RGBA32_B (rgba);
72         a = NR_RGBA32_A (rgba);
73         guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
74         p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
75         p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
76         p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
77 #ifdef SAFE_SETPIXEL
78     }
79 #endif
80 }
82 /**
83     \brief  This function renders a line on a particular canvas buffer,
84             using Bresenham's line drawing function.
85             http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
86             Coordinates are interpreted as SCREENcoordinates
87 */
88 static void
89 sp_caxonomgrid_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
90 {
91     int dy = y1 - y0;
92     int dx = x1 - x0;
93     int stepx, stepy;
95     if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
96     if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
97     dy <<= 1;                                                  // dy is now 2*dy
98     dx <<= 1;                                                  // dx is now 2*dx
100     sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
101     if (dx > dy) {
102         int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
103         while (x0 != x1) {
104             if (fraction >= 0) {
105                 y0 += stepy;
106                 fraction -= dx;                                // same as fraction -= 2*dx
107             }
108             x0 += stepx;
109             fraction += dy;                                    // same as fraction -= 2*dy
110             sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
111         }
112     } else {
113         int fraction = dx - (dy >> 1);
114         while (y0 != y1) {
115             if (fraction >= 0) {
116                 x0 += stepx;
117                 fraction -= dy;
118             }
119             y0 += stepy;
120             fraction += dx;
121             sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
122         }
123     }
127 static void
128 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
130     if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
131         guint r, g, b, a;
132         gint y0, y1, y;
133         guchar *p;
134         r = NR_RGBA32_R(rgba);
135         g = NR_RGBA32_G (rgba);
136         b = NR_RGBA32_B (rgba);
137         a = NR_RGBA32_A (rgba);
138         y0 = MAX (buf->rect.y0, ys);
139         y1 = MIN (buf->rect.y1, ye + 1);
140         p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
141         for (y = y0; y < y1; y++) {
142             p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
143             p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
144             p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
145             p += buf->buf_rowstride;
146         }
147     }
150 namespace Inkscape {
153 /**
154 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP  TO QUICKLY GET RESULTS
156  * Helper function that attachs widgets in a 3xn table. The widgets come in an
157  * array that has two entries per table row. The two entries code for four
158  * possible cases: (0,0) means insert space in first column; (0, non-0) means
159  * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
160  * (non-0, non-0) means two widgets in columns 2 and 3.
161 **/
162 #define SPACE_SIZE_X 15
163 #define SPACE_SIZE_Y 10
164 static inline void
165 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
167     for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
168         if (arr[i] && arr[i+1]) {
169             table.attach (const_cast<Gtk::Widget&>(*arr[i]),   1, 2, r, r+1,
170                           Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
171             table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
172                           Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
173         } else {
174             if (arr[i+1]) {
175                 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
176                               Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
177             } else if (arr[i]) {
178                 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
179                 label.set_alignment (0.0);
180                 table.attach (label, 0, 3, r, r+1,
181                               Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
182             } else {
183                 Gtk::HBox *space = manage (new Gtk::HBox);
184                 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
185                 table.attach (*space, 0, 1, r, r+1,
186                               (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
187             }
188         }
189         ++r;
190     }
193 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
194     : CanvasGrid(nv, in_repr, in_doc, GRID_AXONOMETRIC)
196     gridunit = sp_unit_get_by_abbreviation( prefs_get_string_attribute("options.grids.axonom", "units") );
197     if (!gridunit)
198         gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
199     origin[NR::X] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.axonom", "origin_x", 0.0), *(gridunit) );
200     origin[NR::Y] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.axonom", "origin_y", 0.0), *(gridunit) );
201     color = prefs_get_int_attribute("options.grids.axonom", "color", 0x0000ff20);
202     empcolor = prefs_get_int_attribute("options.grids.axonom", "empcolor", 0x0000ff40);
203     empspacing = prefs_get_int_attribute("options.grids.axonom", "empspacing", 5);
204     lengthy = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.axonom", "spacing_y", 1.0), *(gridunit) );
205     angle_deg[X] = prefs_get_double_attribute ("options.grids.axonom", "angle_x", 30.0);
206     angle_deg[Z] = prefs_get_double_attribute ("options.grids.axonom", "angle_z", 30.0);
207     angle_deg[Y] = 0;
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     if (repr) readRepr();
219 CanvasAxonomGrid::~CanvasAxonomGrid ()
221     if (snapper) delete snapper;
225 /* fixme: Collect all these length parsing methods and think common sane API */
227 static gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
229     if (!str) {
230         return FALSE;
231     }
233     gchar *u;
234     gdouble v = g_ascii_strtod(str, &u);
235     if (!u) {
236         return FALSE;
237     }
238     while (isspace(*u)) {
239         u += 1;
240     }
242     if (!*u) {
243         /* No unit specified - keep default */
244         *val = v;
245         return TRUE;
246     }
248     if (base & SP_UNIT_DEVICE) {
249         if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
250             *unit = &sp_unit_get_by_id(SP_UNIT_PX);
251             *val = v;
252             return TRUE;
253         }
254     }
256     if (base & SP_UNIT_ABSOLUTE) {
257         if (!strncmp(u, "pt", 2)) {
258             *unit = &sp_unit_get_by_id(SP_UNIT_PT);
259         } else if (!strncmp(u, "mm", 2)) {
260             *unit = &sp_unit_get_by_id(SP_UNIT_MM);
261         } else if (!strncmp(u, "cm", 2)) {
262             *unit = &sp_unit_get_by_id(SP_UNIT_CM);
263         } else if (!strncmp(u, "m", 1)) {
264             *unit = &sp_unit_get_by_id(SP_UNIT_M);
265         } else if (!strncmp(u, "in", 2)) {
266             *unit = &sp_unit_get_by_id(SP_UNIT_IN);
267         } else {
268             return FALSE;
269         }
270         *val = v;
271         return TRUE;
272     }
274     return FALSE;
277 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
279     if (!str) {
280         return FALSE;
281     }
283     gchar *u;
284     gdouble v = g_ascii_strtod(str, &u);
285     if (!u) {
286         return FALSE;
287     }
288     v = CLAMP(v, 0.0, 1.0);
290     *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
292     return TRUE;
297 void
298 CanvasAxonomGrid::readRepr()
300     gchar const *value;
301     if ( (value = repr->attribute("originx")) ) {
302         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::X], &gridunit);
303         origin[NR::X] = sp_units_get_pixels(origin[NR::X], *(gridunit));
304     }
305     if ( (value = repr->attribute("originy")) ) {
306         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[NR::Y], &gridunit);
307         origin[NR::Y] = sp_units_get_pixels(origin[NR::Y], *(gridunit));
308     }
310     if ( (value = repr->attribute("spacingy")) ) {
311         sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
312         lengthy = sp_units_get_pixels(lengthy, *(gridunit));
313         if (lengthy < 1.0) lengthy = 1.0;
314     }
316     if ( (value = repr->attribute("gridanglex")) ) {
317         angle_deg[X] = g_ascii_strtod(value, NULL);
318         if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
319         if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
320         angle_rad[X] = deg_to_rad(angle_deg[X]);
321         tan_angle[X] = tan(angle_rad[X]);
322     }
324     if ( (value = repr->attribute("gridanglez")) ) {
325         angle_deg[Z] = g_ascii_strtod(value, NULL);
326         if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
327         if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
328         angle_rad[Z] = deg_to_rad(angle_deg[Z]);
329         tan_angle[Z] = tan(angle_rad[Z]);
330     }
332     if ( (value = repr->attribute("color")) ) {
333         color = (color & 0xff) | sp_svg_read_color(value, color);
334     }
336     if ( (value = repr->attribute("empcolor")) ) {
337         empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
338     }
340     if ( (value = repr->attribute("opacity")) ) {
341         sp_nv_read_opacity(value, &color);
342     }
343     if ( (value = repr->attribute("empopacity")) ) {
344         sp_nv_read_opacity(value, &empcolor);
345     }
347     if ( (value = repr->attribute("empspacing")) ) {
348         empspacing = atoi(value);
349     }
351     if ( (value = repr->attribute("visible")) ) {
352         visible = (strcmp(value,"true") == 0);
353     }
355     if ( (value = repr->attribute("enabled")) ) {
356         g_assert(snapper != NULL);
357         snapper->setEnabled(strcmp(value,"true") == 0);
358     }
360     for (GSList *l = canvasitems; l != NULL; l = l->next) {
361         sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
362     }
363     return;
366 /**
367  * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
368  */
369 void
370 CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
372     readRepr();
374     if ( ! (_wr.isUpdating()) )
375         updateWidgets();
381 Gtk::Widget *
382 CanvasAxonomGrid::newSpecificWidget()
384     Gtk::Table * table = Gtk::manage( new Gtk::Table(1,1) );
385     table->set_spacings(2);
387 _wr.setUpdating (true);
389     Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu(
390             _("Grid _units:"), "units", _wr, repr, doc) );
391     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
392             _("_Origin X:"), _("X coordinate of grid origin"), "originx", *_rumg, _wr, repr, doc) );
393     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
394             _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", *_rumg, _wr, repr, doc) );
395     Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
396             _("Spacing _Y:"), _("Base length of z-axis"), "spacingy", *_rumg, _wr, repr, doc) );
397     Inkscape::UI::Widget::RegisteredScalar *_rsu_ax = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
398             _("Angle X:"), _("Angle of x-axis"), "gridanglex", _wr, repr, doc ) );
399     Inkscape::UI::Widget::RegisteredScalar *_rsu_az = Gtk::manage(  new Inkscape::UI::Widget::RegisteredScalar(
400             _("Angle Z:"), _("Angle of z-axis"), "gridanglez", _wr, repr, doc ) );
402     Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gcol = Gtk::manage(
403         new Inkscape::UI::Widget::RegisteredColorPicker(
404             _("Grid line _color:"), _("Grid line color"), _("Color of grid lines"), 
405             "color", "opacity", _wr, repr, doc));
407     Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gmcol = Gtk::manage(
408         new Inkscape::UI::Widget::RegisteredColorPicker(
409             _("Ma_jor grid line color:"), _("Major grid line color"), 
410             _("Color of the major (highlighted) grid lines"),
411             "empcolor", "empopacity", _wr, repr, doc));
413     Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger(
414             _("_Major grid line every:"), "", _("lines"), "empspacing", _wr, repr, doc ) );
416     _rsu_ox->setDigits(4);
417     _rsu_ox->setIncrements(0.1, 1.0);
419     _rsu_oy->setDigits(4);
420     _rsu_oy->setIncrements(0.1, 1.0);
422     _rsu_sy->setDigits(4);
423     _rsu_sy->setIncrements(0.1, 1.0);
425 _wr.setUpdating (false);
427     Gtk::Widget const *const widget_array[] = {
428         0,       _rumg,
429         0,                  _rsu_ox,
430         0,                  _rsu_oy,
431         0,                  _rsu_sy,
432         0,                  _rsu_ax,
433         0,                  _rsu_az,
434         _rcp_gcol->_label,   _rcp_gcol,
435         0,                  0,
436         _rcp_gmcol->_label,  _rcp_gmcol,
437         0,                   _rsi,
438     };
440     attach_all (*table, widget_array, sizeof(widget_array));
442     // set widget values
443     _rumg->setUnit (gridunit);
445     gdouble val;
446     val = origin[NR::X];
447     val = sp_pixels_get_units (val, *(gridunit));
448     _rsu_ox->setValue (val);
449     val = origin[NR::Y];
450     val = sp_pixels_get_units (val, *(gridunit));
451     _rsu_oy->setValue (val);
452     val = lengthy;
453     double gridy = sp_pixels_get_units (val, *(gridunit));
454     _rsu_sy->setValue (gridy);
456     _rsu_ax->setValue(angle_deg[X]);
457     _rsu_az->setValue(angle_deg[Z]);
459     _rcp_gcol->setRgba32 (color);
460     _rcp_gmcol->setRgba32 (empcolor);
461     _rsi->setValue (empspacing);
463     return table;
467 /**
468  * Update dialog widgets from object's values.
469  */
470 void
471 CanvasAxonomGrid::updateWidgets()
473 /*    if (_wr.isUpdating()) return;
475     _wr.setUpdating (true);
477     _rcb_visible.setActive(visible);
478     if (snapper != NULL) {
479         _rcb_enabled.setActive(snapper->getEnabled());
480     }
482     _rumg.setUnit (gridunit);
484     gdouble val;
485     val = origin[NR::X];
486     val = sp_pixels_get_units (val, *(gridunit));
487     _rsu_ox.setValue (val);
488     val = origin[NR::Y];
489     val = sp_pixels_get_units (val, *(gridunit));
490     _rsu_oy.setValue (val);
491     val = lengthy;
492     double gridy = sp_pixels_get_units (val, *(gridunit));
493     _rsu_sy.setValue (gridy);
495     _rsu_ax.setValue(angle_deg[X]);
496     _rsu_az.setValue(angle_deg[Z]);
498     _rcp_gcol.setRgba32 (color);
499     _rcp_gmcol.setRgba32 (empcolor);
500     _rsi.setValue (empspacing);
502     _wr.setUpdating (false);
504     return;
505     */
510 void
511 CanvasAxonomGrid::Update (NR::Matrix const &affine, unsigned int /*flags*/)
513     ow = origin * affine;
514     sw = NR::Point(fabs(affine[0]),fabs(affine[3]));
516     for(int dim = 0; dim < 2; dim++) {
517         gint scaling_factor = empspacing;
519         if (scaling_factor <= 1)
520             scaling_factor = 5;
522         scaled = FALSE;
523         int watchdog = 0;
524         while (  (sw[dim] < 8.0) & (watchdog < 100) ) {
525             scaled = TRUE;
526             sw[dim] *= scaling_factor;
527             // First pass, go up to the major line spacing, then
528             // keep increasing by two.
529             scaling_factor = 2;
530             watchdog++;
531         }
533     }
535     spacing_ylines = sw[NR::X] * lengthy  /(tan_angle[X] + tan_angle[Z]);
536     lyw            = sw[NR::Y] * lengthy;
537     lxw_x          = (lengthy / tan_angle[X]) * sw[NR::X];
538     lxw_z          = (lengthy / tan_angle[Z]) * sw[NR::X];
540     if (empspacing == 0) {
541         scaled = TRUE;
542     }
546 void
547 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
549     //set correct coloring, depending preference (when zoomed out, always major coloring or minor coloring)
550     guint32 _empcolor;
551     bool preference = prefs_get_int_attribute ("options.grids", "no_emphasize_when_zoomedout", 0) == 1;
552     if( scaled && preference ) {
553         _empcolor = color;
554     } else {
555         _empcolor = empcolor;
556     }
558     // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
559     // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
560     // bc = buffer patch coordinates
562     // tl = topleft ; br = bottomright
563     NR::Point buf_tl_gc;
564     NR::Point buf_br_gc;
565     buf_tl_gc[NR::X] = buf->rect.x0 - ow[NR::X];
566     buf_tl_gc[NR::Y] = buf->rect.y0 - ow[NR::Y];
567     buf_br_gc[NR::X] = buf->rect.x1 - ow[NR::X];
568     buf_br_gc[NR::Y] = buf->rect.y1 - ow[NR::Y];
570     gdouble x;
571     gdouble y;
573     // render the three separate line groups representing the main-axes
575     // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
576     gdouble const xintercept_y_bc = (buf_tl_gc[NR::X] * tan_angle[X]) - buf_tl_gc[NR::Y] ;
577     gdouble const xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
578     gint const xlinestart = (gint) Inkscape::round( (xstart_y_sc - buf->rect.x0*tan_angle[X] -ow[NR::Y]) / lyw );
579     gint xlinenum = xlinestart;
580     // lines starting on left side.
581     for (y = xstart_y_sc; y < buf->rect.y1; y += lyw, xlinenum++) {
582         gint const x0 = buf->rect.x0;
583         gint const y0 = (gint) Inkscape::round(y);
584         gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
585         gint const y1 = buf->rect.y1;
587         if (!scaled && (xlinenum % empspacing) != 0) {
588             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
589         } else {
590             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
591         }
592     }
593     // lines starting from top side
594     gdouble const xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
595     xlinenum = xlinestart-1;
596     for (x = xstart_x_sc; x < buf->rect.x1; x += lxw_x, xlinenum--) {
597         gint const y0 = buf->rect.y0;
598         gint const y1 = buf->rect.y1;
599         gint const x0 = (gint) Inkscape::round(x);
600         gint const x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
602         if (!scaled && (xlinenum % empspacing) != 0) {
603             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
604         } else {
605             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
606         }
607     }
609     // y-axis lines (vertical)
610     gdouble const ystart_x_sc = floor (buf_tl_gc[NR::X] / spacing_ylines) * spacing_ylines + ow[NR::X];
611     gint const  ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[NR::X]) / spacing_ylines);
612     gint ylinenum = ylinestart;
613     for (x = ystart_x_sc; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
614         gint const x0 = (gint) Inkscape::round(x);
616         if (!scaled && (ylinenum % empspacing) != 0) {
617             sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
618         } else {
619             sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, _empcolor);
620         }
621     }
623     // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
624     gdouble const zintercept_y_bc = (buf_tl_gc[NR::X] * -tan_angle[Z]) - buf_tl_gc[NR::Y] ;
625     gdouble const zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
626     gint const  zlinestart = (gint) Inkscape::round( (zstart_y_sc + buf->rect.x0*tan_angle[Z] - ow[NR::Y]) / lyw );
627     gint zlinenum = zlinestart;
628     // lines starting from left side
629     for (y = zstart_y_sc; y < buf->rect.y1; y += lyw, zlinenum++) {
630         gint const x0 = buf->rect.x0;
631         gint const y0 = (gint) Inkscape::round(y);
632         gint const x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
633         gint const y1 = buf->rect.y0;
635         if (!scaled && (zlinenum % empspacing) != 0) {
636             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
637         } else {
638             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
639         }
640     }
641     // draw lines from bottom-up
642     gdouble const zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
643     for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum++) {
644         gint const y0 = buf->rect.y1;
645         gint const y1 = buf->rect.y0;
646         gint const x0 = (gint) Inkscape::round(x);
647         gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
649         if (!scaled && (zlinenum % empspacing) != 0) {
650             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
651         } else {
652             sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
653         }
654     }
657 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
659     this->grid = grid;
662 LineSnapper::LineList
663 CanvasAxonomGridSnapper::_getSnapLines(NR::Point const &p) const
665     LineList s;
667     if ( grid == NULL ) {
668         return s;
669     }
671     /* This is to make sure we snap to only visible grid lines */
672     double scaled_spacing_h = grid->spacing_ylines; // this is spacing of visible lines if screen pixels
673     double scaled_spacing_v = grid->lyw; // vertical
675     // convert screen pixels to px
676     // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
677     if (SP_ACTIVE_DESKTOP) {
678         scaled_spacing_h /= SP_ACTIVE_DESKTOP->current_zoom();
679         scaled_spacing_v /= SP_ACTIVE_DESKTOP->current_zoom();
680     }
682     // In an axonometric grid, any point will be surrounded by 6 grid lines:
683     // - 2 vertical grid lines, one left and one right from the point
684     // - 2 angled z grid lines, one above and one below the point
685     // - 2 angled x grid lines, one above and one below the point
686     
687     // Calculate the x coordinate of the vertical grid lines
688     NR::Coord x_max = Inkscape::Util::round_to_upper_multiple_plus(p[NR::X], scaled_spacing_h, grid->origin[NR::X]);
689     NR::Coord x_min = Inkscape::Util::round_to_lower_multiple_plus(p[NR::X], scaled_spacing_h, grid->origin[NR::X]);
690     
691     // Calculate the y coordinate of the intersection of the angled grid lines with the y-axis
692     double y_proj_along_z = p[NR::Y] - grid->tan_angle[Z]*(p[NR::X] - grid->origin[NR::X]);  
693     double y_proj_along_x = p[NR::Y] + grid->tan_angle[X]*(p[NR::X] - grid->origin[NR::X]);    
694     double y_proj_along_z_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_z, scaled_spacing_v, grid->origin[NR::Y]);
695     double y_proj_along_z_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_z, scaled_spacing_v, grid->origin[NR::Y]);    
696     double y_proj_along_x_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_x, scaled_spacing_v, grid->origin[NR::Y]);
697     double y_proj_along_x_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_x, scaled_spacing_v, grid->origin[NR::Y]);
698     
699     // Calculate the normal for the angled grid lines
700     NR::Point norm_x = NR::rot90(NR::Point(1, -grid->tan_angle[X]));
701     NR::Point norm_z = NR::rot90(NR::Point(1, grid->tan_angle[Z]));
702     
703     // The four angled grid lines form a parallellogram, enclosing the point
704     // One of the two vertical grid lines divides this parallellogram in two triangles
705     // We will now try to find out in which half (i.e. triangle) our point is, and return 
706     // only the three grid lines defining that triangle 
707     
708     // The vertical grid line is at the intersection of two angled grid lines. 
709     // Now go find that intersection!
710     Geom::Point result;
711     Geom::IntersectorKind is = line_intersection(norm_x.to_2geom(), norm_x[NR::Y]*y_proj_along_x_max,
712                                            norm_z.to_2geom(), norm_z[NR::Y]*y_proj_along_z_max,
713                                            result);
714                          
715     // Determine which half of the parallellogram to use 
716     bool use_left_half = true;
717     bool use_right_half = true;
718     
719     if (is == Geom::intersects) {
720         use_left_half = (p[NR::X] - grid->origin[NR::X]) < result[Geom::X];
721         use_right_half = !use_left_half; 
722     }
723     
724     //std::cout << "intersection at " << result << " leads to use_left_half = " << use_left_half << " and use_right_half = " << use_right_half << std::endl;
725        
726     // Return the three grid lines which define the triangle that encloses our point
727     // If we didn't find an intersection above, all 6 grid lines will be returned
728     if (use_left_half) {
729         s.push_back(std::make_pair(norm_z, NR::Point(grid->origin[NR::X], y_proj_along_z_max)));
730         s.push_back(std::make_pair(norm_x, NR::Point(grid->origin[NR::X], y_proj_along_x_min)));
731         s.push_back(std::make_pair(component_vectors[NR::X], NR::Point(x_max, 0)));            
732     }
733     
734     if (use_right_half) {
735         s.push_back(std::make_pair(norm_z, NR::Point(grid->origin[NR::X], y_proj_along_z_min)));
736         s.push_back(std::make_pair(norm_x, NR::Point(grid->origin[NR::X], y_proj_along_x_max)));
737         s.push_back(std::make_pair(component_vectors[NR::X], NR::Point(x_min, 0)));        
738     } 
739     
740     return s;
743 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 
745     SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(), normal_to_line, point_on_line);
746     sc.grid_lines.push_back(dummy);
751 }; // namespace Inkscape
754 /*
755   Local Variables:
756   mode:c++
757   c-file-style:"stroustrup"
758   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
759   indent-tabs-mode:nil
760   fill-column:99
761   End:
762 */
763 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :