From 4cdc3428bdad8a09b8f6a4c32e2dabdc1b5b287a Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Fri, 2 Dec 2016 15:55:50 +0000 Subject: [PATCH 1/2] remove redundant code --- src/text/symbolizer_helpers.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/text/symbolizer_helpers.cpp b/src/text/symbolizer_helpers.cpp index 384a7fe96..b0a2430a1 100644 --- a/src/text/symbolizer_helpers.cpp +++ b/src/text/symbolizer_helpers.cpp @@ -189,12 +189,6 @@ base_symbolizer_helper::base_symbolizer_helper( struct largest_bbox_first { - bool operator() (geometry::geometry const* g0, geometry::geometry const* g1) const - { - box2d b0 = geometry::envelope(*g0); - box2d b1 = geometry::envelope(*g1); - return b0.width() * b0.height() > b1.width() * b1.height(); - } bool operator() (base_symbolizer_helper::geometry_cref const& g0, base_symbolizer_helper::geometry_cref const& g1) const { From 2174c10b2fcfc8d79421e7d22fc6024f9a3b2d9f Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Fri, 2 Dec 2016 16:15:07 +0000 Subject: [PATCH 2/2] find max element instead of std::sort --- include/mapnik/box2d.hpp | 1 + include/mapnik/box2d_impl.hpp | 5 +++++ src/text/symbolizer_helpers.cpp | 35 +++++++++++++++++++++++---------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/include/mapnik/box2d.hpp b/include/mapnik/box2d.hpp index c6c794c83..626aebde2 100644 --- a/include/mapnik/box2d.hpp +++ b/include/mapnik/box2d.hpp @@ -116,6 +116,7 @@ public: bool valid() const; void move(T x, T y); std::string to_string() const; + T area() const; // define some operators box2d_type& operator+=(box2d_type const& other); diff --git a/include/mapnik/box2d_impl.hpp b/include/mapnik/box2d_impl.hpp index b1c462d16..232a25d58 100644 --- a/include/mapnik/box2d_impl.hpp +++ b/include/mapnik/box2d_impl.hpp @@ -407,6 +407,11 @@ std::string box2d::to_string() const return s.str(); } +template +T box2d::area() const +{ + return width() * height(); +} template box2d& box2d::operator+=(box2d const& other) diff --git a/src/text/symbolizer_helpers.cpp b/src/text/symbolizer_helpers.cpp index b0a2430a1..300d91938 100644 --- a/src/text/symbolizer_helpers.cpp +++ b/src/text/symbolizer_helpers.cpp @@ -187,17 +187,26 @@ base_symbolizer_helper::base_symbolizer_helper( initialize_points(); } -struct largest_bbox_first +template +static It largest_bbox(It begin, It end) { - bool operator() (base_symbolizer_helper::geometry_cref const& g0, - base_symbolizer_helper::geometry_cref const& g1) const + if (begin == end) { - // TODO - this has got to be expensive! Can we cache bbox's if there are repeated calls to same geom? - box2d b0 = geometry::envelope(g0); - box2d b1 = geometry::envelope(g1); - return b0.width() * b0.height() > b1.width() * b1.height(); + return end; } -}; + It largest_geom = begin; + double largest_bbox = geometry::envelope(*largest_geom).area(); + for (++begin; begin != end; ++begin) + { + double bbox = geometry::envelope(*begin).area(); + if (bbox > largest_bbox) + { + largest_bbox = bbox; + largest_geom = begin; + } + } + return largest_geom; +} void base_symbolizer_helper::initialize_geometries() const { @@ -210,10 +219,16 @@ void base_symbolizer_helper::initialize_geometries() const type == geometry::geometry_types::MultiPolygon) { bool largest_box_only = text_props_->largest_bbox_only; - if (largest_box_only) + if (largest_box_only && geometries_to_process_.size() > 1) { - geometries_to_process_.sort(largest_bbox_first()); + auto largest_geom = largest_bbox( + geometries_to_process_.begin(), + geometries_to_process_.end()); geo_itr_ = geometries_to_process_.begin(); + if (geo_itr_ != largest_geom) + { + std::swap(*geo_itr_, *largest_geom); + } geometries_to_process_.erase(++geo_itr_, geometries_to_process_.end()); } }