YafaRay Core  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
noise.h
Go to the documentation of this file.
1 #ifndef __NOISE_H
2 #define __NOISE_H
3 
4 #include <core_api/vector3d.h>
5 #include <core_api/color.h>
6 
7 #include <yafray_config.h>
8 
10 
12 {
13 public:
15  virtual ~noiseGenerator_t() {}
16  virtual float operator() (const point3d_t &pt) const=0;
17  // offset only added by blendernoise
18  virtual point3d_t offset(const point3d_t &pt) const { return pt; }
19 };
20 
21 //---------------------------------------------------------------------------
22 // Improved Perlin noise, based on Java reference code by Ken Perlin himself.
24 {
25 public:
27  virtual ~newPerlin_t() {}
28  virtual float operator() (const point3d_t &pt) const;
29 private:
30  float fade(float t) const { return t*t*t*(t*(t*6 - 15) + 10); }
31  float grad(int hash, float x, float y, float z) const
32  {
33  int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
34  float u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
35  v = h<4 ? y : h==12||h==14 ? x : z;
36  return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
37  }
38 };
39 
40 //---------------------------------------------------------------------------
41 // Standard Perlin noise.
43 {
44 public:
46  virtual ~stdPerlin_t() {}
47  virtual float operator() (const point3d_t &pt) const;
48 };
49 
50 // Blender noise, similar to Perlin's
52 {
53 public:
55  virtual ~blenderNoise_t() {}
56  virtual float operator() (const point3d_t &pt) const;
57  // offset texture point coordinates by one
58  virtual point3d_t offset(const point3d_t &pt) const { return pt+point3d_t(1.0, 1.0, 1.0); }
59 };
60 
61 //---------------------------------------
62 // Voronoi, a.k.a. Worley/cellular basis
63 
64 typedef float (*distMetricFunc)(float x, float y, float z, float e);
65 // distance metrics as functors
66 /*struct distanceMetric_t
67 {
68  virtual float operator() (float x, float y, float z, float e)=0;
69 };
70 
71 struct dist_Real : public distanceMetric_t
72 {
73  virtual float operator() (float x, float y, float z, float e) { return sqrt(x*x + y*y + z*z); }
74 };
75 
76 struct dist_Squared : public distanceMetric_t
77 {
78  virtual float operator() (float x, float y, float z, float e) { return (x*x + y*y + z*z); }
79 };
80 
81 struct dist_Manhattan : public distanceMetric_t
82 {
83  virtual float operator() (float x, float y, float z, float e) { return (std::fabs(x) + std::fabs(y) + std::fabs(z)); }
84 };
85 
86 struct dist_Chebychev : public distanceMetric_t
87 {
88  virtual float operator() (float x, float y, float z, float e)
89  {
90  x = std::fabs(x);
91  y = std::fabs(y);
92  z = std::fabs(z);
93  float t = (x>y)?x:y;
94  return ((z>t)?z:t);
95  }
96 };
97 
98 // minkovsky preset exponent 0.5
99 struct dist_MinkovskyH : public distanceMetric_t
100 {
101  virtual float operator() (float x, float y, float z, float e)
102  {
103  float d = sqrt(std::fabs(x)) + sqrt(std::fabs(y)) + sqrt(std::fabs(z));
104  return (d*d);
105  }
106 };
107 
108 // minkovsky preset exponent 4
109 struct dist_Minkovsky4 : public distanceMetric_t
110 {
111  virtual float operator() (float x, float y, float z, float e)
112  {
113  x *= x;
114  y *= y;
115  z *= z;
116  return sqrt(sqrt(x*x + y*y + z*z));
117  }
118 };
119 
120 // Minkovsky, general case, slow
121 struct dist_Minkovsky : public distanceMetric_t
122 {
123  virtual float operator() (float x, float y, float z, float e)
124  {
125  return pow(pow(std::fabs(x), e) + pow(std::fabs(y), e) + pow(std::fabs(z), e), (float)1.0/e);
126  }
127 };
128 */
130 {
131 public:
132  enum voronoiType {V_F1, V_F2, V_F3, V_F4, V_F2F1, V_CRACKLE};
133  enum dMetricType {DIST_REAL, DIST_SQUARED, DIST_MANHATTAN, DIST_CHEBYCHEV,
134  DIST_MINKOVSKY_HALF, DIST_MINKOVSKY_FOUR, DIST_MINKOVSKY};
135  voronoi_t(voronoiType vt=V_F1, dMetricType dm=DIST_REAL, float mex=2.5);
136  virtual ~voronoi_t()
137  {
138  //if (distfunc) { delete distfunc; distfunc=nullptr; }
139  }
140  virtual float operator() (const point3d_t &pt) const;
141  float getDistance(int x, float da[4]) const { return da[x & 3]; }
142  point3d_t getPoint(int x, point3d_t pa[4]) const { return pa[x & 3]; }
143  void setMinkovskyExponent(float me) { mk_exp=me; }
144  void getFeatures(const point3d_t &pt, float da[4], point3d_t pa[4]) const;
145  void setDistM(dMetricType dm);
146 protected:
149  float mk_exp, w1, w2, w3,w4;
150 // distanceMetric_t* distfunc; //test...replace functors
152 // mutable float da[4]; // distance array
153 // mutable point3d_t pa[4]; // feature point array
154 };
155 
156 // cell noise
158 {
159 public:
161  virtual ~cellNoise_t() {}
162  virtual float operator() (const point3d_t &pt) const;
163 };
164 
165 //------------------
166 // Musgrave types
167 
169 {
170 public:
172  virtual ~musgrave_t() {}
173  virtual float operator() (const point3d_t &pt) const=0;
174 };
175 
177 {
178 public:
179  fBm_t(float _H, float _lacu, float _octs, const noiseGenerator_t* _nGen)
180  : H(_H), lacunarity(_lacu), octaves(_octs), nGen(_nGen) {}
181  virtual ~fBm_t() {}
182  virtual float operator() (const point3d_t &pt) const;
183 protected:
184  float H, lacunarity, octaves;
186 };
187 
189 {
190 public:
191  mFractal_t(float _H, float _lacu, float _octs, const noiseGenerator_t* _nGen)
192  : H(_H), lacunarity(_lacu), octaves(_octs), nGen(_nGen) {}
193  virtual ~mFractal_t() {}
194  virtual float operator() (const point3d_t &pt) const;
195 protected:
196  float H, lacunarity, octaves;
198 };
199 
201 {
202 public:
203  heteroTerrain_t(float _H, float _lacu, float _octs, float _offs, const noiseGenerator_t* _nGen)
204  : H(_H), lacunarity(_lacu), octaves(_octs), offset(_offs), nGen(_nGen) {}
205  virtual ~heteroTerrain_t() {}
206  virtual float operator() (const point3d_t &pt) const;
207 protected:
208  float H, lacunarity, octaves, offset;
210 };
211 
213 {
214 public:
215  hybridMFractal_t(float _H, float _lacu, float _octs, float _offs, float _gain, const noiseGenerator_t* _nGen)
216  : H(_H), lacunarity(_lacu), octaves(_octs), offset(_offs), gain(_gain), nGen(_nGen) {}
217  virtual ~hybridMFractal_t() {}
218  virtual float operator() (const point3d_t &pt) const;
219 protected:
220  float H, lacunarity, octaves, offset, gain;
222 };
223 
225 {
226 public:
227  ridgedMFractal_t(float _H, float _lacu, float _octs, float _offs, float _gain, const noiseGenerator_t* _nGen)
228  : H(_H), lacunarity(_lacu), octaves(_octs), offset(_offs), gain(_gain), nGen(_nGen) {}
229  virtual ~ridgedMFractal_t() {}
230  virtual float operator() (const point3d_t &pt) const;
231 protected:
232  float H, lacunarity, octaves, offset, gain;
234 };
235 
236 
237 // basic turbulence, half amplitude, double frequency defaults
238 // returns value in range (0,1)
239 float YAFRAYPLUGIN_EXPORT turbulence(const noiseGenerator_t* ngen, const point3d_t &pt, int oct, float size, bool hard);
240 // noise cell color (used with voronoi)
242 
243 static inline float getSignedNoise(const noiseGenerator_t* nGen, const point3d_t &pt)
244 {
245  return (float)2.0 * (*nGen)(pt) - (float)1.0;
246 }
247 
248 
250 //---------------------------------------------------------------------------
251 #endif //__NOISE_H
virtual ~hybridMFractal_t()
Definition: noise.h:217
float offset
Definition: noise.h:208
float YAFRAYPLUGIN_EXPORT turbulence(const noiseGenerator_t *ngen, const point3d_t &pt, int oct, float size, bool hard)
Definition: noise.cc:758
colorA_t YAFRAYPLUGIN_EXPORT cellNoiseColor(const point3d_t &pt)
Definition: noise.cc:748
stdPerlin_t()
Definition: noise.h:45
virtual ~musgrave_t()
Definition: noise.h:172
float getDistance(int x, float da[4]) const
Definition: noise.h:141
float octaves
Definition: noise.h:196
newPerlin_t()
Definition: noise.h:26
virtual ~voronoi_t()
Definition: noise.h:136
void setMinkovskyExponent(float me)
Definition: noise.h:143
mFractal_t(float _H, float _lacu, float _octs, const noiseGenerator_t *_nGen)
Definition: noise.h:191
blenderNoise_t()
Definition: noise.h:54
#define __BEGIN_YAFRAY
musgrave_t()
Definition: noise.h:171
distMetricFunc distfunc2
Definition: noise.h:151
dMetricType dmType
Definition: noise.h:148
virtual ~noiseGenerator_t()
Definition: noise.h:15
float offset
Definition: noise.h:232
voronoiType vType
Definition: noise.h:147
#define YAFRAYPLUGIN_EXPORT
hybridMFractal_t(float _H, float _lacu, float _octs, float _offs, float _gain, const noiseGenerator_t *_nGen)
Definition: noise.h:215
point3d_t getPoint(int x, point3d_t pa[4]) const
Definition: noise.h:142
virtual ~stdPerlin_t()
Definition: noise.h:46
fBm_t(float _H, float _lacu, float _octs, const noiseGenerator_t *_nGen)
Definition: noise.h:179
heteroTerrain_t(float _H, float _lacu, float _octs, float _offs, const noiseGenerator_t *_nGen)
Definition: noise.h:203
virtual ~mFractal_t()
Definition: noise.h:193
Definition: noise.h:176
ridgedMFractal_t(float _H, float _lacu, float _octs, float _offs, float _gain, const noiseGenerator_t *_nGen)
Definition: noise.h:227
const noiseGenerator_t * nGen
Definition: noise.h:209
virtual float operator()(const point3d_t &pt) const =0
voronoiType
Definition: noise.h:132
noiseGenerator_t()
Definition: noise.h:14
virtual ~fBm_t()
Definition: noise.h:181
float offset
Definition: noise.h:220
float octaves
Definition: noise.h:184
virtual ~cellNoise_t()
Definition: noise.h:161
virtual point3d_t offset(const point3d_t &pt) const
Definition: noise.h:18
const noiseGenerator_t * nGen
Definition: noise.h:185
cellNoise_t()
Definition: noise.h:160
virtual ~heteroTerrain_t()
Definition: noise.h:205
const noiseGenerator_t * nGen
Definition: noise.h:233
virtual ~blenderNoise_t()
Definition: noise.h:55
virtual ~newPerlin_t()
Definition: noise.h:27
const noiseGenerator_t * nGen
Definition: noise.h:221
float(* distMetricFunc)(float x, float y, float z, float e)
Definition: noise.h:64
const noiseGenerator_t * nGen
Definition: noise.h:197
virtual float operator()(const point3d_t &pt) const =0
virtual ~ridgedMFractal_t()
Definition: noise.h:229
virtual point3d_t offset(const point3d_t &pt) const
Definition: noise.h:58
dMetricType
Definition: noise.h:133
float w4
Definition: noise.h:149
#define __END_YAFRAY