summaryrefslogtreecommitdiff
path: root/src/mongo/db/geo/shapes.h
blob: 96f8ec621ae5d9a020659833472939f087b7f5e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
*    Copyright (C) 2008-2012 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <string>
#include <vector>

#include "mongo/base/owned_pointer_vector.h"
#include "mongo/db/jsobj.h"
#include "third_party/s2/s2.h"
#include "third_party/s2/s2cap.h"
#include "third_party/s2/s2cell.h"
#include "third_party/s2/s2latlng.h"
#include "third_party/s2/s2polygon.h"
#include "third_party/s2/s2polyline.h"

namespace mongo {

    struct Point;
    double distance(const Point& p1, const Point &p2);
    bool distanceWithin(const Point &p1, const Point &p2, double radius);
    void checkEarthBounds(const Point &p);
    double spheredist_rad(const Point& p1, const Point& p2);
    double spheredist_deg(const Point& p1, const Point& p2);

    struct Point {
        Point();
        Point(double x, double y);
        explicit Point(const BSONElement& e);
        explicit Point(const BSONObj& o);
        string toString() const;

        double x;
        double y;
    };

    struct Circle {
        Circle();
        Circle(double radius, Point center);

        double radius;
        Point center;
    };

    class Box {
    public:
        Box();
        Box(double x, double y, double size);
        Box(Point min, Point max);

        BSONArray toBSON() const;
        string toString() const;

        bool between(double min, double max, double val, double fudge = 0) const;
        bool onBoundary(double bound, double val, double fudge = 0) const;
        bool mid(double amin, double amax, double bmin, double bmax, bool min, double* res) const;

        double intersects(const Box& other) const;
        double area() const;
        double maxDim() const;
        Point center() const;
        void truncate(double min, double max);
        void fudge(double error);
        bool onBoundary(Point p, double fudge = 0);
        bool inside(Point p, double fudge = 0) const;
        bool inside(double x, double y, double fudge = 0) const;
        bool contains(const Box& other, double fudge = 0);
        Point _min;
        Point _max;
    };

    class Polygon {
    public:
        Polygon();
        Polygon(vector<Point> points);

        void add(Point p);
        int size() const;

        bool contains(const Point& p) const;

        /* 
         * Return values:
         * -1 if no intersection
         * 0 if maybe an intersection (using fudge)
         * 1 if there is an intersection
         */
        int contains(const Point &p, double fudge) const;

        /**
         * Calculate the centroid, or center of mass of the polygon object.
         */
        Point centroid();
        Box bounds();
    private:
        bool _centroidCalculated;
        Point _centroid;
        Box _bounds;
        bool _boundsCalculated;
        vector<Point> _points;
    };

    // Clearly this isn't right but currently it's sufficient.
    enum CRS {
        FLAT,
        SPHERE
    };

    struct PointWithCRS {
        S2Point point;
        S2Cell cell;
        Point oldPoint;
        CRS crs;
    };

    struct LineWithCRS {
        S2Polyline line;
        CRS crs;
    };

    struct CapWithCRS {
        S2Cap cap;
        Circle circle;
        CRS crs;
    };

    struct BoxWithCRS {
        Box box;
        CRS crs;
    };

    struct PolygonWithCRS {
        S2Polygon polygon;
        Polygon oldPolygon;
        CRS crs;
    };

    struct MultiPointWithCRS {
        vector<S2Point> points;
        vector<S2Cell> cells;
        CRS crs;
    };

    struct MultiLineWithCRS {
        OwnedPointerVector<S2Polyline> lines;
        CRS crs;
    };

    struct MultiPolygonWithCRS {
        OwnedPointerVector<S2Polygon> polygons;
        CRS crs;
    };

    struct GeometryCollection {
        vector<PointWithCRS> points;

        // The amount of indirection here is painful but we can't operator= scoped_ptr or
        // OwnedPointerVector.
        OwnedPointerVector<LineWithCRS> lines;
        OwnedPointerVector<PolygonWithCRS> polygons;
        OwnedPointerVector<MultiPointWithCRS> multiPoints;
        OwnedPointerVector<MultiLineWithCRS> multiLines;
        OwnedPointerVector<MultiPolygonWithCRS> multiPolygons;

        bool supportsContains() {
            // Only polygons (and multiPolygons) support containment.
            return (polygons.vector().size() > 0 || multiPolygons.vector().size() > 0);
        }
    };

}  // namespace mongo