find max element instead of std::sort

This commit is contained in:
Jiri Drbalek 2016-12-02 16:15:07 +00:00 committed by Mickey Rose
parent cdefee3524
commit b5c04cc132
3 changed files with 31 additions and 10 deletions

View file

@ -116,6 +116,7 @@ public:
bool valid() const; bool valid() const;
void move(T x, T y); void move(T x, T y);
std::string to_string() const; std::string to_string() const;
T area() const;
// define some operators // define some operators
box2d_type& operator+=(box2d_type const& other); box2d_type& operator+=(box2d_type const& other);

View file

@ -393,6 +393,11 @@ std::string box2d<T>::to_string() const
return s.str(); return s.str();
} }
template <typename T>
T box2d<T>::area() const
{
return width() * height();
}
template <typename T> template <typename T>
box2d<T>& box2d<T>::operator+=(box2d<T> const& other) box2d<T>& box2d<T>::operator+=(box2d<T> const& other)

View file

@ -187,17 +187,26 @@ base_symbolizer_helper::base_symbolizer_helper(
initialize_points(); initialize_points();
} }
struct largest_bbox_first template <typename It>
static It largest_bbox(It begin, It end)
{ {
bool operator() (base_symbolizer_helper::geometry_cref const& g0, if (begin == end)
base_symbolizer_helper::geometry_cref const& g1) const
{ {
// TODO - this has got to be expensive! Can we cache bbox's if there are repeated calls to same geom? return end;
box2d<double> b0 = geometry::envelope(g0); }
box2d<double> b1 = geometry::envelope(g1); It largest_geom = begin;
return b0.width() * b0.height() > b1.width() * b1.height(); 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 void base_symbolizer_helper::initialize_geometries() const
{ {
@ -210,10 +219,16 @@ void base_symbolizer_helper::initialize_geometries() const
type == geometry::geometry_types::MultiPolygon) type == geometry::geometry_types::MultiPolygon)
{ {
bool largest_box_only = text_props_->largest_bbox_only; 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(); 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()); geometries_to_process_.erase(++geo_itr_, geometries_to_process_.end());
} }
} }