202a18ee936bbfb63840e8faacfad950a82d7dc5
[2dsolver.git] / src / main.c
1 /*
2 * main.c: entry point for 2dsolve
3 * Creation date: Fri Apr 13 23:15:04 2012
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <getopt.h>
11
12 #include <2dsolve.h>
13
14 #define SX(x, zoom) ((int) (((x - 0.05) * (zoom)) + SCREEN_WIDTH / 2))
15 #define SY(y, zoom) ((int) (-((y + 0.45) * (zoom)) + SCREEN_HEIGHT / 2))
16
17 int triangle_select;
18 struct geometry_info *curr_info;
19 float curr_zoom;
20
21 void display_geometry (display_t *, struct geometry_info *, float);
22
23 int
24 khandler (int key, struct display_info *disp, struct event_info *event)
25 {
26 if (event->state)
27 return;
28
29 if (event->code == SDLK_n)
30 triangle_select++;
31 else if (event->code == SDLK_p && triangle_select)
32 triangle_select--;
33 else
34 return 0;
35
36 display_geometry (disp, curr_info, curr_zoom);
37 display_refresh (disp);
38 }
39
40 void
41 display_geometry (display_t *disp, struct geometry_info *info, float zoom)
42 {
43 int i;
44 int select;
45
46 select = triangle_select % info->triangle_count;
47
48 clear (disp, OPAQUE (0));
49
50 display_printf (disp, 0, 0, OPAQUE (0xffa500), OPAQUE (0), "Triangle select: %5d/%d",
51 triangle_select, info->triangle_count);
52
53 for (i = 0; i < info->triangle_count; i++)
54 {
55 line (disp, SX (info->triangle_list[i]->nodes[0]->x, zoom),
56 SY (info->triangle_list[i]->nodes[0]->y, zoom),
57 SX (info->triangle_list[i]->nodes[1]->x, zoom),
58 SY (info->triangle_list[i]->nodes[1]->y, zoom),
59 OPAQUE (0xff0000));
60
61 line (disp, SX (info->triangle_list[i]->nodes[1]->x, zoom),
62 SY (info->triangle_list[i]->nodes[1]->y, zoom),
63 SX (info->triangle_list[i]->nodes[2]->x, zoom),
64 SY (info->triangle_list[i]->nodes[2]->y, zoom),
65 OPAQUE (0xff0000));
66
67 line (disp, SX (info->triangle_list[i]->nodes[2]->x, zoom),
68 SY (info->triangle_list[i]->nodes[2]->y, zoom),
69 SX (info->triangle_list[i]->nodes[0]->x, zoom),
70 SY (info->triangle_list[i]->nodes[0]->y, zoom),
71 OPAQUE (0xff0000));
72 }
73
74 line (disp, SX (FT_x (info->triangle_list[select]->nodes[0],
75 info->triangle_list[select]->nodes[1],
76 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
77 SY (FT_y (info->triangle_list[select]->nodes[0],
78 info->triangle_list[select]->nodes[1],
79 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
80 SX (FT_x (info->triangle_list[select]->nodes[0],
81 info->triangle_list[select]->nodes[1],
82 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
83 SY (FT_y (info->triangle_list[select]->nodes[0],
84 info->triangle_list[select]->nodes[1],
85 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
86 OPAQUE (0x00ffff));
87
88 line (disp, SX (FT_x (info->triangle_list[select]->nodes[0],
89 info->triangle_list[select]->nodes[1],
90 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
91 SY (FT_y (info->triangle_list[select]->nodes[0],
92 info->triangle_list[select]->nodes[1],
93 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
94 SX (FT_x (info->triangle_list[select]->nodes[0],
95 info->triangle_list[select]->nodes[1],
96 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
97 SY (FT_y (info->triangle_list[select]->nodes[0],
98 info->triangle_list[select]->nodes[1],
99 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
100 OPAQUE (0x00ffff));
101
102 line (disp, SX (FT_x (info->triangle_list[select]->nodes[0],
103 info->triangle_list[select]->nodes[1],
104 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
105 SY (FT_y (info->triangle_list[select]->nodes[0],
106 info->triangle_list[select]->nodes[1],
107 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
108 SX (FT_x (info->triangle_list[select]->nodes[0],
109 info->triangle_list[select]->nodes[1],
110 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
111 SY (FT_y (info->triangle_list[select]->nodes[0],
112 info->triangle_list[select]->nodes[1],
113 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
114 OPAQUE (0x00ffff));
115
116 for (i = 0; i < info->edge_count; i++)
117 line (disp, SX (info->edge_list[i]->p1->x, zoom),
118 SY (info->edge_list[i]->p1->y, zoom),
119 SX (info->edge_list[i]->p2->x, zoom),
120 SY (info->edge_list[i]->p2->y, zoom),
121 OPAQUE (0xffa500));
122
123 for (i = 0; i < info->point_count; i++)
124 mark (disp, SX (info->point_list[i]->x, zoom),
125 SY (info->point_list[i]->y, zoom),
126 2, OPAQUE (0x00ff00), 0);
127 }
128
129 struct problem2d *
130 get_default_problem (struct geometry_info *geom)
131 {
132 struct problem2d *new;
133
134 new = xmalloc (sizeof (struct problem2d));
135
136 memset (new, 0, sizeof (struct problem2d));
137
138 new->geometry = geom;
139 jit_compile (new->a[A_11] = jit_expr_new ("1"));
140 jit_compile (new->a[A_22] = jit_expr_new ("1"));
141 jit_compile (new->a[A_21] = jit_expr_new ("0"));
142 jit_compile (new->a[A_12] = jit_expr_new ("0"));
143 jit_compile (new->a0 = jit_expr_new ("1"));
144 jit_compile (new->f = jit_expr_new ("9.8"));
145
146 return new;
147 }
148
149 int
150 main (int argc, char *argv[], char *envp[])
151 {
152 int i, j;
153
154 display_t *disp;
155 struct problem2d *problem;
156 gsl_matrix *A;
157 gsl_vector *b;
158
159 curr_zoom = 200.0;
160
161 if (argc != 2)
162 {
163 fprintf (stderr, "Usage: %s <file.mat>\n", argv[0]);
164 exit (EXIT_FAILURE);
165 }
166
167 if ((curr_info = geometry_load_from_matlab (argv[1], "p", "e", "t")) == NULL)
168 {
169 fprintf (stderr, "%s: couldn't load geometry from `%s'\n", argv[0], argv[1]);
170 exit (EXIT_FAILURE);
171 }
172
173 problem = get_default_problem (curr_info);
174
175 build_matrix_and_vector (problem, &A, &b);
176
177 if ((disp = display_new (SCREEN_WIDTH, SCREEN_HEIGHT)) == NULL)
178 exit (EXIT_FAILURE);
179
180 display_register_key_handler (disp, SDLK_n, khandler);
181 display_register_key_handler (disp, SDLK_p, khandler);
182
183 display_geometry (disp, curr_info, curr_zoom);
184
185 for (i = 0; i < A->size1; i++)
186 for (j = 0; j < A->size2; j++)
187 pset_abs (disp, i, j, OPAQUE (calc_color (15 * gsl_matrix_get (A, i, j))));
188
189 display_refresh (disp);
190
191 display_end (disp);
192 geometry_destroy (curr_info);
193 return 0;
194 }
195