Skip to content

Commit

Permalink
Convex hull
Browse files Browse the repository at this point in the history
  • Loading branch information
lehmann-4178656ch committed May 21, 2024
1 parent c2711d3 commit 586f7e3
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/spatialjoin/GeometryCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ sj::Line sj::GeometryCache<sj::Line>::getFromDisk(size_t off,
ret.cutouts[boxid] = cutout;
}

// convex hull
readPoly(_geomsFReads[tid], ret.convexHull);

// OBB
readPoly(_geomsFReads[tid], ret.obb);

Expand Down Expand Up @@ -206,6 +209,9 @@ sj::Area sj::GeometryCache<sj::Area>::getFromDisk(size_t off,
ret.cutouts[boxid] = cutout;
}

// convex hull
readPoly(_geomsFReads[tid], ret.convexHull);

// OBB
readPoly(_geomsFReads[tid], ret.obb);

Expand Down Expand Up @@ -313,6 +319,9 @@ size_t sj::GeometryCache<sj::Line>::add(const sj::Line& val) {
_geomsOffset += sizeof(int32_t) + sizeof(int32_t);
}

// convex hull
writePoly(val.convexHull);

// OBB
writePoly(val.obb);

Expand Down Expand Up @@ -375,6 +384,9 @@ size_t sj::GeometryCache<sj::Area>::add(const sj::Area& val) {
_geomsOffset += sizeof(int32_t) + sizeof(int32_t);
}

// convex hull
writePoly(val.convexHull);

// OBB
writePoly(val.obb);

