spec_syn.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Updated by Michel Wurtz 12/99 */
  2. /*****************************************************************************/
  3. /*** ***/
  4. /*** spec_syn() ***/
  5. /*** Creates a fractal surface using spectral synthesis. ***/
  6. /*** Algorithm adapted from Peitgen and Sauper (1988), p.108. ***/
  7. /*** Jo Wood, V1.0, 19th October, 1994 ***/
  8. /*** Modified to allow multiple realisations of same surface, ***/
  9. /*** Jo Wood, V1.1 15th October, 1995. ***/
  10. /*** ***/
  11. /*****************************************************************************/
  12. #include <unistd.h>
  13. #include <math.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. #include "frac.h"
  17. #include <grass/gmath.h>
  18. int specsyn(double *data[2], /* Array holding complex data to transform. */
  19. int nn /* Size of array in one dimension. */
  20. )
  21. {
  22. /* ---------------------------------------------------------------- */
  23. /* --- Initialise --- */
  24. /* ---------------------------------------------------------------- */
  25. int row, col, /* Counts through half the data array. */
  26. row0, col0, /* 'other half' of the array. */
  27. coeff; /* No. of Fourier coeffficents to calc. */
  28. double phase, rad, /* polar coordinates of Fourier coeff. */
  29. *temp[2];
  30. /* FIXME - allow seed to be specified for repeatability */
  31. G_math_srand_auto(); /* Reset random number generator. */
  32. temp[0] = (double *)G_malloc(nn * nn * sizeof(double));
  33. temp[1] = (double *)G_malloc(nn * nn * sizeof(double));
  34. /* ---------------------------------------------------------------- */
  35. /* --- Create fractal surface --- */
  36. /* ---------------------------------------------------------------- */
  37. /* Calculate all the preliminary random coefficients. */
  38. /* ================================================== */
  39. G_message(_("Preliminary surface calculations..."));
  40. data_reset(data, nn);
  41. for (row = 0; row <= nn / 2; row++)
  42. for (col = 0; col <= nn / 2; col++) {
  43. /* Generate random Fourier coefficients. */
  44. phase = TWOPI * G_math_rand();
  45. if ((row != 0) || (col != 0))
  46. rad =
  47. pow(row * row + col * col,
  48. -(H + 1) / 2.0) * G_math_rand_gauss(1.);
  49. else
  50. rad = 0.0;
  51. /* Fill half the array with coefficients. */
  52. *(data[0] + row * nn + col) = rad * cos(phase);
  53. *(data[1] + row * nn + col) = rad * sin(phase);
  54. /* Fill other half of array with coefficients. */
  55. if (row == 0)
  56. row0 = 0;
  57. else
  58. row0 = nn - row;
  59. if (col == 0)
  60. col0 = 0;
  61. else
  62. col0 = nn - col;
  63. *(data[0] + row0 * nn + col0) = rad * cos(phase);
  64. *(data[1] + row0 * nn + col0) = -rad * sin(phase);
  65. }
  66. *(temp[1] + nn / 2) = 0.0;
  67. *(temp[1] + nn * nn / 2) = 0.0;
  68. *(temp[1] + nn * nn / 2 + nn / 2) = 0.0;
  69. for (row = 1; row < nn / 2; row++)
  70. for (col = 1; col < nn / 2; col++) {
  71. phase = TWOPI * G_math_rand();
  72. rad =
  73. pow(row * row + col * col,
  74. -(H + 1) / 2.0) * G_math_rand_gauss(1.);
  75. *(data[0] + row * nn + nn - col) = rad * cos(phase);
  76. *(data[1] + row * nn + nn - col) = rad * sin(phase);
  77. *(data[0] + (nn - row) * nn + col) = rad * cos(phase);
  78. *(data[1] + (nn - row) * nn + col) = -rad * sin(phase);
  79. }
  80. /* Transfer random coeffients to array before ifft transform */
  81. /* ========================================================= */
  82. for (coeff = 0; coeff < Steps; coeff++) {
  83. G_message(_("Calculating surface %d (of %d)..."), coeff + 1, Steps);
  84. data_reset(temp, nn);
  85. for (row = 0; row <= (coeff + 1) * nn / (Steps * 2); row++)
  86. for (col = 0; col <= (coeff + 1) * nn / (Steps * 2); col++) {
  87. if (row == 0)
  88. row0 = 0;
  89. else
  90. row0 = nn - row;
  91. if (col == 0)
  92. col0 = 0;
  93. else
  94. col0 = nn - col;
  95. *(temp[0] + row * nn + col) = *(data[0] + row * nn + col);
  96. *(temp[1] + row * nn + col) = *(data[1] + row * nn + col);
  97. *(temp[0] + row0 * nn + col0) = *(data[0] + row0 * nn + col0);
  98. *(temp[1] + row0 * nn + col0) = *(data[1] + row0 * nn + col0);
  99. }
  100. for (row = 1; row < (coeff + 1) * nn / (Steps * 2); row++)
  101. for (col = 1; col < (coeff + 1) * nn / (Steps * 2); col++) {
  102. *(temp[0] + row * nn + nn - col) =
  103. *(data[0] + row * nn + nn - col);
  104. *(temp[1] + row * nn + nn - col) =
  105. *(data[1] + row * nn + nn - col);
  106. *(temp[0] + (nn - row) * nn + col) =
  107. *(data[0] + (nn - row) * nn + col);
  108. *(temp[1] + (nn - row) * nn + col) =
  109. *(data[1] + (nn - row) * nn + col);
  110. }
  111. fft(1, temp, nn * nn, nn, nn); /* Perform inverse FFT and */
  112. write_rast(temp, nn, coeff + 1); /* write out raster. */
  113. }
  114. /* Free memory. */
  115. /* ============ */
  116. G_free(temp[0]);
  117. G_free(temp[1]);
  118. return 0;
  119. }