stupid vsc

This commit is contained in:
yggdrasil75
2025-11-11 19:52:31 -05:00
parent 4107d750b5
commit b82c56e61c
2 changed files with 54 additions and 58 deletions

View File

@@ -15,6 +15,9 @@ struct AnimationConfig {
int numSeeds = 1; int numSeeds = 1;
}; };
const float PI4 = M_PI / 4.0f;
const float PI43 = PI4 * 3.0f;
bool initializeGrid(Grid2& grid, const AnimationConfig& config) { bool initializeGrid(Grid2& grid, const AnimationConfig& config) {
TIME_FUNCTION; TIME_FUNCTION;
std::cout << "Initializing grayscale grid..." << std::endl; std::cout << "Initializing grayscale grid..." << std::endl;
@@ -52,20 +55,6 @@ std::pair<std::vector<Vec2>, std::vector<Vec4>> generateSeedPoints(const Animati
return {seedPoints, seedColors}; 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, Vec4 calculateInfluencedColor(const Vec2& position, const Vec4& originalColor, float progress,
const std::vector<Vec2>& seedPoints, const std::vector<Vec4>& seedColors, const std::vector<Vec2>& seedPoints, const std::vector<Vec4>& seedColors,
const AnimationConfig& config) { const AnimationConfig& config) {
@@ -73,14 +62,25 @@ Vec4 calculateInfluencedColor(const Vec2& position, const Vec4& originalColor, f
Vec4 newColor = originalColor; Vec4 newColor = originalColor;
float maxDistance = std::max(config.width, config.height) * 0.6f; float maxDistance = std::max(config.width, config.height) * 0.6f;
for (int s = 0; s < config.numSeeds; ++s) { for (int s = 0; s < config.numSeeds; ++s) {
float distance = position.distance(seedPoints[s]); float distance = position.distance(seedPoints[s]);
float influence = std::max(0.0f, 1.0f - (distance / maxDistance)); float influence = std::max(0.0f, 1.0f - (distance / maxDistance));
Vec2 direction = position - seedPoints[s]; Vec2 direction = position - seedPoints[s];
float angle = std::atan2(direction.y, direction.x); float angle = std::atan2(direction.y, direction.x);
auto SC = seedColors[s];
applyDirectionalColorInfluence(newColor, seedColors[s], influence, progress, angle); // 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(); return newColor.clampColor();

View File

@@ -23,18 +23,15 @@ struct PairHash {
class Grid2 { class Grid2 {
private: private:
//size_t is index. // Changed from multimap to unordered_map - each ID can only have one position/color/size
//vec2 is x,y position of the sparse value std::unordered_map<size_t, Vec2> positions;
std::multimap<size_t, Vec2> positions; std::unordered_map<size_t, Vec4> colors;
//vec4 is rgba color at the position std::unordered_map<size_t, float> sizes;
std::multimap<size_t, Vec4> 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<size_t, float> sizes;
//others will be added later
size_t next_id; size_t next_id;
std::unordered_map<size_t, std::pair<int, int>> cellIndices; // object ID -> grid cell std::unordered_map<size_t, std::pair<int, int>> cellIndices;
std::unordered_map<std::pair<int, int>, std::unordered_set<size_t>, PairHash> spatialGrid; // cell -> object IDs std::unordered_map<std::pair<int, int>, std::unordered_set<size_t>, PairHash> spatialGrid;
float cellSize; float cellSize;
public: public:
@@ -43,57 +40,54 @@ public:
size_t addObject(const Vec2& position, const Vec4& color, float size = 1.0f) { size_t addObject(const Vec2& position, const Vec4& color, float size = 1.0f) {
size_t id = next_id++; size_t id = next_id++;
positions.insert({id, position}); positions[id] = position; // Direct assignment instead of insert
colors.insert({id, color}); colors[id] = color;
sizes.insert({id, size}); sizes[id] = size;
std::pair<int,int> cell = worldToGrid(position); std::pair<int,int> cell = worldToGrid(position);
spatialGrid[cell].insert(id); spatialGrid[cell].insert(id);
cellIndices[id] = cell; cellIndices[id] = cell;
return id; return id;
} }
//gets //gets - much simpler now
Vec2 getPosition(size_t id) const { Vec2 getPosition(size_t id) const {
std::multimap<size_t, Vec2>::const_iterator it = positions.find(id); auto it = positions.find(id);
if (it != positions.end()) return it->second; if (it != positions.end()) return it->second;
return Vec2(); return Vec2();
} }
Vec4 getColor(size_t id) const { Vec4 getColor(size_t id) const {
std::multimap<size_t, Vec4>::const_iterator it = colors.find(id); auto it = colors.find(id);
if (it != colors.end()) return it->second; if (it != colors.end()) return it->second;
return Vec4(); return Vec4();
} }
float getSize(size_t id) const { float getSize(size_t id) const {
std::multimap<size_t, float>::const_iterator it = sizes.find(id); auto it = sizes.find(id);
if (it != sizes.end()) return it->second; if (it != sizes.end()) return it->second;
return 1.0f; return 1.0f;
} }
//sets //sets - simpler too
void setPosition(size_t id, const Vec2& position) { void setPosition(size_t id, const Vec2& position) {
if (!hasObject(id)) return; if (!hasObject(id)) return;
Vec2 oldPos = getPosition(id); Vec2 oldPos = positions[id]; // Direct access
positions.erase(id); positions[id] = position; // Direct assignment
positions.insert({id, position});
updateSpatialIndex(id, oldPos, position); updateSpatialIndex(id, oldPos, position);
} }
void setColor(size_t id, const Vec4& color) { void setColor(size_t id, const Vec4& color) {
colors.erase(id); colors[id] = color; // Direct assignment
colors.insert({id, color});
} }
void setSize(size_t id, float size) { void setSize(size_t id, float size) {
sizes.erase(id); sizes[id] = size; // Direct assignment
sizes.insert({id, size});
} }
// Batch add/remove operations // Batch add/remove operations
void addObjects(const std::vector<std::tuple<Vec2, Vec4, float>>& objects) { void addObjects(const std::vector<std::tuple<Vec2, Vec4, float>>& objects) {
for (const std::tuple<Vec2, Vec4, float>& obj : objects) { for (const auto& obj : objects) {
addObject(std::get<0>(obj), std::get<1>(obj), std::get<2>(obj)); addObject(std::get<0>(obj), std::get<1>(obj), std::get<2>(obj));
} }
} }
@@ -106,30 +100,27 @@ public:
// Batch position updates // Batch position updates
void updatePositions(const std::unordered_map<size_t, Vec2>& newPositions) { void updatePositions(const std::unordered_map<size_t, Vec2>& newPositions) {
// Bulk update spatial grid - collect all changes first
std::vector<std::tuple<size_t, Vec2, Vec2>> spatialUpdates; std::vector<std::tuple<size_t, Vec2, Vec2>> spatialUpdates;
for (const std::pair<const size_t, Vec2>& pair : newPositions) { for (const auto& pair : newPositions) {
if (hasObject(pair.first)) { if (hasObject(pair.first)) {
Vec2 oldPos = getPosition(pair.first); Vec2 oldPos = positions[pair.first];
positions.erase(pair.first); positions[pair.first] = pair.second;
positions.insert({pair.first, pair.second});
spatialUpdates.emplace_back(pair.first, oldPos, pair.second); spatialUpdates.emplace_back(pair.first, oldPos, pair.second);
} }
} }
// Apply all spatial updates at once for (const auto& update : spatialUpdates) {
for (const std::tuple<size_t, Vec2, Vec2>& update : spatialUpdates) {
updateSpatialIndex(std::get<0>(update), std::get<1>(update), std::get<2>(update)); updateSpatialIndex(std::get<0>(update), std::get<1>(update), std::get<2>(update));
} }
} }
std::vector<size_t> getAllObjectIds() const { std::vector<size_t> getAllObjectIds() const {
std::vector<size_t> ids; std::vector<size_t> ids;
ids.reserve(positions.size());
for (const auto& pair : positions) { for (const auto& pair : positions) {
ids.push_back(pair.first); ids.push_back(pair.first);
} }
// Sort by ID to ensure consistent order
std::sort(ids.begin(), ids.end()); std::sort(ids.begin(), ids.end());
return ids; return ids;
} }
@@ -161,15 +152,9 @@ public:
updates[i] = {id, newColor}; updates[i] = {id, newColor};
} }
// Batch update colors - much more efficient // Batch update colors - much more efficient with unordered_map
for (const auto& update : updates) { for (const auto& update : updates) {
// Directly update existing entry instead of erase/insert colors[update.first] = update.second; // Direct assignment
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<Vec4&>(it->second) = update.second;
}
} }
} }
@@ -500,6 +485,17 @@ public:
size_t getSpatialGridCellCount() const { return spatialGrid.size(); } size_t getSpatialGridCellCount() const { return spatialGrid.size(); }
size_t getSpatialGridObjectCount() const { return cellIndices.size(); } size_t getSpatialGridObjectCount() const { return cellIndices.size(); }
float getCellSize() const { return cellSize; } 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 #endif