add comment to methods in lsystem.engine
This commit is contained in:
parent
0aa4f88abd
commit
6e83f8c0d9
@ -33,12 +33,12 @@
|
|||||||
</java>
|
</java>
|
||||||
</target>
|
</target>
|
||||||
<target name="javadoc">
|
<target name="javadoc">
|
||||||
<javadoc sourcepath="src" destdir="doc">
|
<javadoc2 sourcepath="src" destdir="doc" access="private">
|
||||||
<fileset dir="src" defaultexcludes="yes">
|
<fileset dir="src" defaultexcludes="yes">
|
||||||
<include name="**"/>
|
<include name="**"/>
|
||||||
</fileset>
|
</fileset>
|
||||||
<classpath refid="project.classpath"/>
|
<classpath refid="project.classpath"/>
|
||||||
</javadoc>
|
</javadoc2>
|
||||||
</target>
|
</target>
|
||||||
<target name="packaging" depends="compile">
|
<target name="packaging" depends="compile">
|
||||||
<mkdir dir="${project.bin.dir}/META-INF" />
|
<mkdir dir="${project.bin.dir}/META-INF" />
|
||||||
|
@ -2,11 +2,27 @@ package lsystem.engine;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object created by {@link Parser#parse(String)}, represent a tree with {@link Element#children branches}
|
||||||
|
* @see ElementProperties
|
||||||
|
* @see Parser#parse(String)
|
||||||
|
*/
|
||||||
public class Element {
|
public class Element {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stored value can be {@link ElementProperties#NOTHING} or {@link ElementProperties#DRAW}<br>
|
||||||
|
* {@linkplain ElementProperties#NOTHING NOTHING} is used to control the evolution<br>
|
||||||
|
* and {@linkplain ElementProperties#DRAW DRAW} is used to draw a 1 unit high cylinder
|
||||||
|
*/
|
||||||
public final ElementProperties property;
|
public final ElementProperties property;
|
||||||
public final Element parent;
|
public final Element parent;
|
||||||
|
/**
|
||||||
|
* Rotation applied to this element, 3-dim tab which represent yaw, pitch and roll rotation
|
||||||
|
*/
|
||||||
public final float[] rotation;
|
public final float[] rotation;
|
||||||
|
/**
|
||||||
|
* Branches of the tree
|
||||||
|
*/
|
||||||
public final ArrayList<Element> children = new ArrayList<>();
|
public final ArrayList<Element> children = new ArrayList<>();
|
||||||
|
|
||||||
public Element(ElementProperties property, Element parent) {
|
public Element(ElementProperties property, Element parent) {
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
package lsystem.engine;
|
package lsystem.engine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Element
|
||||||
|
* @see Parser#parse(String)
|
||||||
|
*/
|
||||||
public enum ElementProperties {
|
public enum ElementProperties {
|
||||||
|
/**
|
||||||
|
* used to draw a 1 unit high cylinder
|
||||||
|
*/
|
||||||
DRAW('X'),
|
DRAW('X'),
|
||||||
|
/**
|
||||||
|
* used to control the evolution
|
||||||
|
*/
|
||||||
NOTHING('Y'),
|
NOTHING('Y'),
|
||||||
ROTATION_X('x', (byte) 0),
|
ROTATION_X('x', (byte) 0),
|
||||||
ROTATION_Y('y', (byte) 1);
|
ROTATION_Y('y', (byte) 1);
|
||||||
|
|
||||||
|
// char is a java reserved word, we replace "a" by a "4" to avoid this problem
|
||||||
private final char ch4r;
|
private final char ch4r;
|
||||||
private final byte direction;
|
private final byte direction;
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if given axiom and rules are correct and parse {@link Rewrite rewritten} word to an {@link Element} object
|
||||||
|
*/
|
||||||
public class Parser {
|
public class Parser {
|
||||||
|
|
||||||
private final String axiom;
|
private final String axiom;
|
||||||
@ -37,7 +40,12 @@ public class Parser {
|
|||||||
return bl;
|
return bl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param stringToCheck check if the string respect the syntax
|
||||||
|
* @param type to help the program know if the string is an axiom or a rule to check if is correct
|
||||||
|
* @return {@code true} if {@code stringToCheck} respect the syntax, {@code false} otherwise
|
||||||
|
*/
|
||||||
private boolean isCorrect(String stringToCheck, Type type) {
|
private boolean isCorrect(String stringToCheck, Type type) {
|
||||||
if(type == Type.RULE && !stringToCheck.contains("="))
|
if(type == Type.RULE && !stringToCheck.contains("="))
|
||||||
return false;
|
return false;
|
||||||
@ -91,6 +99,12 @@ public class Parser {
|
|||||||
return rules;
|
return rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert a rewritten string to an organized L-System stored like a tree as an {@link Element} object
|
||||||
|
* @param word rewritten word
|
||||||
|
* @return the organized L-System object
|
||||||
|
* @throws NumberFormatException if an number has been written incorrectly in {@code word} param
|
||||||
|
*/
|
||||||
public static Element parse(String word) throws NumberFormatException {
|
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;
|
||||||
@ -146,6 +160,13 @@ public class Parser {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add some pre condition before execute {@link Float#parseFloat(String)} like if {@code number} give a {@code +}
|
||||||
|
* or an empty {@link String}, method will return 0.25° and {@code -} will return 0.25°
|
||||||
|
* @param number the number which will be converted
|
||||||
|
* @return the converted float from {@code number} param
|
||||||
|
* @throws NumberFormatException throw by {@link Float#parseFloat(String)}
|
||||||
|
*/
|
||||||
private static float getFloat(String number) throws NumberFormatException {
|
private static float getFloat(String number) throws NumberFormatException {
|
||||||
float n;
|
float n;
|
||||||
if(number.equals("") || number.equals("+"))
|
if(number.equals("") || number.equals("+"))
|
||||||
|
@ -4,9 +4,18 @@ import lsystem.utils.Pair;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to rewrite axiom depending of given rules
|
||||||
|
*/
|
||||||
public class Rewrite {
|
public class Rewrite {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* replace occurrences of left side of {@code rules} to ${#ruleId} in {@code rewritten}
|
||||||
|
* @see Pair
|
||||||
|
* @param rewritten initial word
|
||||||
|
* @param rules list of rules to rewrite {@code rewritten}
|
||||||
|
* @return modified rewritten word
|
||||||
|
*/
|
||||||
private static String replaceRulesByID(final String rewritten, List<Pair<String, String>> rules) {
|
private static String replaceRulesByID(final String rewritten, List<Pair<String, String>> rules) {
|
||||||
String toRewrite = rewritten;
|
String toRewrite = rewritten;
|
||||||
for(int j = 0; j < rules.size(); ++j){
|
for(int j = 0; j < rules.size(); ++j){
|
||||||
@ -16,6 +25,13 @@ public class Rewrite {
|
|||||||
return toRewrite;
|
return toRewrite;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* replace every occurrences of ${#ruleId} to the right side of {@code rules} in {@code toRewrite}
|
||||||
|
* @see Pair
|
||||||
|
* @param toRewrite initial word
|
||||||
|
* @param rules list of rules to rewrite {@code toRewrite}
|
||||||
|
* @return final rewritten word
|
||||||
|
*/
|
||||||
private static String replaceIDByRuleApplication(final String toRewrite, List<Pair<String, String>> rules) {
|
private static String replaceIDByRuleApplication(final String toRewrite, List<Pair<String, String>> rules) {
|
||||||
String rewritten = toRewrite;
|
String rewritten = toRewrite;
|
||||||
for(int j = 0; j < rules.size(); ++j){
|
for(int j = 0; j < rules.size(); ++j){
|
||||||
@ -24,6 +40,13 @@ public class Rewrite {
|
|||||||
return rewritten;
|
return rewritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rewrite {@code axiom} iteratively
|
||||||
|
* @param axiom initial word
|
||||||
|
* @param rules list of rules to rewrite {@code axiom}
|
||||||
|
* @param recurrences number a time we will execute the loop
|
||||||
|
* @return rewritten word
|
||||||
|
*/
|
||||||
public static String rewrite(String axiom, List<Pair<String, String>> rules, int recurrences) {
|
public static String rewrite(String axiom, List<Pair<String, String>> rules, int recurrences) {
|
||||||
String rewritten = axiom;
|
String rewritten = axiom;
|
||||||
for(int i = 0; i < recurrences; ++i) {
|
for(int i = 0; i < recurrences; ++i) {
|
||||||
|
Loading…
Reference in New Issue
Block a user