-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtph_poisson.h
1307 lines (1145 loc) · 48.3 KB
/
tph_poisson.h
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright(c) 2024 Tommy Hinks
* For LICENSE (MIT), USAGE, and HISTORY see the end of this file.
*/
#ifndef TPH_POISSON_H
#define TPH_POISSON_H
#define TPH_POISSON_MAJOR_VERSION 0
#define TPH_POISSON_MINOR_VERSION 4
#define TPH_POISSON_PATCH_VERSION 0
#include <stddef.h> /* size_t, ptrdiff_t, NULL */
#include <stdint.h> /* uint32_t, uintptr_t, etc */
#ifdef __cplusplus
extern "C" {
#endif
/* BEGIN PUBLIC API --------------------------------------------------------- */
/* clang-format off */
#if (defined(TPH_POISSON_REAL_TYPE) \
&& (!defined(TPH_POISSON_SQRT) || !defined(TPH_POISSON_CEIL) || !defined(TPH_POISSON_FLOOR))) \
|| (defined(TPH_POISSON_SQRT) \
&& (!defined(TPH_POISSON_REAL_TYPE) || !defined(TPH_POISSON_CEIL) || !defined(TPH_POISSON_FLOOR))) \
|| (defined(TPH_POISSON_CEIL) \
&& (!defined(TPH_POISSON_REAL_TYPE) || !defined(TPH_POISSON_SQRT) || !defined(TPH_POISSON_FLOOR))) \
|| (defined(TPH_POISSON_FLOOR) \
&& (!defined(TPH_POISSON_REAL_TYPE) || !defined(TPH_POISSON_SQRT) || !defined(TPH_POISSON_CEIL)))
#error \
"TPH_POISSON_REAL_TYPE, TPH_POISSON_SQRT, TPH_POISSON_CEIL and TPH_POISSON_FLOOR must all be defined; or none of them."
#endif
#if !defined(TPH_POISSON_REAL_TYPE) && !defined(TPH_POISSON_SQRT) && !defined(TPH_POISSON_CEIL) \
&& !defined(TPH_POISSON_FLOOR)
#define TPH_POISSON_REAL_TYPE float
#include <math.h>
#define TPH_POISSON_SQRT(_X_) sqrtf((_X_))
#define TPH_POISSON_CEIL(_X_) ceilf((_X_))
#define TPH_POISSON_FLOOR(_X_) floorf((_X_))
#endif
typedef TPH_POISSON_REAL_TYPE tph_poisson_real;
typedef struct tph_poisson_args_ tph_poisson_args;
typedef struct tph_poisson_allocator_ tph_poisson_allocator;
typedef struct tph_poisson_sampling_ tph_poisson_sampling;
typedef struct tph_poisson_sampling_internal_ tph_poisson_sampling_internal;
typedef void *(*tph_poisson_malloc_fn)(ptrdiff_t size, void *ctx);
typedef void (*tph_poisson_free_fn)(void *ptr, ptrdiff_t size, void *ctx);
/* clang-format on */
#pragma pack(push, 1)
/**
* Allocator interface. Must provide malloc and free functions.
* Context is optional and may be NULL.
*/
struct tph_poisson_allocator_
{
tph_poisson_malloc_fn malloc;
tph_poisson_free_fn free;
void *ctx;
};
/**
* Parameters used when creating a Poisson disk sampling.
* bounds_min/max are assumed to point to arrays of length ndims.
*/
struct tph_poisson_args_
{
const tph_poisson_real *bounds_min;
const tph_poisson_real *bounds_max;
uint64_t seed;
tph_poisson_real radius;
int32_t ndims;
uint32_t max_sample_attempts;
};
/**
* Result of creating a Poisson disk sampling.
* Use with tph_poisson_get_samples to retrieve sample positions.
* Memory must be freed after use by calling tph_poisson_destroy.
*/
struct tph_poisson_sampling_
{
tph_poisson_sampling_internal *internal;
ptrdiff_t nsamples;
int32_t ndims;
};
#pragma pack(pop)
/* clang-format off */
/* Possible return values from tph_poisson_create. */
#define TPH_POISSON_SUCCESS 0
#define TPH_POISSON_BAD_ALLOC 1
#define TPH_POISSON_INVALID_ARGS 2
#define TPH_POISSON_OVERFLOW 3
/* clang-format on */
/**
* Generates a list of samples with the guarantees:
* (1) No two samples are closer to each other than args.radius;
* (2) No sample is outside the region [args.bounds_min, args.bounds_max].
*
* The algorithm tries to fit as many samples as possible into the provided region
* without violating the above requirements. After creation, the samples can be
* accessed using the tph_poisson_get_samples function.
*
* Errors:
* TPH_POISSON_BAD_ALLOC - Failed memory allocation.
* TPH_POISSON_INVALID_ARGS - The arguments are invalid if:
* - args.radius is <= 0, or
* - args.ndims is < 1, or
* - args.bounds_min[i] >= args.bounds_max[i], or
* - args.max_sample_attempts == 0, or
* - an invalid allocator is provided.
* TPH_POISSON_OVERFLOW - The number of samples exceeds the maximum number.
*
* Note that when an error is returned the sampling doesn't need to be destroyed
* using the tph_poisson_destroy function.
*
* @param sampling Sampling to store samples.
* @param args Arguments.
* @param alloc Optional custom allocator (may be null).
* @return TPH_POISSON_SUCCESS if no errors; otherwise a non-zero error code.
*/
extern int tph_poisson_create(const tph_poisson_args *args,
const tph_poisson_allocator *alloc,
tph_poisson_sampling *sampling);
/**
* @brief Frees all memory used by the sampling. Note that the sampling itself is not free'd.
* @param sampling Sampling to store samples.
*/
extern void tph_poisson_destroy(tph_poisson_sampling *sampling);
/**
* Returns a pointer to the samples in the provided sampling. Samples are stored as
* N-dimensional points, i.e. the first N values are the coordinates of the first point, etc.
* Note that sampling.ndims and sampling.nsamples can be used to unpack the raw samples into points.
* @param sampling Sampling to store samples.
* @return Pointer to samples, or NULL if the sampling has not been successfully initialized by a
* call to the tph_poisson_create function (without being destroy by the tph_poisson_destroy
* function in between).
*/
extern const tph_poisson_real *tph_poisson_get_samples(const tph_poisson_sampling *sampling);
/* END PUBLIC API ----------------------------------------------------------- */
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* TPH_POISSON_H */
/* BEGIN IMPLEMENTATION ------------------------------------------------------*/
#ifdef TPH_POISSON_IMPLEMENTATION
#undef TPH_POISSON_IMPLEMENTATION
#include <stdalign.h> /* alignof */
#include <stdbool.h> /* bool, true, false */
#if defined(_MSC_VER) && !defined(__cplusplus)
#define TPH_POISSON_INLINE __inline
#else
#define TPH_POISSON_INLINE inline
#endif
#ifndef TPH_POISSON_ASSERT
#include <assert.h>
#define TPH_POISSON_ASSERT(_X_) assert((_X_))
#endif
#ifndef TPH_POISSON_MEMCPY
#include <string.h>
#define TPH_POISSON_MEMCPY(_DST_, _SRC_, _N_) memcpy((_DST_), (_SRC_), (_N_))
#endif
#ifndef TPH_POISSON_MEMSET
#include <string.h>
#define TPH_POISSON_MEMSET(_S_, _C_, _N_) memset((_S_), (_C_), (_N_))
#endif
/*
* MEMORY
*/
/* clang-format off */
#if ( defined(TPH_POISSON_MALLOC) && !defined(TPH_POISSON_FREE)) || \
(!defined(TPH_POISSON_MALLOC) && defined(TPH_POISSON_FREE))
#error \
"TPH_POISSON_MALLOC and TPH_POISSON_FREE must both be defined; or none of them."
#endif
#if !defined(TPH_POISSON_MALLOC) && !defined(TPH_POISSON_FREE)
#include <stdlib.h>
#define TPH_POISSON_MALLOC(_SIZE_) malloc((_SIZE_))
#define TPH_POISSON_FREE(_PTR_) free((_PTR_))
#endif
/* clang-format on */
/* Adapt libc memory functions to our allocator interface. These functions will be called if no
* custom allocator is provided at run-time. */
static TPH_POISSON_INLINE void *tph_poisson_malloc(ptrdiff_t size, void *ctx)
{
(void)ctx;
return size > 0 ? TPH_POISSON_MALLOC((size_t)size) : (void *)NULL;
}
static TPH_POISSON_INLINE void tph_poisson_free(void *ptr, ptrdiff_t size, void *ctx)
{
(void)size;
(void)ctx;
TPH_POISSON_FREE(ptr);
}
/**
* Default allocator used when no custom allocator is provided.
*/
static tph_poisson_allocator tph_poisson_default_alloc = { tph_poisson_malloc,
tph_poisson_free,
/*.ctx=*/NULL };
/**
* @brief Returns a pointer aligned to the provided alignment. The address pointed
* to is a multiple of the alignment. Assumes that alignment is a power of two (which
* seems to always be the case when querying the value using the alignof function). The
* alignment is done by (potentially) moving the pointer 'forward' until it satisfies
* that the address is a multiple of alignment.
* @param ptr Pointer to align.
* @param alignment Requested alignment. Assumed to be a (non-zero) power of two.
* @return An aligned pointer.
*/
static TPH_POISSON_INLINE void *tph_poisson_align(void *const ptr, const size_t alignment)
{
TPH_POISSON_ASSERT((alignment > 0) & ((alignment & (alignment - 1)) == 0));
return (void *)(((uintptr_t)ptr + (alignment - 1)) & ~(alignment - 1));
}
/*
* PSEUDO-RANDOM NUMBER GENERATION
*/
typedef struct tph_poisson_splitmix64_state_
{
uint64_t s;
} tph_poisson_splitmix64_state;
/**
* @brief Returns a pseudo-random number generated using the SplitMix64 algorithm and mutates the
* state in preparation for subsequent calls.
* @param state State to be mutated into the next number in the sequence.
* @return A pseudo-random number
*/
static uint64_t tph_poisson_splitmix64(tph_poisson_splitmix64_state *state)
{
uint64_t result = (state->s += 0x9E3779B97f4A7C15);
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
return result ^ (result >> 31);
}
typedef struct tph_poisson_xoshiro256p_state_
{
uint64_t s[4];
} tph_poisson_xoshiro256p_state;
/**
* @brief Initializes a xoshiro256p state.
* @param state The state to initialize.
* @param seed Seed value, can be zero.
*/
static void tph_poisson_xoshiro256p_init(tph_poisson_xoshiro256p_state *state, const uint64_t seed)
{
/* As suggested at https://prng.di.unimi.it, use SplitMix64 to initialize the state of a
* generator starting from a 64-bit seed. It has been shown that initialization must be
* performed with a generator radically different in nature from the one used to avoid
* correlation on similar seeds. */
tph_poisson_splitmix64_state sm_state = { seed };
state->s[0] = tph_poisson_splitmix64(&sm_state);
state->s[1] = tph_poisson_splitmix64(&sm_state);
state->s[2] = tph_poisson_splitmix64(&sm_state);
state->s[3] = tph_poisson_splitmix64(&sm_state);
}
/**
* @brief Returns a pseudo-random number generated using the xoshiro256+ algorithm and mutates the
* state in preparation for subsequent calls. Assumes that the value of the passed in state is not
* all zeros.
* @param state State to be mutated into the next number in the sequence.
* @return A pseudo-random number.
*/
static uint64_t tph_poisson_xoshiro256p_next(tph_poisson_xoshiro256p_state *state)
{
uint64_t *s = state->s;
const uint64_t result = s[0] + s[3];
const uint64_t t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
/* Hard-code: s[3] = rotl(s[3], 45) */
s[3] = (s[3] << 45) | (s[3] >> 49);
return result;
}
/*
* MISC
*/
/**
* @brief Returns a floating point number in [0..1).
* @param x Bit representation.
* @return A number in [0..1).
*/
static TPH_POISSON_INLINE double tph_poisson_to_double(const uint64_t x)
{
/* Convert to double, as suggested at https://prng.di.unimi.it. This conversion prefers the high
* bits of x (usually, a good idea). */
return (double)(x >> 11) * 0x1.0p-53;
}
/*
* VECTOR
*/
/* clang-format off */
typedef struct tph_poisson_vec_
{
ptrdiff_t mem_size; /** Size of memory buffer in bytes. */
void *mem; /** Memory buffer. */
void *begin; /** Pointer to first element, or NULL if empty. */
void *end; /** Pointer to first byte after last element, or NULL if empty. */
} tph_poisson_vec;
/* clang-format on */
/**
* @brief Frees all memory associated with the vector.
* @param vec Vector.
* @param alloc Allocator.
*/
static TPH_POISSON_INLINE void tph_poisson_vec_free(tph_poisson_vec *vec,
const tph_poisson_allocator *alloc)
{
TPH_POISSON_ASSERT(vec != NULL);
TPH_POISSON_ASSERT(alloc != NULL);
if (((int)(vec->mem != NULL) & (int)(vec->mem_size > 0)) == 1) {
alloc->free(vec->mem, vec->mem_size, alloc->ctx);
}
}
/**
* @brief Returns the number of bytes in the vector.
* @param vec Vector.
* @return The number of bytes in the vector.
*/
static TPH_POISSON_INLINE ptrdiff_t tph_poisson_vec_size(const tph_poisson_vec *vec)
{
TPH_POISSON_ASSERT((intptr_t)vec->end >= (intptr_t)vec->begin);
return (ptrdiff_t)((intptr_t)vec->end - (intptr_t)vec->begin);
}
/**
* @brief Increase the capacity of the vector (the total number of elements that the vector can hold
* without requiring reallocation) to a value that's greater or equal to new_cap. If new_cap is
* greater than the current capacity, new storage is allocated, otherwise the function does
* nothing.
* @param vec Vector.
* @param alloc Allocator.
* @param new_cap Minimum number of elements that can be stored without requiring reallocation.
* @param alignment Alignment of objects intended to be stored in the vector.
* @return TPH_POISSON_SUCCESS, or a non-zero error code.
*/
static int tph_poisson_vec_reserve(tph_poisson_vec *vec,
const tph_poisson_allocator *alloc,
const ptrdiff_t new_cap,
const ptrdiff_t alignment)
{
TPH_POISSON_ASSERT(vec != NULL);
TPH_POISSON_ASSERT(alloc != NULL);
TPH_POISSON_ASSERT(new_cap > 0);
TPH_POISSON_ASSERT(alignment > 0);
/* NOTE: For zero-initialized vector cap is 0. */
const ptrdiff_t cap = vec->mem_size - ((intptr_t)vec->begin - (intptr_t)vec->mem);
if (new_cap <= cap) { return TPH_POISSON_SUCCESS; }
/* Allocate and align a new buffer with sufficient capacity. Take into account that
* the memory returned by the allocator may not match the requested alignment. */
const ptrdiff_t new_mem_size = new_cap + alignment;
void *const new_mem = alloc->malloc(new_mem_size, alloc->ctx);
if (new_mem == NULL) { return TPH_POISSON_BAD_ALLOC; }
void *const new_begin = tph_poisson_align(new_mem, (size_t)alignment);
const ptrdiff_t size = (intptr_t)vec->end - (intptr_t)vec->begin;
/* Copy existing data (if any) to the new buffer. */
if (size > 0) {
TPH_POISSON_ASSERT(vec->begin != NULL);
TPH_POISSON_MEMCPY(new_begin, vec->begin, (size_t)size);
}
/* Destroy the old buffer (if any). */
if (vec->mem_size > 0) {
TPH_POISSON_ASSERT(vec->mem != NULL);
alloc->free(vec->mem, vec->mem_size, alloc->ctx);
}
/* Configure vector to use the new buffer. */
vec->mem = new_mem;
vec->mem_size = new_mem_size;
vec->begin = new_begin;
vec->end = (void *)((intptr_t)new_begin + size);
TPH_POISSON_ASSERT(vec->mem_size - ((intptr_t)vec->begin - (intptr_t)vec->mem) >= new_cap);
return TPH_POISSON_SUCCESS;
}
/**
* @brief Add elements to the end of the vector.
* @param vec Vector.
* @param alloc Allocator.
* @param buf Pointer to values to be added. Assumed to be non-null.
* @param n Number of bytes to copy from values. Assumed to be > 0.
* @param alignment Alignment of objects intended to be stored in the vector.
* @return TPH_POISSON_SUCCESS, or a non-zero error code.
*/
static int tph_poisson_vec_append(tph_poisson_vec *vec,
const tph_poisson_allocator *alloc,
const void *buf,
const ptrdiff_t n,
const ptrdiff_t alignment)
{
TPH_POISSON_ASSERT(vec != NULL);
TPH_POISSON_ASSERT(alloc != NULL);
TPH_POISSON_ASSERT(buf != NULL);
TPH_POISSON_ASSERT(n > 0);
TPH_POISSON_ASSERT(alignment > 0);
const ptrdiff_t cap = vec->mem_size - ((intptr_t)vec->begin - (intptr_t)vec->mem);
const ptrdiff_t req_cap = (intptr_t)vec->end - (intptr_t)vec->begin + n;
if (req_cap > cap) {
/* Current buffer does not have enough capacity. Try doubling the vector
* capacity and check if it's enough to hold the new elements. If not,
* set the new capacity to hold exactly the new elements. */
ptrdiff_t new_cap = cap <= (PTRDIFF_MAX >> 1) ? (cap << 1) : cap;
if (new_cap < req_cap) { new_cap = req_cap; }
/* Allocate and align a new buffer with sufficient capacity. Take into
* account that the memory returned by the allocator may not be aligned to
* the type of element that will be stored. */
new_cap += alignment;
void *new_mem = alloc->malloc(new_cap, alloc->ctx);
if (new_mem == NULL) { return TPH_POISSON_BAD_ALLOC; }
void *new_begin = tph_poisson_align(new_mem, (size_t)alignment);
const ptrdiff_t size = (intptr_t)vec->end - (intptr_t)vec->begin;
/* Copy existing data (if any) to the new buffer. */
if (size > 0) {
TPH_POISSON_ASSERT(vec->begin != NULL);
TPH_POISSON_MEMCPY(new_begin, vec->begin, (size_t)size);
}
/* Destroy the old buffer (if any). */
if (vec->mem_size > 0) {
TPH_POISSON_ASSERT(vec->mem != NULL);
alloc->free(vec->mem, vec->mem_size, alloc->ctx);
}
/* Configure vector to use the new buffer. */
vec->mem = new_mem;
vec->mem_size = new_cap;
vec->begin = new_begin;
vec->end = (void *)((intptr_t)new_begin + size);
}
TPH_POISSON_ASSERT(vec->mem_size - ((intptr_t)vec->begin - (intptr_t)vec->mem) >= req_cap);
TPH_POISSON_ASSERT((intptr_t)vec->end % alignment == 0);
TPH_POISSON_MEMCPY(vec->end, buf, (size_t)n);
vec->end = (void *)((intptr_t)vec->end + n);
return TPH_POISSON_SUCCESS;
}
/**
* @brief Erase an element from the vector using a "swap & pop" approach. Note that this may
* re-order bytes.
* @param vec Vector.
* @param pos Position to be erased. Assuming 0 <= pos and pos < vector size.
* @return TPH_POISSON_SUCCESS, or a non-zero error code.
*/
static void tph_poisson_vec_erase_swap(tph_poisson_vec *vec, const ptrdiff_t pos, const ptrdiff_t n)
{
TPH_POISSON_ASSERT(vec != NULL);
TPH_POISSON_ASSERT(pos >= 0);
TPH_POISSON_ASSERT((intptr_t)vec->begin + pos < (intptr_t)vec->end);
TPH_POISSON_ASSERT(n >= 1);
TPH_POISSON_ASSERT((intptr_t)vec->begin + pos + n <= (intptr_t)vec->end);
const intptr_t dst_begin = (intptr_t)vec->begin + pos;
const intptr_t dst_end = dst_begin + n;
intptr_t src_begin = (intptr_t)vec->end - n;
if (src_begin < dst_end) { src_begin = dst_end; }
const intptr_t m = (intptr_t)vec->end - src_begin;
if (m > 0) { TPH_POISSON_MEMCPY((void *)dst_begin, (const void *)src_begin, (size_t)m); }
/* else: when erasing up to the last element there is no need to copy anything,
* just "pop" the end of the vector. */
/* "pop" the end of the buffer, decreasing the vector size by n. */
vec->end = (void *)((intptr_t)vec->end - n);
}
/**
* @brief Requests the removal of unused capacity.
* @param vec Vector.
* @param alloc Allocator.
* @return TPH_POISSON_SUCCESS, or a non-zero error code.
*/
static int tph_poisson_vec_shrink_to_fit(tph_poisson_vec *vec,
const tph_poisson_allocator *alloc,
const ptrdiff_t alignment)
{
TPH_POISSON_ASSERT(vec != NULL);
TPH_POISSON_ASSERT(alloc != NULL);
TPH_POISSON_ASSERT(alignment > 0);
const ptrdiff_t size = (intptr_t)vec->end - (intptr_t)vec->begin;
if (size == 0) {
/* Empty vector, no elements. Wipe capacity!
* This includes the case of a zero-initialized vector. */
if (vec->mem != NULL) {
/* Existing vector is empty but has capacity. */
TPH_POISSON_ASSERT(vec->mem_size > 0);
alloc->free(vec->mem, vec->mem_size, alloc->ctx);
TPH_POISSON_MEMSET((void *)vec, 0, sizeof(tph_poisson_vec));
}
TPH_POISSON_ASSERT(vec->mem == NULL);
TPH_POISSON_ASSERT(vec->mem_size == 0);
return TPH_POISSON_SUCCESS;
}
/* Check if allocating a new buffer (size + alignment) would be smaller than
* the existing buffer. */
TPH_POISSON_ASSERT(vec->mem_size > alignment);
const ptrdiff_t new_mem_size = size + alignment;
if (vec->mem_size > new_mem_size) {
/* Allocate and align a new buffer with sufficient capacity. Take into
* account that the memory returned by the allocator may not be aligned to
* the type of element that will be stored. */
void *const new_mem = alloc->malloc(new_mem_size, alloc->ctx);
if (new_mem == NULL) { return TPH_POISSON_BAD_ALLOC; }
void *const new_begin = tph_poisson_align(new_mem, (size_t)alignment);
/* Copy existing data to the new buffer and destroy the old buffer. */
TPH_POISSON_MEMCPY(new_begin, vec->begin, (size_t)size);
alloc->free(vec->mem, vec->mem_size, alloc->ctx);
/* Configure vector to use the new buffer. */
vec->mem = new_mem;
vec->mem_size = new_mem_size;
vec->begin = new_begin;
vec->end = (void *)((intptr_t)new_begin + size);
}
return TPH_POISSON_SUCCESS;
}
/*
* STRUCTS
*/
struct tph_poisson_sampling_internal_
{
tph_poisson_allocator alloc;
void *mem;
ptrdiff_t mem_size;
tph_poisson_vec samples; /** ElemT = tph_poisson_real */
};
typedef struct tph_poisson_context_
{
void *mem;
ptrdiff_t mem_size;
tph_poisson_real radius; /** No two samples are closer to each other than the radius. */
int32_t ndims; /** Number of dimensions, typically 2 or 3. */
uint32_t max_sample_attempts; /** Maximum attempts when spawning samples from existing ones. */
tph_poisson_real *bounds_min; /** Hyper-rectangle lower bound. */
tph_poisson_real *bounds_max; /** Hyper-rectangle upper bound. */
tph_poisson_xoshiro256p_state prng_state; /** Pseudo-random number generator state. */
tph_poisson_vec active_indices; /** ElemT = ptrdiff_t */
tph_poisson_real grid_dx; /** Uniform cell extent. */
tph_poisson_real grid_dx_rcp; /** 1 / dx */
ptrdiff_t grid_linear_size; /** Total number of grid cells. */
ptrdiff_t *grid_size; /** Number of grid cells in each dimension. */
ptrdiff_t *grid_stride; /** Strides in each dimension, used to compute linear index. */
uint32_t *grid_cells; /** Grid cells storing indices to points inside them. */
/* Arrays of size ndims. Pre-allocated in the context to provide 'scratch' variables that are used
* during the creation of a sampling, but don't need to be stored afterwards. */
tph_poisson_real *sample;
ptrdiff_t *grid_index;
ptrdiff_t *min_grid_index;
ptrdiff_t *max_grid_index;
} tph_poisson_context;
/**
* @brief Returns an allocated instance of sampling internal data. If allocator is NULL,
* a default allocator is used. The instance must be free'd using the free function that
* it stores (see tph_poisson_destroy).
* @param alloc Allocator.
* @return A dynamically allocated initialized instance of sampling internal data.
*/
static tph_poisson_sampling_internal *tph_poisson_alloc_internal(const tph_poisson_allocator *alloc)
{
TPH_POISSON_ASSERT(!alloc || (alloc && ((alloc->malloc != NULL) & (alloc->free != NULL))));
const ptrdiff_t mem_size =
(ptrdiff_t)(sizeof(tph_poisson_sampling_internal) + alignof(tph_poisson_sampling_internal));
tph_poisson_malloc_fn malloc_fn =
alloc != NULL ? alloc->malloc : tph_poisson_default_alloc.malloc;
void *alloc_ctx = alloc != NULL ? alloc->ctx : tph_poisson_default_alloc.ctx;
void *mem = malloc_fn(mem_size, alloc_ctx);
if (mem == NULL) { return NULL; }
TPH_POISSON_MEMSET(mem, 0, (size_t)mem_size);
void *aligned_mem = tph_poisson_align(mem, alignof(tph_poisson_sampling_internal));
tph_poisson_sampling_internal *internal = (tph_poisson_sampling_internal *)aligned_mem;
internal->alloc.malloc = malloc_fn;
internal->alloc.free = alloc != NULL ? alloc->free : tph_poisson_default_alloc.free;
internal->alloc.ctx = alloc_ctx;
internal->mem = mem;
internal->mem_size = mem_size;
return internal;
}
/**
* @brief Initialize the context using the provided allocator and arguments. Sets up the
* data structures needed to perform a single run, but that don't need to be kept alive
* after the run has been completed.
* @param ctx Context.
* @param alloc Allocator.
* @param args Arguments.
* @return TPH_POISSON_SUCCESS, or a non-zero error code.
*/
static int tph_poisson_context_init(const tph_poisson_allocator *alloc,
const tph_poisson_args *args,
tph_poisson_context *ctx)
{
/* clang-format off */
bool valid_args = (args != NULL);
if (!valid_args) { return TPH_POISSON_INVALID_ARGS; }
valid_args &= (args->radius > 0);
valid_args &= (args->ndims > 0);
valid_args &= (args->max_sample_attempts > 0);
valid_args &= (args->bounds_min != NULL);
valid_args &= (args->bounds_max != NULL);
if (!valid_args) { return TPH_POISSON_INVALID_ARGS; }
for (int32_t i = 0; i < args->ndims; ++i) {
valid_args &= (args->bounds_max[i] > args->bounds_min[i]);
}
if (!valid_args) { return TPH_POISSON_INVALID_ARGS; }
/* clang-format on */
ctx->radius = args->radius;
ctx->ndims = args->ndims;
ctx->max_sample_attempts = args->max_sample_attempts;
/* Use a slightly smaller radius to avoid numerical issues. */
ctx->grid_dx =
((tph_poisson_real)0.999 * ctx->radius) / TPH_POISSON_SQRT((tph_poisson_real)ctx->ndims);
ctx->grid_dx_rcp = (tph_poisson_real)1 / ctx->grid_dx;
/* Seed pseudo-random number generator. */
tph_poisson_xoshiro256p_init(&ctx->prng_state, args->seed);
/* Compute grid linear size so that we know how much memory to allocate for grid cells. */
ctx->grid_linear_size = 1;
for (int32_t i = 0; i < ctx->ndims; ++i) {
/* Not checking for overflow! */
ctx->grid_linear_size *=
(ptrdiff_t)TPH_POISSON_CEIL((args->bounds_max[i] - args->bounds_min[i]) * ctx->grid_dx_rcp);
}
/* clang-format off */
ctx->mem_size =
/* bounds_min, bounds_max, sample */
(ptrdiff_t)(ctx->ndims * 3) * (ptrdiff_t)sizeof(tph_poisson_real)
+ (ptrdiff_t)alignof(tph_poisson_real) +
/* grid_index, min_grid_index, max_grid_index, grid.size, grid.stride*/
(ptrdiff_t)(ctx->ndims * 5) * (ptrdiff_t)sizeof(ptrdiff_t)
+ (ptrdiff_t)alignof(ptrdiff_t) +
/* grid.cells */
ctx->grid_linear_size * (ptrdiff_t)sizeof(uint32_t) + (ptrdiff_t)alignof(uint32_t);
ctx->mem = alloc->malloc(ctx->mem_size, alloc->ctx);
/* clang-format on */
if (ctx->mem == NULL) { return TPH_POISSON_BAD_ALLOC; }
TPH_POISSON_MEMSET(ctx->mem, 0, (size_t)ctx->mem_size);
/* Initialize context pointers. Make sure alignment is correct. */
void *ptr = ctx->mem;
#ifdef TPH_POISSON_CTX_ALLOC
#error "TPH_POISSON_CTX_ALLOC already defined!"
#endif
/* clang-format off */
#define TPH_POISSON_CTX_ALLOC(type, count, var) \
do { \
TPH_POISSON_ASSERT((uintptr_t)ptr < (uintptr_t)ctx->mem + (size_t)ctx->mem_size); \
ptr = ((var) = (type *)ptr) + (count); \
TPH_POISSON_ASSERT((uintptr_t)(var) + (size_t)((ptrdiff_t)sizeof(type) * (count) - 1) \
< (uintptr_t)ctx->mem + (size_t)ctx->mem_size); \
TPH_POISSON_ASSERT(((uintptr_t)(var) & (alignof(type) - 1)) == 0); \
} while (0)
/* clang-format on */
ptr = tph_poisson_align(ptr, alignof(ptrdiff_t));
TPH_POISSON_CTX_ALLOC(ptrdiff_t, ctx->ndims, ctx->grid_index);
TPH_POISSON_CTX_ALLOC(ptrdiff_t, ctx->ndims, ctx->min_grid_index);
TPH_POISSON_CTX_ALLOC(ptrdiff_t, ctx->ndims, ctx->max_grid_index);
TPH_POISSON_CTX_ALLOC(ptrdiff_t, ctx->ndims, ctx->grid_size);
TPH_POISSON_CTX_ALLOC(ptrdiff_t, ctx->ndims, ctx->grid_stride);
ptr = tph_poisson_align(ptr, alignof(tph_poisson_real));
TPH_POISSON_CTX_ALLOC(tph_poisson_real, ctx->ndims, ctx->bounds_min);
TPH_POISSON_CTX_ALLOC(tph_poisson_real, ctx->ndims, ctx->bounds_max);
TPH_POISSON_CTX_ALLOC(tph_poisson_real, ctx->ndims, ctx->sample);
ptr = tph_poisson_align(ptr, alignof(uint32_t));
TPH_POISSON_CTX_ALLOC(uint32_t, ctx->grid_linear_size, ctx->grid_cells);
#undef TPH_POISSON_CTX_ALLOC
/* Copy bounds into context memory buffer to improve locality. */
TPH_POISSON_MEMCPY(
ctx->bounds_min, args->bounds_min, (size_t)(ctx->ndims * (ptrdiff_t)sizeof(tph_poisson_real)));
TPH_POISSON_MEMCPY(
ctx->bounds_max, args->bounds_max, (size_t)(ctx->ndims * (ptrdiff_t)sizeof(tph_poisson_real)));
/* Initialize grid size and stride. */
ctx->grid_size[0] =
(ptrdiff_t)TPH_POISSON_CEIL((ctx->bounds_max[0] - ctx->bounds_min[0]) * ctx->grid_dx_rcp);
ctx->grid_stride[0] = 1;
for (int32_t i = 1; i < ctx->ndims; ++i) {
ctx->grid_size[i] =
(ptrdiff_t)TPH_POISSON_CEIL((ctx->bounds_max[i] - ctx->bounds_min[i]) * ctx->grid_dx_rcp);
ctx->grid_stride[i] = ctx->grid_stride[i - 1] * ctx->grid_size[i - 1];
}
/* Initialize cells with sentinel value 0xFFFFFFFF, indicating no sample there.
* Cell values are later set to sample indices. */
TPH_POISSON_MEMSET(
ctx->grid_cells, 0xFF, (size_t)(ctx->grid_linear_size * (ptrdiff_t)sizeof(uint32_t)));
return TPH_POISSON_SUCCESS;
}
/**
* @brief Frees all memory allocated by the context.
* @param ctx Context.
*/
static void tph_poisson_context_destroy(tph_poisson_context *ctx, tph_poisson_allocator *alloc)
{
TPH_POISSON_ASSERT(ctx && alloc);
tph_poisson_vec_free(&ctx->active_indices, alloc);
alloc->free(ctx->mem, ctx->mem_size, alloc->ctx);
}
/**
* @brief Returns true if p is element-wise inclusively inside b_min and b_max; otherwise false.
* Assumes that b_min is element-wise less than b_max.
* @param p Position to test.
* @param b_min Minimum bounds.
* @param b_max Maximum bounds.
* @param ndims Number of values in p, b_min, and b_max.
* @return Non-zero if p is element-wise inclusively inside the bounded region; otherwise zero.
*/
static bool tph_poisson_inside(const tph_poisson_real *p,
const tph_poisson_real *b_min,
const tph_poisson_real *b_max,
const int32_t ndims)
{
/* Note returns true if ndims <= 0. */
bool inside = true;
for (int32_t i = 0; i < ndims; ++i) {
/* No early exit, always check all dims. */
TPH_POISSON_ASSERT(b_min[i] < b_max[i]);
inside &= (p[i] >= b_min[i]);
inside &= (p[i] <= b_max[i]);
}
return inside;
}
/**
* @brief Add a sample, which is assumed here to fulfill all the Poisson requirements. Updates the
* necessary internal data structures and the context.
* @param ctx Context.
* @param internal Internal data.
* @param sample Sample to add.
* @return TPH_POISSON_SUCCESS, or a non-zero error code.
*/
static int tph_poisson_add_sample(tph_poisson_context *ctx,
tph_poisson_sampling_internal *internal,
const tph_poisson_real *sample)
{
TPH_POISSON_ASSERT(tph_poisson_inside(sample, ctx->bounds_min, ctx->bounds_max, ctx->ndims));
TPH_POISSON_ASSERT(
tph_poisson_vec_size(&internal->samples) % ((ptrdiff_t)sizeof(tph_poisson_real) * ctx->ndims)
== 0);
const ptrdiff_t sample_index =
tph_poisson_vec_size(&internal->samples) / ((ptrdiff_t)sizeof(tph_poisson_real) * ctx->ndims);
if ((uint32_t)sample_index == 0xFFFFFFFF) {
/* The sample index cannot be the same as the sentinel value of the grid. */
return TPH_POISSON_OVERFLOW;
}
int ret = tph_poisson_vec_append(&internal->samples,
&internal->alloc,
sample,
(ptrdiff_t)sizeof(tph_poisson_real) * ctx->ndims,
(ptrdiff_t)alignof(tph_poisson_real));
if (ret != TPH_POISSON_SUCCESS) { return ret; }
ret = tph_poisson_vec_append(&ctx->active_indices,
&internal->alloc,
&sample_index,
(ptrdiff_t)sizeof(ptrdiff_t),
(ptrdiff_t)alignof(ptrdiff_t));
if (ret != TPH_POISSON_SUCCESS) { return ret; }
/* Compute linear grid index. */
TPH_POISSON_ASSERT(ctx->grid_stride[0] == 1);
ptrdiff_t xi = (ptrdiff_t)TPH_POISSON_FLOOR((sample[0] - ctx->bounds_min[0]) * ctx->grid_dx_rcp);
TPH_POISSON_ASSERT((0 <= xi) & (xi < ctx->grid_size[0]));
ptrdiff_t k = xi;
for (int32_t i = 1; i < ctx->ndims; ++i) {
xi = (ptrdiff_t)TPH_POISSON_FLOOR((sample[i] - ctx->bounds_min[i]) * ctx->grid_dx_rcp);
TPH_POISSON_ASSERT((0 <= xi) & (xi < ctx->grid_size[i]));
/* Not checking for overflow! */
k += xi * ctx->grid_stride[i];
}
/* Record sample index in grid. Each grid cell can hold up to one sample,
* and once a cell has been assigned a sample it should not be updated.
* It is assumed here that the cell has its sentinel value before being
* assigned a sample index. */
TPH_POISSON_ASSERT(ctx->grid_cells[k] == 0xFFFFFFFF);
ctx->grid_cells[k] = (uint32_t)sample_index;
return TPH_POISSON_SUCCESS;
}
/**
* @brief Generate a pseudo-random sample position that is guaranteed be at a distance
* [radius, 2 * radius] from the provided center position.
* @param ctx Context.
* @param center Center position.
* @param sample Output sample position.
*/
static void tph_poisson_rand_annulus_sample(tph_poisson_context *ctx,
const tph_poisson_real *center,
tph_poisson_real *sample)
{
int32_t i = 0;
tph_poisson_real sqr_mag = 0;
for (;;) {
/* Generate a random component in the range [-2, 2] for each dimension.
* Use sample storage to temporarily store components. */
sqr_mag = 0;
for (i = 0; i < ctx->ndims; ++i) {
/* clang-format off */
sample[i] = (tph_poisson_real)(-2 + 4 * tph_poisson_to_double(
tph_poisson_xoshiro256p_next(&ctx->prng_state)));
/* clang-format on */
sqr_mag += sample[i] * sample[i];
}
/* The randomized offset is not guaranteed to be within the radial
* distance that we need to guarantee. If we found an offset with
* magnitude in the range (1, 2] we are done, otherwise generate a new
* offset. Continue until a valid offset is found. */
if (((int)((tph_poisson_real)1 < sqr_mag) & (int)(sqr_mag <= (tph_poisson_real)4)) == 1) {
/* Found a valid offset.
* Add the offset scaled by radius to the center coordinate to
* produce the final sample. */
for (i = 0; i < ctx->ndims; ++i) { sample[i] = center[i] + ctx->radius * sample[i]; }
break;
}
}
}
/**
* @brief Computes the grid index range in which the provided sample position needs to check for
* other samples that are possible closer than the radius (given in context).
* @param ctx Context.
* @param sample Input sample position.
* @param min_grid_index Minimum grid index.
* @param max_grid_index Maximum grid index.
*/
static void tph_poisson_grid_index_bounds(tph_poisson_context *ctx,
const tph_poisson_real *sample,
ptrdiff_t *min_grid_index,
ptrdiff_t *max_grid_index)
{
#ifdef TPH_POISSON_GRID_CLAMP
#error "TPH_POISSON_GRID_CLAMP already defined!"
#endif
/* clang-format off */
#define TPH_POISSON_GRID_CLAMP(grid_index, grid_size) \
if ((grid_index) < 0) { \
(grid_index) = 0; \
} else if ((grid_index) >= (grid_size)) { \
(grid_index) = (grid_size) - 1; \
}
/* clang-format on */
tph_poisson_real si = 0;
for (int32_t i = 0; i < ctx->ndims; ++i) {
TPH_POISSON_ASSERT(ctx->grid_size[i] > 0);
si = sample[i] - ctx->bounds_min[i];
min_grid_index[i] = (ptrdiff_t)TPH_POISSON_FLOOR((si - ctx->radius) * ctx->grid_dx_rcp);
max_grid_index[i] = (ptrdiff_t)TPH_POISSON_FLOOR((si + ctx->radius) * ctx->grid_dx_rcp);
TPH_POISSON_GRID_CLAMP(min_grid_index[i], ctx->grid_size[i]);
TPH_POISSON_GRID_CLAMP(max_grid_index[i], ctx->grid_size[i]);
}
#undef TPH_POISSON_GRID_CLAMP
}
/**
* @brief Returns true if there exists another sample within the radius used to
* construct the grid; otherwise false.
* @param ctx Context.
* @param sample Input sample position.
* @param active_sample_index Index of the existing sample that 'spawned' the sample tested here.
* @param min_grid_index Minimum grid index.
* @param max_grid_index Maximum grid index.
*/
static bool tph_poisson_existing_sample_within_radius(tph_poisson_context *ctx,
const tph_poisson_vec *samples,
const tph_poisson_real *sample,
const ptrdiff_t active_sample_index,
const ptrdiff_t *min_grid_index,
const ptrdiff_t *max_grid_index)
{
const tph_poisson_real r_sqr = ctx->radius * ctx->radius;
tph_poisson_real di = 0;
tph_poisson_real d_sqr = -1;
const tph_poisson_real *cell_sample = NULL;
int32_t i = -1;
ptrdiff_t k = -1;
bool test_cell = false;
const int32_t ndims = ctx->ndims;
TPH_POISSON_MEMCPY(
ctx->grid_index, min_grid_index, (size_t)(ndims * (ptrdiff_t)sizeof(ptrdiff_t)));
do {
/* Compute linear grid index. */
TPH_POISSON_ASSERT((0 <= ctx->grid_index[0]) & (ctx->grid_index[0] < ctx->grid_size[0]));
k = ctx->grid_index[0];
for (i = 1; i < ndims; ++i) {
/* Not checking for overflow! */
TPH_POISSON_ASSERT((0 <= ctx->grid_index[i]) & (ctx->grid_index[i] < ctx->grid_size[i]));
k += ctx->grid_index[i] * ctx->grid_stride[i];
}
test_cell = (ctx->grid_cells[k] != 0xFFFFFFFF);
test_cell &= (ctx->grid_cells[k] != (uint32_t)active_sample_index);
if (test_cell) {
/* Compute (squared) distance to the existing sample and then check if the existing sample is
* closer than (squared) radius to the provided sample. */
cell_sample =
(const tph_poisson_real *)samples->begin + (ptrdiff_t)ctx->grid_cells[k] * ndims;
di = sample[0] - cell_sample[0];
d_sqr = di * di;
for (i = 1; i < ndims; ++i) {
di = sample[i] - cell_sample[i];
d_sqr += di * di;
}
if (d_sqr < r_sqr) { return true; }
}
/* Iterate over grid index range. Enumerate every grid index between min_grid_index and
* max_grid_index (inclusive) exactly once. Assumes that min_index is element-wise less than or
* equal to max_index. */
for (i = 0; i < ndims; ++i) {
TPH_POISSON_ASSERT(min_grid_index[i] <= max_grid_index[i]);
ctx->grid_index[i]++;
if (ctx->grid_index[i] <= max_grid_index[i]) { break; }
ctx->grid_index[i] = min_grid_index[i];
}
/* If the above loop ran to completion, without triggering the break, the grid_index has been
* set to its original value (min_grid_index). Since this was the starting value for grid_index
* we exit the outer loop when this happens. */
} while (i != ndims);
/* No existing sample was found to be closer to the provided sample than the radius. */