wl_lib_0.1.3_1c94dc7c/src/wl-brownian_noise.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
with Ada.Numerics.Elementary_Functions;

package body WL.Brownian_Noise is

   ---------
   -- Get --
   ---------

   function Get
     (Noise      : Brownian_Noise_Type'Class;
      Coordinate : WL.Noise.Noise_Vector;
      Octave     : Float)
      return Signed_Unit_Real
   is
      Result : Float := 0.0;
      Temp   : WL.Noise.Noise_Vector := Coordinate;
      Last_Octave : constant Octave_Range :=
                      Octave_Range (Octave);
   begin
      for I in 1 .. Last_Octave loop
         Result := Result + Noise.Get (Temp) * Noise.Exponents (I);
         for J in Temp'Range loop
            Temp (J) := Temp (J) * Noise.Lacunarity;
         end loop;
      end loop;

      declare
         Remainder : constant Float :=
                       Octave - Float'Truncation (Octave);
      begin
         if Remainder > 0.0 then
            Result := Result
              + Remainder * Noise.Get (Temp)
              * Noise.Exponents (Last_Octave + 1);
         end if;
      end;

      return (if Result < -1.0
              then -1.0
              elsif Result > 1.0
              then 1.0
              else Result);

   end Get;

   -----------
   -- Reset --
   -----------

   procedure Reset
     (Noise       : in out Brownian_Noise_Type'Class;
      Initiator   : Integer;
      Roughness   : Unit_Real;
      Lacunarity  : Float)
   is
      use Ada.Numerics.Elementary_Functions;
      F : Float := 1.0;
   begin
      WL.Noise.Perlin_Noise (Noise).Reset (Initiator);
      Noise.Roughness := Roughness;
      Noise.Lacunarity := Lacunarity;
      for I in Noise.Exponents'Range loop
         Noise.Exponents (I) :=
           F ** (-Roughness);
         F := F * Lacunarity;
      end loop;

   end Reset;

end WL.Brownian_Noise;