display.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2019 Mael Keryell
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "display.h"
  18. void mandelbrot_graph(char *filename, int *pixels, unsigned width, unsigned height)
  19. {
  20. FILE *myfile;
  21. myfile = fopen(filename, "w");
  22. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  23. unsigned i,j;
  24. for (i = 0; i < height; i++){
  25. for (j = 0; j < width; j++){
  26. fprintf(myfile, "%d 0 0 ", pixels[j + i*width]);
  27. }
  28. fprintf(myfile, "\n");
  29. }
  30. fclose(myfile);
  31. }
  32. void mandelbrot_graph_transpose(char *filename, int64_t *pixels, unsigned width, unsigned height)
  33. {
  34. FILE *myfile;
  35. myfile = fopen(filename, "w");
  36. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  37. unsigned i,j;
  38. for (i = 0; i < width; i++){
  39. for (j = 0; j < height; j++){
  40. fprintf(myfile, "%d 0 0 ", pixels[i + j*width]);
  41. }
  42. fprintf(myfile, "\n");
  43. }
  44. fclose(myfile);
  45. }
  46. void pixels_print(int *pixels, unsigned width, unsigned height)
  47. {
  48. unsigned i,j;
  49. for (i = 0; i < height; i++){
  50. for (j = 0; j < width; j++){
  51. printf("%d ", pixels[j + i * width]);
  52. }
  53. printf("\n");
  54. }
  55. }
  56. ////////////////////////// NBODY ////////////////////////
  57. static int is_inside(struct Position p, unsigned width, unsigned height);
  58. static void get_planet_pixels(struct Position *pix, struct Position p);
  59. static void graph_pixels(struct Pixel *pixel, unsigned width, unsigned height, struct Position *pix, unsigned index, unsigned nbr_planets);
  60. static void nbody_ppm(char *filename, struct Pixel *pixels, unsigned width, unsigned height);
  61. static int is_inside(struct Position p, unsigned width, unsigned height)
  62. {
  63. if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
  64. return 1;
  65. else
  66. return 0;
  67. }
  68. void nbody_print(double *array, unsigned nbr_planets)
  69. {
  70. unsigned i,j;
  71. for (j = 0; j < nbr_planets; j++){
  72. printf("Planet %u:\n", j);
  73. printf("%f ; %f\n", array[j], array[j + nbr_planets]);
  74. }
  75. }
  76. //Returns a circle centered on the position p.
  77. static void get_planet_pixels(struct Position *pix, struct Position p)
  78. {
  79. int i,j,k;
  80. k = 0;
  81. for (i = p.x - 1; i <= p.x + 1; i++){
  82. for (j = p.y - 3; j <= p.y + 3; j++){
  83. pix[k].x = i;
  84. pix[k].y = j;
  85. k++;
  86. }
  87. }
  88. for (j = p.y - 2; j <= p.y + 2; j++){
  89. pix[k].x = p.x - 2;
  90. pix[k].y = j;
  91. k++;
  92. pix[k].x = p.x + 2;
  93. pix[k].y = j;
  94. k++;
  95. }
  96. for (j = p.y - 1; j <= p.y + 1; j++){
  97. pix[k].x = p.x - 3;
  98. pix[k].y = j;
  99. k++;
  100. pix[k].x = p.x + 3;
  101. pix[k].y = j;
  102. k++;
  103. }
  104. }
  105. static void graph_pixels(struct Pixel *pixels, unsigned width, unsigned height, struct Position *pix, unsigned index, unsigned nbr_planets)
  106. {
  107. /* printf("Planet: %u\n", index); */
  108. unsigned j;
  109. struct Pixel pixel = {0,0,0};
  110. for (j = 0; j < 37; j++){
  111. /* printf("X: %d, Y: %d\n", pix[j].x, pix[j].y); */
  112. if ( is_inside(pix[j], width, height) ){
  113. pixel.r = 125;
  114. pixel.b = round((255. * index) / nbr_planets);
  115. pixels[pix[j].x + pix[j].y * width] = pixel;
  116. }
  117. }
  118. }
  119. static void nbody_ppm(char *filename, struct Pixel *pixels, unsigned width, unsigned height)
  120. {
  121. unsigned i,j;
  122. FILE *myfile;
  123. myfile = fopen(filename, "w");
  124. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  125. for (i = 0; i < height; i++){
  126. for (j = 0; j < width; j++){
  127. struct Pixel pixel = pixels[j + i * width];
  128. fprintf(myfile, "%u %u %u ", pixel.r, pixel.g, pixel.b);
  129. }
  130. fprintf(myfile, "\n");
  131. }
  132. fclose(myfile);
  133. }
  134. void nbody_graph(char *filename, double *positions, unsigned nbr_planets, unsigned width, unsigned height, double min_val, double max_val)
  135. {
  136. struct Position *pix = malloc(37 * sizeof(struct Position));
  137. struct Pixel *pixels = calloc(width * height, sizeof(struct Pixel));
  138. unsigned i,j;
  139. for (i = 0; i < nbr_planets; i++){
  140. struct Position posi;
  141. posi.x = round( (positions[i] - min_val) / (max_val - min_val) * (width - 1));
  142. posi.y = round( (positions[i + nbr_planets] - min_val) / (max_val - min_val) * (width - 1));
  143. get_planet_pixels(pix, posi);
  144. graph_pixels(pixels, width, height, pix, i, nbr_planets);
  145. }
  146. nbody_ppm(filename, pixels, width, height);
  147. }
  148. void nbody_graph_transpose(char *filename, double *positions, unsigned nbr_planets, unsigned width, unsigned height, double min_val, double max_val)
  149. {
  150. struct Position *pix = malloc(37 * sizeof(struct Position));
  151. struct Pixel *pixels = calloc(width * height, sizeof(struct Pixel));
  152. unsigned i,j;
  153. for (i = 0; i < nbr_planets; i++){
  154. struct Position posi;
  155. posi.x = round( (positions[2 * i] - min_val) / (max_val - min_val) * (width - 1));
  156. posi.y = round( (positions[2 * i + 1] - min_val) / (max_val - min_val) * (width - 1));
  157. get_planet_pixels(pix, posi);
  158. graph_pixels(pixels, width, height, pix, i, nbr_planets);
  159. }
  160. nbody_ppm(filename, pixels, width, height);
  161. }