Source code: org/maloi/evolvo/expressiontree/operators/Noise.java
1 /* Evolvo - Image Generator
2 * Copyright (C) 2000 Andrew Molloy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /**
20 * $Id: Noise.java,v 1.1.1.1 2002/10/04 15:39:32 maloi Exp $
21 */
22
23 package org.maloi.evolvo.expressiontree.operators;
24
25 import java.io.Serializable;
26 import java.util.Random;
27
28 import org.maloi.evolvo.expressiontree.vm.Stack;
29
30 /**
31 * Returns the value of a point in a 3 dimensional noise field
32 */
33 public class Noise implements OperatorInterface, Serializable
34 {
35 /** The size of each dimension of our noise table. */
36 static int maxNoise = 100;
37 /** The actual noise table. */
38 static double noiseTable[][][] = new double[maxNoise][maxNoise][maxNoise];
39
40 /** Performs any initialization the operator requires. */
41 public void init()
42 {
43 int ix, iy, iz;
44 int xx, yy, zz;
45
46 Random r = new Random();
47
48 for (ix = 0; ix < maxNoise; ix++)
49 {
50 for (iy = 0; iy < maxNoise; iy++)
51 {
52 for (iz = 0; iz < maxNoise; iz++)
53 {
54 noiseTable[ix][iy][iz] = r.nextDouble() * 2.0 - 1.0;
55 if (ix == maxNoise)
56 {
57 xx = 0;
58 }
59 else
60 {
61 xx = ix;
62 }
63 if (iy == maxNoise)
64 {
65 yy = 0;
66 }
67 else
68 {
69 yy = iy;
70 }
71 if (iz == maxNoise)
72 {
73 zz = 0;
74 }
75 else
76 {
77 zz = iz;
78 }
79 noiseTable[ix][iy][iz] = noiseTable[xx][yy][zz];
80 }
81 }
82 }
83 }
84
85 /** Returns the fractional part of a number. */
86 double frac(double r)
87 {
88 return (r - (double) (int) r);
89 }
90
91 /** Perform the operation. */
92 public void perform(Stack theStack)
93 {
94 int xx, yy, zz;
95 double x, y, z;
96 int ix, iy, iz;
97 double ox, oy, oz;
98 double n;
99 double n00, n01, n10, n11;
100 double n0, n1;
101
102 x = ((theStack.pop() + 1.0) / 2.0) * (maxNoise - 2.0);
103 y = ((theStack.pop() + 1.0) / 2.0) * (maxNoise - 2.0);
104 z = ((theStack.pop() + 1.0) / 2.0) * (maxNoise - 2.0);
105
106 ix = (int) x % (maxNoise - 2) + 1;
107 iy = (int) y % (maxNoise - 2) + 1;
108 iz = (int) z % (maxNoise - 2) + 1;
109
110 ox = frac(x);
111 oy = frac(y);
112 oz = frac(z);
113
114 n = noiseTable[ix][iy][iz];
115 n00 = n + ox * (noiseTable[ix + 1][iy][iz] - n);
116 n = noiseTable[ix][iy][iz + 1];
117 n01 = n + ox * (noiseTable[ix + 1][iy][iz - 1] - n);
118 n = noiseTable[ix][iy + 1][iz];
119 n10 = n + ox * (noiseTable[ix + 1][iy + 1][iz] - n);
120 n = noiseTable[ix][iy + 1][iz + 1];
121 n11 = n + ox * (noiseTable[ix + 1][iy + 1][iz + 1] - n);
122
123 n0 = n00 + oy * (n10 - n00);
124 n1 = n01 + oy * (n11 - n01);
125 theStack.push((n0 + oz * (n1 - n0)));
126 }
127
128 /** Returns the operator's name. */
129 public String getName()
130 {
131 return "noise";
132 }
133
134 /** Returns the number of parameters expected by the operator. */
135 public int getNumberOfParameters()
136 {
137 return 3;
138 }
139 }