Normalise.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace OpenSim.Terrain.BasicTerrain
  5. {
  6. static class Normalise
  7. {
  8. /// <summary>
  9. /// Converts the heightmap to values ranging from 0..1
  10. /// </summary>
  11. /// <param name="map">The heightmap to be normalised</param>
  12. public static void normalise(float[,] map)
  13. {
  14. double max = findMax(map);
  15. double min = findMin(map);
  16. int w = map.GetLength(0);
  17. int h = map.GetLength(1);
  18. int x, y;
  19. for (x = 0; x < w; x++)
  20. {
  21. for (y = 0; y < h; y++)
  22. {
  23. map[x, y] = (float)((map[x, y] - min) * (1.0 / (max - min)));
  24. }
  25. }
  26. }
  27. /// <summary>
  28. /// Converts the heightmap to values ranging from 0..<newmax>
  29. /// </summary>
  30. /// <param name="map">The heightmap to be normalised</param>
  31. /// <param name="newmax">The new maximum height value of the map</param>
  32. public static void normalise(float[,] map, double newmax)
  33. {
  34. double max = findMax(map);
  35. double min = findMin(map);
  36. int w = map.GetLength(0);
  37. int h = map.GetLength(1);
  38. int x, y;
  39. for (x = 0; x < w; x++)
  40. {
  41. for (y = 0; y < h; y++)
  42. {
  43. map[x, y] = (float)((map[x, y] - min) * (1.0 / (max - min)) * newmax);
  44. }
  45. }
  46. }
  47. /// <summary>
  48. /// Finds the largest value in the heightmap
  49. /// </summary>
  50. /// <param name="map">The heightmap</param>
  51. /// <returns>The highest value</returns>
  52. public static double findMax(float[,] map)
  53. {
  54. int x, y;
  55. int w = map.GetLength(0);
  56. int h = map.GetLength(1);
  57. double max = double.MinValue;
  58. for (x = 0; x < w; x++)
  59. {
  60. for (y = 0; y < h; y++)
  61. {
  62. if (map[x, y] > max)
  63. max = map[x, y];
  64. }
  65. }
  66. return max;
  67. }
  68. /// <summary>
  69. /// Finds the lowest value in a heightmap
  70. /// </summary>
  71. /// <param name="map">The heightmap</param>
  72. /// <returns>The minimum value</returns>
  73. public static double findMin(float[,] map)
  74. {
  75. int x, y;
  76. int w = map.GetLength(0);
  77. int h = map.GetLength(1);
  78. double min = double.MaxValue;
  79. for (x = 0; x < w; x++)
  80. {
  81. for (y = 0; y < h; y++)
  82. {
  83. if (map[x, y] < min)
  84. min = map[x, y];
  85. }
  86. }
  87. return min;
  88. }
  89. }
  90. }