Skip to content

Commit 965fc23

Browse files
committed
expand vector math use
1 parent 802bbbe commit 965fc23

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

sources/noise/cell.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ extension _CellNoise2D
2222

2323
// 0b XXXX YYYY
2424

25-
let dp:Math.DoubleV2 = ((Double(hash >> 4 ) - 15/2) * 1/16,
26-
(Double(hash & 0b1111) - 15/2) * 1/16)
25+
let dp:Math.DoubleV2 = ((Double(hash >> 4 ) - 15 / 2) * 1 / 16,
26+
(Double(hash & 0b1111) - 15 / 2) * 1 / 16)
2727

2828
let dv:Math.DoubleV2 = Math.sub(Math.add(Math.cast_double(generating_point), dp), sample_point)
2929
return Math.dot(dv, dv)
@@ -133,7 +133,7 @@ extension _CellNoise2D
133133
// Cell group II:
134134
// within r^2 = 1.0
135135
// cumulative sample coverage = 99.96%
136-
let inner:Math.IntV2 = (near.a + quadrant.a, near.b + quadrant.b)
136+
let inner:Math.IntV2 = Math.add(near, quadrant)
137137

138138
// B points
139139
_inspect(generating_point: (inner.a, near.b), dx: nearpoint_disp.x + 0.5)
@@ -152,7 +152,7 @@ extension _CellNoise2D
152152
// Cell group III:
153153
// within r^2 = 2.0
154154
// cumulative sample coverage = 100%
155-
let outer:Math.IntV2 = (far.a - quadrant.a, far.b - quadrant.b)
155+
let outer:Math.IntV2 = Math.sub(far, quadrant)
156156

157157
// D points
158158
_inspect(generating_point: (near.a, outer.b), dy: nearpoint_disp.y - 1.5)
@@ -304,9 +304,9 @@ extension _CellNoise3D
304304
let axes:Math.DoubleV3 = Math.cast_double(( hash >> 5,
305305
hash >> 2 & 0b0111,
306306
hash << 1 & 0b0111 + ((hash >> 5 ^ hash >> 2) & 1)))
307-
let dp:Math.DoubleV3 = ((axes.x - 7.0/2.0) * 1.0/8.0,
308-
(axes.y - 7.0/2.0) * 1.0/8.0,
309-
(axes.z - 7.0/2.0) * 1.0/8.0)
307+
let dp:Math.DoubleV3 = ((axes.x - 7 / 2) * 1.0 / 8,
308+
(axes.y - 7 / 2) * 1.0 / 8,
309+
(axes.z - 7 / 2) * 1.0 / 8)
310310

311311
let dv:Math.DoubleV3 = Math.sub(Math.add(Math.cast_double(generating_point), dp), sample_point)
312312
return Math.dot(dv, dv)
@@ -377,9 +377,7 @@ extension _CellNoise3D
377377
return
378378
}
379379

380-
let generating_point:Math.IntV3 = (near.a + quadrant.a*offset.a,
381-
near.b + quadrant.b*offset.b,
382-
near.c + quadrant.c*offset.c)
380+
let generating_point:Math.IntV3 = Math.add(near, Math.mult(quadrant, offset))
383381
let dr2:Double = self.distance2(from: sample, generating_point: generating_point)
384382
if dr2 < r2
385383
{

sources/noise/gradient.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,16 @@ struct GradientNoise2D:HashedNoise
373373
(0.7, 0.7), (-0.7, 0.7), (-0.7, -0.7), (0.7, -0.7),
374374

375375
(0.7, -0.7),
376-
(1 , 0 ), ( 0 , 1 ), (-1 , 0 ), (0 , -1),
377-
(0.7, 0.7), (-0.7, 0.7), (-0.7, -0.7),
376+
(1 , 0 ), ( 0 , 1 ), (-1 , 0 ), (0, -1),
377+
(0.7, 0.7), (-0.7, 0.7), (-0.7, -0.7),
378378

379-
(-0.7, -0.7), (0.7, -0.7),
380-
(1 , 0 ), ( 0 , 1 ), (-1 , 0 ), (0 , -1),
381-
(0.7, 0.7), (-0.7, 0.7),
379+
(-0.7, -0.7), ( 0.7, -0.7),
380+
( 1 , 0 ), ( 0 , 1 ), (-1, 0), (0, -1),
381+
( 0.7, 0.7), (-0.7, 0.7),
382382

383-
(-0.7, 0.7), (-0.7, -0.7), (0.7, -0.7),
384-
(1 , 0 ), ( 0 , 1 ), (-1 , 0 ), (0 , -1),
385-
(0.7, 0.7)
383+
(-0.7, 0.7), (-0.7, -0.7), ( 0.7, -0.7),
384+
( 1 , 0 ), ( 0 , 1 ), (-1 , 0 ), (0, -1),
385+
( 0.7, 0.7)
386386
]
387387

388388
let permutation_table:PermutationTable,

sources/noise/noise.swift

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ enum Math
117117
(i2, f2):(Int, Double) = Math.fraction(v.1)
118118
return ((i1, i2), (f1, f2))
119119
}
120-
121120
@inline(__always)
122121
static
123122
func fraction(_ v:DoubleV3) -> (IntV3, DoubleV3)
@@ -134,7 +133,6 @@ enum Math
134133
{
135134
return (v1.a + v2.a, v1.b + v2.b)
136135
}
137-
138136
@inline(__always)
139137
static
140138
func add(_ v1:IntV3, _ v2:IntV3) -> IntV3
@@ -148,35 +146,58 @@ enum Math
148146
{
149147
return (v1.x + v2.x, v1.y + v2.y)
150148
}
151-
152149
@inline(__always)
153150
static
154151
func add(_ v1:DoubleV3, _ v2:DoubleV3) -> DoubleV3
155152
{
156153
return (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
157154
}
158155

156+
@inline(__always)
157+
static
158+
func sub(_ v1:IntV2, _ v2:IntV2) -> IntV2
159+
{
160+
return (v1.a - v2.a, v1.b - v2.b)
161+
}
162+
@inline(__always)
163+
static
164+
func sub(_ v1:IntV3, _ v2:IntV3) -> IntV3
165+
{
166+
return (v1.a - v2.a, v1.b - v2.b, v1.c - v2.c)
167+
}
168+
159169
@inline(__always)
160170
static
161171
func sub(_ v1:DoubleV2, _ v2:DoubleV2) -> DoubleV2
162172
{
163173
return (v1.x - v2.x, v1.y - v2.y)
164174
}
165-
166175
@inline(__always)
167176
static
168177
func sub(_ v1:DoubleV3, _ v2:DoubleV3) -> DoubleV3
169178
{
170179
return (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
171180
}
172181

182+
@inline(__always)
183+
static
184+
func mult(_ v1:IntV2, _ v2:IntV2) -> IntV2
185+
{
186+
return (v1.a * v2.a, v1.b * v2.b)
187+
}
188+
@inline(__always)
189+
static
190+
func mult(_ v1:IntV3, _ v2:IntV3) -> IntV3
191+
{
192+
return (v1.a * v2.a, v1.b * v2.b, v1.c * v2.c)
193+
}
194+
173195
@inline(__always)
174196
static
175197
func mult(_ v1:DoubleV2, _ v2:DoubleV2) -> DoubleV2
176198
{
177199
return (v1.x * v2.x, v1.y * v2.y)
178200
}
179-
180201
@inline(__always)
181202
static
182203
func mult(_ v1:DoubleV3, _ v2:DoubleV3) -> DoubleV3
@@ -190,7 +211,6 @@ enum Math
190211
{
191212
return (v1.x / v2.x, v1.y / v2.y)
192213
}
193-
194214
@inline(__always)
195215
static
196216
func div(_ v1:DoubleV3, _ v2:DoubleV3) -> DoubleV3
@@ -212,7 +232,6 @@ enum Math
212232
{
213233
return (Math.mod(v1.a, v2.a), Math.mod(v1.b, v2.b))
214234
}
215-
216235
@inline(__always)
217236
static
218237
func mod(_ v1:IntV3, _ v2:IntV3) -> IntV3
@@ -226,7 +245,6 @@ enum Math
226245
{
227246
return v1.x * v2.x + v1.y * v2.y
228247
}
229-
230248
@inline(__always)
231249
static
232250
func dot(_ v1:DoubleV3, _ v2:DoubleV3) -> Double
@@ -240,7 +258,6 @@ enum Math
240258
{
241259
return (Double(v.a), Double(v.b))
242260
}
243-
244261
@inline(__always)
245262
static
246263
func cast_double(_ v:IntV3) -> DoubleV3
@@ -269,7 +286,6 @@ enum Math
269286
{
270287
return (Math.quintic_ease(v.x), Math.quintic_ease(v.y))
271288
}
272-
273289
@inline(__always)
274290
static
275291
func quintic_ease(_ v:DoubleV3) -> DoubleV3

0 commit comments

Comments
 (0)