Code

return boost::optional for second and penultimate points of SPCurve
authorjohanengelen <johanengelen@users.sourceforge.net>
Wed, 13 Aug 2008 18:49:22 +0000 (18:49 +0000)
committerjohanengelen <johanengelen@users.sourceforge.net>
Wed, 13 Aug 2008 18:49:22 +0000 (18:49 +0000)
src/display/curve-test.h
src/display/curve.cpp
src/display/curve.h
src/pencil-context.cpp
src/sp-conn-end.cpp

index a45f01afdcc05e50398238574d4dbe88b4696ca3..56b49ff4ceeee1be66e05cc54e593e6ba039c521 100644 (file)
@@ -215,32 +215,32 @@ public:
 
     void testSecondPoint()
     {
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).second_point() , Geom::Point(1,0));
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).second_point() , Geom::Point(3,0));
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).second_point() , Geom::Point(5,1));
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).second_point() , Geom::Point(3,5));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path1)).second_point()) , Geom::Point(1,0));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path2)).second_point()) , Geom::Point(3,0));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path3)).second_point()) , Geom::Point(5,1));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path4)).second_point()) , Geom::Point(3,5));
         Geom::PathVector pv;
         pv.push_back(path1);
         pv.push_back(path2);
         pv.push_back(path3);
-        TS_ASSERT_EQUALS(SPCurve(pv).second_point() , Geom::Point(1,0));
+        TS_ASSERT_EQUALS( *(SPCurve(pv).second_point()) , Geom::Point(1,0));
         pv.insert(pv.begin(), path4);
-        TS_ASSERT_EQUALS(SPCurve(pv).second_point() , Geom::Point(0,0));
+        TS_ASSERT_EQUALS( SPCurve(pv).second_point() == false, true );
     }
 
     void testPenultimatePoint()
     {
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path1)).penultimate_point() , Geom::Point(1,1));
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path2)).penultimate_point() , Geom::Point(3,0));
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path3)).penultimate_point() , Geom::Point(6,4));
-        TS_ASSERT_EQUALS(SPCurve(Geom::PathVector(1, path4)).penultimate_point() , Geom::Point(3,5));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path1)).penultimate_point()) , Geom::Point(1,1));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path2)).penultimate_point()) , Geom::Point(3,0));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path3)).penultimate_point()) , Geom::Point(6,4));
+        TS_ASSERT_EQUALS( *(SPCurve(Geom::PathVector(1, path4)).penultimate_point()) , Geom::Point(3,5));
         Geom::PathVector pv;
         pv.push_back(path1);
         pv.push_back(path2);
         pv.push_back(path3);
-        TS_ASSERT_EQUALS(SPCurve(pv).penultimate_point() , Geom::Point(6,4));
+        TS_ASSERT_EQUALS( *(SPCurve(pv).penultimate_point()) , Geom::Point(6,4));
         pv.push_back(path4);
-        TS_ASSERT_EQUALS(SPCurve(pv).penultimate_point() , Geom::Point(8,4));
+        TS_ASSERT_EQUALS( *(SPCurve(pv).penultimate_point()) , Geom::Point(8,4));
     }
 
     // TODO: Rest of the methods
index d142b6eac19e793d3d76fc3261cddb1f40f8db61..9f6c34981e4aad37b21327949afb2bc926108940 100644 (file)
@@ -373,48 +373,55 @@ SPCurve::first_point() const
 
 /**
  * Return the second point of first subpath or _movePos if curve too short.
- * If the pathvector is empty, this returns (0,0). If the first path is only a moveto, this method
+ * If the pathvector is empty, this returns nothing. If the first path is only a moveto, this method
  * returns the first point of the second path, if it exists. If there is no 2nd path, it returns the
  * first point of the first path.
- *
- * FIXME: for empty paths shouldn't this return (NR_HUGE,NR_HUGE)
  */
