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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright 2015 The Noise-rs Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use {math, Seed};
use num::Float;

#[inline(always)]
fn get_cell_point2<T: Float>(seed: &Seed, cell: math::Point2<T>) -> math::Point2<T> {
    let val = seed.get2(math::cast2::<_, i64>(cell));
    math::add2(cell, math::mul2(math::cast2([val & 0x0F, (val & 0xF0) >> 4]), math::cast(1.0 / 15.0)))
}

#[inline(always)]
fn get_cell_point3<T: Float>(seed: &Seed, cell: math::Point3<T>) -> math::Point3<T> {
    let cell_int = math::cast3::<_, i64>(cell);
    let val1 = seed.get3(cell_int);
    let val2 = seed.get3([cell_int[0], cell_int[1], cell_int[2] + 128]);
    math::add3(cell, math::mul3(math::cast3([val1 & 0x0F, (val1 & 0xF0) >> 4, val2 & 0x0F]), math::cast(1.0 / 15.0)))
}

#[inline(always)]
fn get_cell_point4<T: Float>(seed: &Seed, cell: math::Point4<T>) -> math::Point4<T> {
    let cell_int = math::cast4::<_, i64>(cell);
    let val1 = seed.get4(cell_int);
    let val2 = seed.get4([cell_int[0], cell_int[1], cell_int[2], cell_int[3] + 128]);
    math::add4(cell, math::mul4(math::cast4([val1 & 0x0F, (val1 & 0xF0) >> 4, val2 & 0x0F, (val2 & 0xF0) >> 4]), math::cast(1.0 / 15.0)))
}

#[inline(always)]
pub fn range_sqr_euclidian2<T: Float>(p1: math::Point2<T>, p2: math::Point2<T>) -> T {
    let offset = math::sub2(p1, p2);
    math::dot2(offset, offset)
}

#[inline(always)]
pub fn range_sqr_euclidian3<T: Float>(p1: math::Point3<T>, p2: math::Point3<T>) -> T {
    let offset = math::sub3(p1, p2);
    math::dot3(offset, offset)
}

#[inline(always)]
pub fn range_sqr_euclidian4<T: Float>(p1: math::Point4<T>, p2: math::Point4<T>) -> T {
    let offset = math::sub4(p1, p2);
    math::dot4(offset, offset)
}

#[inline(always)]
pub fn range_manhattan2<T: Float>(p1: math::Point2<T>, p2: math::Point2<T>) -> T {
    let offset = math::sub2(p1, p2);
    offset[0].abs() + offset[1].abs()
}

#[inline(always)]
pub fn range_manhattan3<T: Float>(p1: math::Point3<T>, p2: math::Point3<T>) -> T {
    let offset = math::sub3(p1, p2);
    offset[0].abs() + offset[1].abs() + offset[2].abs()
}

#[inline(always)]
pub fn range_manhattan4<T: Float>(p1: math::Point4<T>, p2: math::Point4<T>) -> T {
    let offset = math::sub4(p1, p2);
    offset[0].abs() + offset[1].abs() + offset[2].abs() + offset[3].abs()
}

#[inline(always)]
pub fn get_vec2<T: Float>(index: usize) -> math::Point2<T> {
    let length = math::cast::<_,T>((index & 0xF8) >> 3) * math::cast(0.5 / 31.0);
    let diag = length * math::cast(0.70710678118);
    let one = length;
    let zero: T = math::cast(0.0);
    match index & 0x07 {
        0 => [ diag,  diag],
        1 => [ diag, -diag],
        2 => [-diag,  diag],
        3 => [-diag, -diag],
        4 => [ one,   zero],
        5 => [-one,   zero],
        6 => [ zero,  one],
        7 => [ zero, -one],
        _ => unreachable!(),
    }
}

