wl_lib_0.1.3_1c94dc7c/src/wl-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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
with Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Float_Random;

package body WL.Noise is

   procedure Normalise (Dimension : in out Dimension_Float_Buffer);

   function Interpolate
     (A, B : Float;
      X    : Unit_Real)
      return Float
   is (A + X * (B - A));

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

   function Get
     (Noise      : Perlin_Noise'Class;
      Coordinate : Noise_Vector)
      return Signed_Unit_Real
   is

      Indices : Index_Record_Array (1 .. Noise.Dimension_Count);

      Result  : Float;

   begin
      for I in Indices'Range loop
         declare
            Float_Index : constant Float :=
                            Float'Truncation (Coordinate (I));
            Index       : constant Map_Index_Type :=
                            Map_Index_Type
                              (Integer (Float_Index)
                               mod Map_Index_Type'Modulus);
            Remainder   : constant Float :=
                            Coordinate (I) - Float_Index;
            Cubic       : constant Float :=
                            Remainder ** 2 * (3.0 - 2.0 * Remainder);
         begin
            Indices (I) := (Index, Remainder, Cubic);
         end;
      end loop;

      case Noise.Dimension_Count is
         when 1 =>
            Result := Interpolate
              (Noise.Lattice
                 ((1 => Indices (1).Index), (1 => Indices (1).Remainder)),
               Noise.Lattice
                 ((1 => Indices (1).Index + 1),
                  (1 => Indices (1).Remainder - 1.0)),
               Indices (1).Cubic);
         when 2 =>
            declare
               Index_1     : constant Map_Index_Type := Indices (1).Index;
               Index_2     : constant Map_Index_Type := Indices (2).Index;
               Remainder_1 : constant Float := Indices (1).Remainder;
               Remainder_2 : constant Float := Indices (2).Remainder;
               Cubic_1     : constant Float := Indices (1).Cubic;
               Cubic_2     : constant Float := Indices (2).Cubic;
               A           : constant Float :=
                               Interpolate
                                 (Noise.Lattice
                                    ((Index_1, Index_2),
                                     (Remainder_1, Remainder_2)),
                                  Noise.Lattice
                                    ((Index_1 + 1, Index_2),
                                     (Remainder_1 - 1.0, Remainder_2)),
                                  Cubic_1);
               B           : constant Float :=
                               Interpolate
                                 (Noise.Lattice
                                    ((Index_1, Index_2 + 1),
                                     (Remainder_1, Remainder_2 - 1.0)),
                                  Noise.Lattice
                                    ((Index_1 + 1, Index_2 + 1),
                                     (Remainder_1 - 1.0, Remainder_2 - 1.0)),
                                  Cubic_1);
            begin
               Result := Interpolate (A, B, Cubic_2);
            end;

         when 3 =>
            declare
               Index_1 : constant Map_Index_Type := Indices (1).Index;
               Index_2 : constant Map_Index_Type := Indices (2).Index;
               Index_3 : constant Map_Index_Type := Indices (3).Index;
               Rem_1   : constant Float := Indices (1).Remainder;
               Rem_2   : constant Float := Indices (2).Remainder;
               Rem_3   : constant Float := Indices (3).Remainder;
               Cubic_1     : constant Float := Indices (1).Cubic;
               Cubic_2     : constant Float := Indices (2).Cubic;
               Cubic_3     : constant Float := Indices (3).Cubic;
               A_1_1       : constant Float :=
                           Noise.Lattice
                             ((Index_1, Index_2, Index_3),
                              (Rem_1, Rem_2, Rem_3));
               A_1_2   : constant Float :=
                           Noise.Lattice
                             ((Index_1 + 1, Index_2, Index_3),
                              (Rem_1 - 1.0, Rem_2, Rem_3));
               A_2_1   : constant Float :=
                           Noise.Lattice
                             ((Index_1, Index_2 + 1, Index_3),
                              (Rem_1, Rem_2 - 1.0, Rem_3));
               A_2_2   : constant Float :=
                           Noise.Lattice
                             ((Index_1 + 1, Index_2 + 1, Index_3),
                              (Rem_1 - 1.0, Rem_2 - 1.0, Rem_3));
               B_1_1   : constant Float :=
                           Noise.Lattice
                             ((Index_1, Index_2, Index_3 + 1),
                              (Rem_1, Rem_2, Rem_3 - 1.0));
               B_1_2   : constant Float :=
                           Noise.Lattice
                             ((Index_1 + 1, Index_2, Index_3 + 1),
                              (Rem_1 - 1.0, Rem_2, Rem_3 - 1.0));
               B_2_1   : constant Float :=
                           Noise.Lattice
                             ((Index_1, Index_2 + 1, Index_3 + 1),
                              (Rem_1, Rem_2 - 1.0, Rem_3 - 1.0));
               B_2_2   : constant Float :=
                           Noise.Lattice
                             ((Index_1 + 1, Index_2 + 1, Index_3 + 1),
                              (Rem_1 - 1.0, Rem_2 - 1.0, Rem_3 - 1.0));
               A_1     : constant Float :=
                           Interpolate (A_1_1, A_1_2, Cubic_1);
               A_2     : constant Float :=
                           Interpolate (A_2_1, A_2_2, Cubic_1);
               B_1     : constant Float :=
                           Interpolate (B_1_1, B_1_2, Cubic_1);
               B_2     : constant Float :=
                           Interpolate (B_2_1, B_2_2, Cubic_1);
               A       : constant Float :=
                           Interpolate (A_1, A_2, Cubic_2);
               B       : constant Float :=
                           Interpolate (B_1, B_2, Cubic_2);
            begin
               Result := Interpolate (A, B, Cubic_3);
            end;
         when others =>
            Result := 0.0;
      end case;

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

   -------------
   -- Lattice --
   -------------

   function Lattice
     (Noise      : Perlin_Noise;
      Indices    : Dimension_Index_Buffer;
      Remainders : Dimension_Float_Buffer)
      return Float
   is
      Index : Map_Index_Type := 0;
      Result : Float := 0.0;
   begin
      for I in Indices'Range loop
         Index := Noise.Map (Index + Indices (I));
      end loop;

      for I in Remainders'Range loop
         Result := Result + Noise.Buffer (Index) (I) * Remainders (I);
      end loop;

      return Result;
   end Lattice;

   ---------------
   -- Normalise --
   ---------------

   procedure Normalise (Dimension : in out Dimension_Float_Buffer) is
      use Ada.Numerics.Elementary_Functions;
      Norm : Float := 0.0;
   begin
      for X of Dimension loop
         Norm := Norm + X ** 2;
      end loop;
      Norm := 1.0 / Sqrt (Norm);
      for I in Dimension'Range loop
         Dimension (I) := Dimension (I) * Norm;
      end loop;

   end Normalise;

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

   procedure Reset
     (Noise           : in out Perlin_Noise'Class;
      Initiator       : Integer)
   is
      use Ada.Numerics.Float_Random;
      Gen : Generator;
   begin
      Reset (Gen, Initiator);

      for Index in Noise.Map'Range loop
         Noise.Map (Index) := Index;
         Noise.Buffer (Index) :=
           new Dimension_Float_Buffer (1 .. Noise.Dimension_Count);

         for Dimension in Noise.Buffer (Index)'Range loop
            Noise.Buffer (Index) (Dimension) :=
              Random (Gen) - 0.5;
         end loop;

         Normalise (Noise.Buffer (Index).all);
      end loop;

      for Index in reverse Noise.Map'Range loop
         declare
            Target_Index : constant Map_Index_Type :=
                             Map_Index_Type (Natural (Random (Gen) * 65535.0)
                                             / 256);
            Temp         : constant Map_Index_Type :=
                             Noise.Map (Index);
         begin
            Noise.Map (Index) := Noise.Map (Target_Index);
            Noise.Map (Target_Index) := Temp;
         end;
      end loop;

   end Reset;

end WL.Noise;