annotate src/dmperlin.c @ 2289:81b561abb6e9

Cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 27 Jun 2019 14:19:33 +0300
parents 848a88ce7a57
children 69a5af2eb1ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
2 * DMLib
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
3 * -- Perlin noise functionality
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
4 * Programmed and designed by Matti 'ccr' Hamalainen
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
5 * (C) Copyright 2012 Tecnic Software productions (TNSP)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 */
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
7 #include "dmperlin.h"
979
76e8dcfeb808 Curve macros are needed in dmperlin module.
Matti Hamalainen <ccr@tnsp.org>
parents: 889
diff changeset
8 #include "dmcurves.h"
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <math.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
639
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
12 #define DM_PERLIN_SETUP(i, b0, b1, r0, r1) \
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
13 { \
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
14 t = (vec[i] + DM_PLNS_N); \
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
15 b0 = (((int) t) & DM_PLNS_BM); \
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
16 b1 = ((b0 + 1) & DM_PLNS_BM); \
639
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17 r0 = (t - ((int) t)); \
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
18 r1 = (r0 - 1.0f); \
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
19 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 #define DM_PERLIN_AT2(rx,ry) ((rx) * q[0] + (ry) * q[1])
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
24 static DMFloat dmPerlinDoNoise2(const DMPerlinContext *ctx, const DMFloat vec[2])
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 int bx0, bx1, by0, by1, b00, b10, b01, b11;
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
27 DMFloat rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
28 const DMFloat *q;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 int i, j;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 DM_PERLIN_SETUP(0, bx0, bx1, rx0, rx1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 DM_PERLIN_SETUP(1, by0, by1, ry0, ry1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
34 i = ctx->p[bx0];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
35 j = ctx->p[bx1];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
37 b00 = ctx->p[i + by0];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
38 b10 = ctx->p[j + by0];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
39 b01 = ctx->p[i + by1];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
40 b11 = ctx->p[j + by1];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 sx = DMM_S_CURVE(rx0);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 sy = DMM_S_CURVE(ry0);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
45 q = ctx->g2[b00];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 u = DM_PERLIN_AT2(rx0, ry0);
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
47
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
48 q = ctx->g2[b10];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 v = DM_PERLIN_AT2(rx1, ry0);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 a = DMM_LERP(sx, u, v);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
52 q = ctx->g2[b01];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 u = DM_PERLIN_AT2(rx0, ry1);
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
54
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
55 q = ctx->g2[b11];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 v = DM_PERLIN_AT2(rx1, ry1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 b = DMM_LERP(sx, u, v);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 return DMM_LERP(sy, a, b);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 static void dmPerlinNormalize2(DMFloat v[2])
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 {
639
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
65 DMFloat s = sqrt(v[0] * v[0] + v[1] * v[1]);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 v[0] /= s;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 v[1] /= s;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
71 int dmPerlinInit(DMPerlinContext *ctx, const int seed)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
73 int i, j;
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
74
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
75 if (ctx == NULL)
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
76 return DMERR_NULLPTR;
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
77
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 979
diff changeset
78 dmMemset(ctx, 0, sizeof(*ctx));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
80 if (seed < 0)
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
81 return DMERR_INVALID_ARGS;
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
82
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
83 srand(seed);
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
84
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
85 for (i = 0; i < DM_PLNS_B; i++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 {
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
87 ctx->p[i] = i;
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
88 ctx->g1[i] = (DMFloat) ((rand() % (DM_PLNS_B + DM_PLNS_B)) - DM_PLNS_B) / DM_PLNS_B;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 for (j = 0; j < 2; j++)
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
91 ctx->g2[i][j] = (DMFloat) ((rand() % (DM_PLNS_B + DM_PLNS_B)) - DM_PLNS_B) / DM_PLNS_B;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
93 dmPerlinNormalize2(ctx->g2[i]);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 while (--i)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 {
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
98 int k = ctx->p[i];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
99 ctx->p[i] = ctx->p[j = rand() % DM_PLNS_B];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
100 ctx->p[j] = k;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
103 for (i = 0; i < DM_PLNS_B + 2; i++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
105 ctx->p[DM_PLNS_B + i] = ctx->p[i];
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
106 ctx->g1[DM_PLNS_B + i] = ctx->g1[i];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 for (j = 0; j < 2; j++)
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
109 ctx->g2[DM_PLNS_B + i][j] = ctx->g2[i][j];
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 }
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
111
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
112 return DMERR_OK;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 /* Harmonic summing functions - PDB
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 * In what follows "alpha" is the weight when the sum is formed.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 * Typically it is 2, As this approaches 1 the function is noisier.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 * "beta" is the harmonic scaling/spacing, typically 2.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 */
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
121 DMFloat dmPerlinNoise2D(const DMPerlinContext *ctx, DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 DMFloat val, sum = 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 DMFloat p[2], scale = 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 p[0] = x;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 p[1] = y;
2289
81b561abb6e9 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
128 for (int i = 0; i < n; i++)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 {
889
e5fde730b4fa Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
130 val = dmPerlinDoNoise2(ctx, p);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 sum += val / scale;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 scale *= alpha;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 p[0] *= beta;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 p[1] *= beta;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
639
62f5861d332b Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
137 return sum;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 }