display.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2019 Mael Keryell
  4. *
  5. * StarPU 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. * StarPU 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 "display.h"
  17. void mandelbrot_graph(char *filename, int *pixels, unsigned width, unsigned height)
  18. {
  19. FILE *myfile;
  20. myfile = fopen(filename, "w");
  21. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  22. unsigned i,j;
  23. for (i = 0; i < height; i++){
  24. for (j = 0; j < width; j++){
  25. fprintf(myfile, "%d 0 0 ", pixels[j + i*width]);
  26. }
  27. fprintf(myfile, "\n");
  28. }
  29. fclose(myfile);
  30. }
  31. void mandelbrot_graph_transpose(char *filename, int64_t *pixels, unsigned width, unsigned height)
  32. {
  33. FILE *myfile;
  34. myfile = fopen(filename, "w");
  35. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  36. unsigned i,j;
  37. for (i = 0; i < width; i++){
  38. for (j = 0; j < height; j++){
  39. fprintf(myfile, "%d 0 0 ", pixels[i + j*width]);
  40. }
  41. fprintf(myfile, "\n");
  42. }
  43. fclose(myfile);
  44. }
  45. void pixels_print(int *pixels, unsigned width, unsigned height)
  46. {
  47. unsigned i,j;
  48. for (i = 0; i < height; i++){
  49. for (j = 0; j < width; j++){
  50. printf("%d ", pixels[j + i * width]);
  51. }
  52. printf("\n");
  53. }
  54. }
  55. ////////////////////////// NBODY ////////////////////////
  56. static int is_inside(struct Position p, unsigned width, unsigned height);
  57. static void get_planet_pixels(struct Position *pix, struct Position p);
  58. static void graph_pixels(struct Pixel *pixel, unsigned width, unsigned height, struct Position *pix, unsigned index, unsigned nbr_planets);
  59. static void nbody_ppm(char *filename, struct Pixel *pixels, unsigned width, unsigned height);
  60. static int is_inside(struct Position p, unsigned width, unsigned height)
  61. {
  62. if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
  63. return 1;
  64. else
  65. return 0;
  66. }
  67. void nbody_print(double *array, unsigned nbr_planets)
  68. {
  69. unsigned i,j;
  70. for (j = 0; j < nbr_planets; j++){
  71. printf("Planet %u:\n", j);
  72. printf("%f ; %f\n", array[j], array[j + nbr_planets]);
  73. }
  74. }
  75. //Returns a circle centered on the position p.
  76. static void get_planet_pixels(struct Position *pix, struct Position p)
  77. {
  78. int i,j,k;
  79. k = 0;
  80. for (i = p.x - 1; i <= p.x + 1; i++){
  81. for (j = p.y - 3; j <= p.y + 3; j++){
  82. pix[k].x = i;
  83. pix[k].y = j;
  84. k++;
  85. }
  86. }
  87. for (j = p.y - 2; j <= p.y + 2; j++){
  88. pix[k].x = p.x - 2;
  89. pix[k].y = j;
  90. k++;
  91. pix[k].x = p.x + 2;
  92. pix[k].y = j;
  93. k++;
  94. }
  95. for (j = p.y - 1; j <= p.y + 1; j++){
  96. pix[k].x = p.x - 3;
  97. pix[k].y = j;
  98. k++;
  99. pix[k].x = p.x + 3;
  100. pix[k].y = j;
  101. k++;
  102. }
  103. }
  104. static void graph_pixels(struct Pixel *pixels, unsigned width, unsigned height, struct Position *pix, unsigned index, unsigned nbr_planets)
  105. {
  106. /* printf("Planet: %u\n", index); */
  107. unsigned j;
  108. struct Pixel pixel = {0,0,0};
  109. for (j = 0; j < 37; j++){
  110. /* printf("X: %d, Y: %d\n", pix[j].x, pix[j].y); */
  111. if ( is_inside(pix[j], width, height) ){
  112. pixel.r = 125;
  113. pixel.b = round((255. * index) / nbr_planets);
  114. pixels[pix[j].x + pix[j].y * width] = pixel;
  115. }
  116. }
  117. }
  118. static void nbody_ppm(char *filename, struct Pixel *pixels, unsigned width, unsigned height)
  119. {
  120. unsigned i,j;
  121. FILE *myfile;
  122. myfile = fopen(filename, "w");
  123. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  124. for (i = 0; i < height; i++){
  125. for (j = 0; j < width; j++){
  126. struct Pixel pixel = pixels[j + i * width];
  127. fprintf(myfile, "%u %u %u ", pixel.r, pixel.g, pixel.b);
  128. }
  129. fprintf(myfile, "\n");
  130. }
  131. fclose(myfile);
  132. }
  133. void nbody_graph(char *filename, double *positions, unsigned nbr_planets, unsigned width, unsigned height, double min_val, double max_val)
  134. {
  135. struct Position *pix = malloc(37 * sizeof(struct Position));
  136. struct Pixel *pixels = calloc(width * height, sizeof(struct Pixel));
  137. unsigned i,j;
  138. for (i = 0; i < nbr_planets; i++){
  139. struct Position posi;
  140. posi.x = round( (positions[i] - min_val) / (max_val - min_val) * (width - 1));
  141. posi.y = round( (positions[i + nbr_planets] - min_val) / (max_val - min_val) * (width - 1));
  142. get_planet_pixels(pix, posi);
  143. graph_pixels(pixels, width, height, pix, i, nbr_planets);
  144. }
  145. nbody_ppm(filename, pixels, width, height);
  146. }
  147. void nbody_graph_transpose(char *filename, double *positions, unsigned nbr_planets, unsigned width, unsigned height, double min_val, double max_val)
  148. {
  149. struct Position *pix = malloc(37 * sizeof(struct Position));
  150. struct Pixel *pixels = calloc(width * height, sizeof(struct Pixel));
  151. unsigned i,j;
  152. for (i = 0; i < nbr_planets; i++){
  153. struct Position posi;
  154. posi.x = round( (positions[2 * i] - min_val) / (max_val - min_val) * (width - 1));
  155. posi.y = round( (positions[2 * i + 1] - min_val) / (max_val - min_val) * (width - 1));
  156. get_planet_pixels(pix, posi);
  157. graph_pixels(pixels, width, height, pix, i, nbr_planets);
  158. }
  159. nbody_ppm(filename, pixels, width, height);
  160. }