#include #include #include #include #include #include #include #include "../util/grid/grid22.hpp" #include "../util/output/aviwriter.hpp" #include "../util/output/bmpwriter.hpp" #include "../util/timing_decorator.cpp" struct AnimationConfig { int width = 256; int height = 256; int totalFrames = 480; float fps = 30.0f; int numSeeds = 8; }; Grid2 setup(AnimationConfig config) { TIME_FUNCTION; Grid2 grid; std::vector pos; std::vector colors; std::vector sizes; for (int y = 0; y < config.height; ++y) { for (int x = 0; x < config.width; ++x) { float gradient = (x + y) / float(config.width + config.height - 2); pos.push_back(Vec2(x,y)); colors.push_back(Vec4(gradient, gradient, gradient, 1.0f)); sizes.push_back(1.0f); } } grid.bulkAddObjects(pos,colors,sizes); return grid; } void Preview(Grid2 grid) { TIME_FUNCTION; int width; int height; std::vector rgbData; grid.getGridAsRGB(width,height,rgbData); bool success = BMPWriter::saveBMP("output/grayscalesource.bmp", rgbData, width, height); } std::vector> pickSeeds(Grid2 grid, AnimationConfig config) { TIME_FUNCTION; std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> xDist(0, config.width - 1); std::uniform_int_distribution<> yDist(0, config.height - 1); std::uniform_real_distribution<> colorDist(0.2f, 0.8f); std::vector> seeds; for (int i = 0; i < config.numSeeds; ++i) { Vec2 point(xDist(gen), yDist(gen)); Vec4 color(colorDist(gen), colorDist(gen), colorDist(gen), 1.0f); size_t id = grid.getPositionVec(point); grid.setColor(id, color); seeds.push_back(std::make_tuple(id,point, color)); } return seeds; } void expandPixel(Grid2& grid, AnimationConfig config, std::vector>& seeds) { TIME_FUNCTION; std::vector> newseeds; std::unordered_set visitedThisFrame; for (const auto& seed : seeds) { visitedThisFrame.insert(std::get<0>(seed)); } for (const std::tuple& seed : seeds) { size_t id = std::get<0>(seed); Vec2 seedPOS = std::get<1>(seed); Vec4 seedColor = std::get<2>(seed); std::vector neighbors = grid.getNeighbors(id); for (size_t neighbor : neighbors) { if (visitedThisFrame.count(neighbor)) { continue; } visitedThisFrame.insert(neighbor); Vec2 neipos = grid.getPositionID(neighbor); Vec4 neighborColor = grid.getColor(neighbor); float distance = seedPOS.distance(neipos); float angle = seedPOS.directionTo(neipos); float normalizedAngle = (angle + M_PI) / (2.0f * M_PI); float blendFactor = 0.3f + 0.4f * std::sin(normalizedAngle * 2.0f * M_PI); blendFactor = std::clamp(blendFactor, 0.1f, 0.9f); Vec4 newcolor = Vec4( seedColor.r * blendFactor + neighborColor.r * (1.0f - blendFactor), seedColor.g * (1.0f - blendFactor) + neighborColor.g * blendFactor, seedColor.b * (0.5f + 0.5f * std::sin(normalizedAngle * 4.0f * M_PI)), 1.0f ); newcolor = newcolor.clamp(0.0f, 1.0f); grid.setColor(neighbor, newcolor); newseeds.emplace_back(neighbor, neipos, newcolor); } } seeds.clear(); seeds = std::move(newseeds); } bool exportavi(std::vector> frames, AnimationConfig config) { TIME_FUNCTION; std::string filename = "output/chromatic_transformation.avi"; std::cout << "Frame count: " << frames.size() << std::endl; std::cout << "Frame size: " << (frames.empty() ? 0 : frames[0].size()) << std::endl; std::cout << "Width: " << config.width << ", Height: " << config.height << std::endl; std::filesystem::path dir = "output"; if (!std::filesystem::exists(dir)) { if (!std::filesystem::create_directories(dir)) { std::cout << "Failed to create output directory!" << std::endl; return false; } } bool success = AVIWriter::saveAVI(filename, frames, config.width+1, config.height+1, config.fps); if (success) { // Check if file actually exists if (std::filesystem::exists(filename)) { auto file_size = std::filesystem::file_size(filename); } } else { std::cout << "Failed to save AVI file!" << std::endl; } return success; } int main() { AnimationConfig config; Grid2 grid = setup(config); grid.updateNeighborMap(); Preview(grid); std::vector> seeds = pickSeeds(grid,config); std::vector> frames; for (int i = 0; i < config.totalFrames; ++i){ std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; expandPixel(grid,config,seeds); int width; int height; std::vector frame; grid.getGridAsBGR(width,height,frame); frames.push_back(frame); } exportavi(frames,config); FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED); return 0; }