Code

avoid code duplication, i.e. use sp_item_snappoints in the object-snapper.cpp
authordvlierop2 <dvlierop2@users.sourceforge.net>
Sat, 15 Sep 2007 08:19:07 +0000 (08:19 +0000)
committerdvlierop2 <dvlierop2@users.sourceforge.net>
Sat, 15 Sep 2007 08:19:07 +0000 (08:19 +0000)
src/object-snapper.cpp
src/object-snapper.h
src/selection.cpp
src/sp-item-group.cpp
src/sp-item-notify-moveto.cpp
src/sp-item-rm-unsatisfied-cns.cpp
src/sp-item-update-cns.cpp
src/sp-item.cpp
src/sp-item.h

index ab222f286a44935e4167806f7f4c585c09e42569..53f04f8c42986497fd3c7ecfbd1de66a9568205d 100644 (file)
@@ -31,9 +31,9 @@ Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
     _snap_to_bboxnode(true), _snap_to_bboxpath(true), _strict_snapping(true),
     _include_item_center(false)
 {
-       _candidates = new std::list<SPItem*>;
-       _points_to_snap_to = new std::list<NR::Point>;
-       _paths_to_snap_to = new std::list<Path*>;
+       _candidates = new std::vector<SPItem*>;
+       _points_to_snap_to = new std::vector<NR::Point>;
+       _paths_to_snap_to = new std::vector<Path*>;
 }
 
 Inkscape::ObjectSnapper::~ObjectSnapper() 
@@ -44,7 +44,7 @@ Inkscape::ObjectSnapper::~ObjectSnapper()
        _points_to_snap_to->clear();
        delete _points_to_snap_to;
        
-       for (std::list<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
+       for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
                delete *k;
     }
     _paths_to_snap_to->clear();
@@ -143,44 +143,19 @@ void Inkscape::ObjectSnapper::_snapNodes(Inkscape::Snapper::PointType const &t,
     // first point and store the collection for later use. This dramatically improves the performance
        if (first_point) {
                _points_to_snap_to->clear();
-           for (std::list<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
+           for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
                
-               NR::Matrix i2doc(NR::identity());
-               SPItem *root_item = NULL;
+               //NR::Matrix i2doc(NR::identity());
+               SPItem *root_item = *i;
                if (SP_IS_USE(*i)) {
-                   i2doc = sp_use_get_root_transform(SP_USE(*i));
                    root_item = sp_use_root(SP_USE(*i));
-               } else { 
-                   i2doc = sp_item_i2doc_affine(*i);
-                   root_item = *i;
-               }
-               
-               SPCurve *curve = NULL;
-               
-               if (SP_IS_SHAPE(root_item)) {
-                   SPShape const *sh = SP_SHAPE(root_item);
-                   curve = sh->curve;
-               } else if (SP_IS_IMAGE(root_item)) {
-                   SPImage const *im = SP_IMAGE(root_item);
-                   curve = im->curve;
                }
-                   
+                 
                        //Collect all nodes so we can snap to them
                if (_snap_to_itemnode) {
                        if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
-                               if (curve) {
-                                   int j = 0;
-                                   while (SP_CURVE_BPATH(curve)[j].code != NR_END) {        
-                                       /* Get this node in desktop coordinates */
-                                       NArtBpath const &bp = SP_CURVE_BPATH(curve)[j];
-                                       _points_to_snap_to->push_back(desktop->doc2dt(bp.c(3) * i2doc));
-                                       j++;
-                                   }
-                                   if (_include_item_center) {
-                                       _points_to_snap_to->push_back(root_item->getCenter());  
-                                   }                       
-                               }
-                       }
+                               sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(*_points_to_snap_to));
+                               }
                }
                
                //Collect the bounding box's corners so we can snap to them
@@ -198,7 +173,7 @@ void Inkscape::ObjectSnapper::_snapNodes(Inkscape::Snapper::PointType const &t,
        }
     
     //Do the snapping, using all the nodes and corners collected above
