summaryrefslogtreecommitdiff
path: root/src/animate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/animate.cpp')
-rw-r--r--src/animate.cpp205
1 files changed, 205 insertions, 0 deletions
diff --git a/src/animate.cpp b/src/animate.cpp
new file mode 100644
index 0000000..8c2d12f
--- /dev/null
+++ b/src/animate.cpp
@@ -0,0 +1,205 @@
+
+#include "animate.hpp"
+
+animate::animate(unsigned int Lx, unsigned int Ly, unsigned int window_size, int argc, char *argv[]) : Lx(Lx), Ly(Ly), G(Lx * Ly) {
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+ glutInitWindowSize(window_size, (unsigned int )(window_size * ((double)Ly / (double)Lx)));
+ glutCreateWindow("wolff");
+ glClearColor(0.0,0.0,0.0,0.0);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluOrtho2D(-1.0, Lx + 3.0, -1.0 , Ly + 3.0);
+}
+
+void animate::pre_fracture(const network &n) {
+ lv = std::numeric_limits<long double>::lowest();
+ avalanches = {{}};
+ boost::remove_edge_if(trivial, G);
+
+ glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
+ glClear(GL_COLOR_BUFFER_BIT);
+ glBegin(GL_LINES);
+ glColor3f(0.0f, 0.0f, 0.0f);
+ for (unsigned int i = 0; i < n.G.edges.size(); i++) {
+ graph::coordinate r1 = n.G.vertices[n.G.edges[i].v[0]].r;
+ graph::coordinate r2 = n.G.vertices[n.G.edges[i].v[1]].r;
+
+ if (fabs(r1.x - r2.x) > Lx / 2) {
+ if (r1.x < Lx / 2) {
+ r1.x += Lx;
+ } else {
+ r2.x += Lx;
+ }
+ }
+ if (fabs(r1.y - r2.y) > Ly / 2) {
+ if (r1.y < Ly / 2) {
+ r1.y += Ly;
+ } else {
+ r2.y += Ly;
+ }
+ }
+
+ glVertex2d(r1.x, r1.y);
+ glVertex2d(r2.x, r2.y);
+ }
+ glEnd();
+ glFlush();
+}
+
+void animate::bond_broken(const network& net, const current_info& cur, unsigned int i) {
+ long double c = logl(cur.conductivity / fabs(cur.currents[i])) + net.thresholds[i];
+ if (c > lv && avalanches.back().size() > 0) {
+ lv = c;
+ avalanches.push_back({i});
+ } else {
+ avalanches.back().push_back(i);
+ }
+
+ boost::add_edge(net.G.dual_edges[i].v[0], net.G.dual_edges[i].v[1], {i}, G);
+
+ glBegin(GL_LINES); // Each set of 3 vertices form a triangle
+ glColor3f(1.0f, 1.0f, 1.0f); // Blue
+ graph::coordinate r1 = net.G.vertices[net.G.edges[i].v[0]].r;
+ graph::coordinate r2 = net.G.vertices[net.G.edges[i].v[1]].r;
+
+ if (fabs(r1.x - r2.x) > Lx / 2) {
+ if (r1.x < Lx / 2) {
+ r2.x -= Lx;
+ } else {
+ r2.x += Lx;
+ }
+ }
+ if (fabs(r1.y - r2.y) > Ly / 2) {
+ if (r1.y < Ly / 2) {
+ r2.y -= Ly;
+ } else {
+ r2.y += Ly;
+ }
+ }
+
+ glVertex2d(r1.x, r1.y);
+ glVertex2d(r2.x, r2.y);
+ glEnd();
+ glFlush();
+}
+
+void animate::post_fracture(network &n) {
+ std::vector<unsigned int> component(boost::num_vertices(G));
+ unsigned int num = boost::connected_components(G, &component[0]);
+
+ std::list<unsigned int> crack = find_minimal_crack(G, n);
+ unsigned int crack_component = component[n.G.dual_edges[crack.front()].v[0]];
+
+ std::vector<std::list<unsigned int>> components(num);
+
+ for (unsigned int i = 0; i < n.G.dual_vertices.size(); i++) {
+ components[component[i]].push_back(i);
+ }
+
+ std::default_random_engine gen;
+ std::uniform_real_distribution<double> dis(0.0,1.0);
+
+ char key;
+ while ((key = getchar()) != 'n') {
+ glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
+ glClear(GL_COLOR_BUFFER_BIT);
+ glBegin(GL_LINES);
+ glColor3f(0.0f, 0.0f, 0.0f);
+ for (unsigned int i = 0; i < n.G.edges.size(); i++) {
+ if (!n.fuses[i]) {
+ graph::coordinate r1 = n.G.vertices[n.G.edges[i].v[0]].r;
+ graph::coordinate r2 = n.G.vertices[n.G.edges[i].v[1]].r;
+
+ if (fabs(r1.x - r2.x) > Lx / 2) {
+ if (r1.x < Lx / 2) {
+ r1.x += Lx;
+ } else {
+ r2.x += Lx;
+ }
+ }
+ if (fabs(r1.y - r2.y) > Ly / 2) {
+ if (r1.y < Ly / 2) {
+ r1.y += Ly;
+ } else {
+ r2.y += Ly;
+ }
+ }
+
+ glVertex2d(r1.x, r1.y);
+ glVertex2d(r2.x, r2.y);
+ }
+ }
+ glEnd();
+
+ switch (key) {
+ case 's' :
+ for (auto edge : crack) {
+ glBegin(GL_TRIANGLES);
+ glColor3f(1.0f, 0.0f, 0.0f);
+ glVertex2d(n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon[0].x, n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon[0].y);
+ glVertex2d(n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon[1].x, n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon[1].y);
+ glVertex2d(n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon[2].x, n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon[2].y);
+
+ glVertex2d(n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon[0].x, n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon[0].y);
+ glVertex2d(n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon[1].x, n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon[1].y);
+ glVertex2d(n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon[2].x, n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon[2].y);
+ glEnd();
+ }
+ glFlush();
+ break;
+
+ case 'c' :
+ glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
+ for (unsigned int i = 0; i < num; i++) {
+ if (i == crack_component) {
+ glColor3d(1.0, 0.0, 0.0);
+ } else {
+ glColor3d(dis(gen), dis(gen), dis(gen));
+ }
+
+ for (auto it = components[i].begin(); it != components[i].end(); it++) {
+ glVertex2d(n.G.dual_vertices[*it].polygon[0].x, n.G.dual_vertices[*it].polygon[0].y);
+ glVertex2d(n.G.dual_vertices[*it].polygon[1].x, n.G.dual_vertices[*it].polygon[1].y);
+ glVertex2d(n.G.dual_vertices[*it].polygon[2].x, n.G.dual_vertices[*it].polygon[2].y);
+ }
+ }
+ glEnd();
+ glFlush();
+ break;
+
+ case 'C' :
+ glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
+ for (unsigned int i = 0; i < num; i++) {
+ if (components[i].size() > 1) {
+ if (i == crack_component) {
+ glColor3d(1.0, 0.0, 0.0);
+ } else {
+ glColor3d(dis(gen), dis(gen), dis(gen));
+ }
+
+ for (auto it = components[i].begin(); it != components[i].end(); it++) {
+ glVertex2d(n.G.dual_vertices[*it].polygon[0].x, n.G.dual_vertices[*it].polygon[0].y);
+ glVertex2d(n.G.dual_vertices[*it].polygon[1].x, n.G.dual_vertices[*it].polygon[1].y);
+ glVertex2d(n.G.dual_vertices[*it].polygon[2].x, n.G.dual_vertices[*it].polygon[2].y);
+ }
+ }
+ }
+ glEnd();
+ glFlush();
+ break;
+
+ case 'm' :
+ glBegin(GL_TRIANGLES);
+ glColor3d(1.0, 0.0, 0.0);
+ for (auto it = components[crack_component].begin(); it != components[crack_component].end(); it++) {
+ glVertex2d(n.G.dual_vertices[*it].polygon[0].x, n.G.dual_vertices[*it].polygon[0].y);
+ glVertex2d(n.G.dual_vertices[*it].polygon[1].x, n.G.dual_vertices[*it].polygon[1].y);
+ glVertex2d(n.G.dual_vertices[*it].polygon[2].x, n.G.dual_vertices[*it].polygon[2].y);
+ }
+ glEnd();
+ glFlush();
+ }
+ }
+}
+