2020
2121#include < geos/geom/Envelope.h>
2222
23- constexpr double DEFAULT_GRID_COMPAT_TOL = 1e-6 ;
24-
2523namespace geos ::operation::grid {
2624struct infinite_extent
2725{
@@ -33,11 +31,19 @@ struct bounded_extent
3331 static constexpr size_t padding = 0 ;
3432};
3533
34+ /* *
35+ * @brief The Grid class represents a grid of constant-size rectangular cells that covers a specified envelope.
36+ * The width of the cells may be different from the height. If the Grid has an "infinite" extent rather than
37+ * a "bounded" extent, then an extra row and column will be added on all side of the grid. The size of the cells
38+ * in these columns may be larger than those in the primary grid, such that the extended grid covers a "domain"
39+ * that is larger than the extent of the regular grid.
40+ */
3641template <typename extent_tag>
3742class GEOS_DLL Grid
3843{
3944
4045 public:
46+ // / Construct a bounded grid covering a specified extent.
4147 Grid (const geom::Envelope& extent, double dx, double dy)
4248 : m_extent{ extent }
4349 , m_domain{}
@@ -50,6 +56,8 @@ class GEOS_DLL Grid
5056 static_assert (std::is_same_v<extent_tag, bounded_extent>);
5157 }
5258
59+ // / Construct an infinite grid covering a specified extent with regularly-sized cells, and adding a row and column
60+ // / of variably-sized cells to each edge of the primary grid such that the specified domain is covered.
5361 Grid (const geom::Envelope& extent, double dx, double dy, const geom::Envelope& domain)
5462 : m_extent{ extent }
5563 , m_domain{ domain }
@@ -73,6 +81,7 @@ class GEOS_DLL Grid
7381 }
7482 }
7583
84+ // / Get the column in which the specified x coordinate would fall.
7685 size_t getColumn (double x) const
7786 {
7887 if (extent_tag::padding) {
@@ -98,6 +107,7 @@ class GEOS_DLL Grid
98107 getColumn (m_extent.getMaxX ()));
99108 }
100109
110+ // / Get the row in which the specified y coordinate would fall.
101111 size_t getRow (double y) const
102112 {
103113 if (extent_tag::padding) {
@@ -123,6 +133,8 @@ class GEOS_DLL Grid
123133 getRow (m_extent.getMinY ()));
124134 }
125135
136+ // / Get the cell index in which the specified x and y values would fall. Cells are indexed from left-to-right,
137+ // / then top-to-bottom.
126138 std::size_t getCell (double x, double y) const
127139 {
128140 return getRow (y) * getNumCols () + getColumn (x);
@@ -150,12 +162,20 @@ class GEOS_DLL Grid
150162
151163 const geom::Envelope& getExtent () const { return m_extent; }
152164
165+ // / Return the number of rows by which another grid is offset from this Grid. It is assumed that the two
166+ // / grids have the same resolution, and that the maximum y value of the other grid is less than or equal
167+ // / to the maximum y value of this grid.
153168 size_t getRowOffset (const Grid& other) const { return static_cast <size_t >(std::round (std::abs (other.m_extent .getMaxY () - m_extent.getMaxY ()) / m_dy)); }
154169
170+ // / Return the number of columns by which another grid is offset from this Grid. It is assumed that the two
171+ // / grids have the same resolution, and that the minimum x value of the other grid is greater than or equal
172+ // / to the minimum x value of this grid.
155173 size_t getColOffset (const Grid& other) const { return static_cast <size_t >(std::round (std::abs (m_extent.getMinX () - other.m_extent .getMinX ()) / m_dx)); }
156174
175+ // / Get the x coordinate at the center of the specified column.
157176 double getColX (size_t col) const { return m_extent.getMinX () + (static_cast <double >(col - extent_tag::padding) + 0.5 ) * m_dx; }
158177
178+ // / Get the y coordinate at the center of the specified row.
159179 double getRowY (size_t row) const { return m_extent.getMaxY () - (static_cast <double >(row - extent_tag::padding) + 0.5 ) * m_dy; }
160180
161181 Grid crop (const geom::Envelope& e) const
@@ -167,6 +187,10 @@ class GEOS_DLL Grid
167187 }
168188 }
169189
190+ // / Reduce the size of the grid to contain only the provided Envelope
191+ // / If calcExtentFromNewGrid is true, then the xmax and ymin of the new
192+ // / grid will be calculated relative to the origin point of the original grid
193+ // / rather than the newly cropped grid.
170194 Grid<extent_tag> shrinkToFit (const geom::Envelope& b, bool calcExtentFromNewGrid=true ) const
171195 {
172196 if (b.getArea () == 0 ) {
@@ -295,11 +319,12 @@ class GEOS_DLL Grid
295319 double m_dx;
296320 double m_dy;
297321
298- double m_xOrig;
322+ double m_xOrig; // origin point that is distinct from xmin of m_extent. Used to allow a subgrid to calculate
323+ // sub-envelopes that exactly match those of the parent grid
299324 double m_yOrig;
300325
301- size_t m_skipRows{0 };
302- size_t m_skipCols{0 };
326+ size_t m_skipRows{0 }; // number of rows to skip when computing a cell envelope using m_yOrig
327+ size_t m_skipCols{0 }; // number of cols to skip when computing a cell envelope using m_xOrig
303328
304329 size_t m_num_rows;
305330 size_t m_num_cols;
0 commit comments