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/line.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 "preferences.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 }
125 }
127 static void
128 sp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
129 {
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 }
148 }
150 namespace Inkscape {
153 /**
154 * A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
155 *
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)
166 {
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 }
191 }
193 CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
194 : CanvasGrid(nv, in_repr, in_doc, GRID_AXONOMETRIC)
195 {
196 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
197 gridunit = sp_unit_get_by_abbreviation( prefs->getString("/options/grids/axonom/units").data() );
198 if (!gridunit)
199 gridunit = &sp_unit_get_by_id(SP_UNIT_PX);
200 origin[Geom::X] = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/origin_x", 0.0), *gridunit );
201 origin[Geom::Y] = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/origin_y", 0.0), *gridunit );
202 color = prefs->getInt("/options/grids/axonom/color", 0x0000ff20);
203 empcolor = prefs->getInt("/options/grids/axonom/empcolor", 0x0000ff40);
204 empspacing = prefs->getInt("/options/grids/axonom/empspacing", 5);
205 lengthy = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/spacing_y", 1.0), *gridunit );
206 angle_deg[X] = prefs->getDouble("/options/grids/axonom/angle_x", 30.0);
207 angle_deg[Z] = prefs->getDouble("/options/grids/axonom/angle_z", 30.0);
208 angle_deg[Y] = 0;
210 angle_rad[X] = deg_to_rad(angle_deg[X]);
211 tan_angle[X] = tan(angle_rad[X]);
212 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
213 tan_angle[Z] = tan(angle_rad[Z]);
215 snapper = new CanvasAxonomGridSnapper(this, &namedview->snap_manager, 0);
217 if (repr) readRepr();
218 }
220 CanvasAxonomGrid::~CanvasAxonomGrid ()
221 {
222 if (snapper) delete snapper;
223 }
226 /* fixme: Collect all these length parsing methods and think common sane API */
228 static gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
229 {
230 if (!str) {
231 return FALSE;
232 }
234 gchar *u;
235 gdouble v = g_ascii_strtod(str, &u);
236 if (!u) {
237 return FALSE;
238 }
239 while (isspace(*u)) {
240 u += 1;
241 }
243 if (!*u) {
244 /* No unit specified - keep default */
245 *val = v;
246 return TRUE;
247 }
249 if (base & SP_UNIT_DEVICE) {
250 if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
251 *unit = &sp_unit_get_by_id(SP_UNIT_PX);
252 *val = v;
253 return TRUE;
254 }
255 }
257 if (base & SP_UNIT_ABSOLUTE) {
258 if (!strncmp(u, "pt", 2)) {
259 *unit = &sp_unit_get_by_id(SP_UNIT_PT);
260 } else if (!strncmp(u, "mm", 2)) {
261 *unit = &sp_unit_get_by_id(SP_UNIT_MM);
262 } else if (!strncmp(u, "cm", 2)) {
263 *unit = &sp_unit_get_by_id(SP_UNIT_CM);
264 } else if (!strncmp(u, "m", 1)) {
265 *unit = &sp_unit_get_by_id(SP_UNIT_M);
266 } else if (!strncmp(u, "in", 2)) {
267 *unit = &sp_unit_get_by_id(SP_UNIT_IN);
268 } else {
269 return FALSE;
270 }
271 *val = v;
272 return TRUE;
273 }
275 return FALSE;
276 }
278 static gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
279 {
280 if (!str) {
281 return FALSE;
282 }
284 gchar *u;
285 gdouble v = g_ascii_strtod(str, &u);
286 if (!u) {
287 return FALSE;
288 }
289 v = CLAMP(v, 0.0, 1.0);
291 *color = (*color & 0xffffff00) | (guint32) floor(v * 255.9999);
293 return TRUE;
294 }
298 void
299 CanvasAxonomGrid::readRepr()
300 {
301 gchar const *value;
302 if ( (value = repr->attribute("originx")) ) {
303 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[Geom::X], &gridunit);
304 origin[Geom::X] = sp_units_get_pixels(origin[Geom::X], *(gridunit));
305 }
306 if ( (value = repr->attribute("originy")) ) {
307 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &origin[Geom::Y], &gridunit);
308 origin[Geom::Y] = sp_units_get_pixels(origin[Geom::Y], *(gridunit));
309 }
311 if ( (value = repr->attribute("spacingy")) ) {
312 sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &lengthy, &gridunit);
313 lengthy = sp_units_get_pixels(lengthy, *(gridunit));
314 if (lengthy < 0.0500) lengthy = 0.0500;
315 }
317 if ( (value = repr->attribute("gridanglex")) ) {
318 angle_deg[X] = g_ascii_strtod(value, NULL);
319 if (angle_deg[X] < 1.0) angle_deg[X] = 1.0;
320 if (angle_deg[X] > 89.0) angle_deg[X] = 89.0;
321 angle_rad[X] = deg_to_rad(angle_deg[X]);
322 tan_angle[X] = tan(angle_rad[X]);
323 }
325 if ( (value = repr->attribute("gridanglez")) ) {
326 angle_deg[Z] = g_ascii_strtod(value, NULL);
327 if (angle_deg[Z] < 1.0) angle_deg[Z] = 1.0;
328 if (angle_deg[Z] > 89.0) angle_deg[Z] = 89.0;
329 angle_rad[Z] = deg_to_rad(angle_deg[Z]);
330 tan_angle[Z] = tan(angle_rad[Z]);
331 }
333 if ( (value = repr->attribute("color")) ) {
334 color = (color & 0xff) | sp_svg_read_color(value, color);
335 }
337 if ( (value = repr->attribute("empcolor")) ) {
338 empcolor = (empcolor & 0xff) | sp_svg_read_color(value, empcolor);
339 }
341 if ( (value = repr->attribute("opacity")) ) {
342 sp_nv_read_opacity(value, &color);
343 }
344 if ( (value = repr->attribute("empopacity")) ) {
345 sp_nv_read_opacity(value, &empcolor);
346 }
348 if ( (value = repr->attribute("empspacing")) ) {
349 empspacing = atoi(value);
350 }
352 if ( (value = repr->attribute("visible")) ) {
353 visible = (strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
354 }
356 if ( (value = repr->attribute("enabled")) ) {
357 g_assert(snapper != NULL);
358 snapper->setEnabled(strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
359 }
361 if ( (value = repr->attribute("snapvisiblegridlinesonly")) ) {
362 g_assert(snapper != NULL);
363 snapper->setSnapVisibleOnly(strcmp(value,"false") != 0 && strcmp(value, "0") != 0);
364 }
366 for (GSList *l = canvasitems; l != NULL; l = l->next) {
367 sp_canvas_item_request_update ( SP_CANVAS_ITEM(l->data) );
368 }
369 return;
370 }
372 /**
373 * Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
374 */
375 void
376 CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
377 {
378 readRepr();
380 if ( ! (_wr.isUpdating()) )
381 updateWidgets();
382 }
387 Gtk::Widget *
388 CanvasAxonomGrid::newSpecificWidget()
389 {
390 Gtk::Table * table = Gtk::manage( new Gtk::Table(1,1) );
391 table->set_spacings(2);
393 _wr.setUpdating (true);
395 Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu(
396 _("Grid _units:"), "units", _wr, repr, doc) );
397 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
398 _("_Origin X:"), _("X coordinate of grid origin"), "originx", *_rumg, _wr, repr, doc) );
399 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
400 _("O_rigin Y:"), _("Y coordinate of grid origin"), "originy", *_rumg, _wr, repr, doc) );
401 Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
402 _("Spacing _Y:"), _("Base length of z-axis"), "spacingy", *_rumg, _wr, repr, doc) );
403 Inkscape::UI::Widget::RegisteredScalar *_rsu_ax = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
404 _("Angle X:"), _("Angle of x-axis"), "gridanglex", _wr, repr, doc ) );
405 Inkscape::UI::Widget::RegisteredScalar *_rsu_az = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
406 _("Angle Z:"), _("Angle of z-axis"), "gridanglez", _wr, repr, doc ) );
408 Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gcol = Gtk::manage(
409 new Inkscape::UI::Widget::RegisteredColorPicker(
410 _("Grid line _color:"), _("Grid line color"), _("Color of grid lines"),
411 "color", "opacity", _wr, repr, doc));
413 Inkscape::UI::Widget::RegisteredColorPicker *_rcp_gmcol = Gtk::manage(
414 new Inkscape::UI::Widget::RegisteredColorPicker(
415 _("Ma_jor grid line color:"), _("Major grid line color"),
416 _("Color of the major (highlighted) grid lines"),
417 "empcolor", "empopacity", _wr, repr, doc));
419 Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger(
420 _("_Major grid line every:"), "", _("lines"), "empspacing", _wr, repr, doc ) );
422 _rsu_ox->setDigits(4);
423 _rsu_ox->setIncrements(0.1, 1.0);
425 _rsu_oy->setDigits(4);
426 _rsu_oy->setIncrements(0.1, 1.0);
428 _rsu_sy->setDigits(4);
429 _rsu_sy->setIncrements(0.1, 1.0);
431 _wr.setUpdating (false);
433 Gtk::Widget const *const widget_array[] = {
434 0, _rumg,
435 0, _rsu_ox,
436 0, _rsu_oy,
437 0, _rsu_sy,
438 0, _rsu_ax,
439 0, _rsu_az,
440 _rcp_gcol->_label, _rcp_gcol,
441 0, 0,
442 _rcp_gmcol->_label, _rcp_gmcol,
443 0, _rsi,
444 };
446 attach_all (*table, widget_array, sizeof(widget_array));
448 // set widget values
449 _rumg->setUnit (gridunit);
451 gdouble val;
452 val = origin[Geom::X];
453 val = sp_pixels_get_units (val, *(gridunit));
454 _rsu_ox->setValue (val);
455 val = origin[Geom::Y];
456 val = sp_pixels_get_units (val, *(gridunit));
457 _rsu_oy->setValue (val);
458 val = lengthy;
459 double gridy = sp_pixels_get_units (val, *(gridunit));
460 _rsu_sy->setValue (gridy);
462 _rsu_ax->setValue(angle_deg[X]);
463 _rsu_az->setValue(angle_deg[Z]);
465 _rcp_gcol->setRgba32 (color);
466 _rcp_gmcol->setRgba32 (empcolor);
467 _rsi->setValue (empspacing);
469 return table;
470 }
473 /**
474 * Update dialog widgets from object's values.
475 */
476 void
477 CanvasAxonomGrid::updateWidgets()
478 {
479 /* if (_wr.isUpdating()) return;
481 _wr.setUpdating (true);
483 _rcb_visible.setActive(visible);
484 if (snapper != NULL) {
485 _rcb_enabled.setActive(snapper->getEnabled());
486 }
488 _rumg.setUnit (gridunit);
490 gdouble val;
491 val = origin[Geom::X];
492 val = sp_pixels_get_units (val, *(gridunit));
493 _rsu_ox.setValue (val);
494 val = origin[Geom::Y];
495 val = sp_pixels_get_units (val, *(gridunit));
496 _rsu_oy.setValue (val);
497 val = lengthy;
498 double gridy = sp_pixels_get_units (val, *(gridunit));
499 _rsu_sy.setValue (gridy);
501 _rsu_ax.setValue(angle_deg[X]);
502 _rsu_az.setValue(angle_deg[Z]);
504 _rcp_gcol.setRgba32 (color);
505 _rcp_gmcol.setRgba32 (empcolor);
506 _rsi.setValue (empspacing);
508 _wr.setUpdating (false);
510 return;
511 */
512 }
516 void
517 CanvasAxonomGrid::Update (Geom::Matrix const &affine, unsigned int /*flags*/)
518 {
519 ow = origin * affine;
520 sw = Geom::Point(fabs(affine[0]),fabs(affine[3]));
521 sw *= lengthy;
523 scaled = false;
525 for(int dim = 0; dim < 2; dim++) {
526 gint scaling_factor = empspacing;
528 if (scaling_factor <= 1)
529 scaling_factor = 5;
531 int watchdog = 0;
532 while ( (sw[dim] < 8.0) & (watchdog < 100) ) {
533 scaled = true;
534 sw[dim] *= scaling_factor;
535 // First pass, go up to the major line spacing, then
536 // keep increasing by two.
537 scaling_factor = 2;
538 watchdog++;
539 }
541 }
543 spacing_ylines = sw[Geom::X] /(tan_angle[X] + tan_angle[Z]);
544 lyw = sw[Geom::Y];
545 lxw_x = sw[Geom::X] / tan_angle[X];
546 lxw_z = sw[Geom::X] / tan_angle[Z];
548 if (empspacing == 0) {
549 scaled = true;
550 }
552 }
554 void
555 CanvasAxonomGrid::Render (SPCanvasBuf *buf)
556 {
557 //set correct coloring, depending preference (when zoomed out, always major coloring or minor coloring)
558 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
559 guint32 _empcolor;
560 bool preference = prefs->getBool("/options/grids/no_emphasize_when_zoomedout", false);
561 if( scaled && preference ) {
562 _empcolor = color;
563 } else {
564 _empcolor = empcolor;
565 }
567 // gc = gridcoordinates (the coordinates calculated from the grids origin 'grid->ow'.
568 // sc = screencoordinates ( for example "buf->rect.x0" is in screencoordinates )
569 // bc = buffer patch coordinates
571 // tl = topleft ; br = bottomright
572 Geom::Point buf_tl_gc;
573 Geom::Point buf_br_gc;
574 buf_tl_gc[Geom::X] = buf->rect.x0 - ow[Geom::X];
575 buf_tl_gc[Geom::Y] = buf->rect.y0 - ow[Geom::Y];
576 buf_br_gc[Geom::X] = buf->rect.x1 - ow[Geom::X];
577 buf_br_gc[Geom::Y] = buf->rect.y1 - ow[Geom::Y];
579 gdouble x;
580 gdouble y;
582 // render the three separate line groups representing the main-axes
584 // x-axis always goes from topleft to bottomright. (0,0) - (1,1)
585 gdouble const xintercept_y_bc = (buf_tl_gc[Geom::X] * tan_angle[X]) - buf_tl_gc[Geom::Y] ;
586 gdouble const xstart_y_sc = ( xintercept_y_bc - floor(xintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
587 gint const xlinestart = (gint) Inkscape::round( (xstart_y_sc - buf->rect.x0*tan_angle[X] -ow[Geom::Y]) / lyw );
588 gint xlinenum = xlinestart;
589 // lines starting on left side.
590 for (y = xstart_y_sc; y < buf->rect.y1; y += lyw, xlinenum++) {
591 gint const x0 = buf->rect.x0;
592 gint const y0 = (gint) Inkscape::round(y);
593 gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - y) / tan_angle[X] );
594 gint const y1 = buf->rect.y1;
596 if (!scaled && (xlinenum % empspacing) != 0) {
597 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
598 } else {
599 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
600 }
601 }
602 // lines starting from top side
603 gdouble const xstart_x_sc = buf->rect.x0 + (lxw_x - (xstart_y_sc - buf->rect.y0) / tan_angle[X]) ;
604 xlinenum = xlinestart-1;
605 for (x = xstart_x_sc; x < buf->rect.x1; x += lxw_x, xlinenum--) {
606 gint const y0 = buf->rect.y0;
607 gint const y1 = buf->rect.y1;
608 gint const x0 = (gint) Inkscape::round(x);
609 gint const x1 = x0 + (gint) Inkscape::round( (y1 - y0) / tan_angle[X] );
611 if (!scaled && (xlinenum % empspacing) != 0) {
612 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
613 } else {
614 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
615 }
616 }
618 // y-axis lines (vertical)
619 gdouble const ystart_x_sc = floor (buf_tl_gc[Geom::X] / spacing_ylines) * spacing_ylines + ow[Geom::X];
620 gint const ylinestart = (gint) Inkscape::round((ystart_x_sc - ow[Geom::X]) / spacing_ylines);
621 gint ylinenum = ylinestart;
622 for (x = ystart_x_sc; x < buf->rect.x1; x += spacing_ylines, ylinenum++) {
623 gint const x0 = (gint) Inkscape::round(x);
625 if (!scaled && (ylinenum % empspacing) != 0) {
626 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, color);
627 } else {
628 sp_grid_vline (buf, x0, buf->rect.y0, buf->rect.y1 - 1, _empcolor);
629 }
630 }
632 // z-axis always goes from bottomleft to topright. (0,1) - (1,0)
633 gdouble const zintercept_y_bc = (buf_tl_gc[Geom::X] * -tan_angle[Z]) - buf_tl_gc[Geom::Y] ;
634 gdouble const zstart_y_sc = ( zintercept_y_bc - floor(zintercept_y_bc/lyw)*lyw ) + buf->rect.y0;
635 gint const zlinestart = (gint) Inkscape::round( (zstart_y_sc + buf->rect.x0*tan_angle[Z] - ow[Geom::Y]) / lyw );
636 gint zlinenum = zlinestart;
637 // lines starting from left side
638 for (y = zstart_y_sc; y < buf->rect.y1; y += lyw, zlinenum++) {
639 gint const x0 = buf->rect.x0;
640 gint const y0 = (gint) Inkscape::round(y);
641 gint const x1 = x0 + (gint) Inkscape::round( (y - buf->rect.y0 ) / tan_angle[Z] );
642 gint const y1 = buf->rect.y0;
644 if (!scaled && (zlinenum % empspacing) != 0) {
645 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
646 } else {
647 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
648 }
649 }
650 // draw lines from bottom-up
651 gdouble const zstart_x_sc = buf->rect.x0 + (y - buf->rect.y1) / tan_angle[Z] ;
652 for (x = zstart_x_sc; x < buf->rect.x1; x += lxw_z, zlinenum++) {
653 gint const y0 = buf->rect.y1;
654 gint const y1 = buf->rect.y0;
655 gint const x0 = (gint) Inkscape::round(x);
656 gint const x1 = x0 + (gint) Inkscape::round( (buf->rect.y1 - buf->rect.y0) / tan_angle[Z] );
658 if (!scaled && (zlinenum % empspacing) != 0) {
659 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, color);
660 } else {
661 sp_caxonomgrid_drawline (buf, x0, y0, x1, y1, _empcolor);
662 }
663 }
664 }
666 CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SnapManager *sm, Geom::Coord const d) : LineSnapper(sm, d)
667 {
668 this->grid = grid;
669 }
671 /**
672 * \return Snap tolerance (desktop coordinates); depends on current zoom so that it's always the same in screen pixels
673 */
674 Geom::Coord CanvasAxonomGridSnapper::getSnapperTolerance() const
675 {
676 SPDesktop const *dt = _snapmanager->getDesktop();
677 double const zoom = dt ? dt->current_zoom() : 1;
678 return _snapmanager->snapprefs.getGridTolerance() / zoom;
679 }
681 bool CanvasAxonomGridSnapper::getSnapperAlwaysSnap() const
682 {
683 return _snapmanager->snapprefs.getGridTolerance() == 10000; //TODO: Replace this threshold of 10000 by a constant; see also tolerance-slider.cpp
684 }
686 LineSnapper::LineList
687 CanvasAxonomGridSnapper::_getSnapLines(Geom::Point const &p) const
688 {
689 LineList s;
691 if ( grid == NULL ) {
692 return s;
693 }
695 double spacing_h;
696 double spacing_v;
698 if (getSnapVisibleOnly()) {
699 // Only snapping to visible grid lines
700 spacing_h = grid->spacing_ylines; // this is the spacing of the visible grid lines measured in screen pixels
701 spacing_v = grid->lyw; // vertical
702 // convert screen pixels to px
703 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
704 SPDesktop const *dt = _snapmanager->getDesktop();
705 if (dt) {
706 spacing_h /= dt->current_zoom();
707 spacing_v /= dt->current_zoom();
708 }
709 } else {
710 // Snapping to any grid line, whether it's visible or not
711 spacing_h = grid->lengthy /(grid->tan_angle[X] + grid->tan_angle[Z]);
712 spacing_v = grid->lengthy;
714 }
716 // In an axonometric grid, any point will be surrounded by 6 grid lines:
717 // - 2 vertical grid lines, one left and one right from the point
718 // - 2 angled z grid lines, one above and one below the point
719 // - 2 angled x grid lines, one above and one below the point
721 // Calculate the x coordinate of the vertical grid lines
722 Geom::Coord x_max = Inkscape::Util::round_to_upper_multiple_plus(p[Geom::X], spacing_h, grid->origin[Geom::X]);
723 Geom::Coord x_min = Inkscape::Util::round_to_lower_multiple_plus(p[Geom::X], spacing_h, grid->origin[Geom::X]);
725 // Calculate the y coordinate of the intersection of the angled grid lines with the y-axis
726 double y_proj_along_z = p[Geom::Y] - grid->tan_angle[Z]*(p[Geom::X] - grid->origin[Geom::X]);
727 double y_proj_along_x = p[Geom::Y] + grid->tan_angle[X]*(p[Geom::X] - grid->origin[Geom::X]);
728 double y_proj_along_z_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_z, spacing_v, grid->origin[Geom::Y]);
729 double y_proj_along_z_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_z, spacing_v, grid->origin[Geom::Y]);
730 double y_proj_along_x_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_x, spacing_v, grid->origin[Geom::Y]);
731 double y_proj_along_x_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_x, spacing_v, grid->origin[Geom::Y]);
733 // Calculate the versor for the angled grid lines
734 Geom::Point vers_x = Geom::Point(1, -grid->tan_angle[X]);
735 Geom::Point vers_z = Geom::Point(1, grid->tan_angle[Z]);
737 // Calculate the normal for the angled grid lines
738 Geom::Point norm_x = Geom::rot90(vers_x);
739 Geom::Point norm_z = Geom::rot90(vers_z);
741 // The four angled grid lines form a parallelogram, enclosing the point
742 // One of the two vertical grid lines divides this parallelogram in two triangles
743 // We will now try to find out in which half (i.e. triangle) our point is, and return
744 // only the three grid lines defining that triangle
746 // The vertical grid line is at the intersection of two angled grid lines.
747 // Now go find that intersection!
748 Geom::Point p_x(0, y_proj_along_x_max);
749 Geom::Line line_x(p_x, p_x + vers_x);
750 Geom::Point p_z(0, y_proj_along_z_max);
751 Geom::Line line_z(p_z, p_z + vers_z);
753 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
754 try
755 {
756 inters = Geom::intersection(line_x, line_z);
757 }
758 catch (Geom::InfiniteSolutions e)
759 {
760 // We're probably dealing with parallel lines; this is useless!
761 return s;
762 }
764 // Determine which half of the parallelogram to use
765 bool use_left_half = true;
766 bool use_right_half = true;
768 if (inters) {
769 Geom::Point inters_pt = line_x.pointAt((*inters).ta);
770 use_left_half = (p[Geom::X] - grid->origin[Geom::X]) < inters_pt[Geom::X];
771 use_right_half = !use_left_half;
772 }
774 // Return the three grid lines which define the triangle that encloses our point
775 // If we didn't find an intersection above, all 6 grid lines will be returned
776 if (use_left_half) {
777 s.push_back(std::make_pair(norm_z, Geom::Point(grid->origin[Geom::X], y_proj_along_z_max)));
778 s.push_back(std::make_pair(norm_x, Geom::Point(grid->origin[Geom::X], y_proj_along_x_min)));
779 s.push_back(std::make_pair(component_vectors[Geom::X], Geom::Point(x_max, 0)));
780 }
782 if (use_right_half) {
783 s.push_back(std::make_pair(norm_z, Geom::Point(grid->origin[Geom::X], y_proj_along_z_min)));
784 s.push_back(std::make_pair(norm_x, Geom::Point(grid->origin[Geom::X], y_proj_along_x_max)));
785 s.push_back(std::make_pair(component_vectors[Geom::X], Geom::Point(x_min, 0)));
786 }
788 return s;
789 }
791 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
792 {
793 SnappedLine dummy = SnappedLine(snapped_point, snapped_distance, source, source_num, Inkscape::SNAPTARGET_GRID, getSnapperTolerance(), getSnapperAlwaysSnap(), normal_to_line, point_on_line);
794 sc.grid_lines.push_back(dummy);
795 }
797 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
798 {
799 SnappedPoint dummy = SnappedPoint(snapped_point, source, source_num, Inkscape::SNAPTARGET_GRID, snapped_distance, getSnapperTolerance(), getSnapperAlwaysSnap(), constrained_snap, true);
800 sc.points.push_back(dummy);
801 }
803 bool CanvasAxonomGridSnapper::ThisSnapperMightSnap() const
804 {
805 return _snap_enabled && _snapmanager->snapprefs.getSnapToGrids() && _snapmanager->snapprefs.getSnapModeBBoxOrNodes();
806 }
809 }; // namespace Inkscape
812 /*
813 Local Variables:
814 mode:c++
815 c-file-style:"stroustrup"
816 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
817 indent-tabs-mode:nil
818 fill-column:99
819 End:
820 */
821 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :