forked from jonathantompson/matlabnoise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perlin2D.cpp
61 lines (53 loc) · 1.66 KB
/
Perlin2D.cpp
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
#include <stdio.h>
#include <string>
#include "noise_common.h"
#include "stdint.h"
#include "mex.h"
using namespace std;
// The gateway function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
// Only 2 inputs allowed
if (nrhs != 2) {
mexErrMsgIdAndTxt("MATLAB:Perlin2D:invalidNumInputs",
"Input must be X, Y");
}
// input must be X, Y
for (int i = 0; i < 2; i++) {
if (mxIsDouble(prhs[i]) != 1) {
mexErrMsgIdAndTxt("MATLAB:Perlin2D:notDouble",
"Inputs must be double.");
}
}
// Check that the inputs are the same dimension
for (int i = 1; i < 2; i++) {
if (mxGetNumberOfDimensions(prhs[i]) != mxGetNumberOfDimensions(prhs[0]) ||
mxGetNumberOfElements(prhs[i]) != mxGetNumberOfElements(prhs[0])) {
mexErrMsgIdAndTxt("MATLAB:Perlin2D:badSize",
"Inputs must be the same size.");
}
size_t K = mxGetNumberOfDimensions(prhs[i]);
const mwSize* Ni = mxGetDimensions(prhs[i]);
const mwSize* N0 = mxGetDimensions(prhs[0]);
for (size_t j = 0; j < K; j++) {
if (Ni[j] != N0[j]) {
mexErrMsgIdAndTxt("MATLAB:Perlin2D:badSize",
"Inputs must be the same size.");
}
}
}
// Only 1 input allowed
if (nlhs != 1) {
mexErrMsgIdAndTxt("MATLAB:Perlin2D:invalidNumOutputs",
"One output is required");
}
// Allocate the output
mxArray* fout = mxDuplicateArray(prhs[0]);
plhs[0] = fout;
size_t npts = mxGetNumberOfElements(prhs[0]);
double* X = mxGetPr(prhs[0]);
double* Y = mxGetPr(prhs[1]);
double* out = mxGetPr(fout);
for (size_t i = 0; i < npts; i++) {
out[i] = Perlin2D(vec2(X[i], Y[i]));
}
}