triangle_group_limit_set/limit_set.c

515 lines
13 KiB
C

#include <math.h>
#include <stdio.h>
#include <sys/time.h>
#include <X11/XKBlib.h>
#include "initcairo.h"
#include "triangle.h"
#include "linalg.h"
#define LOOP(i) for(int i = 0; i < 3; i++)
typedef struct {
double x;
double y;
} point_t;
int processEvent(GraphicsInfo *info, XEvent *ev, void *data);
void setup();
void destroy();
void cartanMatrix(gsl_matrix *cartan, double a1, double a2, double a3, double s);
void initializeTriangleGenerators(gsl_matrix **gen, gsl_matrix *cartan);
int compareAngle(const void *x, const void *y);
int computeLimitCurve();
void draw(cairo_t *ctx, void *data);
point_t intersect(point_t a, point_t b, point_t c, point_t d);
void drawBoxStd(cairo_t *ctx, const char *word, char base);
void drawBox(cairo_t *ctx, const char *word1, const char *word2);
GraphicsInfo G;
double parameter;
int n_group_elements;
double *limit_curve; // x, y, angle triples
int limit_curve_valid = 0;
groupelement_t *group;
workspace_t *ws;
gsl_matrix *gen[3];
gsl_matrix **matrices;
gsl_matrix *cartan, *cob, *coxeter, *coxeter_fixedpoints, *fixedpoints;
int show_boxes = 0;
int show_attractors = 0;
int show_limit = 1;
int processEvent(GraphicsInfo *info, XEvent *ev, void *data)
{
int state;
unsigned long key;
switch(ev->type) {
case KeyPress:
state = ev->xkey.state & (ShiftMask | LockMask | ControlMask);
key = XkbKeycodeToKeysym(ev->xkey.display, ev->xkey.keycode, 0, !!(state & ShiftMask));
printf("Key pressed: %ld\n", key);
switch(key) {
case '<':
parameter /= exp(0.002);
limit_curve_valid = computeLimitCurve();
break;
case '>':
parameter *= exp(0.002);
limit_curve_valid = computeLimitCurve();
break;
case ' ':
parameter = 2.890053638;
limit_curve_valid = computeLimitCurve();
break;
case XK_Return:
parameter = 2.76375163;
limit_curve_valid = computeLimitCurve();
break;
case 'm':
printf("G.matrix.xx = %f;\n", G.matrix.xx);
printf("G.matrix.xy = %f;\n", G.matrix.xy);
printf("G.matrix.x0 = %f;\n", G.matrix.x0);
printf("G.matrix.yx = %f;\n", G.matrix.yx);
printf("G.matrix.yy = %f;\n", G.matrix.yy);
printf("G.matrix.y0 = %f;\n", G.matrix.y0);
printf("G.scalefactor = %f;\n", G.scalefactor);
break;
case 'b':
show_boxes = !show_boxes;
break;
case 'a':
show_attractors = !show_attractors;
break;
case 'l':
show_limit = !show_limit;
}
draw(G.ctx, 0);
}
return 0;
}
void setup()
{
n_group_elements = 10000;
limit_curve = malloc(3*n_group_elements*sizeof(double));
group = malloc(n_group_elements*sizeof(groupelement_t));
ws = workspace_alloc(3);
LOOP(i) gen[i] = gsl_matrix_alloc(3, 3);
matrices = malloc(n_group_elements*sizeof(gsl_matrix*));
for(int i = 0; i < n_group_elements; i++)
matrices[i] = gsl_matrix_alloc(3, 3);
cartan = gsl_matrix_alloc(3, 3);
cob = gsl_matrix_alloc(3, 3);
coxeter = gsl_matrix_alloc(3, 3);
coxeter_fixedpoints = gsl_matrix_alloc(3, 3);
fixedpoints = gsl_matrix_alloc(3, 3);
}
void destroy()
{
free(limit_curve);
free(group);
workspace_free(ws);
LOOP(i) gsl_matrix_free(gen[i]);
for(int i = 0; i < n_group_elements; i++)
gsl_matrix_free(matrices[i]);
free(matrices);
gsl_matrix_free(cartan);
gsl_matrix_free(cob);
gsl_matrix_free(coxeter);
gsl_matrix_free(coxeter_fixedpoints);
gsl_matrix_free(fixedpoints);
}
void cartanMatrix(gsl_matrix *cartan, double a1, double a2, double a3, double s)
{
gsl_matrix_set(cartan, 0, 0, -2);
gsl_matrix_set(cartan, 0, 1, 2*s*cos(a3));
gsl_matrix_set(cartan, 0, 2, 2/s*cos(a2));
gsl_matrix_set(cartan, 1, 0, 2/s*cos(a3));
gsl_matrix_set(cartan, 1, 1, -2);
gsl_matrix_set(cartan, 1, 2, 2*s*cos(a1));
gsl_matrix_set(cartan, 2, 0, 2*s*cos(a2));
gsl_matrix_set(cartan, 2, 1, 2/s*cos(a1));
gsl_matrix_set(cartan, 2, 2, -2);
}
void initializeTriangleGenerators(gsl_matrix **gen, gsl_matrix *cartan)
{
LOOP(i) gsl_matrix_set_identity(gen[i]);
LOOP(i) LOOP(j) *gsl_matrix_ptr(gen[i], i, j) += gsl_matrix_get(cartan, i, j);
}
int compareAngle(const void *x, const void *y)
{
return ((double*)x)[2] > ((double*)y)[2] ? 1 : -1;
}
// intersect the lines a-b and c-d
point_t intersect(point_t a, point_t b, point_t c, point_t d)
{
point_t res;
double t = ((c.y-d.y)*(c.x-a.x) - (c.x-d.x)*(c.y-a.y)) / ((b.x-a.x)*(c.y-d.y) - (b.y-a.y)*(c.x-d.x));
res.x = (1-t)*a.x + t*b.x;
res.y = (1-t)*a.y + t*b.y;
return res;
}
void drawPoint(cairo_t *ctx, point_t p)
{
// not implemented
cairo_save(ctx);
cairo_move_to(ctx, p.x, p.y);
cairo_close_path(ctx);
cairo_set_line_cap(ctx, CAIRO_LINE_CAP_ROUND);
cairo_set_line_width(ctx, 10.0/G.scalefactor);
cairo_stroke(ctx);
cairo_restore(ctx);
}
void drawLine(cairo_t *ctx, point_t a, point_t b)
{
// not implemented
}
void drawSegment(cairo_t *ctx, point_t a, point_t b)
{
cairo_move_to(ctx, a.x, a.y);
cairo_line_to(ctx, b.x, b.y);
cairo_stroke(ctx);
}
void drawPolygon(cairo_t *ctx, int sides, point_t a, point_t b, point_t c, point_t d) // only quadrilaterals so far
{
drawSegment(ctx, a, b);
drawSegment(ctx, b, c);
drawSegment(ctx, c, d);
drawSegment(ctx, d, a);
}
void fixedPoints(const char *word, point_t *out)
{
gsl_matrix *tmp = ws->stack[10];
gsl_matrix *ev = ws->stack[11];
gsl_matrix_set_identity(tmp);
for(int i = 0; i < strlen(word); i++) {
if(word[i] == ' ')
continue;
multiply_right(tmp, gen[word[i]-'a'], ws);
}
eigenvectors(tmp, ev, ws);
multiply_left(cob, ev, ws);
LOOP(i) out[i].x = gsl_matrix_get(ev, 0, i) / gsl_matrix_get(ev, 2, i);
LOOP(i) out[i].y = gsl_matrix_get(ev, 1, i) / gsl_matrix_get(ev, 2, i);
}
void drawAttractorConnection(cairo_t *ctx, const char *word1, const char *word2)
{
point_t p1[3], p2[3];
fixedPoints(word1, p1);
fixedPoints(word2, p2);
drawSegment(ctx, p1[0], p2[0]);
}
void drawBox(cairo_t *ctx, const char *word1, const char *word2)
{
point_t p[2][3],i[2];
fixedPoints(word1, p[0]);
fixedPoints(word2, p[1]);
// intersect attracting line with neutral line of the other element
for(int j = 0; j < 2; j++)
i[j] = intersect(p[j%2][0],p[j%2][1],p[(j+1)%2][0],p[(j+1)%2][2]);
drawPolygon(ctx, 4, p[0][0], i[0], p[1][0], i[1]);
}
void drawBoxStd(cairo_t *ctx, const char *word, char base)
{
char word1[100];
char word2[100];
int len = strlen(word);
if(len*2 + 4 > 100)
return;
for(int i = 0; i < len; i++) {
word1[i] = word1[2*len+2-i] = word[i];
word2[i] = word2[2*len+2-i] = word[i];
}
word1[2*len+3] = 0;
word2[2*len+3] = 0;
LOOP(i) word1[len+i] = (base-'A'+6+i+1)%3+'a';
LOOP(i) word2[len+i] = (base-'A'+6-i-1)%3+'a';
// printf("Words: %s %s\n", word1, word2);
drawBox(ctx, word1, word2);
}
int computeLimitCurve()
{
int p = 5, q = 5, r = 5;
int k = 2, l = 2, m = 2;
generate_triangle_group(group, n_group_elements, p, q, r);
// do first in the Fuchsian case to get the angles
cartanMatrix(cartan, M_PI/p, M_PI/q, M_PI/r, 1.0);
initializeTriangleGenerators(gen, cartan);
gsl_matrix_set_identity(matrices[0]);
for(int i = 1; i < n_group_elements; i++)
multiply(matrices[group[i].parent->id], gen[group[i].letter], matrices[i]);
diagonalize_symmetric_form(cartan, cob, ws);
multiply_many(ws, coxeter, 3, gen[0], gen[1], gen[2]);
if(!eigenvectors(coxeter, coxeter_fixedpoints, ws))
return 0;
for(int i = 0; i < n_group_elements; i++) {
multiply_many(ws, fixedpoints, 3, cob, matrices[i], coxeter_fixedpoints);
limit_curve[3*i+2] = atan2(
gsl_matrix_get(fixedpoints, 0, 0)/gsl_matrix_get(fixedpoints, 2, 0),
gsl_matrix_get(fixedpoints, 1, 0)/gsl_matrix_get(fixedpoints, 2, 0));
}
// now to it again to calculate x and y coordinates
cartanMatrix(cartan, M_PI*k/p, M_PI*l/q, M_PI*m/r, parameter);
initializeTriangleGenerators(gen, cartan);
gsl_matrix_set_identity(matrices[0]);
for(int i = 1; i < n_group_elements; i++)
multiply(matrices[group[i].parent->id], gen[group[i].letter], matrices[i]);
gsl_matrix_memcpy(cob, cartan); // is this a good choice of basis
// gsl_matrix_set_identity(cob);
multiply_many(ws, coxeter, 3, gen[0], gen[1], gen[2]);
if(!eigenvectors(coxeter, coxeter_fixedpoints, ws))
return 0;
for(int i = 0; i < n_group_elements; i++) {
multiply_many(ws, fixedpoints, 3, cob, matrices[i], coxeter_fixedpoints);
limit_curve[3*i] = gsl_matrix_get(fixedpoints, 0, 0)/gsl_matrix_get(fixedpoints, 2, 0);
limit_curve[3*i+1] = gsl_matrix_get(fixedpoints, 1, 0)/gsl_matrix_get(fixedpoints, 2, 0);
}
qsort(limit_curve, n_group_elements, 3*sizeof(double), compareAngle);
return 1;
}
void drawBoxes(cairo_t *ctx)
{
cairo_set_source_rgb(ctx, 1, 0, 0);
drawBoxStd(ctx, "c", 'C');
drawBoxStd(ctx, "", 'B');
drawBoxStd(ctx, "a", 'A');
drawBoxStd(ctx, "", 'C');
// drawBoxStd(ctx, "", 'A');
drawBoxStd(ctx, "b", 'B');
cairo_set_source_rgb(ctx, 0, 0, 1);
drawBoxStd(ctx, "ca", 'A');
drawBoxStd(ctx, "cac", 'C');
drawBoxStd(ctx, "caca", 'A');
// drawBoxStd(ctx, "cacac", 'C');
// drawBoxStd(ctx, "acac", 'A');
// drawBoxStd(ctx, "aca", 'C');
cairo_set_source_rgb(ctx, 0, 1, 0);
drawBoxStd(ctx, "ac", 'C');
// drawBoxStd(ctx, "aca", 'A');
drawBoxStd(ctx, "acac", 'C');
// drawBoxStd(ctx, "acaca", 'A');
// drawBoxStd(ctx, "caca", 'C');
// drawBoxStd(ctx, "cac", 'A');
cairo_set_source_rgb(ctx, 1, 0, 1);
drawBoxStd(ctx, "aca cb", 'B');
drawBoxStd(ctx, "aca cbc", 'C');
drawBoxStd(ctx, "aca cbcb", 'B');
drawBoxStd(ctx, "aca bcbc", 'C');
drawBoxStd(ctx, "aca bcb", 'B');
drawBoxStd(ctx, "aca bc", 'C');
/*
cairo_set_source_rgb(ctx, 1, 0, 1);
drawAttractorConnection("acb", "acaba");
drawAttractorConnection("acaba", "acacbca");
drawAttractorConnection("ac acb ca", "ac acaba ca");
drawAttractorConnection("ac acaba ca", "ac acacbca ca");
drawAttractorConnection("acac acb caca", "acac acaba caca");
drawAttractorConnection("acac acaba caca", "acac acacbca caca");
drawAttractorConnection("caca acb acac", "caca acaba acac");
// drawAttractorConnection("caca acaba acac", "caca acacbca acac");
drawAttractorConnection("ca acb ac", "ca acaba ac");
drawAttractorConnection("ca acaba ac", "ca acacbca ac");
cairo_set_source_rgb(ctx, 0, 1, 0);
drawAttractorConnection("cab", "cacbc");
drawAttractorConnection("cacbc", "cacabac");
drawAttractorConnection("ca cab ac", "ca cacbc ac");
drawAttractorConnection("ca cacbc ac", "ca cacabac ac");
drawAttractorConnection("caca cab acac", "caca cacbc acac");
drawAttractorConnection("caca cacbc acac", "caca cacabac acac");
drawAttractorConnection("acac cab caca", "acac cacbc caca");
drawAttractorConnection("acac cacbc caca", "acac cacabac caca");
// drawAttractorConnection("ac cab ca", "ac cacbc ca");
drawAttractorConnection("ac cacbc ca", "ac cacabac ca");
*/
// finer box test
/*
cairo_set_source_rgb(ctx, 0, 1, 0);
drawBox("acb", "acabacaca");
drawBox("acabacaca", "acaba");
// drawBox("acb", "acaba");
drawBox("acaba", "acacbacac");
drawBox("acacbacac", "acacbca");
drawBox("acacbca", "cacacbcac");
drawBox("cacacbcac", "acacabaca");
drawBox("acacabaca", "cacabac");
drawBox("cacabac", "cacabcaca");
drawBox("cacabcaca", "cacbc");
// drawBox("cacbc", "cab");
drawBox("cacbc", "cacbcacac");
drawBox("cacbcacac", "cab"); // strange
*/
}
void drawAttractors(cairo_t *ctx)
{
point_t p[3][3];
fixedPoints("abc", p[0]);
fixedPoints("bca", p[1]);
fixedPoints("cab", p[2]);
cairo_set_source_rgb(ctx, 0, 0, 0);
LOOP(i) LOOP(j) drawPoint(ctx, p[i][j]);
LOOP(i) LOOP(j) drawLine(ctx, p[i][j], p[i][(j+1)%3]);
}
void draw(cairo_t *ctx, void *data)
{
struct timeval current_time;
gettimeofday(&current_time, 0);
double start_time = current_time.tv_sec + current_time.tv_usec*1e-6;
cairo_push_group(ctx);
cairo_set_source_rgb(ctx, 1, 1, 1);
cairo_paint(ctx);
cairo_set_matrix(ctx, &G.matrix);
// defaults; use save/restore whenever these are changed
cairo_set_line_width(ctx, 1.0/G.scalefactor);
cairo_set_font_size(ctx, 16);
cairo_set_line_join(ctx, CAIRO_LINE_JOIN_BEVEL);
cairo_set_line_cap(ctx, CAIRO_LINE_CAP_ROUND);
if(limit_curve_valid) {
if(show_limit) {
int last_inside = 0;
for(int i = 0; i < n_group_elements; i++) {
double x = limit_curve[3*i];
double y = limit_curve[3*i+1];
cairo_user_to_device(ctx, &x, &y);
if(-x < G.width && x < 3*G.width && -y < G.height && y < 3*G.height) {
if(!last_inside) {
cairo_move_to(ctx, limit_curve[3*i], limit_curve[3*i+1]);
} else {
cairo_line_to(ctx, limit_curve[3*i], limit_curve[3*i+1]);
}
last_inside = 1;
} else {
last_inside = 0;
}
}
cairo_set_source_rgb(ctx, 0, 0, 0);
cairo_stroke(ctx);
}
if(show_boxes)
drawBoxes(ctx);
if(show_attractors)
drawAttractors(ctx);
}
cairo_identity_matrix(ctx);
cairo_move_to(ctx, 15, 30);
cairo_set_source_rgb(ctx, 0, 0, 0);
char buf[100];
sprintf(buf, "t = %.8f", parameter);
cairo_show_text(ctx, buf);
cairo_pop_group_to_source(ctx);
cairo_paint(ctx);
cairo_surface_flush(cairo_get_target(ctx));
gettimeofday(&current_time, 0);
double end_time = current_time.tv_sec + current_time.tv_usec*1e-6;
printf("draw() finished in %g seconds\n", end_time - start_time);
}
int main()
{
parameter = 3.0;
setup();
limit_curve_valid = computeLimitCurve();
if(!initCairo(0, KeyPressMask, 200, 200, "Triangle group", &G))
return 1;
/*
cairo_matrix_init_identity(&G.matrix);
G.matrix.xx = G.matrix.yy = G.scalefactor = 1100.0;
G.matrix.x0 = 1150.0;
G.matrix.y0 = 900.0;
*/
G.matrix.xx = 837.930824;
G.matrix.xy = -712.651341;
G.matrix.x0 = 180.427716;
G.matrix.yx = 712.651341;
G.matrix.yy = 837.930824;
G.matrix.y0 = 1412.553240;
G.scalefactor = 1100.000000;
startTimer(&G);
while(!checkEvents(&G, processEvent, draw, 0)) {
waitUpdateTimer(&G);
}
destroyCairo(&G);
destroy();
return 0;
}