#[inline(always)]
pub fn get_vec3<T: Float>(index: usize) -> math::Point3<T> {
    let length = math::cast::<_,T>((index & 0xE0) >> 5) * math::cast(0.5 / 7.0);
    let diag = length * math::cast(0.70710678118f32);
    let one = length;
    let zero = math::cast(0.0);
    match index % 18 {
        0  => [ diag,  diag,  zero],
        1  => [ diag, -diag,  zero],
        2  => [-diag,  diag,  zero],
        3  => [-diag, -diag,  zero],
        4  => [ diag,  zero,  diag],
        5  => [ diag,  zero, -diag],
        6  => [-diag,  zero,  diag],
        7  => [-diag,  zero, -diag],
        8  => [ zero,  diag,  diag],
        9  => [ zero,  diag, -diag],
        10 => [ zero, -diag,  diag],
        11 => [ zero, -diag, -diag],
        12 => [ one,   zero,  zero],
        13 => [ zero,  one,   zero],
        14 => [ zero,  zero,  one],
        15 => [-one,   zero,  zero],
        16 => [ zero, -one,   zero],
        17 => [ zero,  zero, -one],
        _ => panic!("Attempt to access 3D gradient {} of 12", index % 12),
    }
}

#[inline(always)]
pub fn get_vec4<T: Float>(index: usize) -> math::Point4<T> {
    let length = math::cast::<_,T>((index & 0xE0) >> 5) * math::cast(0.5 / 7.0);
    let diag = length * math::cast(0.57735026919);
    let zero = math::cast(0.0);
    match index % 32 {
        0  => [ diag,  diag,  diag,  zero],
        1  => [ diag, -diag,  diag,  zero],
        2  => [-diag,  diag,  diag,  zero],
        3  => [-diag, -diag,  diag,  zero],
        4  => [ diag,  diag, -diag,  zero],
        5  => [ diag, -diag, -diag,  zero],
        6  => [-diag,  diag, -diag,  zero],
        7  => [-diag, -diag, -diag,  zero],
        8  => [ diag,  diag,  zero,  diag],
        9  => [ diag, -diag,  zero,  diag],
        10 => [-diag,  diag,  zero,  diag],
        11 => [-diag, -diag,  zero,  diag],
        12 => [ diag,  diag,  zero, -diag],
        13 => [ diag, -diag,  zero, -diag],
        14 => [-diag,  diag,  zero, -diag],
        15 => [-diag, -diag,  zero, -diag],
        16 => [ diag,  zero,  diag,  diag],
        17 => [ diag,  zero, -diag,  diag],
        18 => [-diag,  zero,  diag,  diag],
        19 => [-diag,  zero, -diag,  diag],
        20 => [ diag,  zero,  diag, -diag],
        21 => [ diag,  zero, -diag, -diag],
        22 => [-diag,  zero,  diag, -diag],
        23 => [-diag,  zero, -diag, -diag],
        24 => [ zero,  diag,  diag,  diag],
        25 => [ zero,  diag, -diag,  diag],
        26 => [ zero, -diag,  diag,  diag],
        27 => [ zero, -diag, -diag,  diag],
        28 => [ zero,  diag,  diag, -diag],
        29 => [ zero,  diag, -diag, -diag],
        30 => [ zero, -diag,  diag, -diag],
        31 => [ zero, -diag, -diag, -diag],
        _ => panic!("Attempt to access 4D gradient {} of 32", index % 32),
    }
}

