|
| 1 | +#include "neighbour_selection.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <random> |
| 5 | + |
| 6 | + |
| 7 | +bool areSimilar(const RayHit& lhs, const RayHit& rhs, const Features& features) { |
| 8 | + // Same geometries |
| 9 | + if (features.neighbourSameGeometry && lhs.hit.geometryId != rhs.hit.geometryId) { return false; } |
| 10 | + |
| 11 | + // Depth difference |
| 12 | + float depthFracDiff = std::abs(1.0f - (lhs.ray.t / rhs.ray.t)); |
| 13 | + if (depthFracDiff > features.neighbourMaxDepthDifferenceFraction) { return false; } |
| 14 | + |
| 15 | + // Normal angle difference |
| 16 | + float maxDiffCos = std::cos(features.neighbourMaxNormalAngleDifferenceRadians); |
| 17 | + float normalsDotProd = glm::dot(lhs.hit.normal, rhs.hit.normal); |
| 18 | + if (normalsDotProd < features.neighbourMaxNormalAngleDifferenceRadians) { return false; } |
| 19 | + |
| 20 | + // We passed the similarity test! |
| 21 | + return true; |
| 22 | +} |
| 23 | + |
| 24 | +std::vector<glm::ivec2> indicesRandom(int32_t x, int32_t y, |
| 25 | + const glm::ivec2& windowResolution, const Features& features) { |
| 26 | + // Define the range of possible values based on window dimensions and resample radius |
| 27 | + std::random_device rd; |
| 28 | + std::mt19937 gen(rd()); |
| 29 | + int32_t resampleRadiusCast = static_cast<int32_t>(features.spatialResampleRadius); |
| 30 | + std::uniform_int_distribution<> distrX(std::max(0, x - resampleRadiusCast), |
| 31 | + std::min(windowResolution.x - 1, x + resampleRadiusCast)); |
| 32 | + std::uniform_int_distribution<> distrY(std::max(0, y - resampleRadiusCast), |
| 33 | + std::min(windowResolution.y - 1, y + resampleRadiusCast)); |
| 34 | + |
| 35 | + // Create indices |
| 36 | + std::vector<glm::ivec2> indices; |
| 37 | + indices.reserve(features.numNeighboursToSample + 1); // Assign enough space for current pixel AND neighbours |
| 38 | + indices.push_back(glm::ivec2(x, y)); // Always include the pixel itself |
| 39 | + for (uint32_t candidateIdx = 0U; candidateIdx < features.numNeighboursToSample; candidateIdx++) { |
| 40 | + indices.push_back(glm::ivec2(distrX(gen), distrY(gen))); |
| 41 | + } |
| 42 | + return indices; |
| 43 | +} |
| 44 | + |
| 45 | +std::vector<glm::ivec2> indicesSimilarity(int32_t x, int32_t y, |
| 46 | + const PrimaryHitGrid& primaryHits, const glm::ivec2& windowResolution, const Features& features) { |
| 47 | + // In extreme cases, all neighbours are similar or dissimilar. We reserve enough memory for either |
| 48 | + std::vector<glm::ivec2> similarIndices, dissimilarIndices; |
| 49 | + similarIndices.reserve(features.spatialResampleRadius * features.spatialResampleRadius * 4U); |
| 50 | + dissimilarIndices.reserve(features.spatialResampleRadius * features.spatialResampleRadius * 4U); |
| 51 | + |
| 52 | + // Ensure that our traversal does not exceed screen bounds |
| 53 | + int32_t resampleRadiusCast = static_cast<int32_t>(features.spatialResampleRadius); |
| 54 | + glm::ivec2 minExtents(std::max(0, x - resampleRadiusCast), std::max(0, y - resampleRadiusCast)); |
| 55 | + glm::ivec2 maxExtents(std::min(windowResolution.x - 1, x + resampleRadiusCast), std::min(windowResolution.y - 1, y + resampleRadiusCast)); |
| 56 | + |
| 57 | + // Categorise all pixels in neighbourhood |
| 58 | + const RayHit& canonical = primaryHits[y][x]; |
| 59 | + for (int32_t neighbourY = minExtents.y; neighbourY <= maxExtents.y; neighbourY++) { |
| 60 | + for (int32_t neighbourX = minExtents.x; neighbourX <= maxExtents.x; neighbourX++) { |
| 61 | + // Skip canonical pixel |
| 62 | + if (neighbourY == y && neighbourX == x) { continue; } |
| 63 | + |
| 64 | + // Categorise neighbour |
| 65 | + glm::ivec2 neighbourCoords(neighbourX, neighbourY); |
| 66 | + const RayHit& neighbour = primaryHits[neighbourY][neighbourX]; |
| 67 | + if (areSimilar(canonical, neighbour, features)) { similarIndices.push_back(neighbourCoords); } |
| 68 | + else { dissimilarIndices.push_back(neighbourCoords); } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Create indices |
| 73 | + std::vector<glm::ivec2> indices; |
| 74 | + indices.reserve(features.numNeighboursToSample + 1); // Assign enough space for current pixel AND neighbours |
| 75 | + indices.push_back(glm::ivec2(x, y)); // Always include the pixel itself |
| 76 | + std::random_device rd; |
| 77 | + std::mt19937 rng(rd()); |
| 78 | + switch (features.neighbourSelectionStrategy) { |
| 79 | + case NeighbourSelectionStrategy::Similar: { |
| 80 | + if (similarIndices.size() < features.numNeighboursToSample) { // Not enough similar neighbours |
| 81 | + indices.insert(indices.end(), similarIndices.begin(), similarIndices.end()); // Place however many we can |
| 82 | + std::sample(dissimilarIndices.begin(), dissimilarIndices.end(), std::back_inserter(indices), // Make up for deficit from dissimilar neighbours |
| 83 | + features.numNeighboursToSample - similarIndices.size(), rng); |
| 84 | + } else { std::sample(similarIndices.begin(), similarIndices.end(), std::back_inserter(indices), features.numNeighboursToSample, rng); } |
| 85 | + } break; |
| 86 | + case NeighbourSelectionStrategy::Dissimilar: { |
| 87 | + if (dissimilarIndices.size() < features.numNeighboursToSample) { // Not enough dissimilar neighbours |
| 88 | + indices.insert(indices.end(), dissimilarIndices.begin(), dissimilarIndices.end()); // Place however many we can |
| 89 | + std::sample(similarIndices.begin(), similarIndices.end(), std::back_inserter(indices), // Make up for deficit from similar neighbours |
| 90 | + features.numNeighboursToSample - similarIndices.size(), rng); |
| 91 | + } else { std::sample(dissimilarIndices.begin(), dissimilarIndices.end(), std::back_inserter(indices), features.numNeighboursToSample, rng); } |
| 92 | + } break; |
| 93 | + case NeighbourSelectionStrategy::EqualSimilarDissimilar: { |
| 94 | + // Ensure there are sufficient quantities of similars and dissimilars to satisfy halfway split and make up for difference if that is not possible |
| 95 | + uint32_t similarsSampled = std::min((features.numNeighboursToSample / 2U) + 1U, static_cast<uint32_t>(similarIndices.size())); |
| 96 | + uint32_t desiredDissimilars = features.numNeighboursToSample - similarsSampled; |
| 97 | + if (desiredDissimilars > dissimilarIndices.size()) { similarsSampled += features.numNeighboursToSample - dissimilarIndices.size() - similarsSampled; } |
| 98 | + |
| 99 | + std::sample(similarIndices.begin(), similarIndices.end(), std::back_inserter(indices), similarsSampled, rng); |
| 100 | + std::sample(dissimilarIndices.begin(), dissimilarIndices.end(), std::back_inserter(indices), features.numNeighboursToSample - similarsSampled, rng); |
| 101 | + } break; |
| 102 | + default: { throw std::runtime_error("indicesSimilarity called with unsupported neighbour selection strategy"); } |
| 103 | + } |
| 104 | + return indices; |
| 105 | +} |
| 106 | + |
| 107 | +ResampleIndicesGrid generateResampleIndicesGrid(const PrimaryHitGrid& primaryHits, |
| 108 | + const glm::ivec2& windowResolution, const Features& features) { |
| 109 | + ResampleIndicesGrid resampleIndices(windowResolution.y, std::vector<std::vector<glm::ivec2>>(windowResolution.x)); |
| 110 | + bool useRandom = features.neighbourSelectionStrategy == NeighbourSelectionStrategy::Random; |
| 111 | + #ifdef NDEBUG |
| 112 | + #pragma omp parallel for schedule(guided) |
| 113 | + #endif |
| 114 | + for (int y = 0; y < windowResolution.y; y++) { |
| 115 | + for (int x = 0; x != windowResolution.x; x++) { |
| 116 | + resampleIndices[y][x] = useRandom ? |
| 117 | + indicesRandom(x, y, windowResolution, features) : |
| 118 | + indicesSimilarity(x, y, primaryHits, windowResolution, features); |
| 119 | + } |
| 120 | + } |
| 121 | + return resampleIndices; |
| 122 | +} |
0 commit comments