24 #include "mongo/client/export_macros.h"
29 #include "mongo/util/assert_util.h"
35 template <
typename TCoordinates>
38 template <
typename TCoordinates>
42 static std::vector<double> parseCoords(
const BSONElement& coordArr);
43 static std::vector<double> parseCoords(
const BSONObj& bson);
45 static std::vector<Point<TCoordinates> > parsePointArray(
46 const std::vector<BSONElement>& pointArr);
47 static std::vector<Point<TCoordinates> > parseAllPoints(
const BSONObj& bson);
86 static void findMinAndMaxCoordinatesOfDimension(
const std::vector<
Point<TCoordinates> >& points,
92 template <
typename TCoordinates>
96 "bson must contain a field \"coordinates\" of type Array",
101 template <
typename TCoordinates>
102 std::vector<double> Geometry<TCoordinates>::parseCoords(
const BSONElement& coordArr) {
103 std::vector<BSONElement> coordElems = coordArr.Array();
104 std::vector<double> coords;
105 for (
size_t i = 0; i < coordElems.size(); ++i)
106 coords.push_back(coordElems[i].Double());
110 template <
typename TCoordinates>
111 std::vector<double> Geometry<TCoordinates>::parseCoords(
const BSONObj& bson) {
112 return parseCoords(getCoordsField(bson));
115 template <
typename TCoordinates>
116 Point<TCoordinates> Geometry<TCoordinates>::parsePoint(
const BSONElement& coordArr) {
117 TCoordinates pointCoords(parseCoords(coordArr));
118 return Point<TCoordinates>(pointCoords);
121 template <
typename TCoordinates>
122 std::vector<Point<TCoordinates> > Geometry<TCoordinates>::parsePointArray(
123 const std::vector<BSONElement>& pointArr) {
124 std::vector<Point<TCoordinates> > points;
125 for (
size_t i = 0; i < pointArr.size(); ++i) {
126 points.push_back(parsePoint(pointArr[i]));
131 template <
typename TCoordinates>
132 std::vector<Point<TCoordinates> > Geometry<TCoordinates>::parseAllPoints(
const BSONObj& bson) {
133 return parsePointArray(getCoordsField(bson).
Array());
136 template <
typename TCoordinates>
147 std::vector<double> minCoordComponents, maxCoordComponents;
148 for (
size_t i = 0; i < TCoordinates::dimensionality(); ++i) {
150 findMinAndMaxCoordinatesOfDimension(points, i, &min, &max);
151 minCoordComponents.push_back(min);
152 maxCoordComponents.push_back(max);
154 TCoordinates minCoords(minCoordComponents);
155 TCoordinates maxCoords(maxCoordComponents);
159 template <
typename TCoordinates>
161 const std::vector<
Point<TCoordinates> >& points,
size_t dimension,
double* min,
double* max) {
167 *min = std::numeric_limits<double>::max();
168 *max = -std::numeric_limits<double>::max();
171 for (
size_t i = 0; i < points.size(); ++i) {
172 if (points[i][dimension] < *min)
173 *min = points[i][dimension];
174 if (points[i][dimension] > *max)
175 *max = points[i][dimension];
179 template <
typename TCoordinates>
185 std::vector<double> minCoords = bboxes[0].getMin().getValues();
186 std::vector<double> maxCoords = bboxes[0].getMax().getValues();
187 for (
size_t i = 1; i < bboxes.size(); ++i) {
188 std::vector<double> curMin = bboxes[i].getMin().getValues();
189 std::vector<double> curMax = bboxes[i].getMax().getValues();
190 for (
size_t j = 0; j < curMin.size(); ++j) {
191 minCoords[j] = std::min(minCoords[j], curMin[j]);
192 maxCoords[j] = std::max(maxCoords[j], curMax[j]);
196 TCoordinates globalMin(minCoords), globalMax(maxCoords);
200 template <
typename TCoordinates>
202 if (bson.
hasField(kBoundingBoxFieldName))
static BoundingBox< TCoordinates > * parseBoundingBox(const BSONObj &bson)
Parses the bounding box defined by the given geometry shape, represented in BSON. ...
Definition: geometry.h:201
bool hasField(const StringData &name) const
Definition: bsonobj.h:250
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
BSONType type() const
Returns the type of the element.
Definition: bsonelement.h:154
BSONElement getField(const StringData &name) const
Get the field of the specified name.
Represents a Point.
Definition: geometry.h:36
bool eoo() const
Indicates if it is the end-of-object element, which is present at the end of every BSON object...
Definition: bsonelement.h:172
an embedded array
Definition: bsontypes.h:50
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
Definition: geometry.h:39
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
static BoundingBox< TCoordinates > * computeBoundingBox(const std::vector< Point< TCoordinates > > &points)
Compute the bounding box around the given points.
Definition: geometry.h:137
Represents a bounding box.
Definition: boundingbox.h:67