#[inline(always)]
fn cell2_seed<T, F>(seed: &Seed, point: &math::Point2<T>, range_func: F) -> (math::Point2<i64>, math::Point2<T>, T)
    where T: Float, F: Fn(math::Point2<T>, math::Point2<T>) -> T
{
    #[inline(always)]
    fn get_point<T: Float>(seed: &Seed, whole: math::Point2<i64>) -> math::Point2<T> {
        math::add2(get_vec2(seed.get2(whole)), math::cast2::<_, T>(whole))
    }

    let half: T = math::cast(0.5);

    let floored = math::map2(*point, Float::floor);
    let whole   = math::map2(floored, math::cast::<_, i64>);
    let frac    = math::sub2(*point, floored);

    let x_half = frac[0] > half;
    let y_half = frac[1] > half;

    let near = [whole[0] + (x_half as i64), whole[1] + (y_half as i64)];
    let far = [whole[0] + (!x_half as i64), whole[1] + (!y_half as i64)];

    let mut seed_cell = near;
    let mut seed_point = get_point(seed, near);
    let mut range = range_func(*point, seed_point);

    let x_range = (half - frac[0]) * (half - frac[0]); // x-distance squared to center line
    let y_range = (half - frac[1]) * (half - frac[1]); // y-distance squared to center line

    macro_rules! test_point(
        [$x:expr, $y:expr] => {
            {
                let cur_point = get_point(seed, [$x, $y]);
                let cur_range = range_func(*point, cur_point);
                if cur_range < range {
                    range = cur_range;
                    seed_cell = [$x, $y];
                    seed_point = cur_point;
                }
            }
        }
    );

    if x_range < range { test_point![far[0], near[1]]; }
    if y_range < range { test_point![near[0], far[1]]; }

    if x_range < range && y_range < range { test_point![far[0], far[1]]; }

    (seed_cell, seed_point, range)
}

#[inline(always)]
fn cell3_seed<T, F>(seed: &Seed, point: &math::Point3<T>, range_func: F) -> (math::Point3<i64>, math::Point3<T>, T)
    where T: Float, F: Fn(math::Point3<T>, math::Point3<T>) -> T
{
    #[inline(always)]
    fn get_point<T: Float>(seed: &Seed, whole: math::Point3<i64>) -> math::Point3<T> {
        math::add3(get_vec3(seed.get3(whole)), math::cast3::<_, T>(whole))
    }

    let half: T = math::cast(0.5);

    let floored = math::map3(*point, Float::floor);
    let whole   = math::map3(floored, math::cast::<_, i64>);
    let frac    = math::sub3(*point, floored);

    let x_half = frac[0] > half;
    let y_half = frac[1] > half;
    let z_half = frac[2] > half;

    let near = [whole[0] + (x_half as i64), whole[1] + (y_half as i64), whole[2] + (z_half as i64)];
    let far = [whole[0] + (!x_half as i64), whole[1] + (!y_half as i64), whole[2] + (!z_half as i64)];

    let mut seed_cell = near;
    let mut seed_point = get_point(seed, near);
    let mut range = range_func(*point, seed_point);

    let x_range = (half - frac[0]) * (half - frac[0]); // x-distance squared to center line
    let y_range = (half - frac[1]) * (half - frac[1]); // y-distance squared to center line
    let z_range = (half - frac[2]) * (half - frac[2]); // z-distance squared to center line

    macro_rules! test_point(
        [$x:expr, $y:expr, $z:expr] => {
            {
                let cur_point = get_point(seed, [$x, $y, $z]);
                let cur_range = range_func(*point, cur_point);
                if cur_range < range {
                    range = cur_range;
                    seed_cell = [$x, $y, $z];
                    seed_point = cur_point;
                }
            }
        }
    );

    if x_range < range { test_point![far[0], near[1], near[2]]; }
    if y_range < range { test_point![near[0], far[1], near[2]]; }
    if z_range < range { test_point![near[0], near[1], far[2]]; }

    if x_range < range && y_range < range { test_point![far[0], far[1], near[2]]; }
    if x_range < range && z_range < range { test_point![far[0], near[1], far[2]]; }
    if y_range < range && z_range < range { test_point![near[0], far[1], far[2]]; }

    if x_range < range && y_range < range && z_range < range { test_point![far[0], far[1], far[2]]; }

    (seed_cell, seed_point, range)
}

