Line.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2013 Jason Bell
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. // DEALINGS IN THE SOFTWARE.
  21. //
  22. using System;
  23. namespace LibNoise.Models
  24. {
  25. /// <summary>
  26. /// Model that maps the output of a module onto a line.
  27. /// </summary>
  28. public class Line
  29. {
  30. /// <summary>
  31. /// The module from which to retrieve noise.
  32. /// </summary>
  33. public IModule SourceModule { get; set; }
  34. /// <summary>
  35. /// Specifies whether to attenuate the noise. If true, the noise will approach 0.0
  36. /// as the ends of the line are approached.
  37. /// </summary>
  38. public bool Attenuate { get; set; }
  39. /// @a x coordinate of the start of the line segment.
  40. double m_x0;
  41. /// @a x coordinate of the end of the line segment.
  42. double m_x1;
  43. /// @a y coordinate of the start of the line segment.
  44. double m_y0;
  45. /// @a y coordinate of the end of the line segment.
  46. double m_y1;
  47. /// @a z coordinate of the start of the line segment.
  48. double m_z0;
  49. /// @a z coordinate of the end of the line segment.
  50. double m_z1;
  51. /// <summary>
  52. /// Initialises a new instance of the Line class.
  53. /// </summary>
  54. /// <param name="sourceModule">The module from which to retrieve noise.</param>
  55. public Line(IModule sourceModule)
  56. {
  57. if (sourceModule == null)
  58. throw new ArgumentNullException("A source module must be provided.");
  59. SourceModule = sourceModule;
  60. Attenuate = true;
  61. m_x0 = 0.0;
  62. m_x1 = 1.0;
  63. m_y0 = 0.0;
  64. m_y1 = 1.0;
  65. m_z0 = 0.0;
  66. m_z1 = 1.0;
  67. }
  68. /// <summary>
  69. /// Returns noise mapped to the given point along the length of the line.
  70. /// </summary>
  71. public double GetValue(double p)
  72. {
  73. if (SourceModule == null)
  74. throw new NullReferenceException("A source module must be provided.");
  75. double x = (m_x1 - m_x0) * p + m_x0;
  76. double y = (m_y1 - m_y0) * p + m_y0;
  77. double z = (m_z1 - m_z0) * p + m_z0;
  78. double value = SourceModule.GetValue(x, y, z);
  79. if (Attenuate)
  80. {
  81. return p * (1.0 - p) * 4 * value;
  82. }
  83. else
  84. {
  85. return value;
  86. }
  87. }
  88. /// <summary>
  89. /// Sets the start point of the line in 3D space.
  90. /// </summary>
  91. public void SetStartPoint(double x, double y, double z)
  92. {
  93. m_x0 = x;
  94. m_y0 = y;
  95. m_z0 = z;
  96. }
  97. /// <summary>
  98. /// Sets the end point of the line in 3D space.
  99. /// </summary>
  100. public void SetEndPoint(double x, double y, double z)
  101. {
  102. m_x1 = x;
  103. m_y1 = y;
  104. m_z1 = z;
  105. }
  106. }
  107. }