diff options
author | Hari Khalsa <hkhalsa@10gen.com> | 2013-01-11 10:33:20 -0500 |
---|---|---|
committer | Hari Khalsa <hkhalsa@10gen.com> | 2013-01-11 10:33:36 -0500 |
commit | 4968e36adf286829d80dc857f515d77ace6a801a (patch) | |
tree | 000664025b1b2f061ffb2b92663ec61b3f8b949f /src/mongo/db/geo/geoparser.cpp | |
parent | 2e7a1a24160a09c9de2df4caad0fc9c2a278b998 (diff) | |
download | mongo-4968e36adf286829d80dc857f515d77ace6a801a.tar.gz |
use s2's loop orientation routines
Diffstat (limited to 'src/mongo/db/geo/geoparser.cpp')
-rw-r--r-- | src/mongo/db/geo/geoparser.cpp | 36 |
1 files changed, 9 insertions, 27 deletions
diff --git a/src/mongo/db/geo/geoparser.cpp b/src/mongo/db/geo/geoparser.cpp index f6c4772b6a8..624787774aa 100644 --- a/src/mongo/db/geo/geoparser.cpp +++ b/src/mongo/db/geo/geoparser.cpp @@ -168,27 +168,6 @@ namespace mongo { return true; } - void fixOrientationTo(vector<S2Point>* points, const bool wantClockwise) { - const vector<S2Point>& pointsRef = *points; - massert(16463, "Don't have enough points in S2 orientation fixing to work with", - 4 <= points->size()); - double sum = 0; - // Sum the area under the curve...well really, it's twice the area. - for (size_t i = 0; i < pointsRef.size(); ++i) { - // TODO(hk): Convince yourself thoroughly that this is right. - S2Point a = pointsRef[i]; - S2Point b = pointsRef[(i + 1) % pointsRef.size()]; - sum += (b[1] - a[1]) * (b[0] - a[0]); - } - // If sum > 0, clockwise - // If sum < 0, counter-clockwise - bool isClockwise = sum > 0; - if (isClockwise != wantClockwise) { - vector<S2Point> reversed(pointsRef.rbegin(), pointsRef.rend()); - *points = reversed; - } - } - void GeoParser::parseGeoJSONPolygon(const BSONObj& obj, S2Polygon* out) { const vector<BSONElement>& coordinates = obj.getFieldDotted(GEOJSON_COORDINATES).Array(); @@ -197,13 +176,13 @@ namespace mongo { vector<S2Point> exteriorVertices; parsePoints(exteriorRing, &exteriorVertices); - // The exterior ring must be counter-clockwise. We fix it up for the user. - fixOrientationTo(&exteriorVertices, false); - S2PolygonBuilderOptions polyOptions; polyOptions.set_validate(true); S2PolygonBuilder polyBuilder(polyOptions); S2Loop exteriorLoop(exteriorVertices); + if (exteriorLoop.is_hole()) { + exteriorLoop.Invert(); + } polyBuilder.AddLoop(&exteriorLoop); // Subsequent arrays of coordinates are interior rings/holes. @@ -211,8 +190,10 @@ namespace mongo { vector<S2Point> holePoints; parsePoints(coordinates[i].Array(), &holePoints); // Interior rings are clockwise. - fixOrientationTo(&holePoints, true); S2Loop holeLoop(holePoints); + if (!holeLoop.is_hole()) { + holeLoop.Invert(); + } polyBuilder.AddLoop(&holeLoop); } @@ -267,12 +248,13 @@ namespace mongo { } points.push_back(points[0]); - fixOrientationTo(&points, false); - S2PolygonBuilderOptions polyOptions; polyOptions.set_validate(true); S2PolygonBuilder polyBuilder(polyOptions); S2Loop exteriorLoop(points); + if (exteriorLoop.is_hole()) { + exteriorLoop.Invert(); + } polyBuilder.AddLoop(&exteriorLoop); polyBuilder.AssemblePolygon(out, NULL); } |