heat_display.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "heat.h"
  17. #ifdef OPENGL_RENDER
  18. /*
  19. * Just some dummy OpenGL code to display our results
  20. *
  21. */
  22. static float minval, maxval;
  23. static unsigned ntheta;
  24. static unsigned nthick;
  25. static float *result;
  26. static unsigned printmesh =0;
  27. static point *pmesh;
  28. float xmin, xmax, ymin, ymax;
  29. float xcenter, ycenter;
  30. static void generate_graph(void)
  31. {
  32. unsigned theta, thick;
  33. for (theta = 0; theta < ntheta-1; theta++)
  34. {
  35. for (thick = 0; thick < nthick-1; thick++)
  36. {
  37. unsigned nodeA = NODE_NUMBER(theta, thick);
  38. unsigned nodeB = NODE_NUMBER(theta, thick+1);
  39. unsigned nodeC = NODE_NUMBER(theta+1, thick+1);
  40. unsigned nodeD = NODE_NUMBER(theta+1, thick);
  41. float colorA_R, colorB_R, colorC_R, colorD_R;
  42. float colorA_G, colorB_G, colorC_G, colorD_G;
  43. float colorA_B, colorB_B, colorC_B, colorD_B;
  44. if (maxval == minval) {
  45. colorA_R = 1.0f; colorA_G = 1.0f; colorA_B = 1.0f;
  46. colorB_R = 1.0f; colorB_G = 1.0f; colorB_B = 1.0f;
  47. colorC_R = 1.0f; colorC_G = 1.0f; colorC_B = 1.0f;
  48. colorD_R = 1.0f; colorD_G = 1.0f; colorD_B = 1.0f;
  49. }
  50. else {
  51. float amplitude = maxval - minval;
  52. float coeffA, coeffB, coeffC, coeffD;
  53. coeffA = (result[nodeA] - minval)/amplitude;
  54. coeffB = (result[nodeB] - minval)/amplitude;
  55. coeffC = (result[nodeC] - minval)/amplitude;
  56. coeffD = (result[nodeD] - minval)/amplitude;
  57. colorA_R = coeffA>0.5f?1.0f:(2.0*coeffA)*1.0f;
  58. colorB_R = coeffB>0.5f?1.0f:(2.0*coeffB)*1.0f;
  59. colorC_R = coeffC>0.5f?1.0f:(2.0*coeffC)*1.0f;
  60. colorD_R = coeffD>0.5f?1.0f:(2.0*coeffD)*1.0f;
  61. colorA_B = 0.0f;
  62. colorB_B = 0.0f;
  63. colorC_B = 0.0f;
  64. colorD_B = 0.0f;
  65. colorA_G = coeffA<0.5f?1.0f:2.0*(1 - coeffA)*1.0f;
  66. colorB_G = coeffB<0.5f?1.0f:2.0*(1 - coeffB)*1.0f;
  67. colorC_G = coeffC<0.5f?1.0f:2.0*(1 - coeffC)*1.0f;
  68. colorD_G = coeffD<0.5f?1.0f:2.0*(1 - coeffD)*1.0f;
  69. }
  70. if (printmesh) {
  71. glColor3f (0.0f, 0.0f, 0.0f);
  72. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  73. glLineWidth(3.0f);
  74. glBegin(GL_POLYGON);
  75. glVertex3f(pmesh[nodeA].x, pmesh[nodeA].y, 2.0f);
  76. glVertex3f(pmesh[nodeD].x, pmesh[nodeD].y, 2.0f);
  77. glVertex3f(pmesh[nodeC].x, pmesh[nodeC].y, 2.0f);
  78. glVertex3f(pmesh[nodeA].x, pmesh[nodeA].y, 2.0f);
  79. glEnd();
  80. glBegin(GL_POLYGON);
  81. glVertex3f(pmesh[nodeA].x, pmesh[nodeA].y, 1.0f);
  82. glVertex3f(pmesh[nodeC].x, pmesh[nodeC].y, 1.0f);
  83. glVertex3f(pmesh[nodeB].x, pmesh[nodeB].y, 1.0f);
  84. glVertex3f(pmesh[nodeA].x, pmesh[nodeA].y, 1.0f);
  85. glEnd();
  86. }
  87. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  88. glBegin(GL_POLYGON);
  89. glColor3f (colorA_R, colorA_G, colorA_B);
  90. glVertex3f(pmesh[nodeA].x, pmesh[nodeA].y, 0.0f);
  91. glColor3f (colorD_R, colorD_G, colorD_B);
  92. glVertex3f(pmesh[nodeD].x, pmesh[nodeD].y, 0.0f);
  93. glColor3f (colorC_R, colorC_G, colorC_B);
  94. glVertex3f(pmesh[nodeC].x, pmesh[nodeC].y, 0.0f);
  95. glEnd();
  96. glBegin(GL_POLYGON);
  97. glColor3f (colorA_R, colorA_G, colorA_B);
  98. glVertex3f(pmesh[nodeA].x, pmesh[nodeA].y, 0.0f);
  99. glColor3f (colorC_R, colorC_G, colorC_B);
  100. glVertex3f(pmesh[nodeC].x, pmesh[nodeC].y, 0.0f);
  101. glColor3f (colorB_R, colorB_G, colorB_B);
  102. glVertex3f(pmesh[nodeB].x, pmesh[nodeB].y, 0.0f);
  103. glEnd();
  104. }
  105. }
  106. }
  107. static void display(void)
  108. {
  109. glClear (GL_COLOR_BUFFER_BIT);
  110. glLoadIdentity (); /* clear the matrix */
  111. float amplitude = STARPU_MAX(xmax - xmin, ymax - ymin);
  112. float factor = 1.0/amplitude;
  113. glScalef (factor, factor, factor); /* modeling transformation */
  114. gluLookAt (xcenter, ycenter, 30.0f, xcenter, ycenter, 0.0f, 0.0f, 1.0f, 0.0f);
  115. // printf("factor %f\n", factor);
  116. // glRotatef(-0,0.0,0.0,0.0);
  117. generate_graph();
  118. glFlush ();
  119. }
  120. static void pressKey(unsigned char key, int x __attribute__ ((unused)), int y __attribute__ ((unused)))
  121. {
  122. switch (key) {
  123. case 'q':
  124. exit(0);
  125. default:
  126. printmesh = !printmesh;
  127. display();
  128. break;
  129. }
  130. }
  131. static void reshape (int w, int h)
  132. {
  133. glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  134. glMatrixMode (GL_PROJECTION);
  135. glLoadIdentity ();
  136. glFrustum (xmin, xmax, ymin, ymax, 5.0f, 5.0f);
  137. glMatrixMode (GL_MODELVIEW);
  138. }
  139. void find_limits(void)
  140. {
  141. minval = 100000000.0f;
  142. maxval = -10000000.0f;
  143. unsigned i;
  144. for (i = 0; i < DIM; i++)
  145. {
  146. /* find min */
  147. minval = STARPU_MIN(result[i], minval);
  148. /* find max */
  149. maxval = STARPU_MAX(result[i], maxval);
  150. }
  151. xmin = 10000000.0f;
  152. xmax = -10000000.0f;
  153. ymin = 10000000.0f;
  154. ymax = -10000000.0f;
  155. unsigned theta, thick;
  156. for (theta = 0; theta < ntheta; theta++)
  157. {
  158. for (thick = 0; thick < nthick; thick++)
  159. {
  160. point *p = &pmesh[NODE_NUMBER(theta, thick)];
  161. if (p->x < xmin)
  162. xmin = p->x;
  163. if (p->x > xmax)
  164. xmax = p->x;
  165. if (p->y < ymin)
  166. ymin = p->y;
  167. if (p->y > ymax)
  168. ymax = p->y;
  169. }
  170. }
  171. ycenter = (ymin + ymax)/2;
  172. xcenter = (xmin + xmax)/2;
  173. }
  174. void opengl_render(unsigned _ntheta, unsigned _nthick, float *_result, point *_pmesh, int argc_, char **argv_)
  175. {
  176. fprintf(stderr, "OpenGL rendering ... \n");
  177. ntheta = _ntheta;
  178. nthick = _nthick;
  179. result = _result;
  180. printmesh = 0;
  181. pmesh = _pmesh;
  182. find_limits();
  183. glutInit(&argc_, argv_);
  184. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  185. glutInitWindowSize (800, 800);
  186. glutInitWindowPosition (100, 100);
  187. glutCreateWindow ("Temperature");
  188. /* init */
  189. glClearColor (0.0, 0.0, 0.0, 0.0);
  190. glShadeModel (GL_MODELVIEW);
  191. glutKeyboardFunc(pressKey);
  192. glutDisplayFunc(display);
  193. glutReshapeFunc(reshape);
  194. glutMainLoop();
  195. }
  196. #endif // OPENGL_RENDER
  197. #ifdef USE_POSTSCRIPT
  198. static void postscript_gen(void)
  199. {
  200. FILE *psfile;
  201. psfile = fopen("output.ps", "w+");
  202. int offx, offy;
  203. unsigned theta, thick;
  204. offx = RMAX+50;
  205. offy = 100;
  206. for (theta = 0; theta < ntheta-1; theta++)
  207. {
  208. for (thick = 0; thick < nthick-1; thick++)
  209. {
  210. fprintf(psfile, "newpath\n");
  211. fprintf(psfile, "%d %d moveto\n", (int)pmesh[NODE_NUMBER(theta, thick)].x + offx,
  212. (int)pmesh[NODE_NUMBER(theta, thick)].y+ offy);
  213. fprintf(psfile, "%d %d lineto\n", (int)pmesh[NODE_NUMBER(theta+1, thick)].x + offx,
  214. (int)pmesh[NODE_NUMBER(theta+1, thick)].y+ offy);
  215. fprintf(psfile, "%d %d lineto\n", (int)pmesh[NODE_NUMBER(theta+1, thick+1)].x + offx,
  216. (int)pmesh[NODE_NUMBER(theta+1, thick+1)].y+ offy);
  217. fprintf(psfile, "closepath\n");
  218. fprintf(psfile, "stroke\n");
  219. fprintf(psfile, "newpath\n");
  220. fprintf(psfile, "%d %d moveto\n", (int)pmesh[NODE_NUMBER(theta, thick)].x + offx,
  221. (int)pmesh[NODE_NUMBER(theta, thick)].y+ offy);
  222. fprintf(psfile, "%d %d lineto\n", (int)pmesh[NODE_NUMBER(theta, thick+1)].x + offx,
  223. (int)pmesh[NODE_NUMBER(theta, thick+1)].y+ offy);
  224. fprintf(psfile, "%d %d lineto\n", (int)pmesh[NODE_NUMBER(theta+1, thick+1)].x + offx,
  225. (int)pmesh[NODE_NUMBER(theta+1, thick+1)].y+ offy);
  226. fprintf(psfile, "closepath\n");
  227. fprintf(psfile, "stroke\n");
  228. }
  229. }
  230. fclose(psfile);
  231. }
  232. #endif