From 69fe16df2639c8f3aed8502db0e2873bce685086 Mon Sep 17 00:00:00 2001 From: Yggdrasil75 Date: Fri, 28 Nov 2025 10:24:23 -0500 Subject: [PATCH] fixed some mistakes from last version. --- makefile | 1 - tests/g2temp.cpp | 26 ++++------------ util/grid/grid2.hpp | 69 ++++++++++++++++++++--------------------- util/noise/pnoise2.hpp | 19 +++++++++--- util/simblocks/temp.hpp | 4 +-- 5 files changed, 55 insertions(+), 64 deletions(-) diff --git a/makefile b/makefile index 3bbe7c7..8458ce0 100644 --- a/makefile +++ b/makefile @@ -8,7 +8,6 @@ STB_DIR := ./stb # Compiler and flags CXX := g++ CXXFLAGS = -std=c++23 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(STB_DIR) -#CXXFLAGS += -g -Wall -Wformat CXXFLAGS += `pkg-config --cflags glfw3` CFLAGS = $(CXXFLAGS) LDFLAGS := -L./imgui -limgui -lGL diff --git a/tests/g2temp.cpp b/tests/g2temp.cpp index fe99791..f7d8059 100644 --- a/tests/g2temp.cpp +++ b/tests/g2temp.cpp @@ -87,7 +87,7 @@ void Preview(Grid2& grid) { void livePreview(Grid2& grid, AnimationConfig config) { std::lock_guard lock(previewMutex); - currentPreviewFrame = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256)); + currentPreviewFrame = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256),frame::colormap::RGBA); // Vec2 min; // Vec2 max; // grid.getBoundingBox(min, max); @@ -253,7 +253,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { if (gradnoise == 0) { grid = setup(config); } else if (gradnoise == 1) { - grid = grid.noiseGenGridTemps(0,0,config.height-1, config.width-1, 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)); { @@ -268,7 +268,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { std::cout << "generated grid" << std::endl; Preview(grid); std::cout << "generated preview" << std::endl; - grid = grid.backfillGrid(); + //grid = grid.backfillGrid(); frame tempData = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256)); std::cout << "Temp frame looks like: " << tempData << std::endl; bool success = BMPWriter::saveBMP("output/temperature.bmp", tempData); @@ -286,7 +286,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { } //expandPixel(grid,config,seeds); - //grid.diffuseTemperatures(1.0, 1.0, 1.0); + grid.diffuseTemps(100); std::lock_guard lock(state.mutex); state.grid = grid; @@ -395,11 +395,7 @@ int main() { ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas"); #endif ImGui_ImplOpenGL3_Init(glsl_version); - - - // std::cout << "created glfw window" << std::endl; - - + bool show_demo_window = true; bool show_another_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); @@ -498,7 +494,6 @@ int main() { ImGui::Text(previewText.c_str()); } else if (textu != 0){ - //ImGui::EndDisabled(); ImGui::Text(previewText.c_str()); @@ -529,14 +524,11 @@ int main() { ImGui::Text("No preview available"); ImGui::Text("Start generation to see live preview"); } - //std::cout << "sleeping" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(100)); - //std::cout << "ending" << std::endl; ImGui::End(); } - // std::cout << "ending frame" << std::endl; ImGui::Render(); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); @@ -544,31 +536,25 @@ int main() { glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); - // std::cout << "rendering" << std::endl; ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); - //mainlogicthread.join(); - // std::cout << "swapping buffers" << std::endl; } cancelGeneration(); + FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED); - // std::cout << "shutting down" << std::endl; ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); - // std::cout << "destroying" << std::endl; glfwDestroyWindow(window); if (textu != 0) { glDeleteTextures(1, &textu); textu = 0; } glfwTerminate(); - FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED); - // std::cout << "printing" << std::endl; return 0; } \ No newline at end of file diff --git a/util/grid/grid2.hpp b/util/grid/grid2.hpp index 38ed260..0400a4c 100644 --- a/util/grid/grid2.hpp +++ b/util/grid/grid2.hpp @@ -230,11 +230,6 @@ protected: std::vector unassignedIDs; - //grid min - Vec2 gridMin; - //grid max - Vec2 gridMax; - float neighborRadius = 1.0f; //TODO: spatial map @@ -243,20 +238,18 @@ protected: // Default background color for empty spaces Vec4 defaultBackgroundColor = Vec4(0.0f, 0.0f, 0.0f, 0.0f); - PNoise2 noisegen; - //water std::unordered_map water; std::unordered_map tempMap; + bool regenpreventer = false; public: - bool usable = false; - Grid2 noiseGenGrid(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; + noisegen = PNoise2(noisemod); std::cout << "generating a noise grid with the following: (" << minx << ", " << miny << ") by (" << maxx << ", " << maxy << ") " << "chance: " << minChance << " max: " << maxChance << " gen colors: " << color << std::endl; @@ -270,9 +263,6 @@ public: float alpha = noisegen.permute(pos); if (alpha > minChance && alpha < maxChance) { if (color) { - // float red = noisegen.noise(x,y,1000); - // float green = noisegen.noise(x,y,2000); - // float blue = noisegen.noise(x,y,3000); 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)); @@ -403,10 +393,9 @@ public: } } - size_t getOrCreatePositionVec(const Vec2& pos, float radius = 0.0f, bool create = false) { + size_t getOrCreatePositionVec(const Vec2& pos, float radius = 0.0f, bool create = true) { TIME_FUNCTION; if (radius == 0.0f) { - // Exact match - use spatial grid to find the cell Vec2 gridPos = spatialGrid.worldToGrid(pos); auto cellIt = spatialGrid.grid.find(gridPos); if (cellIt != spatialGrid.grid.end()) { @@ -423,7 +412,7 @@ public: } else { auto results = getPositionVecRegion(pos, radius); if (!results.empty()) { - return results[0]; // Return first found + return results[0]; } if (create) { return addObject(pos, defaultBackgroundColor, 1.0f); @@ -432,7 +421,6 @@ public: } } - //get all id in region std::vector getPositionVecRegion(const Vec2& pos, float radius = 1.0f) const { TIME_FUNCTION; float searchRadius = (radius == 0.0f) ? std::numeric_limits::epsilon() : radius; @@ -457,7 +445,7 @@ public: return Pixels.at(id).getColor(); } - double getTemp(size_t id) { + float getTemp(size_t id) { if (tempMap.find(id) != tempMap.end()) { Temp temp = Temp(getPositionID(id), getTemps()); tempMap.emplace(id, temp); @@ -468,20 +456,18 @@ public: return tempMap.at(id).temp; } - double getTemp(Vec2 pos) { - size_t posid; - if (Positions.contains(pos)) { - posid = Positions.at(pos); - } - if (tempMap.find(posid) != tempMap.end()) { - return Temp(getPositionID(posid), getTemps()).temp; - } - else { - return tempMap.at(posid).temp; + double getTemp(const Vec2 pos) { + size_t id = getOrCreatePositionVec(pos, 0.01f, true); + if (tempMap.find(id) == tempMap.end()) { + //std::cout << "missing a temp at: " << pos << std::endl; + double dtemp = Temp::calTempIDW(pos, getTemps(id)); + setTemp(id, dtemp); + return dtemp; } + else return tempMap.at(id).temp; } - std::unordered_map getTemps() { + std::unordered_map getTemps() const { std::unordered_map out; for (const auto& [id, temp] : tempMap) { out.emplace(getPositionID(id), temp); @@ -489,7 +475,7 @@ public: return out; } - std::unordered_map getTemps(size_t id) { + std::unordered_map getTemps(size_t id) const { std::unordered_map out; std::vector tval = spatialGrid.queryRange(Positions.at(id), 10); for (size_t tempid : tval) { @@ -528,7 +514,7 @@ public: } frame getGridRegionAsFrame(const Vec2& minCorner, const Vec2& maxCorner, - Vec2& res, frame::colormap outChannels = frame::colormap::RGB) const { + Vec2& res, frame::colormap outChannels = frame::colormap::RGB) { TIME_FUNCTION; size_t width = static_cast(maxCorner.x - minCorner.x); size_t height = static_cast(maxCorner.y - minCorner.y); @@ -544,6 +530,8 @@ public: width = height = 0; return outframe; } + if (regenpreventer) return outframe; + else regenpreventer = true; std::cout << "Rendering region: " << minCorner << " to " << maxCorner << " at resolution: " << res << std::endl; @@ -568,7 +556,6 @@ public: colorTempBuffer[pix] += Pixels.at(id).getColor(); countBuffer[pix]++; - //std::cout << "pixel at " << pix << " is: " << Pixels.at(id).getColor(); } } std::cout << std::endl << "built initial buffer" << std::endl; @@ -596,6 +583,7 @@ public: frame result = frame(res.x,res.y, frame::colormap::RGBA); result.setData(colorBuffer2); std::cout << "returning result" << std::endl; + regenpreventer = false; return result; break; } @@ -613,6 +601,7 @@ public: frame result = frame(res.x,res.y, frame::colormap::BGR); result.setData(colorBuffer2); std::cout << "returning result" << std::endl; + regenpreventer = false; return result; break; } @@ -631,6 +620,7 @@ public: frame result = frame(res.x,res.y, frame::colormap::RGB); result.setData(colorBuffer2); std::cout << "returning result" << std::endl; + regenpreventer = false; return result; break; } @@ -648,6 +638,8 @@ public: frame getTempAsFrame(Vec2 minCorner, Vec2 maxCorner, Vec2 res, frame::colormap outcolor = frame::colormap::RGB) { TIME_FUNCTION; + if (regenpreventer) return frame(); + else regenpreventer = true; int pcount = 0; size_t sheight = maxCorner.x - minCorner.x; size_t swidth = maxCorner.y - minCorner.y; @@ -686,6 +678,7 @@ public: } frame result = frame(res.x,res.y, frame::colormap::RGBA); result.setData(rgbaBuffer); + regenpreventer = false; return result; break; } @@ -700,6 +693,7 @@ public: } frame result = frame(res.x,res.y, frame::colormap::BGR); result.setData(rgbaBuffer); + regenpreventer = false; return result; break; } @@ -715,6 +709,7 @@ public: } frame result = frame(res.x,res.y, frame::colormap::RGB); result.setData(rgbaBuffer); + regenpreventer = false; return result; break; } @@ -763,7 +758,6 @@ public: shrinkIfNeeded(); - usable = true; return newids; } @@ -792,7 +786,6 @@ public: shrinkIfNeeded(); - usable = true; return newids; } @@ -860,6 +853,7 @@ public: 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; + noisegen = PNoise2(noisemod); std::cout << "generating a noise grid with the following: (" << minx << ", " << miny << ") by (" << maxx << ", " << maxy << ") " << "chance: " << minChance << " max: " << maxChance << " gen colors: " << color << std::endl; @@ -990,9 +984,12 @@ public: if (tempMap.empty() || timestep < 1) return; std::unordered_map cTemps; cTemps.reserve(tempMap.size()); - for (const auto& [id, temp] : tempMap) { - Vec2 pos = getPositionID(id); - + for (const auto& [id, pos] : Positions) { + float oldtemp = getTemp(id); + tempMap.erase(id); + float newtemp = Temp::calGrad(pos, getTemps(id)); + float newtempMult = (newtemp-oldtemp) * timestep; + setTemp(id, newtempMult); } } }; diff --git a/util/noise/pnoise2.hpp b/util/noise/pnoise2.hpp index 976f2ed..3c2cbe0 100644 --- a/util/noise/pnoise2.hpp +++ b/util/noise/pnoise2.hpp @@ -14,6 +14,7 @@ class PNoise2 { private: std::vector permutation; std::default_random_engine rng; + float lerp(float t, float a1, float a2) { return a1 + t * (a2 - a1); } @@ -49,18 +50,26 @@ private: return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } -public: - PNoise2() : rng(std::random_device{}()) { + void initializePermutation() { + permutation.clear(); std::vector permutationt; permutationt.reserve(256); for (int i = 0; i < 256; i++){ permutationt.push_back(i); } std::ranges::shuffle(permutationt, rng); - permutation.insert(permutation.end(),permutationt.begin(),permutationt.end()); - permutation.insert(permutation.end(),permutationt.begin(),permutationt.end()); + permutation.insert(permutation.end(), permutationt.begin(), permutationt.end()); + permutation.insert(permutation.end(), permutationt.begin(), permutationt.end()); } - +public: + PNoise2() : rng(std::random_device{}()) { + initializePermutation(); + } + + PNoise2(unsigned int seed) : rng(seed) { + initializePermutation(); + } + float permute(Vec2 point) { TIME_FUNCTION; float x = point.x; diff --git a/util/simblocks/temp.hpp b/util/simblocks/temp.hpp index 75f960f..0e08950 100644 --- a/util/simblocks/temp.hpp +++ b/util/simblocks/temp.hpp @@ -59,7 +59,7 @@ public: this->temp = num / den; } - static float calTempIDW(const Vec2& testPos, std::unordered_map others) { + static float calTempIDW(const Vec2& testPos, const std::unordered_map& others) { TIME_FUNCTION; float power = 2.0; float num = 0.0; @@ -78,7 +78,7 @@ public: return num / den; } - static float calGrad(const Vec2& testPos, std::unordered_map& others) { + static float calGrad(const Vec2& testPos, const std::unordered_map& others) { std::vector> nearbyPoints; for (const auto& [point, temp] : others) { if (point.distance(testPos) <= 25) {