This commit is contained in:
yggdrasil75
2025-11-09 17:10:32 -05:00
parent 8244d11a68
commit 0abe373959
4 changed files with 298 additions and 109 deletions

18
.vscode/settings.json vendored
View File

@@ -68,7 +68,23 @@
"xstring": "cpp", "xstring": "cpp",
"xtr1common": "cpp", "xtr1common": "cpp",
"xtree": "cpp", "xtree": "cpp",
"xutility": "cpp" "xutility": "cpp",
"bitset": "cpp",
"codecvt": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"string_view": "cpp",
"mutex": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"span": "cpp",
"cinttypes": "cpp",
"variant": "cpp",
"__nullptr": "cpp"
}, },
"files.exclude": { "files.exclude": {
"**/*.rpyc": true, "**/*.rpyc": true,

351
main.cpp
View File

@@ -5,6 +5,8 @@
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <queue>
#include <condition_variable>
#include "util/simple_httpserver.hpp" #include "util/simple_httpserver.hpp"
#include "util/grid2.hpp" #include "util/grid2.hpp"
#include "util/bmpwriter.hpp" #include "util/bmpwriter.hpp"
@@ -12,17 +14,35 @@
#include "util/timing_decorator.hpp" #include "util/timing_decorator.hpp"
#include "simtools/sim2.hpp" #include "simtools/sim2.hpp"
std::vector<uint8_t> currentFrame; // Global variables for live streaming
std::atomic<bool> frameReady{false}; std::queue<std::vector<uint8_t>> frameQueue;
std::mutex queueMutex;
std::condition_variable frameCondition;
std::atomic<bool> streaming{false}; std::atomic<bool> streaming{false};
std::mutex frameMutex; std::atomic<int> activeClients{0};
std::atomic<uint32_t> frameCounter{0};
// Current simulation parameters
struct SimulationParams {
std::string mode = "gradient";
uint32_t seed = 42;
float scale = 4.0f;
int octaves = 4;
float persistence = 0.5f;
float lacunarity = 2.0f;
float elevation = 1.0f;
float waterLevel = 0.3f;
};
SimulationParams currentParams;
std::mutex paramsMutex;
// Function to convert hex color string to Vec4 // Function to convert hex color string to Vec4
Vec4 hexToVec4(const std::string& hex) { Vec4 hexToVec4(const std::string& hex) {
TIME_FUNCTION; TIME_FUNCTION;
if (hex.length() != 6) { if (hex.length() != 6) {
return Vec4(0, 0, 0, 1); // Default to black if invalid return Vec4(0, 0, 0, 1);
} }
int r, g, b; int r, g, b;
@@ -36,31 +56,27 @@ std::vector<uint8_t> generateGradientFrame(int width = 512, int height = 512) {
TIME_FUNCTION; TIME_FUNCTION;
const int POINTS_PER_DIM = 256; const int POINTS_PER_DIM = 256;
Grid2 grid; Grid2 grid;
// Define our target colors at specific positions // Define colors
Vec4 white = hexToVec4("ffffff"); // Top-left corner (1,1) Vec4 white = hexToVec4("ffffff");
Vec4 red = hexToVec4("ff0000"); // Top-right corner (1,-1) Vec4 red = hexToVec4("ff0000");
Vec4 green = hexToVec4("00ff00"); // Center (0,0) Vec4 green = hexToVec4("00ff00");
Vec4 blue = hexToVec4("0000ff"); // Bottom-left corner (-1,-1) Vec4 blue = hexToVec4("0000ff");
Vec4 black = hexToVec4("000000"); // Bottom-right corner (-1,1) Vec4 black = hexToVec4("000000");
// Animate based on frame counter for live effect
float time = frameCounter * 0.01f;
// Create gradient points
for (int y = 0; y < POINTS_PER_DIM; ++y) { for (int y = 0; y < POINTS_PER_DIM; ++y) {
for (int x = 0; x < POINTS_PER_DIM; ++x) { for (int x = 0; x < POINTS_PER_DIM; ++x) {
// Normalize coordinates to [-1, 1]
float nx = (static_cast<float>(x) / (POINTS_PER_DIM - 1)) * 2.0f - 1.0f; float nx = (static_cast<float>(x) / (POINTS_PER_DIM - 1)) * 2.0f - 1.0f;
float ny = (static_cast<float>(y) / (POINTS_PER_DIM - 1)) * 2.0f - 1.0f; float ny = (static_cast<float>(y) / (POINTS_PER_DIM - 1)) * 2.0f - 1.0f;
// Create position
Vec2 pos(nx, ny); Vec2 pos(nx, ny);
// Convert to [0,1] range for interpolation
float u = (nx + 1.0f) / 2.0f; float u = (nx + 1.0f) / 2.0f;
float v = (ny + 1.0f) / 2.0f; float v = (ny + 1.0f) / 2.0f;
// Bilinear interpolation between corners
Vec4 top = white * (1.0f - u) + red * u; Vec4 top = white * (1.0f - u) + red * u;
Vec4 bottom = blue * (1.0f - u) + black * u; Vec4 bottom = blue * (1.0f - u) + black * u;
Vec4 cornerColor = top * (1.0f - v) + bottom * v; Vec4 cornerColor = top * (1.0f - v) + bottom * v;
@@ -74,30 +90,65 @@ std::vector<uint8_t> generateGradientFrame(int width = 512, int height = 512) {
} }
} }
// Render to RGB image
return grid.renderToRGB(width, height); return grid.renderToRGB(width, height);
} }
// Generate terrain simulation frame data
std::vector<uint8_t> generateTerrainFrame(int width = 512, int height = 512) {
TIME_FUNCTION;
SimulationParams params;
{
std::lock_guard<std::mutex> lock(paramsMutex);
params = currentParams;
}
// Use frame counter to animate terrain
uint32_t animatedSeed = params.seed + frameCounter;
Sim2 sim(width, height, animatedSeed, params.scale, params.octaves,
params.persistence, params.lacunarity, params.waterLevel, params.elevation);
sim.generateTerrain();
return sim.renderToRGB(width, height);
}
// Streaming thread function // Streaming thread function
void streamingThread(const std::string& mode) { void streamingThread() {
uint32_t frameCount = 0;
auto lastFrameTime = std::chrono::steady_clock::now(); auto lastFrameTime = std::chrono::steady_clock::now();
while (streaming) { while (streaming) {
auto startTime = std::chrono::steady_clock::now(); auto startTime = std::chrono::steady_clock::now();
std::vector<uint8_t> newFrame; // Only generate frames if there are active clients
if (activeClients > 0) {
newFrame = generateGradientFrame(512, 512); std::vector<uint8_t> frame;
{
std::lock_guard<std::mutex> lock(frameMutex); {
currentFrame = std::move(newFrame); std::lock_guard<std::mutex> lock(paramsMutex);
frameReady = true; if (currentParams.mode == "terrain") {
frame = generateTerrainFrame();
} else {
frame = generateGradientFrame();
}
}
// Add frame to queue
{
std::lock_guard<std::mutex> lock(queueMutex);
// Limit queue size to prevent memory issues
while (frameQueue.size() > 10) {
frameQueue.pop();
}
frameQueue.push(std::move(frame));
frameCounter++;
}
// Notify waiting clients
frameCondition.notify_all();
} }
frameCount++; // Control frame rate (30 FPS max)
// Limit frame rate to ~30 FPS
auto endTime = std::chrono::steady_clock::now(); auto endTime = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime); auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
auto targetFrameTime = std::chrono::milliseconds(33); // ~30 FPS auto targetFrameTime = std::chrono::milliseconds(33); // ~30 FPS
@@ -110,9 +161,34 @@ void streamingThread(const std::string& mode) {
} }
} }
// Get the latest frame (blocks until frame is available)
std::vector<uint8_t> getLatestFrame(int timeoutMs = 1000) {
std::unique_lock<std::mutex> lock(queueMutex);
if (frameCondition.wait_for(lock, std::chrono::milliseconds(timeoutMs),
[]{ return !frameQueue.empty(); })) {
auto frame = std::move(frameQueue.front());
// Keep only the latest frame for new clients
std::queue<std::vector<uint8_t>> empty;
std::swap(frameQueue, empty);
frameQueue.push(frame); // Keep one frame for the next client
return frame;
}
return {}; // Return empty frame on timeout
}
// Convert RGB data to JPEG (simplified - in real implementation use libjpeg)
std::vector<uint8_t> rgbToJpeg(const std::vector<uint8_t>& rgbData, int width, int height) {
TIME_FUNCTION;
// TODO: return mjpeg
return rgbData; // Return uncompressed RGB for now
}
// Add this function to get timing stats as JSON // Add this function to get timing stats as JSON
std::string getTimingStatsJSON() { std::string getTimingStatsJSON() {
auto stats = FunctionTimer::getStats(); auto stats = FunctionTimer::getStats();
std::stringstream json; std::stringstream json;
@@ -147,18 +223,16 @@ int main(int argc, char* argv[]) {
// Check command line arguments // Check command line arguments
int port = 8080; int port = 8080;
std::string webRoot = "web"; std::string webRoot = "web";
std::string mode = "gradient"; // Default mode std::string mode = "gradient";
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
std::string arg = argv[i]; std::string arg = argv[i];
if (arg == "--port" || arg == "-p") { if (arg == "--port" || arg == "-p") {
if (i + 1 < argc) { if (i + 1 < argc) port = std::stoi(argv[++i]);
port = std::stoi(argv[++i]);
}
} else if (arg == "--webroot" || arg == "-w") { } else if (arg == "--webroot" || arg == "-w") {
if (i + 1 < argc) { if (i + 1 < argc) webRoot = argv[++i];
webRoot = argv[++i]; } else if (arg == "-2d") {
} mode = "terrain";
} else if (arg == "-all") { } else if (arg == "-all") {
mode = "all"; mode = "all";
} else if (arg == "--help" || arg == "-h") { } else if (arg == "--help" || arg == "-h") {
@@ -166,56 +240,34 @@ int main(int argc, char* argv[]) {
std::cout << "Options:" << std::endl; std::cout << "Options:" << std::endl;
std::cout << " -p, --port PORT Set server port (default: 8080)" << std::endl; std::cout << " -p, --port PORT Set server port (default: 8080)" << std::endl;
std::cout << " -w, --webroot DIR Set web root directory (default: web)" << std::endl; std::cout << " -w, --webroot DIR Set web root directory (default: web)" << std::endl;
std::cout << " -2d Display 2D terrain simulation" << std::endl;
std::cout << " -all Allow switching between gradient and terrain" << std::endl; std::cout << " -all Allow switching between gradient and terrain" << std::endl;
std::cout << " -h, --help Show this help message" << std::endl; std::cout << " -h, --help Show this help message" << std::endl;
return 0; return 0;
} }
} }
// Generate initial frame // Set initial mode
std::cout << "Starting " << mode << " streaming..." << std::endl; {
std::lock_guard<std::mutex> lock(paramsMutex);
currentParams.mode = mode;
}
// Start streaming thread // Start streaming thread
streaming = true; streaming = true;
std::thread streamThread(streamingThread, mode); std::thread streamThread(streamingThread);
SimpleHTTPServer server(port, webRoot); SimpleHTTPServer server(port, webRoot);
// Add live stream endpoint // MJPEG stream endpoint
server.addRoute("/api/live-stream", [](const std::string& method, const std::string& body) {
if (method == "GET") {
int maxWait = 100;
while (!frameReady && maxWait-- > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (!frameReady) {
return std::make_pair(503, std::basic_string("{\"error\":\"No frame available\"}"));
}
std::vector<uint8_t> frameCopy;
{
std::lock_guard<std::mutex> lock(frameMutex);
frameCopy = currentFrame;
}
// Convert to base64 for JSON response
std::string base64Data = "data:image/jpeg;base64,";
// TODO
std::stringstream json;
json << "{\"frame_available\":true,\"width\":512,\"height\":512,\"data_size\":" << frameCopy.size() << "}";
return std::make_pair(200, json.str());
}
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
});
server.addRoute("/stream.mjpg", [](const std::string& method, const std::string& body) { server.addRoute("/stream.mjpg", [](const std::string& method, const std::string& body) {
if (method == "GET") { if (method == "GET") {
// TODO activeClients++;
// Set up MJPEG stream headers
std::string response = "HTTP/1.1 200 OK\r\n"; std::string response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n"; response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n";
response += "Cache-Control: no-cache\r\n";
response += "Connection: close\r\n"; response += "Connection: close\r\n";
response += "\r\n"; response += "\r\n";
@@ -223,32 +275,140 @@ int main(int argc, char* argv[]) {
} }
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}")); return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
}); });
// Single frame endpoint
server.addRoute("/api/frame", [](const std::string& method, const std::string& body) {
if (method == "GET") {
activeClients++;
auto frame = getLatestFrame();
if (!frame.empty()) {
// Convert to JPEG (in real implementation)
auto jpegData = rgbToJpeg(frame, 512, 512);
// For now, send as raw RGB with custom content type
std::string response(jpegData.begin(), jpegData.end());
activeClients--;
return std::make_pair(200, response);
}
activeClients--;
return std::make_pair(503, std::basic_string("No frame available"));
}
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
});
// Frame info endpoint
server.addRoute("/api/frame-info", [](const std::string& method, const std::string& body) {
if (method == "GET") {
std::stringstream json;
json << "{"
<< "\"frame_count\":" << frameCounter << ","
<< "\"active_clients\":" << activeClients << ","
<< "\"width\":512,"
<< "\"height\":512,"
<< "\"channels\":3"
<< "}";
return std::make_pair(200, json.str());
}
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
});
// Parameter setting endpoint
server.addRoute("/api/set-parameters", [](const std::string& method, const std::string& body) {
if (method == "POST") {
try {
SimulationParams newParams;
{
std::lock_guard<std::mutex> lock(paramsMutex);
newParams = currentParams;
}
// Parse parameters from JSON body
if (body.find("\"scale\"") != std::string::npos) {
size_t pos = body.find("\"scale\":") + 8;
newParams.scale = std::stof(body.substr(pos));
}
if (body.find("\"octaves\"") != std::string::npos) {
size_t pos = body.find("\"octaves\":") + 10;
newParams.octaves = std::stoi(body.substr(pos));
}
if (body.find("\"persistence\"") != std::string::npos) {
size_t pos = body.find("\"persistence\":") + 14;
newParams.persistence = std::stof(body.substr(pos));
}
if (body.find("\"lacunarity\"") != std::string::npos) {
size_t pos = body.find("\"lacunarity\":") + 13;
newParams.lacunarity = std::stof(body.substr(pos));
}
if (body.find("\"elevation\"") != std::string::npos) {
size_t pos = body.find("\"elevation\":") + 12;
newParams.elevation = std::stof(body.substr(pos));
}
if (body.find("\"waterLevel\"") != std::string::npos) {
size_t pos = body.find("\"waterLevel\":") + 13;
newParams.waterLevel = std::stof(body.substr(pos));
}
if (body.find("\"seed\"") != std::string::npos) {
size_t pos = body.find("\"seed\":") + 7;
newParams.seed = std::stoul(body.substr(pos));
}
{
std::lock_guard<std::mutex> lock(paramsMutex);
currentParams = newParams;
}
return std::make_pair(200, std::basic_string("{\"status\":\"success\"}"));
} catch (const std::exception& e) {
return std::make_pair(400, std::basic_string("{\"error\":\"Invalid parameters\"}"));
}
}
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
});
// Mode switching endpoint
server.addRoute("/api/switch-mode", [](const std::string& method, const std::string& body) {
if (method == "POST") {
std::lock_guard<std::mutex> lock(paramsMutex);
if (currentParams.mode == "gradient") {
currentParams.mode = "terrain";
} else {
currentParams.mode = "gradient";
}
std::string response = "{\"status\":\"success\", \"mode\":\"" + currentParams.mode + "\"}";
return std::make_pair(200, response);
}
return std::make_pair(405, std::basic_string("Method Not Allowed"));
});
// Current mode endpoint
server.addRoute("/api/current-mode", [](const std::string& method, const std::string& body) {
if (method == "GET") {
std::lock_guard<std::mutex> lock(paramsMutex);
std::string response = "{\"mode\":\"" + currentParams.mode + "\"}";
return std::make_pair(200, response);
}
return std::make_pair(405, std::basic_string("Method Not Allowed"));
});
// Add timing stats endpoint // Add timing stats endpoint
server.addRoute("/api/timing-stats", [](const std::string& method, const std::string& body) { server.addRoute("/api/timing-stats", [](const std::string& method, const std::string& body) {
if (method == "GET") { if (method == "GET") {
return std::make_pair(200, getTimingStatsJSON()); return std::make_pair(200, getTimingStatsJSON());
} }
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}")); return std::make_pair(405, std::basic_string("Method Not Allowed"));
}); });
// Add mode switching endpoint for -all mode // Add clear stats endpoint
if (mode == "all") { server.addRoute("/api/clear-stats", [](const std::string& method, const std::string& body) {
server.addRoute("/api/switch-mode", [](const std::string& method, const std::string& body) { if (method == "POST") {
if (method == "POST") { FunctionTimer::clearStats();
//TODO return std::make_pair(200, std::basic_string("{\"status\":\"success\"}"));
return std::make_pair(200, std::basic_string("{\"status\":\"success\", \"mode\":\"switched\"}")); }
} return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}")); });
});
server.addRoute("/api/current-mode", [](const std::string& method, const std::string& body) {
if (method == "GET") {
return std::make_pair(200, std::basic_string("{\"mode\":\"live\"}"));
}
return std::make_pair(405, std::basic_string("{\"error\":\"Method Not Allowed\"}"));
});
}
if (!server.start()) { if (!server.start()) {
std::cerr << "Failed to start server on port " << port << std::endl; std::cerr << "Failed to start server on port " << port << std::endl;
@@ -259,16 +419,11 @@ int main(int argc, char* argv[]) {
std::cout << "Server running on http://localhost:" << port << std::endl; std::cout << "Server running on http://localhost:" << port << std::endl;
std::cout << "Web root: " << webRoot << std::endl; std::cout << "Web root: " << webRoot << std::endl;
std::cout << "Mode: " << mode << std::endl; std::cout << "Mode: " << mode << " (Live Streaming)" << std::endl;
std::cout << "Live stream available at /api/live-stream" << std::endl; std::cout << "Live stream available at /stream.mjpg" << std::endl;
std::cout << "Single frames available at /api/frame" << std::endl;
std::cout << "Timing stats available at /api/timing-stats" << std::endl; std::cout << "Timing stats available at /api/timing-stats" << std::endl;
if (mode == "all") {
std::cout << "Mode switching available at /api/switch-mode" << std::endl;
}
std::cout << "Press Ctrl+C to stop the server" << std::endl;
server.handleRequests(); server.handleRequests();
// Cleanup // Cleanup

View File

@@ -44,6 +44,6 @@
</div> </div>
<script src="stream.js"></script> <script src="stream.js"></script>
<script src="script.js"></script> <!-- <script src="script.js"></script> -->
</body> </body>
</html> </html>

View File

@@ -9,6 +9,9 @@ const ctx = canvas.getContext('2d');
const fpsCounter = document.getElementById('fpsCounter'); const fpsCounter = document.getElementById('fpsCounter');
const frameCounter = document.getElementById('frameCounter'); const frameCounter = document.getElementById('frameCounter');
// Image data buffer for direct pixel manipulation
let imageData = ctx.createImageData(canvas.width, canvas.height);
function toggleStream() { function toggleStream() {
const button = document.getElementById('streamBtn'); const button = document.getElementById('streamBtn');
@@ -31,7 +34,7 @@ function startStream() {
lastFrameTime = performance.now(); lastFrameTime = performance.now();
// Start the stream loop // Start the stream loop
streamInterval = setInterval(fetchFrame, 1000 / 30); // Start with 30 FPS streamInterval = setInterval(fetchFrame, 1000 / 30);
} }
function stopStream() { function stopStream() {
@@ -53,14 +56,15 @@ async function fetchFrame() {
if (!isStreaming) return; if (!isStreaming) return;
try { try {
const response = await fetch('/api/live-stream'); const response = await fetch('/api/frame');
const data = await response.json(); if (response.ok) {
const arrayBuffer = await response.arrayBuffer();
if (data.frame_available) { const rgbData = new Uint8Array(arrayBuffer);
// In a real implementation, you'd decode the frame data and draw it
// For now, we'll simulate by generating a pattern based on time if (rgbData.length === 512 * 512 * 3) {
drawSimulatedFrame(); drawRGBFrame(rgbData);
updateFrameCounter(); updateFrameCounter();
}
} }
} catch (error) { } catch (error) {
console.error('Error fetching frame:', error); console.error('Error fetching frame:', error);
@@ -68,6 +72,20 @@ async function fetchFrame() {
} }
} }
function drawRGBFrame(rgbData) {
const data = imageData.data;
// Convert RGB to RGBA
for (let i = 0, j = 0; i < data.length; i += 4, j += 3) {
data[i] = rgbData[j]; // R
data[i + 1] = rgbData[j + 1]; // G
data[i + 2] = rgbData[j + 2]; // B
data[i + 3] = 255; // A
}
ctx.putImageData(imageData, 0, 0);
}
function updateFrameCounter() { function updateFrameCounter() {
frameCount++; frameCount++;
const now = performance.now(); const now = performance.now();