001package squidpony.squidmath;
002
003/**
004 * A noise generator for 1D, 2D, 3D, 4D, or 6D noise that should look "glitchy", with waves of changing values moving
005 * through triangular shapes. Intended for aesthetic purposes where something needs to look inorganic, unlike Perlin
006 * or Simplex noise.
007 */
008public class GlitchNoise implements Noise.Noise1D, Noise.Noise2D, Noise.Noise3D, Noise.Noise4D, Noise.Noise6D {
009
010    public static final GlitchNoise instance = new GlitchNoise();
011    public static double zigzagRandomized(long seed, double value)
012    {
013        final long floor = value >= 0.0 ? (long) value : (long) value - 1L;
014        final double start = (((seed += floor * 0x6C8E9CF570932BD5L) ^ (seed >>> 25)) * (seed | 0xA529L)) * 0x0.fffffffffffffbp-62,
015                end = (((seed += 0x6C8E9CF570932BD5L) ^ (seed >>> 25)) * (seed | 0xA529L)) * 0x0.fffffffffffffbp-62;
016        value -= floor;
017//        value *= value * (3.0 - 2.0 * value);
018        return (1.0 - value) * start + value * end;
019    }
020
021    public GlitchNoise()
022    {
023    }
024    @Override
025    public double getNoise(final double x) {
026        return getNoiseWithSeed(x, 0xAEF17502108EF2D9L);
027//        return NumberTools.randomSignedDouble((((long)(x + zigzagRandomized(0xAEF17502108EF2D9L, -x)) * 0x6C8E9CF570932BD5L
028//        ) >>> 32));
029
030//        return NumberTools.bounce(
031//                TangleRNG.determine((long)(x*0x0.9E3779B9p4 - x*0x0.8329C6DFp3 + x*0x0.953976F9p2))
032//                        ^ TangleRNG.determine((long)(x*0x0.953976F9p4 + x*0x0.8329C6DFp3 - x*0x0.9E3779B9p2)));
033    }
034    @Override
035    public double getNoiseWithSeed(final double x, final long seed) {
036        return NumberTools.randomSignedDouble((((long)(x + zigzagRandomized(seed, -x)) * 0x6C8E9CF570932BD5L
037                ) >>> 32) ^ seed);
038
039//        return NumberTools.bounce(
040//                TangleRNG.determine((long)(x*0x0.9E3779B9p4 - x*0x0.8329C6DFp3 + x*0x0.953976F9p2) + (seed & 0xA5A5A5A5))
041//                        ^ TangleRNG.determine((long)(x*0x0.953976F9p4 + x*0x0.8329C6DFp3 - x*0x0.9E3779B9p2) + (seed & 0x5A5A5A5A)));
042    }
043    @Override
044    public double getNoise(final double x, final double y) {
045        return getNoiseWithSeed(x, y, 0xAEF17502108EF2D9L);//        return NumberTools.bounce(
046//                TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 - x*0x0.953976F9p2))
047//                        ^ TangleRNG.determine((long)(y*0x0.953976F9p4 + x*0x0.9E3779B9p3 - y*0x0.8329C6DFp2))
048//                        ^ TangleRNG.determine((long)(x*0x0.85157AF5p4 - y*0x2.9E3779B9p2 + x*0x4.953976F9p0))
049//                        ^ TangleRNG.determine((long)(y*0x0.953976F9p4 - x*0x2.8329C6DFp2 + y*0x4.85157AF5p0)));
050    }
051    @Override
052    public double getNoiseWithSeed(final double x, final double y, final long seed) {
053        return NumberTools.randomSignedDouble((((long)(x + zigzagRandomized(seed, y)) * 0x6C8E9CF570932BD5L
054                ^ (long)(y + zigzagRandomized(seed + 0xAEF17502108EF2D9L, x)) * 0x9E3779B97F4A7C15L) >>> 32)
055                ^ seed);
056//        return NumberTools.bounce(
057//                TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 - x*0x0.953976F9p2) + (seed & 0xA5A5A5A5))
058//                        ^ TangleRNG.determine((long)(y*0x0.953976F9p4 + x*0x0.9E3779B9p3 - y*0x0.8329C6DFp2) + (seed & 0x5A5A5A5A))
059//                        ^ TangleRNG.determine((long)(x*0x0.85157AF5p4 - y*0x2.9E3779B9p2 - x*0x4.953976F9p0) + (seed & 0x3C3C3C3C))
060//                        ^ TangleRNG.determine((long)(y*0x0.953976F9p4 - x*0x2.8329C6DFp2 - y*0x4.85157AF5p0) + (seed & 0xC3C3C3C3)));
061    }
062    @Override
063    public double getNoise(final double x, final double y, final double z) {
064        return getNoiseWithSeed(x, y, z, 0xAEF17502108EF2D9L);//        return NumberTools.randomSignedDouble(NumberTools.doubleToLongBits(x) * 0x9E3779B97F4A7C15L
065//                + NumberTools.doubleToLongBits(y) * 0x6C8E9CF570932BD5L
066//                + NumberTools.doubleToLongBits(z) * 0xC6BC279692B5CC85L);
067//        return NumberTools.bounce(
068//                TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 + z*0x0.953976F9p2))
069//                        ^ TangleRNG.determine((long)(y*0x0.953976F9p4 + z*0x0.9E3779B9p3 + x*0x0.8329C6DFp2))
070//                        ^ TangleRNG.determine((long)(z*0x0.8329C6DFp4 + x*0x0.953976F9p3 + y*0x0.9E3779B9p2))
071//                        ^ TangleRNG.determine((long)(x*0x0.85157AF5p4 - y*0x2.9E3779B9p1 - z*0x3.953976F9p0))
072//                        ^ TangleRNG.determine((long)(y*0x0.85157AF5p4 - z*0x2.8329C6DFp1 - y*0x3.9E3779B9p0))
073//                        ^ TangleRNG.determine((long)(z+0x0.85157AF5p4 - x*0x2.953976F9p1 - y*0x3.8329C6DFp0)));
074    }
075    @Override
076    public double getNoiseWithSeed(final double x, final double y, final double z, final long seed) {
077        return NumberTools.randomSignedDouble((((long)(x + zigzagRandomized(seed, y) + zigzagRandomized(seed, z)) * 0x6C8E9CF570932BD5L
078                ^ (long)(y + zigzagRandomized(seed + 0xAEF17502108EF2D9L, x) + zigzagRandomized(seed + 0xAEF17502108EF2D9L, z)) * 0x9E3779B97F4A7C15L
079                ^ (long)(z + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, x) + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, y)) * 0xC6BC279692B5CC85L
080        ) >>> 32)
081                ^ seed);
082//        return NumberTools.randomSignedDouble(NumberTools.doubleToLongBits(x) * 0x9E3779B97F4A7C15L
083//                + NumberTools.doubleToLongBits(y) * 0x6C8E9CF570932BD5L
084//                + NumberTools.doubleToLongBits(z) * 0xC6BC279692B5CC85L 
085//                ^ seed);
086//        return NumberTools.bounce(
087//                TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 + z*0x0.953976F9p2) + (seed & 0x24924924))
088//                        ^ TangleRNG.determine((long)(y*0x0.953976F9p4 + z*0x0.9E3779B9p3 + x*0x0.8329C6DFp2) + (seed & 0x49249249))
089//                        ^ TangleRNG.determine((long)(z*0x0.8329C6DFp4 + x*0x0.953976F9p3 + y*0x0.9E3779B9p2) + (seed & 0x92492492))
090//                        ^ TangleRNG.determine((long)(x*0x0.85157AF5p4 - y*0x2.9E3779B9p1 - z*0x3.953976F9p0) + (seed & 0x49249249))
091//                        ^ TangleRNG.determine((long)(y*0x0.85157AF5p4 - z*0x2.8329C6DFp1 - y*0x3.9E3779B9p0) + (seed & 0x92492492))
092//                        ^ TangleRNG.determine((long)(z+0x0.85157AF5p4 - x*0x2.953976F9p1 - y*0x3.8329C6DFp0) + (seed & 0x24924924)));
093    }
094    @Override
095    public double getNoise(final double x, final double y, final double z, final double w) {
096        return getNoiseWithSeed(x, y, z, w, 0xAEF17502108EF2D9L);
097        //        return NumberTools.bounce(
098//                (TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 + w*0x0.953976F9p2))
099//                        ^ TangleRNG.determine((long)(y*0x0.85157AF5p4 + z*0x0.9E3779B9p3 + x*0x0.8329C6DFp2))
100//                        ^ TangleRNG.determine((long)(z*0x0.953976F9p4 + w*0x0.85157AF5p3 + y*0x0.9E3779B9p2))
101//                        ^ TangleRNG.determine((long)(w*0x0.8329C6DFp4 + x*0x0.953976F9p3 + z*0x0.85157AF5p2))
102//                ));
103    }
104    @Override
105    public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final long seed) {
106        return NumberTools.randomSignedDouble((((long)(x 
107                + zigzagRandomized(seed, y) 
108                + zigzagRandomized(seed, z) 
109                + zigzagRandomized(seed, w)) * 0x6C8E9CF570932BD5L
110                ^ (long)(y
111                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, x)
112                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, z)
113                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, w)) * 0x9E3779B97F4A7C15L
114                ^ (long)(z
115                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, x)
116                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, y)
117                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, w)) * 0xC6BC279692B5CC85L
118                ^ (long)(w
119                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, x)
120                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, y)
121                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, z)) * 0x352E9CF570932BDDL
122        ) >>> 32)
123                ^ seed);
124
125//        return NumberTools.bounce(
126//                (TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 + w*0x0.953976F9p2) + (seed & 0x81818181))
127//                        ^ TangleRNG.determine((long)(y*0x0.85157AF5p4 + z*0x0.9E3779B9p3 + x*0x0.8329C6DFp2) + (seed & 0x18181818))
128//                        ^ TangleRNG.determine((long)(z*0x0.953976F9p4 + w*0x0.85157AF5p3 + y*0x0.9E3779B9p2) + (seed & 0x42424242))
129//                        ^ TangleRNG.determine((long)(w*0x0.8329C6DFp4 + x*0x0.953976F9p3 + z*0x0.85157AF5p2) + (seed & 0x24242424))
130//                        ));
131    }
132    @Override
133    public double getNoise(final double x, final double y, final double z, final double w, final double u, final double v) {
134        return getNoiseWithSeed(x, y, z, w, u, v, 0xAEF17502108EF2D9L);
135
136//        return NumberTools.bounce(
137//                (TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 + z*0x0.953976F9p2 + w*0x0.85157AF5p1))
138//                        ^ TangleRNG.determine((long)(y*0x0.712BE5ABp4 + z*0x0.9E3779B9p3 + x*0x0.8329C6DFp2 + u*0x0.953976F9p1))
139//                        ^ TangleRNG.determine((long)(z*0x0.85157AF5p4 + x*0x0.712BE5ABp3 + y*0x0.9E3779B9p2 + v*0x0.8329C6DFp1))
140//                        ^ TangleRNG.determine((long)(w*0x0.953976F9p4 + u*0x0.85157AF5p3 + v*0x0.712BE5ABp2 + x*0x0.9E3779B9p1))
141//                        ^ TangleRNG.determine((long)(u*0x0.8329C6DFp4 + v*0x0.953976F9p3 + w*0x0.85157AF5p2 + y*0x0.712BE5ABp1))
142//                        ^ TangleRNG.determine((long)(v*0x0.9E3779B9p4 + w*0x0.8329C6DFp3 + u*0x0.953976F9p2 + z*0x0.85157AF5p1))));
143    }
144    @Override
145    public double getNoiseWithSeed(final double x, final double y, final double z, final double w, final double u, final double v, final long seed) {
146//        return NumberTools.randomSignedDouble((((long)(x + zigzagRandomized(seed, y)) * 0x6C8E9CF570932BD5L
147//                ^ (long)(y + zigzagRandomized(seed + 0xAEF17502108EF2D9L, z)) * 0x9E3779B97F4A7C15L
148//                ^ (long)(z + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, w)) * 0xC6BC279692B5CC85L
149//                ^ (long)(w + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, u)) * 0x352E9CF570932BDDL
150//                ^ (long)(u + zigzagRandomized(seed + 0xBBC5D408423BCB64L, v)) * 0x5851F42D4C957F2DL
151//                ^ (long)(v + zigzagRandomized(seed + 0x6AB7490A52CABE3DL, x)) * 0x2545F4914F6CDD1DL
152//        ) >>> 32)
153//                ^ seed);
154        return NumberTools.randomSignedDouble((((long)(x
155                + zigzagRandomized(seed, y)
156                + zigzagRandomized(seed, z)
157                + zigzagRandomized(seed, w)
158                + zigzagRandomized(seed, u)
159                + zigzagRandomized(seed, v)) * 0x6C8E9CF570932BD5L
160                ^ (long)(y
161                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, x)
162                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, z)
163                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, w)
164                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, u)
165                + zigzagRandomized(seed + 0xAEF17502108EF2D9L, v)) * 0x9E3779B97F4A7C15L
166                ^ (long)(z
167                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, x)
168                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, y)
169                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, w)
170                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, u)
171                + zigzagRandomized(seed + 0x5DE2EA04211DE5B2L, v)) * 0xC6BC279692B5CC85L
172                ^ (long)(w
173                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, x)
174                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, y)
175                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, z)
176                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, u)
177                + zigzagRandomized(seed + 0x0CD45F0631ACD88BL, v)) * 0x352E9CF570932BDDL
178                ^ (long)(u
179                + zigzagRandomized(seed + 0xBBC5D408423BCB64L, x)
180                + zigzagRandomized(seed + 0xBBC5D408423BCB64L, y)
181                + zigzagRandomized(seed + 0xBBC5D408423BCB64L, z)
182                + zigzagRandomized(seed + 0xBBC5D408423BCB64L, w)
183                + zigzagRandomized(seed + 0xBBC5D408423BCB64L, v)) * 0x5851F42D4C957F2DL
184                ^ (long)(v
185                + zigzagRandomized(seed + 0x6AB7490A52CABE3DL, x)
186                + zigzagRandomized(seed + 0x6AB7490A52CABE3DL, y)
187                + zigzagRandomized(seed + 0x6AB7490A52CABE3DL, z)
188                + zigzagRandomized(seed + 0x6AB7490A52CABE3DL, w)
189                + zigzagRandomized(seed + 0x6AB7490A52CABE3DL, u)) * 0x2545F4914F6CDD1DL
190        ) >>> 32)
191                ^ seed);
192
193//        return NumberTools.bounce(
194//                (TangleRNG.determine((long)(x*0x0.9E3779B9p4 + y*0x0.8329C6DFp3 + z*0x0.953976F9p2 + w*0x0.85157AF5p1) + (seed & 0x24924924))
195//                        ^ TangleRNG.determine((long)(y*0x0.712BE5ABp4 + z*0x0.9E3779B9p3 + x*0x0.8329C6DFp2 + u*0x0.953976F9p1) + (seed & 0x49249249))
196//                        ^ TangleRNG.determine((long)(z*0x0.85157AF5p4 + x*0x0.712BE5ABp3 + y*0x0.9E3779B9p2 + v*0x0.8329C6DFp1) + (seed & 0x92492492))
197//                        ^ TangleRNG.determine((long)(w*0x0.953976F9p4 + u*0x0.85157AF5p3 + v*0x0.712BE5ABp2 + x*0x0.9E3779B9p1) + (seed & 0x24924924))
198//                        ^ TangleRNG.determine((long)(u*0x0.8329C6DFp4 + v*0x0.953976F9p3 + w*0x0.85157AF5p2 + y*0x0.712BE5ABp1) + (seed & 0x49249249))
199//                        ^ TangleRNG.determine((long)(v*0x0.9E3779B9p4 + w*0x0.8329C6DFp3 + u*0x0.953976F9p2 + z*0x0.85157AF5p1) + (seed & 0x92492492))));
200    }
201}