📄 configsexpression.java
字号:
if (!(elements.get(1) instanceof Point4d) || !(elements.get(2) instanceof Point4d) || !(elements.get(3) instanceof Point4d)) syntaxError(st, file, "All rows must have four elements") ; return new Matrix4d(((Point4d)elements.get(0)).x, ((Point4d)elements.get(0)).y, ((Point4d)elements.get(0)).z, ((Point4d)elements.get(0)).w, ((Point4d)elements.get(1)).x, ((Point4d)elements.get(1)).y, ((Point4d)elements.get(1)).z, ((Point4d)elements.get(1)).w, ((Point4d)elements.get(2)).x, ((Point4d)elements.get(2)).y, ((Point4d)elements.get(2)).z, ((Point4d)elements.get(2)).w, ((Point4d)elements.get(3)).x, ((Point4d)elements.get(3)).y, ((Point4d)elements.get(3)).z, ((Point4d)elements.get(3)).w) ; } // Anything else is an error. syntaxError(st, file, "Syntax error") ; return null ; } /** * Scan for Java properties in the specified string. Nested properties are * not supported. * * @param st stream tokenizer in use * @param f current file name * @param s string containing non-nested Java properties possibly * interspersed with arbitrary text. * @return scanned string with Java properties replaced with values */ private String scanJavaProperties(StreamTokenizer st, String f, String s) { int open = s.indexOf("${") ; if (open == -1) return s ; int close = 0 ; StringBuffer buf = new StringBuffer() ; while (open != -1) { buf.append(s.substring(close, open)) ; close = s.indexOf('}', open) ; if (close == -1) { elements.add(s) ; // so that the bad element prints out syntaxError(st, f, "Java property substitution syntax error") ; return null ; } String property = s.substring(open + 2, close) ; String value = ConfigCommand.evaluateJavaProperty(property) ; if (value == null) { elements.add(s) ; // so that the bad element prints out syntaxError(st, f, "Java property \"" + property + "\" has a null value") ; return null ; } buf.append(value) ; open = s.indexOf("${", close) ; close++ ; } buf.append(s.substring(close)) ; return buf.toString() ; } /** * This method gets called from the s-expression parser to evaluate a * built-in command. * * @param elements tokenized list of sexp elements * @return object representing result of evaluation */ private Object evaluateBuiltIn(ConfigContainer configContainer, ArrayList elements, int lineNumber) { int argc ; String functionName ; argc = elements.size() ; functionName = (String)elements.get(0) ; if (functionName.equals("Rotate")) { return makeRotate(elements) ; } else if (functionName.equals("Translate")) { return makeTranslate(elements) ; } else if (functionName.equals("RotateTranslate") || functionName.equals("TranslateRotate") || functionName.equals("Concatenate")) { return concatenate(elements) ; } else if (functionName.equals("BoundingSphere")) { return makeBoundingSphere(elements) ; } else { // This built-in can't be evaluated immediately or contains an // unknown command. Create a ConfigCommand for later evaluation. return new ConfigCommand (elements, configContainer.currentFileName, lineNumber) ; } } /** * Processes the built-in command (Translate x y z). * * @param elements ArrayList containing Doubles wrapping x, y, and z * translation components at indices 1, 2, and 3 respectively * * @return matrix that translates by the given x, y, and z components */ private Matrix4d makeTranslate(ArrayList elements) { if (elements.size() != 4) { throw new IllegalArgumentException ("Incorrect number of arguments to Translate") ; } if (!(elements.get(1) instanceof Double) || !(elements.get(2) instanceof Double) || !(elements.get(3) instanceof Double)) { throw new IllegalArgumentException ("All arguments to Translate must be numbers") ; } Matrix4d m4d = new Matrix4d() ; m4d.set(new Vector3d(((Double)elements.get(1)).doubleValue(), ((Double)elements.get(2)).doubleValue(), ((Double)elements.get(3)).doubleValue())) ; return m4d ; } /** * Processes the (Rotate x y z) built-in command. * * @param elements ArrayList containing Doubles wrapping x, y, and z Euler * angles at indices 1, 2, and 3 respectively * * @return matrix that rotates by the given Euler angles around static X, * Y, and Z basis vectors: first about X, then Y, and then Z * * @see Transform3D#setEuler() */ private Matrix4d makeRotate(ArrayList elements) { if (elements.size() != 4) { throw new IllegalArgumentException ("Incorrect number of arguments to Rotate") ; } if (!(elements.get(1) instanceof Double) || !(elements.get(2) instanceof Double) || !(elements.get(3) instanceof Double)) { throw new IllegalArgumentException ("All arguments to Rotate must be numbers") ; } double x = Math.toRadians(((Double)elements.get(1)).doubleValue()) ; double y = Math.toRadians(((Double)elements.get(2)).doubleValue()) ; double z = Math.toRadians(((Double)elements.get(3)).doubleValue()) ; Transform3D t3d = new Transform3D() ; t3d.setEuler(new Vector3d(x, y, z)) ; Matrix4d m4d = new Matrix4d() ; t3d.get(m4d) ; return m4d ; } /** * Processes the (RotateTranslate m1 m2), (TranslateRotate m1 m2), and * (Concatenate m1 m2) built-in commands. Although these do exactly the * same thing, using the appropriate command is recommended in order to * explicitly describe the sequence of transforms and their intent. * * @param elements ArrayList containing Matrix4d objects m1 and m2 at * indices 1 and 2 respectively * * @return matrix that concatenates m1 and m2 in that order: if a point is * transformed by the resulting matrix, then in effect the points are * first transformed by m1 and then m2 */ private Matrix4d concatenate(ArrayList elements) { String functionName = (String)elements.get(0) ; if (elements.size() != 3) { throw new IllegalArgumentException ("Incorrect number of arguments to " + functionName) ; } if (!(elements.get(1) instanceof Matrix4d) || !(elements.get(2) instanceof Matrix4d)) { throw new IllegalArgumentException ("Both arguments to " + functionName + " must be Matrix4d") ; } // Multiply the matrices in the order such that the result, when // transforming a 3D point, will apply the transform represented by // the 1st matrix and then apply the transform represented by the 2nd // matrix. Matrix4d m4d = new Matrix4d((Matrix4d)elements.get(2)) ; m4d.mul((Matrix4d)elements.get(1)) ; return m4d ; } /** * Processes the built-in command (BoundingSphere center radius). * This is used when configuring behaviors. * * @param elements ArrayList containing Point3d at index 1 for the sphere * center and Double at index 2 wrapping the sphere radius, or the String * "infinite" at index 2. * * @return BoundingSphere with the given center and radius */ private BoundingSphere makeBoundingSphere(ArrayList elements) { if (elements.size() != 3) { throw new IllegalArgumentException ("Incorrect number of arguments to BoundingSphere") ; } if (! (elements.get(1) instanceof Point3d) || ! (elements.get(2) instanceof Double || elements.get(2) instanceof String)) throw new IllegalArgumentException ("BoundingSphere needs a Point3d center " + "followed by a Double radius or the String \"infinite\"") ; double r ; if (elements.get(2) instanceof Double) r = ((Double)elements.get(2)).doubleValue() ; else r = Double.POSITIVE_INFINITY ; return new BoundingSphere((Point3d)elements.get(1), r) ; } void print() { System.out.print("(") ; int argc = elements.size() ; for (int i = 0 ; i < argc ; i++) { if (elements.get(i) instanceof Matrix3d) { String[] rows = ConfigCommand.formatMatrixRows ((Matrix3d)elements.get(i)) ; System.out.println("\n ((" + rows[0] + ")") ; System.out.println(" (" + rows[1] + ")") ; System.out.print(" (" + rows[2] + "))") ; if (i != (argc - 1)) System.out.println() ; } else if (elements.get(i) instanceof Matrix4d) { String[] rows = ConfigCommand.formatMatrixRows ((Matrix4d)elements.get(i)) ; System.out.println("\n ((" + rows[0] + ")") ; System.out.println(" (" + rows[1] + ")") ; System.out.println(" (" + rows[2] + ")") ; System.out.print(" (" + rows[3] + "))") ; if (i != (argc - 1)) System.out.println() ; } else if (elements.get(i) instanceof ConfigSexpression) { if (i > 0) System.out.print(" ") ; ((ConfigSexpression)elements.get(i)).print() ; if (i != (argc - 1)) System.out.println() ; } else if (elements.get(i) instanceof ConfigCommand) { if (i > 0) System.out.print(" ") ; System.out.print(elements.get(i).toString()) ; if (i != (argc - 1)) System.out.println() ; } else { if (i > 0) System.out.print(" ") ; System.out.print(elements.get(i).toString()) ; } } System.out.print(")") ; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -