From ea7328be64b7ec9374fb530005723d692aa0367c Mon Sep 17 00:00:00 2001 From: Yggdrasil75 Date: Tue, 25 Nov 2025 12:44:41 -0500 Subject: [PATCH] stuff working better now. still kinda meh, but its decent enough. --- makefile | 1 + tests/g2temp.cpp | 8 +- util/grid/grid2.hpp | 155 +++++++++++++++++--------------------- util/output/bmpwriter.hpp | 3 - util/output/frame.hpp | 1 - util/simblocks/temp.hpp | 65 +++++++++++++++- 6 files changed, 140 insertions(+), 93 deletions(-) diff --git a/makefile b/makefile index eb98198..3bbe7c7 100644 --- a/makefile +++ b/makefile @@ -18,6 +18,7 @@ CXXFLAGS += $(PKG_FLAGS) # Source files SRC := $(SRC_DIR)/g2temp.cpp +#SRC := $(SRC_DIR)/g2chromatic2.cpp SRC += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp SRC += $(IMGUI_DIR)/backends/imgui_impl_glfw.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp SRC += $(SRC_DIR)/stb_image.cpp diff --git a/tests/g2temp.cpp b/tests/g2temp.cpp index 9e4a74d..8854821 100644 --- a/tests/g2temp.cpp +++ b/tests/g2temp.cpp @@ -252,7 +252,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { if (gradnoise == 0) { grid = setup(config); } else if (gradnoise == 1) { - grid = grid.noiseGenGrid(0,0,config.height, config.width, 0.01, 1.0, false, config.noisemod); + grid = grid.noiseGenGridTemps(0,0,config.height, config.width, 0.01, 1.0, false, config.noisemod); } grid.setDefault(Vec4(0,0,0,0)); { @@ -261,7 +261,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { state.hasNewFrame = true; state.currentFrame = 0; } - pickTempSeeds(grid,config); + //pickTempSeeds(grid,config); //std::vector> seeds = pickSeeds(grid, config); std::cout << "generated grid" << std::endl; @@ -294,7 +294,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { if (i % 10 == 0 ) { frame bgrframe; std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; - bgrframe = grid.getGridAsFrame(frame::colormap::BGR); + bgrframe = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256), frame::colormap::BGR); frames.push_back(bgrframe); //bgrframe.decompress(); //BMPWriter::saveBMP(std::format("output/grayscalesource.{}.bmp", i), bgrframe); @@ -407,7 +407,7 @@ int main() { static int i4 = 8; static int noisemod = 42; static float fs = 1.0; - int gradnoise = 0; + int gradnoise = 1; std::future mainlogicthread; Shared state; diff --git a/util/grid/grid2.hpp b/util/grid/grid2.hpp index 7799bb6..5e5f798 100644 --- a/util/grid/grid2.hpp +++ b/util/grid/grid2.hpp @@ -274,7 +274,7 @@ public: } else { Vec4 newc = Vec4(alpha,alpha,alpha,1.0); colors.push_back(newc); - poses.push_back(pos); + poses.push_back(Vec2(x,y)); sizes.push_back(1.0f); } } @@ -619,13 +619,13 @@ public: size_t id = Positions.set(poses[i]); Colors[id] = colors[i]; Sizes[id] = sizes[i]; - tempMap.emplace(id, Temp(temps[i])); + Temp temptemp = Temp(temps[i]); + tempMap.insert({id, temptemp}); spatialGrid.insert(id,poses[i]); newids.push_back(id); } shrinkIfNeeded(); - // updateNeighborMap(); usable = true; return newids; @@ -663,13 +663,6 @@ public: // Initialize RGB data with default background color std::vector rgbaBuffer(width * height, Vec4(0,0,0,0)); - // for (int x = minCorner.x; x < maxCorner.x; x++) { - // for (int y = minCorner.y; x < maxCorner.y; y++){ - // Vec2 pos = Vec2(x,y); - // size_t posID = getPositionVec(pos, 1.0f, false); - - // } - // } // For each position in the grid, find the corresponding pixel for (const auto& [id, pos] : Positions) { size_t size = Sizes.at(id); @@ -993,59 +986,6 @@ public: } } - // 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 (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); - - // 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); @@ -1071,7 +1011,6 @@ public: optimizeSpatialGrid(); } - //temp stuff void setTemp(const Vec2 pos, double temp) { size_t id = getOrCreatePositionVec(pos, 0.0, true); @@ -1092,33 +1031,38 @@ public: std::vector poses; std::vector colors; std::vector sizes; + std::vector temps; + int callnumber = 0; 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 temp = noisegen.permute(Vec2(nx*0.2+1,ny*0.1+2)); 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); + temps.push_back(temp * 100); + //std::cout << "temp: " << temp << std::endl; } else { Vec4 newc = Vec4(alpha,alpha,alpha,1.0); colors.push_back(newc); - poses.push_back(pos); + poses.push_back(Vec2(x,y)); sizes.push_back(1.0f); + temps.push_back(temp * 100); } } } } std::cout << "noise generated" << std::endl; - bulkAddObjects(poses,colors,sizes); + bulkAddObjects(poses, colors, sizes, temps); return *this; } @@ -1147,8 +1091,10 @@ public: 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}); + if (tempMap.find(id) != tempMap.end()) { + Temp temp = tempMap.at(tempid); + out.insert({pos, temp}); + } } return out; } @@ -1163,12 +1109,12 @@ public: else return tempMap.at(id).temp; } - frame getTempAsFrame(Vec2 minCorner, Vec2 maxCorner, Vec2 res) { + frame getTempAsFrame(Vec2 minCorner, Vec2 maxCorner, Vec2 res, frame::colormap outcolor = frame::colormap::RGB) { TIME_FUNCTION; if (updatingView) return frame(); updatingView = true; int pcount = 0; - std::cout << "getTempAsFrame() started" << pcount++ << std::endl; + // std::cout << "getTempAsFrame() started" << pcount++ << std::endl; size_t sheight = maxCorner.x - minCorner.x; size_t swidth = maxCorner.y - minCorner.y; // std::cout << "getTempAsFrame() started" << pcount++ << std::endl; @@ -1200,22 +1146,63 @@ public: std::cout << "max temp: " << maxTemp << " min temp: " << minTemp << std::endl; // std::cout << "getTempAsFrame() middle" << std::endl; - std::vector rgbaBuffer(width*height*3, 0); - for (const auto& [v2, temp] : tempBuffer) { - 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; + switch (outcolor) { + case frame::colormap::RGBA: { + std::vector rgbaBuffer(width*height*4, 0); + for (const auto& [v2, temp] : tempBuffer) { + size_t index = (v2.y * width + v2.x) * 4; + uint8_t atemp = static_cast((((temp-minTemp)) / (maxTemp-minTemp)) * 255); + rgbaBuffer[index+0] = atemp; + rgbaBuffer[index+1] = atemp; + rgbaBuffer[index+2] = atemp; + rgbaBuffer[index+3] = 255; + } + // std::cout << "rgba buffer is " << rgbaBuffer.size() << std::endl; + frame result = frame(res.x,res.y, frame::colormap::RGBA); + result.setData(rgbaBuffer); + updatingView = false; + return result; + break; + } + case frame::colormap::BGR: { + std::vector rgbaBuffer(width*height*3, 0); + for (const auto& [v2, temp] : tempBuffer) { + size_t index = (v2.y * width + v2.x) * 3; + uint8_t atemp = static_cast((((temp-minTemp)) / (maxTemp-minTemp)) * 255); + rgbaBuffer[index+2] = atemp; + rgbaBuffer[index+1] = atemp; + rgbaBuffer[index+0] = atemp; + //rgbaBuffer[index+3] = 255; + } + // std::cout << "rgba buffer is " << rgbaBuffer.size() << std::endl; + frame result = frame(res.x,res.y, frame::colormap::BGR); + result.setData(rgbaBuffer); + updatingView = false; + return result; + break; + } + case frame::colormap::RGB: + default: { + std::vector rgbaBuffer(width*height*3, 0); + for (const auto& [v2, temp] : tempBuffer) { + size_t index = (v2.y * width + v2.x) * 3; + uint8_t atemp = static_cast((((temp-minTemp)) / (maxTemp-minTemp)) * 255); + rgbaBuffer[index+0] = atemp; + rgbaBuffer[index+1] = atemp; + rgbaBuffer[index+2] = atemp; + //rgbaBuffer[index+3] = 255; + } + // std::cout << "rgba buffer is " << rgbaBuffer.size() << std::endl; + frame result = frame(res.x,res.y, frame::colormap::RGB); + result.setData(rgbaBuffer); + updatingView = false; + return result; + break; + } } - std::cout << "rgba buffer is " << rgbaBuffer.size() << std::endl; - frame result = frame(res.x,res.y, frame::colormap::RGB); - result.setData(rgbaBuffer); - updatingView = false; - return result; } + }; #endif \ No newline at end of file diff --git a/util/output/bmpwriter.hpp b/util/output/bmpwriter.hpp index e4ff39f..791d42a 100644 --- a/util/output/bmpwriter.hpp +++ b/util/output/bmpwriter.hpp @@ -145,12 +145,9 @@ public: static bool saveBMP(const std::string& filename, frame& frame) { if (frame.colorFormat == frame::colormap::RGB) { - std::cout << "found correct colormap" << std::endl; return saveBMP(filename, frame.getData(), frame.getWidth(), frame.getHeight()); } else if (frame.colorFormat == frame::colormap::RGBA) { - std::cout << "found incorrect colormap. converting from RGBA" << std::endl; std::vector fdata = convertRGBAtoRGB(frame.getData()); - std::cout << "source data: " << frame.getData().size() << " out data: " << fdata.size() << std::endl; return saveBMP(filename, fdata, frame.getWidth(), frame.getHeight()); } else { diff --git a/util/output/frame.hpp b/util/output/frame.hpp index 8253e23..9b29461 100644 --- a/util/output/frame.hpp +++ b/util/output/frame.hpp @@ -76,7 +76,6 @@ public: _compressedData.shrink_to_fit(); overheadmap.clear(); sourceSize = data.size(); - std::cout << "wrote " << data.size() << " as frame" << std::endl; } const std::vector& getData() const { diff --git a/util/simblocks/temp.hpp b/util/simblocks/temp.hpp index eadc68d..d3cf77c 100644 --- a/util/simblocks/temp.hpp +++ b/util/simblocks/temp.hpp @@ -10,10 +10,31 @@ class Temp { private: protected: + static Vec2 findClosestPoint(const Vec2& position, std::unordered_map others) { + if (others.empty()) { + return position; + } + + auto closest = others.begin(); + double minDistance = position.distance(closest->first); + + for (auto it = std::next(others.begin()); it != others.end(); ++it) { + double distance = position.distance(it->first); + if (distance < minDistance) { + minDistance = distance; + closest = it; + } + } + + return closest->first; + } + public: double temp; - Temp(float temp) : temp(temp) {}; + Temp(float temp) : temp(temp) { + //std::cout << "setting temp to: " << temp << std::endl; + }; Temp(const Vec2& testPos, const std::unordered_map& others) { TIME_FUNCTION; @@ -52,6 +73,48 @@ public: } return num / den; } + + static float calGrad(const Vec2& testPos, std::unordered_map others) { + std::vector> nearbyPoints; + for (const auto& [point, temp] : others) { + if (point.distance(testPos) <= 25) { + nearbyPoints.emplace_back(point, temp.temp); + } + } + double sumX, sumY, sumT, sumX2, sumY2, sumXY, sumXT, sumYT = 0; + int n = nearbyPoints.size(); + for (const auto& [point, temp] : nearbyPoints) { + double x = point.x - testPos.x; + double y = point.y - testPos.y; + + sumX += x; + sumY += y; + sumT += temp; + sumX2 += x * x; + sumY2 += y * y; + sumXY += x * y; + sumXT += x * temp; + sumYT += y * temp; + } + + double det = sumX2 * sumY2 - sumXY * sumXY; + + if (std::abs(det) < 1e-10) { + Vec2 calpoint = Vec2(0, 0); // Singular matrix, cannot solve + } + + double a = (sumXT * sumY2 - sumYT * sumXY) / det; + double b = (sumX2 * sumYT - sumXY * sumXT) / det; + + Vec2 calpoint = Vec2(a, b); // ∇T = (∂T/∂x, ∂T/∂y) + + Vec2 closest = findClosestPoint(testPos, others); + Vec2 displacement = testPos - closest; + + float refTemp = others.at(closest).temp; + float estimatedTemp = refTemp + (calpoint.x * displacement.x + calpoint.y * displacement.y); + return estimatedTemp; + } }; #endif \ No newline at end of file