Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HotPixelIndex: Use STRtree instead of KdTree #769

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions include/geos/noding/snapround/HotPixelIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <geos/io/WKTWriter.h>
#include <geos/index/kdtree/KdTree.h>
#include <geos/index/kdtree/KdNodeVisitor.h>
#include <geos/index/strtree/TemplateSTRtree.h>

#include <array>
#include <map>
Expand Down Expand Up @@ -63,8 +64,10 @@ class GEOS_DLL HotPixelIndex {
/* members */
const geom::PrecisionModel* pm;
double scaleFactor;
std::unique_ptr<geos::index::kdtree::KdTree> index;
//std::unique_ptr<geos::index::kdtree::KdTree> index;
index::strtree::TemplateSTRtree<HotPixel*> index;
std::deque<HotPixel> hotPixelQue;
std::map<geom::Coordinate, HotPixel*> hotPixelMap;

/* methods */
geom::Coordinate round(const geom::Coordinate& c);
Expand All @@ -84,8 +87,15 @@ class GEOS_DLL HotPixelIndex {
* The visitor must determine whether each hot pixel actually intersects
* the segment.
*/
template<typename Visitor>
void query(const geom::Coordinate& p0, const geom::Coordinate& p1,
index::kdtree::KdNodeVisitor& visitor);
Visitor&& visitor) {
geom::Envelope queryEnv(p0, p1);
queryEnv.expandBy(1.0 / scaleFactor);

index.query(queryEnv, visitor);
}


};

Expand Down
1 change: 1 addition & 0 deletions src/index/kdtree/KdTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <vector>
#include <algorithm>
#include <iostream>
#include <stack>

using namespace geos::geom;
Expand Down
28 changes: 13 additions & 15 deletions src/noding/snapround/HotPixelIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ namespace snapround { // geos.noding.snapround
HotPixelIndex::HotPixelIndex(const PrecisionModel* p_pm)
:
pm(p_pm),
scaleFactor(p_pm->getScale()),
index(new KdTree())
scaleFactor(p_pm->getScale())
{
}

Expand Down Expand Up @@ -72,8 +71,9 @@ HotPixelIndex::add(const Coordinate& p)
// Pick up a pointer to the most recently added
// HotPixel.
hp = &(hotPixelQue.back());
hotPixelMap[hp->getCoordinate()] = hp;

index->insert(hp->getCoordinate(), hp);
index.insert(Envelope(hp->getCoordinate()), hp);
return hp;
}

Expand Down Expand Up @@ -140,11 +140,18 @@ HotPixelIndex::addNodes(const std::vector<geom::Coordinate>& pts)
HotPixel*
HotPixelIndex::find(const geom::Coordinate& pixelPt)
{
index::kdtree::KdNode *kdNode = index->query(pixelPt);
if (kdNode == nullptr) {
auto it = hotPixelMap.find(pixelPt);
if (it != hotPixelMap.end()) {
return it->second;
} else {
return nullptr;
}
return (HotPixel*)(kdNode->getData());
//HotPixel* ret = nullptr;
//index.query(Envelope(pixelPt, pixelPt), [&ret](HotPixel* hit) {
// ret = hit;
// return false;
//});
//return ret;
}

/*private*/
Expand All @@ -157,15 +164,6 @@ HotPixelIndex::round(const Coordinate& pt)
}


/*public*/
void
HotPixelIndex::query(const Coordinate& p0, const Coordinate& p1, index::kdtree::KdNodeVisitor& visitor)
{
Envelope queryEnv(p0, p1);
queryEnv.expandBy(1.0 / scaleFactor);
index->query(queryEnv, visitor);
}


} // namespace geos.noding.snapround
} // namespace geos.noding
Expand Down
7 changes: 7 additions & 0 deletions src/noding/snapround/SnapRoundingNoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ SnapRoundingNoder::snapSegment(Coordinate& p0, Coordinate& p1, NodedSegmentStrin

void visit(KdNode* node) override {
HotPixel* hp = static_cast<HotPixel*>(node->getData());
(*this)(hp);
}

void operator()(HotPixel* hp) {
/**
* If the hot pixel is not a node, and it contains one of the segment vertices,
* then that vertex is the source for the hot pixel.
Expand Down Expand Up @@ -265,7 +269,10 @@ SnapRoundingNoder::snapVertexNode(const Coordinate& p0, NodedSegmentString* ss,

void visit(KdNode* node) override {
HotPixel* hp = static_cast<HotPixel*>(node->getData());
(*this)(hp);
}

void operator()(const HotPixel* hp) {
/**
* If vertex pixel is a node, add it.
*/
Expand Down