23#ifndef VCL_SPACE_COMPLEX_GRID_REGULAR_GRID_H
24#define VCL_SPACE_COMPLEX_GRID_REGULAR_GRID_H
26#include "iterators/cell_iterator.h"
28#include <vclib/space/core/box.h>
29#include <vclib/types/view.h>
33template<
typename Scalar,
int N>
38 "Number of dimensions of the regular grid must be > 1.");
49 static const int DIM = N;
50 using ScalarType = Scalar;
66 mBBox(
bbox), mSize(size)
79 Scalar
length(uint d)
const {
return mBBox.
dim(d); }
88 for (
size_t i = 0;
i < DIM; ++
i)
114 unsigned long int ind = c[0];
116 for (
unsigned int i = 1;
i < N;
i++) {
132 for (
long int i = N - 1;
i >= 0;
i--) {
133 c[
i] = index % mSize[
i];
153 for (
size_t i = 0;
i < DIM; ++
i)
158 Scalar cellDiagonal()
const {
return cellLengths().norm(); }
160 uint cell(uint d,
const Scalar& s)
const
162 if (s < mBBox.
min()(d))
164 if (s > mBBox.
max()(d))
166 Scalar t = s - mBBox.
min()(d);
170 CellCoord cell(
const Point<Scalar, N>& p)
const
173 for (
size_t i = 0; i < DIM; ++i)
174 c(i) = cell(i, p(i));
178 Point<Scalar, N> cellLowerCorner(
const CellCoord& c)
const
181 for (
size_t i = 0; i < DIM; ++i)
186 BBoxType cellBox(
const CellCoord& c)
const
191 for (
size_t i = 0; i < DIM; ++i)
200 CellIterator cellBegin()
const {
return CellIterator(CellCoord(), mSize); }
202 CellIterator cellBegin(
const CellCoord& first,
const CellCoord& last)
const
204 return CellIterator(first, last + 1);
207 CellIterator cellEnd()
const {
return CellIterator(); }
209 CellView cells()
const {
return CellView(cellBegin(), cellEnd()); }
211 CellView cells(
const CellCoord& first,
const CellCoord& last)
const
213 return CellView(cellBegin(first, last), cellEnd());
217 void set(
const Box<Point<Scalar, N>>& box,
const Point<uint, N>& size)
226template<
typename Scalar>
227using RegularGrid2 = RegularGrid<Scalar, 2>;
229template<
typename Scalar>
230using RegularGrid3 = RegularGrid<Scalar, 3>;
234template<Po
intConcept Po
intType,
typename D>
235RegularGrid(PointType, PointType, D)
236 -> RegularGrid<typename PointType::ScalarType, PointType::DIM>;
249template<Po
intConcept Po
intType>
250Point<uint, PointType::DIM> bestGridSize(
251 const PointType& lengths,
254 using Scalar = PointType::ScalarType;
256 static const int DIM = PointType::DIM;
258 const uint mincells = 1;
259 const Scalar GFactor = 1;
260 const Scalar diag = lengths.norm();
261 const Scalar eps = diag * 1e-4;
262 const uint ncell = nElements * GFactor;
264 Point<uint, PointType::DIM> sizes;
265 sizes.setConstant(mincells);
267 bool sanityCheckLengths =
true;
268 uint lessEpsDimNumber = 0;
269 std::array<bool, DIM> isLessEps;
271 for (uint i = 0; i < DIM; i++) {
272 sanityCheckLengths = sanityCheckLengths && lengths(i) > 0.0;
273 if (lengths(i) < eps) {
278 uint greaterEpsDimNumber = DIM - lessEpsDimNumber;
280 if (nElements > 0 && sanityCheckLengths) {
282 if (greaterEpsDimNumber == DIM) {
284 for (uint i = 0; i < DIM; i++)
285 product *= lengths(i);
287 Scalar k = std::pow((ncell / product), (1.0 / DIM));
289 for (uint i = 0; i < DIM; i++)
290 sizes(i) = int(lengths(i) * k);
294 for (uint i = 0; i < DIM; i++) {
302 for (uint j = 0; j < DIM; j++)
303 if (j != i && !isLessEps[j])
304 product *= lengths(j);
308 (ncell * lengths(i) / product),
309 (1.0 / greaterEpsDimNumber));
314 for (uint i = 0; i < DIM; i++)
315 sizes(i) = std::max(sizes(i), (uint) 1);
PointT & max()
Returns a reference to the maximum point of the box.
Definition box.h:107
PointT & min()
Returns a reference to the minimum point of the box.
Definition box.h:93
auto dim(uint i) const
Get the length of the box along a given dimension.
Definition box.h:295
Definition regular_grid.h:35
Point< Scalar, N > lengths() const
Returns the edge legths of the bounding box of the grid.
Definition regular_grid.h:85
uint indexOfCell(const CellCoord &c) const
Return an unique index that can be associated to the given cell coordinate.
Definition regular_grid.h:112
Scalar cellLength(uint d) const
Returns the length of a cell of the grid in the d-th dimension.
Definition regular_grid.h:144
Scalar length(uint d) const
Returns the edge legth of the bounding box of the grid in the d-th dimension.
Definition regular_grid.h:79
uint cellNumber(uint d) const
Returns the number of cells of the Grid in the d-th dimension.
Definition regular_grid.h:98
Point< uint, N > cellNumbers() const
Returns the number of cells for each dimension of the grid.
Definition regular_grid.h:104
Point< Scalar, N > cellLengths() const
Returns the lengths of a cell of the grid for each dimension.
Definition regular_grid.h:150
CellCoord cellOfIndex(uint index) const
Returns the cell coordinate associated to the given unique index.
Definition regular_grid.h:129
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
constexpr auto max(const T &p1, const T &p2)
Returns the maximum between the two parameters.
Definition min_max.h:83
constexpr auto min(const T &p1, const T &p2)
Returns the minimum between the two parameters.
Definition min_max.h:42