aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/shared/internal/src/authoring/Noise.h
blob: 1b589a31ca5ab84eceb4fdd159b2974101c4156c (plain) (blame)
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
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */


#ifndef NOISE_H
#define NOISE_H

#include "authoring/ApexCSGMath.h"

#include "NoiseUtils.h"

#ifndef WITHOUT_APEX_AUTHORING
 
namespace ApexCSG 
{

class UserRandom;

/**
	Provides Perlin noise sampling across multiple dimensions and for different data types
*/
template<typename T, int SampleSize = 1024, int D = 3, class VecType = Vec<T, D> >
class PerlinNoise
{
public:
	PerlinNoise(UserRandom& rnd, int octaves = 1, T frequency = 1., T amplitude = 1.)
	  : mRnd(rnd),
		mOctaves(octaves),
		mFrequency(frequency),
		mAmplitude(amplitude),
		mbInit(false)
	{

	}

	void reset(int octaves = 1, T frequency = (T)1., T amplitude = (T)1.)
	{
		mOctaves   = octaves;
		mFrequency = frequency;
		mAmplitude = amplitude;
		init();
	}

	T sample(const VecType& point) 
	{
		return perlinNoise(point);
	}

private:
	PerlinNoise& operator=(const PerlinNoise&);

	T perlinNoise(VecType point)
	{
		if (!mbInit)
			init();

		const int octaves  = mOctaves;
		const T frequency  = mFrequency;
		T amplitude        = mAmplitude;
		T result           = (T)0;

		point *= frequency;

		for (int i = 0; i < octaves; ++i)
		{
			result    += noiseSample<T, SampleSize>(point, p, g) * amplitude;
			point     *= (T)2.0;
			amplitude *= (T)0.5;
		}

		return result;
	}

	void init(void)
	{
		mbInit = true;

		unsigned  i, j;
		int k;

		for (i = 0 ; i < (unsigned)SampleSize; i++)
		{
			p[i]  = (int)i;
			for (j = 0; j < D; ++j)
				g[i][j] = (T)((mRnd.getInt() % (SampleSize + SampleSize)) - SampleSize) / SampleSize;
			g[i].normalize();
		}

		while (--i)
		{
			k    = p[i];
			j = (unsigned)mRnd.getInt() % SampleSize;
			p[i] = p[j];
			p[j] = k;
		}

		for (i = 0 ; i < SampleSize + 2; ++i)
		{
			p [(unsigned)SampleSize + i] =  p[i];
			for (j = 0; j < D; ++j)
				g[(unsigned)SampleSize + i][j] = g[i][j];
		}

	}

	UserRandom& mRnd;
	int   mOctaves;
	T     mFrequency;
	T     mAmplitude;

	// Permutation vector
	int p[(unsigned)(SampleSize + SampleSize + 2)];
	// Gradient vector
	VecType g[(unsigned)(SampleSize + SampleSize + 2)];

	bool  mbInit;
};

}

#endif /* #ifndef WITHOUT_APEX_AUTHORING */

#endif /* #ifndef NOISE_H */