Parser#parse(String) now work + deleted ';' for each rules in Tab + fixed Parser#isCorrect(String, Type) which will not accept rule without '=' anymore

This commit is contained in:
Quentin Legot 2021-03-03 12:17:35 +01:00
parent 764a0fe710
commit 9ebf9cff82
9 changed files with 120 additions and 75 deletions

View File

@ -3,16 +3,13 @@ package lsystem;
import lsystem.screen.MainFrame; import lsystem.screen.MainFrame;
import lsystem.screen.SwingGLCanvas; import lsystem.screen.SwingGLCanvas;
import java.util.Arrays;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(-180%360);
MainFrame frame = new MainFrame(); MainFrame frame = new MainFrame();
frame.setVisible(true); frame.setVisible(true);
SwingGLCanvas canvas = new SwingGLCanvas(); SwingGLCanvas canvas = new SwingGLCanvas();
canvas.setVisible(true);
} }
} }

View File

@ -4,19 +4,19 @@ import java.util.LinkedList;
public class Element { public class Element {
public final ElementProperties properties; public final ElementProperties property;
public final LinkedList<Element> children = new LinkedList<>();
public final Element parent; public final Element parent;
public final float value; public final float[] values;
public final LinkedList<Element> children = new LinkedList<>();
public Element(ElementProperties properties, Element parent) { public Element(ElementProperties property, Element parent) {
this(properties, 0, parent); this(property, parent, new float[]{0f, 0f, 0f});
} }
public Element(ElementProperties properties, float value, Element parent) { public Element(ElementProperties property, Element parent, float[] values) {
this.properties = properties; this.property = property;
this.parent = parent; this.parent = parent;
this.value = value; this.values = values;
} }
} }

View File

@ -4,17 +4,28 @@ public enum ElementProperties {
DRAW('X'), DRAW('X'),
NOTHING('Y'), NOTHING('Y'),
ROTATION_X('x'), ROTATION_X('x', (byte) 0),
ROTATION_Y('y'), ROTATION_Y('y', (byte) 1),
ROTATION_Z('z'); ROTATION_Z('z', (byte) 2);
private final char c;
ElementProperties(char c) { private final char ch4r;
this.c = c; private final byte direction;
ElementProperties(char x) {
this(x, (byte) -1);
}
ElementProperties(char x, byte direction) {
this.ch4r = x;
this.direction = direction;
} }
public char getChar() { public char getChar() {
return c; return ch4r;
}
public byte getDirection() {
return direction;
} }
} }

View File

@ -12,7 +12,7 @@ public class Parser {
private final String axiom; private final String axiom;
private final List<String> rules; private final List<String> rules;
private final int nbIterations; private final int nbIterations;
private final char[] validChars = {'=',']','[','.','+','-','X','Y','Z','x','y','z','0','1','2','3','4','5','6','7','8','9',' '}; private final char[] validChars = {'=',']','[','.','+','-','X','Y','x','y','z','0','1','2','3','4','5','6','7','8','9',' '};
public Parser(String axiom, List<String> rules,int nbIterations) { public Parser(String axiom, List<String> rules,int nbIterations) {
this.axiom = axiom; this.axiom = axiom;
@ -39,16 +39,15 @@ public class Parser {
private boolean isCorrect(String stringToCheck, Type type) { private boolean isCorrect(String stringToCheck, Type type) {
if(type == Type.RULE && !stringToCheck.contains("="))
return false;
char old = ' '; char old = ' ';
int bracket = 0; int bracket = 0;
boolean equalsSymbolFound = false; boolean equalsSymbolFound = false;
for (int i = 0; i < stringToCheck.length(); i++) { for (int i = 0; i < stringToCheck.length(); i++) {
char temp = stringToCheck.charAt(i); char temp = stringToCheck.charAt(i);
if (temp == '[') { if (temp == '[')
bracket++; bracket++;
if(stringToCheck.charAt(i - 1) == '[')
return false;
}
if(temp == ']') { if(temp == ']') {
bracket--; bracket--;
if(stringToCheck.charAt(i - 1) == '[') if(stringToCheck.charAt(i - 1) == '[')
@ -92,41 +91,73 @@ public class Parser {
return rules; return rules;
} }
// TODO: 03/03/2021 to finish
// TODO: 02/03/2021 to finish public static Element parse(String word) {
public Element parse(String rewritten) { char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-'};
String toParse = rewritten;
Element root = null; Element root = null;
Element workingElement = null; Element workingElement = null;
float number = 0; String number = "";
boolean bracket = false; boolean bracket = false;
int i = 0; float[] appliedRotation = new float[3];
while (!toParse.isEmpty()) { Element lastCreatedElement = null;
char c = toParse.charAt(i);
toParse = toParse.substring(i); for(int i = 0; i < word.length(); ++i) {
ElementProperties pro = Arrays.stream(ElementProperties.values()).filter(p -> p.getChar() == c).findFirst().orElse(null); char c = word.charAt(i);
if(pro != null) { ElementProperties property = Arrays.stream(ElementProperties.values()).filter(p -> p.getChar() == c).findFirst().orElse(null);
if(workingElement == null) { if(property != null) {
workingElement = new Element(pro, null); if(property.getDirection() == -1) {
root = workingElement; if(!number.isEmpty()) {
} else { float n = getFloat(number);
Element element = new Element(pro, number, workingElement); number = "";
workingElement.children.add(element); appliedRotation[0] = n;
if(bracket) {
workingElement = element;
bracket = false;
} }
if(workingElement == null) {
workingElement = new Element(property, null);
lastCreatedElement = workingElement;
root = workingElement;
} else {
Element element = new Element(property, workingElement, appliedRotation);
lastCreatedElement = element;
appliedRotation = new float[]{0f, 0f, 0f};
workingElement.children.add(element);
}
} else {
float n = getFloat(number);
number = "";
appliedRotation[property.getDirection()] = n;
} }
} else { } else {
if(c == '[') for(char n : numbers) {
if(c == n) {
number += c;
break;
}
}
if(c == '[') {
workingElement = lastCreatedElement;
bracket = true; bracket = true;
}
if(c == ']') { if(c == ']') {
assert workingElement != null;
workingElement = workingElement.parent; workingElement = workingElement.parent;
} }
} }
i++;
} }
return root; return root;
} }
private static float getFloat(String number) throws NumberFormatException {
float n;
if(number.equals("") || number.equals("+"))
n = 0.25f;
else if(number.equals("-")) {
n = -0.25f;
}else{
System.out.println(number);
n = Float.parseFloat(number);
}
return n;
}
} }

View File

@ -17,8 +17,8 @@ public class Rewrite {
} }
private String replaceRulesByID(final String rewritted) { private String replaceRulesByID(final String rewritten) {
String toRewrite = rewritted; String toRewrite = rewritten;
for(int j = 0; j < rules.size(); ++j){ for(int j = 0; j < rules.size(); ++j){
Pair<String, String> pair = rules.get(j); Pair<String, String> pair = rules.get(j);
toRewrite = toRewrite.replace(pair.getLeft(), "${" + j + "}"); toRewrite = toRewrite.replace(pair.getLeft(), "${" + j + "}");
@ -27,20 +27,20 @@ public class Rewrite {
} }
private String replaceIDByRuleApplication(final String toRewrite) { private String replaceIDByRuleApplication(final String toRewrite) {
String rewritted = toRewrite; String rewritten = toRewrite;
for(int j = 0; j < rules.size(); ++j){ for(int j = 0; j < rules.size(); ++j){
rewritted = rewritted.replace("${" + j + "}", rules.get(j).getRight()); rewritten = rewritten.replace("${" + j + "}", rules.get(j).getRight());
} }
return rewritted; return rewritten;
} }
public String rewrite() { public String rewrite() {
String rewritted = axiom; String rewritten = axiom;
for(int i = 0; i < recurrences; ++i) { for(int i = 0; i < recurrences; ++i) {
String toRewrite = replaceRulesByID(rewritted); String toRewrite = replaceRulesByID(rewritten);
rewritted = replaceIDByRuleApplication(toRewrite); rewritten = replaceIDByRuleApplication(toRewrite);
} }
return rewritted; return rewritten.replace("[", "Y[");
} }
} }

View File

@ -16,6 +16,8 @@ import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
public class SwingGLCanvas { public class SwingGLCanvas {
public JFrame frame;
FPSAnimator animator;
public final GLCanvas glCanvas; public final GLCanvas glCanvas;
public float[] camera = {0f, 1f, 5f, // camera pos x,y, z public float[] camera = {0f, 1f, 5f, // camera pos x,y, z
0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis) 0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis)
@ -32,19 +34,24 @@ public class SwingGLCanvas {
glCanvas.addMouseMotionListener(mouse); glCanvas.addMouseMotionListener(mouse);
glCanvas.addMouseWheelListener(mouse); glCanvas.addMouseWheelListener(mouse);
glCanvas.addKeyListener(new KeyboardListener(this)); glCanvas.addKeyListener(new KeyboardListener(this));
final JFrame jframe = new JFrame("L-System"); frame = new JFrame("L-System");
final FPSAnimator animator = new FPSAnimator(glCanvas, 60); animator = new FPSAnimator(glCanvas, 60);
jframe.addWindowListener(new WindowAdapter() { frame.addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
jframe.dispose(); frame.dispose();
} }
}); });
jframe.getContentPane().add(glCanvas, BorderLayout.CENTER); frame.getContentPane().add(glCanvas, BorderLayout.CENTER);
jframe.setSize(Constants.WIDTH, Constants.HEIGHT); frame.setSize(Constants.WIDTH, Constants.HEIGHT);
}
animator.start(); public void setVisible(boolean bl) {
jframe.setVisible(true); if(bl)
animator.start();
else
animator.stop();
frame.setVisible(bl);
} }
} }

