Improved performance + implemented better responsiveness against exceptions when parsing

This commit is contained in:
Quentin Legot 2021-03-16 16:21:29 +01:00
parent 562f366a2b
commit 6242618413
5 changed files with 13 additions and 22 deletions

View File

@ -1,13 +1,13 @@
package lsystem.engine; package lsystem.engine;
import java.util.LinkedList; import java.util.ArrayList;
public class Element { public class Element {
public final ElementProperties property; public final ElementProperties property;
public final Element parent; public final Element parent;
public final float[] rotation; public final float[] rotation;
public final LinkedList<Element> children = new LinkedList<>(); public final ArrayList<Element> children = new ArrayList<>();
public Element(ElementProperties property, Element parent) { public Element(ElementProperties property, Element parent) {
this(property, parent, new float[]{0f, 0f, 0f}); this(property, parent, new float[]{0f, 0f, 0f});

View File

@ -92,7 +92,7 @@ public class Parser {
} }
// TODO: 03/03/2021 to finish // 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', '.', '+', '-'}; char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-'};
Element root = null; Element root = null;
Element workingElement = null; Element workingElement = null;

View File

@ -65,7 +65,7 @@ public abstract class AbstractCanvas {
return lSystem; 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; parsedState = State.LOAD;
this.lSystem = Parser.parse(Rewrite.rewrite(axiom, rules, iterations)); this.lSystem = Parser.parse(Rewrite.rewrite(axiom, rules, iterations));
parsedState = State.FINISH_OR_NULL; parsedState = State.FINISH_OR_NULL;

View File

@ -5,10 +5,6 @@ import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.gl2.GLUT;
import lsystem.engine.Element; import lsystem.engine.Element;
import lsystem.utils.Pair;
import java.util.LinkedList;
import java.util.Random;
public class GLEventListener implements com.jogamp.opengl.GLEventListener { 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[] light_0_position = {1000f, 1000f, 1000f, 1f};
private final float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f}; 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 GLU glu;
private final GLUT glut; private final GLUT glut;
@ -52,14 +47,6 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener {
gl.glDepthFunc(GL2.GL_LEQUAL); gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); 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(() -> { new Thread(() -> {
while (canvas.frame.isVisible()) { while (canvas.frame.isVisible()) {
try { 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); 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.glPushMatrix();
gl.glRotatef(90f, -1f, 0f, 0f); gl.glRotatef(90f, -1f, 0f, 0f);
displayLSystem(gl, glut, canvas.getLSystem()); displayLSystem(gl, glut, canvas.getLSystem());
gl.glPopMatrix(); 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[1] * 360, 0f, 1f, 0f);
gl.glRotatef((element.rotation[0] + element.rotation[1]) * 360, 0f, 0f, 1f); 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.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, 10, 10);
glut.glutSolidCylinder(0.25f, 1f, 20, 20);
gl.glEnd();
gl.glTranslatef(0f, 0f, 1f); gl.glTranslatef(0f, 0f, 1f);
for(Element child : element.children) { for(Element child : element.children) {
displayLSystem(gl, glut, child); displayLSystem(gl, glut, child);

View File

@ -59,8 +59,13 @@ public class Listener implements ActionListener, KeyListener {
openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
} else { } else {
parserThread = new Thread(() -> { parserThread = new Thread(() -> {
Main.joglFrame.setLSystem(axiom, parser.parseRules(), tab.getNbIterations()); try {
Main.joglFrame.setVisible(true); 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(); parserThread.start();
} }