10 #include <unordered_map>
13 #include "makUtil/dataTypes.h"
14 #include "makUtil/array.h"
15 #include "makUtil/vector.h"
18 #include <immintrin.h>
19 #include <tbb/spin_mutex.h>
23 template<
typename DATA_ID>
38 Bool insert(
const DATA_ID globalId,
const Vector3d& pos);
42 Bool remove(
const DATA_ID globalId);
44 Bool update(
const DATA_ID globalId,
const Vector3d& pos);
48 std::vector<DATA_ID>
nearestNeighbors(
const Vector3d& loc,
double distanceInMeter)
const;
50 std::vector<DATA_ID>
objectsWithin(
const Vector3d& min,
const Vector3d& max)
const;
52 std::vector<DATA_ID>
objectsWithin(
const Vector2d& minLatLonDeg,
const Vector2d& maxLatLonDeg)
const;
54 std::vector<DATA_ID>
objectsNearChord(
const Vector3d& pointA,
const Vector3d& pointB,
const double delta)
const;
72 double radius = pos.length();
74 ret.x = atan2(pos.y, pos.x);
75 ret.y = radius ? acos(pos.z / radius) : 0;
78 ret.x = (ret.x + 3.1415926535897932);
124 for (
UInt64 bb = 1; mask; bb += bb) {
140 static const UInt64 bitmask = (1ULL << (BITS_PER_COMPONENT * 2)) - 1;
143 static double qauntizationFactor = quantization / 6.283185307179586;
146 UInt64 azimuth = spos.x * qauntizationFactor;
147 UInt64 elevation = spos.y * qauntizationFactor;
149 UInt64 region = azimuth / (quantization / 4);
152 azimuth =
my_pdep_u64(azimuth, 0x5555555555555555);
153 elevation =
my_pdep_u64(elevation, 0xaaaaaaaaaaaaaaaa);
156 UInt64 ret = region << (BITS_PER_COMPONENT * 2);
185 const Vector3d& absMin,
const Vector3d& absMax, std::vector<DATA_ID> &result)
const;
187 const Vector3d& pos1,
const Vector3d& pos2,
const double distance,
188 std::vector<DATA_ID> &result)
const;
192 void addToPage(Page& p, DATA_ID globalId,
UInt64 key,
const Vector3d& loc);
196 bool isPositionWithin(
const Vector3d &min,
const Vector3d &max,
const Vector3d &pos)
const;
197 bool isPositionWithin(
const Vector2d &latLong1,
const Vector2d &latLong2,
const Vector3d &pos)
const;
233 dataMutex = std::shared_ptr<tbb::spin_mutex>(
new tbb::spin_mutex);
253 #include "spatial.inl"
std::vector< Node > myNodes
Definition: spatial.h:243
unsigned int UInt32
Definition: stateDataTypes.h:18
bool generateMinMaxKeys(const Vector3d &pos1, const Vector3d &pos2, UInt64 &minKey, UInt64 &maxKey, Vector3d &minPos, Vector3d &maxPos) const
std::vector< Page > myPages
Definition: spatial.h:244
Page(const Page &orig)
Definition: spatial.h:237
UInt32 depth
Definition: spatial.h:211
UInt32 myPageCount
Definition: spatial.h:241
std::vector< Entry > data
Definition: spatial.h:227
static const UInt64 MASK_ELEVATION_EBITS_REVERSE
Definition: spatial.h:204
Bool canSplit(const NodeIndex nodeIdx) const
NodeIndex findNodeContaining(NodeIndex, const UInt64 key) const
static const int SpatialMaxNodeDepth
Definition: spatial.h:206
std::mutex myPageMutex
Definition: spatial.h:245
static const int BITS_PER_COMPONENT
Definition: spatial.h:26
void addToPage(Page &p, DATA_ID globalId, UInt64 key, const Vector3d &loc)
Vector3d location
Definition: spatial.h:219
Bool update(const DATA_ID globalId, const Vector3d &pos)
Update entity's location.
bool isWholeGlobeRegion(const Vector3d &pos1, const Vector3d &pos2) const
SpatialIndex()
Public APIs.
std::unordered_map< DATA_ID, int > myReverseLookup
Definition: spatial.h:249
UInt8 Bool
Definition: stateDataTypes.h:20
std::mutex myNodeMutex
Definition: spatial.h:246
DT_DLL_VRVCORE double distance(const makVrv::DtCoordinateSystem &, const DtVector &from, const DtVector &to)
Returns the distance from the two points. Coordinates are in local database coordinates The coordinat...
bool isPositionWithin(const Vector3d &min, const Vector3d &max, const Vector3d &pos) const
unsigned int NodeIndex
Definition: spatial.h:34
int findNodeToAdd(const UInt64 key)
NodeIndex findNodeToAdd(const UInt64 key);.
Definition: spatial.h:215
Bool needSplit(const NodeIndex nodeIdx) const
UInt64 key
Definition: spatial.h:217
bool isDistanceToLineWithin(const Vector3d &pointC, double distance, const Vector3d &lineA, const Vector3d &lineB) const
Find the shortest distance from the point C to the line.
void reset()
Reset the entity entries, but maintains the node tree since that is likely useful the next frame...
unsigned long long UInt64
Definition: stateDataTypes.h:17
DATA_ID globalId
Definition: spatial.h:218
Definition: spatial.h:207
void growNodes(int count)
NodeIndex findCommonRootNode(NodeIndex idx, const UInt64 key1, const UInt64 key2) const
unsigned int PageIndex
Definition: spatial.h:35
std::vector< DATA_ID > objectsNearChord(const Vector3d &pointA, const Vector3d &pointB, const double delta) const
Find objects within the delta distance from the chord defined by npoint A & B.
static const UInt64 MASK_ELEVATION_EVEN_BITS
Definition: spatial.h:202
tbb::spin_mutex::scoped_lock DataMutexLock
Definition: spatial.h:229
UInt32 pageIdx
Definition: spatial.h:212
UInt32 objectsWithinNode(const NodeIndex idx, const UInt64 minKey, const UInt64 maxKey, const Vector3d &absMin, const Vector3d &absMax, std::vector< DATA_ID > &result) const
UInt32 myNodeCount
Definition: spatial.h:240
void eraseFromPage(Page &p, DATA_ID globalId)
UInt32 children[4]
Definition: spatial.h:210
static Vector2d geocToSpherical(const Vector3d &pos)
spatial key generation specific static member functions
Definition: spatial.h:70
bool contains(UInt64 key, NodeIndex n)
static const int thePageSize
Definition: spatial.h:222
std::shared_ptr< tbb::spin_mutex > dataMutex
Definition: spatial.h:228
std::vector< DATA_ID > spatialTreeWalk(UInt32 &totalDataCnt) const
Definition: spatial.h:224
static const UInt64 MASK_AZIMUTH_OBITS_REVERSE
Definition: spatial.h:203
Bool insert(const DATA_ID globalId, const Vector3d &pos)
Generate spatial key and insert entity's global ID and location into spatial tree.
static UInt64 geocentricToSpatialKey(const Vector3d &pos)
Definition: spatial.h:132
int count
Definition: spatial.h:226
static const UInt64 MASK_AZIMUTH_ODD_BITS
60 bits masks used to encode or decode spatial keys (AZIMUTH, ELEVATION), Top 2 most significant bits...
Definition: spatial.h:201
static UInt64 my_pdep_u64(UInt64 val, UInt64 mask)
Definition: spatial.h:122
std::vector< DATA_ID > objectsWithin(const Vector3d &min, const Vector3d &max) const
Find objects within the area of min and max locations, including the min & max locations.
std::vector< DATA_ID > nearestNeighbors(const Vector3d &loc, double distanceInMeter) const
Find nearest neighbors from the input location, neighbors x/y/z locations are <= distanceInMeter. It returns an array of global IDs.
static const int BITS_PER_COMPONENT_AND_REGION
Definition: spatial.h:27
Page()
Definition: spatial.h:231
Bool splitNode(NodeIndex n)
void growPages(int count)
UInt64 key
Definition: spatial.h:209
std::queue< int > myFreePageQueue
Definition: spatial.h:242