| 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; |
nothing calls this directly
no test coverage detected