moved stuff around, added a grayscale test.

This commit is contained in:
Yggdrasil75
2025-11-11 14:34:19 -05:00
parent 5b7b6115a9
commit bff1efb291
13 changed files with 851 additions and 414 deletions

141
tests/g2chromatic.cpp Normal file
View File

@@ -0,0 +1,141 @@
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include "../util/grid/grid2.hpp"
#include "../util/output/aviwriter.hpp"
int main() {
// Create a Grid2 instance
Grid2 grid;
// Grid dimensions
const int width = 100;
const int height = 100;
const int totalFrames = 60; // 2 seconds at 30fps
std::cout << "Creating chromatic transformation animation..." << std::endl;
// Initialize with grayscale gradient
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
float gradient = (x + y) / float(width + height - 2);
Vec2 position(static_cast<float>(x), static_cast<float>(y));
Vec4 color(gradient, gradient, gradient, 1.0f);
grid.addObject(position, color, 1.0f);
}
}
std::cout << "Initial grayscale grid created with " << width * height << " objects" << std::endl;
// Random number generation for seed points
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> xDist(0, width - 1);
std::uniform_int_distribution<> yDist(0, height - 1);
std::uniform_real_distribution<> colorDist(0.2f, 0.8f);
// Generate multiple seed points for more interesting patterns
const int numSeeds = 8;
std::vector<Vec2> seedPoints;
std::vector<Vec4> seedColors;
for (int i = 0; i < numSeeds; ++i) {
seedPoints.emplace_back(xDist(gen), yDist(gen));
seedColors.emplace_back(colorDist(gen), colorDist(gen), colorDist(gen), colorDist(gen));
}
std::cout << "Generated " << numSeeds << " seed points for color propagation" << std::endl;
// Create frames for AVI
std::vector<std::vector<uint8_t>> frames;
for (int frame = 0; frame < totalFrames; ++frame) {
std::cout << "Processing frame " << frame + 1 << "/" << totalFrames << std::endl;
// Apply color propagation based on frame progress
float progress = static_cast<float>(frame) / (totalFrames - 1);
// Update colors based on seed propagation
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Vec2 currentPos(x, y);
size_t id = grid.getIndicesAt(currentPos)[0]; // Assuming one object per position
Vec4 originalColor = grid.getColor(id);
Vec4 newColor = originalColor;
// For each seed point, calculate influence
for (int s = 0; s < numSeeds; ++s) {
float distance = currentPos.distance(seedPoints[s]);
float maxDistance = std::max(width, height) * 0.6f;
float influence = std::max(0.0f, 1.0f - (distance / maxDistance));
// Apply influence based on relative position to seed
Vec2 direction = currentPos - seedPoints[s];
float angle = std::atan2(direction.y, direction.x);
// Different color channels respond to different directions
if (std::abs(angle) < M_PI / 4.0f) { // Right - affect alpha
newColor.a = std::fmod(newColor.a + seedColors[s].a * influence * progress, 1.0f);
} else if (std::abs(angle) > 3.0f * M_PI / 4.0f) { // Left - affect blue
newColor.b = std::fmod(newColor.b + seedColors[s].b * influence * progress, 1.0f);
} else if (angle > 0) { // Below - affect green
newColor.g = std::fmod(newColor.g + seedColors[s].g * influence * progress, 1.0f);
} else { // Above - affect red
newColor.r = std::fmod(newColor.r + seedColors[s].r * influence * progress, 1.0f);
}
}
// Clamp colors to valid range
newColor = newColor.clampColor();
grid.setColor(id, newColor);
}
}
// Get current frame as RGB data
int frameWidth, frameHeight;
std::vector<int> rgbData;
grid.getGridAsRGB(frameWidth, frameHeight, rgbData);
// Convert to BGR format for AVI (OpenCV uses BGR)
std::vector<uint8_t> bgrFrame(frameWidth * frameHeight * 3);
#pragma omp parallel for
for (int i = 0; i < frameWidth * frameHeight; ++i) {
bgrFrame[i * 3] = rgbData[i * 3 + 2];
bgrFrame[i * 3 + 1] = rgbData[i * 3 + 1];
bgrFrame[i * 3 + 2] = rgbData[i * 3];
}
// for (int i = 0; i < frameWidth * frameHeight; ++i) {
// bgrFrame[i * 3] = static_cast<uint8_t>(rgbData[i * 3 + 2]); // B
// bgrFrame[i * 3 + 1] = static_cast<uint8_t>(rgbData[i * 3 + 1]); // G
// bgrFrame[i * 3 + 2] = static_cast<uint8_t>(rgbData[i * 3]); // R
// }
frames.push_back(bgrFrame);
}
// Save as AVI
std::string filename = "output/chromatic_transformation.avi";
bool success = AVIWriter::saveAVI(filename, frames, width, height, 30.0f);
if (success) {
std::cout << "\nSuccessfully saved chromatic transformation animation to: " << filename << std::endl;
std::cout << "Video details:" << std::endl;
std::cout << " - Dimensions: " << width << " x " << height << std::endl;
std::cout << " - Frames: " << totalFrames << " (2 seconds at 30fps)" << std::endl;
std::cout << " - Seed points: " << numSeeds << std::endl;
// Print seed point information
std::cout << "\nSeed points used:" << std::endl;
for (int i = 0; i < numSeeds; ++i) {
std::cout << " Seed " << i + 1 << ": Position " << seedPoints[i]
<< ", Color " << seedColors[i].toColorString() << std::endl;
}
} else {
std::cerr << "Failed to save AVI file!" << std::endl;
return 1;
}
return 0;
}

