From 9ebf9cff828e766665392577048a6287271af76c Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 3 Mar 2021 12:17:35 +0100 Subject: [PATCH] Parser#parse(String) now work + deleted ';' for each rules in Tab + fixed Parser#isCorrect(String, Type) which will not accept rule without '=' anymore --- src/lsystem/Main.java | 5 +- src/lsystem/engine/Element.java | 16 ++-- src/lsystem/engine/ElementProperties.java | 25 ++++-- src/lsystem/engine/Parser.java | 85 +++++++++++++------ src/lsystem/engine/Rewrite.java | 18 ++-- src/lsystem/screen/SwingGLCanvas.java | 23 +++-- src/lsystem/screen/Tab.java | 4 +- .../screen/listener/JoglEventListener.java | 17 ++-- src/lsystem/screen/listener/Listener.java | 2 + 9 files changed, 120 insertions(+), 75 deletions(-) diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index 77ce7a1..cd222ba 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -3,16 +3,13 @@ package lsystem; import lsystem.screen.MainFrame; import lsystem.screen.SwingGLCanvas; -import java.util.Arrays; - public class Main { public static void main(String[] args) { - System.out.println(-180%360); MainFrame frame = new MainFrame(); frame.setVisible(true); SwingGLCanvas canvas = new SwingGLCanvas(); - + canvas.setVisible(true); } } diff --git a/src/lsystem/engine/Element.java b/src/lsystem/engine/Element.java index 00267ae..8e7c9fe 100644 --- a/src/lsystem/engine/Element.java +++ b/src/lsystem/engine/Element.java @@ -4,19 +4,19 @@ import java.util.LinkedList; public class Element { - public final ElementProperties properties; - public final LinkedList children = new LinkedList<>(); + public final ElementProperties property; public final Element parent; - public final float value; + public final float[] values; + public final LinkedList children = new LinkedList<>(); - public Element(ElementProperties properties, Element parent) { - this(properties, 0, parent); + public Element(ElementProperties property, Element parent) { + this(property, parent, new float[]{0f, 0f, 0f}); } - public Element(ElementProperties properties, float value, Element parent) { - this.properties = properties; + public Element(ElementProperties property, Element parent, float[] values) { + this.property = property; this.parent = parent; - this.value = value; + this.values = values; } } diff --git a/src/lsystem/engine/ElementProperties.java b/src/lsystem/engine/ElementProperties.java index a4864e8..f2ea42b 100644 --- a/src/lsystem/engine/ElementProperties.java +++ b/src/lsystem/engine/ElementProperties.java @@ -4,17 +4,28 @@ public enum ElementProperties { DRAW('X'), NOTHING('Y'), - ROTATION_X('x'), - ROTATION_Y('y'), - ROTATION_Z('z'); + ROTATION_X('x', (byte) 0), + ROTATION_Y('y', (byte) 1), + ROTATION_Z('z', (byte) 2); - private final char c; - ElementProperties(char c) { - this.c = c; + private final char ch4r; + 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() { - return c; + return ch4r; + } + + public byte getDirection() { + return direction; } } diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index 4edecf0..f4971e2 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -12,7 +12,7 @@ public class Parser { private final String axiom; private final List rules; 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 rules,int nbIterations) { this.axiom = axiom; @@ -39,16 +39,15 @@ public class Parser { private boolean isCorrect(String stringToCheck, Type type) { + if(type == Type.RULE && !stringToCheck.contains("=")) + return false; char old = ' '; int bracket = 0; boolean equalsSymbolFound = false; for (int i = 0; i < stringToCheck.length(); i++) { char temp = stringToCheck.charAt(i); - if (temp == '[') { + if (temp == '[') bracket++; - if(stringToCheck.charAt(i - 1) == '[') - return false; - } if(temp == ']') { bracket--; if(stringToCheck.charAt(i - 1) == '[') @@ -92,41 +91,73 @@ public class Parser { return rules; } - - // TODO: 02/03/2021 to finish - public Element parse(String rewritten) { - String toParse = rewritten; + // TODO: 03/03/2021 to finish + public static Element parse(String word) { + char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-'}; Element root = null; Element workingElement = null; - float number = 0; + String number = ""; boolean bracket = false; - int i = 0; - while (!toParse.isEmpty()) { - char c = toParse.charAt(i); - toParse = toParse.substring(i); - ElementProperties pro = Arrays.stream(ElementProperties.values()).filter(p -> p.getChar() == c).findFirst().orElse(null); - if(pro != null) { - if(workingElement == null) { - workingElement = new Element(pro, null); - root = workingElement; - } else { - Element element = new Element(pro, number, workingElement); - workingElement.children.add(element); - if(bracket) { - workingElement = element; - bracket = false; + float[] appliedRotation = new float[3]; + Element lastCreatedElement = null; + + for(int i = 0; i < word.length(); ++i) { + char c = word.charAt(i); + ElementProperties property = Arrays.stream(ElementProperties.values()).filter(p -> p.getChar() == c).findFirst().orElse(null); + if(property != null) { + if(property.getDirection() == -1) { + if(!number.isEmpty()) { + float n = getFloat(number); + number = ""; + appliedRotation[0] = n; } + 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 { - if(c == '[') + for(char n : numbers) { + if(c == n) { + number += c; + break; + } + } + if(c == '[') { + workingElement = lastCreatedElement; bracket = true; + } if(c == ']') { + assert workingElement != null; workingElement = workingElement.parent; } } - i++; } 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; + } + } diff --git a/src/lsystem/engine/Rewrite.java b/src/lsystem/engine/Rewrite.java index 2dbbb22..81c8b43 100644 --- a/src/lsystem/engine/Rewrite.java +++ b/src/lsystem/engine/Rewrite.java @@ -17,8 +17,8 @@ public class Rewrite { } - private String replaceRulesByID(final String rewritted) { - String toRewrite = rewritted; + private String replaceRulesByID(final String rewritten) { + String toRewrite = rewritten; for(int j = 0; j < rules.size(); ++j){ Pair pair = rules.get(j); toRewrite = toRewrite.replace(pair.getLeft(), "${" + j + "}"); @@ -27,20 +27,20 @@ public class Rewrite { } private String replaceIDByRuleApplication(final String toRewrite) { - String rewritted = toRewrite; + String rewritten = toRewrite; 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() { - String rewritted = axiom; + String rewritten = axiom; for(int i = 0; i < recurrences; ++i) { - String toRewrite = replaceRulesByID(rewritted); - rewritted = replaceIDByRuleApplication(toRewrite); + String toRewrite = replaceRulesByID(rewritten); + rewritten = replaceIDByRuleApplication(toRewrite); } - return rewritted; + return rewritten.replace("[", "Y["); } } diff --git a/src/lsystem/screen/SwingGLCanvas.java b/src/lsystem/screen/SwingGLCanvas.java index 4dd7be2..0a073cc 100644 --- a/src/lsystem/screen/SwingGLCanvas.java +++ b/src/lsystem/screen/SwingGLCanvas.java @@ -16,6 +16,8 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class SwingGLCanvas { + public JFrame frame; + FPSAnimator animator; public final GLCanvas glCanvas; 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) @@ -32,19 +34,24 @@ public class SwingGLCanvas { glCanvas.addMouseMotionListener(mouse); glCanvas.addMouseWheelListener(mouse); glCanvas.addKeyListener(new KeyboardListener(this)); - final JFrame jframe = new JFrame("L-System"); - final FPSAnimator animator = new FPSAnimator(glCanvas, 60); - jframe.addWindowListener(new WindowAdapter() { + frame = new JFrame("L-System"); + animator = new FPSAnimator(glCanvas, 60); + frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { - jframe.dispose(); + frame.dispose(); } }); - jframe.getContentPane().add(glCanvas, BorderLayout.CENTER); - jframe.setSize(Constants.WIDTH, Constants.HEIGHT); + frame.getContentPane().add(glCanvas, BorderLayout.CENTER); + frame.setSize(Constants.WIDTH, Constants.HEIGHT); + } - animator.start(); - jframe.setVisible(true); + public void setVisible(boolean bl) { + if(bl) + animator.start(); + else + animator.stop(); + frame.setVisible(bl); } } diff --git a/src/lsystem/screen/Tab.java b/src/lsystem/screen/Tab.java index b7d4870..d320d07 100644 --- a/src/lsystem/screen/Tab.java +++ b/src/lsystem/screen/Tab.java @@ -116,13 +116,13 @@ public class Tab extends JPanel{ } public String getAxiom(){ String str = axiomList.getText(); - str = str.substring(10,str.length()); + str = str.substring(10).replaceAll(";", ""); return str; } public java.util.List getRules(){ List list = new ArrayList<>(); String str = rulesList.getText(); - str = str.substring(10,str.length()); + str = str.substring(10).replaceAll(";", ""); String[] strsplit = str.split("\n"); for(int y = 0;y { - while (true) { - synchronized (this){ - try { - wait(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.out.println(fps); - fps = 0; + while (canvas.frame.isVisible()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); } + System.out.println(fps); + fps = 0; } }).start(); } @Override public void dispose(GLAutoDrawable glAutoDrawable) { - } @Override diff --git a/src/lsystem/screen/listener/Listener.java b/src/lsystem/screen/listener/Listener.java index 33a62a7..96127fd 100644 --- a/src/lsystem/screen/listener/Listener.java +++ b/src/lsystem/screen/listener/Listener.java @@ -1,5 +1,6 @@ package lsystem.screen.listener; +import lsystem.engine.Element; import lsystem.engine.Parser; import lsystem.engine.Rewrite; import lsystem.screen.MainFrame; @@ -56,6 +57,7 @@ public class Listener implements ActionListener, KeyListener { Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations()); final String word = rewriter.rewrite(); System.out.println(word); + final Element parsed = Parser.parse(word); } break;