From 872cf3db1aef8e94ebc2846ed6b9b88c4a70a9e4 Mon Sep 17 00:00:00 2001 From: yggdrasil75 Date: Tue, 25 Nov 2025 05:36:01 -0500 Subject: [PATCH] pushing to work. --- tests/g2temp.cpp | 74 ++++++------ util/grid/grid2.hpp | 261 ++++++++++++++++++++++++++-------------- util/simblocks/temp.hpp | 20 +++ 3 files changed, 226 insertions(+), 129 deletions(-) diff --git a/tests/g2temp.cpp b/tests/g2temp.cpp index c52c17d..9e4a74d 100644 --- a/tests/g2temp.cpp +++ b/tests/g2temp.cpp @@ -85,25 +85,25 @@ void Preview(Grid2& grid) { } void livePreview(Grid2& grid) { - // std::lock_guard lock(previewMutex); + std::lock_guard lock(previewMutex); - // //currentPreviewFrame = grid.getGridAsFrame(frame::colormap::RGBA); + currentPreviewFrame = grid.getGridAsFrame(frame::colormap::RGBA); // Vec2 min; // Vec2 max; // grid.getBoundingBox(min, max); - // currentPreviewFrame = grid.getTempAsFrame(min,max, Vec2(1024,1024)); + //currentPreviewFrame = grid.getTempAsFrame(min,max, Vec2(1024,1024)); - // glGenTextures(1, &textu); - // glBindTexture(GL_TEXTURE_2D, textu); - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - // glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glGenTextures(1, &textu); + glBindTexture(GL_TEXTURE_2D, textu); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); - // glBindTexture(GL_TEXTURE_2D, textu); - // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, currentPreviewFrame.getWidth(), currentPreviewFrame.getHeight(), - // 0, GL_RGBA, GL_UNSIGNED_BYTE, currentPreviewFrame.getData().data()); + glBindTexture(GL_TEXTURE_2D, textu); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, currentPreviewFrame.getWidth(), currentPreviewFrame.getHeight(), + 0, GL_RGBA, GL_UNSIGNED_BYTE, currentPreviewFrame.getData().data()); - // updatePreview = true; + updatePreview = true; } std::vector> pickSeeds(Grid2 grid, AnimationConfig config) { @@ -134,7 +134,7 @@ void pickTempSeeds(Grid2& grid, AnimationConfig config) { std::uniform_int_distribution<> yDist(0, config.height - 1); std::uniform_real_distribution<> temp(0.0f, 100.0f); - for (int i = 0; i < config.numSeeds; ++i){ + for (int i = 0; i < config.numSeeds * 100; ++i){ grid.setTemp(Vec2(xDist(gen),yDist(gen)).floor(), temp(gen)); } } @@ -276,33 +276,33 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { std::vector frames; - // for (int i = 0; i < config.totalFrames; ++i){ - // // Check if we should stop the generation - // if (!isGenerating) { - // std::cout << "Generation cancelled at frame " << i << std::endl; - // return; - // } + for (int i = 0; i < config.totalFrames; ++i){ + // Check if we should stop the generation + if (!isGenerating) { + std::cout << "Generation cancelled at frame " << i << std::endl; + return; + } - // //expandPixel(grid,config,seeds); + //expandPixel(grid,config,seeds); - // std::lock_guard lock(state.mutex); - // state.grid = grid; - // state.hasNewFrame = true; - // state.currentFrame = i; + std::lock_guard lock(state.mutex); + state.grid = grid; + state.hasNewFrame = true; + state.currentFrame = i; - // // Print compression info for this frame - // if (i % 10 == 0 ) { - // frame bgrframe; - // std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; - // bgrframe = grid.getGridAsFrame(frame::colormap::BGR); - // frames.push_back(bgrframe); - // //bgrframe.decompress(); - // //BMPWriter::saveBMP(std::format("output/grayscalesource.{}.bmp", i), bgrframe); - // bgrframe.compressFrameLZ78(); - // //bgrframe.printCompressionStats(); - // } - // } - // exportavi(frames,config); + // Print compression info for this frame + if (i % 10 == 0 ) { + frame bgrframe; + std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; + bgrframe = grid.getGridAsFrame(frame::colormap::BGR); + frames.push_back(bgrframe); + //bgrframe.decompress(); + //BMPWriter::saveBMP(std::format("output/grayscalesource.{}.bmp", i), bgrframe); + bgrframe.compressFrameLZ78(); + //bgrframe.printCompressionStats(); + } + } + exportavi(frames,config); } catch (const std::exception& e) { std::cerr << "errored at: " << e.what() << std::endl; diff --git a/util/grid/grid2.hpp b/util/grid/grid2.hpp index 9d9b8d2..7799bb6 100644 --- a/util/grid/grid2.hpp +++ b/util/grid/grid2.hpp @@ -202,7 +202,7 @@ protected: Vec2 gridMax; //neighbor map - std::unordered_map> neighborMap; + //std::unordered_map> neighborMap; float neighborRadius = 1.0f; //TODO: spatial map @@ -416,7 +416,7 @@ public: // Add to spatial grid spatialGrid.insert(id, pos); - updateNeighborForID(id); + // updateNeighborForID(id); return id; } @@ -425,7 +425,7 @@ public: Vec2 oldPosition = Positions.at(id); spatialGrid.update(id, oldPosition, newPosition); Positions.at(id).move(newPosition); - updateNeighborForID(id); + // updateNeighborForID(id); } void setPosition(size_t id, float x, float y) { @@ -434,7 +434,7 @@ public: spatialGrid.update(id, oldPos, newPos); Positions.at(id).move(newPos); - updateNeighborForID(id); + // updateNeighborForID(id); } //set color by id (by pos same as get color) @@ -489,7 +489,7 @@ public: Sizes.erase(id); unassignedIDs.push_back(id); spatialGrid.remove(id, oldPosition); - updateNeighborForID(id); + // updateNeighborForID(id); return id; } @@ -500,7 +500,7 @@ public: Sizes.erase(id); unassignedIDs.push_back(id); spatialGrid.remove(id, pos); - updateNeighborForID(id); + // updateNeighborForID(id); return id; } @@ -513,7 +513,7 @@ public: Positions.at(id).move(newPos); spatialGrid.update(id, oldPosition, newPos); } - updateNeighborMap(); + // updateNeighborMap(); } // Bulk update colors @@ -567,7 +567,7 @@ public: } shrinkIfNeeded(); - updateNeighborMap(); + // updateNeighborMap(); return getAllIDs(); // Or generate ID range } @@ -594,7 +594,38 @@ public: } shrinkIfNeeded(); - updateNeighborMap(); + // updateNeighborMap(); + + usable = true; + return newids; + } + + std::vector bulkAddObjects(const std::vector poses, std::vector colors, std::vector& sizes, std::vector& temps) { + TIME_FUNCTION; + std::vector ids; + ids.reserve(poses.size()); + + // Reserve space in maps to avoid rehashing + if (Positions.bucket_count() < Positions.size() + poses.size()) { + Positions.reserve(Positions.size() + poses.size()); + Colors.reserve(Colors.size() + colors.size()); + Sizes.reserve(Sizes.size() + sizes.size()); + tempMap.reserve(tempMap.size() + temps.size()); + } + + // Batch insertion + std::vector newids; + for (size_t i = 0; i < poses.size(); ++i) { + size_t id = Positions.set(poses[i]); + Colors[id] = colors[i]; + Sizes[id] = sizes[i]; + tempMap.emplace(id, Temp(temps[i])); + spatialGrid.insert(id,poses[i]); + newids.push_back(id); + } + + shrinkIfNeeded(); + // updateNeighborMap(); usable = true; return newids; @@ -900,36 +931,9 @@ public: Frame = std::move(getGridRegionAsFrameRGB(minCorner, maxCorner)); break; } - //Frame.compressFrameDiff(); - //Frame.compressFrameRLE(); - //Frame.compressFrameLZ78(); return Frame; } - // Get compressed frame with specified compression - frame getGridAsCompressedFrame(frame::colormap format = frame::colormap::RGB, - frame::compresstype compression = frame::compresstype::RLE) { - TIME_FUNCTION; - frame gridFrame = getGridAsFrame(format); - - if (gridFrame.getData().empty()) { - return gridFrame; - } - - switch (compression) { - case frame::compresstype::RLE: - return gridFrame.compressFrameRLE(); - case frame::compresstype::DIFF: - return gridFrame.compressFrameDiff(); - case frame::compresstype::DIFFRLE: - return gridFrame.compressFrameDiffRLE(); - case frame::compresstype::HUFFMAN: - return gridFrame.compressFrameHuffman(); - case frame::compresstype::RAW: - default: - return gridFrame; - } - } //get bounding box void getBoundingBox(Vec2& minCorner, Vec2& maxCorner) const { @@ -968,10 +972,10 @@ public: Colors.clear(); Sizes.clear(); spatialGrid.clear(); - neighborMap.clear(); + //neighborMap.clear(); Colors.rehash(0); Sizes.rehash(0); - neighborMap.rehash(0); + // neighborMap.rehash(0); // Reset to default background color defaultBackgroundColor = Vec4(0.0f, 0.0f, 0.0f, 0.0f); } @@ -989,63 +993,82 @@ public: } } - void updateNeighborMap() { - //std::cout << "updateNeighborMap()" << std::endl; - TIME_FUNCTION; - neighborMap.clear(); - optimizeSpatialGrid(); + // void updateNeighborMap() { + // //std::cout << "updateNeighborMap()" << std::endl; + // TIME_FUNCTION; + // neighborMap.clear(); + // optimizeSpatialGrid(); - // For each object, find nearby neighbors - float radiusSq = neighborRadius * neighborRadius; - #pragma omp parallel for - for (const auto& [id1, pos1] : Positions) { - std::vector neighbors; - //std::vector candidate_ids = spatialGrid.queryRange(pos1, neighborRadius); - std::unordered_set candidate_idset = spatialGrid.find(pos1); - std::vector candidate_ids; - candidate_ids.reserve(candidate_idset.size()); - for (auto it = candidate_idset.begin(); it != candidate_idset.end();) { - candidate_ids.push_back(std::move(candidate_idset.extract(it++).value())); - } + // // For each object, find nearby neighbors + // float radiusSq = neighborRadius * neighborRadius; + // #pragma omp parallel for + // for (const auto& [id1, pos1] : Positions) { + // std::vector neighbors; + // //std::vector candidate_ids = spatialGrid.queryRange(pos1, neighborRadius); + // std::unordered_set candidate_idset = spatialGrid.find(pos1); + // std::vector candidate_ids; + // candidate_ids.reserve(candidate_idset.size()); + // for (auto it = candidate_idset.begin(); it != candidate_idset.end();) { + // candidate_ids.push_back(std::move(candidate_idset.extract(it++).value())); + // } - for (size_t id2 : candidate_ids) { - if (id1 != id2) { // && Positions.at(id1).distanceSquared(Positions.at(id2)) <= radiusSq) { - neighbors.push_back(id2); - } - } - #pragma omp critical - neighborMap[id1] = std::move(neighbors); - } - } + // for (size_t id2 : candidate_ids) { + // if (id1 != id2) { // && Positions.at(id1).distanceSquared(Positions.at(id2)) <= radiusSq) { + // neighbors.push_back(id2); + // } + // } + // #pragma omp critical + // neighborMap[id1] = std::move(neighbors); + // } + // } // Update neighbor map for a single object - void updateNeighborForID(size_t id) { - TIME_FUNCTION; - Vec2 pos_it = Positions.at(id); + // void updateNeighborForID(size_t id) { + // TIME_FUNCTION; + // Vec2 pos_it = Positions.at(id); + // std::vector neighbors; + // float radiusSq = neighborRadius * neighborRadius; + + // for (const auto& [id2, pos2] : Positions) { + // if (id != id2 && pos_it.distanceSquared(pos2) <= radiusSq) { + // neighbors.push_back(id2); + // } + // } + // neighborMap[id] = std::move(neighbors); + // } + + // // Get neighbors for an ID + // const std::vector& getNeighbors(size_t id) const { + // static const std::vector empty; + // auto it = neighborMap.find(id); + // return it != neighborMap.end() ? it->second : empty; + // } + + std::vector getNeighbors(size_t id) const { + Vec2 pos = Positions.at(id); + std::vector candidates = spatialGrid.queryRange(pos, neighborRadius); + + // Filter out self and apply exact distance check std::vector neighbors; float radiusSq = neighborRadius * neighborRadius; - for (const auto& [id2, pos2] : Positions) { - if (id != id2 && pos_it.distanceSquared(pos2) <= radiusSq) { - neighbors.push_back(id2); + for (size_t candidateId : candidates) { + if (candidateId != id && + pos.distanceSquared(Positions.at(candidateId)) <= radiusSq) { + neighbors.push_back(candidateId); } } - neighborMap[id] = std::move(neighbors); - } - - // Get neighbors for an ID - const std::vector& getNeighbors(size_t id) const { - static const std::vector empty; - auto it = neighborMap.find(id); - return it != neighborMap.end() ? it->second : empty; + + return neighbors; } // Set neighbor search radius void setNeighborRadius(float radius) { neighborRadius = radius; - updateNeighborMap(); // Recompute all neighbors + // updateNeighborMap(); // Recompute all neighbors + optimizeSpatialGrid(); } @@ -1057,14 +1080,56 @@ public: void setTemp(size_t id, double temp) { Temp tval = Temp(temp); - tempMap[id] = tval; + tempMap.emplace(id, tval); } + Grid2 noiseGenGridTemps(size_t minx,size_t miny, size_t maxx, size_t maxy, float minChance = 0.1f + , float maxChance = 1.0f, bool color = true, int noisemod = 42) { + TIME_FUNCTION; + std::cout << "generating a noise grid with the following: (" << minx << ", " << miny + << ") by (" << maxx << ", " << maxy << ") " << "chance: " << minChance + << " max: " << maxChance << " gen colors: " << color << std::endl; + std::vector poses; + std::vector colors; + std::vector sizes; + for (int x = minx; x < maxx; x++) { + for (int y = miny; y < maxy; y++) { + float nx = (x+noisemod)/(maxx+EPSILON)/0.1; + float ny = (y+noisemod)/(maxy+EPSILON)/0.1; + Vec2 pos = Vec2(nx,ny); + float alpha = noisegen.permute(pos); + if (alpha > minChance && alpha < maxChance) { + if (color) { + float red = noisegen.permute(Vec2(nx*0.3,ny*0.3)); + float green = noisegen.permute(Vec2(nx*0.6,ny*.06)); + float blue = noisegen.permute(Vec2(nx*0.9,ny*0.9)); + std::uniform_real_distribution<> temp(0.0f, 100.0f); + Vec4 newc = Vec4(red,green,blue,1.0); + colors.push_back(newc); + poses.push_back(Vec2(x,y)); + sizes.push_back(1.0f); + } else { + Vec4 newc = Vec4(alpha,alpha,alpha,1.0); + colors.push_back(newc); + poses.push_back(pos); + sizes.push_back(1.0f); + } + } + } + } + std::cout << "noise generated" << std::endl; + bulkAddObjects(poses,colors,sizes); + return *this; + } + double getTemp(size_t id) { if (tempMap.find(id) != tempMap.end()) { - Temp temp; - double dtemp = temp.calTempIDW(getPositionID(id), getTemps()); - setTemp(id, dtemp); + Temp temp = Temp(getPositionID(id), getTemps()); + //double dtemp = Temp::calTempIDW(getPositionID(id), getTemps()); + tempMap.emplace(id, temp); + } + else { + std::cout << "found a temp: " << tempMap.at(id).temp << std::endl; } return tempMap.at(id).temp; } @@ -1072,7 +1137,18 @@ public: std::unordered_map getTemps() { std::unordered_map out; for (const auto& [id, temp] : tempMap) { - out[getPositionID(id)] = temp; + out.emplace(getPositionID(id), temp); + } + return out; + } + + std::unordered_map getTemps(size_t id) { + std::unordered_map out; + std::vector tval = spatialGrid.queryRange(Positions.at(id), 10); + for (size_t tempid : tval) { + Vec2 pos = Positions.at(tempid); + Temp temp = tempMap.at(tempid); + out.insert({pos, temp}); } return out; } @@ -1080,8 +1156,7 @@ public: double getTemp(const Vec2 pos) { size_t id = getOrCreatePositionVec(pos, 0.0f, true); if (tempMap.find(id) == tempMap.end()) { - Temp temp; - double dtemp = temp.calTempIDW(pos, getTemps()); + double dtemp = Temp::calTempIDW(pos, getTemps(id)); setTemp(id, dtemp); return dtemp; } @@ -1106,11 +1181,13 @@ public: // std::cout << "getTempAsFrame() started" << pcount++ << std::endl; double maxTemp = 0.0; double minTemp = 0.0; + float xdiff = (maxCorner.x - minCorner.x); + float ydiff = (maxCorner.y - minCorner.y); // std::cout << "getTempAsFrame() started" << pcount++ << std::endl; for (int x = 0; x < res.x; x++) { for (int y = 0; y < res.y; y++) { Vec2 cposout = Vec2(x,y); - Vec2 cposin = Vec2(x/sheight, y/swidth); + Vec2 cposin = Vec2(minCorner.x + (x * xdiff / res.x),minCorner.y + (y * ydiff / res.y)); // std::cout << "getTempAsFrame() started" << pcount++ << std::endl; double ctemp = getTemp(cposin); @@ -1123,17 +1200,17 @@ public: std::cout << "max temp: " << maxTemp << " min temp: " << minTemp << std::endl; // std::cout << "getTempAsFrame() middle" << std::endl; - std::vector rgbaBuffer(width*height*4, 0); + std::vector rgbaBuffer(width*height*3, 0); for (const auto& [v2, temp] : tempBuffer) { - size_t index = (v2.y * width + v2.y) * 4; - uint8_t atemp = static_cast((((temp-minTemp) * 100) / (maxTemp-minTemp)) * 255); + size_t index = (v2.y * width + v2.x) * 3; + uint8_t atemp = static_cast((((temp-minTemp)) / (maxTemp-minTemp)) * 255); rgbaBuffer[index] = atemp; rgbaBuffer[index+1] = atemp; rgbaBuffer[index+2] = atemp; - rgbaBuffer[index+3] = 255; + //rgbaBuffer[index+3] = 255; } std::cout << "rgba buffer is " << rgbaBuffer.size() << std::endl; - frame result = frame(res.x,res.y, frame::colormap::RGBA); + frame result = frame(res.x,res.y, frame::colormap::RGB); result.setData(rgbaBuffer); updatingView = false; return result; diff --git a/util/simblocks/temp.hpp b/util/simblocks/temp.hpp index 0505734..eadc68d 100644 --- a/util/simblocks/temp.hpp +++ b/util/simblocks/temp.hpp @@ -13,6 +13,26 @@ protected: public: double temp; + Temp(float temp) : temp(temp) {}; + + Temp(const Vec2& testPos, const std::unordered_map& others) { + TIME_FUNCTION; + double power = 2.0; + double num = 0.0; + double den = 0.0; + + for (const auto& [point, tempObj] : others) { + double dist = testPos.distance(point); + double weight = 1.0 / std::pow(dist, power); + num += weight * tempObj.temp; + den += weight; + } + + if (den < 1e-10 && den > -1e-10) { + den = 1e-10; + } + this->temp = num / den; + } static double calTempIDW(const Vec2& testPos, std::unordered_map others) { TIME_FUNCTION;