#[inline(always)]
fn cell4_seed<T, F>(seed: &Seed, point: &math::Point4<T>, range_func: F) -> (math::Point4<i64>, math::Point4<T>, T)
    where T: Float, F: Fn(math::Point4<T>, math::Point4<T>) -> T
{
    #[inline(always)]
    fn get_point<T: Float>(seed: &Seed, whole: math::Point4<i64>) -> math::Point4<T> {
        math::add4(get_vec4(seed.get4(whole)), math::cast4::<_, T>(whole))
    }

    let half: T = math::cast(0.5);

    let floored = math::map4(*point, Float::floor);
    let whole   = math::map4(floored, math::cast::<_, i64>);
    let frac    = math::sub4(*point, floored);

    let x_half = frac[0] > half;
    let y_half = frac[1] > half;
    let z_half = frac[2] > half;
    let w_half = frac[3] > half;

    let near = [whole[0] + (x_half as i64), whole[1] + (y_half as i64), whole[2] + (z_half as i64), whole[3] + (w_half as i64)];
    let far = [whole[0] + (!x_half as i64), whole[1] + (!y_half as i64), whole[2] + (!z_half as i64), whole[3] + (!w_half as i64)];

    let mut seed_cell = near;
    let mut seed_point = get_point(seed, near);
    let mut range = range_func(*point, seed_point);

    let x_range = (half - frac[0]) * (half - frac[0]); // x-distance squared to center line
    let y_range = (half - frac[1]) * (half - frac[1]); // y-distance squared to center line
    let z_range = (half - frac[2]) * (half - frac[2]); // z-distance squared to center line
    let w_range = (half - frac[3]) * (half - frac[3]); // w-distance squared to center line

    macro_rules! test_point(
        [$x:expr, $y:expr, $z:expr, $w:expr] => {
            {
                let cur_point = get_point(seed, [$x, $y, $z, $w]);
                let cur_range = range_func(*point, cur_point);
                if cur_range < range {
                    range = cur_range;
                    seed_cell = [$x, $y, $z, $w];
                    seed_point = cur_point;
                }
            }
        }
    );

    if x_range < range { test_point![far[0], near[1], near[2], near[3]]; }
    if y_range < range { test_point![near[0], far[1], near[2], near[3]]; }
    if z_range < range { test_point![near[0], near[1], far[2], near[3]]; }
    if w_range < range { test_point![near[0], near[1], near[2], far[3]]; }

    if x_range < range && y_range < range { test_point![far[0], far[1], near[2], near[3]]; }
    if x_range < range && z_range < range { test_point![far[0], near[1], far[2], near[3]]; }
    if x_range < range && w_range < range { test_point![far[0], near[1], near[2], far[3]]; }
    if y_range < range && z_range < range { test_point![near[0], far[1], far[2], near[3]]; }
    if y_range < range && w_range < range { test_point![near[0], far[1], near[2], far[3]]; }
    if z_range < range && w_range < range { test_point![near[0], near[1], far[2], far[3]]; }

    if x_range < range && y_range < range && z_range < range { test_point![far[0], far[1], far[2], near[3]]; }
    if x_range < range && y_range < range && w_range < range { test_point![far[0], far[1], near[2], far[3]]; }
    if x_range < range && z_range < range && w_range < range { test_point![far[0], near[1], far[2], far[3]]; }
    if y_range < range && z_range < range && w_range < range { test_point![near[0], far[1], far[2], far[3]]; }

    if x_range < range && y_range < range && z_range < range && w_range < range { test_point![far[0], far[1], far[2], far[3]]; }

    (seed_cell, seed_point, range)
}

#[inline(always)]
pub fn cell2_seed_point<T, F>(seed: &Seed, point: &math::Point2<T>, range_func: F) -> (math::Point2<T>, T)
    where T: Float, F: Fn(math::Point2<T>, math::Point2<T>) -> T
{
    let (_, seed_point, range) = cell2_seed(seed, point, range_func);
    (seed_point, range)
}

#[inline(always)]
pub fn cell2_seed_cell<T, F>(seed: &Seed, point: &math::Point2<T>, range_func: F) -> math::Point2<i64>
    where T: Float, F: Fn(math::Point2<T>, math::Point2<T>) -> T
{
    let (cell, _, _) = cell2_seed(seed, point, range_func);
    cell
}

