From b82c56e61ce65c643e13015137361839e42cedb4 Mon Sep 17 00:00:00 2001 From: yggdrasil75 Date: Tue, 11 Nov 2025 19:52:31 -0500 Subject: [PATCH] stupid vsc --- tests/g2chromatic.cpp | 32 ++++++++--------- util/grid/grid2.hpp | 80 ++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 58 deletions(-) diff --git a/tests/g2chromatic.cpp b/tests/g2chromatic.cpp index 08fdef4..195313a 100644 --- a/tests/g2chromatic.cpp +++ b/tests/g2chromatic.cpp @@ -15,6 +15,9 @@ struct AnimationConfig { int numSeeds = 1; }; +const float PI4 = M_PI / 4.0f; +const float PI43 = PI4 * 3.0f; + bool initializeGrid(Grid2& grid, const AnimationConfig& config) { TIME_FUNCTION; std::cout << "Initializing grayscale grid..." << std::endl; @@ -52,20 +55,6 @@ std::pair, std::vector> generateSeedPoints(const Animati return {seedPoints, seedColors}; } -void applyDirectionalColorInfluence(Vec4& color, const Vec4& seedColor, float influence, - float progress, float angle) { - //TIME_FUNCTION; - if (std::abs(angle) < M_PI / 4.0f) { // Right - affect alpha - color.a = std::fmod(color.a + seedColor.a * influence * progress, 1.0f); - } else if (std::abs(angle) > 3.0f * M_PI / 4.0f) { // Left - affect blue - color.b = std::fmod(color.b + seedColor.b * influence * progress, 1.0f); - } else if (angle > 0) { // Below - affect green - color.g = std::fmod(color.g + seedColor.g * influence * progress, 1.0f); - } else { // Above - affect red - color.r = std::fmod(color.r + seedColor.r * influence * progress, 1.0f); - } -} - Vec4 calculateInfluencedColor(const Vec2& position, const Vec4& originalColor, float progress, const std::vector& seedPoints, const std::vector& seedColors, const AnimationConfig& config) { @@ -73,14 +62,25 @@ Vec4 calculateInfluencedColor(const Vec2& position, const Vec4& originalColor, f Vec4 newColor = originalColor; float maxDistance = std::max(config.width, config.height) * 0.6f; + for (int s = 0; s < config.numSeeds; ++s) { float distance = position.distance(seedPoints[s]); float influence = std::max(0.0f, 1.0f - (distance / maxDistance)); Vec2 direction = position - seedPoints[s]; float angle = std::atan2(direction.y, direction.x); - - applyDirectionalColorInfluence(newColor, seedColors[s], influence, progress, angle); + auto SC = seedColors[s]; + // applyDirectionalColorInfluence(newColor, seedColors[s], influence, progress, angle); + float absAngle = std::abs(angle); + if (absAngle < PI4) { // Right - affect alpha + newColor.a = std::fmod(newColor.a + SC.a * influence * progress, 1.0f); + } else if (absAngle > PI43) { // Left - affect blue + newColor.b = std::fmod(newColor.b + SC.b * influence * progress, 1.0f); + } else if (angle > 0) { // Below - affect green + newColor.g = std::fmod(newColor.g + SC.g * influence * progress, 1.0f); + } else { // Above - affect red + newColor.r = std::fmod(newColor.r + SC.r * influence * progress, 1.0f); + } } return newColor.clampColor(); diff --git a/util/grid/grid2.hpp b/util/grid/grid2.hpp index d449705..52a7d78 100644 --- a/util/grid/grid2.hpp +++ b/util/grid/grid2.hpp @@ -23,18 +23,15 @@ struct PairHash { class Grid2 { private: - //size_t is index. - //vec2 is x,y position of the sparse value - std::multimap positions; - //vec4 is rgba color at the position - std::multimap colors; - //size is a floating size to assign to a "pixel" (or voxel for grid3) to allow larger or smaller assignments in this map - std::multimap sizes; - //others will be added later + // Changed from multimap to unordered_map - each ID can only have one position/color/size + std::unordered_map positions; + std::unordered_map colors; + std::unordered_map sizes; + size_t next_id; - std::unordered_map> cellIndices; // object ID -> grid cell - std::unordered_map, std::unordered_set, PairHash> spatialGrid; // cell -> object IDs + std::unordered_map> cellIndices; + std::unordered_map, std::unordered_set, PairHash> spatialGrid; float cellSize; public: @@ -43,57 +40,54 @@ public: size_t addObject(const Vec2& position, const Vec4& color, float size = 1.0f) { size_t id = next_id++; - positions.insert({id, position}); - colors.insert({id, color}); - sizes.insert({id, size}); + positions[id] = position; // Direct assignment instead of insert + colors[id] = color; + sizes[id] = size; std::pair cell = worldToGrid(position); spatialGrid[cell].insert(id); cellIndices[id] = cell; return id; } - //gets + //gets - much simpler now Vec2 getPosition(size_t id) const { - std::multimap::const_iterator it = positions.find(id); + auto it = positions.find(id); if (it != positions.end()) return it->second; return Vec2(); } Vec4 getColor(size_t id) const { - std::multimap::const_iterator it = colors.find(id); + auto it = colors.find(id); if (it != colors.end()) return it->second; return Vec4(); } float getSize(size_t id) const { - std::multimap::const_iterator it = sizes.find(id); + auto it = sizes.find(id); if (it != sizes.end()) return it->second; return 1.0f; } - //sets + //sets - simpler too void setPosition(size_t id, const Vec2& position) { if (!hasObject(id)) return; - Vec2 oldPos = getPosition(id); - positions.erase(id); - positions.insert({id, position}); + Vec2 oldPos = positions[id]; // Direct access + positions[id] = position; // Direct assignment updateSpatialIndex(id, oldPos, position); } void setColor(size_t id, const Vec4& color) { - colors.erase(id); - colors.insert({id, color}); + colors[id] = color; // Direct assignment } void setSize(size_t id, float size) { - sizes.erase(id); - sizes.insert({id, size}); + sizes[id] = size; // Direct assignment } // Batch add/remove operations void addObjects(const std::vector>& objects) { - for (const std::tuple& obj : objects) { + for (const auto& obj : objects) { addObject(std::get<0>(obj), std::get<1>(obj), std::get<2>(obj)); } } @@ -106,30 +100,27 @@ public: // Batch position updates void updatePositions(const std::unordered_map& newPositions) { - // Bulk update spatial grid - collect all changes first std::vector> spatialUpdates; - for (const std::pair& pair : newPositions) { + for (const auto& pair : newPositions) { if (hasObject(pair.first)) { - Vec2 oldPos = getPosition(pair.first); - positions.erase(pair.first); - positions.insert({pair.first, pair.second}); + Vec2 oldPos = positions[pair.first]; + positions[pair.first] = pair.second; spatialUpdates.emplace_back(pair.first, oldPos, pair.second); } } - // Apply all spatial updates at once - for (const std::tuple& update : spatialUpdates) { + for (const auto& update : spatialUpdates) { updateSpatialIndex(std::get<0>(update), std::get<1>(update), std::get<2>(update)); } } std::vector getAllObjectIds() const { std::vector ids; + ids.reserve(positions.size()); for (const auto& pair : positions) { ids.push_back(pair.first); } - // Sort by ID to ensure consistent order std::sort(ids.begin(), ids.end()); return ids; } @@ -161,15 +152,9 @@ public: updates[i] = {id, newColor}; } - // Batch update colors - much more efficient + // Batch update colors - much more efficient with unordered_map for (const auto& update : updates) { - // Directly update existing entry instead of erase/insert - auto it = colors.find(update.first); - if (it != colors.end()) { - // If multimap doesn't support direct modification, we need to replace - // For better performance, consider changing data structure - const_cast(it->second) = update.second; - } + colors[update.first] = update.second; // Direct assignment } } @@ -500,6 +485,17 @@ public: size_t getSpatialGridCellCount() const { return spatialGrid.size(); } size_t getSpatialGridObjectCount() const { return cellIndices.size(); } float getCellSize() const { return cellSize; } + + // Additional utility methods + size_t getObjectCount() const { return positions.size(); } + void clear() { + positions.clear(); + colors.clear(); + sizes.clear(); + spatialGrid.clear(); + cellIndices.clear(); + next_id = 0; + } }; #endif \ No newline at end of file