A lot of shit you don't want

This commit is contained in:
Joshua Coles 2023-02-17 15:38:16 +00:00
parent 496c1022db
commit 0ae7892998
9 changed files with 4219 additions and 275 deletions

View File

@ -4,28 +4,35 @@
#include "DLASystem.h"
// colors
namespace colours {
GLfloat blue[] = {0.1, 0.3, 0.9, 1.0}; // blue
GLfloat red[] = {1.0, 0.2, 0.1, 0.2}; // red
GLfloat green[] = {0.3, 0.6, 0.3, 1.0}; // green
GLfloat paleGrey[] = {0.7, 0.7, 0.7, 1.0}; // green
GLfloat darkGrey[] = {0.2, 0.2, 0.2, 1.0}; // green
}
#include <utility>
// this function gets called every step,
// if there is an active particle then it gets moved,
// if not then add a particle
void DLASystem::Update() {
if (lastParticleIsActive == 1)
bool DLASystem::Update() {
if (lastParticleIsActive == 1) {
moveLastParticle();
else if (numParticles < endNum) {
} else if (numParticles < endNum) {
addParticleOnAddCircle();
setParticleActive();
} else {
return false;
}
if (lastParticleIsActive == 0 || slowNotFast == 1)
glutPostRedisplay(); //Tell GLUT that the display has changed
if (!lastParticleIsActive) {
for (int i = 0; i < numParticles; ++i) {
this->csv_out << this->particleList[i]->pos[0] << "," << this->particleList[i]->pos[1];
if (i != numParticles - 1) {
this->csv_out << ",";
}
}
this->csv_out << "\n";
this->csv_out.flush();
}
return true;
}
@ -88,7 +95,7 @@ int DLASystem::checkStop() {
if (killCircle + 2 >= gridSize / 2) {
pauseRunning();
cout << "STOP" << endl;
glutPostRedisplay(); // update display
// glutPostRedisplay(); // update display
return 1;
} else return 0;
}
@ -239,18 +246,20 @@ int DLASystem::checkStick() {
// constructor
DLASystem::DLASystem(Window *set_win) {
DLASystem::DLASystem(ofstream out_file) {
cout << "creating system, gridSize " << gridSize << endl;
win = set_win;
numParticles = 0;
endNum = 1000;
csv_out = std::move(out_file);
// allocate memory for the grid, remember to free the memory in destructor
grid = new int *[gridSize];
for (int i = 0; i < gridSize; i++) {
grid[i] = new int[gridSize];
}
slowNotFast = 1;
slowNotFast = 0;
running = true;
// reset initial parameters
Reset();
@ -272,45 +281,7 @@ DLASystem::~DLASystem() {
delete[] grid[i];
delete[] grid;
if (logfile.is_open())
logfile.close();
}
// this draws the system
void DLASystem::DrawSquares() {
// draw the particles
double halfSize = 0.5;
for (int p = 0; p < numParticles; p++) {
double *vec = particleList[p]->pos;
glPushMatrix();
if (p == numParticles - 1 && lastParticleIsActive == 1)
glColor4fv(colours::red);
else if (p == 0)
glColor4fv(colours::green);
else
glColor4fv(colours::blue);
glRectd(drawScale * (vec[0] - halfSize),
drawScale * (vec[1] - halfSize),
drawScale * (vec[0] + halfSize),
drawScale * (vec[1] + halfSize));
glPopMatrix();
if (this->csv_out.is_open()) {
this->csv_out.close();
}
// print some information (at top left)
// this ostringstream is a way to create a string with numbers and words (similar to cout << ... )
ostringstream str;
str << "num " << numParticles << " size " << clusterRadius;
// print the string
win->displayString(str, -0.9, 0.9, colours::red);
// if we are paused then print this (at bottom left)
if (running == 0) {
ostringstream pauseStr;
pauseStr << "paused";
win->displayString(pauseStr, -0.9, -0.9, colours::red);
}
}

View File

@ -1,6 +1,5 @@
#pragma once
#include <GLUT/glut.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
@ -13,7 +12,6 @@
#include <string>
#include <sstream>
#include "Window.h"
#include "Particle.h"
#include "rnd.h"
@ -21,11 +19,7 @@ using namespace std;
class DLASystem {
private:
// these are private variables and functions that the user will not see
Window *win; // window in which the system is running
public:
// list of particles
vector<Particle *> particleList;
int numParticles;
@ -39,6 +33,8 @@ private:
double addCircle;
double killCircle;
ofstream csv_out;
// size of grid
static const int gridSize = 1600;
int **grid; // this will be a 2d array that stores whether each site is occupied
@ -67,7 +63,7 @@ public:
// update the system: if there is an active particle then move it,
// else create a new particle (on the adding circle)
void Update();
bool Update();
// draw particles as squares
void DrawSquares();
@ -82,7 +78,7 @@ public:
int lastParticleIsActive;
// constructor
DLASystem(Window *set_win);
DLASystem(ofstream output);
// destructor
~DLASystem();
@ -158,11 +154,4 @@ public:
// check whether the last particle should stick
// currently it sticks whenever it touches another particle
int checkStick();
// set the background colour for the window
// it would be better for an OOP philosophy to make these member functions for the Window class
// but we are being a bit lazy here
void setWinBackgroundWhite() { glClearColor(1.0, 1.0, 1.0, 1.0); }
void setWinBackgroundBlack() { glClearColor(0.0, 0.0, 0.0, 0.0); }
};

View File

@ -10,7 +10,7 @@
CXX = clang++
CXXFLAGS = -Wall -Wextra -g -O0
CXXFLAGS = -Wall -Wextra -g -O0 -std=c++20 -stdlib=libc++
IFLAGS = -I/usr/local/include -I/usr/include

View File

@ -1,27 +0,0 @@
#include "Window.h"
// constructor
Window::Window(int set_size[], string &set_title) {
size[0] = set_size[0];
size[1] = set_size[1];
title = set_title;
locateOnScreen();
glutInitWindowSize(size[0], size[1]);
glutInitWindowPosition(pos[0], pos[1]);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow(title.c_str());
// sets the background to black
glClearColor(0.0, 0.0, 0.0, 0.0);
}
// print a string at a given position, don't worry about how this works...
void Window::displayString(ostringstream &str, double x, double y, GLfloat col[]) {
string localString = str.str();
glColor4fv(col);
glRasterPos2d(x, y);
for (int i = 0; i < localString.length(); i++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, localString[i]);
}
}

View File

@ -1,34 +0,0 @@
#pragma once
#include <GLUT/glut.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Window {
public:
string title;
int size[2];
int pos[2];
void locateOnScreen() {
// the fx sets where on the screen the window will appear
// (values should be between 0 and 1)
double fx[] = {0.7, 0.5};
pos[0] = (glutGet(GLUT_SCREEN_WIDTH) - size[0]) * fx[0];
pos[1] = (glutGet(GLUT_SCREEN_HEIGHT) - size[1]) * fx[1];
}
// constructor, size is in pixels
Window(int set_size[], string &set_title);
// function which prints a string to the screen, at a given position, with a given color
// note position is "absolute", not easy to get two strings spaced one above each other like this
void displayString(ostringstream &str, double x, double y, GLfloat col[]);
};

2087
example.csv Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,12 +1,7 @@
#include <GLUT/glut.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string>
#include "DLASystem.h"
#include "Window.h"
using namespace std;
@ -25,148 +20,24 @@ namespace drawFuncs {
DLASystem *sys;
int main(int argc, char **argv) {
// turn on glut
glutInit(&argc, argv);
int window_size[] = {480, 480};
string window_title("simple DLA simulation");
// create a window
Window *win = new Window(window_size, window_title);
const auto seed = std::stoi(argv[1]);
char buffer[20];
snprintf(buffer, 20, "seed-%i.csv", seed);
const auto p1 = std::chrono::system_clock::now();
std::ofstream myfile;
myfile.open(buffer);
// create the system
sys = new DLASystem(win);
sys = new DLASystem(std::move(myfile));
// this is the seed for the random numbers
int seed = 6;
cout << "setting seed " << seed << endl;
sys->setSeed(seed);
// print the "help" message to the console
drawFuncs::introMessage();
while (sys->Update()) {
}
// tell openGL how to redraw the screen and respond to the keyboard
glutDisplayFunc(drawFuncs::display);
glutKeyboardFunc(drawFuncs::handleKeypress);
// tell openGL to do its first update after waiting 10ms
int wait = 10;
int val = 0;
glutTimerFunc(wait, drawFuncs::update, val);
// start the openGL stuff
glutMainLoop();
cout << sys->numParticles;
return 0;
}
// this is just a help message
void drawFuncs::introMessage() {
cout << "Keys (while in graphics window):" << endl << " q or e to quit (or exit)" << endl;
cout << " h to print this message (help)" << endl;
cout << " u for a single update" << endl;
cout << " g to start running (go)" << endl;
cout << " p to pause running" << endl;
cout << " s to run in slow-mode" << endl;
cout << " f to run in fast-mode" << endl;
cout << " r to clear everything (reset)" << endl;
cout << " z to pause and zoom in" << endl;
cout << " w or b to change background colour to white or black" << endl;
}
// openGL function deals with the keyboard
void drawFuncs::handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 'h':
drawFuncs::introMessage();
break;
case 'q':
case 'e':
cout << "Exiting..." << endl;
// delete the system
delete sys;
exit(0);
break;
case 'p':
cout << "pause" << endl;
sys->pauseRunning();
break;
case 'g':
cout << "go" << endl;
sys->setRunning();
glutTimerFunc(0, drawFuncs::update, 0);
break;
case 's':
cout << "slow" << endl;
sys->setSlow();
break;
case 'w':
cout << "white" << endl;
sys->setWinBackgroundWhite();
break;
case 'b':
cout << "black" << endl;
sys->setWinBackgroundBlack();
break;
case 'f':
cout << "fast" << endl;
sys->setFast();
break;
case 'r':
cout << "reset" << endl;
sys->Reset();
break;
case 'z':
cout << "zoom" << endl;
sys->pauseRunning();
sys->viewAddCircle();
break;
case 'u':
cout << "upd" << endl;
sys->Update();
break;
}
// tell openGL to redraw the window
glutPostRedisplay();
}
// this function gets called whenever the algorithm should do its update
void drawFuncs::update(int val) {
int wait; // time to wait between updates (milliseconds)
if (sys->running) {
if (sys->slowNotFast == 1)
wait = 10;
else
wait = 0;
sys->Update();
// tell openGL to call this funtion again after "wait" milliseconds
glutTimerFunc(wait, drawFuncs::update, 0);
}
}
// this function redraws the window when necessary
void drawFuncs::display() {
// Clear the window or more specifically the frame buffer...
// This happens by replacing all the contents of the frame
// buffer by the clear color (black in our case)
glClear(GL_COLOR_BUFFER_BIT);
// this puts the camera at the origin (not sure why) with (I think) z axis out of page and y axis up
// there is also the question of the GL perspective which is not set up in any clear way at the moment
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 1.0, /* camera position */
0.0, 0.0, -1.0, /* point to look at */
0.0, 1.0, 0.0); /* up direction */
//sys->DrawSpheres();
sys->DrawSquares();
// Swap contents of backward and forward frame buffers
glutSwapBuffers();
}

BIN
run Executable file

Binary file not shown.

2087
seed-2.csv Normal file

File diff suppressed because one or more lines are too long