Expand Down
6 changes: 6 additions & 0 deletions src/spatialjoin/GeometryCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct Area {
// cutouts
std::map<int32_t, size_t> cutouts;

// Convex hull
util::geo::I32XSortedPolygon convexHull;

// OBB
util::geo::I32XSortedPolygon obb;

Expand Down Expand Up @@ -90,6 +93,9 @@ struct Line {
// cutouts
std::map<int32_t, size_t> cutouts;

// Convex hull
util::geo::I32XSortedPolygon convexHull;

// OBB
util::geo::I32XSortedPolygon obb;
};
Expand Down
7 changes: 6 additions & 1 deletion src/spatialjoin/SpatialJoinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ void printHelp(int argc, char** argv) {
<< "disable cutouts\n"
<< std::setw(41) << " --no-diag-box"
<< "disable diagonal bounding-box based pre-filter\n"
<< std::setw(41) << " --no-convex-hulls"
<< "disable convex hulls\n"
<< std::setw(41) << " --no-fast-sweep-skip"
<< "disable fast sweep skip using binary search\n"
<< std::setw(41) << " --use-inner-outer"
Expand Down Expand Up @@ -107,6 +109,7 @@ int main(int argc, char** argv) {
bool useOBB = true;
bool useCutouts = true;
bool useDiagBox = true;
bool useConvexHulls = true;
bool useFastSweepSkip = true;
bool useInnerOuter = false;

Expand Down Expand Up @@ -153,6 +156,8 @@ int main(int argc, char** argv) {
useCutouts = false;
} else if (cur == "--no-diag-box") {
useDiagBox = false;
} else if (cur == "--no-convex-hulls") {
useConvexHulls = false;
} else if (cur == "--no-fast-sweep-skip") {
useFastSweepSkip = false;
} else if (cur == "--use-inner-outer") {
Expand Down Expand Up @@ -219,7 +224,7 @@ int main(int argc, char** argv) {
Sweeper sweeper(
{NUM_THREADS, prefix, intersects, contains, covers, touches, equals,
overlaps, crosses, suffix, useBoxIds, useArea, useOBB, useCutouts,
useDiagBox, useFastSweepSkip, useInnerOuter},
useDiagBox, useConvexHulls, useFastSweepSkip, useInnerOuter},
useCache, cache, output);

if (!useCache) {
Expand Down
25 changes: 24 additions & 1 deletion src/spatialjoin/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ struct Stats {
uint64_t timeOBBIsectAreaPoint = 0;
uint64_t timeOBBIsectLineLine = 0;

uint64_t timeConvexHullIsectAreaArea = 0;
uint64_t timeConvexHullIsectAreaLine = 0;
uint64_t timeConvexHullIsectAreaPoint = 0;
uint64_t timeConvexHullIsectLineLine = 0;

uint64_t timeFullGeoCheckAreaArea = 0;
uint64_t timeFullGeoCheckAreaLine = 0;
uint64_t timeFullGeoCheckAreaPoint = 0;
Expand Down Expand Up @@ -64,7 +69,9 @@ inline std::string Stats::toString() {
timeGeoCacheRetrievalPoint + timeWrite + timeBoxIdIsectAreaArea +
timeBoxIdIsectAreaLine + timeOBBIsectAreaArea +
timeOBBIsectAreaLine + timeOBBIsectAreaPoint +
timeOBBIsectLineLine + timeBoxIdIsectAreaPoint +
timeOBBIsectLineLine + timeConvexHullIsectAreaArea +
timeConvexHullIsectAreaLine + timeConvexHullIsectAreaPoint +
timeConvexHullIsectLineLine + timeBoxIdIsectAreaPoint +
timeBoxIdIsectLineLine + timeBoxIdIsectLinePoint +
timeFullGeoCheckAreaArea + timeFullGeoCheckAreaLine +
timeFullGeoCheckAreaPoint + timeFullGeoCheckLineLine +
Expand Down Expand Up @@ -124,6 +131,22 @@ inline std::string Stats::toString() {
ss << "time for obb intersections LINE/LINE: " << t << " s ("
<< ((t / sum) * 100.0) << "%)\n";

t = double(timeConvexHullIsectAreaArea) / 1000000000.0;
ss << "time for convex hull intersections AREA/AREA: " << t << " s ("
<< ((t / sum) * 100.0) << "%)\n";

t = double(timeConvexHullIsectAreaLine) / 1000000000.0;
ss << "time for convex hull intersections AREA/LINE: " << t << " s ("
<< ((t / sum) * 100.0) << "%)\n";

t = double(timeConvexHullIsectAreaPoint) / 1000000000.0;
ss << "time for convex hull intersections AREA/POINT: " << t << " s ("
<< ((t / sum) * 100.0) << "%)\n";

t = double(timeConvexHullIsectLineLine) / 1000000000.0;
ss << "time for convex hull intersections LINE/LINE: " << t << " s ("
<< ((t / sum) * 100.0) << "%)\n";

t = double(timeFullGeoCheckAreaArea) / 1000000000.0;
ss << "time for " << fullGeoChecksAreaArea
<< " full geom checks AREA/AREA: " << t << " s (" << ((t / sum) * 100.0)
Expand Down
48 changes: 45 additions & 3 deletions src/spatialjoin/Sweeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ void Sweeper::add(const I32Polygon& poly, const std::string& gid) {
void Sweeper::add(const I32Polygon& poly, const std::string& gid,
size_t subid) {
const auto& box = getBoundingBox(poly);
const auto& hull = util::geo::convexHull(poly);
I32XSortedPolygon spoly(poly);

double areaSize = area(poly);
double outerAreaSize = outerArea(poly);
BoxIdList boxIds;
Expand Down Expand Up @@ -163,6 +163,11 @@ void Sweeper::add(const I32Polygon& poly, const std::string& gid,
outer = sj::innerouter::simplifiedPoly<Mode::OUTER>(poly, 1 / (3.14 * 20));
}

util::geo::I32Polygon convexHull;
if (_cfg.useConvexHull) {
convexHull = util::geo::convexHull(poly);
}

util::geo::I32Polygon obb;
obb = util::geo::convexHull(util::geo::getOrientedEnvelope(poly));

Expand All @@ -178,7 +183,7 @@ void Sweeper::add(const I32Polygon& poly, const std::string& gid,

size_t id = _areaCache.add({spoly, box, gid, subid, areaSize,
_cfg.useArea ? outerAreaSize : 0, boxIds, cutouts,
obb, inner, outer});
obb, inner, outer, convexHull});

diskAdd({id, box.getLowerLeft().getY(), box.getUpperRight().getY(),
box.getLowerLeft().getX(), false, POLYGON, areaSize, box45});
Expand Down Expand Up @@ -210,6 +215,11 @@ void Sweeper::add(const I32Line& line, const std::string& gid, size_t subid) {

double len = util::geo::len(line);

util::geo::I32Polygon convexHull;
if (_cfg.useConvexHull) {
convexHull = util::geo::convexHull(line);
}

util::geo::I32Polygon obb;
obb = util::geo::convexHull(util::geo::getOrientedEnvelope(line));

Expand Down Expand Up @@ -241,7 +251,7 @@ void Sweeper::add(const I32Line& line, const std::string& gid, size_t subid) {
}

size_t id =
_lineCache.add({sline, box, gid, subid, len, boxIds, cutouts, obb});
_lineCache.add({sline, box, gid, subid, len, boxIds, cutouts, obb, convexHull});

diskAdd({id, box.getLowerLeft().getY(), box.getUpperRight().getY(),
box.getLowerLeft().getX(), false, LINE, len, box45});
Expand Down Expand Up @@ -665,6 +675,15 @@ std::tuple<bool, bool, bool, bool, bool> Sweeper::check(const Area* a,
}
}

if (_cfg.useConvexHull) {
auto ts = TIME();
auto r = util::geo::intersectsContainsCovers(
a->convexHull, a->box, a->outerArea,
b->convexHull, b->box, b->outerArea);
_stats[t].timeConvexHullIsectAreaArea += TOOK(ts);
if (!std::get<0>(r)) return {0, 0, 0, 0, 0};
}

if (_cfg.useInnerOuter && !a->outer.empty() && !b->outer.empty()) {
auto ts = TIME();
auto r = util::geo::intersectsContainsCovers(
Expand Down Expand Up @@ -759,6 +778,14 @@ std::tuple<bool, bool, bool, bool, bool> Sweeper::check(const Line* a,
}
}

if (_cfg.useConvexHull) {
auto ts = TIME();
auto r = intersectsContainsCovers(
a->convexHull, a->box, 0, b->convexHull, b->box, 0);
_stats[t].timeConvexHullIsectAreaLine += TOOK(ts);
if (!std::get<0>(r)) return {0, 0, 0, 0, 0};
}

if (_cfg.useInnerOuter && !b->outer.empty()) {
auto ts = TIME();
auto r =
Expand Down Expand Up @@ -839,6 +866,14 @@ std::tuple<bool, bool, bool, bool, bool> Sweeper::check(const Line* a,
}
}

if (_cfg.useConvexHull) {
auto ts = TIME();
auto r = intersectsContainsCovers(
a->convexHull, a->box, 0, b->convexHull, b->box, 0);
_stats[t].timeConvexHullIsectLineLine += TOOK(ts);
if (!std::get<0>(r)) return {0, 0, 0, 0, 0};
}

auto ts = TIME();
auto res = intersectsCovers(a->geom, b->geom, a->box, b->box);
_stats[t].timeFullGeoCheckLineLine += TOOK(ts);
Expand Down Expand Up @@ -1081,6 +1116,13 @@ std::pair<bool, bool> Sweeper::check(const I32Point& a, const Area* b,
if (!std::get<1>(r)) return {0, 0};
}

if (_cfg.useConvexHull) {
auto ts = TIME();
auto r = containsCovers(a, b->convexHull);
_stats[t].timeConvexHullIsectAreaPoint += TOOK(ts);
if (!std::get<0>(r)) return {0, 0};
}

if (_cfg.useInnerOuter && !b->outer.empty()) {
auto ts = TIME();
auto r = containsCovers(a, b->outer);
Expand Down
1 change: 1 addition & 0 deletions src/spatialjoin/Sweeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct SweeperCfg {
bool useDiagBox;
bool useFastSweepSkip;
bool useInnerOuter;
bool useConvexHull;
};

// buffer size _must_ be multiples of sizeof(BoxVal)
Expand Down
2 changes: 1 addition & 1 deletion src/spatialjoin/tests/TestMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using sj::Sweeper;
std::string fullRun(const std::string& file) {
Sweeper sweeper({1, "$", " intersects ", " contains ", " covers ",
" touches ", " equals ", " overlaps ", " crosses ", "$\n",
true, true, true, true, true, true, true},
true, true, true, true, true, true, true, true},
false, ".", ".resTmp");

size_t gid = 0;
Expand Down

0 comments on commit 586f7e3

Please sign in to comment.