View File

@ -116,13 +116,13 @@ public class Tab extends JPanel{
} }
public String getAxiom(){ public String getAxiom(){
String str = axiomList.getText(); String str = axiomList.getText();
str = str.substring(10,str.length()); str = str.substring(10).replaceAll(";", "");
return str; return str;
} }
public java.util.List<String> getRules(){ public java.util.List<String> getRules(){
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
String str = rulesList.getText(); String str = rulesList.getText();
str = str.substring(10,str.length()); str = str.substring(10).replaceAll(";", "");
String[] strsplit = str.split("\n"); String[] strsplit = str.split("\n");
for(int y = 0;y<strsplit.length;y++){ for(int y = 0;y<strsplit.length;y++){
list.add(strsplit[y]); list.add(strsplit[y]);

View File

@ -63,23 +63,20 @@ public class JoglEventListener implements GLEventListener {
} }
System.out.println(prismPosition.size() * 8); System.out.println(prismPosition.size() * 8);
new Thread(() -> { new Thread(() -> {
while (true) { while (canvas.frame.isVisible()) {
synchronized (this){ try {
try { Thread.sleep(1000);
wait(1000); } catch (InterruptedException e) {
} catch (InterruptedException e) { e.printStackTrace();
e.printStackTrace();
}
System.out.println(fps);
fps = 0;
} }
System.out.println(fps);
fps = 0;
} }
}).start(); }).start();
} }
@Override @Override
public void dispose(GLAutoDrawable glAutoDrawable) { public void dispose(GLAutoDrawable glAutoDrawable) {
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package lsystem.screen.listener; package lsystem.screen.listener;
import lsystem.engine.Element;
import lsystem.engine.Parser; import lsystem.engine.Parser;
import lsystem.engine.Rewrite; import lsystem.engine.Rewrite;
import lsystem.screen.MainFrame; import lsystem.screen.MainFrame;
@ -56,6 +57,7 @@ public class Listener implements ActionListener, KeyListener {
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations()); Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations());
final String word = rewriter.rewrite(); final String word = rewriter.rewrite();
System.out.println(word); System.out.println(word);
final Element parsed = Parser.parse(word);
} }
break; break;