1 /*
2 * Copyright (C) 2006-2008 Johan Engelen <johan@shouraizou.nl>
3 */
5 /*
6 * Current limits are: one axis (y-axis) is always vertical. The other two
7 * axes are bound to a certain range of angles. The z-axis always has an angle
8 * smaller than 90 degrees (measured from horizontal, 0 degrees being a line extending
9 * to the right). The x-axis will always have an angle between 0 and 90 degrees.
10 */
12 /*
13 * TODO:
14 * THIS FILE AND THE HEADER FILE NEED CLEANING UP. PLEASE DO NOT HESISTATE TO DO SO.
15 * For example: the line drawing code should not be here. There _must_ be a function somewhere else that can provide this functionality...
16 */
18 #include "sp-canvas-util.h"
19 #include "canvas-axonomgrid.h"
20 #include "util/mathfns.h"
21 #include "2geom/line.h"
22 #include <libnr/nr-pixops.h>
24 #include "canvas-grid.h"
25 #include "desktop-handles.h"
26 #include "helper/units.h"
27 #include "svg/svg-color.h"
28 #include "xml/node-event-vector.h"
29 #include "sp-object.h"
31 #include "sp-namedview.h"
32 #include "inkscape.h"
33 #include "desktop.h"
35 #include "document.h"
36 #include "preferences.h"
38 #define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
40 enum Dim3 { X=0, Y, Z };
42 #ifndef M_PI
43 # define M_PI 3.14159265358979323846
44 #endif
46 static double deg_to_rad(double deg) { return deg*M_PI/180.0;}
49 /**
50 \brief This function renders a pixel on a particular buffer.
52 The topleft of the buffer equals
53 ( rect.x0 , rect.y0 ) in screen coordinates
54 ( 0 , 0 ) in setpixel coordinates
55 The bottomright of the buffer equals
56 ( rect.x1 , rect,y1 ) in screen coordinates
57 ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
58 */
59 static void
60 sp_caxonomgrid_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
61 {
62 #ifdef SAFE_SETPIXEL
63 if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
64 #endif
65 guint r, g, b, a;
66 r = NR_RGBA32_R (rgba);
67 g = NR_RGBA32_G (rgba);
68 b = NR_RGBA32_B (rgba);
69 a = NR_RGBA32_A (rgba);
70 guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
71 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
72 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
73 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
74 #ifdef SAFE_SETPIXEL
75 }
76 #endif
77 }
79 /**
80 \brief This function renders a line on a particular canvas buffer,
81 using Bresenham's line drawing function.
82 http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
83 Coordinates are interpreted as SCREENcoordinates
84 */
85 static void
86 sp_caxonomgrid_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
87 {
88 int dy = y1 - y0;
89 int dx = x1 - x0;
90 int stepx, stepy;
92 if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
93 if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
94 dy <<= 1; // dy is now 2*dy
95 dx <<= 1; // dx is now 2*dx
97 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
98 if (dx > dy) {
99 int fraction = dy - (dx >> 1); // same as 2*dy - dx
100 while (x0 != x1) {
101 if (fraction >= 0) {
102 y0 += stepy;
103 fraction -= dx; // same as fraction -= 2*dx
104 }
105 x0 += stepx;
106 fraction += dy; // same as fraction -= 2*dy
107 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
108 }
109 } else {
110 int fraction = dx - (dy >> 1);
111 while (y0 != y1) {
112 if (fraction >= 0) {
113 x0 += stepx;
114 fraction -= dy;
115 }
116 y0 += stepy;
117 fraction += dx;
118 sp_caxonomgrid_setpixel(buf, x0, y0, rgba);
119 }
120 }
122 }
124 static void
125 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
126 {
127 if ((x >= buf->rect.x0) && (x < buf->rect.x1)) {
128 guint r, g, b, a;
129 gint y0, y1, y;
130 guchar *p;
131 r = NR_RGBA32_R(rgba);
132 g = NR_RGBA32_G (rgba);
133 b = NR_RGBA32_B (rgba);
134 a = NR_RGBA32_A (rgba);
135 y0 = MAX (buf->rect.y0, ys);
136 y1 = MIN (buf->rect.y1, ye + 1);
137 p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
138 for (y = y0; y < y1; y++) {
139 p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
140 p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
141 p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
142 p += buf->buf_rowstride;
143 }
144 }
145 }
147 namespace Inkscape {
150 /**
151 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
152 *
153 * Helper function that attachs widgets in a 3xn table. The widgets come in an
154 * array that has two entries per table row. The two entries code for four
155 * possible cases: (0,0) means insert space in first column; (0, non-0) means
156 * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
157 * (non-0, non-0) means two widgets in columns 2 and 3.
158 **/
159 #define SPACE_SIZE_X 15
160 #define SPACE_SIZE_Y 10
161 static inline void
162 attach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
163 {
164 for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
165 if (arr[i] && arr[i+1]) {
166 table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
167 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
168 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
169 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
170 } else {
171 if (arr[i+1]) {
172 table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
173 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
174 } else if (arr[i]) {
175 Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
176 label.set_alignment (0.0);
177 table.attach (label, 0, 3, r, r+1,
178 Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
179 } else {
180 Gtk::HBox *space = manage (new Gtk::HBox);
181 space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
182 table.attach (*space, 0, 1, r, r+1,
183 (Gtk::AttachOptions)0, (Gtk::AttachOptions)0,0,0);
184 }
185 }
186 ++r;
187 }
188 }
190 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
191 : CanvasGrid(nv, in_repr, in_doc, GRID_AXONOMETRIC)
192 {
193 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
194 gridunit = sp_unit_get_by_abbreviation( prefs->getString("/options/grids/axonom/units").data() );
195 if (!gridunit)
196 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
197 origin[Geom::X] = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/origin_x", 0.0), *gridunit );
198 origin[Geom::Y] = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/origin_y", 0.0), *gridunit );
199 color = prefs->getInt("/options/grids/axonom/color", 0x0000ff20);
200 empcolor = prefs->getInt("/options/grids/axonom/empcolor", 0x0000ff40);
201 empspacing = prefs->getInt("/options/grids/axonom/empspacing", 5);
202 lengthy = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/spacing_y", 1.0), *gridunit );
203 angle_deg[X] = prefs->getDouble("/options/grids/axonom/angle_x", 30.0);
204 angle_deg[Z] = prefs->getDouble("/options/grids/axonom/angle_z", 30.0);
205 angle_deg[Y] = 0;
207 angle_rad[X] = deg_to_rad(angle_deg[X]);
208 tan_angle[X] = tan(angle_rad[X]);
209 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
210 tan_angle[Z] = tan(angle_rad[Z]);
212 snapper = new CanvasAxonomGridSnapper(this, &namedview->snap_manager, 0);
214 if (repr) readRepr();
215 }
217 CanvasAxonomGrid::~CanvasAxonomGrid ()
218 {
219 if (snapper) delete snapper;
220 }
223 /* fixme: Collect all these length parsing methods and think common sane API */
225 static gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
226 {
227 if (!str) {
228 return FALSE;
229 }
231 gchar *u;
232 gdouble v = g_ascii_strtod(str, &u);
233 if (!u) {
234 return FALSE;
235 }
236 while (isspace(*u)) {
237 u += 1;
238 }
240 if (!*u) {
241 /* No unit specified - keep default */
242 *val = v;
243 return TRUE;
244 }
246 if (base & SP_UNIT_DEVICE) {
247 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
248 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
249 *val = v;
250 return TRUE;
251 }
252 }
254 if (base & SP_UNIT_ABSOLUTE) {
255 if (!strncmp(u, "pt", 2)) {
256 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
257 } else if (!strncmp(u, "mm", 2)) {
258 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
259 } else if (!strncmp(u, "cm", 2)) {
260 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
261 } else if (!strncmp(u, "m", 1)) {
262 *unit = &sp_unit_get_by_id(SP_UNIT_M);
263 } else if (!strncmp(u, "in", 2)) {
264 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
265 } else {
266 return FALSE;
267 }
268 *val = v;
269 return TRUE;
270 }
272 return FALSE;
273 }
275 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
276 {
277 if (!str) {
278 return FALSE;
279 }
281 gchar *u;
282 gdouble v = g_ascii_strtod(str, &u);
283 if (!u) {
284 return FALSE;
285 }
286 v = CLAMP(v, 0.0, 1.0);
288 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
290 return TRUE;
291 }
295 void
296 CanvasAxonomGrid::readRepr()
297 {
298 gchar const *value;
299 if ( (value = repr->attribute("originx")) ) {
300 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[Geom::X], &gridunit);
301 origin[Geom::X] = sp_units_get_pixels(origin[Geom::X], *(gridunit));
302 }
303 if ( (value = repr->attribute("originy")) ) {
304 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[Geom::Y], &gridunit);
305 origin[Geom::Y] = sp_units_get_pixels(origin[Geom::Y], *(gridunit));
306 }
308 if ( (value = repr->attribute("spacingy")) ) {
309 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
310 lengthy = sp_units_get_pixels(lengthy, *(gridunit));
311 if (lengthy < 0.0500) lengthy = 0.0500;
312 }
314 if ( (value = repr->attribute("gridanglex")) ) {
315 angle_deg[X] = g_ascii_strtod(value, NULL);
316 if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
317 if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
318 angle_rad[X] = deg_to_rad(angle_deg[X]);
319 tan_angle[X] = tan(angle_rad[X]);
320 }
322 if ( (value = repr->attribute("gridanglez")) ) {
323 angle_deg[Z] = g_ascii_strtod(value, NULL);
324 if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
325 if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
326 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
327 tan_angle[Z] = tan(angle_rad[Z]);
328 }
330 if ( (value = repr->attribute("color")) ) {
331 color = (color & 0xff) | sp_svg_read_color(value, color);
332 }
334 if ( (value = repr->attribute("empcolor")) ) {
335 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
336 }
338 if ( (value = repr->attribute("opacity")) ) {
339 sp_nv_read_opacity(value, &color);
340 }
341 if ( (value = repr->attribute("empopacity")) ) {
342 sp_nv_read_opacity(value, &empcolor);
343 }
345 if ( (value = repr->attribute("empspacing")) ) {
346 empspacing = atoi(value);
347 }
349 if ( (value = repr->attribute("visible")) ) {
350 visible = (strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
351 }
353 if ( (value = repr->attribute("enabled")) ) {
354 g_assert(snapper != NULL);
355 snapper->setEnabled(strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
356 }
358 if ( (value = repr->attribute("snapvisiblegridlinesonly")) ) {
359 g_assert(snapper != NULL);
360 snapper->setSnapVisibleOnly(strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
361 }
363 for (GSList *l = canvasitems; l != NULL; l = l->next) {
364 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
365 }
366 return;
367 }
369 /**
370 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
371 */
372 void
373 CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
374 {
375 readRepr();
377 if ( ! (_wr.isUpdating()) )
378 updateWidgets();
379 }
384 Gtk::Widget *
385 CanvasAxonomGrid::newSpecificWidget()
386 {
387 Gtk::Table * table = Gtk::manage( new Gtk::Table(1,1) );
388 table->set_spacings(2);
390 _wr.setUpdating (true);
392 Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu(
393 _("Grid _units:"), "units", _wr, repr, doc) );
394 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
395 _("_Origin X:"), _("X coordinate of grid origin"), "originx", *_rumg, _wr, repr, doc) );
396 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
397 _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", *_rumg, _wr, repr, doc) );
398 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
399 _("Spacing _Y:"), _("Base length of z-axis"), "spacingy", *_rumg, _wr, repr, doc) );
400 Inkscape::UI::Widget::RegisteredScalar *_rsu_ax = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
401 _("Angle X:"), _("Angle of x-axis"), "gridanglex", _wr, repr, doc ) );
402 Inkscape::UI::Widget::RegisteredScalar *_rsu_az = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
403 _("Angle Z:"), _("Angle of z-axis"), "gridanglez", _wr, repr, doc ) );
405 Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gcol = Gtk::manage(
406 new Inkscape::UI::Widget::RegisteredColorPicker(
407 _("Grid line _color:"), _("Grid line color"), _("Color of grid lines"),
408 "color", "opacity", _wr, repr, doc));
410 Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gmcol = Gtk::manage(
411 new Inkscape::UI::Widget::RegisteredColorPicker(
412 _("Ma_jor grid line color:"), _("Major grid line color"),
413 _("Color of the major (highlighted) grid lines"),
414 "empcolor", "empopacity", _wr, repr, doc));
416 Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger(
417 _("_Major grid line every:"), "", _("lines"), "empspacing", _wr, repr, doc ) );
419 _rsu_ox->setDigits(4);
420 _rsu_ox->setIncrements(0.1, 1.0);
422 _rsu_oy->setDigits(4);
423 _rsu_oy->setIncrements(0.1, 1.0);
425 _rsu_sy->setDigits(4);
426 _rsu_sy->setIncrements(0.1, 1.0);
428 _wr.setUpdating (false);
430 Gtk::Widget const *const widget_array[] = {
431 0, _rumg,
432 0, _rsu_ox,
433 0, _rsu_oy,
434 0, _rsu_sy,
435 0, _rsu_ax,
436 0, _rsu_az,
437 _rcp_gcol->_label, _rcp_gcol,
438 0, 0,
439 _rcp_gmcol->_label, _rcp_gmcol,
440 0, _rsi,
441 };
443 attach_all (*table, widget_array, sizeof(widget_array));
445 // set widget values
446 _rumg->setUnit (gridunit);
448 gdouble val;
449 val = origin[Geom::X];
450 val = sp_pixels_get_units (val, *(gridunit));
451 _rsu_ox->setValue (val);
452 val = origin[Geom::Y];
453 val = sp_pixels_get_units (val, *(gridunit));
454 _rsu_oy->setValue (val);
455 val = lengthy;
456 double gridy = sp_pixels_get_units (val, *(gridunit));
457 _rsu_sy->setValue (gridy);
459 _rsu_ax->setValue(angle_deg[X]);
460 _rsu_az->setValue(angle_deg[Z]);
462 _rcp_gcol->setRgba32 (color);
463 _rcp_gmcol->setRgba32 (empcolor);
464 _rsi->setValue (empspacing);
466 return table;
467 }
470 /**
471 * Update dialog widgets from object's values.
472 */
473 void
474 CanvasAxonomGrid::updateWidgets()
475 {
476 /* if (_wr.isUpdating()) return;
478 _wr.setUpdating (true);
480 _rcb_visible.setActive(visible);
481 if (snapper != NULL) {
482 _rcb_enabled.setActive(snapper->getEnabled());
483 }
485 _rumg.setUnit (gridunit);
487 gdouble val;
488 val = origin[Geom::X];
489 val = sp_pixels_get_units (val, *(gridunit));
490 _rsu_ox.setValue (val);
491 val = origin[Geom::Y];
492 val = sp_pixels_get_units (val, *(gridunit));
493 _rsu_oy.setValue (val);
494 val = lengthy;
495 double gridy = sp_pixels_get_units (val, *(gridunit));
496 _rsu_sy.setValue (gridy);
498 _rsu_ax.setValue(angle_deg[X]);
499 _rsu_az.setValue(angle_deg[Z]);
501 _rcp_gcol.setRgba32 (color);
502 _rcp_gmcol.setRgba32 (empcolor);
503 _rsi.setValue (empspacing);
505 _wr.setUpdating (false);
507 return;
508 */
509 }
513 void
514 CanvasAxonomGrid::Update (Geom::Matrix const &affine, unsigned int /*flags*/)
515 {
516 ow = origin * affine;
517 sw = Geom::Point(fabs(affine[0]),fabs(affine[3]));
518 sw *= lengthy;
520 scaled = false;
522 for(int dim = 0; dim < 2; dim++) {
523 gint scaling_factor = empspacing;
525 if (scaling_factor <= 1)
526 scaling_factor = 5;
528 int watchdog = 0;
529 while ( (sw[dim] < 8.0) & (watchdog < 100) ) {
530 scaled = true;
531 sw[dim] *= scaling_factor;
532 // First pass, go up to the major line spacing, then
533 // keep increasing by two.
534 scaling_factor = 2;
535 watchdog++;
536 }
538 }
540 spacing_ylines = sw[Geom::X] /(tan_angle[X] + tan_angle[Z]);
541 lyw = sw[Geom::Y];
542 lxw_x = sw[Geom::X] / tan_angle[X];
543 lxw_z = sw[Geom::X] / tan_angle[Z];
545 if (empspacing == 0) {
546 scaled = true;
547 }
549 }
551 void
552 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
553 {
554 //set correct coloring, depending preference (when zoomed out, always major coloring or minor coloring)
555 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
556 guint32 _empcolor;
557 bool preference = prefs->getBool("/options/grids/no_emphasize_when_zoomedout", false);
558 if( scaled && preference ) {
559 _empcolor = color;
560 } else {
561 _empcolor = empcolor;
562 }
564 // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
565 // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
566 // bc = buffer patch coordinates
568 // tl = topleft ; br = bottomright
569 Geom::Point buf_tl_gc;
570 Geom::Point buf_br_gc;
571 buf_tl_gc[Geom::X] = buf->rect.x0 - ow[Geom::X];
572 buf_tl_gc[Geom::Y] = buf->rect.y0 - ow[Geom::Y];
573 buf_br_gc[Geom::X] = buf->rect.x1 - ow[Geom::X];
574 buf_br_gc[Geom::Y] = buf->rect.y1 - ow[Geom::Y];
576 gdouble x;
577 gdouble y;
579 // render the three separate line groups representing the main-axes
581 // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
582 gdouble const xintercept_y_bc = (buf_tl_gc[Geom::X] * tan_angle[X]) - buf_tl_gc[Geom::Y] ;
583 gdouble const xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
584 gint const xlinestart = (gint) Inkscape::round( (xstart_y_sc - buf->rect.x0*tan_angle[X] -ow[Geom::Y]) / lyw );
585 gint xlinenum = xlinestart;
586 // lines starting on left side.
587 for (y = xstart_y_sc; y < buf->rect.y1; y += lyw, xlinenum++) {
588 gint const x0 = buf->rect.x0;
589 gint const y0 = (gint) Inkscape::round(y);
590 gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
591 gint const y1 = buf->rect.y1;
593 if (!scaled && (xlinenum % empspacing) != 0) {
594 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
595 } else {
596 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
597 }
598 }
599 // lines starting from top side
600 gdouble const xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
601 xlinenum = xlinestart-1;
602 for (x = xstart_x_sc; x < buf->rect.x1; x += lxw_x, xlinenum--) {
603 gint const y0 = buf->rect.y0;
604 gint const y1 = buf->rect.y1;
605 gint const x0 = (gint) Inkscape::round(x);
606 gint const x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
608 if (!scaled && (xlinenum % empspacing) != 0) {
609 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
610 } else {
611 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
612 }
613 }
615 // y-axis lines (vertical)
616 gdouble const ystart_x_sc = floor (buf_tl_gc[Geom::X] / spacing_ylines) * spacing_ylines + ow[Geom::X];
617 gint const ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[Geom::X]) / spacing_ylines);
618 gint ylinenum = ylinestart;
619 for (x = ystart_x_sc; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
620 gint const x0 = (gint) Inkscape::round(x);
622 if (!scaled && (ylinenum % empspacing) != 0) {
623 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
624 } else {
625 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, _empcolor);
626 }
627 }
629 // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
630 gdouble const zintercept_y_bc = (buf_tl_gc[Geom::X] * -tan_angle[Z]) - buf_tl_gc[Geom::Y] ;
631 gdouble const zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
632 gint const zlinestart = (gint) Inkscape::round( (zstart_y_sc + buf->rect.x0*tan_angle[Z] - ow[Geom::Y]) / lyw );
633 gint zlinenum = zlinestart;
634 // lines starting from left side
635 for (y = zstart_y_sc; y < buf->rect.y1; y += lyw, zlinenum++) {
636 gint const x0 = buf->rect.x0;
637 gint const y0 = (gint) Inkscape::round(y);
638 gint const x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
639 gint const y1 = buf->rect.y0;
641 if (!scaled && (zlinenum % empspacing) != 0) {
642 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
643 } else {
644 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
645 }
646 }
647 // draw lines from bottom-up
648 gdouble const zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
649 for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum++) {
650 gint const y0 = buf->rect.y1;
651 gint const y1 = buf->rect.y0;
652 gint const x0 = (gint) Inkscape::round(x);
653 gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
655 if (!scaled && (zlinenum % empspacing) != 0) {
656 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
657 } else {
658 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
659 }
660 }
661 }
663 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SnapManager *sm, Geom::Coord const d) : LineSnapper(sm, d)
664 {
665 this->grid = grid;
666 }
668 /**
669 * \return Snap tolerance (desktop coordinates); depends on current zoom so that it's always the same in screen pixels
670 */
671 Geom::Coord CanvasAxonomGridSnapper::getSnapperTolerance() const
672 {
673 SPDesktop const *dt = _snapmanager->getDesktop();
674 double const zoom = dt ? dt->current_zoom() : 1;
675 return _snapmanager->snapprefs.getGridTolerance() / zoom;
676 }
678 bool CanvasAxonomGridSnapper::getSnapperAlwaysSnap() const
679 {
680 return _snapmanager->snapprefs.getGridTolerance() == 10000; //TODO: Replace this threshold of 10000 by a constant; see also tolerance-slider.cpp
681 }
683 LineSnapper::LineList
684 CanvasAxonomGridSnapper::_getSnapLines(Geom::Point const &p) const
685 {
686 LineList s;
688 if ( grid == NULL ) {
689 return s;
690 }
692 double spacing_h;
693 double spacing_v;
695 if (getSnapVisibleOnly()) {
696 // Only snapping to visible grid lines
697 spacing_h = grid->spacing_ylines; // this is the spacing of the visible grid lines measured in screen pixels
698 spacing_v = grid->lyw; // vertical
699 // convert screen pixels to px
700 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
701 SPDesktop const *dt = _snapmanager->getDesktop();
702 if (dt) {
703 spacing_h /= dt->current_zoom();
704 spacing_v /= dt->current_zoom();
705 }
706 } else {
707 // Snapping to any grid line, whether it's visible or not
708 spacing_h = grid->lengthy /(grid->tan_angle[X] + grid->tan_angle[Z]);
709 spacing_v = grid->lengthy;
711 }
713 // In an axonometric grid, any point will be surrounded by 6 grid lines:
714 // - 2 vertical grid lines, one left and one right from the point
715 // - 2 angled z grid lines, one above and one below the point
716 // - 2 angled x grid lines, one above and one below the point
718 // Calculate the x coordinate of the vertical grid lines
719 Geom::Coord x_max = Inkscape::Util::round_to_upper_multiple_plus(p[Geom::X], spacing_h, grid->origin[Geom::X]);
720 Geom::Coord x_min = Inkscape::Util::round_to_lower_multiple_plus(p[Geom::X], spacing_h, grid->origin[Geom::X]);
722 // Calculate the y coordinate of the intersection of the angled grid lines with the y-axis
723 double y_proj_along_z = p[Geom::Y] - grid->tan_angle[Z]*(p[Geom::X] - grid->origin[Geom::X]);
724 double y_proj_along_x = p[Geom::Y] + grid->tan_angle[X]*(p[Geom::X] - grid->origin[Geom::X]);
725 double y_proj_along_z_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_z, spacing_v, grid->origin[Geom::Y]);
726 double y_proj_along_z_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_z, spacing_v, grid->origin[Geom::Y]);
727 double y_proj_along_x_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_x, spacing_v, grid->origin[Geom::Y]);
728 double y_proj_along_x_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_x, spacing_v, grid->origin[Geom::Y]);
730 // Calculate the versor for the angled grid lines
731 Geom::Point vers_x = Geom::Point(1, -grid->tan_angle[X]);
732 Geom::Point vers_z = Geom::Point(1, grid->tan_angle[Z]);
734 // Calculate the normal for the angled grid lines
735 Geom::Point norm_x = Geom::rot90(vers_x);
736 Geom::Point norm_z = Geom::rot90(vers_z);
738 // The four angled grid lines form a parallelogram, enclosing the point
739 // One of the two vertical grid lines divides this parallelogram in two triangles
740 // We will now try to find out in which half (i.e. triangle) our point is, and return
741 // only the three grid lines defining that triangle
743 // The vertical grid line is at the intersection of two angled grid lines.
744 // Now go find that intersection!
745 Geom::Point p_x(0, y_proj_along_x_max);
746 Geom::Line line_x(p_x, p_x + vers_x);
747 Geom::Point p_z(0, y_proj_along_z_max);
748 Geom::Line line_z(p_z, p_z + vers_z);
750 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
751 try
752 {
753 inters = Geom::intersection(line_x, line_z);
754 }
755 catch (Geom::InfiniteSolutions e)
756 {
757 // We're probably dealing with parallel lines; this is useless!
758 return s;
759 }
761 // Determine which half of the parallelogram to use
762 bool use_left_half = true;
763 bool use_right_half = true;
765 if (inters) {
766 Geom::Point inters_pt = line_x.pointAt((*inters).ta);
767 use_left_half = (p[Geom::X] - grid->origin[Geom::X]) < inters_pt[Geom::X];
768 use_right_half = !use_left_half;
769 }
771 // Return the three grid lines which define the triangle that encloses our point
772 // If we didn't find an intersection above, all 6 grid lines will be returned
773 if (use_left_half) {
774 s.push_back(std::make_pair(norm_z, Geom::Point(grid->origin[Geom::X], y_proj_along_z_max)));
775 s.push_back(std::make_pair(norm_x, Geom::Point(grid->origin[Geom::X], y_proj_along_x_min)));
776 s.push_back(std::make_pair(component_vectors[Geom::X], Geom::Point(x_max, 0)));
777 }
779 if (use_right_half) {
780 s.push_back(std::make_pair(norm_z, Geom::Point(grid->origin[Geom::X], y_proj_along_z_min)));
781 s.push_back(std::make_pair(norm_x, Geom::Point(grid->origin[Geom::X], y_proj_along_x_max)));
782 s.push_back(std::make_pair(component_vectors[Geom::X], Geom::Point(x_min, 0)));
783 }
785 return s;
786 }
788 void CanvasAxonomGridSnapper::_addSnappedLine(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, SnapSourceType const &source, long source_num, Geom::Point const normal_to_line, Geom::Point const point_on_line) const
789 {
790 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, source, source_num, Inkscape::SNAPTARGET_GRID, getSnapperTolerance(), getSnapperAlwaysSnap(), normal_to_line, point_on_line);
791 sc.grid_lines.push_back(dummy);
792 }
794 void CanvasAxonomGridSnapper::_addSnappedPoint(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, SnapSourceType const &source, long source_num, bool constrained_snap) const
795 {
796 SnappedPoint dummy = SnappedPoint(snapped_point, source, source_num, Inkscape::SNAPTARGET_GRID, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(), constrained_snap, true);
797 sc.points.push_back(dummy);
798 }
800 bool CanvasAxonomGridSnapper::ThisSnapperMightSnap() const
801 {
802 return _snap_enabled && _snapmanager->snapprefs.getSnapToGrids() && _snapmanager->snapprefs.getSnapModeBBoxOrNodes();
803 }
806 }; // namespace Inkscape
809 /*
810 Local Variables:
811 mode:c++
812 c-file-style:"stroustrup"
813 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
814 indent-tabs-mode:nil
815 fill-column:99
816 End:
817 */
818 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :