INET Framework for OMNeT++/OMNEST
inet::SpatialGrid Class Reference

This class implements a spatial grid data structure using a 3 dimensional grid. More...

#include <SpatialGrid.h>

Classes

class  LineSegmentIterator
 
class  Triplet
 

Public Types

typedef std::list< const cObject * > Voxel
 
typedef std::vector< VoxelGrid
 

Public Member Functions

bool insertObject (const cObject *object, const Coord &pos, const Coord &boundingBoxSize)
 
bool insertPoint (const cObject *point, const Coord &pos)
 
bool removePoint (const cObject *point)
 
bool movePoint (const cObject *point, const Coord &newPos)
 
void clearGrid ()
 
void rangeQuery (const Coord &pos, double range, const IVisitor *visitor) const
 
void lineSegmentQuery (const LineSegment &lineSegment, const IVisitor *visitor) const
 
 SpatialGrid (const Coord &voxelSizes, const Coord &constraintAreaMin, const Coord &constraintAreaMax)
 

Protected Member Functions

Coord computeConstraintAreaSideLengths () const
 
Triplet< int > computeNumberOfVoxels () const
 
unsigned int computeGridVectorLength () const
 
Triplet< int > decodeRowMajorIndex (unsigned int ind) const
 
unsigned int rowMajorIndex (const Triplet< int > &indices) const
 
unsigned int coordToRowMajorIndex (const Coord &pos) const
 
Triplet< int > coordToMatrixIndices (const Coord &pos) const
 
void computeBoundingVoxels (const Coord &pos, const Triplet< double > &boundings, Triplet< int > &start, Triplet< int > &end) const
 

Protected Attributes

Grid grid
 
unsigned int gridVectorLength
 
Triplet< double > voxelSizes
 
Coord constraintAreaSideLengths
 
Coord constraintAreaMin
 
Coord constraintAreaMax
 
Triplet< int > numVoxels
 

Detailed Description

This class implements a spatial grid data structure using a 3 dimensional grid.

It can also be used with zero-length {X, Y, Z} sides, and then it operates in 1 or 2 dimensional mode.

NOTE: With minimal effort, it can be extended to work in arbitrary dimension spaces.

Member Typedef Documentation

typedef std::vector<Voxel> inet::SpatialGrid::Grid
typedef std::list<const cObject *> inet::SpatialGrid::Voxel

Constructor & Destructor Documentation

inet::SpatialGrid::SpatialGrid ( const Coord voxelSizes,
const Coord constraintAreaMin,
const Coord constraintAreaMax 
)
57 {
58  this->voxelSizes = Triplet<double>(voxelSizes.x, voxelSizes.y, voxelSizes.z);
64  if (gridVectorLength <= 0)
65  throw cRuntimeError("Invalid gridVectorLength = %d", gridVectorLength);
66  grid.resize(gridVectorLength);
67 }
T z
Definition: SpatialGrid.h:44
unsigned int computeGridVectorLength() const
Definition: SpatialGrid.cc:141
Coord computeConstraintAreaSideLengths() const
Definition: SpatialGrid.cc:127
Coord constraintAreaMin
Definition: SpatialGrid.h:100
unsigned int gridVectorLength
Definition: SpatialGrid.h:97
T x
Definition: SpatialGrid.h:42
Coord constraintAreaSideLengths
Definition: SpatialGrid.h:99
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
T y
Definition: SpatialGrid.h:43
Triplet< int > computeNumberOfVoxels() const
Definition: SpatialGrid.cc:133
Triplet< double > voxelSizes
Definition: SpatialGrid.h:98
Coord constraintAreaMax
Definition: SpatialGrid.h:100
Grid grid
Definition: SpatialGrid.h:96

Member Function Documentation

void inet::SpatialGrid::clearGrid ( )
185 {
186  for (unsigned int i = 0; i < gridVectorLength; i++)
187  grid[i].clear();
188 }
unsigned int gridVectorLength
Definition: SpatialGrid.h:97
Grid grid
Definition: SpatialGrid.h:96
void inet::SpatialGrid::computeBoundingVoxels ( const Coord pos,
const Triplet< double > &  boundings,
Triplet< int > &  start,
Triplet< int > &  end 
) const
protected

Referenced by insertObject(), and rangeQuery().

39 {
40  int xVoxel = constraintAreaSideLengths.x == 0 ? 0 : ceil((boundings.x * numVoxels[0]) / constraintAreaSideLengths.x);
41  int yVoxel = constraintAreaSideLengths.y == 0 ? 0 : ceil((boundings.y * numVoxels[1]) / constraintAreaSideLengths.y);
42  int zVoxel = constraintAreaSideLengths.z == 0 ? 0 : ceil((boundings.z * numVoxels[2]) / constraintAreaSideLengths.z);
43  Triplet<int> voxels(xVoxel, yVoxel, zVoxel);
44  Triplet<int> matrixIndices = coordToMatrixIndices(pos);
45  for (unsigned int i = 0; i < 3; i++)
46  {
47  start[i] = matrixIndices[i] - voxels[i] < 0 ? 0 : matrixIndices[i] - voxels[i];
48  int endCell = matrixIndices[i] + voxels[i] >= numVoxels[i] ? numVoxels[i] - 1 : matrixIndices[i] + voxels[i];
49  if (endCell < 0)
50  end[i] = 0;
51  else
52  end[i] = endCell;
53  }
54 }
T z
Definition: SpatialGrid.h:44
double z
Definition: Coord.h:51
T x
Definition: SpatialGrid.h:42
Coord constraintAreaSideLengths
Definition: SpatialGrid.h:99
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
T y
Definition: SpatialGrid.h:43
Triplet< int > coordToMatrixIndices(const Coord &pos) const
Definition: SpatialGrid.cc:190
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
Coord inet::SpatialGrid::computeConstraintAreaSideLengths ( ) const
protected

Referenced by SpatialGrid().

128 {
131 }
Coord constraintAreaMin
Definition: SpatialGrid.h:100
double z
Definition: Coord.h:51
double y
Definition: Coord.h:50
Coord constraintAreaMax
Definition: SpatialGrid.h:100
double x
Definition: Coord.h:49
unsigned int inet::SpatialGrid::computeGridVectorLength ( ) const
protected

Referenced by SpatialGrid().

142 {
143  unsigned int gridVectorLength = 1;
144  for (unsigned int i = 0; i < 3; i++)
145  if (numVoxels[i] != 0)
146  gridVectorLength *= numVoxels[i];
147  return gridVectorLength;
148 }
unsigned int gridVectorLength
Definition: SpatialGrid.h:97
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
SpatialGrid::Triplet< int > inet::SpatialGrid::computeNumberOfVoxels ( ) const
protected

Referenced by SpatialGrid().

134 {
135  return Triplet<int>(
139 }
T z
Definition: SpatialGrid.h:44
double z
Definition: Coord.h:51
T x
Definition: SpatialGrid.h:42
Coord constraintAreaSideLengths
Definition: SpatialGrid.h:99
T y
Definition: SpatialGrid.h:43
Triplet< double > voxelSizes
Definition: SpatialGrid.h:98
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
SpatialGrid::Triplet< int > inet::SpatialGrid::coordToMatrixIndices ( const Coord pos) const
protected

Referenced by computeBoundingVoxels(), coordToRowMajorIndex(), and inet::SpatialGrid::LineSegmentIterator::LineSegmentIterator().

191 {
192  int xCoord = numVoxels[0] == 0 ? 0 : std::min((int)floor((pos.x - constraintAreaMin.x) / voxelSizes.x), numVoxels[0] - 1);
193  int yCoord = numVoxels[1] == 0 ? 0 : std::min((int)floor((pos.y - constraintAreaMin.y) / voxelSizes.y), numVoxels[1] - 1);
194  int zCoord = numVoxels[2] == 0 ? 0 : std::min((int)floor((pos.z - constraintAreaMin.z) / voxelSizes.z), numVoxels[2] - 1);
195  return Triplet<int>(xCoord, yCoord, zCoord);
196 }
T z
Definition: SpatialGrid.h:44
Coord constraintAreaMin
Definition: SpatialGrid.h:100
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SCTPAssociation.h:270
double z
Definition: Coord.h:51
T x
Definition: SpatialGrid.h:42
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
T y
Definition: SpatialGrid.h:43
Triplet< double > voxelSizes
Definition: SpatialGrid.h:98
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
unsigned int inet::SpatialGrid::coordToRowMajorIndex ( const Coord pos) const
protected

Referenced by insertPoint().

180 {
181  return rowMajorIndex(coordToMatrixIndices(pos));
182 }
Triplet< int > coordToMatrixIndices(const Coord &pos) const
Definition: SpatialGrid.cc:190
unsigned int rowMajorIndex(const Triplet< int > &indices) const
Definition: SpatialGrid.cc:165
SpatialGrid::Triplet< int > inet::SpatialGrid::decodeRowMajorIndex ( unsigned int  ind) const
protected
151 {
152  Triplet<int> indices;
153  for (unsigned int k = 0; k < 3; k++)
154  {
155  unsigned int prodDim = 1;
156  for (unsigned int l = k + 1; l < 3; l++)
157  if (numVoxels[l] > 0)
158  prodDim *= numVoxels[l];
159  indices[k] = ind / prodDim;
160  ind %= prodDim;
161  }
162  return indices;
163 }
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
const double k
Definition: QAM16Modulation.cc:24
bool inet::SpatialGrid::insertObject ( const cObject *  object,
const Coord pos,
const Coord boundingBoxSize 
)

Referenced by inet::physicalenvironment::GridObjectCache::insertObject().

23 {
24  Triplet<int> start, end;
25  computeBoundingVoxels(pos, Triplet<double>(boundingBoxSize.x / 2, boundingBoxSize.y / 2, boundingBoxSize.z / 2), start, end);
26  for (int i = start[0]; i <= end[0]; i++) {
27  for (int j = start[1]; j <= end[1]; j++) {
28  for (int k = start[2]; k <= end[2]; k++) {
29  int voxelIndex = rowMajorIndex(Triplet<int>(i,j,k));
30  Voxel& neighborVoxel = grid[voxelIndex];
31  neighborVoxel.push_back(check_and_cast<const cObject*>(object));
32  }
33  }
34  }
35  return true;
36 }
std::list< const cObject * > Voxel
Definition: SpatialGrid.h:92
Grid grid
Definition: SpatialGrid.h:96
const double k
Definition: QAM16Modulation.cc:24
void computeBoundingVoxels(const Coord &pos, const Triplet< double > &boundings, Triplet< int > &start, Triplet< int > &end) const
Definition: SpatialGrid.cc:38
unsigned int rowMajorIndex(const Triplet< int > &indices) const
Definition: SpatialGrid.cc:165
bool inet::SpatialGrid::insertPoint ( const cObject *  point,
const Coord pos 
)

Referenced by inet::physicallayer::GridNeighborCache::addRadio(), and inet::physicallayer::GridNeighborCache::fillCubeVector().

70 {
71  unsigned int ind = coordToRowMajorIndex(pos);
72  if (ind >= gridVectorLength)
73  {
74  throw cRuntimeError("Out of range with index: %d", ind);
75  return false;
76  }
77  grid[ind].push_back(point);
78  return true;
79 }
unsigned int gridVectorLength
Definition: SpatialGrid.h:97
unsigned int coordToRowMajorIndex(const Coord &pos) const
Definition: SpatialGrid.cc:179
Grid grid
Definition: SpatialGrid.h:96
void inet::SpatialGrid::lineSegmentQuery ( const LineSegment lineSegment,
const IVisitor visitor 
) const

Referenced by inet::physicalenvironment::GridObjectCache::visitObjects().

108 {
109  std::map<const cObject *,bool> visited;
110  for (LineSegmentIterator it(this, lineSegment, voxelSizes, numVoxels); !it.end(); ++it)
111  {
112  Triplet<int> ind = it.getMatrixIndices();
113  unsigned int voxelIndex = rowMajorIndex(ind);
114  if (voxelIndex >= gridVectorLength)
115  throw cRuntimeError("Out of index, gridVectorLength = %d, voxelIndex = %d", gridVectorLength, voxelIndex);
116  const Voxel& intersectedVoxel = grid[voxelIndex];
117  for (const auto & elem : intersectedVoxel)
118  {
119  std::map<const cObject *, bool>::const_iterator objIter = visited.find(elem);
120  if (objIter == visited.end() || !objIter->second)
121  visitor->visit(elem);
122  visited.insert(std::pair<const cObject*,bool>(elem, true));
123  }
124  }
125 }
unsigned int gridVectorLength
Definition: SpatialGrid.h:97
std::list< const cObject * > Voxel
Definition: SpatialGrid.h:92
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
Triplet< double > voxelSizes
Definition: SpatialGrid.h:98
Grid grid
Definition: SpatialGrid.h:96
unsigned int rowMajorIndex(const Triplet< int > &indices) const
Definition: SpatialGrid.cc:165
bool inet::SpatialGrid::movePoint ( const cObject *  point,
const Coord newPos 
)
87 {
88  throw cRuntimeError("Unimplemented");
89 }
void inet::SpatialGrid::rangeQuery ( const Coord pos,
double  range,
const IVisitor visitor 
) const

Referenced by inet::physicallayer::GridNeighborCache::sendToNeighbors().

92 {
93  Triplet<int> start, end;
94  computeBoundingVoxels(pos, Triplet<double>(range, range, range), start, end);
95  for (int i = start[0]; i <= end[0]; i++) {
96  for (int j = start[1]; j <= end[1]; j++) {
97  for (int k = start[2]; k <= end[2]; k++) {
98  int voxelIndex = rowMajorIndex(Triplet<int>(i,j,k));
99  const Voxel& neighborVoxel = grid[voxelIndex];
100  for (const auto & elem : neighborVoxel)
101  visitor->visit(elem);
102  }
103  }
104  }
105 }
std::list< const cObject * > Voxel
Definition: SpatialGrid.h:92
Grid grid
Definition: SpatialGrid.h:96
const double k
Definition: QAM16Modulation.cc:24
void computeBoundingVoxels(const Coord &pos, const Triplet< double > &boundings, Triplet< int > &start, Triplet< int > &end) const
Definition: SpatialGrid.cc:38
unsigned int rowMajorIndex(const Triplet< int > &indices) const
Definition: SpatialGrid.cc:165
bool inet::SpatialGrid::removePoint ( const cObject *  point)
82 {
83  throw cRuntimeError("Unimplemented");
84 }
unsigned int inet::SpatialGrid::rowMajorIndex ( const Triplet< int > &  indices) const
protected

Referenced by coordToRowMajorIndex(), insertObject(), lineSegmentQuery(), and rangeQuery().

166 {
167  int ind = 0;
168  for (unsigned int k = 0; k < 3; k++)
169  {
170  unsigned int prodDim = 1;
171  for (unsigned int l = k + 1; l < 3; l++)
172  if (numVoxels[l] > 0)
173  prodDim *= numVoxels[l];
174  ind += prodDim * indices[k];
175  }
176  return ind;
177 }
Triplet< int > numVoxels
Definition: SpatialGrid.h:101
const double k
Definition: QAM16Modulation.cc:24

Member Data Documentation

Coord inet::SpatialGrid::constraintAreaMax
protected
Coord inet::SpatialGrid::constraintAreaSideLengths
protected
Grid inet::SpatialGrid::grid
protected
unsigned int inet::SpatialGrid::gridVectorLength
protected
Triplet<double> inet::SpatialGrid::voxelSizes
protected

The documentation for this class was generated from the following files: