@@ -170,6 +170,34 @@ enum Math
170
170
return ( v1. x - v2. x, v1. y - v2. y, v1. z - v2. z)
171
171
}
172
172
173
+ @inline ( __always)
174
+ static
175
+ func mult( _ v1: DoubleV2 , _ v2: DoubleV2 ) -> DoubleV2
176
+ {
177
+ return ( v1. x * v2. x, v1. y * v2. y)
178
+ }
179
+
180
+ @inline ( __always)
181
+ static
182
+ func mult( _ v1: DoubleV3 , _ v2: DoubleV3 ) -> DoubleV3
183
+ {
184
+ return ( v1. x * v2. x, v1. y * v2. y, v1. z * v2. z)
185
+ }
186
+
187
+ @inline ( __always)
188
+ static
189
+ func div( _ v1: DoubleV2 , _ v2: DoubleV2 ) -> DoubleV2
190
+ {
191
+ return ( v1. x / v2. x, v1. y / v2. y)
192
+ }
193
+
194
+ @inline ( __always)
195
+ static
196
+ func div( _ v1: DoubleV3 , _ v2: DoubleV3 ) -> DoubleV3
197
+ {
198
+ return ( v1. x / v2. x, v1. y / v2. y, v1. z / v2. z)
199
+ }
200
+
173
201
@inline ( __always)
174
202
private static
175
203
func mod( _ x: Int , _ n: Int ) -> Int
@@ -255,72 +283,62 @@ public
255
283
struct Domain2D : Sequence
256
284
{
257
285
private
258
- let i_max : Double ,
259
- j_max : Double ,
260
-
261
- dx : Double ,
262
- dy : Double ,
263
- i0 : Double ,
264
- j0 : Double
286
+ let sample_lower_bound : Math . DoubleV2 ,
287
+ sample_upper_bound : Math . DoubleV2 ,
288
+ increment : Math . DoubleV2
265
289
266
290
public
267
291
struct Iterator : IteratorProtocol
268
292
{
269
293
private
270
- var i : Double = - 0.5 ,
271
- j : Double = 0.5
294
+ var sample : Math . DoubleV2
272
295
273
296
private
274
297
let domain : Domain2D
275
298
276
299
init ( _ domain: Domain2D )
277
300
{
278
- self . i = domain. i0 - 0.5
279
- self . j = domain. j0 + 0.5
280
-
301
+ self . sample = Math . add ( domain. sample_lower_bound, ( - 0.5 , 0.5 ) )
281
302
self . domain = domain
282
303
}
283
304
284
305
public mutating
285
306
func next( ) -> ( Double , Double ) ?
286
307
{
287
- self . i += 1
288
- if self . i >= self . domain. i_max
308
+ self . sample . x += 1
309
+ if self . sample . x >= self . domain. sample_upper_bound . x
289
310
{
290
- self . i = self . domain. i0 + 0.5
291
- self . j += 1
292
- if self . j >= self . domain. j_max
311
+ self . sample . x = self . domain. sample_lower_bound . x + 0.5
312
+ self . sample . y += 1
313
+ if self . sample . y >= self . domain. sample_upper_bound . y
293
314
{
294
315
return nil
295
316
}
296
317
}
297
318
298
- return ( self . domain. dx * self . i , self . domain . dy * self . j )
319
+ return Math . mult ( self . domain. increment , self . sample )
299
320
}
300
321
}
301
322
302
323
public
303
324
init ( samples_x: Int , samples_y: Int )
304
325
{
305
- self . dx = 1
306
- self . dy = 1
307
- self . i0 = 0
308
- self . j0 = 0
309
-
310
- self . i_max = Double ( samples_x)
311
- self . j_max = Double ( samples_y)
326
+ self . increment = ( 1 , 1 )
327
+ self . sample_lower_bound = ( 0 , 0 )
328
+ self . sample_upper_bound = Math . cast_double ( ( samples_x, samples_y) )
312
329
}
313
330
314
331
public
315
332
init ( _ x_range: ClosedRange < Double > , _ y_range: ClosedRange < Double > , samples_x: Int , samples_y: Int )
316
333
{
317
- self . dx = ( x_range . upperBound - x_range . lowerBound ) / Double ( samples_x)
318
- self . dy = ( y_range . upperBound - y_range. lowerBound) / Double ( samples_y )
319
- self . i0 = x_range . lowerBound * Double ( samples_x ) / ( x_range. upperBound - x_range . lowerBound )
320
- self . j0 = y_range . lowerBound * Double ( samples_y ) / ( y_range . upperBound - y_range . lowerBound )
334
+ let sample_count : Math . DoubleV2 = Math . cast_double ( ( samples_x, samples_y ) ) ,
335
+ range_lower_bound : Math . DoubleV2 = ( x_range . lowerBound , y_range. lowerBound) ,
336
+ range_upper_bound : Math . DoubleV2 = ( x_range. upperBound, y_range . upperBound ) ,
337
+ range_difference : Math . DoubleV2 = Math . sub ( range_upper_bound , range_lower_bound )
321
338
322
- self . i_max = self . i0 + Double( samples_x)
323
- self . j_max = self . j0 + Double( samples_y)
339
+ self . increment = Math . div ( range_difference, sample_count)
340
+ self . sample_lower_bound = Math . div ( Math . mult ( range_lower_bound, sample_count) , range_difference)
341
+ self . sample_upper_bound = Math . add ( self . sample_lower_bound, sample_count)
324
342
}
325
343
326
344
public
@@ -335,89 +353,68 @@ public
335
353
struct Domain3D : Sequence
336
354
{
337
355
private
338
- let i_max : Double ,
339
- j_max : Double ,
340
- k_max : Double ,
341
-
342
- dx : Double ,
343
- dy : Double ,
344
- dz : Double ,
345
- i0 : Double ,
346
- j0 : Double ,
347
- k0 : Double
356
+ let sample_lower_bound : Math . DoubleV3 ,
357
+ sample_upper_bound : Math . DoubleV3 ,
358
+ increment : Math . DoubleV3
348
359
349
360
public
350
361
struct Iterator : IteratorProtocol
351
362
{
352
363
private
353
- var i : Double = - 0.5 ,
354
- j : Double = 0.5 ,
355
- k : Double = 0.5
364
+ var sample : Math . DoubleV3
356
365
357
366
private
358
367
let domain : Domain3D
359
368
360
369
init ( _ domain: Domain3D )
361
370
{
362
- self . i = domain. i0 - 0.5
363
- self . j = domain. j0 + 0.5
364
- self . k = domain. k0 + 0.5
365
-
371
+ self . sample = Math . add ( domain. sample_lower_bound, ( - 0.5 , 0.5 , 0.5 ) )
366
372
self . domain = domain
367
373
}
368
374
369
375
public mutating
370
376
func next( ) -> ( Double , Double , Double ) ?
371
377
{
372
- self . i += 1
373
- if self . i >= self . domain. i_max
378
+ self . sample . x += 1
379
+ if self . sample . x >= self . domain. sample_upper_bound . x
374
380
{
375
- self . i = self . domain. i0 + 0.5
376
- self . j += 1
377
- if self . j >= self . domain. j_max
381
+ self . sample . x = self . domain. sample_lower_bound . x + 0.5
382
+ self . sample . y += 1
383
+ if self . sample . y >= self . domain. sample_upper_bound . y
378
384
{
379
- self . j = self . domain. j0 + 0.5
380
- self . k += 1
381
- if self . k >= self . domain. k_max
385
+ self . sample . y = self . domain. sample_lower_bound . y + 0.5
386
+ self . sample . z += 1
387
+ if self . sample . z >= self . domain. sample_upper_bound . z
382
388
{
383
389
return nil
384
390
}
385
391
}
386
392
}
387
393
388
- return ( self . domain. dx * self . i , self . domain . dy * self . j , self . domain . dz * self . k )
394
+ return Math . mult ( self . domain. increment , self . sample )
389
395
}
390
396
}
391
397
392
398
public
393
399
init ( samples_x: Int , samples_y: Int , samples_z: Int )
394
400
{
395
- self . dx = 1
396
- self . dy = 1
397
- self . dz = 1
398
- self . i0 = 0
399
- self . j0 = 0
400
- self . k0 = 0
401
-
402
- self . i_max = Double ( samples_x)
403
- self . j_max = Double ( samples_y)
404
- self . k_max = Double ( samples_z)
401
+ self . increment = ( 1 , 1 , 1 )
402
+ self . sample_lower_bound = ( 0 , 0 , 0 )
403
+ self . sample_upper_bound = Math . cast_double ( ( samples_x, samples_y, samples_z) )
405
404
}
406
405
407
406
public
408
407
init ( _ x_range: ClosedRange < Double > , _ y_range: ClosedRange < Double > , _ z_range: ClosedRange < Double > ,
409
408
samples_x: Int , samples_y: Int , samples_z: Int )
410
409
{
411
- self . dx = ( x_range. upperBound - x_range. lowerBound) / Double( samples_x)
412
- self . dy = ( y_range. upperBound - y_range. lowerBound) / Double( samples_y)
413
- self . dz = ( z_range. upperBound - z_range. lowerBound) / Double( samples_z)
414
- self . i0 = x_range. lowerBound * Double( samples_x) / ( x_range. upperBound - x_range. lowerBound)
415
- self . j0 = y_range. lowerBound * Double( samples_y) / ( y_range. upperBound - y_range. lowerBound)
416
- self . k0 = z_range. lowerBound * Double( samples_z) / ( z_range. upperBound - z_range. lowerBound)
410
+ let sample_count : Math . DoubleV3 = Math . cast_double ( ( samples_x, samples_y, samples_z) ) ,
411
+ range_lower_bound : Math . DoubleV3 = ( x_range. lowerBound, y_range. lowerBound, z_range. lowerBound) ,
412
+ range_upper_bound : Math . DoubleV3 = ( x_range. upperBound, y_range. upperBound, z_range. upperBound) ,
413
+ range_difference : Math . DoubleV3 = Math . sub ( range_upper_bound, range_lower_bound)
417
414
418
- self . i_max = self . i0 + Double ( samples_x )
419
- self . j_max = self . j0 + Double ( samples_y )
420
- self . k_max = self . k0 + Double ( samples_z )
415
+ self . increment = Math . div ( range_difference , sample_count )
416
+ self . sample_lower_bound = Math . div ( Math . mult ( range_lower_bound , sample_count ) , range_difference )
417
+ self . sample_upper_bound = Math . add ( self . sample_lower_bound , sample_count )
421
418
}
422
419
423
420
public
0 commit comments