forked from jonathantompson/matlabnoise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mex.m
176 lines (149 loc) · 5.19 KB
/
test_mex.m
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
clearvars; clc; close all;
% This script will just plot the scalar fields and check the derivatives
% using FEM (central differences).
ydim = 512;
xdim = 511;
% 2D Grid
[v, u] = ndgrid(1:ydim, 1:xdim);
xstep = 1 / (0.5 * (xdim - 1));
ystep = 1 / (0.5 * (ydim - 1));
X = (u - 1) .* xstep - 1; % [-1, 1]
Y = (v - 1) .* ystep - 1; % [-1, 1]
% [ X, Y, xstep, ystep ] = ndgrid_normalized( xdim, ydim );
%HERMITE NOISE
figure;
noise = Hermite2D(X, Y);
imshow(noise, [min(noise(:)) max(noise(:))]);
title('Hermite2D');
% 2D Gradient
[noise_deriv, gradNoise] = Hermite2DDeriv(X, Y);
figure;
plot_2D_scalar_and_grad(noise_deriv, gradNoise);
title('Hermite2DDeriv');
err = abs(noise_deriv - noise);
assert(max(err(:)) < 1e-6, ...
'scalar field from Hermite2DDeriv does not match Hermite2D!');
% Check the gradient using finite differences (central)
epsilon = 1e-6;
[pos, ~] = Hermite2DDeriv(X + epsilon, Y);
[neg, ~] = Hermite2DDeriv(X - epsilon, Y);
dx = (pos - neg) / (2 * epsilon);
[pos, ~] = Hermite2DDeriv(X, Y + epsilon);
[neg, ~] = Hermite2DDeriv(X, Y - epsilon);
dy = (pos - neg) / (2 * epsilon);
gradNoiseFEM = permute(cat(3, dx, dy), [3 1 2]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-8, 'FEM derivative does not match!');
% Also check the gradient using Matla's approximation
[dx, dy] = gradient(noise_deriv, xstep, ystep);
gradNoiseFEM = permute(cat(3, dx, dy), [3 1 2]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-3, 'FEM derivative does not match!');
%PERLIN NOISE TESTS
figure;
noise = Perlin2D(X, Y);
imshow(noise, [min(noise(:)) max(noise(:))]);
title('Perlin2D');
% 2D Gradient
[noise_deriv, gradNoise] = Perlin2DDeriv(X, Y);
figure;
plot_2D_scalar_and_grad(noise_deriv, gradNoise);
title('Perlin2DDeriv');
err = abs(noise_deriv - noise);
assert(max(err(:)) < 1e-6, ...
'scalar field from Perlin2DDeriv does not match Perlin2D!');
% addpath('C:\Users\IggyMenou\Documents\NYU\detection_nets\matlab\export_fig');
% export_fig('grad_field.jpg', gcf, '-jpg', '-a4');
% Check the gradient using finite differences (central)
epsilon = 1e-6;
[pos, ~] = Perlin2DDeriv(X + epsilon, Y);
[neg, ~] = Perlin2DDeriv(X - epsilon, Y);
dx = (pos - neg) / (2 * epsilon);
[pos, ~] = Perlin2DDeriv(X, Y + epsilon);
[neg, ~] = Perlin2DDeriv(X, Y - epsilon);
dy = (pos - neg) / (2 * epsilon);
gradNoiseFEM = permute(cat(3, dx, dy), [3 1 2]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-8, 'FEM derivative does not match!');
% Also check the gradient using Matla's approximation
[dx, dy] = gradient(noise_deriv, xstep, ystep);
gradNoiseFEM = permute(cat(3, dx, dy), [3 1 2]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-3, 'FEM derivative does not match!');
% 3D Grid
xdim = 63;
ydim = 64;
zdim = 65;
[D, V, U] = ndgrid(1:zdim, 1:ydim, 1:xdim);
X = 2 * (U - 1) ./ (xdim - 1) - 1; % [-1, 1]
Y = 2 * (V - 1) ./ (ydim - 1) - 1; % [-1, 1]
Z = 2 * (D - 1) ./ (zdim - 1) - 1; % [-1, 1]
%HERMITE 3D NOISE
figure;
noise = Hermite3D(X, Y, Z);
plot_3D_scalar(noise);
title('Hermite3D');
% 3D Gradient
[noise_deriv, gradNoise] = Hermite3DDeriv(X, Y, Z);
err = abs(noise_deriv - noise);
assert(max(err(:)) < 1e-6, ...
'scalar field from Hermite3DDeriv does not match Hermite3D!');
figure;
plot_3D_scalar_and_grad(noise_deriv, gradNoise);
title('Hermite3DDeriv');
set(gcf,'Renderer','OpenGL')
title('Hermite3DDeriv');
% Check the gradient using finite differences (central)
epsilon = 1e-6;
[pos, ~] = Hermite3DDeriv(X + epsilon, Y, Z);
[neg, ~] = Hermite3DDeriv(X - epsilon, Y, Z);
dx = (pos - neg) / (2 * epsilon);
[pos, ~] = Hermite3DDeriv(X, Y + epsilon, Z);
[neg, ~] = Hermite3DDeriv(X, Y - epsilon, Z);
dy = (pos - neg) / (2 * epsilon);
[pos, ~] = Hermite3DDeriv(X, Y, Z + epsilon);
[neg, ~] = Hermite3DDeriv(X, Y, Z - epsilon);
dz = (pos - neg) / (2 * epsilon);
gradNoiseFEM = permute(cat(4, dx, dy, dz), [4 1 2 3]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-8, 'FEM derivative does not match!');
%PERLIN 3D NOISE
figure;
noise = Perlin3D(X, Y, Z);
plot_3D_scalar(noise);
title('Perlin3D');
% 3D Gradient
[noise_deriv, gradNoise] = Perlin3DDeriv(X, Y, Z);
err = abs(noise_deriv - noise);
assert(max(err(:)) < 1e-6, ...
'scalar field from Perlin3DDeriv does not match Perlin3D!');
figure;
plot_3D_scalar_and_grad(noise_deriv, gradNoise);
title('Perlin3DDeriv');
set(gcf,'Renderer','OpenGL')
title('Perlin3DDeriv');
% Check the gradient using finite differences (central)
epsilon = 1e-6;
[pos, ~] = Perlin3DDeriv(X + epsilon, Y, Z);
[neg, ~] = Perlin3DDeriv(X - epsilon, Y, Z);
dx = (pos - neg) / (2 * epsilon);
[pos, ~] = Perlin3DDeriv(X, Y + epsilon, Z);
[neg, ~] = Perlin3DDeriv(X, Y - epsilon, Z);
dy = (pos - neg) / (2 * epsilon);
[pos, ~] = Perlin3DDeriv(X, Y, Z + epsilon);
[neg, ~] = Perlin3DDeriv(X, Y, Z - epsilon);
dz = (pos - neg) / (2 * epsilon);
gradNoiseFEM = permute(cat(4, dx, dy, dz), [4 1 2 3]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-8, 'FEM derivative does not match!');
% Now just do a time series of a 3D grid (because I can't visualize 4D
% noise :-) )
figure;
for i = 1:4
subplot(2,2,i);
W = ones(size(X,1), size(X,2), size(X,3)) * i;
noise = Perlin4D(X, Y, Z, W);
plot_3D_scalar(noise);
title(['Perlin4D w=', num2str(i)]);
end
disp('Tests pass!');