Skip to content

Commit

Permalink
fix unique_ptr related bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Sep 29, 2021
1 parent 230a756 commit e07009c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 68 deletions.
12 changes: 6 additions & 6 deletions src/common/geo/GeoIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::vector<uint64_t> GeographyIndex::indexCells(const Geography& g) const noexc
}

std::vector<ScanRange> GeographyIndex::dWithin(const Geography& g, double distance) const noexcept {
auto* r = g.asS2().get();
auto r = g.asS2();
// TODO(jie): Better to ensure the Geography value is valid to build s2 when constructing.
if (!r) {
LOG(INFO) << "GeographyIndex::dWithin(), asS2() failed.";
Expand All @@ -63,21 +63,21 @@ std::vector<ScanRange> GeographyIndex::dWithin(const Geography& g, double distan
S1Angle radius = S2Earth::ToAngle(util::units::Meters(distance));
switch (g.shape()) {
case GeoShape::POINT: {
const S2Point& gPoint = static_cast<S2PointRegion*>(r)->point();
const S2Point& gPoint = static_cast<S2PointRegion*>(r.get())->point();
S2Cap gCap(gPoint, radius);
auto cells = coveringCells(gCap);
return scanRange(cells);
}
case GeoShape::LINESTRING: {
S2Polyline* gLine = static_cast<S2Polyline*>(r);
S2Polyline* gLine = static_cast<S2Polyline*>(r.get());
MutableS2ShapeIndex index;
index.Add(std::make_unique<S2Polyline::Shape>(gLine));
S2ShapeIndexBufferedRegion gBuffer(&index, radius);
auto cells = coveringCells(gBuffer);
return scanRange(cells);
}
case GeoShape::POLYGON: {
S2Polygon* gPolygon = static_cast<S2Polygon*>(r);
S2Polygon* gPolygon = static_cast<S2Polygon*>(r.get());
S2ShapeIndexBufferedRegion gBuffer(&gPolygon->index(), radius);
auto cells = coveringCells(gBuffer);
return scanRange(cells);
Expand All @@ -90,7 +90,7 @@ std::vector<ScanRange> GeographyIndex::dWithin(const Geography& g, double distan
}

std::vector<S2CellId> GeographyIndex::coveringCells(const Geography& g) const noexcept {
auto* r = g.asS2().get();
auto r = g.asS2();
// TODO(jie): Better to ensure the Geography value is valid to build s2 when constructing.
if (!r) {
LOG(INFO) << "GeographyIndex::coveringCells(), asS2() failed.";
Expand All @@ -99,7 +99,7 @@ std::vector<S2CellId> GeographyIndex::coveringCells(const Geography& g) const no

// Currently region coverer options doesn't work for point. Point always use level 30.
if (g.shape() == GeoShape::POINT) {
const S2Point& gPoint = static_cast<S2PointRegion*>(r)->point();
const S2Point& gPoint = static_cast<S2PointRegion*>(r.get())->point();
return {S2CellId(gPoint)};
}

Expand Down
23 changes: 12 additions & 11 deletions src/common/geo/function/Covers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace nebula {
// If no point in b lies exterior of b, a covers b.
// http://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html
bool covers(const Geography& a, const Geography& b) {
auto* aRegion = a.asS2().get();
auto* bRegion = b.asS2().get();
auto aRegion = a.asS2();
auto bRegion = b.asS2();
// TODO(jie): Better to ensure the Geography value is valid to build s2 when constructing.
if (!aRegion || !bRegion) {
LOG(INFO) << "covers(), asS2() failed.";
Expand All @@ -27,8 +27,8 @@ bool covers(const Geography& a, const Geography& b) {
case GeoShape::POINT: {
switch (b.shape()) {
case GeoShape::POINT:
return static_cast<S2PointRegion*>(aRegion)->Contains(
static_cast<S2PointRegion*>(bRegion)->point());
return static_cast<S2PointRegion*>(aRegion.get())
->Contains(static_cast<S2PointRegion*>(bRegion.get())->point());
case GeoShape::LINESTRING:
return false;
case GeoShape::POLYGON:
Expand All @@ -40,12 +40,13 @@ bool covers(const Geography& a, const Geography& b) {
}
}
case GeoShape::LINESTRING: {
S2Polyline* aLine = static_cast<S2Polyline*>(aRegion);
S2Polyline* aLine = static_cast<S2Polyline*>(aRegion.get());
switch (b.shape()) {
case GeoShape::POINT:
return aLine->MayIntersect(S2Cell(static_cast<S2PointRegion*>(bRegion)->point()));
return aLine->MayIntersect(S2Cell(static_cast<S2PointRegion*>(bRegion.get())->point()));
case GeoShape::LINESTRING:
return aLine->NearlyCovers(*static_cast<S2Polyline*>(bRegion), S1Angle::Radians(1e-15));
return aLine->NearlyCovers(*static_cast<S2Polyline*>(bRegion.get()),
S1Angle::Radians(1e-15));
case GeoShape::POLYGON:
return false;
default:
Expand All @@ -55,14 +56,14 @@ bool covers(const Geography& a, const Geography& b) {
}
}
case GeoShape::POLYGON: {
S2Polygon* aPolygon = static_cast<S2Polygon*>(aRegion);
S2Polygon* aPolygon = static_cast<S2Polygon*>(aRegion.get());
switch (b.shape()) {
case GeoShape::POINT:
return aPolygon->Contains(static_cast<S2PointRegion*>(bRegion)->point());
return aPolygon->Contains(static_cast<S2PointRegion*>(bRegion.get())->point());
case GeoShape::LINESTRING:
return aPolygon->Contains(*static_cast<S2Polyline*>(bRegion));
return aPolygon->Contains(*static_cast<S2Polyline*>(bRegion.get()));
case GeoShape::POLYGON:
return aPolygon->Contains(static_cast<S2Polygon*>(bRegion));
return aPolygon->Contains(static_cast<S2Polygon*>(bRegion.get()));
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
Expand Down
34 changes: 17 additions & 17 deletions src/common/geo/function/DWithin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace nebula {
// We don't need to find the closest points. We just need to find the first point pair whose
// distance is less than or less equal than the given distance. (Early quit)
bool dWithin(const Geography& a, const Geography& b, double distance, bool inclusive) {
auto aRegion = a.asS2().get();
auto bRegion = b.asS2().get();
auto aRegion = a.asS2();
auto bRegion = b.asS2();
// TODO(jie): Better to ensure the Geography value is valid to build s2 when constructing.
if (!aRegion || !bRegion) {
LOG(INFO) << "dWithin(), asS2() failed.";
Expand All @@ -25,36 +25,36 @@ bool dWithin(const Geography& a, const Geography& b, double distance, bool inclu

switch (a.shape()) {
case GeoShape::POINT: {
const S2Point& aPoint = static_cast<S2PointRegion*>(aRegion)->point();
const S2Point& aPoint = static_cast<S2PointRegion*>(aRegion.get())->point();
switch (b.shape()) {
case GeoShape::POINT: {
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion)->point();
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion.get())->point();
double closestDistance = S2Earth::GetDistanceMeters(aPoint, bPoint);
return inclusive ? closestDistance <= distance : closestDistance < distance;
}
case GeoShape::LINESTRING: {
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion);
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion.get());
return s2PointAndS2PolylineAreWithinDistance(aPoint, bLine, distance, inclusive);
}
case GeoShape::POLYGON: {
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion);
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion.get());
return s2PointAndS2PolygonAreWithinDistance(aPoint, bPolygon, distance, inclusive);
}
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return -1.0;
return false;
}
}
case GeoShape::LINESTRING: {
S2Polyline* aLine = static_cast<S2Polyline*>(aRegion);
S2Polyline* aLine = static_cast<S2Polyline*>(aRegion.get());
switch (b.shape()) {
case GeoShape::POINT: {
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion)->point();
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion.get())->point();
return s2PointAndS2PolylineAreWithinDistance(bPoint, aLine, distance, inclusive);
}
case GeoShape::LINESTRING: {
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion);
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion.get());
MutableS2ShapeIndex aIndex, bIndex;
aIndex.Add(std::make_unique<S2Polyline::Shape>(aLine));
bIndex.Add(std::make_unique<S2Polyline::Shape>(bLine));
Expand All @@ -68,28 +68,28 @@ bool dWithin(const Geography& a, const Geography& b, double distance, bool inclu
S2Earth::ToChordAngle(util::units::Meters(distance)));
}
case GeoShape::POLYGON: {
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion);
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion.get());
return s2PolylineAndS2PolygonAreWithinDistance(aLine, bPolygon, distance, inclusive);
}
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return -1.0;
return false;
}
}
case GeoShape::POLYGON: {
S2Polygon* aPolygon = static_cast<S2Polygon*>(aRegion);
S2Polygon* aPolygon = static_cast<S2Polygon*>(aRegion.get());
switch (b.shape()) {
case GeoShape::POINT: {
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion)->point();
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion.get())->point();
return s2PointAndS2PolygonAreWithinDistance(bPoint, aPolygon, distance, inclusive);
}
case GeoShape::LINESTRING: {
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion);
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion.get());
return s2PolylineAndS2PolygonAreWithinDistance(bLine, aPolygon, distance, inclusive);
}
case GeoShape::POLYGON: {
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion);
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion.get());
S2ClosestEdgeQuery query(&aPolygon->index());
S2ClosestEdgeQuery::ShapeIndexTarget target(&bPolygon->index());
if (inclusive) {
Expand All @@ -102,7 +102,7 @@ bool dWithin(const Geography& a, const Geography& b, double distance, bool inclu
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return -1.0;
return false;
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/common/geo/function/Distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ namespace nebula {

// Find the closest distance of a and b
double distance(const Geography& a, const Geography& b) {
auto aRegion = a.asS2().get();
auto bRegion = b.asS2().get();
auto aRegion = a.asS2();
auto bRegion = b.asS2();
// TODO(jie): Better to ensure the Geography value is valid to build s2 when constructing.
if (!aRegion || !bRegion) {
if (!aRegion.get() || !bRegion.get()) {
LOG(INFO) << "distance(), asS2() failed.";
return -1.0;
}

switch (a.shape()) {
case GeoShape::POINT: {
const S2Point& aPoint = static_cast<S2PointRegion*>(aRegion)->point();
const S2Point& aPoint = static_cast<S2PointRegion*>(aRegion.get())->point();
switch (b.shape()) {
case GeoShape::POINT: {
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion)->point();
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion.get())->point();
return S2Earth::GetDistanceMeters(aPoint, bPoint);
}
case GeoShape::LINESTRING: {
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion);
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion.get());
return distanceOfS2PolylineWithS2Point(bLine, aPoint);
}
case GeoShape::POLYGON: {
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion);
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion.get());
return distanceOfS2PolygonWithS2Point(bPolygon, aPoint);
}
default:
Expand All @@ -46,14 +46,14 @@ double distance(const Geography& a, const Geography& b) {
}
}
case GeoShape::LINESTRING: {
S2Polyline* aLine = static_cast<S2Polyline*>(aRegion);
S2Polyline* aLine = static_cast<S2Polyline*>(aRegion.get());
switch (b.shape()) {
case GeoShape::POINT: {
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion)->point();
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion.get())->point();
return distanceOfS2PolylineWithS2Point(aLine, bPoint);
}
case GeoShape::LINESTRING: {
const S2Polyline* bLine = static_cast<S2Polyline*>(bRegion);
const S2Polyline* bLine = static_cast<S2Polyline*>(bRegion.get());
MutableS2ShapeIndex aIndex, bIndex;
aIndex.Add(std::make_unique<S2Polyline::Shape>(aLine));
bIndex.Add(std::make_unique<S2Polyline::Shape>(bLine));
Expand All @@ -62,7 +62,7 @@ double distance(const Geography& a, const Geography& b) {
return S2Earth::ToMeters(query.GetDistance(&target));
}
case GeoShape::POLYGON: {
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion);
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion.get());
return distanceOfS2PolygonWithS2Polyline(bPolygon, aLine);
}
default:
Expand All @@ -72,18 +72,18 @@ double distance(const Geography& a, const Geography& b) {
}
}
case GeoShape::POLYGON: {
S2Polygon* aPolygon = static_cast<S2Polygon*>(aRegion);
S2Polygon* aPolygon = static_cast<S2Polygon*>(aRegion.get());
switch (b.shape()) {
case GeoShape::POINT: {
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion)->point();
const S2Point& bPoint = static_cast<S2PointRegion*>(bRegion.get())->point();
return distanceOfS2PolygonWithS2Point(aPolygon, bPoint);
}
case GeoShape::LINESTRING: {
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion);
S2Polyline* bLine = static_cast<S2Polyline*>(bRegion.get());
return distanceOfS2PolygonWithS2Polyline(aPolygon, bLine);
}
case GeoShape::POLYGON: {
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion);
S2Polygon* bPolygon = static_cast<S2Polygon*>(bRegion.get());
S2ClosestEdgeQuery query(&aPolygon->index());
S2ClosestEdgeQuery::ShapeIndexTarget target(&bPolygon->index());
return S2Earth::ToMeters(query.GetDistance(&target));
Expand Down
42 changes: 23 additions & 19 deletions src/common/geo/function/Intersects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace nebula {
// If any point in the set that comprises A is also a member of the set of points that make up B,
// they intersects;
bool intersects(const Geography& a, const Geography& b) {
auto aRegion = a.asS2().get();
auto bRegion = b.asS2().get();
auto aRegion = a.asS2();
auto bRegion = b.asS2();
// TODO(jie): Better to ensure the Geography value is valid to build s2 when constructing.
if (!aRegion || !bRegion) {
LOG(INFO) << "intersects(), asS2() failed.";
Expand All @@ -27,48 +27,52 @@ bool intersects(const Geography& a, const Geography& b) {
case GeoShape::POINT: {
switch (b.shape()) {
case GeoShape::POINT:
return static_cast<S2PointRegion*>(aRegion)->MayIntersect(
S2Cell(static_cast<S2PointRegion*>(bRegion)->point()));
return static_cast<S2PointRegion*>(aRegion.get())
->MayIntersect(S2Cell(static_cast<S2PointRegion*>(bRegion.get())->point()));
case GeoShape::LINESTRING:
return static_cast<S2Polyline*>(bRegion)->MayIntersect(
S2Cell(static_cast<S2PointRegion*>(aRegion)->point()));
return static_cast<S2Polyline*>(bRegion.get())
->MayIntersect(S2Cell(static_cast<S2PointRegion*>(aRegion.get())->point()));
case GeoShape::POLYGON:
return static_cast<S2Polygon*>(bRegion)->MayIntersect(
S2Cell(static_cast<S2PointRegion*>(aRegion)->point()));
return static_cast<S2Polygon*>(bRegion.get())
->MayIntersect(S2Cell(static_cast<S2PointRegion*>(aRegion.get())->point()));
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return -1.0;
return false;
}
}
case GeoShape::LINESTRING: {
switch (b.shape()) {
case GeoShape::POINT:
return static_cast<S2Polyline*>(aRegion)->MayIntersect(
S2Cell(static_cast<S2PointRegion*>(bRegion)->point()));
return static_cast<S2Polyline*>(aRegion.get())
->MayIntersect(S2Cell(static_cast<S2PointRegion*>(bRegion.get())->point()));
case GeoShape::LINESTRING:
return static_cast<S2Polyline*>(aRegion)->Intersects(static_cast<S2Polyline*>(bRegion));
return static_cast<S2Polyline*>(aRegion.get())
->Intersects(static_cast<S2Polyline*>(bRegion.get()));
case GeoShape::POLYGON:
return static_cast<S2Polygon*>(bRegion)->Intersects(*static_cast<S2Polyline*>(aRegion));
return static_cast<S2Polygon*>(bRegion.get())
->Intersects(*static_cast<S2Polyline*>(aRegion.get()));
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return -1.0;
return false;
}
}
case GeoShape::POLYGON: {
switch (b.shape()) {
case GeoShape::POINT:
return static_cast<S2Polygon*>(aRegion)->MayIntersect(
S2Cell(static_cast<S2PointRegion*>(bRegion)->point()));
return static_cast<S2Polygon*>(aRegion.get())
->MayIntersect(S2Cell(static_cast<S2PointRegion*>(bRegion.get())->point()));
case GeoShape::LINESTRING:
return static_cast<S2Polygon*>(aRegion)->Intersects(*static_cast<S2Polyline*>(bRegion));
return static_cast<S2Polygon*>(aRegion.get())
->Intersects(*static_cast<S2Polyline*>(bRegion.get()));
case GeoShape::POLYGON:
return static_cast<S2Polygon*>(aRegion)->Intersects(static_cast<S2Polygon*>(bRegion));
return static_cast<S2Polygon*>(aRegion.get())
->Intersects(static_cast<S2Polygon*>(bRegion.get()));
default:
LOG(FATAL)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return -1.0;
return false;
}
}
}
Expand Down

0 comments on commit e07009c

Please sign in to comment.