mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 06:11:24 +00:00
allow paralithic expressions to be defined in expression samplers
This commit is contained in:
+28
-17
@@ -1,5 +1,7 @@
|
|||||||
package com.dfsek.terra.config.loaders.config.sampler.templates.noise;
|
package com.dfsek.terra.config.loaders.config.sampler.templates.noise;
|
||||||
|
|
||||||
|
import com.dfsek.paralithic.eval.parser.Parser;
|
||||||
|
import com.dfsek.paralithic.eval.parser.Scope;
|
||||||
import com.dfsek.paralithic.eval.tokenizer.ParseException;
|
import com.dfsek.paralithic.eval.tokenizer.ParseException;
|
||||||
import com.dfsek.paralithic.functions.Function;
|
import com.dfsek.paralithic.functions.Function;
|
||||||
import com.dfsek.tectonic.annotations.Default;
|
import com.dfsek.tectonic.annotations.Default;
|
||||||
@@ -9,12 +11,13 @@ import com.dfsek.tectonic.exception.ValidationException;
|
|||||||
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
||||||
import com.dfsek.terra.api.math.noise.samplers.noise.ExpressionFunction;
|
import com.dfsek.terra.api.math.noise.samplers.noise.ExpressionFunction;
|
||||||
import com.dfsek.terra.api.math.paralithic.BlankFunction;
|
import com.dfsek.terra.api.math.paralithic.BlankFunction;
|
||||||
import com.dfsek.terra.api.math.paralithic.noise.NoiseFunction2;
|
import com.dfsek.terra.api.math.paralithic.defined.UserDefinedFunction;
|
||||||
import com.dfsek.terra.api.math.paralithic.noise.NoiseFunction3;
|
|
||||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||||
|
import com.dfsek.terra.config.loaders.config.function.FunctionTemplate;
|
||||||
import com.dfsek.terra.config.loaders.config.sampler.templates.SamplerTemplate;
|
import com.dfsek.terra.config.loaders.config.sampler.templates.SamplerTemplate;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
@@ -31,17 +34,14 @@ public class ExpressionFunctionTemplate extends SamplerTemplate<ExpressionFuncti
|
|||||||
@Default
|
@Default
|
||||||
private Map<String, NoiseSeeded> functions = new HashMap<>();
|
private Map<String, NoiseSeeded> functions = new HashMap<>();
|
||||||
|
|
||||||
|
@Value("expressions")
|
||||||
|
@Default
|
||||||
|
private LinkedHashMap<String, FunctionTemplate> expressions = new LinkedHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NoiseSampler apply(Long seed) {
|
public NoiseSampler apply(Long seed) {
|
||||||
try {
|
try {
|
||||||
Map<String, Function> noiseFunctionMap = new HashMap<>();
|
Map<String, Function> noiseFunctionMap = generateFunctions();
|
||||||
|
|
||||||
functions.forEach((id, function) -> {
|
|
||||||
if(function.getDimensions() == 2) {
|
|
||||||
noiseFunctionMap.put(id, new NoiseFunction2(function.apply(seed)));
|
|
||||||
} else noiseFunctionMap.put(id, new NoiseFunction3(function.apply(seed)));
|
|
||||||
});
|
|
||||||
|
|
||||||
return new ExpressionFunction(noiseFunctionMap, equation, vars);
|
return new ExpressionFunction(noiseFunctionMap, equation, vars);
|
||||||
} catch(ParseException e) {
|
} catch(ParseException e) {
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
@@ -51,17 +51,28 @@ public class ExpressionFunctionTemplate extends SamplerTemplate<ExpressionFuncti
|
|||||||
@Override
|
@Override
|
||||||
public boolean validate() throws ValidationException {
|
public boolean validate() throws ValidationException {
|
||||||
try {
|
try {
|
||||||
Map<String, Function> noiseFunctionMap = new HashMap<>();
|
Map<String, Function> noiseFunctionMap = generateFunctions();
|
||||||
|
|
||||||
functions.forEach((id, function) -> {
|
|
||||||
if(function.getDimensions() == 2) {
|
|
||||||
noiseFunctionMap.put(id, new BlankFunction(2));
|
|
||||||
} else noiseFunctionMap.put(id, new BlankFunction(3));
|
|
||||||
});
|
|
||||||
new ExpressionFunction(noiseFunctionMap, equation, vars);
|
new ExpressionFunction(noiseFunctionMap, equation, vars);
|
||||||
} catch(ParseException e) {
|
} catch(ParseException e) {
|
||||||
throw new ValidationException("Errors occurred while parsing noise equation: ", e);
|
throw new ValidationException("Errors occurred while parsing noise equation: ", e);
|
||||||
}
|
}
|
||||||
return super.validate();
|
return super.validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, Function> generateFunctions() throws ParseException {
|
||||||
|
Map<String, Function> noiseFunctionMap = new HashMap<>();
|
||||||
|
|
||||||
|
for(Map.Entry<String, FunctionTemplate> entry : expressions.entrySet()) {
|
||||||
|
System.out.println(entry);
|
||||||
|
noiseFunctionMap.put(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), new Parser(), new Scope()));
|
||||||
|
}
|
||||||
|
|
||||||
|
functions.forEach((id, function) -> {
|
||||||
|
if(function.getDimensions() == 2) {
|
||||||
|
noiseFunctionMap.put(id, new BlankFunction(2));
|
||||||
|
} else noiseFunctionMap.put(id, new BlankFunction(3));
|
||||||
|
});
|
||||||
|
|
||||||
|
return noiseFunctionMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user