-Geom::Point
+boost::optional<Geom::Point>
 SPCurve::second_point() const
 {
-    if (is_empty()) {
-        return Geom::Point(0,0);
-    }
-    else if (_pathv.front().empty()) {
-        // first path is only a moveto
-        // check if there is second path
-        if (_pathv.size() > 1) {
-            return _pathv[1].initialPoint();
+    boost::optional<Geom::Point> retval;
+    if (!is_empty()) {
+        if (_pathv.front().empty()) {
+            // first path is only a moveto
+            // check if there is second path
+            if (_pathv.size() > 1) {
+                retval = _pathv[1].initialPoint();
+            } else {
+                retval = _pathv[0].initialPoint();
+            }
         } else {
-            return _pathv[0].initialPoint();
+            retval = _pathv.front()[0].finalPoint();
         }
     }
-    else
-        return _pathv.front()[0].finalPoint();
+
+    return retval;
 }
 
 /**
  * TODO: fix comment: Return the second-last point of last subpath or _movePos if curve too short.
  */
-Geom::Point
+boost::optional<Geom::Point>
 SPCurve::penultimate_point() const
 {
-    Geom::Curve const& back = _pathv.back().back_default();
-    return back.initialPoint();
+    boost::optional<Geom::Point> retval;
+    if (!is_empty()) {
+        Geom::Curve const& back = _pathv.back().back_default();
+        retval = back.initialPoint();
+    }
+
+    return retval;
 }
 
 /**
  * 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) ?
  * If the last path is only a moveto, then return that point.
  */
-Geom::Point
+boost::optional<Geom::Point>
 SPCurve::last_point() const
 {
+    boost::optional<Geom::Point> retval;
+
     if (is_empty())
         return Geom::Point(0, 0);
 
index c3659de078d9616a5d9c95ebb2ac3ddd5f7a21a8..8bb3fb36012aed80f0ed327530aa5f6743454734 100644 (file)
@@ -20,6 +20,8 @@
 
 #include <2geom/forward.h>
 
+#include <boost/optional.hpp>
+
 class SPCurve {
 public:
     /* Constructors */
@@ -48,8 +50,8 @@ public:
     Geom::Path const * first_path() const;
     Geom::Point first_point() const;
     Geom::Point last_point() const;
-    Geom::Point second_point() const;
-    Geom::Point penultimate_point() const;
+    boost::optional<Geom::Point> second_point() const;
+    boost::optional<Geom::Point> penultimate_point() const;
 
     void reset();
 
index 2c97beafe57afa3ac6b77feef9aa0ede70362660..4a7032e707a0d37862bd79abdebd42d1ee76a75e 100644 (file)
@@ -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);
index 40017912fc969afe0ef82d7c1c18ff7b6d39a7e6..947a88d714afc55e3c02e744d34472afe4c5890a 100644 (file)
@@ -68,8 +68,8 @@ sp_conn_end_move_compensate(NR::Matrix const */*mp*/, SPItem */*moved_item*/,
         NR::Matrix h2i2anc[2];
         NR::Rect h2bbox_icoordsys[2];
         NR::Point last_seg_endPt[2] = {
-            path->curve->second_point(),
-            path->curve->penultimate_point()
+            *(path->curve->second_point()),
+            *(path->curve->penultimate_point())
         };
         for (unsigned h = 0; h < 2; ++h) {
             boost::optional<NR::Rect> bbox = h2attItem[h]->getBounds(NR::identity());
@@ -102,12 +102,12 @@ sp_conn_end_move_compensate(NR::Matrix const */*mp*/, SPItem */*moved_item*/,
         NR::Point last_seg_pt;
         if (h2attItem[0] != NULL) {
             other_endpt = path->curve->last_point();
-            last_seg_pt = path->curve->second_point();
+            last_seg_pt = *(path->curve->second_point());
             ind = 0;
         }
         else {
             other_endpt = path->curve->first_point();
-            last_seg_pt = path->curve->penultimate_point();
+            last_seg_pt = *(path->curve->penultimate_point());
             ind = 1;
         }
         NR::Point h2endPt_icoordsys[2];