From: johanengelen Date: Fri, 9 Nov 2007 23:15:51 +0000 (+0000) Subject: Remove geom.cpp and geom.h. Now use 2geom/geom.cpp and 2geom.h. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=c3bc76d351bed6e320f6dbf2242012f026659287;p=inkscape.git Remove geom.cpp and geom.h. Now use 2geom/geom.cpp and 2geom.h. Add conversion functions between Geom::Point and NR::Point --- diff --git a/src/2geom/coord.h b/src/2geom/coord.h index 6636b4ad7..34a585bf9 100644 --- a/src/2geom/coord.h +++ b/src/2geom/coord.h @@ -46,9 +46,20 @@ typedef double Coord; const Coord EPSILON = 1e-5; //1e-18; + +#ifdef near +#define lib2geom_near_save near +#undef near +#endif + //IMPL: NearConcept inline bool near(Coord a, Coord b, double eps=EPSILON) { return fabs(a-b) <= eps; } +#ifdef lib2geom_near_save +#define near lib2geom_near_save +#undef lib2geom_near_save +#endif + } /* namespace Geom */ diff --git a/src/2geom/geom.cpp b/src/2geom/geom.cpp index d2f2ef29b..e93247899 100644 --- a/src/2geom/geom.cpp +++ b/src/2geom/geom.cpp @@ -9,6 +9,7 @@ #include "geom.h" #include "point.h" +namespace Geom { /** * Finds the intersection of the two (infinite) lines * defined by the points p such that dot(n0, p) == d0 and dot(n1, p) == d1. @@ -206,6 +207,8 @@ int centroid(std::vector p, Geom::Point& centroid, double &area) { return 2; } +} + /* Local Variables: mode:c++ diff --git a/src/2geom/geom.h b/src/2geom/geom.h index 5386edbd7..c24acea63 100644 --- a/src/2geom/geom.h +++ b/src/2geom/geom.h @@ -31,12 +31,16 @@ * the specific language governing rights and limitations. * */ +#ifndef SEEN_2Geom_GEOM_H +#define SEEN_2Geom_GEOM_H //TODO: move somewhere else #include #include "point.h" +namespace Geom { + enum IntersectorKind { intersects = 0, parallel, @@ -66,3 +70,7 @@ line_twopoint_intersect(Geom::Point const &p00, Geom::Point const &p01, Geom::Point &result); int centroid(std::vector p, Geom::Point& centroid, double &area); + +} + +#endif diff --git a/src/2geom/point.h b/src/2geom/point.h index 030776915..bf7dc444f 100644 --- a/src/2geom/point.h +++ b/src/2geom/point.h @@ -174,11 +174,22 @@ extern double atan2(Point const p); /** compute the angle turning from a to b (signed). */ extern double angle_between(Point const a, Point const b); + +#ifdef near +#define lib2geom_near_save near +#undef near +#endif + //IMPL: NearConcept inline bool near(Point const &a, Point const &b, double const eps=EPSILON) { return ( near(a[X],b[X],eps) && near(a[Y],b[Y],eps) ); } +#ifdef lib2geom_near_save +#define near lib2geom_near_save +#undef lib2geom_near_save +#endif + /** Returns p * Geom::rotate_degrees(90), but more efficient. * * Angle direction in Inkscape code: If you use the traditional mathematics convention that y diff --git a/src/2geom/utils.h b/src/2geom/utils.h index 2be995248..a96a34dff 100644 --- a/src/2geom/utils.h +++ b/src/2geom/utils.h @@ -33,6 +33,8 @@ #include #include +namespace Geom { + class NotImplemented : public std::logic_error { public: NotImplemented() : std::logic_error("method not implemented") {} @@ -79,4 +81,6 @@ inline double decimal_round(double const x, int const places) { return round( x * multiplier ) / multiplier; } +} + #endif diff --git a/src/Makefile_insert b/src/Makefile_insert index 3e62c730c..823449890 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -79,7 +79,6 @@ libinkpre_a_SOURCES = \ flood-context.cpp flood-context.h \ fontsize-expansion.cpp fontsize-expansion.h \ forward.h \ - geom.cpp geom.h \ gradient-context.cpp gradient-context.h \ gradient-drag.cpp gradient-drag.h \ help.cpp help.h \ diff --git a/src/geom.cpp b/src/geom.cpp deleted file mode 100644 index 5072b0c0e..000000000 --- a/src/geom.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/** - * \file src/geom.cpp - * \brief Various geometrical calculations. - */ - -#ifdef HAVE_CONFIG_H -# include -#endif -#include "geom.h" -#include - -/** - * Finds the intersection of the two (infinite) lines - * defined by the points p such that dot(n0, p) == d0 and dot(n1, p) == d1. - * - * If the two lines intersect, then \a result becomes their point of - * intersection; otherwise, \a result remains unchanged. - * - * This function finds the intersection of the two lines (infinite) - * defined by n0.X = d0 and n1.X = d1. The algorithm is as follows: - * To compute the intersection point use Cramer's rule: - * (see http://en.wikipedia.org/wiki/Cramer%27s_rule) - * \verbatim - * convert lines to form - * ax + by = c - * dx + ey = f - * - * ( - * e.g. a = (x2 - x1), b = (y2 - y1), c = (x2 - x1)*x1 + (y2 - y1)*y1 - * ) - * - * In our case we use: - * a = n0.x d = n1.x - * b = n0.y e = n1.y - * c = d0 f = d1 - * - * so: - * - * adx + bdy = cd - * adx + aey = af - * - * bdy - aey = cd - af - * (bd - ae)y = cd - af - * - * y = (cd - af)/(bd - ae) - * - * repeat for x and you get: - * - * x = (fb - ce)/(bd - ae) \endverbatim - * - * If the denominator (bd-ae) is 0 then the lines are parallel, if the - * numerators are then 0 then the lines coincide. - * - * \todo Why not use existing but outcommented code below - * (HAVE_NEW_INTERSECTOR_CODE)? - */ -IntersectorKind intersector_line_intersection(NR::Point const &n0, double const d0, - NR::Point const &n1, double const d1, - NR::Point &result) -{ - double denominator = dot(NR::rot90(n0), n1); - double X = n1[NR::Y] * d0 - - n0[NR::Y] * d1; - /* X = (-d1, d0) dot (n0[Y], n1[Y]) */ - - if (denominator == 0) { - if ( X == 0 ) { - return COINCIDENT; - } else { - return PARALLEL; - } - } - - double Y = n0[NR::X] * d1 - - n1[NR::X] * d0; - - result = NR::Point(X, Y) / denominator; - - return INTERSECTS; -} - - - - -/* - New code which we are not yet using -*/ -#ifdef HAVE_NEW_INTERSECTOR_CODE - - -/* ccw exists as a building block */ -static int -sp_intersector_ccw(const NR::Point p0, const NR::Point p1, const NR::Point p2) -/* Determine which way a set of three points winds. */ -{ - NR::Point d1 = p1 - p0; - NR::Point d2 = p2 - p0; -/* compare slopes but avoid division operation */ - double c = dot(NR::rot90(d1), d2); - if(c > 0) - return +1; // ccw - do these match def'n in header? - if(c < 0) - return -1; // cw - - /* Colinear [or NaN]. Decide the order. */ - if ( ( d1[0] * d2[0] < 0 ) || - ( d1[1] * d2[1] < 0 ) ) { - return -1; // p2 < p0 < p1 - } else if ( dot(d1,d1) < dot(d2,d2) ) { - return +1; // p0 <= p1 < p2 - } else { - return 0; // p0 <= p2 <= p1 - } -} - -/** Determine whether two line segments intersect. This doesn't find - the point of intersection, use the line_intersect function above, - or the segment_intersection interface below. - - \pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11. - */ -static bool -sp_intersector_segment_intersectp(NR::Point const &p00, NR::Point const &p01, - NR::Point const &p10, NR::Point const &p11) -{ - g_return_val_if_fail(p00 != p01, false); - g_return_val_if_fail(p10 != p11, false); - - /* true iff ( (the p1 segment straddles the p0 infinite line) - * and (the p0 segment straddles the p1 infinite line) ). */ - return ((sp_intersector_ccw(p00,p01, p10) - *sp_intersector_ccw(p00, p01, p11)) <=0 ) - && - ((sp_intersector_ccw(p10,p11, p00) - *sp_intersector_ccw(p10, p11, p01)) <=0 ); -} - - -/** Determine whether \& where two line segments intersect. - - If the two segments don't intersect, then \a result remains unchanged. - - \pre neither segment is zero-length; i.e. p00 != p01 and p10 != p11. -**/ -static sp_intersector_kind -sp_intersector_segment_intersect(NR::Point const &p00, NR::Point const &p01, - NR::Point const &p10, NR::Point const &p11, - NR::Point &result) -{ - if(sp_intersector_segment_intersectp(p00, p01, p10, p11)) { - NR::Point n0 = (p00 - p01).ccw(); - double d0 = dot(n0,p00); - - NR::Point n1 = (p10 - p11).ccw(); - double d1 = dot(n1,p10); - return sp_intersector_line_intersection(n0, d0, n1, d1, result); - } else { - return no_intersection; - } -} - -#endif /* end yet-unused HAVE_NEW_INTERSECTOR_CODE code */ - -/* - Local Variables: - mode:c++ - c-file-style:"stroustrup" - c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) - indent-tabs-mode:nil - fill-column:99 - End: -*/ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : diff --git a/src/geom.h b/src/geom.h deleted file mode 100644 index 27e2533a2..000000000 --- a/src/geom.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef SEEN_GEOM_H -#define SEEN_GEOM_H - -/** - * \file geom.h - * \brief Various geometrical calculations - * - * Authors: - * Nathan Hurst - * - * Copyright (C) 1999-2002 authors - * - * Released under GNU GPL, read the file 'COPYING' for more information - */ - -#include "libnr/nr-forward.h" - -enum IntersectorKind { - INTERSECTS = 0, - PARALLEL, - COINCIDENT, - NO_INTERSECTION -}; - -/* Define here various primatives, such as line, line segment, circle, bezier path etc. */ - - - -/* intersectors */ - -IntersectorKind intersector_line_intersection(NR::Point const &n0, double const d0, - NR::Point const &n1, double const d1, - NR::Point &result); - -#endif /* !SEEN_GEOM_H */ diff --git a/src/libnr/nr-point.h b/src/libnr/nr-point.h index f67a66800..60c5586b0 100644 --- a/src/libnr/nr-point.h +++ b/src/libnr/nr-point.h @@ -16,6 +16,8 @@ //#include "round.h" #include "decimal-round.h" +#include <2geom/point.h> + /// A NRPoint consists of x and y coodinates. /// \todo /// This class appears to be obsoleted out in favour of NR::Point. @@ -49,6 +51,11 @@ public: } } + inline Point(Geom::Point const &p) { + _pt[X] = p[Geom::X]; + _pt[Y] = p[Geom::Y]; + } + inline Point &operator=(Point const &p) { for (unsigned i = 0; i < 2; ++i) { _pt[i] = p._pt[i]; @@ -132,6 +139,10 @@ public: friend inline std::ostream &operator<< (std::ostream &out_file, const NR::Point &in_pnt); + inline Geom::Point to_2geom() const { + return Geom::Point(_pt[X], _pt[Y]); + } + private: Coord _pt[2]; }; diff --git a/src/line-snapper.cpp b/src/line-snapper.cpp index f0ca38e3a..b0dcca22c 100644 --- a/src/line-snapper.cpp +++ b/src/line-snapper.cpp @@ -1,6 +1,6 @@ #include "libnr/nr-values.h" #include "libnr/nr-point-fns.h" -#include "geom.h" +#include <2geom/geom.h> #include "line-snapper.h" #include "snapped-line.cpp" @@ -48,10 +48,11 @@ void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc, NR::Coord const q = dot(n, point_on_line); /* Try to intersect this line with the target line */ - NR::Point t = NR::Point(NR_HUGE, NR_HUGE); - IntersectorKind const k = intersector_line_intersection(n, q, component_vectors[i->first], i->second, t); - - if (k == INTERSECTS) { + Geom::Point t_2geom(NR_HUGE, NR_HUGE); + Geom::IntersectorKind const k = Geom::line_intersection(n.to_2geom(), q, component_vectors[i->first].to_2geom(), i->second, t_2geom); + NR::Point t(t_2geom); + + if (k == Geom::intersects) { const NR::Coord dist = L2(t - p); //Store any line that's within snapping range if (dist < getDistance()) { diff --git a/src/live_effects/parameter/point.cpp b/src/live_effects/parameter/point.cpp index caa81692f..ab9f3f98a 100644 --- a/src/live_effects/parameter/point.cpp +++ b/src/live_effects/parameter/point.cpp @@ -135,7 +135,7 @@ PointParam::on_button_click() sp_knot_update_ctrl(knot); // move knot to the given point - sp_knot_set_position (knot, &NR::Point((*this)[0], (*this)[1]), SP_KNOT_STATE_NORMAL); + sp_knot_set_position (knot, &NR::Point(*this), SP_KNOT_STATE_NORMAL); sp_knot_show (knot); /* // connect knot's signals diff --git a/src/snapped-line.cpp b/src/snapped-line.cpp index 663da2e8a..c507108ac 100644 --- a/src/snapped-line.cpp +++ b/src/snapped-line.cpp @@ -9,7 +9,7 @@ */ #include "snapped-line.h" -#include "geom.h" +#include <2geom/geom.h> #include "libnr/nr-values.h" Inkscape::SnappedLineSegment::SnappedLineSegment(NR::Point snapped_point, NR::Coord snapped_distance, NR::Point start_point_of_line, NR::Point end_point_of_line) @@ -81,13 +81,14 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) // Calculate the intersection of to lines, which are both within snapping range // The point of intersection should be considered for snapping, but might be outside the snapping range - NR::Point intersection = NR::Point(NR_HUGE, NR_HUGE); + Geom::Point intersection_2geom(NR_HUGE, NR_HUGE); NR::Coord distance = NR_HUGE; - IntersectorKind result = intersector_line_intersection(getNormal(), getConstTerm(), - line.getNormal(), line.getConstTerm(), intersection); + Geom::IntersectorKind result = Geom::line_intersection(getNormal().to_2geom(), getConstTerm(), + line.getNormal().to_2geom(), line.getConstTerm(), intersection_2geom); + NR::Point intersection(intersection_2geom); - if (result == INTERSECTS) { + if (result == Geom::intersects) { /* The relevant snapped distance is the distance to the closest snapped line, not the distance to the intersection. For example, when a box is almost aligned with a grid in both horizontal and vertical directions, the distance to the intersection of the @@ -99,8 +100,8 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line) distance = std::min(_distance, line.getDistance()); //std::cout << "Intersected nicely, now getSIL distance = " << distance << std::endl; } - - return SnappedPoint(intersection, distance, result == INTERSECTS); + + return SnappedPoint(intersection, distance, result == Geom::intersects); } // search for the closest snapped line