#[inline(always)]
pub fn cell3_seed_point<T, F>(seed: &Seed, point: &math::Point3<T>, range_func: F) -> (math::Point3<T>, T)
    where T: Float, F: Fn(math::Point3<T>, math::Point3<T>) -> T
{
    let (_, seed_point, range) = cell3_seed(seed, point, range_func);
    (seed_point, range)
}

#[inline(always)]
pub fn cell3_seed_cell<T, F>(seed: &Seed, point: &math::Point3<T>, range_func: F) -> math::Point3<i64>
    where T: Float, F: Fn(math::Point3<T>, math::Point3<T>) -> T
{
    let (cell, _, _) = cell3_seed(seed, point, range_func);
    cell
}

#[inline(always)]
pub fn cell4_seed_point<T, F>(seed: &Seed, point: &math::Point4<T>, range_func: F) -> (math::Point4<T>, T)
    where T: Float, F: Fn(math::Point4<T>, math::Point4<T>) -> T
{
    let (_, seed_point, range) = cell4_seed(seed, point, range_func);
    (seed_point, range)
}

// These would be faster if we unrolled them like in the 2D version
// Doing that, however, increases the compile time of library users from
// 1 second to 120 seconds which is a poor tradeoff
static CELL3_SEARCH_ORDER: [math::Point3<isize>; 26] = [
    [-1,  0,  0], [ 1,  0,  0], [ 0, -1,  0], [ 0,  1,  0], [ 0,  0, -1],
    [ 0,  0,  1], [-1, -1,  0], [-1,  1,  0], [ 1, -1,  0], [ 1,  1,  0],
    [-1,  0, -1], [-1,  0,  1], [ 1,  0, -1], [ 1,  0,  1], [ 0, -1, -1],
    [ 0, -1,  1], [ 0,  1, -1], [ 0,  1,  1], [-1, -1, -1], [-1, -1,  1],
    [-1,  1, -1], [-1,  1,  1], [ 1, -1, -1], [ 1, -1,  1], [ 1,  1, -1],
    [ 1,  1,  1],
];

static CELL4_SEARCH_ORDER: [math::Point4<isize>; 80] = [
    [-1,  0,  0,  0], [ 1,  0,  0,  0], [ 0, -1,  0,  0], [ 0,  1,  0,  0],
    [ 0,  0, -1,  0], [ 0,  0,  1,  0], [ 0,  0,  0, -1], [ 0,  0,  0,  1],
    [-1, -1,  0,  0], [-1,  1,  0,  0], [ 1, -1,  0,  0], [ 1,  1,  0,  0],
    [-1,  0, -1,  0], [-1,  0,  1,  0], [ 1,  0, -1,  0], [ 1,  0,  1,  0],
    [-1,  0,  0, -1], [-1,  0,  0,  1], [ 1,  0,  0, -1], [ 1,  0,  0,  1],
    [ 0, -1, -1,  0], [ 0, -1,  1,  0], [ 0,  1, -1,  0], [ 0,  1,  1,  0],
    [ 0, -1,  0, -1], [ 0, -1,  0,  1], [ 0,  1,  0, -1], [ 0,  1,  0,  1],
    [ 0,  0, -1, -1], [ 0,  0, -1,  1], [ 0,  0,  1, -1], [ 0,  0,  1,  1],
    [-1, -1, -1,  0], [-1, -1,  1,  0], [-1,  1, -1,  0], [-1,  1,  1,  0],
    [ 1, -1, -1,  0], [ 1, -1,  1,  0], [ 1,  1, -1,  0], [ 1,  1,  1,  0],
    [-1, -1,  0, -1], [-1, -1,  0,  1], [-1,  1,  0, -1], [-1,  1,  0,  1],
    [ 1, -1,  0, -1], [ 1, -1,  0,  1], [ 1,  1,  0, -1], [ 1,  1,  0,  1],
    [-1,  0, -1, -1], [-1,  0, -1,  1], [-1,  0,  1, -1], [-1,  0,  1,  1],
    [ 1,  0, -1, -1], [ 1,  0, -1,  1], [ 1,  0,  1, -1], [ 1,  0,  1,  1],
    [ 0, -1, -1, -1], [ 0, -1, -1,  1], [ 0, -1,  1, -1], [ 0, -1,  1,  1],
    [ 0,  1, -1, -1], [ 0,  1, -1,  1], [ 0,  1,  1, -1], [ 0,  1,  1,  1],
    [-1, -1, -1, -1], [-1, -1, -1,  1], [-1, -1,  1, -1], [-1, -1,  1,  1],
    [-1,  1, -1, -1], [-1,  1, -1,  1], [-1,  1,  1, -1], [-1,  1,  1,  1],
    [ 1, -1, -1, -1], [ 1, -1, -1,  1], [ 1, -1,  1, -1], [ 1, -1,  1,  1],
    [ 1,  1, -1, -1], [ 1,  1, -1,  1], [ 1,  1,  1, -1], [ 1,  1,  1,  1],
];

