plot2d.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. 2D FDTD simulator
  3. Copyright (C) 2019 Emilia Blåsten
  4. This program is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Affero General Public License
  6. as published by the Free Software Foundation, either version 3 of
  7. the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public
  13. License along with this program. If not, see
  14. <http://www.gnu.org/licenses/>.
  15. */
  16. #include <SDL2/SDL.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include "plotWindow.h"
  20. #include "plot2d.h"
  21. Uint32 value2colour(plotWindow *plotter, double val) {
  22. double t;
  23. SDL_PixelFormat *format;
  24. format = plotter->surface->format;
  25. double red1 = plotter->red1;
  26. double green1 = plotter->green1;
  27. double blue1 = plotter->blue1;
  28. double red2 = plotter->red2;
  29. double green2 = plotter->green2;
  30. double blue2 = plotter->blue2;
  31. double maxf = plotter->maxf;
  32. double minf = plotter->minf;
  33. if( val >= 0 ) {
  34. val = fmin(val,maxf);
  35. t = val/maxf;
  36. return SDL_MapRGB( format, (Uint8)(t*red2+(1-t)*255),
  37. (Uint8)(t*green2+(1-t)*255), (Uint8)(t*blue2+(1-t)*255) );
  38. } else {
  39. val = fmax(val,minf);
  40. t = val/minf;
  41. return SDL_MapRGB( format, (Uint8)(t*red1+(1-t)*255),
  42. (Uint8)(t*green1+(1-t)*255), (Uint8)(t*blue1+(1-t)*255));
  43. }
  44. }
  45. void plot2d(double **array, int sizeX, int sizeY, plotWindow *plotter) {
  46. if(SDL_MUSTLOCK(plotter->surface)) {
  47. if(SDL_LockSurface(plotter->surface) < 0) return;
  48. }
  49. int width = plotter->surface->w;
  50. if(sizeX != width) {
  51. fprintf(stderr, "plotter width not same as sizeX.\n");
  52. exit(-1);
  53. }
  54. int mm, nn;
  55. for(mm = 0; mm < sizeX; mm++) {
  56. for(nn = 0; nn < sizeY; nn++) {
  57. ((Uint32*) plotter->surface->pixels)[nn*width + mm] =
  58. value2colour(plotter, array[mm][nn]);
  59. }
  60. }
  61. if(SDL_MUSTLOCK(plotter->surface)) SDL_UnlockSurface(plotter->surface);
  62. // Next, draw the pixels onto the window
  63. SDL_UpdateTexture(plotter->texture, NULL, plotter->surface->pixels,
  64. plotter->surface->pitch);
  65. SDL_RenderClear(plotter->renderer);
  66. SDL_RenderCopy(plotter->renderer, plotter->texture, NULL, NULL);
  67. SDL_RenderPresent(plotter->renderer);
  68. return;
  69. }
  70. void plot2dLinComb(double a1, double **array1, double a2, double **array2,
  71. int sizeX, int sizeY, plotWindow *plotter) {
  72. if(SDL_MUSTLOCK(plotter->surface)) {
  73. if(SDL_LockSurface(plotter->surface) < 0) return;
  74. }
  75. int width = plotter->surface->w;
  76. if(sizeX != width) {
  77. fprintf(stderr, "plotter width not same as sizeX.\n");
  78. exit(-1);
  79. }
  80. int mm, nn;
  81. for(mm = 0; mm < sizeX; mm++) {
  82. for(nn = 0; nn < sizeY; nn++) {
  83. ((Uint32*) plotter->surface->pixels)[nn*width + mm] =
  84. value2colour(plotter, a1*array1[mm][nn] + a2*array2[mm][nn]);
  85. }
  86. }
  87. if(SDL_MUSTLOCK(plotter->surface)) SDL_UnlockSurface(plotter->surface);
  88. // Next, draw the pixels onto the window
  89. SDL_UpdateTexture(plotter->texture, NULL,
  90. plotter->surface->pixels, plotter->surface->pitch);
  91. SDL_RenderClear(plotter->renderer);
  92. SDL_RenderCopy(plotter->renderer, plotter->texture, NULL, NULL);
  93. SDL_RenderPresent(plotter->renderer);
  94. return;
  95. }