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:
parent
764a0fe710
commit
9ebf9cff82
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,19 +4,19 @@ import java.util.LinkedList;
|
||||
|
||||
public class Element {
|
||||
|
||||
public final ElementProperties properties;
|
||||
public final LinkedList<Element> children = new LinkedList<>();
|
||||
public final ElementProperties property;
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class Parser {
|
||||
private final String axiom;
|
||||
private final List<String> 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<String> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<String, String> 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[");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<String> getRules(){
|
||||
List<String> 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<strsplit.length;y++){
|
||||
list.add(strsplit[y]);
|
||||
|
@ -63,23 +63,20 @@ public class JoglEventListener implements GLEventListener {
|
||||
}
|
||||
System.out.println(prismPosition.size() * 8);
|
||||
new Thread(() -> {
|
||||
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
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user