#[inline(always)]
pub fn cell4_seed_cell<T, F>(seed: &Seed, point: &math::Point4<T>, range_func: F) -> math::Point4<i64>
    where T: Float, F: Fn(math::Point4<T>, math::Point4<T>) -> T
{
    let (cell, _, _) = cell4_seed(seed, point, range_func);
    cell
}

#[inline(always)]
pub fn cell2_seed_2_points<T, F>(seed: &Seed, point: &math::Point2<T>, range_func: F) -> (math::Point2<T>, T, math::Point2<T>, T)
    where T: Float, F: Fn(math::Point2<T>, math::Point2<T>) -> T
{
    let one: T = math::cast(1.0);
    let zero: T = math::cast(0.0);

    let cell = math::map2(*point, Float::floor);
    let frac = math::sub2(*point, cell);

    let mut seed_point0 = get_cell_point2(seed, cell);
    let mut seed_point1 = [one, one];
    let mut range0 = range_func(*point, seed_point0);
    let mut range1 = Float::max_value();

    // Distance squared for the previous, current, and next cells in each dimension
    let dx2 = [frac[0] * frac[0], zero, (one - frac[0]) * (one - frac[0])];
    let dy2 = [frac[1] * frac[1], zero, (one - frac[1]) * (one - frac[1])];

    macro_rules! test_point(
        [$x:expr, $y:expr] => {
            {
                let x_range = dx2[($x + 1) as usize];
                let y_range = dy2[($y + 1) as usize];

                if x_range + y_range < range1 {
                    let cur_point = get_cell_point2(seed, math::add2(cell, math::cast2([$x, $y])));
                    let cur_range = range_func(*point, cur_point);
                    if cur_range < range0 {
                        range1 = range0;
                        seed_point1 = seed_point0;
                        range0 = cur_range;
                        seed_point0 = cur_point;
                    } else if cur_range < range1 {
                        range1 = cur_range;
                        seed_point1 = cur_point;
                    }
                }
            }
        }
    );

    // Check four facing cells
    test_point![-1,  0];
    test_point![ 1,  0];
    test_point![ 0, -1];
    test_point![ 0,  1];

    // Check four corner cells
    test_point![-1, -1];
    test_point![-1,  1];
    test_point![ 1, -1];
    test_point![ 1,  1];

    (seed_point0, range0, seed_point1, range1)
}

#[inline(always)]
pub fn cell3_seed_2_points<T, F>(seed: &Seed, point: &math::Point3<T>, range_func: F) -> (math::Point3<T>, T, math::Point3<T>, T)
    where T: Float, F: Fn(math::Point3<T>, math::Point3<T>) -> T
{
    let one: T = math::cast(1.0);
    let zero: T = math::cast(0.0);

    let cell = math::map3(*point, Float::floor);
    let frac = math::sub3(*point, cell);

    let mut seed_point0 = get_cell_point3(seed, cell);
    let mut seed_point1 = [one, one, one];
    let mut range0 = range_func(*point, seed_point0);
    let mut range1 = Float::max_value();

    // Distance squared for the previous, current, and next cells in each dimension
    let dx2 = [frac[0] * frac[0], zero, (one - frac[0]) * (one - frac[0])];
    let dy2 = [frac[1] * frac[1], zero, (one - frac[1]) * (one - frac[1])];
    let dz2 = [frac[2] * frac[2], zero, (one - frac[2]) * (one - frac[2])];

    for offset in CELL3_SEARCH_ORDER.iter() {
        let x_range = dx2[(offset[0] + 1) as usize];
        let y_range = dy2[(offset[1] + 1) as usize];
        let z_range = dz2[(offset[2] + 1) as usize];

        if x_range + y_range + z_range < range1 {
            let cur_point = get_cell_point3(seed, math::add3(cell, math::cast3(*offset)));
            let cur_range = range_func(*point, cur_point);
            if cur_range < range0 {
                range1 = range0;
                seed_point1 = seed_point0;
                range0 = cur_range;
                seed_point0 = cur_point;
            } else if cur_range < range1 {
                range1 = cur_range;
                seed_point1 = cur_point;
            }
        }
    }

    (seed_point0, range0, seed_point1, range1)
}

#[inline(always)]
pub fn cell4_seed_2_points<T, F>(seed: &Seed, point: &math::Point4<T>, range_func: F) -> (math::Point4<T>, T, math::Point4<T>, T)
    where T: Float, F: Fn(math::Point4<T>, math::Point4<T>) -> T
{
    let one: T = math::cast(1.0);
    let zero: T = math::cast(0.0);

    let cell = math::map4(*point, Float::floor);
    let frac = math::sub4(*point, cell);

    let mut seed_point0 = get_cell_point4(seed, cell);
    let mut seed_point1 = [one, one, one, one];
    let mut range0 = range_func(*point, seed_point0);
    let mut range1 = Float::max_value();

    // Distance squared for the previous, current, and next cells in each dimension
    let dx2 = [frac[0] * frac[0], zero, (one - frac[0]) * (one - frac[0])];
    let dy2 = [frac[1] * frac[1], zero, (one - frac[1]) * (one - frac[1])];
    let dz2 = [frac[2] * frac[2], zero, (one - frac[2]) * (one - frac[2])];
    let dw2 = [frac[3] * frac[3], zero, (one - frac[3]) * (one - frac[3])];

    for offset in CELL4_SEARCH_ORDER.iter() {
        let x_range = dx2[(offset[0] + 1) as usize];
        let y_range = dy2[(offset[1] + 1) as usize];
        let z_range = dz2[(offset[2] + 1) as usize];
        let w_range = dw2[(offset[3] + 1) as usize];

        if x_range + y_range + z_range + w_range < range1 {
            let cur_point = get_cell_point4(seed, math::add4(cell, math::cast4(*offset)));
            let cur_range = range_func(*point, cur_point);
            if cur_range < range0 {
                range1 = range0;
                seed_point1 = seed_point0;
                range0 = cur_range;
                seed_point0 = cur_point;
            } else if cur_range < range1 {
                range1 = cur_range;
                seed_point1 = cur_point;
            }
        }
    }

    (seed_point0, range0, seed_point1, range1)
}

pub fn cell2_range<T: Float>(seed: &Seed, point: &math::Point2<T>) -> T {
    let (_, range) = cell2_seed_point(seed, point, range_sqr_euclidian2);
    range
}

pub fn cell3_range<T: Float>(seed: &Seed, point: &math::Point3<T>) -> T {
    let (_, range) = cell3_seed_point(seed, point, range_sqr_euclidian3);
    range
}

pub fn cell4_range<T: Float>(seed: &Seed, point: &math::Point4<T>) -> T {
    let (_, range) = cell4_seed_point(seed, point, range_sqr_euclidian4);
    range
}

pub fn cell2_range_inv<T: Float>(seed: &Seed, point: &math::Point2<T>) -> T {
    let (_, range1, _, range2) = cell2_seed_2_points(seed, point, range_sqr_euclidian2);
    range2 - range1
}

pub fn cell3_range_inv<T: Float>(seed: &Seed, point: &math::Point3<T>) -> T {
    let (_, range1, _, range2) = cell3_seed_2_points(seed, point, range_sqr_euclidian3);
    range2 - range1
}

pub fn cell4_range_inv<T: Float>(seed: &Seed, point: &math::Point4<T>) -> T {
    let (_, range1, _, range2) = cell4_seed_2_points(seed, point, range_sqr_euclidian4);
    range2 - range1
}

pub fn cell2_value<T: Float>(seed: &Seed, point: &math::Point2<T>) -> T {
    let cell = cell2_seed_cell(seed, point, range_sqr_euclidian2);
    math::cast::<_,T>(seed.get2(cell)) * math::cast(1.0 / 255.0)
}

pub fn cell3_value<T: Float>(seed: &Seed, point: &math::Point3<T>) -> T {
    let cell = cell3_seed_cell(seed, point, range_sqr_euclidian3);
    math::cast::<_,T>(seed.get3(cell)) * math::cast(1.0 / 255.0)
}

pub fn cell4_value<T: Float>(seed: &Seed, point: &math::Point4<T>) -> T {
    let cell = cell4_seed_cell(seed, point, range_sqr_euclidian4);
    math::cast::<_,T>(seed.get4(cell)) * math::cast(1.0 / 255.0)
}

pub fn cell2_manhattan<T: Float>(seed: &Seed, point: &math::Point2<T>) -> T {
    let (_, range) = cell2_seed_point(seed, point, range_manhattan2);
    range
}

pub fn cell3_manhattan<T: Float>(seed: &Seed, point: &math::Point3<T>) -> T {
    let (_, range) = cell3_seed_point(seed, point, range_manhattan3);
    range
}

pub fn cell4_manhattan<T: Float>(seed: &Seed, point: &math::Point4<T>) -> T {
    let (_, range) = cell4_seed_point(seed, point, range_manhattan4);
    range
}

pub fn cell2_manhattan_inv<T: Float>(seed: &Seed, point: &math::Point2<T>) -> T {
    let (_, range1, _, range2) = cell2_seed_2_points(seed, point, range_manhattan2);
    range2 - range1
}

pub fn cell3_manhattan_inv<T: Float>(seed: &Seed, point: &math::Point3<T>) -> T {
    let (_, range1, _, range2) = cell3_seed_2_points(seed, point, range_manhattan3);
    range2 - range1
}

pub fn cell4_manhattan_inv<T: Float>(seed: &Seed, point: &math::Point4<T>) -> T {
    let (_, range1, _, range2) = cell4_seed_2_points(seed, point, range_manhattan4);
    range2 - range1
}

pub fn cell2_manhattan_value<T: Float>(seed: &Seed, point: &math::Point2<T>) -> T {
    let cell = cell2_seed_cell(seed, point, range_manhattan2);
    math::cast::<_,T>(seed.get2(cell)) * math::cast(1.0 / 255.0)
}

pub fn cell3_manhattan_value<T: Float>(seed: &Seed, point: &math::Point3<T>) -> T {
    let cell = cell3_seed_cell(seed, point, range_manhattan3);
    math::cast::<_,T>(seed.get3(cell)) * math::cast(1.0 / 255.0)
}

pub fn cell4_manhattan_value<T: Float>(seed: &Seed, point: &math::Point4<T>) -> T {
    let cell = cell4_seed_cell(seed, point, range_manhattan4);
    math::cast::<_,T>(seed.get4(cell)) * math::cast(1.0 / 255.0)
}