From cdefee3524ed510ab61cbacda939c1e325520323 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 fe5568ad2..80e4b2dd2 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 b5c04cc132b9a9d29e983d53e815a384c2e7ea6c 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 267120111..61652839d 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 7f3e9d1e7..4f3117cd1 100644 --- a/include/mapnik/box2d_impl.hpp +++ b/include/mapnik/box2d_impl.hpp @@ -393,6 +393,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 80e4b2dd2..8064e6c6f 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()); } }