74
tests/g2grayscale.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <iostream>
#include <vector>
#include "../util/grid/grid2.hpp"
#include "../util/output/bmpwriter.hpp"
int main() {
// Create a Grid2 instance
Grid2 grid;
// Grid dimensions
const int width = 100;
const int height = 100;
std::cout << "Creating grayscale gradient..." << std::endl;
// Add objects to create a grayscale gradient
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// Calculate gradient value (0.0 at top-left to 1.0 at bottom-right)
float gradient = (x + y) / float(width + height - 2);
// Create position
Vec2 position(static_cast<float>(x), static_cast<float>(y));
// Create grayscale color (r=g=b=gradient, a=1.0)
Vec4 color(gradient, gradient, gradient, 1.0f);
// Add to grid with size 1.0 (single pixel)
grid.addObject(position, color, 1.0f);
}
}
std::cout << "Added " << width * height << " objects to grid" << std::endl;
// Get the entire grid as RGB data
int outputWidth, outputHeight;
std::vector<int> rgbData;
grid.getGridAsRGB(outputWidth, outputHeight, rgbData);
std::cout << "Output dimensions: " << outputWidth << " x " << outputHeight << std::endl;
std::cout << "RGB data size: " << rgbData.size() << " elements" << std::endl;
// Convert RGB data to format suitable for BMPWriter
std::vector<Vec3> pixels;
pixels.reserve(outputWidth * outputHeight);
for (size_t i = 0; i < rgbData.size(); i += 3) {
float r = rgbData[i] / 255.0f;
float g = rgbData[i + 1] / 255.0f;
float b = rgbData[i + 2] / 255.0f;
pixels.emplace_back(r, g, b);
}
// Save as BMP
std::string filename = "output/grayscale_gradient.bmp";
bool success = BMPWriter::saveBMP(filename, pixels, outputWidth, outputHeight);
if (success) {
std::cout << "Successfully saved grayscale gradient to: " << filename << std::endl;
// Print some gradient values for verification
std::cout << "\nGradient values at key positions:" << std::endl;
std::cout << "Top-left (0,0): " << grid.getColor(grid.getIndicesAt(0, 0)[0]).r << std::endl;
std::cout << "Center (" << width/2 << "," << height/2 << "): "
<< grid.getColor(grid.getIndicesAt(width/2, height/2)[0]).r << std::endl;
std::cout << "Bottom-right (" << width-1 << "," << height-1 << "): "
<< grid.getColor(grid.getIndicesAt(width-1, height-1)[0]).r << std::endl;
} else {
std::cerr << "Failed to save BMP file!" << std::endl;
return 1;
}
return 0;
}