]> git.tdb.fi Git - r2c2.git/commitdiff
Create track length map in ExtendTool constructor
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Oct 2013 11:58:59 +0000 (14:58 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Oct 2013 11:58:59 +0000 (14:58 +0300)
source/designer/extendtool.cpp
source/designer/extendtool.h

index 2a66181f32772c44557aef23fc64cd3479a65c27..021f184a8fb89a4393a9b8dfa6525691ad0da426 100644 (file)
@@ -8,7 +8,8 @@ using namespace Msp;
 using namespace R2C2;
 
 ExtendTool::ExtendTool(Designer &d, Input::Keyboard &k, Input::Mouse &m, const set<Object *> &objects):
-       Tool(d, k, m)
+       Tool(d, k, m),
+       max_preference(0)
 {
        for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
                if(Track *t = dynamic_cast<Track *>(*i))
@@ -24,6 +25,20 @@ ExtendTool::ExtendTool(Designer &d, Input::Keyboard &k, Input::Mouse &m, const s
                done = true;
                set_status("No free endpoints");
        }
+
+       const Catalogue::ObjectMap &object_types = designer.get_catalogue().get_all();
+       for(Catalogue::ObjectMap::const_iterator i=object_types.begin(); i!=object_types.end(); ++i)
+               if(const TrackType *tt = dynamic_cast<const TrackType *>(i->second))
+               {
+                       const vector<TrackPart> &parts = tt->get_parts();
+                       if(parts.size()!=1)
+                               continue;
+                       if(parts.front().is_curved() || parts.front().is_dead_end())
+                               continue;
+
+                       types_by_length[parts.front().get_length()] = tt;
+                       max_preference = max(max_preference, tt->get_autofit_preference());
+               }
 }
 
 ExtendTool::~ExtendTool()
@@ -154,23 +169,8 @@ void ExtendTool::finish()
 
 vector<Track *> ExtendTool::create_straight(const Vector &start, const Angle &dir, float length, float limit)
 {
-       const Catalogue::ObjectMap &object_types = designer.get_catalogue().get_all();
-       map<float, const TrackType *> types_by_length;
-       unsigned preference = 0;
-       for(Catalogue::ObjectMap::const_iterator i=object_types.begin(); i!=object_types.end(); ++i)
-               if(const TrackType *tt = dynamic_cast<const TrackType *>(i->second))
-               {
-                       const vector<TrackPart> &parts = tt->get_parts();
-                       if(parts.size()!=1)
-                               continue;
-                       if(parts.front().is_curved() || parts.front().is_dead_end())
-                               continue;
-
-                       types_by_length[parts.front().get_length()] = tt;
-                       preference = max(preference, tt->get_autofit_preference());
-               }
-
        vector<float> lengths;
+       unsigned preference = max_preference;
        float removed = 0;
        while(length>limit)
        {
index 69e184aceae37afc7ce3837725d9b0bfc3535ed9..866d258a68e6e4f72b6ac5b1c99e187e2b909b82 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef EXTENDTOOL_H_
 #define EXTENDTOOL_H_
 
+#include <map>
 #include <set>
 #include <vector>
 #include "libr2c2/object.h"
@@ -10,6 +11,8 @@
 class ExtendTool: public Tool
 {
 private:
+       std::map<float, const R2C2::TrackType *> types_by_length;
+       unsigned max_preference;
        std::vector<R2C2::TrackIter> unlinked_endpoints;
        std::vector<R2C2::Track *> extend_tracks;