Improved performance + implemented better responsiveness against exceptions when parsing
This commit is contained in:
parent
562f366a2b
commit
6242618413
@ -1,13 +1,13 @@
|
||||
package lsystem.engine;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Element {
|
||||
|
||||
public final ElementProperties property;
|
||||
public final Element parent;
|
||||
public final float[] rotation;
|
||||
public final LinkedList<Element> children = new LinkedList<>();
|
||||
public final ArrayList<Element> children = new ArrayList<>();
|
||||
|
||||
public Element(ElementProperties property, Element parent) {
|
||||
this(property, parent, new float[]{0f, 0f, 0f});
|
||||
|
@ -92,7 +92,7 @@ public class Parser {
|
||||
}
|
||||
|
||||
// TODO: 03/03/2021 to finish
|
||||
public static Element parse(String word) {
|
||||
public static Element parse(String word) throws NumberFormatException {
|
||||
char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-'};
|
||||
Element root = null;
|
||||
Element workingElement = null;
|
||||
|
@ -65,7 +65,7 @@ public abstract class AbstractCanvas {
|
||||
return lSystem;
|
||||
}
|
||||
|
||||
public void setLSystem(String axiom, List<Pair<String, String>> rules, int iterations) {
|
||||
public void setLSystem(String axiom, List<Pair<String, String>> rules, int iterations) throws NumberFormatException {
|
||||
parsedState = State.LOAD;
|
||||
this.lSystem = Parser.parse(Rewrite.rewrite(axiom, rules, iterations));
|
||||
parsedState = State.FINISH_OR_NULL;
|
||||
|
@ -5,10 +5,6 @@ import com.jogamp.opengl.GLAutoDrawable;
|
||||
import com.jogamp.opengl.glu.GLU;
|
||||
import com.jogamp.opengl.util.gl2.GLUT;
|
||||
import lsystem.engine.Element;
|
||||
import lsystem.utils.Pair;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
public class GLEventListener implements com.jogamp.opengl.GLEventListener {
|
||||
|
||||
@ -19,7 +15,6 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener {
|
||||
private final float[] light_0_position = {1000f, 1000f, 1000f, 1f};
|
||||
|
||||
private final float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f};
|
||||
private final LinkedList<Pair<Integer, Integer>> prismPosition = new LinkedList<>();
|
||||
|
||||
private final GLU glu;
|
||||
private final GLUT glut;
|
||||
@ -52,14 +47,6 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener {
|
||||
|
||||
gl.glDepthFunc(GL2.GL_LEQUAL);
|
||||
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
|
||||
for(int i = -50; i < 51; ++i) {
|
||||
for(int j = -50; j < 51; ++j) {
|
||||
if(new Random().nextFloat() < 0.05) {
|
||||
prismPosition.add(new Pair<>(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(prismPosition.size() * 8);
|
||||
new Thread(() -> {
|
||||
while (canvas.frame.isVisible()) {
|
||||
try {
|
||||
@ -90,6 +77,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener {
|
||||
glu.gluLookAt(canvas.camera[0], canvas.camera[1], canvas.camera[2], canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1, 0f, 1f, 0f);
|
||||
gl.glPushMatrix();
|
||||
gl.glRotatef(90f, -1f, 0f, 0f);
|
||||
|
||||
displayLSystem(gl, glut, canvas.getLSystem());
|
||||
gl.glPopMatrix();
|
||||
|
||||
@ -105,9 +93,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener {
|
||||
gl.glRotatef(element.rotation[1] * 360, 0f, 1f, 0f);
|
||||
gl.glRotatef((element.rotation[0] + element.rotation[1]) * 360, 0f, 0f, 1f);
|
||||
gl.glTranslated(-Math.sin(element.rotation[0]), -Math.sin(element.rotation[1]), -Math.sin(element.rotation[0] + element.rotation[1]));
|
||||
gl.glBegin(GL2.GL_LINES);
|
||||
glut.glutSolidCylinder(0.25f, 1f, 20, 20);
|
||||
gl.glEnd();
|
||||
glut.glutSolidCylinder(0.25f, 1f, 10, 10);
|
||||
gl.glTranslatef(0f, 0f, 1f);
|
||||
for(Element child : element.children) {
|
||||
displayLSystem(gl, glut, child);
|
||||
|
@ -59,8 +59,13 @@ public class Listener implements ActionListener, KeyListener {
|
||||
openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
|
||||
} else {
|
||||
parserThread = new Thread(() -> {
|
||||
Main.joglFrame.setLSystem(axiom, parser.parseRules(), tab.getNbIterations());
|
||||
Main.joglFrame.setVisible(true);
|
||||
try {
|
||||
Main.joglFrame.setLSystem(axiom, parser.parseRules(), tab.getNbIterations());
|
||||
Main.joglFrame.setVisible(true);
|
||||
} catch (NumberFormatException err) {
|
||||
Main.joglFrame.parsedState = AbstractCanvas.State.FINISH_OR_NULL;
|
||||
openDialog("Une erreur de type " + err.getClass().getSimpleName() + " est survenue lors de l'execution du parser: " + err.getMessage());
|
||||
}
|
||||
});
|
||||
parserThread.start();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user