observer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * ALGO : PROJ. : Volume
  3. * RESEARCH : File : observer.c
  4. * : Date : 20100531.0725UTC
  5. * : Email : mail@algoresearch.net
  6. */
  7. #include "main.h"
  8. #include "observer.h"
  9. IMG img;
  10. VERTEX coord[8], center;
  11. void create_window(SYS_TABLE *sys)
  12. {
  13. XInitThreads();
  14. create_x (sys->dw, sys->dh, "Volume VIZ");
  15. create_image (&img, img.w, img.h);
  16. set_colormap();
  17. set_graymap();
  18. set_spectrum();
  19. clear_buffer(0, 0, sys->dw, sys->dh);
  20. }
  21. void set_env(SYS_TABLE *sys)
  22. {
  23. sys->dw = 640;
  24. sys->dh = 480;
  25. sys->main = 1;
  26. sys->ax = 1.2;
  27. sys->ay = -2.7;
  28. sys->c_ax = sys->ax;
  29. sys->c_ay = sys->ay;
  30. sys->a_sync = 0;
  31. sys->motion = 1;
  32. img.w = sys->dw;
  33. img.h = sys->dh;
  34. img.cx = img.w / 2;
  35. img.cy = img.h / 2;
  36. center.x = sys->space_w / 2;
  37. center.y = sys->space_h / 2;
  38. center.z = sys->space_d / 2;
  39. coord[0].x = 0;
  40. coord[0].y = 0;
  41. coord[0].z = 0;
  42. coord[1].x = sys->space_w;
  43. coord[1].y = 0;
  44. coord[1].z = 0;
  45. coord[2].x = 0;
  46. coord[2].y = sys->space_h;
  47. coord[2].z = 0;
  48. coord[3].x = sys->space_w;
  49. coord[3].y = sys->space_h;
  50. coord[3].z = 0;
  51. coord[4].x = sys->space_w;
  52. coord[4].y = 0;
  53. coord[4].z = sys->space_d;
  54. coord[5].x = 0;
  55. coord[5].y = sys->space_h;
  56. coord[5].z = sys->space_d;
  57. coord[6].x = 0;
  58. coord[6].y = 0;
  59. coord[6].z = sys->space_d;
  60. coord[7].x = sys->space_w;
  61. coord[7].y = sys->space_h;
  62. coord[7].z = sys->space_d;
  63. }
  64. int event_loop(SYS_TABLE *sys)
  65. {
  66. while(XPending(dpy) || sys->main)
  67. {
  68. XNextEvent(dpy, &e);
  69. switch(e.type)
  70. {
  71. case Expose:
  72. if (!sys->display) draw_buffer (0, 0, sys->dw, sys->dh);
  73. break;
  74. case ClientMessage:
  75. if (e.xclient.data.l[0] == delWin) sys->main = 0;
  76. break;
  77. case KeyPress:
  78. if(XLookupKeysym(&e.xkey, 0) == XK_Escape) sys->main = 0;
  79. if(XLookupKeysym(&e.xkey, 0) == XK_q) sys->main = 0;
  80. if(XLookupKeysym(&e.xkey, 0) == XK_0)
  81. {
  82. sys->c_ax = 0; sys->c_ay = 0;
  83. sys->a_sync = 1;
  84. sys->display = 1;
  85. sys->mapping = 1;
  86. }
  87. if(XLookupKeysym(&e.xkey, 0) == XK_Left)
  88. {
  89. sys->c_ay += .05;
  90. sys->a_sync = 1;
  91. sys->display = 1;
  92. sys->mapping = 1;
  93. }
  94. if(XLookupKeysym(&e.xkey, 0) == XK_Right)
  95. {
  96. sys->c_ay -= .05;
  97. sys->a_sync = 1;
  98. sys->display = 1;
  99. sys->mapping = 1;
  100. }
  101. if(XLookupKeysym(&e.xkey, 0) == XK_Up)
  102. {
  103. sys->c_ax -= .05;
  104. sys->a_sync = 1;
  105. sys->display = 1;
  106. sys->mapping = 1;
  107. }
  108. if(XLookupKeysym(&e.xkey, 0) == XK_Down)
  109. {
  110. sys->c_ax += .05;
  111. sys->a_sync = 1;
  112. sys->display = 1;
  113. sys->mapping = 1;
  114. }
  115. break;
  116. case MotionNotify:
  117. sys->display = 1;
  118. sys->a_sync = 0;
  119. if (sys->motion)
  120. {
  121. sys->mx = e.xmotion.x;
  122. sys->my = e.xmotion.y;
  123. sys->motion = 0;
  124. }
  125. sys->c_ay += (sys->mx - e.xmotion.x)*.01;
  126. sys->c_ax += (sys->my - e.xmotion.y)*.01;
  127. sys->mx = e.xmotion.x;
  128. sys->my = e.xmotion.y;
  129. break;
  130. case ButtonRelease:
  131. sys->a_sync = 1;
  132. sys->motion = 1;
  133. sys->mapping = 1;
  134. break;
  135. default:
  136. break;
  137. }
  138. usleep(1200);
  139. }
  140. pthread_exit(NULL);
  141. return 0;
  142. }
  143. void draw_status(SYS_TABLE *sys)
  144. {
  145. char string[64];
  146. XSetForeground(dpy, gc, gray[200]);
  147. sprintf(string, "Space size: %d*%d*%d",
  148. sys->space_w, sys->space_h, sys->space_d);
  149. XDrawString (dpy, pixmap, gc, 5, 15, string, strlen(string));
  150. sprintf(string, "Iterations: %d", sys->iter);
  151. XDrawString (dpy, pixmap, gc, 5, 30, string, strlen(string));
  152. }
  153. void draw_coord(SYS_TABLE *sys)
  154. {
  155. int i;
  156. VERTEX out[8];
  157. XSetForeground(dpy, gc, colors[255]);
  158. for (i=0; i<8; i++)
  159. {
  160. rotate_xy (&coord[i], &out[i], sys->c_ax, sys->c_ay, &center);
  161. out[i].x += img.cx;
  162. out[i].y += img.cy;
  163. XDrawLine
  164. (dpy, pixmap, gc, out[i].x-3, out[i].y-3, out[i].x+3, out[i].y+3);
  165. XDrawLine
  166. (dpy, pixmap, gc, out[i].x+3, out[i].y-3, out[i].x-3, out[i].y+3);
  167. }
  168. XDrawLine (dpy, pixmap, gc, out[0].x, out[0].y, out[1].x, out[1].y);
  169. XDrawLine (dpy, pixmap, gc, out[0].x, out[0].y, out[2].x, out[2].y);
  170. XDrawLine (dpy, pixmap, gc, out[0].x, out[0].y, out[6].x, out[6].y);
  171. }
  172. void display(SYS_TABLE *sys)
  173. {
  174. while(sys->main)
  175. {
  176. if (sys->display)
  177. {
  178. draw_image (&img, 0, 0, sys->dw, sys->dh);
  179. draw_status (sys);
  180. draw_coord (sys);
  181. draw_buffer (0, 0, sys->dw, sys->dh);
  182. sys->display = 0;
  183. }
  184. usleep(1000);
  185. }
  186. pthread_exit(NULL);
  187. }
  188. void mapping(SYS_TABLE *sys)
  189. {
  190. int x, y, z;
  191. VERTEX in, out;
  192. float alpha;
  193. clear_image (&img);
  194. if (sys->a_sync)
  195. {
  196. sys->ax = sys->c_ax;
  197. sys->ay = sys->c_ay;
  198. sys->a_sync = 0;
  199. }
  200. for(z=0; z<sys->space_d; z++)
  201. {
  202. for(y=0; y<sys->space_h; y++)
  203. {
  204. for(x=0; x<sys->space_w; x++)
  205. {
  206. in.x = x;
  207. in.y = y;
  208. in.z = z;
  209. rotate_xy (&in, &out, sys->ax, sys->ay, &center);
  210. //put_pixel (&img, out.x + img.cx, out.y + img.cy,
  211. // colors[cell_get(sys, x, y, z)]);
  212. alpha = cell_get(sys, x, y, z) / 4080.0;
  213. put_apixel (&img, out.x + img.cx, out.y + img.cy,
  214. colors[cell_get(sys, x, y, z)], alpha);
  215. }
  216. }
  217. if (!(z%4)) sys->display = 1;
  218. if (!sys->main) return;
  219. usleep(120);
  220. //if (!(z%2)) burn_image (&img);
  221. }
  222. sys->display = 1;
  223. }
  224. void observer(SYS_TABLE *sys)
  225. {
  226. while(sys->main)
  227. {
  228. if (sys->mapping)
  229. {
  230. mapping (sys);
  231. sys->mapping = 0;
  232. }
  233. usleep(1000);
  234. }
  235. pthread_exit(NULL);
  236. }
  237. void create_observer(SYS_TABLE *sys)
  238. {
  239. set_env (sys);
  240. create_window (sys);
  241. if(pthread_create(&(sys->pid_observer), NULL, (void *)observer, sys)|
  242. pthread_create(&(sys->pid_display), NULL, (void *)display, sys)|
  243. pthread_create(&(sys->pid_event_loop), NULL, (void *)event_loop, sys))
  244. {
  245. perror("pthread_create()");
  246. exit(1);
  247. }
  248. }