summaryrefslogtreecommitdiff
path: root/src/animate.cpp
blob: 5bae15eef1bd671c34ad681d998cb3b46c5f48bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

#include "animate.hpp"

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<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 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 = logl(cur.conductivity / fabs(cur.currents[i])) + n.thresholds[i];
  if (c > lv && avalanches.back().size() > 0) {
    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::vector<unsigned> component(boost::num_vertices(G));
  unsigned num = boost::connected_components(G, &component[0]);

  std::list<unsigned> crack = find_minimal_crack(G, n);
  unsigned crack_component = component[n.G.dual_edges[crack.front()].v[0]];

  std::vector<std::list<unsigned>> components(num);

  for (unsigned 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 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();
    }
  }
}