MCPcopy Create free account
hub / github.com/TombEngine/TombEngine / GetCellKeys

Method GetCellKeys

TombEngine/Specific/Structures/SpatialHash.cpp:241–312  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

239 }
240
241 std::vector<Vector3i> SpatialHash::GetCellKeys(const Ray& ray, float dist) const
242 {
243 // Reserve minimum key vector.
244 auto keys = std::vector<Vector3i>{};
245 keys.reserve(int(dist / _cellSize) + 1);
246
247 // Calculate cell position.
248 auto pos = Vector3(
249 floor(ray.position.x / _cellSize) * _cellSize,
250 floor(ray.position.y / _cellSize) * _cellSize,
251 floor(ray.position.z / _cellSize) * _cellSize);
252
253 // Calculate cell position step.
254 auto posStep = Vector3(
255 (ray.direction.x > 0) ? _cellSize : -_cellSize,
256 (ray.direction.y > 0) ? _cellSize : -_cellSize,
257 (ray.direction.z > 0) ? _cellSize : -_cellSize);
258
259 // Calculate next intersection.
260 auto nextIntersect = Vector3(
261 ((pos.x + ((posStep.x > 0) ? _cellSize : 0)) - ray.position.x) / ray.direction.x,
262 ((pos.y + ((posStep.y > 0) ? _cellSize : 0)) - ray.position.y) / ray.direction.y,
263 ((pos.z + ((posStep.z > 0) ? _cellSize : 0)) - ray.position.z) / ray.direction.z);
264
265 // Calculate ray step.
266 auto rayStep = Vector3(
267 _cellSize / abs(ray.direction.x),
268 _cellSize / abs(ray.direction.y),
269 _cellSize / abs(ray.direction.z));
270
271 // Traverse cells and collect keys.
272 float currentDist = 0.0f;
273 while (currentDist <= dist)
274 {
275 auto key = GetCellKey(pos);
276 keys.push_back(key);
277
278 // Determine which axis to step along.
279 if (nextIntersect.x < nextIntersect.y)
280 {
281 if (nextIntersect.x < nextIntersect.z)
282 {
283 pos.x += posStep.x;
284 currentDist = nextIntersect.x;
285 nextIntersect.x += rayStep.x;
286 }
287 else
288 {
289 pos.z += posStep.z;
290 currentDist = nextIntersect.z;
291 nextIntersect.z += rayStep.z;
292 }
293 }
294 else
295 {
296 if (nextIntersect.y < nextIntersect.z)
297 {
298 pos.y += posStep.y;

Callers

nothing calls this directly

Calls 11

floorFunction · 0.85
FloorToStepFunction · 0.85
GetAabbFunction · 0.85
IntersectsMethod · 0.80
Vector3Function · 0.50
reserveMethod · 0.45
push_backMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
ToVector3Method · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected