Skip to content

Commit

Permalink
un-template markers_basic_placement
Browse files Browse the repository at this point in the history
- move Locator/Detector-dependent stuff back to markers_point_placement
- slightly reduces library size, by about 20% of what mapnik#3338 added
  • Loading branch information
lightmare committed Mar 5, 2016
1 parent fe57454 commit f96631f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
6 changes: 2 additions & 4 deletions include/mapnik/markers_placement.hpp
Expand Up @@ -37,8 +37,6 @@ template <typename Locator, typename Detector>
class markers_placement_finder : util::noncopyable
{
public:
using basic_placement = markers_basic_placement<Locator, Detector>;

markers_placement_finder(marker_placement_e placement_type,
Locator &locator,
Detector &detector,
Expand Down Expand Up @@ -75,7 +73,7 @@ class markers_placement_finder : util::noncopyable

~markers_placement_finder()
{
active_placement_->~basic_placement();
active_placement_->~markers_basic_placement();
}

// Get next point where the marker should be placed. Returns true if a place is found, false if none is found.
Expand All @@ -85,7 +83,7 @@ class markers_placement_finder : util::noncopyable
}

private:
basic_placement* active_placement_;
markers_basic_placement* active_placement_;

union
{
Expand Down
46 changes: 3 additions & 43 deletions include/mapnik/markers_placements/basic.hpp
Expand Up @@ -46,43 +46,27 @@ struct markers_placement_params
direction_enum direction;
};

template <typename Locator, typename Detector>
class markers_basic_placement : util::noncopyable
{
public:
markers_basic_placement(Locator & locator, Detector & detector,
markers_placement_params const& params)
: locator_(locator),
detector_(detector),
params_(params),
done_(false)
markers_basic_placement(markers_placement_params const& params)
: params_(params)
{
// no need to rewind locator here, markers_placement_finder
// does that after construction
}

markers_basic_placement(markers_basic_placement && ) = default;

virtual ~markers_basic_placement()
{
// empty but necessary
}

// Start again at first marker. Returns the same list of markers only works when they were NOT added to the detector.
virtual void rewind()
{
locator_.rewind(0);
done_ = false;
}
virtual void rewind() = 0;

// Get next point where the marker should be placed. Returns true if a place is found, false if none is found.
virtual bool get_point(double &x, double &y, double &angle, bool ignore_placement) = 0;

protected:
Locator & locator_;
Detector & detector_;
markers_placement_params const& params_;
bool done_;

// Rotates the size_ box and translates the position.
box2d<double> perform_transform(double angle, double dx, double dy) const
Expand All @@ -91,30 +75,6 @@ class markers_basic_placement : util::noncopyable
return box2d<double>(params_.size, tr);
}

// Checks transformed box placement with collision detector.
// returns false if the box:
// - a) isn't wholly inside extent and avoid_edges == true
// - b) collides with something and allow_overlap == false
// otherwise returns true, and if ignore_placement == true,
// also adds the box to collision detector
bool push_to_detector(double x, double y, double angle, bool ignore_placement)
{
auto box = perform_transform(angle, x, y);
if (params_.avoid_edges && !detector_.extent().contains(box))
{
return false;
}
if (!params_.allow_overlap && !detector_.has_placement(box))
{
return false;
}
if (!ignore_placement)
{
detector_.insert(box);
}
return true;
}

bool set_direction(double & angle) const
{
switch (params_.direction)
Expand Down
50 changes: 47 additions & 3 deletions include/mapnik/markers_placements/point.hpp
Expand Up @@ -30,11 +30,26 @@
namespace mapnik {

template <typename Locator, typename Detector>
class markers_point_placement : public markers_basic_placement<Locator, Detector>
class markers_point_placement : public markers_basic_placement
{
public:
using basic_placement = markers_basic_placement<Locator, Detector>;
using basic_placement::basic_placement;
markers_point_placement(Locator & locator, Detector & detector,
markers_placement_params const& params)
: markers_basic_placement(params),
locator_(locator),
detector_(detector),
done_(false)
{
// no need to rewind locator here, markers_placement_finder
// does that after construction
}

// Start again at first marker. Returns the same list of markers only works when they were NOT added to the detector.
void rewind()
{
locator_.rewind(0);
done_ = false;
}

// Get next point where the marker should be placed. Returns true if a place is found, false if none is found.
bool get_point(double &x, double &y, double &angle, bool ignore_placement)
Expand Down Expand Up @@ -71,6 +86,35 @@ class markers_point_placement : public markers_basic_placement<Locator, Detector
this->done_ = true;
return true;
}

protected:
Locator & locator_;
Detector & detector_;
bool done_;

// Checks transformed box placement with collision detector.
// returns false if the box:
// - a) isn't wholly inside extent and avoid_edges == true
// - b) collides with something and allow_overlap == false
// otherwise returns true, and if ignore_placement == false,
// also adds the box to collision detector
bool push_to_detector(double x, double y, double angle, bool ignore_placement)
{
auto box = perform_transform(angle, x, y);
if (params_.avoid_edges && !detector_.extent().contains(box))
{
return false;
}
if (!params_.allow_overlap && !detector_.has_placement(box))
{
return false;
}
if (!ignore_placement)
{
detector_.insert(box);
}
return true;
}
};

}
Expand Down

0 comments on commit f96631f

Please sign in to comment.