#include "animate.hpp" #include animate::animate(double Lx, double Ly, unsigned window_size, int argc, char *argv[]) : G(2 * (unsigned)ceil(Lx * Ly / 2)) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize((unsigned)(Lx / Ly * window_size), window_size); glutCreateWindow("wolff"); glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, Lx + 3, -1 , Ly + 3); } void animate::pre_fracture(const network &n) { lv = std::numeric_limits::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 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 (n.G.edges[i].crossings[0]) { if (r1.x < r2.x) { r1.x += n.G.L.x; } else { r2.x += n.G.L.x; } } if (n.G.edges[i].crossings[1]) { if (r1.y < r2.y) { r1.y += n.G.L.y; } else { r2.y += n.G.L.y; } } glVertex2d(r1.x, r1.y); glVertex2d(r2.x, r2.y); } glEnd(); glFlush(); } void animate::bond_broken(const network& n, const current_info& cur, unsigned i) { long double c = n.thresholds[i] - logl(fabs(cur.currents[i])); if (c > lv) { lv = c; avalanches.push_back({i}); } else { avalanches.back().push_back(i); } boost::add_edge(n.G.dual_edges[i].v[0], n.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 = n.G.vertices[n.G.edges[i].v[0]].r; graph::coordinate r2 = n.G.vertices[n.G.edges[i].v[1]].r; if (n.G.edges[i].crossings[0]) { if (r1.x < r2.x) { r1.x += n.G.L.x; } else { r2.x += n.G.L.x; } } if (n.G.edges[i].crossings[1]) { if (r1.y < r2.y) { r1.y += n.G.L.y; } else { r2.y += n.G.L.y; } } glVertex2d(r1.x, r1.y); glVertex2d(r2.x, r2.y); glEnd(); glFlush(); } void animate::post_fracture(network &n) { std::list crack; // unsigned crack_component = component[n.G.dual_edges[crack.front()].v[0]]; unsigned crack_component = 10000; std::default_random_engine gen; std::uniform_real_distribution dis(0.0,1.0); auto av_it = avalanches.rbegin(); while (true) { for (unsigned e : *av_it) { boost::remove_edge(n.G.dual_edges[e].v[0], n.G.dual_edges[e].v[1], G); n.fuses[e] = false; } auto cracks = find_minimal_crack(G, n); if (cracks.size() == 0) { break; } av_it++; } std::vector component(boost::num_vertices(G)); unsigned num = boost::connected_components(G, &component[0]); std::vector> components(num); for (unsigned i = 0; i < n.G.dual_vertices.size(); i++) { components[component[i]].push_back(i); } 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 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 (n.G.edges[i].crossings[0]) { if (r1.x < r2.x) { r1.x += n.G.L.x; } else { r2.x += n.G.L.x; } } if (n.G.edges[i].crossings[1]) { if (r1.y < r2.y) { r1.y += n.G.L.y; } else { r2.y += n.G.L.y; } } glVertex2d(r1.x, r1.y); glVertex2d(r2.x, r2.y); } } glEnd(); switch (key) { case 's' : for (auto edge : crack) { glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_POLYGON); for (const graph::coordinate &r : n.G.dual_vertices[n.G.dual_edges[edge].v[0]].polygon) { glVertex2d(r.x, r.y); } glEnd(); glBegin(GL_POLYGON); for (const graph::coordinate &r : n.G.dual_vertices[n.G.dual_edges[edge].v[1]].polygon) { glVertex2d(r.x, r.y); } glEnd(); } glFlush(); break; case 'c' : for (unsigned 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++) { glBegin(GL_POLYGON); // Each set of 3 vertices form a triangle for (const graph::coordinate &r: n.G.dual_vertices[*it].polygon) { glVertex2d(r.x, r.y); } glEnd(); } } glFlush(); break; case 'C' : for (unsigned 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++) { glBegin(GL_POLYGON); // Each set of 3 vertices form a triangle for (const graph::coordinate &r :n.G.dual_vertices[*it].polygon) { glVertex2d(r.x, r.y); } glEnd(); } } } glFlush(); break; case 'm' : for (auto it = components[crack_component].begin(); it != components[crack_component].end(); it++) { glBegin(GL_POLYGON); glColor3d(1.0, 0.0, 0.0); for (const graph::coordinate &r :n.G.dual_vertices[*it].polygon) { glVertex2d(r.x, r.y); } glEnd(); } glFlush(); } } }