From 99fae8b11f74e464ad0f55a7bfcc02933c4c1747 Mon Sep 17 00:00:00 2001 From: johanengelen Date: Wed, 13 Aug 2008 19:06:18 +0000 Subject: [PATCH] make spcurve::first_point and last_point boost::optional --- src/connector-context.cpp | 8 ++++---- src/display/curve-test.h | 26 +++++++++++++----------- src/display/curve.cpp | 30 ++++++++++++++++------------ src/display/curve.h | 4 ++-- src/draw-context.cpp | 4 ++-- src/live_effects/lpe-copy_rotate.cpp | 4 ++-- src/live_effects/lpe-offset.cpp | 2 +- src/live_effects/lpe-parallel.cpp | 4 ++-- src/live_effects/lpe-spiro.cpp | 2 +- src/pencil-context.cpp | 2 +- src/sp-conn-end-pair.cpp | 4 ++-- src/sp-conn-end.cpp | 4 ++-- src/verbs.cpp | 4 ++-- 13 files changed, 52 insertions(+), 46 deletions(-) diff --git a/src/connector-context.cpp b/src/connector-context.cpp index 8e8c4b066..c982a8c89 100644 --- a/src/connector-context.cpp +++ b/src/connector-context.cpp @@ -1170,10 +1170,10 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item) if (cc->active_conn == item) { // Just adjust handle positions. - Geom::Point startpt = curve->first_point() * i2d; + Geom::Point startpt = *(curve->first_point()) * i2d; sp_knot_set_position(cc->endpt_handle[0], startpt, 0); - Geom::Point endpt = curve->last_point() * i2d; + Geom::Point endpt = *(curve->last_point()) * i2d; sp_knot_set_position(cc->endpt_handle[1], endpt, 0); return; @@ -1237,10 +1237,10 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item) G_CALLBACK(endpt_handler), cc); } - Geom::Point startpt = curve->first_point() * i2d; + Geom::Point startpt = *(curve->first_point()) * i2d; sp_knot_set_position(cc->endpt_handle[0], startpt, 0); - Geom::Point endpt = curve->last_point() * i2d; + Geom::Point endpt = *(curve->last_point()) * i2d; sp_knot_set_position(cc->endpt_handle[1], endpt, 0); sp_knot_show(cc->endpt_handle[0]); diff --git a/src/display/curve-test.h b/src/display/curve-test.h index 56b49ff4c..c0b0e900b 100644 --- a/src/display/curve-test.h +++ b/src/display/curve-test.h @@ -185,32 +185,34 @@ public: void testFirstPoint() { - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).first_point() , Geom::Point(0,0)); - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).first_point() , Geom::Point(2,0)); - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).first_point() , Geom::Point(4,0)); - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).first_point() , Geom::Point(3,5)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path1)).first_point()) , Geom::Point(0,0)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path2)).first_point()) , Geom::Point(2,0)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path3)).first_point()) , Geom::Point(4,0)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path4)).first_point()) , Geom::Point(3,5)); Geom::PathVector pv; + TS_ASSERT(!SPCurve(pv).first_point()); pv.push_back(path1); pv.push_back(path2); pv.push_back(path3); - TS_ASSERT_EQUALS(SPCurve(pv).first_point() , Geom::Point(0,0)); + TS_ASSERT_EQUALS(*(SPCurve(pv).first_point()) , Geom::Point(0,0)); pv.insert(pv.begin(), path4); - TS_ASSERT_EQUALS(SPCurve(pv).first_point() , Geom::Point(3,5)); + TS_ASSERT_EQUALS(*(SPCurve(pv).first_point()) , Geom::Point(3,5)); } void testLastPoint() { - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).last_point() , Geom::Point(0,0)); - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).last_point() , Geom::Point(2,0)); - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).last_point() , Geom::Point(8,4)); - TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).last_point() , Geom::Point(3,5)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path1)).last_point()) , Geom::Point(0,0)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path2)).last_point()) , Geom::Point(2,0)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path3)).last_point()) , Geom::Point(8,4)); + TS_ASSERT_EQUALS(*(SPCurve(Geom::PathVector(1, path4)).last_point()) , Geom::Point(3,5)); Geom::PathVector pv; + TS_ASSERT(!SPCurve(pv).last_point()); pv.push_back(path1); pv.push_back(path2); pv.push_back(path3); - TS_ASSERT_EQUALS(SPCurve(pv).last_point() , Geom::Point(8,4)); + TS_ASSERT_EQUALS(*(SPCurve(pv).last_point()) , Geom::Point(8,4)); pv.push_back(path4); - TS_ASSERT_EQUALS(SPCurve(pv).last_point() , Geom::Point(3,5)); + TS_ASSERT_EQUALS(*(SPCurve(pv).last_point()) , Geom::Point(3,5)); } void testSecondPoint() diff --git a/src/display/curve.cpp b/src/display/curve.cpp index 9f6c34981..8b1d977f5 100644 --- a/src/display/curve.cpp +++ b/src/display/curve.cpp @@ -360,15 +360,18 @@ SPCurve::first_path() const } /** - * Return first point of first subpath or (0,0). TODO: shouldn't this be (NR_HUGE, NR_HUGE) to be able to tell it apart from normal (0,0) ? + * Return first point of first subpath or nothing when the path is empty. */ -Geom::Point +boost::optional SPCurve::first_point() const { - if (is_empty()) - return Geom::Point(0, 0); + boost::optional retval; + + if (!is_empty()) { + retval = _pathv.front().initialPoint(); + } - return _pathv.front().initialPoint(); + return retval; } /** @@ -414,7 +417,7 @@ SPCurve::penultimate_point() const } /** - * Return last point of last subpath or (0,0). TODO: shouldn't this be (NR_HUGE, NR_HUGE) to be able to tell it apart from normal (0,0) ? + * Return last point of last subpath or nothing when the curve is empty. * If the last path is only a moveto, then return that point. */ boost::optional @@ -422,10 +425,11 @@ SPCurve::last_point() const { boost::optional retval; - if (is_empty()) - return Geom::Point(0, 0); + if (!is_empty()) { + retval = _pathv.back().finalPoint(); + } - return _pathv.back().finalPoint(); + return retval; } /** @@ -500,8 +504,8 @@ SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance) return this; } - if ( (fabs(this->last_point()[X] - c1->first_point()[X]) <= tolerance) - && (fabs(this->last_point()[Y] - c1->first_point()[Y]) <= tolerance) ) + if ( (fabs((*this->last_point())[X] - (*c1->first_point())[X]) <= tolerance) + && (fabs((*this->last_point())[Y] - (*c1->first_point())[Y]) <= tolerance) ) { // c1's first subpath can be appended to this curve's last subpath Geom::PathVector::const_iterator path_it = c1->_pathv.begin(); @@ -548,8 +552,8 @@ SPCurve::stretch_endpoints(Geom::Point const &new_p0, Geom::Point const &new_p1) return; } - Geom::Point const offset0( new_p0 - first_point() ); - Geom::Point const offset1( new_p1 - last_point() ); + Geom::Point const offset0( new_p0 - *first_point() ); + Geom::Point const offset1( new_p1 - *last_point() ); Geom::Piecewise > pwd2 = _pathv.front().toPwSb(); Geom::Piecewise arclength = Geom::arcLengthSb(pwd2); diff --git a/src/display/curve.h b/src/display/curve.h index 8bb3fb360..1b09c8c6e 100644 --- a/src/display/curve.h +++ b/src/display/curve.h @@ -48,8 +48,8 @@ public: Geom::Path const * last_path() const; Geom::Curve const * first_segment() const; Geom::Path const * first_path() const; - Geom::Point first_point() const; - Geom::Point last_point() const; + boost::optional first_point() const; + boost::optional last_point() const; boost::optional second_point() const; boost::optional penultimate_point() const; diff --git a/src/draw-context.cpp b/src/draw-context.cpp index 97fcb7be6..6f5d3e4e6 100644 --- a/src/draw-context.cpp +++ b/src/draw-context.cpp @@ -440,9 +440,9 @@ spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/) g_return_if_fail( c->get_segment_count() > 0 ); if ( !c->is_closed() ) { SPDrawAnchor *a; - a = sp_draw_anchor_new(dc, c, TRUE, c->first_point()); + a = sp_draw_anchor_new(dc, c, TRUE, *(c->first_point())); dc->white_anchors = g_slist_prepend(dc->white_anchors, a); - a = sp_draw_anchor_new(dc, c, FALSE, c->last_point()); + a = sp_draw_anchor_new(dc, c, FALSE, *(c->last_point())); dc->white_anchors = g_slist_prepend(dc->white_anchors, a); } } diff --git a/src/live_effects/lpe-copy_rotate.cpp b/src/live_effects/lpe-copy_rotate.cpp index 3451a10b6..dac34c5d4 100644 --- a/src/live_effects/lpe-copy_rotate.cpp +++ b/src/live_effects/lpe-copy_rotate.cpp @@ -76,8 +76,8 @@ LPECopyRotate::doOnApply(SPLPEItem *lpeitem) { SPCurve *curve = SP_SHAPE(lpeitem)->curve; - A = curve->first_point(); - B = curve->last_point(); + A = *(curve->first_point()); + B = *(curve->last_point()); origin.param_setValue(A); diff --git a/src/live_effects/lpe-offset.cpp b/src/live_effects/lpe-offset.cpp index 027eda23f..9ee53267c 100644 --- a/src/live_effects/lpe-offset.cpp +++ b/src/live_effects/lpe-offset.cpp @@ -41,7 +41,7 @@ LPEOffset::~LPEOffset() void LPEOffset::doOnApply(SPLPEItem *lpeitem) { - offset_pt.param_set_and_write_new_value(SP_SHAPE(lpeitem)->curve->first_point()); + offset_pt.param_set_and_write_new_value(*(SP_SHAPE(lpeitem)->curve->first_point())); } static void append_half_circle(Geom::Piecewise > &pwd2, diff --git a/src/live_effects/lpe-parallel.cpp b/src/live_effects/lpe-parallel.cpp index e169a3b8f..6b4a55504 100644 --- a/src/live_effects/lpe-parallel.cpp +++ b/src/live_effects/lpe-parallel.cpp @@ -67,8 +67,8 @@ LPEParallel::doOnApply (SPLPEItem *lpeitem) { SPCurve *curve = SP_SHAPE(lpeitem)->curve; - A = curve->first_point(); - B = curve->last_point(); + A = *(curve->first_point()); + B = *(curve->last_point()); dir = unit_vector(B - A); offset_pt.param_set_and_write_new_value((A + B)/2 + dir.ccw() * 100); diff --git a/src/live_effects/lpe-spiro.cpp b/src/live_effects/lpe-spiro.cpp index 44ba32e4d..dd6c5a90a 100644 --- a/src/live_effects/lpe-spiro.cpp +++ b/src/live_effects/lpe-spiro.cpp @@ -51,7 +51,7 @@ void bezctx_ink_quadto(bezctx *bc, double xm, double ym, double x3, double y3) double x1, y1; double x2, y2; - Geom::Point last = bi->curve->last_point(); + Geom::Point last = *(bi->curve->last_point()); x0 = last[Geom::X]; y0 = last[Geom::Y]; x1 = xm + (1./3) * (x0 - xm); diff --git a/src/pencil-context.cpp b/src/pencil-context.cpp index 4a7032e70..cd0f9be7f 100644 --- a/src/pencil-context.cpp +++ b/src/pencil-context.cpp @@ -592,7 +592,7 @@ static void spdc_finish_endpoint(SPPencilContext *const pc) { if ( ( pc->red_curve->is_empty() ) - || ( pc->red_curve->first_point() == *(pc->red_curve->second_point()) ) ) + || ( *(pc->red_curve->first_point()) == *(pc->red_curve->second_point()) ) ) { pc->red_curve->reset(); sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL); diff --git a/src/sp-conn-end-pair.cpp b/src/sp-conn-end-pair.cpp index 328f5a696..0753e1441 100644 --- a/src/sp-conn-end-pair.cpp +++ b/src/sp-conn-end-pair.cpp @@ -178,10 +178,10 @@ SPConnEndPair::getEndpoints(NR::Point endPts[]) const { else { if (h == 0) { - endPts[h] = curve->first_point(); + endPts[h] = *(curve->first_point()); } else { - endPts[h] = curve->last_point(); + endPts[h] = *(curve->last_point()); } } } diff --git a/src/sp-conn-end.cpp b/src/sp-conn-end.cpp index 947a88d71..f529d6d62 100644 --- a/src/sp-conn-end.cpp +++ b/src/sp-conn-end.cpp @@ -101,12 +101,12 @@ sp_conn_end_move_compensate(NR::Matrix const */*mp*/, SPItem */*moved_item*/, NR::Point other_endpt; NR::Point last_seg_pt; if (h2attItem[0] != NULL) { - other_endpt = path->curve->last_point(); + other_endpt = *(path->curve->last_point()); last_seg_pt = *(path->curve->second_point()); ind = 0; } else { - other_endpt = path->curve->first_point(); + other_endpt = *(path->curve->first_point()); last_seg_pt = *(path->curve->penultimate_point()); ind = 1; } diff --git a/src/verbs.cpp b/src/verbs.cpp index 8d7a4a2a1..f96d0a4f0 100644 --- a/src/verbs.cpp +++ b/src/verbs.cpp @@ -1607,7 +1607,7 @@ ZoomVerb::perform(SPAction *action, void *data, void */*pdata*/) if (tools_isactive(dt, TOOLS_FREEHAND_PENCIL) || tools_isactive(dt, TOOLS_FREEHAND_PEN)) { SPCurve *rc = SP_DRAW_CONTEXT(ec)->red_curve; if (!rc->is_empty()) { - NR::Point const zoom_to (rc->last_point()); + Geom::Point const zoom_to (*rc->last_point()); dt->zoom_relative_keep_point(zoom_to, mul*zoom_inc); break; } @@ -1625,7 +1625,7 @@ ZoomVerb::perform(SPAction *action, void *data, void */*pdata*/) if (tools_isactive(dt, TOOLS_FREEHAND_PENCIL) || tools_isactive(dt, TOOLS_FREEHAND_PEN)) { SPCurve *rc = SP_DRAW_CONTEXT(ec)->red_curve; if (!rc->is_empty()) { - NR::Point const zoom_to (rc->last_point()); + Geom::Point const zoom_to (*rc->last_point()); dt->zoom_relative_keep_point(zoom_to, 1 / (mul*zoom_inc)); break; } -- 2.30.2