Compare commits
37
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc4d870e74 | ||
|
|
6e16f18b1a | ||
|
|
096b9e7d29 | ||
|
|
7594b1929a | ||
|
|
04963ebdf0 | ||
|
|
9ff002b19c | ||
|
|
92ef0f728c | ||
|
|
973bcf0381 | ||
|
|
f0e862616d | ||
|
|
1be8e3bd01 | ||
|
|
7cfa5092e8 | ||
|
|
1c49bf92c8 | ||
|
|
14b5cb4679 | ||
|
|
f7ac576129 | ||
|
|
cd1f20c1d2 | ||
|
|
0d972dc76a | ||
|
|
7e131af96d | ||
|
|
42b87c387c | ||
|
|
b88b93db59 | ||
|
|
de88205379 | ||
|
|
b0edfec7f6 | ||
|
|
55364779e0 | ||
|
|
593eff5d1b | ||
|
|
9640a32674 | ||
|
|
ac6e176801 | ||
|
|
0a56c6f83f | ||
|
|
da53077f9a | ||
|
|
51ad848046 | ||
|
|
f5853580f4 | ||
|
|
fcda9726d5 | ||
|
|
0752370ee7 | ||
|
|
639be88ce1 | ||
|
|
924df55e18 | ||
|
|
4f96be8fa6 | ||
|
|
95e328ea1f | ||
|
|
64783b7946 | ||
|
|
a8641690e6 |
@@ -0,0 +1,9 @@
|
|||||||
|
kind: pipeline
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: rust:1.67
|
||||||
|
commands:
|
||||||
|
- cargo build --verbose --all
|
||||||
|
- cargo test --verbose --all
|
||||||
+1
-6
@@ -1,8 +1,3 @@
|
|||||||
# Final executable
|
/target
|
||||||
/run
|
|
||||||
|
|
||||||
# IDE files
|
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
# Data files
|
|
||||||
*.csv
|
*.csv
|
||||||
|
|||||||
Generated
+3683
File diff suppressed because it is too large
Load Diff
+43
@@ -0,0 +1,43 @@
|
|||||||
|
[package]
|
||||||
|
name = "rust-codebase"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "dla"
|
||||||
|
crate-type = ["staticlib"]
|
||||||
|
path = "src/clib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "model"
|
||||||
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "ui"
|
||||||
|
path = "src/ui.rs"
|
||||||
|
|
||||||
|
# Set the default for crate.
|
||||||
|
[profile.dev]
|
||||||
|
opt-level = 1
|
||||||
|
|
||||||
|
# Set the default for crate.
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|
||||||
|
# Set the default for dependencies.
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = 3
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { version = "4.1.8", features = ["derive"] }
|
||||||
|
bevy = { version = "0.9.1" }
|
||||||
|
nd_array = "0.1.0"
|
||||||
|
num-integer = "0.1.45"
|
||||||
|
rand = { version = "0.8.5", features = ["default", "small_rng"] }
|
||||||
|
csv = "1.1"
|
||||||
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
cbindgen = "0.24.3"
|
||||||
-319
@@ -1,319 +0,0 @@
|
|||||||
//
|
|
||||||
// DLASystem.cpp
|
|
||||||
//
|
|
||||||
|
|
||||||
#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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
moveLastParticle();
|
|
||||||
else if (numParticles < endNum) {
|
|
||||||
addParticleOnAddCircle();
|
|
||||||
setParticleActive();
|
|
||||||
}
|
|
||||||
if (lastParticleIsActive == 0 || slowNotFast == 1)
|
|
||||||
glutPostRedisplay(); //Tell GLUT that the display has changed
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void DLASystem::clearParticles() {
|
|
||||||
// delete particles and the particle list
|
|
||||||
for (int i = 0; i < numParticles; i++) {
|
|
||||||
delete particleList[i];
|
|
||||||
}
|
|
||||||
particleList.clear();
|
|
||||||
numParticles = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove any existing particles and setup initial condition
|
|
||||||
void DLASystem::Reset() {
|
|
||||||
// stop running
|
|
||||||
running = 0;
|
|
||||||
|
|
||||||
clearParticles();
|
|
||||||
|
|
||||||
lastParticleIsActive = 0;
|
|
||||||
|
|
||||||
// set the grid to zero
|
|
||||||
for (int i = 0; i < gridSize; i++) {
|
|
||||||
for (int j = 0; j < gridSize; j++) {
|
|
||||||
grid[i][j] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// setup initial condition and parameters
|
|
||||||
addCircle = 10;
|
|
||||||
killCircle = 2.0*addCircle;
|
|
||||||
clusterRadius = 0.0;
|
|
||||||
// add a single particle at the origin
|
|
||||||
double pos[] = { 0.0, 0.0 };
|
|
||||||
addParticle(pos);
|
|
||||||
|
|
||||||
// set the view
|
|
||||||
int InitialViewSize = 40;
|
|
||||||
setViewSize(InitialViewSize);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the value of a grid cell for a particular position
|
|
||||||
// note the position has the initial particle at (0,0)
|
|
||||||
// but this corresponds to the middle of the grid array ie grid[ halfGrid ][ halfGrid ]
|
|
||||||
void DLASystem::setGrid(double pos[], int val) {
|
|
||||||
int halfGrid = gridSize / 2;
|
|
||||||
grid[(int)(pos[0] + halfGrid)][(int)(pos[1] + halfGrid)] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
// read the grid cell for a given position
|
|
||||||
int DLASystem::readGrid(double pos[]) {
|
|
||||||
int halfGrid = gridSize / 2;
|
|
||||||
return grid[(int)(pos[0] + halfGrid)][(int)(pos[1] + halfGrid)];
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the cluster is big enough and we should stop:
|
|
||||||
// to be safe, we need the killCircle to be at least 2 less than the edge of the grid
|
|
||||||
int DLASystem::checkStop() {
|
|
||||||
if (killCircle + 2 >= gridSize / 2) {
|
|
||||||
pauseRunning();
|
|
||||||
cout << "STOP" << endl;
|
|
||||||
glutPostRedisplay(); // update display
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add a particle to the system at a specific position
|
|
||||||
void DLASystem::addParticle(double pos[]) {
|
|
||||||
// create a new particle
|
|
||||||
Particle * p = new Particle(pos);
|
|
||||||
// push_back means "add this to the end of the list"
|
|
||||||
particleList.push_back(p);
|
|
||||||
numParticles++;
|
|
||||||
|
|
||||||
// pos coordinates should be -gridSize/2 < x < gridSize/2
|
|
||||||
setGrid(pos, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add a particle to the system at a random position on the addCircle
|
|
||||||
// if we hit an occupied site then we do nothing except print a message
|
|
||||||
// (this should never happen)
|
|
||||||
void DLASystem::addParticleOnAddCircle() {
|
|
||||||
double pos[2];
|
|
||||||
double theta = rgen.random01() * 2 * M_PI;
|
|
||||||
pos[0] = ceil(addCircle * cos(theta));
|
|
||||||
pos[1] = ceil(addCircle * sin(theta));
|
|
||||||
if (readGrid(pos) == 0)
|
|
||||||
addParticle(pos);
|
|
||||||
else
|
|
||||||
cout << "FAIL " << pos[0] << " " << pos[1] << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// send back the position of a neighbour of a given grid cell
|
|
||||||
// NOTE: there is no check that the neighbour is inside the grid,
|
|
||||||
// this has to be done separately...
|
|
||||||
void DLASystem::setPosNeighbour(double setpos[], double pos[], int val) {
|
|
||||||
switch (val) {
|
|
||||||
case 0:
|
|
||||||
setpos[0] = pos[0] + 1.0;
|
|
||||||
setpos[1] = pos[1];
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
setpos[0] = pos[0] - 1.0;
|
|
||||||
setpos[1] = pos[1];
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
setpos[0] = pos[0];
|
|
||||||
setpos[1] = pos[1] + 1.0;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
setpos[0] = pos[0];
|
|
||||||
setpos[1] = pos[1] - 1.0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the view is smaller than the kill circle then increase the view area (zoom out)
|
|
||||||
void DLASystem::updateViewSize() {
|
|
||||||
double mult = 1.2;
|
|
||||||
if (viewSize < 2.0*killCircle) {
|
|
||||||
setViewSize(viewSize * mult);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the view to be the size of the add circle (ie zoom in on the cluster)
|
|
||||||
void DLASystem::viewAddCircle() {
|
|
||||||
setViewSize(2.0*addCircle); // factor of 2 is to go from radius to diameter
|
|
||||||
}
|
|
||||||
|
|
||||||
// when we add a particle to the cluster, we should update the cluster radius
|
|
||||||
// and the sizes of the addCircle and the killCircle
|
|
||||||
void DLASystem::updateClusterRadius(double pos[]) {
|
|
||||||
|
|
||||||
double rr = distanceFromOrigin(pos);
|
|
||||||
if (rr > clusterRadius) {
|
|
||||||
clusterRadius = rr;
|
|
||||||
// this is how big addCircle is supposed to be:
|
|
||||||
// either 20% more than cluster radius, or at least 5 bigger.
|
|
||||||
double check = clusterRadius * addRatio;
|
|
||||||
if (check < clusterRadius + 5)
|
|
||||||
check = clusterRadius + 5;
|
|
||||||
// if it is smaller then update everything...
|
|
||||||
if (addCircle < check) {
|
|
||||||
addCircle = check;
|
|
||||||
killCircle = killRatio * addCircle;
|
|
||||||
updateViewSize();
|
|
||||||
}
|
|
||||||
checkStop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make a random move of the last particle in the particleList
|
|
||||||
void DLASystem::moveLastParticle() {
|
|
||||||
int rr = rgen.randomInt(4); // pick a random number in the range 0-3, which direction do we hop?
|
|
||||||
double newpos[2];
|
|
||||||
|
|
||||||
Particle *lastP = particleList[numParticles - 1];
|
|
||||||
|
|
||||||
setPosNeighbour(newpos, lastP->pos, rr);
|
|
||||||
|
|
||||||
if (distanceFromOrigin(newpos) > killCircle) {
|
|
||||||
//cout << "#deleting particle" << endl;
|
|
||||||
setGrid(lastP->pos, 0);
|
|
||||||
particleList.pop_back(); // remove particle from particleList
|
|
||||||
numParticles--;
|
|
||||||
setParticleInactive();
|
|
||||||
}
|
|
||||||
// check if destination is empty
|
|
||||||
else if (readGrid(newpos) == 0) {
|
|
||||||
setGrid(lastP->pos, 0); // set the old grid site to empty
|
|
||||||
// update the position
|
|
||||||
particleList[numParticles - 1]->pos[0] = newpos[0];
|
|
||||||
particleList[numParticles - 1]->pos[1] = newpos[1];
|
|
||||||
setGrid(lastP->pos, 1); // set the new grid site to be occupied
|
|
||||||
|
|
||||||
// check if we stick
|
|
||||||
if (checkStick()) {
|
|
||||||
//cout << "stick" << endl;
|
|
||||||
setParticleInactive(); // make the particle inactive (stuck)
|
|
||||||
updateClusterRadius(lastP->pos); // update the cluster radius, addCircle, etc.
|
|
||||||
|
|
||||||
if (numParticles % 100 == 0 && logfile.is_open()) {
|
|
||||||
logfile << numParticles << " " << clusterRadius << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// if we get to here then we are trying to move to an occupied site
|
|
||||||
// (this should never happen as long as the sticking probability is 1.0)
|
|
||||||
cout << "reject " << rr << endl;
|
|
||||||
cout << lastP->pos[0] << " " << lastP->pos[1] << endl;
|
|
||||||
//cout << newpos[0] << " " << newpos[1] << " " << (int)newpos[0] << endl;
|
|
||||||
//printOccupied();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the last particle should stick (to a neighbour)
|
|
||||||
int DLASystem::checkStick() {
|
|
||||||
Particle *lastP = particleList[numParticles - 1];
|
|
||||||
int result = 0;
|
|
||||||
// loop over neighbours
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
double checkpos[2];
|
|
||||||
setPosNeighbour(checkpos, lastP->pos, i);
|
|
||||||
// if the neighbour is occupied...
|
|
||||||
if (readGrid(checkpos) == 1)
|
|
||||||
result = 1;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// constructor
|
|
||||||
DLASystem::DLASystem(Window *set_win) {
|
|
||||||
cout << "creating system, gridSize " << gridSize << endl;
|
|
||||||
win = set_win;
|
|
||||||
numParticles = 0;
|
|
||||||
endNum = 1000;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
// reset initial parameters
|
|
||||||
Reset();
|
|
||||||
|
|
||||||
addRatio = 1.2; // how much bigger the addCircle should be, compared to cluster radius
|
|
||||||
killRatio = 1.7; // how much bigger is the killCircle, compared to the addCircle
|
|
||||||
|
|
||||||
// this opens a logfile, if we want to...
|
|
||||||
//logfile.open("opfile.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
// destructor
|
|
||||||
DLASystem::~DLASystem() {
|
|
||||||
// strictly we should not print inside the destructor but never mind...
|
|
||||||
cout << "deleting system" << endl;
|
|
||||||
// delete the particles
|
|
||||||
clearParticles();
|
|
||||||
// delete the grid
|
|
||||||
for (int i = 0; i < gridSize; i++)
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-154
@@ -1,154 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <GLUT/glut.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <vector>
|
|
||||||
#define _USE_MATH_DEFINES
|
|
||||||
#include <math.h>
|
|
||||||
#include <random>
|
|
||||||
#include <string>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "Window.h"
|
|
||||||
#include "Particle.h"
|
|
||||||
#include "rnd.h"
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// list of particles
|
|
||||||
vector<Particle*> particleList;
|
|
||||||
int numParticles;
|
|
||||||
|
|
||||||
// delete particles and clear the particle list
|
|
||||||
void clearParticles();
|
|
||||||
|
|
||||||
// size of cluster
|
|
||||||
double clusterRadius;
|
|
||||||
// these are related to the DLA algorithm
|
|
||||||
double addCircle;
|
|
||||||
double killCircle;
|
|
||||||
|
|
||||||
// size of grid
|
|
||||||
static const int gridSize = 1600;
|
|
||||||
int **grid; // this will be a 2d array that stores whether each site is occupied
|
|
||||||
|
|
||||||
// the window draws only part of the grid, viewSize controls how much...
|
|
||||||
double viewSize;
|
|
||||||
double drawScale;
|
|
||||||
|
|
||||||
// random number generator, class name is rnd, instance is rgen
|
|
||||||
rnd rgen;
|
|
||||||
|
|
||||||
// output file (not used at the moment)
|
|
||||||
ofstream logfile;
|
|
||||||
|
|
||||||
// number of particles at which the simulation will stop
|
|
||||||
// (the value is set in constructor)
|
|
||||||
int endNum;
|
|
||||||
|
|
||||||
// the values of these variables are set in the constructor
|
|
||||||
double addRatio; // how much bigger the addCircle should be, compared to cluster radius
|
|
||||||
double killRatio; // how much bigger is the killCircle, compared to the addCircle
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
|
||||||
// these are public variables and functions
|
|
||||||
|
|
||||||
// update the system: if there is an active particle then move it,
|
|
||||||
// else create a new particle (on the adding circle)
|
|
||||||
void Update();
|
|
||||||
|
|
||||||
// draw particles as squares
|
|
||||||
void DrawSquares();
|
|
||||||
|
|
||||||
// is the simulation running (1) or paused (0) ?
|
|
||||||
int running;
|
|
||||||
|
|
||||||
// slowNotFast is +1 for slow running, 0 for fast
|
|
||||||
int slowNotFast;
|
|
||||||
|
|
||||||
// lastParticleIsActive is +1 if there is an active particle in the system, otherwise 0
|
|
||||||
int lastParticleIsActive;
|
|
||||||
|
|
||||||
// constructor
|
|
||||||
DLASystem(Window *set_win);
|
|
||||||
// destructor
|
|
||||||
~DLASystem();
|
|
||||||
|
|
||||||
// delete all particles and reset
|
|
||||||
void Reset();
|
|
||||||
|
|
||||||
// this sets the seed for the random numbers
|
|
||||||
void setSeed(int s) { rgen.setSeed(s); }
|
|
||||||
|
|
||||||
// check whether we should stop (eg the cluster has reached the edge of the grid)
|
|
||||||
int checkStop();
|
|
||||||
|
|
||||||
// stop/start the algorithm
|
|
||||||
void setRunning() { if ( checkStop()==0 ) running = 1; }
|
|
||||||
void pauseRunning() { running = 0; }
|
|
||||||
// set whether it runs fast or slow
|
|
||||||
void setSlow() { slowNotFast = 1; }
|
|
||||||
void setFast() { slowNotFast = 0; }
|
|
||||||
void setSuperFast() { slowNotFast = -1; }
|
|
||||||
|
|
||||||
// set which part of the grid is visible on the screen
|
|
||||||
// basically the screen shows co-ordinates -vv < x < vv
|
|
||||||
// where vv is the input value
|
|
||||||
void setViewSize(double vv) { viewSize = vv; drawScale = 2.0/viewSize; }
|
|
||||||
|
|
||||||
// if the killcircle is almost as big as the view then increase the view
|
|
||||||
void updateViewSize();
|
|
||||||
|
|
||||||
// set the view to be the approx size of the addCircle
|
|
||||||
void viewAddCircle();
|
|
||||||
|
|
||||||
// if pos is outside the cluster radius then set clusterRadius to be the distance to pos.
|
|
||||||
void updateClusterRadius( double pos[] );
|
|
||||||
|
|
||||||
// set and read grid entries associated with a given position
|
|
||||||
void setGrid(double pos[], int val);
|
|
||||||
int readGrid(double pos[]);
|
|
||||||
|
|
||||||
// return the distance of a given point from the origin
|
|
||||||
double distanceFromOrigin(double pos[]) {
|
|
||||||
return sqrt( pos[0]*pos[0] + pos[1]*pos[1] );
|
|
||||||
}
|
|
||||||
|
|
||||||
// set whether there is an active particle in the system or not
|
|
||||||
void setParticleActive() { lastParticleIsActive = 1; }
|
|
||||||
void setParticleInactive() { lastParticleIsActive = 0; }
|
|
||||||
|
|
||||||
// add a particle at pos
|
|
||||||
void addParticle(double pos[]);
|
|
||||||
// add a particle at a random point on the addCircle
|
|
||||||
void addParticleOnAddCircle();
|
|
||||||
|
|
||||||
// assign setpos to the position of a neighbour of pos
|
|
||||||
// which neighbour we look at is determined by val (=0,1,2,3)
|
|
||||||
void setPosNeighbour(double setpos[], double pos[], int val);
|
|
||||||
|
|
||||||
// this attempts to move the last particle in the List to a random neighbour
|
|
||||||
// if the neighbour is occupied then nothing happens
|
|
||||||
// the function also checks if the moving particle should stick.
|
|
||||||
void moveLastParticle();
|
|
||||||
|
|
||||||
// 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); }
|
|
||||||
};
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
# ====================================================================================== #
|
|
||||||
# From the Author #
|
|
||||||
# ====================================================================================== #
|
|
||||||
# ! The purpose of this Makefile is to build the DLASystem project
|
|
||||||
# ! This makefile was adapted to work with any cpp project on OSX
|
|
||||||
|
|
||||||
# ====================================================================================== #
|
|
||||||
# Variables of the Makefile #
|
|
||||||
# ====================================================================================== #
|
|
||||||
|
|
||||||
CXX = clang++
|
|
||||||
|
|
||||||
CXXFLAGS = -Wall -Wextra -g -O0
|
|
||||||
|
|
||||||
IFLAGS = -I/usr/local/include -I/usr/include
|
|
||||||
|
|
||||||
LFLAGS = -L/usr/local/lib -lm -framework OpenGL -framework GLUT
|
|
||||||
|
|
||||||
# ------------------------------------------
|
|
||||||
# FOR GENERIC MAKEFILE:
|
|
||||||
# 1 - Binary directory
|
|
||||||
# 2 - Source directory
|
|
||||||
# 3 - Executable name
|
|
||||||
# 4 - Sources names
|
|
||||||
# 5 - Dependencies names
|
|
||||||
# ------------------------------------------
|
|
||||||
BIN = .
|
|
||||||
SOURCE = .
|
|
||||||
EXEC = ./run
|
|
||||||
SOURCES = $(wildcard $(SOURCE)/*.cpp)
|
|
||||||
OBJECTS = $(SOURCES:.cpp=.o)
|
|
||||||
|
|
||||||
# ====================================================================================== #
|
|
||||||
# Targets of the Makefile #
|
|
||||||
# target_name : dependency #
|
|
||||||
# <tabulation> command #
|
|
||||||
# ====================================================================================== #
|
|
||||||
|
|
||||||
# ------------------------------------------
|
|
||||||
# ! - all : Compiles everything
|
|
||||||
# ! - help : Shows this help
|
|
||||||
# ! - clean : erases all object files *.o
|
|
||||||
# ! and all binary executables
|
|
||||||
# ------------------------------------------
|
|
||||||
all : $(BIN)/run
|
|
||||||
|
|
||||||
test: $(BIN)/hllc_test
|
|
||||||
|
|
||||||
help :
|
|
||||||
@grep -E "^# !" Makefile | sed -e 's/# !/ /g'
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f $(EXEC) $(OBJECTS)
|
|
||||||
|
|
||||||
# ------------------------------------------
|
|
||||||
# Executable
|
|
||||||
# ------------------------------------------
|
|
||||||
$(EXEC): $(OBJECTS)
|
|
||||||
$(CXX) $(OBJECTS) -o $(EXEC) $(IFLAGS) $(LFLAGS)
|
|
||||||
|
|
||||||
# ------------------------------------------
|
|
||||||
# Temorary files (*.o) (IFLAGS should be added here)
|
|
||||||
# ------------------------------------------
|
|
||||||
$(SOURCE)/%.o: $(SOURCE)/%.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c $< -o $@ $(IFLAGS) $(LFLAGS)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-20
@@ -1,20 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class Particle {
|
|
||||||
public:
|
|
||||||
static const int dim = 2; // we are in two dimensions
|
|
||||||
double *pos; // pointer to an array of size dim, to store the position
|
|
||||||
|
|
||||||
// default constructor
|
|
||||||
Particle() {
|
|
||||||
pos = new double[dim];
|
|
||||||
}
|
|
||||||
// constructor, with a specified initial position
|
|
||||||
Particle(double set_pos[]) {
|
|
||||||
pos = new double[dim];
|
|
||||||
for (int d=0;d<dim;d++)
|
|
||||||
pos[d]=set_pos[d];
|
|
||||||
}
|
|
||||||
// destructor
|
|
||||||
~Particle() { delete[] pos; }
|
|
||||||
};
|
|
||||||
-26
@@ -1,26 +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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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[]);
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
extern crate cbindgen;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::path::Path;
|
||||||
|
use cbindgen::{Config, Builder};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let crate_env = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||||
|
let crate_path = Path::new(&crate_env);
|
||||||
|
let config = Config::from_root_or_default(crate_path);
|
||||||
|
Builder::new().with_crate(crate_path.to_str().unwrap())
|
||||||
|
.with_config(config)
|
||||||
|
.generate()
|
||||||
|
.expect("Cannot generate header file!")
|
||||||
|
.write_to_file("libdla.h");
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <cstdarg>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ostream>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
struct CStorage;
|
||||||
|
|
||||||
|
struct CPosition {
|
||||||
|
int32_t _0;
|
||||||
|
int32_t _1;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
CStorage *storage_new(uint32_t grid_size);
|
||||||
|
|
||||||
|
bool storage_at(const CStorage *storage, int32_t i, int32_t j);
|
||||||
|
|
||||||
|
void storage_deposit(CStorage *storage, int32_t i, int32_t j, uint8_t val);
|
||||||
|
|
||||||
|
CPosition walk(uint32_t d, int32_t i, int32_t j);
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
-169
@@ -1,169 +0,0 @@
|
|||||||
#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;
|
|
||||||
|
|
||||||
// functions which are needed for openGL go into a namespace so that we can identify them
|
|
||||||
namespace drawFuncs {
|
|
||||||
void handleKeypress(unsigned char key, int x, int y);
|
|
||||||
void display(void);
|
|
||||||
void update(int val);
|
|
||||||
void introMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is a global pointer, which is how we access the system itself
|
|
||||||
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);
|
|
||||||
|
|
||||||
// create the system
|
|
||||||
sys = new DLASystem(win);
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
-27
@@ -1,27 +0,0 @@
|
|||||||
To run the code on a Mac through the command line:
|
|
||||||
|
|
||||||
1. Download Makefile from this section
|
|
||||||
|
|
||||||
2. Put Makefile in same directory as DLASystem.cpp (! Remove the .txt extension if present !)
|
|
||||||
|
|
||||||
3. Relative to MS Visual Studio code, these files include the following changes: DLASystem.h Window.h mainDLA.cpp
|
|
||||||
|
|
||||||
change #include <gl/freeglut.h>
|
|
||||||
|
|
||||||
to #include <GLUT/glut.h>
|
|
||||||
|
|
||||||
4. Open the terminal in the same folder as these files
|
|
||||||
|
|
||||||
5. Type "make" to build project
|
|
||||||
|
|
||||||
6. Run the program by typing "./run"
|
|
||||||
|
|
||||||
7. To edit your files, run gedit, save the code, and recompile with "make -B" before running the program again.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Thanks to Eliot Ayache for figuring this out!
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PS For Coursework 2: In addition, there needs to be a change in Line 72 of MAIN_Source.cpp file from "glutLeaveMainLoop();" to "exit(1);"
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <random>
|
|
||||||
|
|
||||||
// ... don't worry how this all works
|
|
||||||
// ... member functions that you may want to use:
|
|
||||||
// random01() returns a random double between 0 and 1
|
|
||||||
// randomInt(max) returns a random int between 0 and max-1 (inclusive)
|
|
||||||
|
|
||||||
class rnd {
|
|
||||||
private:
|
|
||||||
// nuts and bolts.. should not need to touch this.
|
|
||||||
std::default_random_engine generator;
|
|
||||||
int genMax;
|
|
||||||
std::uniform_int_distribution<int> *intmax;
|
|
||||||
std::uniform_real_distribution<double> *real01;
|
|
||||||
|
|
||||||
public:
|
|
||||||
// constructor
|
|
||||||
rnd() {
|
|
||||||
genMax = 0x7fffffff;
|
|
||||||
//cout << "genMax is " << generator.max() << endl;
|
|
||||||
intmax = new std::uniform_int_distribution<int>(0, genMax);
|
|
||||||
real01 = new std::uniform_real_distribution<double>(0.0, 1.0);
|
|
||||||
}
|
|
||||||
// destructor
|
|
||||||
~rnd() { delete intmax; delete real01; }
|
|
||||||
|
|
||||||
// set the random seed
|
|
||||||
void setSeed(int seed) { generator.seed(seed); }
|
|
||||||
// member functions for generating random double in [0,1] and random integer in [0,max-1]
|
|
||||||
double random01() { return (*real01)(generator); }
|
|
||||||
int randomInt(int max) { return (*intmax)(generator) % max; }
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
||||||
+75
@@ -0,0 +1,75 @@
|
|||||||
|
#![feature(array_zip)]
|
||||||
|
|
||||||
|
use system::Storage;
|
||||||
|
use crate::system::grid::{Position, VectorStorage};
|
||||||
|
|
||||||
|
mod system;
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct CPosition(i32, i32);
|
||||||
|
|
||||||
|
pub struct CStorage(VectorStorage);
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn storage_new(grid_size: u32) -> &'static mut CStorage {
|
||||||
|
let mut pntr = Box::new(CStorage(VectorStorage::new(grid_size)));
|
||||||
|
Box::leak(pntr)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn storage_at(storage: &CStorage, i: i32, j: i32) -> bool {
|
||||||
|
storage.0.at(&Position { x: i, y: j })
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn storage_deposit(storage: &mut CStorage, i: i32, j: i32, val: u8) {
|
||||||
|
storage.0.write(&Position { x: i, y: j }, val == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn walk(d: u32, i: i32, j: i32) -> CPosition {
|
||||||
|
return test::b(d, i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test {
|
||||||
|
use num_integer::Integer;
|
||||||
|
use crate::CPosition;
|
||||||
|
use crate::system::grid::Position;
|
||||||
|
|
||||||
|
pub(crate) fn a(d: u32, i: i32, j: i32) -> CPosition {
|
||||||
|
match d {
|
||||||
|
0 => CPosition(i + 1, j),
|
||||||
|
1 => CPosition(i - 1, j),
|
||||||
|
2 => CPosition(i, j + 1),
|
||||||
|
3 => CPosition(i, j - 1),
|
||||||
|
_ => panic!("Ahh"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn b(d: u32, i: i32, j: i32) -> CPosition {
|
||||||
|
let (dim, sign) = d.div_rem(&2);
|
||||||
|
let sign = if sign == 0 { 1 } else { -1 };
|
||||||
|
// HACK: Our conventin and the MVA are different, since we are trying to strangle fig this, quick hack.
|
||||||
|
let offset = Position::in_direction(dim, sign);
|
||||||
|
let next = Position { x: i, y: j } + offset;
|
||||||
|
|
||||||
|
CPosition(next.x, next.y)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
let d = [0, 1, 2, 3];
|
||||||
|
d.iter()
|
||||||
|
.map(|d| d.div_rem(&2))
|
||||||
|
.for_each(|p| println!("{p:?}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alignment() {
|
||||||
|
let d = [0, 1, 2, 3];
|
||||||
|
d.iter()
|
||||||
|
.map(|d| (a(*d, 0, 0), b(*d, 0, 0)))
|
||||||
|
.for_each(|p| assert_eq!(p.0, p.1));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
use rand::rngs::SmallRng;
|
||||||
|
use rand::SeedableRng;
|
||||||
|
use crate::system::grid::{Position, VectorStorage};
|
||||||
|
use crate::system::model::DLASystem;
|
||||||
|
use crate::system::nd::{NDPosition, NDVectorStorage};
|
||||||
|
use crate::system::walker::LocalRandomWalker;
|
||||||
|
|
||||||
|
pub fn initial_config(seed: u64, max_particles: usize) -> DLASystem<SmallRng, Position, VectorStorage, LocalRandomWalker> {
|
||||||
|
DLASystem::new_g(
|
||||||
|
SmallRng::seed_from_u64(seed),
|
||||||
|
VectorStorage::new(1600),
|
||||||
|
LocalRandomWalker,
|
||||||
|
1.0,
|
||||||
|
max_particles,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stick_probability(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, Position, VectorStorage, LocalRandomWalker> {
|
||||||
|
DLASystem::new_g(
|
||||||
|
SmallRng::seed_from_u64(seed),
|
||||||
|
VectorStorage::new(1600),
|
||||||
|
LocalRandomWalker,
|
||||||
|
stick_probability,
|
||||||
|
max_particles,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn three_dimensional(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, NDPosition<3>, NDVectorStorage<3>, LocalRandomWalker> {
|
||||||
|
DLASystem::new_g(
|
||||||
|
SmallRng::seed_from_u64(seed),
|
||||||
|
NDVectorStorage::new(1600),
|
||||||
|
LocalRandomWalker,
|
||||||
|
stick_probability,
|
||||||
|
max_particles,
|
||||||
|
)
|
||||||
|
}
|
||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
#![feature(array_zip)]
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
mod system;
|
||||||
|
mod example_systems;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
|
use crate::example_systems::stick_probability;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
struct Cli {
|
||||||
|
seed: u64,
|
||||||
|
max_particles: usize,
|
||||||
|
stick_probability: f32,
|
||||||
|
csv_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
println!("Running: {:?}", cli);
|
||||||
|
|
||||||
|
let mut sys = stick_probability(
|
||||||
|
cli.seed,
|
||||||
|
cli.max_particles,
|
||||||
|
cli.stick_probability
|
||||||
|
);
|
||||||
|
|
||||||
|
while sys.running {
|
||||||
|
sys.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
sys.export_data(&cli.csv_path)
|
||||||
|
.expect("Failed to write");
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
use std::f32::consts::PI;
|
||||||
|
use std::ops::Add;
|
||||||
|
use num_integer::Integer;
|
||||||
|
use rand::Rng;
|
||||||
|
use crate::system::{GriddedPosition, Storage};
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
pub struct VectorStorage {
|
||||||
|
backing: Vec<bool>,
|
||||||
|
grid_size: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct Position {
|
||||||
|
pub x: i32,
|
||||||
|
pub y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Position {
|
||||||
|
pub fn in_direction(direction: u32, value: i32) -> Self {
|
||||||
|
if direction == 0 { Position { x: value, y: 0 } } else { Position { x: 0, y: value } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for Position {
|
||||||
|
type Output = Position;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
Position { x: self.x + rhs.x, y: self.y + rhs.y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GriddedPosition for Position {
|
||||||
|
const NEIGHBOURS: u32 = 4;
|
||||||
|
|
||||||
|
fn zero() -> Position {
|
||||||
|
Position { x: 0, y: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
||||||
|
let (x, y) = (radius * theta.cos(), radius * theta.sin());
|
||||||
|
Position { x: x as i32, y: y as i32 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
((self.x.pow(2) + self.y.pow(2)) as f32).powf(0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
|
let (dim, sign) = neighbour_index.div_rem(&2);
|
||||||
|
let sign = if sign == 0 { 1 } else { -1 };
|
||||||
|
let offset = Position::in_direction(dim, sign);
|
||||||
|
|
||||||
|
self.clone() + offset
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
|
let grid_size = grid_size as i32;
|
||||||
|
|
||||||
|
assert!(self.x <= grid_size && -(grid_size) <= self.x);
|
||||||
|
assert!(self.y <= grid_size && -(grid_size) <= self.y);
|
||||||
|
|
||||||
|
let x = (self.x + (grid_size) / 2) as usize;
|
||||||
|
let y = (self.y + (grid_size) / 2) as usize;
|
||||||
|
|
||||||
|
return grid_size as usize * y + x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VectorStorage {
|
||||||
|
pub fn new(grid_size: u32) -> VectorStorage {
|
||||||
|
VectorStorage { grid_size, backing: vec![false; grid_size.pow(2) as usize] }
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convenience function for c-binding
|
||||||
|
* */
|
||||||
|
pub fn write(&mut self, position: &Position, val: bool) {
|
||||||
|
self.backing[position.linear_index(self.grid_size)] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Storage<Position> for VectorStorage {
|
||||||
|
fn at(&self, position: &Position) -> bool {
|
||||||
|
return self.backing[position.linear_index(self.grid_size)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deposit(&mut self, position: &Position) {
|
||||||
|
self.backing[position.linear_index(self.grid_size)] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
use bevy::render::color::HexColorError::Hex;
|
||||||
|
use rand::Rng;
|
||||||
|
use crate::system::GriddedPosition;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct HexPosition {
|
||||||
|
pub q: i32,
|
||||||
|
pub r: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for HexPosition {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
HexPosition { q: self.q + rhs.q, r: self.r + rhs.r }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GriddedPosition for HexPosition {
|
||||||
|
const NEIGHBOURS: u32 = 6;
|
||||||
|
|
||||||
|
fn zero() -> Self {
|
||||||
|
HexPosition { q: 0, r: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
((self.q.pow(2) + self.r.pow(2) + self.q * self.r) as f32).sqrt()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
|
let neighbour_index = neighbour_index as usize;
|
||||||
|
|
||||||
|
const OFFSETS: [(i32, i32); 6] = [
|
||||||
|
(1, 0), (1, -1), (0, -1),
|
||||||
|
(-1, 0), (-1, 1), (0, 1),
|
||||||
|
];
|
||||||
|
|
||||||
|
self.clone() + HexPosition { q: OFFSETS[neighbour_index].0, r: OFFSETS[neighbour_index].0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
|
todo!()
|
||||||
|
// ((self.q + grid_size / 2) + grid_size * self.r) as usize
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
use rand::Rng;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
pub mod walker;
|
||||||
|
pub mod grid;
|
||||||
|
pub mod model;
|
||||||
|
pub mod nd;
|
||||||
|
mod hexagonal;
|
||||||
|
|
||||||
|
pub trait GriddedPosition: Add<Output=Self> + Serialize + Clone {
|
||||||
|
const NEIGHBOURS: u32;
|
||||||
|
|
||||||
|
fn zero() -> Self;
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self;
|
||||||
|
fn abs(&self) -> f32;
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self;
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Storage<P: GriddedPosition> {
|
||||||
|
fn at(&self, position: &P) -> bool;
|
||||||
|
fn deposit(&mut self, position: &P);
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::path::Path;
|
||||||
|
use rand::prelude::*;
|
||||||
|
use crate::system::{GriddedPosition, Storage};
|
||||||
|
use crate::system::walker::Walker;
|
||||||
|
|
||||||
|
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
|
||||||
|
rng: R,
|
||||||
|
space: S,
|
||||||
|
walker: W,
|
||||||
|
|
||||||
|
stick_probability: f32,
|
||||||
|
max_particles: usize,
|
||||||
|
|
||||||
|
pub running: bool,
|
||||||
|
|
||||||
|
particles: Vec<P>,
|
||||||
|
active_particle: Option<P>,
|
||||||
|
|
||||||
|
add_ratio: f32,
|
||||||
|
add_circle: f32,
|
||||||
|
kill_ratio: f32,
|
||||||
|
kill_circle: f32,
|
||||||
|
cluster_radius: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> DLASystem<R, P, S, W> {
|
||||||
|
pub fn new_g(rng: R, space: S, walker: W, stick_probability: f32, max_particles: usize) -> Self {
|
||||||
|
let mut sys = DLASystem {
|
||||||
|
rng,
|
||||||
|
stick_probability,
|
||||||
|
max_particles,
|
||||||
|
running: true,
|
||||||
|
|
||||||
|
space,
|
||||||
|
walker,
|
||||||
|
particles: vec![],
|
||||||
|
active_particle: None,
|
||||||
|
|
||||||
|
add_ratio: 1.2,
|
||||||
|
kill_ratio: 1.7,
|
||||||
|
|
||||||
|
add_circle: 10.0,
|
||||||
|
kill_circle: 20.0,
|
||||||
|
cluster_radius: 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
sys.deposit(&P::zero());
|
||||||
|
|
||||||
|
sys
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
if self.active_particle.is_some() {
|
||||||
|
self.move_particle();
|
||||||
|
} else if self.particles.len() < self.max_particles {
|
||||||
|
self.spawn_particle();
|
||||||
|
} else {
|
||||||
|
self.running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_particle(&mut self) {
|
||||||
|
let current_position = &self
|
||||||
|
.active_particle
|
||||||
|
.clone()
|
||||||
|
.expect("No active particle");
|
||||||
|
|
||||||
|
let next_position = self.walker.walk(&mut self.rng, current_position);
|
||||||
|
let distance = next_position.abs();
|
||||||
|
|
||||||
|
if distance > self.kill_circle {
|
||||||
|
self.active_particle = None;
|
||||||
|
} else if !self.space.at(&next_position) {
|
||||||
|
if self.check_stick(&next_position) {
|
||||||
|
self.deposit(&next_position);
|
||||||
|
self.active_particle = None;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
self.active_particle.replace(next_position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_stick(&mut self, position: &P) -> bool {
|
||||||
|
(0..P::NEIGHBOURS)
|
||||||
|
.map(|n| position.neighbour(n))
|
||||||
|
.any(|neighbour|
|
||||||
|
self.space.at(&neighbour)
|
||||||
|
&& self.rng.gen_range(0.0f32..=1.0) < self.stick_probability
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_particle(&mut self) {
|
||||||
|
let position = P::spawn(&mut self.rng, self.add_circle);
|
||||||
|
|
||||||
|
if !self.space.at(&position) {
|
||||||
|
self.active_particle = Some(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deposit(&mut self, p0: &P) {
|
||||||
|
self.particles.push(p0.clone());
|
||||||
|
self.space.deposit(p0);
|
||||||
|
|
||||||
|
let distance = p0.abs();
|
||||||
|
if distance > self.cluster_radius {
|
||||||
|
self.cluster_radius = distance;
|
||||||
|
|
||||||
|
let new_add_circle = (self.cluster_radius * self.add_ratio).max(self.cluster_radius + 5.0);
|
||||||
|
if self.add_circle < new_add_circle {
|
||||||
|
self.add_circle = new_add_circle;
|
||||||
|
self.kill_circle = self.kill_ratio * self.add_circle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn export_data(&self, path: &Path) -> io::Result<()> {
|
||||||
|
let mut wtr = csv::Writer::from_path(path)?;
|
||||||
|
|
||||||
|
for particle in &self.particles {
|
||||||
|
wtr.serialize(particle)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
wtr.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
use num_integer::Integer;
|
||||||
|
use rand::Rng;
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
|
use serde::ser::SerializeMap;
|
||||||
|
use crate::system::GriddedPosition;
|
||||||
|
use crate::system::Storage;
|
||||||
|
|
||||||
|
pub struct NDVectorStorage<const DIM: usize> {
|
||||||
|
backing: Vec<bool>,
|
||||||
|
grid_size: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> NDVectorStorage<DIM> {
|
||||||
|
pub fn new(grid_size: u32) -> Self {
|
||||||
|
Self { grid_size, backing: vec![false; grid_size.pow(DIM as u32) as usize] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> Storage<NDPosition<DIM>> for NDVectorStorage<DIM> {
|
||||||
|
fn at(&self, position: &NDPosition<DIM>) -> bool {
|
||||||
|
return self.backing[position.linear_index(self.grid_size)];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deposit(&mut self, position: &NDPosition<DIM>) {
|
||||||
|
self.backing[position.linear_index(self.grid_size)] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct NDPosition<const DIM: usize>([i32; DIM]);
|
||||||
|
|
||||||
|
impl<const DIM: usize> NDPosition<DIM> {
|
||||||
|
pub fn in_direction(direction: usize, value: i32) -> Self {
|
||||||
|
let mut arr = [0; DIM];
|
||||||
|
arr[direction] = value;
|
||||||
|
NDPosition(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> Add for NDPosition<DIM> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
Self(self.0.zip(rhs.0).map(|(a, b)| a + b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> Serialize for NDPosition<DIM> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||||
|
let mut map = serializer.serialize_map(Some(DIM))?;
|
||||||
|
for (i, v) in self.0.iter().enumerate() {
|
||||||
|
map.serialize_entry(&format!("r{}", i), v)?;
|
||||||
|
}
|
||||||
|
map.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> GriddedPosition for NDPosition<DIM> {
|
||||||
|
const NEIGHBOURS: u32 = { 2u32.pow(DIM as u32) } as u32;
|
||||||
|
|
||||||
|
fn zero() -> Self {
|
||||||
|
NDPosition([0; DIM])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
let mut a: [f32; DIM] = [0f32; DIM];
|
||||||
|
let mut b: [i32; DIM] = [0i32; DIM];
|
||||||
|
|
||||||
|
for i in 0..DIM {
|
||||||
|
a[i] = rng.gen_range(0f32..1f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
let norm = a.iter().sum::<f32>()
|
||||||
|
.sqrt();
|
||||||
|
|
||||||
|
for i in 0..DIM {
|
||||||
|
a[i] = a[i] * radius / norm;
|
||||||
|
b[i] = a[i] as i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Self(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
let a: i32 = self.0.iter()
|
||||||
|
.map(|r| r.pow(2))
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
(a as f32).powf(0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
|
let (dim, sign) = neighbour_index.div_rem(&(DIM as u32));
|
||||||
|
let sign = if sign == 0 { 1 } else { -1 };
|
||||||
|
let offset = Self::in_direction(dim as usize, sign);
|
||||||
|
|
||||||
|
self.clone() + offset
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
|
self.0.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, v)| (grid_size.pow(i as u32) as usize) * (v + ((grid_size / 2) as i32)) as usize)
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
use rand::prelude::Rng;
|
||||||
|
use crate::system::GriddedPosition;
|
||||||
|
|
||||||
|
pub trait Walker<P: GriddedPosition> {
|
||||||
|
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LocalRandomWalker;
|
||||||
|
|
||||||
|
impl<Position: GriddedPosition> Walker<Position> for LocalRandomWalker {
|
||||||
|
fn walk<R: Rng>(&self, rng: &mut R, position: &Position) -> Position {
|
||||||
|
position.neighbour(rng.gen_range(0u32..Position::NEIGHBOURS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test {
|
||||||
|
use rand::rngs::SmallRng;
|
||||||
|
use rand::{SeedableRng, thread_rng};
|
||||||
|
use crate::system::{GriddedPosition, grid::Position};
|
||||||
|
use crate::system::walker::{LocalRandomWalker, Walker};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn uniformity() {
|
||||||
|
let walker = LocalRandomWalker;
|
||||||
|
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
||||||
|
let mut results: Vec<Position> = vec![];
|
||||||
|
|
||||||
|
let origin = &Position::zero();
|
||||||
|
|
||||||
|
let x: u32 = (1_000_000);
|
||||||
|
for i in 0..x {
|
||||||
|
results.push(walker.walk(&mut rng, origin));
|
||||||
|
}
|
||||||
|
|
||||||
|
let a = results
|
||||||
|
.iter()
|
||||||
|
.filter(|a| **a == Position { x: 0, y: 1 })
|
||||||
|
.count();
|
||||||
|
|
||||||
|
let b = results
|
||||||
|
.iter()
|
||||||
|
.filter(|a| **a == Position { x: 0, y: -1 })
|
||||||
|
.count();
|
||||||
|
|
||||||
|
let c = results
|
||||||
|
.iter()
|
||||||
|
.filter(|a| **a == Position { x: 1, y: 0 })
|
||||||
|
.count();
|
||||||
|
|
||||||
|
let d = results
|
||||||
|
.iter()
|
||||||
|
.filter(|a| **a == Position { x: -1, y: 0 })
|
||||||
|
.count();
|
||||||
|
|
||||||
|
println!("{} {} {} {}", a as f32 / x as f32, b as f32 / x as f32, c as f32 / x as f32, d as f32 / x as f32);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
#![feature(array_zip)]
|
||||||
|
|
||||||
|
use std::default::Default;
|
||||||
|
use std::error::Error;
|
||||||
|
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
||||||
|
use csv::{Position, Reader, ReaderBuilder};
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
#[derive(Parser, Resource)] // requires `derive` feature
|
||||||
|
enum UICli {
|
||||||
|
CSV(CSVArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(clap::Args)]
|
||||||
|
struct CSVArgs {
|
||||||
|
path: std::path::PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Position2D(i32, i32);
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let cli = UICli::parse();
|
||||||
|
|
||||||
|
App::new()
|
||||||
|
.insert_resource(cli)
|
||||||
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||||
|
window: WindowDescriptor {
|
||||||
|
title: "DLA Static 2D Render".to_string(),
|
||||||
|
width: 800.,
|
||||||
|
height: 800.,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
..default()
|
||||||
|
}))
|
||||||
|
.add_startup_system(setup_ui)
|
||||||
|
.add_startup_system(read_csv)
|
||||||
|
.run();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_csv(
|
||||||
|
cli: Res<UICli>,
|
||||||
|
mut commands: Commands
|
||||||
|
) {
|
||||||
|
let csv_path = match &cli.into_inner() {
|
||||||
|
UICli::CSV(CSVArgs { path }) => path,
|
||||||
|
_ => panic!("Ahh"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut reader = ReaderBuilder::new()
|
||||||
|
.has_headers(true)
|
||||||
|
.from_path(csv_path)
|
||||||
|
.expect("Failed to read csv");
|
||||||
|
|
||||||
|
let headers = reader.headers()
|
||||||
|
.expect("Failed to read headers");
|
||||||
|
|
||||||
|
let x_column = headers.iter().position(|name| name.trim() == "x")
|
||||||
|
.expect("Failed to find x column");
|
||||||
|
|
||||||
|
let y_column = headers.iter().position(|name| name.trim() == "y")
|
||||||
|
.expect("Failed to find x column");
|
||||||
|
|
||||||
|
let positions = reader
|
||||||
|
.records()
|
||||||
|
.map(|record| {
|
||||||
|
let record = record.expect("Failed to read position");
|
||||||
|
let x: i32 = record[x_column].trim().parse::<i32>().expect("Failed to read x");
|
||||||
|
let y: i32 = record[y_column].trim().parse::<i32>().expect("Failed to read y");
|
||||||
|
|
||||||
|
Position2D(x, y)
|
||||||
|
});
|
||||||
|
|
||||||
|
for Position2D(x, y) in positions {
|
||||||
|
let rect_size = 5.0;
|
||||||
|
|
||||||
|
commands.spawn(SpriteBundle {
|
||||||
|
sprite: Sprite {
|
||||||
|
color: Color::rgb(0.25, 0.25, 0.75),
|
||||||
|
custom_size: Some(Vec2::new(rect_size, rect_size)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
transform: Transform::from_translation(Vec3::new((x as f32) * rect_size, (y as f32) * rect_size, 0.)),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_ui(
|
||||||
|
mut commands: Commands,
|
||||||
|
) {
|
||||||
|
commands.spawn(Camera2dBundle::default());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user