diff --git a/.vscode/settings.json b/.vscode/settings.json index e6e3417..061a450 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -103,5 +103,6 @@ "**/*.rpa": true, "**/*.rpymc": true, "**/cache/": true - } + }, + "cmake.ignoreCMakeListsMissing": true } \ No newline at end of file diff --git a/tests/g2temp.cpp b/tests/g2temp.cpp index 8854821..c778f0e 100644 --- a/tests/g2temp.cpp +++ b/tests/g2temp.cpp @@ -84,10 +84,10 @@ void Preview(Grid2& grid) { } } -void livePreview(Grid2& grid) { +void livePreview(Grid2& grid, AnimationConfig config) { std::lock_guard lock(previewMutex); - currentPreviewFrame = grid.getGridAsFrame(frame::colormap::RGBA); + currentPreviewFrame = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256)); // Vec2 min; // Vec2 max; // grid.getBoundingBox(min, max); @@ -246,7 +246,7 @@ bool exportavi(std::vector frames, AnimationConfig config) { void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { TIME_FUNCTION; if (isGenerating) return; //apparently sometimes this function is called twice. dont know how, but this might resolve that. - isGenerating = true; + try { Grid2 grid; if (gradnoise == 0) { @@ -273,7 +273,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { if (!success) { std::cout << "yo! this failed in Preview" << std::endl; } - + isGenerating = true; std::vector frames; for (int i = 0; i < config.totalFrames; ++i){ @@ -284,6 +284,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { } //expandPixel(grid,config,seeds); + grid.diffuseTemperatures(1.0, 1.0, 1.0); std::lock_guard lock(state.mutex); state.grid = grid; @@ -291,7 +292,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { state.currentFrame = i; // Print compression info for this frame - if (i % 10 == 0 ) { + //if (i % 10 == 0 ) { frame bgrframe; std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; bgrframe = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256), frame::colormap::BGR); @@ -300,7 +301,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { //BMPWriter::saveBMP(std::format("output/grayscalesource.{}.bmp", i), bgrframe); bgrframe.compressFrameLZ78(); //bgrframe.printCompressionStats(); - } + //} } exportavi(frames,config); } @@ -457,7 +458,7 @@ int main() { { std::lock_guard lock(state.mutex); if (state.hasNewFrame) { - livePreview(state.grid); + livePreview(state.grid, config); state.hasNewFrame = false; previewText = "Generating... Frame: " + std::to_string(state.currentFrame); } @@ -486,7 +487,7 @@ int main() { { std::lock_guard lock(state.mutex); if (state.hasNewFrame) { - livePreview(state.grid); + livePreview(state.grid, config); state.hasNewFrame = false; previewText = "Generating... Frame: " + std::to_string(state.currentFrame); } diff --git a/util/grid/grid2.hpp b/util/grid/grid2.hpp index 5e5f798..c1fd12b 100644 --- a/util/grid/grid2.hpp +++ b/util/grid/grid2.hpp @@ -201,8 +201,6 @@ protected: //grid max Vec2 gridMax; - //neighbor map - //std::unordered_map> neighborMap; float neighborRadius = 1.0f; //TODO: spatial map @@ -1202,6 +1200,112 @@ public: } } + void diffuseTemperatures(double thermalDiffusivity, double timeStep, double gridSpacing = 1.0) { + TIME_FUNCTION; + + if (tempMap.empty()) { + return; + } + + // Create a copy of current temperatures to avoid modifying while iterating + std::unordered_map newTemps; + newTemps.reserve(tempMap.size()); + + // Diffuse heat for each point + for (const auto& [id, tempObj] : tempMap) { + Vec2 position = Positions.at(id); + double currentTemp = tempObj.temp; + + // Get nearby points for diffusion + std::unordered_map neighbors; + std::vector neighborIDs = spatialGrid.queryRange(position, neighborRadius * 2.0); // Use larger radius for temp diffusion + + for (size_t neighborID : neighborIDs) { + if (neighborID != id && tempMap.find(neighborID) != tempMap.end()) { + Vec2 neighborPos = Positions.at(neighborID); + neighbors.emplace(neighborPos, tempMap.at(neighborID)); + } + } + + // Diffuse heat using the existing function from Temp class + double newTemp = Temp::diffuseHeat(position, neighbors, currentTemp, + thermalDiffusivity, timeStep, gridSpacing); + + newTemps[id] = newTemp; + } + + // Update temperatures + for (const auto& [id, newTemp] : newTemps) { + tempMap.at(id).temp = newTemp; + } + } + + void diffuseTemperaturesWeighted(double thermalDiffusivity, double timeStep) { + TIME_FUNCTION; + + if (tempMap.empty()) { + return; + } + + // Create a copy of current temperatures to avoid modifying while iterating + std::unordered_map newTemps; + newTemps.reserve(tempMap.size()); + + // Diffuse heat for each point using weighted method + for (const auto& [id, tempObj] : tempMap) { + Vec2 position = Positions.at(id); + double currentTemp = tempObj.temp; + + // Get nearby points for diffusion + std::unordered_map neighbors; + std::vector neighborIDs = spatialGrid.queryRange(position, neighborRadius * 2.0); + + for (size_t neighborID : neighborIDs) { + if (neighborID != id && tempMap.find(neighborID) != tempMap.end()) { + Vec2 neighborPos = Positions.at(neighborID); + neighbors.emplace(neighborPos, tempMap.at(neighborID)); + } + } + + // Diffuse heat using the weighted method + double newTemp = Temp::diffuseHeatWeighted(position, neighbors, currentTemp, + thermalDiffusivity, timeStep); + + newTemps[id] = newTemp; + } + + // Update temperatures + for (const auto& [id, newTemp] : newTemps) { + tempMap.at(id).temp = newTemp; + } + } + + // Optional: Diffuse temperature for a specific point + double diffuseTemperatureAtPoint(size_t id, double thermalDiffusivity, double timeStep, double gridSpacing = 1.0) { + if (tempMap.find(id) == tempMap.end()) { + return 0.0; + } + + Vec2 position = Positions.at(id); + double currentTemp = tempMap.at(id).temp; + + // Get nearby points + std::unordered_map neighbors; + std::vector neighborIDs = spatialGrid.queryRange(position, neighborRadius * 2.0); + + for (size_t neighborID : neighborIDs) { + if (neighborID != id && tempMap.find(neighborID) != tempMap.end()) { + Vec2 neighborPos = Positions.at(neighborID); + neighbors.emplace(neighborPos, tempMap.at(neighborID)); + } + } + + double newTemp = Temp::diffuseHeat(position, neighbors, currentTemp, + thermalDiffusivity, timeStep, gridSpacing); + + tempMap.at(id).temp = newTemp; + return newTemp; + } }; diff --git a/util/simblocks/temp.hpp b/util/simblocks/temp.hpp index b85b5ae..a717d5b 100644 --- a/util/simblocks/temp.hpp +++ b/util/simblocks/temp.hpp @@ -33,7 +33,6 @@ protected: public: double temp; Temp(float temp) : temp(temp) { - //std::cout << "setting temp to: " << temp << std::endl; }; Temp(const Vec2& testPos, const std::unordered_map& others) { @@ -133,7 +132,7 @@ public: } if (validNeighbors > 0) { - laplacian /= (gridSpacing * gridSpacing); + laplacian /= (validNeighbors * gridSpacing * gridSpacing); double tempChange = thermalDiffusivity * timeStep * laplacian; return currentTemp + tempChange;