canvas-axonomgrid.cpp revision 13fb8f69456a16aa361d3c7db68b9551f3f3ae53
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Copyright (C) 2006-2008 Johan Engelen <johan@shouraizou.nl>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Current limits are: one axis (y-axis) is always vertical. The other two
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * axes are bound to a certain range of angles. The z-axis always has an angle
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * smaller than 90 degrees (measured from horizontal, 0 degrees being a line extending
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * to the right). The x-axis will always have an angle between 0 and 90 degrees.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * THIS FILE AND THE HEADER FILE NEED CLEANING UP. PLEASE DO NOT HESISTATE TO DO SO.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * For example: the line drawing code should not be here. There _must_ be a function somewhere else that can provide this functionality...
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#define SAFE_SETPIXEL //undefine this when it is certain that setpixel is never called with invalid params
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmenum Dim3 { X=0, Y, Z };
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmstatic double deg_to_rad(double deg) { return deg*M_PI/180.0;}
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b \brief This function renders a pixel on a particular buffer.
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b The topleft of the buffer equals
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm ( rect.x0 , rect.y0 ) in screen coordinates
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm ( 0 , 0 ) in setpixel coordinates
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm The bottomright of the buffer equals
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm ( rect.x1 , rect,y1 ) in screen coordinates
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm ( rect.x1 - rect.x0 , rect.y1 - rect.y0 ) in setpixel coordinates
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmstatic void
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelensp_caxonomgrid_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b if ( (x >= buf->rect.x0) && (x < buf->rect.x1) && (y >= buf->rect.y0) && (y < buf->rect.y1) ) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm guint r, g, b, a;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm \brief This function renders a line on a particular canvas buffer,
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm using Bresenham's line drawing function.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Coordinates are interpreted as SCREENcoordinates
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmsp_caxonomgrid_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba)
c0cd5511d3b975ebe07d019c1f5528108725e438johanengelen if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if (fraction >= 0) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmstatic void
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmsp_grid_vline (SPCanvasBuf *buf, gint x, gint ys, gint ye, guint32 rgba)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm guint r, g, b, a;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm p = buf->buf + (y0 - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm* A DIRECT COPY-PASTE FROM DOCUMENT-PROPERTIES.CPP TO QUICKLY GET RESULTS
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Helper function that attachs widgets in a 3xn table. The widgets come in an
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * array that has two entries per table row. The two entries code for four
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * possible cases: (0,0) means insert space in first column; (0, non-0) means
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * widget in columns 2-3; (non-0, 0) means label in columns 1-3; and
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * (non-0, non-0) means two widgets in columns 2 and 3.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmstatic inline void
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmattach_all(Gtk::Table &table, Gtk::Widget const *const arr[], unsigned size, int start = 0)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm for (unsigned i=0, r=start; i<size/sizeof(Gtk::Widget*); i+=2) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm table.attach (const_cast<Gtk::Widget&>(*arr[i]), 1, 2, r, r+1,
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 2, 3, r, r+1,
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm table.attach (const_cast<Gtk::Widget&>(*arr[i+1]), 1, 3, r, r+1,
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm } else if (arr[i]) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Gtk::Label& label = reinterpret_cast<Gtk::Label&> (const_cast<Gtk::Widget&>(*arr[i]));
1e7c268648bcbae15fc13b8c94dee677b401d9b3gustav_b space->set_size_request (SPACE_SIZE_X, SPACE_SIZE_Y);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmCanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPDocument * in_doc)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Inkscape::Preferences *prefs = Inkscape::Preferences::get();
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm gridunit = sp_unit_get_by_abbreviation( prefs->getString("/options/grids/axonom/units").data() );
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm origin[Geom::X] = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/origin_x", 0.0), *gridunit );
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm origin[Geom::Y] = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/origin_y", 0.0), *gridunit );
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm color = prefs->getInt("/options/grids/axonom/color", 0x0000ff20);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm empcolor = prefs->getInt("/options/grids/axonom/empcolor", 0x0000ff40);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm empspacing = prefs->getInt("/options/grids/axonom/empspacing", 5);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm lengthy = sp_units_get_pixels( prefs->getDouble("/options/grids/axonom/spacing_y", 1.0), *gridunit );
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm angle_deg[X] = prefs->getDouble("/options/grids/axonom/angle_x", 30.0);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm angle_deg[Z] = prefs->getDouble("/options/grids/axonom/angle_z", 30.0);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm snapper = new CanvasAxonomGridSnapper(this, &namedview->snap_manager, 0);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm/* fixme: Collect all these length parsing methods and think common sane API */
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmstatic gboolean sp_nv_read_length(gchar const *str, guint base, gdouble *val, SPUnit const **unit)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm while (isspace(*u)) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if (!*u) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm /* No unit specified - keep default */
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen if (u[0] && u[1] && !isalnum(u[2]) && !strncmp(u, "px", 2)) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmstatic gboolean sp_nv_read_opacity(gchar const *str, guint32 *color)
gchar *u;
return FALSE;
return TRUE;
* Called when XML node attribute changed; updates dialog widgets if change was not done by widgets themselves.
CanvasAxonomGrid::onReprAttrChanged(Inkscape::XML::Node */*repr*/, gchar const */*key*/, gchar const */*oldval*/, gchar const */*newval*/, bool /*is_interactive*/)
readRepr();
Inkscape::UI::Widget::RegisteredUnitMenu *_rumg = Gtk::manage( new Inkscape::UI::Widget::RegisteredUnitMenu(
Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_ox = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_oy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
Inkscape::UI::Widget::RegisteredScalarUnit *_rsu_sy = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalarUnit(
Inkscape::UI::Widget::RegisteredScalar *_rsu_ax = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
Inkscape::UI::Widget::RegisteredScalar *_rsu_az = Gtk::manage( new Inkscape::UI::Widget::RegisteredScalar(
Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger(
0, _rumg,
0, _rsu_ox,
0, _rsu_oy,
0, _rsu_sy,
0, _rsu_ax,
0, _rsu_az,
0, _rsi,
return table;
_wr.setUpdating (true);
_rcb_visible.setActive(visible);
_rcb_enabled.setActive(snapper->getEnabled());
_rumg.setUnit (gridunit);
_rsu_ox.setValue (val);
_rsu_oy.setValue (val);
_rsu_sy.setValue (gridy);
_rsu_ax.setValue(angle_deg[X]);
_rsu_az.setValue(angle_deg[Z]);
_rcp_gcol.setRgba32 (color);
_rcp_gmcol.setRgba32 (empcolor);
_rsi.setValue (empspacing);
_wr.setUpdating (false);
int watchdog = 0;
watchdog++;
if (empspacing == 0) {
//set correct coloring, depending preference (when zoomed out, always major coloring or minor coloring)
gdouble x;
gdouble y;
gint const xlinestart = (gint) Inkscape::round( (xstart_y_sc - buf->rect.x0*tan_angle[X] -ow[Geom::Y]) / lyw );
gdouble const ystart_x_sc = floor (buf_tl_gc[Geom::X] / spacing_ylines) * spacing_ylines + ow[Geom::X];
gint const zlinestart = (gint) Inkscape::round( (zstart_y_sc + buf->rect.x0*tan_angle[Z] - ow[Geom::Y]) / lyw );
CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SnapManager const *sm, Geom::Coord const d) : LineSnapper(sm, d)
LineList s;
double scaled_spacing_h = grid->spacing_ylines; // this is spacing of visible lines if screen pixels
if (SP_ACTIVE_DESKTOP) {
Geom::Coord x_max = Inkscape::Util::round_to_upper_multiple_plus(p[Geom::X], scaled_spacing_h, grid->origin[Geom::X]);
Geom::Coord x_min = Inkscape::Util::round_to_lower_multiple_plus(p[Geom::X], scaled_spacing_h, grid->origin[Geom::X]);
double y_proj_along_z_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_z, scaled_spacing_v, grid->origin[Geom::Y]);
double y_proj_along_z_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_z, scaled_spacing_v, grid->origin[Geom::Y]);
double y_proj_along_x_max = Inkscape::Util::round_to_upper_multiple_plus(y_proj_along_x, scaled_spacing_v, grid->origin[Geom::Y]);
double y_proj_along_x_min = Inkscape::Util::round_to_lower_multiple_plus(y_proj_along_x, scaled_spacing_v, grid->origin[Geom::Y]);
result);
bool use_left_half = true;
bool use_right_half = true;
//std::cout << "intersection at " << result << " leads to use_left_half = " << use_left_half << " and use_right_half = " << use_right_half << std::endl;
if (use_left_half) {
if (use_right_half) {
void CanvasAxonomGridSnapper::_addSnappedLine(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, Geom::Point const normal_to_line, Geom::Point const point_on_line) const