-    for (std::list<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
+    for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
            /* Try to snap to this node of the path */
         NR::Coord dist = NR_HUGE;
         NR::Point snapped_point;
@@ -216,6 +191,7 @@ void Inkscape::ObjectSnapper::_snapNodes(Inkscape::Snapper::PointType const &t,
                        snapped_point = *k;
                        break;
         }
+        
         if (dist < getDistance() && dist < s.getDistance()) {
             s = SnappedPoint(snapped_point, dist);
         }       
@@ -248,11 +224,11 @@ void Inkscape::ObjectSnapper::_snapPaths(Inkscape::Snapper::PointType const &t,
     // e.g. when translating an item using the selector tool, then we will only do this for the
     // first point and store the collection for later use. This dramatically improves the performance
     if (first_point) {     
-           for (std::list<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
+           for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
                delete *k;
            }
            _paths_to_snap_to->clear();
-           for (std::list<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
+           for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
        
                /* Transform the requested snap point to this item's coordinates */
                NR::Matrix i2doc(NR::identity());
@@ -277,8 +253,7 @@ void Inkscape::ObjectSnapper::_snapPaths(Inkscape::Snapper::PointType const &t,
                                        bool very_lenghty_prose = false;
                                        if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
                                                very_lenghty_prose =  sp_text_get_length(SP_TEXT(root_item)) > 240; 
-                                       }       
-                                       
+                                       }
                                        // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
                                        // which corresponds to a lag of 500 msec. This is for snapping a rect
                                        // to a single line of text. 
@@ -320,9 +295,9 @@ void Inkscape::ObjectSnapper::_snapPaths(Inkscape::Snapper::PointType const &t,
                }
            }
     }
-        
+
     //Now we can finally do the real snapping, using the paths collected above        
-    for (std::list<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
+    for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
         if (*k) {
             if (first_point) {
                (*k)->ConvertWithBackData(0.01); //This is extremely time consuming!
@@ -398,7 +373,7 @@ Inkscape::SnappedPoint Inkscape::ObjectSnapper::guideSnap(NR::Point const &p,
     }
 
     /* Get a list of all the SPItems that we will try to snap to */
-    std::list<SPItem*> cand;
+    std::vector<SPItem*> cand;
     std::list<SPItem const *> const it; //just an empty list
     
     std::vector<NR::Point> points_to_snap;
index fa0cfa14c294c69cb3e1dcd9b843e2d9d2f8f1c5..e473e3fe90dfb1655d7eb7ba8ad35943afe4337c 100644 (file)
@@ -85,10 +85,9 @@ public:
   
 private:
   //store some lists of candidates, points and paths, so we don't have to rebuild them for each point we want to snap
-  std::list<SPItem*> *_candidates; 
-  std::list<NR::Point> *_points_to_snap_to;
-  std::list<Path*> *_paths_to_snap_to;
-  
+  std::vector<SPItem*> *_candidates; 
+  std::vector<NR::Point> *_points_to_snap_to;
+  std::vector<Path*> *_paths_to_snap_to;
   SnappedPoint _doFreeSnap(Inkscape::Snapper::PointType const &t,
                                        NR::Point const &p,
                                        bool const &first_point,
index ca3de5927e7c41705de52a7df30b384719cabc5f..af5db825c0c19efc905bfef8a4559a298fa10da9 100644 (file)
@@ -381,7 +381,7 @@ std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
         if (!SP_IS_PATH(this_item)) {
             // Only snap if we don't have a path at hand
             // (Same check occurs in sp-item-group)
-            sp_item_snappoints(this_item, SnapPointsIter(p));
+            sp_item_snappoints(this_item, false, SnapPointsIter(p));
         }
         //Include the transformation origin for snapping
         //For a group only the group's origin is considered
@@ -398,7 +398,7 @@ std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
 
     std::vector<NR::Point> p;
     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
-       sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
+               sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
     }
 
     std::vector<NR::Point> pHull;
index f43f4ded952fd4c9f0be6a3a41969fa5a98f5b10..f1035e9eef6028a234a0eac680c841f90ed6808a 100644 (file)
@@ -301,7 +301,7 @@ static void sp_group_snappoints (SPItem const *item, SnapPointsIter p)
                if (SP_IS_ITEM(o) && !SP_IS_PATH(o)) {
             // getSnapPoints() and sp_group_snappoints are only being used in the selector tool,
             // which should not snap path nodes. Only the node tool should snap those.
-            sp_item_snappoints(SP_ITEM(o), p);
+            sp_item_snappoints(SP_ITEM(o), false, p);
                }
        }
 }
index 163a9f5f81cfd181b2cd85e5089757fa6c318ec2..7781b15f0350b30c5fd937db21a792b4a11a900d 100644 (file)
@@ -25,7 +25,7 @@ void sp_item_notify_moveto(SPItem &item, SPGuide const &mv_g, int const snappoin
     g_return_if_fail( dir_lensq != 0 );
 
     vector<NR::Point> snappoints;
-    sp_item_snappoints(&item, SnapPointsIter(snappoints));
+    sp_item_snappoints(&item, true, SnapPointsIter(snappoints));
     g_return_if_fail( snappoint_ix < int(snappoints.size()) );
 
     double const pos0 = dot(dir, snappoints[snappoint_ix]);
index d71c6fe0ccf5aaca614fe35581baed9ca6e13be4..4071a639c6da560f615fc738c2baedbb45c7f370 100644 (file)
@@ -12,7 +12,7 @@ void sp_item_rm_unsatisfied_cns(SPItem &item)
         return;
     }
     vector<NR::Point> snappoints;
-    sp_item_snappoints(&item, SnapPointsIter(snappoints));
+    sp_item_snappoints(&item, true, SnapPointsIter(snappoints));
     for (unsigned i = item.constraints.size(); i--;) {
         g_assert( i < item.constraints.size() );
         SPGuideConstraint const &cn = item.constraints[i];
index 528129fb999859411d22f191888365441d4e7d90..44b26d45f6e2998689f774781216d08b142dbb9c 100644 (file)
@@ -10,7 +10,7 @@ using std::vector;
 void sp_item_update_cns(SPItem &item, SPDesktop const &desktop)
 {
     vector<NR::Point> snappoints;
-    sp_item_snappoints(&item, SnapPointsIter(snappoints));
+    sp_item_snappoints(&item, true, SnapPointsIter(snappoints));
     /* TODO: Implement the ordering. */
     vector<SPGuideConstraint> found_cns;
     satisfied_guide_cns(desktop, snappoints, found_cns);
index 4a5c0079b4fb6bd1499083cd63ad09d3f9eb5b92..8f91c97e650c80db84fb57a0aadc9ae23f312741 100644 (file)
@@ -785,7 +785,7 @@ static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
     }
 }
 
-void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
+void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
 {
     g_assert (item != NULL);
     g_assert (SP_IS_ITEM(item));
@@ -794,6 +794,10 @@ void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
     if (item_class.snappoints) {
         item_class.snappoints(item, p);
     }
+    
+    if (includeItemCenter) {
+       *p = item->getCenter();
+    }
 }
 
 void
index fbd4d6818c52c138cf39deb36b81be90b5065f3c..b3867f146156ca4b53f960b46ff537c58488bed3 100644 (file)
@@ -221,7 +221,7 @@ unsigned int sp_item_display_key_new(unsigned int numkeys);
 NRArenaItem *sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
 void sp_item_invoke_hide(SPItem *item, unsigned int key);
 
-void sp_item_snappoints(SPItem const *item, SnapPointsIter p);
+void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p);
 
 void sp_item_adjust_pattern(SPItem *item, /* NR::Matrix const &premul, */ NR::Matrix const &postmul, bool set = false);
 void sp_item_adjust_gradient(SPItem *item, /* NR::Matrix const &premul, */ NR::Matrix const &postmul, bool set = false);