From a953d535adf46da4cad6a8824de881ec248b8d88 Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sat, 28 Jul 2012 16:34:44 +0200 Subject: [PATCH] Use pixel_position consistently everywhere. --- bindings/python/mapnik_shield_symbolizer.cpp | 10 +++--- bindings/python/mapnik_text_placement.cpp | 4 +-- include/mapnik/box2d.hpp | 1 + include/mapnik/pixel_position.hpp | 37 ++++++++++++++++++++ include/mapnik/shield_symbolizer.hpp | 4 +-- include/mapnik/symbolizer_helpers.hpp | 19 ++++------ include/mapnik/text/placement_finder_ng.hpp | 2 +- include/mapnik/text_properties.hpp | 4 +-- include/mapnik/text_symbolizer.hpp | 4 +-- src/save_map.cpp | 10 +++--- src/shield_symbolizer.cpp | 4 +-- src/symbolizer_helpers.cpp | 36 +++++++++---------- src/text/placement_finder_ng.cpp | 34 ++++++++++-------- src/text_placements/simple.cpp | 20 +++++------ src/text_properties.cpp | 12 +++---- src/text_symbolizer.cpp | 6 ++-- 16 files changed, 119 insertions(+), 88 deletions(-) diff --git a/bindings/python/mapnik_shield_symbolizer.cpp b/bindings/python/mapnik_shield_symbolizer.cpp index 28ae7083e..913fbedb2 100644 --- a/bindings/python/mapnik_shield_symbolizer.cpp +++ b/bindings/python/mapnik_shield_symbolizer.cpp @@ -39,7 +39,7 @@ using mapnik::path_expression_ptr; using mapnik::guess_type; using mapnik::expression_ptr; using mapnik::parse_path; -using mapnik::position; +using mapnik::pixel_position; namespace { @@ -47,8 +47,8 @@ using namespace boost::python; tuple get_shield_displacement(const shield_symbolizer& s) { - position const& pos = s.get_shield_displacement(); - return boost::python::make_tuple(pos.first, pos.second); + pixel_position const& pos = s.get_shield_displacement(); + return boost::python::make_tuple(pos.x, pos.y); } void set_shield_displacement(shield_symbolizer & s, boost::python::tuple arg) @@ -58,8 +58,8 @@ void set_shield_displacement(shield_symbolizer & s, boost::python::tuple arg) tuple get_text_displacement(const shield_symbolizer& t) { - position const& pos = t.get_displacement(); - return boost::python::make_tuple(pos.first, pos.second); + pixel_position const& pos = t.get_displacement(); + return boost::python::make_tuple(pos.x, pos.y); } void set_text_displacement(shield_symbolizer & t, boost::python::tuple arg) diff --git a/bindings/python/mapnik_text_placement.cpp b/bindings/python/mapnik_text_placement.cpp index f91a985f6..d4bcdb5bc 100644 --- a/bindings/python/mapnik_text_placement.cpp +++ b/bindings/python/mapnik_text_placement.cpp @@ -56,7 +56,7 @@ using namespace boost::python; boost::python::tuple get_displacement(text_symbolizer_properties const& t) { - return boost::python::make_tuple(t.displacement.first, t.displacement.second); + return boost::python::make_tuple(t.displacement.x, t.displacement.y); } void set_displacement(text_symbolizer_properties &t, boost::python::tuple arg) @@ -72,7 +72,7 @@ void set_displacement(text_symbolizer_properties &t, boost::python::tuple arg) double x = extract(arg[0]); double y = extract(arg[1]); - t.displacement = std::make_pair(x, y); + t.displacement.set(x, y); } diff --git a/include/mapnik/box2d.hpp b/include/mapnik/box2d.hpp index fa52573fb..828106901 100644 --- a/include/mapnik/box2d.hpp +++ b/include/mapnik/box2d.hpp @@ -26,6 +26,7 @@ // mapnik #include #include +#include // boost #include diff --git a/include/mapnik/pixel_position.hpp b/include/mapnik/pixel_position.hpp index db75d7fb8..ee599c498 100644 --- a/include/mapnik/pixel_position.hpp +++ b/include/mapnik/pixel_position.hpp @@ -22,6 +22,9 @@ #ifndef MAPNIK_PIXEL_POSITION_HPP #define MAPNIK_PIXEL_POSITION_HPP +namespace mapnik +{ + /** Store a pixel position. */ struct pixel_position { @@ -29,6 +32,40 @@ struct pixel_position double y; pixel_position(double x, double y) : x(x), y(y) { } pixel_position() : x(0), y(0) { } + pixel_position operator+ (pixel_position const& other) + { + return pixel_position(x + other.x, y + other.y); + } + + pixel_position operator- (pixel_position const& other) + { + return pixel_position(x - other.x, y - other.y); + } + + pixel_position operator* (double other) + { + return pixel_position(x * other, y * other); + } + + void set(double x_, double y_) + { + x = x_; + y = y_; + } + + void clear() + { + x = 0; + y = 0; + } }; +inline pixel_position operator* (double factor, pixel_position const& pos) +{ + return pixel_position(factor * pos.x, factor * pos.y); +} + +} + + #endif // MAPNIK_PIXEL_POSITION_HPP diff --git a/include/mapnik/shield_symbolizer.hpp b/include/mapnik/shield_symbolizer.hpp index 85322f48e..15f1124e5 100644 --- a/include/mapnik/shield_symbolizer.hpp +++ b/include/mapnik/shield_symbolizer.hpp @@ -52,11 +52,11 @@ struct MAPNIK_DECL shield_symbolizer : public text_symbolizer, bool get_unlock_image() const; // image is not locked to the text placement void set_unlock_image(bool unlock_image); void set_shield_displacement(double shield_dx, double shield_dy); - position const& get_shield_displacement() const; + pixel_position const& get_shield_displacement() const; private: bool unlock_image_; - position shield_displacement_; + pixel_position shield_displacement_; }; } diff --git a/include/mapnik/symbolizer_helpers.hpp b/include/mapnik/symbolizer_helpers.hpp index 801025f4b..32183f832 100644 --- a/include/mapnik/symbolizer_helpers.hpp +++ b/include/mapnik/symbolizer_helpers.hpp @@ -84,9 +84,9 @@ protected: /** Geometry currently being processed. */ std::list::iterator geo_itr_; /** Remaining points to be processed. */ - std::list points_; + std::list points_; /** Point currently being processed. */ - std::list::iterator point_itr_; + std::list::iterator point_itr_; /** Use point placement. Otherwise line placement is used. */ bool point_placement_; /** Place text at points on a line instead of following the line (used for ShieldSymbolizer) .*/ @@ -116,14 +116,9 @@ public: return marker_ext_; } - double get_marker_height() const + pixel_position get_marker_size() const { - return marker_h_; - } - - double get_marker_width() const - { - return marker_w_; + return marker_size_; } bool next(); @@ -138,10 +133,8 @@ protected: box2d marker_ext_; boost::optional marker_; agg::trans_affine image_transform_; - double marker_w_; - double marker_h_; - double marker_x_; - double marker_y_; + pixel_position marker_size_; + pixel_position marker_pos_; using text_symbolizer_helper::geometries_to_process_; using text_symbolizer_helper::placement_; diff --git a/include/mapnik/text/placement_finder_ng.hpp b/include/mapnik/text/placement_finder_ng.hpp index f25d685b1..c0a56f31f 100644 --- a/include/mapnik/text/placement_finder_ng.hpp +++ b/include/mapnik/text/placement_finder_ng.hpp @@ -97,7 +97,7 @@ public: face_manager_freetype & font_manager); /** Try to place a single label at the given point. */ - glyph_positions_ptr find_point_placement(double pos_x, double pos_y); + glyph_positions_ptr find_point_placement(pixel_position pos); bool next_position(); private: diff --git a/include/mapnik/text_properties.hpp b/include/mapnik/text_properties.hpp index c155ceffa..b5f4433f8 100644 --- a/include/mapnik/text_properties.hpp +++ b/include/mapnik/text_properties.hpp @@ -29,6 +29,7 @@ #include #include #include +#include // stl #include @@ -118,7 +119,6 @@ enum justify_alignment DEFINE_ENUM(justify_alignment_e, justify_alignment); -typedef std::pair position; class text_layout; @@ -147,7 +147,7 @@ struct text_symbolizer_properties //Per symbolizer options expression_ptr orientation; - position displacement; + pixel_position displacement; label_placement_e label_placement; horizontal_alignment_e halign; justify_alignment_e jalign; diff --git a/include/mapnik/text_symbolizer.hpp b/include/mapnik/text_symbolizer.hpp index 05a9c2a39..125591ab3 100644 --- a/include/mapnik/text_symbolizer.hpp +++ b/include/mapnik/text_symbolizer.hpp @@ -105,8 +105,8 @@ struct MAPNIK_DECL text_symbolizer : public symbolizer_base void set_vertical_alignment(vertical_alignment_e valign); vertical_alignment_e get_vertical_alignment() const func_deprecated; void set_displacement(double x, double y); - void set_displacement(position const& p); - position const& get_displacement() const func_deprecated; + void set_displacement(pixel_position const& p); + pixel_position const& get_displacement() const func_deprecated; void set_avoid_edges(bool avoid); bool get_avoid_edges() const func_deprecated; void set_minimum_distance(double distance); diff --git a/src/save_map.cpp b/src/save_map.cpp index 0151ff2a7..6dae344db 100644 --- a/src/save_map.cpp +++ b/src/save_map.cpp @@ -205,14 +205,14 @@ public: { set_attr(sym_node, "text-opacity", sym.get_text_opacity()); } - position displacement = sym.get_shield_displacement(); - if (displacement.first != dfl.get_shield_displacement().first || explicit_defaults_) + pixel_position displacement = sym.get_shield_displacement(); + if (displacement.x != dfl.get_shield_displacement().x || explicit_defaults_) { - set_attr(sym_node, "shield-dx", displacement.first); + set_attr(sym_node, "shield-dx", displacement.x); } - if (displacement.second != dfl.get_shield_displacement().second || explicit_defaults_) + if (displacement.y != dfl.get_shield_displacement().y || explicit_defaults_) { - set_attr(sym_node, "shield-dy", displacement.second); + set_attr(sym_node, "shield-dy", displacement.y); } serialize_symbolizer_base(sym_node, sym); } diff --git a/src/shield_symbolizer.cpp b/src/shield_symbolizer.cpp index 17238953b..5a60eefa9 100644 --- a/src/shield_symbolizer.cpp +++ b/src/shield_symbolizer.cpp @@ -79,10 +79,10 @@ bool shield_symbolizer::get_unlock_image() const void shield_symbolizer::set_shield_displacement(double shield_dx,double shield_dy) { - shield_displacement_ = std::make_pair(shield_dx, shield_dy); + shield_displacement_.set(shield_dx, shield_dy); } -position const& shield_symbolizer::get_shield_displacement() const +pixel_position const& shield_symbolizer::get_shield_displacement() const { return shield_displacement_; } diff --git a/src/symbolizer_helpers.cpp b/src/symbolizer_helpers.cpp index 56e604d98..f1a6b4513 100644 --- a/src/symbolizer_helpers.cpp +++ b/src/symbolizer_helpers.cpp @@ -117,7 +117,7 @@ glyph_positions_ptr text_symbolizer_helper::next_point_ point_itr_ = points_.begin(); continue; //Reexecute size check } - glyph_positions_ptr glyphs = finder_.find_point_placement(point_itr_->first, point_itr_->second); + glyph_positions_ptr glyphs = finder_.find_point_placement(*point_itr_); if (glyphs) { //Found a placement @@ -215,7 +215,7 @@ void text_symbolizer_helper::initialize_points() geom.vertex(&label_x, &label_y); prj_trans_.backward(label_x, label_y, z); t_.forward(&label_x, &label_y); - points_.push_back(std::make_pair(label_x, label_y)); + points_.push_back(pixel_position(label_x, label_y)); } } else @@ -234,7 +234,7 @@ void text_symbolizer_helper::initialize_points() } prj_trans_.backward(label_x, label_y, z); t_.forward(&label_x, &label_y); - points_.push_back(std::make_pair(label_x, label_y)); + points_.push_back(pixel_position(label_x, label_y)); } } point_itr_ = points_.begin(); @@ -275,7 +275,7 @@ bool shield_symbolizer_helper::next() template bool shield_symbolizer_helper::next_point_placement() { - position const& shield_pos = sym_.get_shield_displacement(); + pixel_position const& shield_pos = sym_.get_shield_displacement(); while (!points_.empty()) { if (point_itr_ == points_.end()) @@ -286,11 +286,10 @@ bool shield_symbolizer_helper::next_point_placement() point_itr_ = points_.begin(); continue; //Reexecute size check } - position const& text_disp = placement_->properties.displacement; - double label_x = point_itr_->first + shield_pos.first; - double label_y = point_itr_->second + shield_pos.second; + pixel_position const& text_disp = placement_->properties.displacement; + pixel_position label_pos = *point_itr_ + shield_pos; - glyph_positions_ptr glyphs = finder_.find_point_placement(label_x, label_y); + glyph_positions_ptr glyphs = finder_.find_point_placement(label_pos); if (!glyphs) { //No placement for this point. Keep it in points_ for next try. @@ -314,9 +313,8 @@ bool shield_symbolizer_helper::next_point_placement() } else { // center image at reference location - marker_x_ = label_x - 0.5 * marker_w_; - marker_y_ = label_y - 0.5 * marker_h_; - marker_ext_.re_center(label_x, label_y); + marker_pos_ = label_pos - 0.5 * marker_size_; + marker_ext_.re_center(label_pos.x, label_pos.y); } if (placement_->properties.allow_overlap || detector_.has_placement(marker_ext_)) @@ -343,7 +341,7 @@ bool shield_symbolizer_helper::next_point_placement() template bool shield_symbolizer_helper::next_line_placement() { - position const& pos = placement_->properties.displacement; + pixel_position const& pos = placement_->properties.displacement; #if 0 finder_->additional_boxes.clear(); //Markers are automatically centered @@ -368,17 +366,15 @@ void shield_symbolizer_helper::init_marker() marker_ = marker_cache::instance()->find(filename, true); } if (!marker_) { - marker_w_ = 0; - marker_h_ = 0; + marker_size_.clear(); marker_ext_.init(0, 0, 0, 0); return; } - marker_w_ = (*marker_)->width(); - marker_h_ = (*marker_)->height(); - double px0 = - 0.5 * marker_w_; - double py0 = - 0.5 * marker_h_; - double px1 = 0.5 * marker_w_; - double py1 = 0.5 * marker_h_; + marker_size_.set((*marker_)->width(), (*marker_)->height()); + double px0 = - 0.5 * marker_size_.x; + double py0 = - 0.5 * marker_size_.y; + double px1 = 0.5 * marker_size_.x; + double py1 = 0.5 * marker_size_.y; double px2 = px1; double py2 = py0; double px3 = px0; diff --git a/src/text/placement_finder_ng.cpp b/src/text/placement_finder_ng.cpp index 183e82f85..be4e98a05 100644 --- a/src/text/placement_finder_ng.cpp +++ b/src/text/placement_finder_ng.cpp @@ -36,15 +36,6 @@ placement_finder_ng::placement_finder_ng(Feature const& feature, DetectorType &d { } -glyph_positions_ptr placement_finder_ng::find_point_placement(double pos_x, double pos_y) -{ - glyph_positions_ptr glyphs = boost::make_shared(); -// glyphs->point_placement(pixel_position(pos_x, pos_y)); - //TODO: angle - //TODO: Check for placement - return glyphs; -} - bool placement_finder_ng::next_position() { if (!valid_) @@ -79,10 +70,10 @@ void placement_finder_ng::init_alignment() valign_ = p.valign; if (valign_ == V_AUTO) { - if (p.displacement.second > 0.0) + if (p.displacement.y > 0.0) { valign_ = V_BOTTOM; - } else if (p.displacement.second < 0.0) + } else if (p.displacement.y < 0.0) { valign_ = V_TOP; } else @@ -94,10 +85,10 @@ void placement_finder_ng::init_alignment() halign_ = p.halign; if (halign_ == H_AUTO) { - if (p.displacement.first > 0.0) + if (p.displacement.x > 0.0) { halign_ = H_RIGHT; - } else if (p.displacement.first < 0.0) + } else if (p.displacement.x < 0.0) { halign_ = H_LEFT; } else @@ -109,10 +100,10 @@ void placement_finder_ng::init_alignment() jalign_ = p.jalign; if (jalign_ == J_AUTO) { - if (p.displacement.first > 0.0) + if (p.displacement.x > 0.0) { jalign_ = J_LEFT; - } else if (p.displacement.first < 0.0) + } else if (p.displacement.x < 0.0) { jalign_ = J_RIGHT; } else { @@ -121,6 +112,19 @@ void placement_finder_ng::init_alignment() } } +glyph_positions_ptr placement_finder_ng::find_point_placement(pixel_position pos) +{ + glyph_positions_ptr glyphs = boost::make_shared(); + glyphs->set_base_point(pos + info_->properties.displacement); +// glyphs->point_placement(pixel_position(pos_x, pos_y)); + //TODO: angle + //TODO: Check for placement + return glyphs; +} + + +/*********************************************************************************************/ + glyph_positions::glyph_positions() : base_point_(), const_angle_(true) diff --git a/src/text_placements/simple.cpp b/src/text_placements/simple.cpp index 36a996580..fe28dedee 100644 --- a/src/text_placements/simple.cpp +++ b/src/text_placements/simple.cpp @@ -62,8 +62,8 @@ bool text_placement_info_simple::next() bool text_placement_info_simple::next_position_only() { - const position &pdisp = parent_->defaults.displacement; - position &displacement = properties.displacement; + pixel_position const& pdisp = parent_->defaults.displacement; + pixel_position &displacement = properties.displacement; if (position_state >= parent_->direction_.size()) return false; directions_t dir = parent_->direction_[position_state]; switch (dir) { @@ -71,28 +71,28 @@ bool text_placement_info_simple::next_position_only() displacement = pdisp; break; case NORTH: - displacement = std::make_pair(0, -abs(pdisp.second)); + displacement.set(0, -abs(pdisp.y)); break; case EAST: - displacement = std::make_pair(abs(pdisp.first), 0); + displacement.set(abs(pdisp.x), 0); break; case SOUTH: - displacement = std::make_pair(0, abs(pdisp.second)); + displacement.set(0, abs(pdisp.y)); break; case WEST: - displacement = std::make_pair(-abs(pdisp.first), 0); + displacement.set(-abs(pdisp.x), 0); break; case NORTHEAST: - displacement = std::make_pair(abs(pdisp.first), -abs(pdisp.second)); + displacement.set(abs(pdisp.x), -abs(pdisp.y)); break; case SOUTHEAST: - displacement = std::make_pair(abs(pdisp.first), abs(pdisp.second)); + displacement.set(abs(pdisp.x), abs(pdisp.y)); break; case NORTHWEST: - displacement = std::make_pair(-abs(pdisp.first), -abs(pdisp.second)); + displacement.set(-abs(pdisp.x), -abs(pdisp.y)); break; case SOUTHWEST: - displacement = std::make_pair(-abs(pdisp.first), abs(pdisp.second)); + displacement.set(-abs(pdisp.x), abs(pdisp.y)); break; default: MAPNIK_LOG_WARN(text_placements) << "Unknown placement"; diff --git a/src/text_properties.cpp b/src/text_properties.cpp index 3bacae053..194700048 100644 --- a/src/text_properties.cpp +++ b/src/text_properties.cpp @@ -113,9 +113,9 @@ void text_symbolizer_properties::from_xml(xml_node const &sym, fontset_map const optional orientation_ = sym.get_opt_attr("orientation"); if (orientation_) orientation = *orientation_; optional dx = sym.get_opt_attr("dx"); - if (dx) displacement.first = *dx; + if (dx) displacement.x = *dx; optional dy = sym.get_opt_attr("dy"); - if (dy) displacement.second = *dy; + if (dy) displacement.y = *dy; optional max_char_angle_delta_ = sym.get_opt_attr("max-char-angle-delta"); if (max_char_angle_delta_) max_char_angle_delta=(*max_char_angle_delta_)*(M_PI/180); @@ -144,13 +144,13 @@ void text_symbolizer_properties::to_xml(boost::property_tree::ptree &node, } } - if (displacement.first != dfl.displacement.first || explicit_defaults) + if (displacement.x != dfl.displacement.x || explicit_defaults) { - set_attr(node, "dx", displacement.first); + set_attr(node, "dx", displacement.x); } - if (displacement.second != dfl.displacement.second || explicit_defaults) + if (displacement.y != dfl.displacement.y || explicit_defaults) { - set_attr(node, "dy", displacement.second); + set_attr(node, "dy", displacement.y); } if (label_placement != dfl.label_placement || explicit_defaults) { diff --git a/src/text_symbolizer.cpp b/src/text_symbolizer.cpp index 0c6a41fd1..ff7982ade 100644 --- a/src/text_symbolizer.cpp +++ b/src/text_symbolizer.cpp @@ -350,15 +350,15 @@ label_placement_e text_symbolizer::get_label_placement() const void text_symbolizer::set_displacement(double x, double y) { - placement_options_->defaults.displacement = std::make_pair(x,y); + placement_options_->defaults.displacement.set(x, y); } -void text_symbolizer::set_displacement(position const& p) +void text_symbolizer::set_displacement(const pixel_position &p) { placement_options_->defaults.displacement = p; } -position const& text_symbolizer::get_displacement() const +pixel_position const& text_symbolizer::get_displacement() const { return placement_options_->defaults.displacement; }