summaryrefslogtreecommitdiff
path: root/animation.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'animation.hpp')
-rw-r--r--animation.hpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/animation.hpp b/animation.hpp
new file mode 100644
index 0000000..857354e
--- /dev/null
+++ b/animation.hpp
@@ -0,0 +1,111 @@
+
+#include "space_wolff.hpp"
+#include <GL/glut.h>
+
+template <typename T, int D>
+void drawSphere(const Vector<T, D>& x, Radius r, std::array<double, 3> color = {0, 0, 0},
+ unsigned nPoints = 20) {
+ glColor3d(color[0], color[1], color[2]);
+ glBegin(GL_POLYGON);
+ for (unsigned i = 0; i < nPoints; i++) {
+ glVertex2d(x(0) + r * cos(2 * i * M_PI / nPoints), x(1) + r * sin(2 * i * M_PI / nPoints));
+ }
+ glEnd();
+}
+
+template <typename T, int D>
+void draw(const Spin<T, D, Radius>& s, std::array<double, 3> color = {0, 0, 0}) {
+ drawSphere<T, D>(s.x, s.s, color);
+}
+
+template <typename T, int D>
+void draw(const Spin<T, D, Dimer<T, D>>& s, std::array<double, 3> color = {0, 0, 0}) {
+ drawSphere<T, D>(s.x + s.s.relativePosition, s.s.radius, color);
+ drawSphere<T, D>(s.x - s.s.relativePosition, s.s.radius, color);
+}
+
+template <int D>
+void draw(const Spin<signed, D, IsingSpin>& s, std::array<double, 3> color = {0, 0, 0}) {
+ if (s.s == 1) {
+ glColor3d(color[0], color[1], color[2]);
+ } else if (s.s == -1) {
+ glColor3d(1 - color[0], 1 - color[1], 1 - color[2]);
+ }
+ glRecti(s.x(0), s.x(1), s.x(0) + 1, s.x(1) + 1);
+}
+
+template <typename T, int D, class R, class S> class Animation : public measurement<T, D, R, S> {
+private:
+ unsigned n;
+ unsigned wait;
+ double L;
+ R s₀⁻¹;
+
+public:
+ Animation(T L, unsigned w, int argc, char* argv[], unsigned wait, bool periodic = false)
+ : s₀⁻¹(0), wait(wait), L(1000 * L) {
+ n = 0;
+
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+ glutInitWindowSize(w, w);
+ glutCreateWindow("wolffWindow");
+ glClearColor(0.0, 0.0, 0.0, 0.0);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ if (periodic) {
+ gluOrtho2D(0, L, 0, L);
+ } else {
+ gluOrtho2D(-L, L, -L, L);
+ }
+ }
+
+ void pre_cluster(const Model<T, D, R, S>& m, unsigned,
+ const Transformation<T, D, R, S>* t) override {
+ s₀⁻¹ = m.s0.inverse();
+ glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT);
+ for (const Spin<T, D, S>* s : m.s) {
+ draw(s₀⁻¹.act(*s));
+ }
+ if (n > wait) {
+ glColor3d(1.0, 0.0, 0.0);
+ glLineWidth(3);
+ glBegin(GL_LINES);
+ Vector<double, D> r = (t->r.t).template cast<double>() / 2.0;
+ double θ = atan2(r(1), r(0));
+ Vector<double, D> v1 = s₀⁻¹.template act<double>({r(0) + L * sin(θ), r(1) - L * cos(θ)});
+ Vector<double, D> v2 = s₀⁻¹.template act<double>({r(0) - L * sin(θ), r(1) + L * cos(θ)});
+ glVertex2d(v1(0), v1(1));
+ glVertex2d(v2(0), v2(1));
+ glEnd();
+ }
+ glFlush();
+ }
+
+ void plain_site_transformed(const Model<T, D, R, S>& m,
+ const Transformation<T, D, R, S>& t) override {
+ if (n > wait) {
+ std::array<double, 3> color;
+ if (t.current().size() > 1) {
+ color = {0, 0, 1};
+ } else {
+ color = {0, 1, 0};
+ }
+ for (const Spin<T, D, S>* s : t.current()) {
+ if (s != NULL) {
+ draw(s₀⁻¹.act(*s), color);
+ } else {
+ }
+ }
+ }
+ }
+
+ void post_cluster(const Model<T, D, R, S>& m) override {
+ if (n > wait) {
+ glFlush();
+ sleep(2);
+ }
+ n++;
+ }
+};