Package com.sun.j3d.utils.universe

Source Code of com.sun.j3d.utils.universe.ConfigSexpression

/*     */ package com.sun.j3d.utils.universe;
/*     */
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.io.StreamTokenizer;
/*     */ import java.util.ArrayList;
/*     */ import javax.media.j3d.BoundingSphere;
/*     */ import javax.media.j3d.Transform3D;
/*     */ import javax.vecmath.Matrix3d;
/*     */ import javax.vecmath.Matrix4d;
/*     */ import javax.vecmath.Point2d;
/*     */ import javax.vecmath.Point3d;
/*     */ import javax.vecmath.Point4d;
/*     */ import javax.vecmath.Vector3d;
/*     */
/*     */ class ConfigSexpression
/*     */ {
/*  52 */   private ArrayList elements = new ArrayList();
/*     */
/*     */   private void syntaxError(StreamTokenizer st, String file, String s) {
/*  55 */     System.out.println(s + ":\nat line " + st.lineno() + " in " + file);
/*  56 */     print(); System.out.print("\n\n");
/*     */   }
/*     */
/*     */   private int myNextToken(StreamTokenizer st, String file) {
/*  60 */     int tok = 0;
/*     */     try {
/*  62 */       tok = st.nextToken();
/*     */     }
/*     */     catch (IOException e) {
/*  65 */       throw new RuntimeException("\n" + e + "\nwhile reading " + file);
/*     */     }
/*  67 */     return tok;
/*     */   }
/*     */
/*     */   Object parseAndEval(ConfigContainer configContainer, StreamTokenizer st, int level)
/*     */   {
/*  75 */     String file = configContainer.currentFileName;
/*     */
/*  81 */     this.elements.clear();
/*     */     int tok;
/*     */     while (true)
/*     */     {
/*  85 */       tok = myNextToken(st, file);
/*     */
/*  87 */       if (tok == -1) {
/*  88 */         return Boolean.FALSE;
/*     */       }
/*  90 */       if (tok == 41) {
/*  91 */         syntaxError(st, file, "Premature closing parenthesis");
/*     */       }
/*  93 */       if (tok == 40) {
/*  94 */         break;
/*     */       }
/*     */     }
/*     */
/*  98 */     for (int i = 0; ; i++) {
/*  99 */       tok = myNextToken(st, file);
/*     */
/* 101 */       if (tok == -1) {
/* 102 */         syntaxError(st, file, "Missing closing parenthesis");
/* 103 */         break;
/*     */       }
/*     */
/* 108 */       if (tok == 40) {
/* 109 */         st.pushBack();
/* 110 */         ConfigSexpression cs = new ConfigSexpression();
/* 111 */         this.elements.add(cs.parseAndEval(configContainer, st, level + 1));
/*     */       }
/*     */       else
/*     */       {
/* 116 */         if (tok == 41)
/*     */         {
/*     */           break;
/*     */         }
/* 120 */         if (i >= 20) {
/* 121 */           syntaxError(st, file, "Too many arguments");
/*     */         }
/*     */
/* 124 */         if (tok == -2) {
/* 125 */           this.elements.add(new Double(st.nval));
/*     */         }
/* 130 */         else if ((tok != -3) && (tok != 34) && (tok != 39)) {
/* 131 */           String badToken = String.valueOf((char)tok);
/* 132 */           this.elements.add(badToken);
/* 133 */           syntaxError(st, file, "Invalid token \"" + badToken + "\" must be enclosed in quotes");
/*     */         }
/*     */         else
/*     */         {
/* 139 */           String s = scanJavaProperties(st, file, st.sval);
/* 140 */           if (s != null)
/*     */           {
/* 142 */             if (s.equalsIgnoreCase("true"))
/*     */             {
/* 144 */               this.elements.add(new Boolean(true));
/*     */             }
/* 146 */             else if (s.equalsIgnoreCase("false"))
/*     */             {
/* 148 */               this.elements.add(new Boolean(false));
/*     */             }
/*     */             else
/*     */             {
/* 152 */               this.elements.add(s);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */
/* 159 */     if (this.elements.size() == 0) {
/* 160 */       syntaxError(st, file, "Null command");
/*     */     }
/*     */
/* 164 */     if ((this.elements.get(0) instanceof String)) {
/*     */       try {
/* 166 */         if (level == 0) {
/* 167 */           configContainer.evaluateCommand(this.elements, st.lineno());
/*     */
/* 170 */           return Boolean.TRUE;
/*     */         }
/*     */
/* 174 */         return evaluateBuiltIn(configContainer, this.elements, st.lineno());
/*     */       }
/*     */       catch (IllegalArgumentException e)
/*     */       {
/* 179 */         syntaxError(st, file, e.getMessage());
/* 180 */         if (level == 0)
/*     */         {
/* 182 */           return Boolean.TRUE;
/*     */         }
/*     */
/* 186 */         return this;
/*     */       }
/*     */
/*     */     }
/*     */
/* 192 */     if (level == 0) {
/* 193 */       syntaxError(st, file, "Malformed top-level command name");
/*     */     }
/*     */
/* 197 */     if ((this.elements.get(0) instanceof Double)) {
/* 198 */       if (this.elements.size() == 1) {
/* 199 */         syntaxError(st, file, "Can't have single-element vector");
/*     */       }
/*     */
/* 202 */       if (this.elements.size() == 2) {
/* 203 */         if (!(this.elements.get(1) instanceof Double)) {
/* 204 */           syntaxError(st, file, "Both elements must be numbers");
/*     */         }
/* 206 */         return new Point2d(((Double)this.elements.get(0)).doubleValue(), ((Double)this.elements.get(1)).doubleValue());
/*     */       }
/*     */
/* 211 */       if (this.elements.size() == 3) {
/* 212 */         if ((!(this.elements.get(1) instanceof Double)) || (!(this.elements.get(2) instanceof Double)))
/*     */         {
/* 214 */           syntaxError(st, file, "All elements must be numbers");
/*     */         }
/* 216 */         return new Point3d(((Double)this.elements.get(0)).doubleValue(), ((Double)this.elements.get(1)).doubleValue(), ((Double)this.elements.get(2)).doubleValue());
/*     */       }
/*     */
/* 222 */       if (this.elements.size() == 4) {
/* 223 */         if ((!(this.elements.get(1) instanceof Double)) || (!(this.elements.get(2) instanceof Double)) || (!(this.elements.get(3) instanceof Double)))
/*     */         {
/* 226 */           syntaxError(st, file, "All elements must be numbers");
/*     */         }
/* 228 */         return new Point4d(((Double)this.elements.get(0)).doubleValue(), ((Double)this.elements.get(1)).doubleValue(), ((Double)this.elements.get(2)).doubleValue(), ((Double)this.elements.get(3)).doubleValue());
/*     */       }
/*     */
/* 235 */       syntaxError(st, file, "Too many vector elements");
/*     */     }
/*     */
/* 239 */     if ((this.elements.get(0) instanceof Point3d)) {
/* 240 */       if (this.elements.size() != 3) {
/* 241 */         syntaxError(st, file, "Matrix must have three rows");
/*     */       }
/* 243 */       if ((!(this.elements.get(1) instanceof Point3d)) || (!(this.elements.get(2) instanceof Point3d)))
/*     */       {
/* 245 */         syntaxError(st, file, "All rows must have three elements");
/*     */       }
/* 247 */       return new Matrix3d(((Point3d)this.elements.get(0)).x, ((Point3d)this.elements.get(0)).y, ((Point3d)this.elements.get(0)).z, ((Point3d)this.elements.get(1)).x, ((Point3d)this.elements.get(1)).y, ((Point3d)this.elements.get(1)).z, ((Point3d)this.elements.get(2)).x, ((Point3d)this.elements.get(2)).y, ((Point3d)this.elements.get(2)).z);
/*     */     }
/*     */
/* 259 */     if ((this.elements.get(0) instanceof Point4d)) {
/* 260 */       if (this.elements.size() == 3) {
/* 261 */         if ((!(this.elements.get(1) instanceof Point4d)) || (!(this.elements.get(2) instanceof Point4d)))
/*     */         {
/* 263 */           syntaxError(st, file, "All rows must have four elements");
/*     */         }
/* 265 */         return new Matrix4d(((Point4d)this.elements.get(0)).x, ((Point4d)this.elements.get(0)).y, ((Point4d)this.elements.get(0)).z, ((Point4d)this.elements.get(0)).w, ((Point4d)this.elements.get(1)).x, ((Point4d)this.elements.get(1)).y, ((Point4d)this.elements.get(1)).z, ((Point4d)this.elements.get(1)).w, ((Point4d)this.elements.get(2)).x, ((Point4d)this.elements.get(2)).y, ((Point4d)this.elements.get(2)).z, ((Point4d)this.elements.get(2)).w, 0.0D, 0.0D, 0.0D, 1.0D);
/*     */       }
/*     */
/* 279 */       if (this.elements.size() != 4) {
/* 280 */         syntaxError(st, file, "Matrix must have three or four rows");
/*     */       }
/* 282 */       if ((!(this.elements.get(1) instanceof Point4d)) || (!(this.elements.get(2) instanceof Point4d)) || (!(this.elements.get(3) instanceof Point4d)))
/*     */       {
/* 285 */         syntaxError(st, file, "All rows must have four elements");
/*     */       }
/* 287 */       return new Matrix4d(((Point4d)this.elements.get(0)).x, ((Point4d)this.elements.get(0)).y, ((Point4d)this.elements.get(0)).z, ((Point4d)this.elements.get(0)).w, ((Point4d)this.elements.get(1)).x, ((Point4d)this.elements.get(1)).y, ((Point4d)this.elements.get(1)).z, ((Point4d)this.elements.get(1)).w, ((Point4d)this.elements.get(2)).x, ((Point4d)this.elements.get(2)).y, ((Point4d)this.elements.get(2)).z, ((Point4d)this.elements.get(2)).w, ((Point4d)this.elements.get(3)).x, ((Point4d)this.elements.get(3)).y, ((Point4d)this.elements.get(3)).z, ((Point4d)this.elements.get(3)).w);
/*     */     }
/*     */
/* 306 */     syntaxError(st, file, "Syntax error");
/* 307 */     return null;
/*     */   }
/*     */
/*     */   private String scanJavaProperties(StreamTokenizer st, String f, String s)
/*     */   {
/* 321 */     int open = s.indexOf("${");
/* 322 */     if (open == -1) return s;
/*     */
/* 324 */     int close = 0;
/* 325 */     StringBuffer buf = new StringBuffer();
/* 326 */     while (open != -1) {
/* 327 */       buf.append(s.substring(close, open));
/* 328 */       close = s.indexOf('}', open);
/* 329 */       if (close == -1) {
/* 330 */         this.elements.add(s);
/* 331 */         syntaxError(st, f, "Java property substitution syntax error");
/* 332 */         return null;
/*     */       }
/*     */
/* 335 */       String property = s.substring(open + 2, close);
/* 336 */       String value = ConfigCommand.evaluateJavaProperty(property);
/* 337 */       if (value == null) {
/* 338 */         this.elements.add(s);
/* 339 */         syntaxError(st, f, "Java property \"" + property + "\" has a null value");
/*     */
/* 341 */         return null;
/*     */       }
/*     */
/* 344 */       buf.append(value);
/* 345 */       open = s.indexOf("${", close);
/* 346 */       close++;
/*     */     }
/*     */
/* 349 */     buf.append(s.substring(close));
/* 350 */     return buf.toString();
/*     */   }
/*     */
/*     */   private Object evaluateBuiltIn(ConfigContainer configContainer, ArrayList elements, int lineNumber)
/*     */   {
/* 365 */     int argc = elements.size();
/* 366 */     String functionName = (String)elements.get(0);
/*     */
/* 368 */     if (functionName.equals("Rotate")) {
/* 369 */       return makeRotate(elements);
/*     */     }
/* 371 */     if (functionName.equals("Translate")) {
/* 372 */       return makeTranslate(elements);
/*     */     }
/* 374 */     if ((functionName.equals("RotateTranslate")) || (functionName.equals("TranslateRotate")) || (functionName.equals("Concatenate")))
/*     */     {
/* 378 */       return concatenate(elements);
/*     */     }
/* 380 */     if (functionName.equals("BoundingSphere")) {
/* 381 */       return makeBoundingSphere(elements);
/*     */     }
/*     */
/* 386 */     return new ConfigCommand(elements, configContainer.currentFileName, lineNumber);
/*     */   }
/*     */
/*     */   private Matrix4d makeTranslate(ArrayList elements)
/*     */   {
/* 400 */     if (elements.size() != 4) {
/* 401 */       throw new IllegalArgumentException("Incorrect number of arguments to Translate");
/*     */     }
/*     */
/* 405 */     if ((!(elements.get(1) instanceof Double)) || (!(elements.get(2) instanceof Double)) || (!(elements.get(3) instanceof Double)))
/*     */     {
/* 408 */       throw new IllegalArgumentException("All arguments to Translate must be numbers");
/*     */     }
/*     */
/* 412 */     Matrix4d m4d = new Matrix4d();
/* 413 */     m4d.set(new Vector3d(((Double)elements.get(1)).doubleValue(), ((Double)elements.get(2)).doubleValue(), ((Double)elements.get(3)).doubleValue()));
/*     */
/* 417 */     return m4d;
/*     */   }
/*     */
/*     */   private Matrix4d makeRotate(ArrayList elements)
/*     */   {
/* 432 */     if (elements.size() != 4) {
/* 433 */       throw new IllegalArgumentException("Incorrect number of arguments to Rotate");
/*     */     }
/*     */
/* 437 */     if ((!(elements.get(1) instanceof Double)) || (!(elements.get(2) instanceof Double)) || (!(elements.get(3) instanceof Double)))
/*     */     {
/* 440 */       throw new IllegalArgumentException("All arguments to Rotate must be numbers");
/*     */     }
/*     */
/* 444 */     double x = Math.toRadians(((Double)elements.get(1)).doubleValue());
/* 445 */     double y = Math.toRadians(((Double)elements.get(2)).doubleValue());
/* 446 */     double z = Math.toRadians(((Double)elements.get(3)).doubleValue());
/*     */
/* 448 */     Transform3D t3d = new Transform3D();
/* 449 */     t3d.setEuler(new Vector3d(x, y, z));
/*     */
/* 451 */     Matrix4d m4d = new Matrix4d();
/* 452 */     t3d.get(m4d);
/*     */
/* 454 */     return m4d;
/*     */   }
/*     */
/*     */   private Matrix4d concatenate(ArrayList elements)
/*     */   {
/* 471 */     String functionName = (String)elements.get(0);
/*     */
/* 473 */     if (elements.size() != 3) {
/* 474 */       throw new IllegalArgumentException("Incorrect number of arguments to " + functionName);
/*     */     }
/*     */
/* 478 */     if ((!(elements.get(1) instanceof Matrix4d)) || (!(elements.get(2) instanceof Matrix4d)))
/*     */     {
/* 480 */       throw new IllegalArgumentException("Both arguments to " + functionName + " must be Matrix4d");
/*     */     }
/*     */
/* 488 */     Matrix4d m4d = new Matrix4d((Matrix4d)elements.get(2));
/* 489 */     m4d.mul((Matrix4d)elements.get(1));
/*     */
/* 491 */     return m4d;
/*     */   }
/*     */
/*     */   private BoundingSphere makeBoundingSphere(ArrayList elements)
/*     */   {
/* 505 */     if (elements.size() != 3) {
/* 506 */       throw new IllegalArgumentException("Incorrect number of arguments to BoundingSphere");
/*     */     }
/*     */
/* 510 */     if ((!(elements.get(1) instanceof Point3d)) || ((!(elements.get(2) instanceof Double)) && (!(elements.get(2) instanceof String))))
/*     */     {
/* 513 */       throw new IllegalArgumentException("BoundingSphere needs a Point3d center followed by a Double radius or the String \"infinite\"");
/*     */     }
/*     */     double r;
/*     */     double r;
/* 518 */     if ((elements.get(2) instanceof Double))
/* 519 */       r = ((Double)elements.get(2)).doubleValue();
/*     */     else {
/* 521 */       r = (1.0D / 0.0D);
/*     */     }
/* 523 */     return new BoundingSphere((Point3d)elements.get(1), r);
/*     */   }
/*     */
/*     */   void print() {
/* 527 */     System.out.print("(");
/* 528 */     int argc = this.elements.size();
/* 529 */     for (int i = 0; i < argc; i++) {
/* 530 */       if ((this.elements.get(i) instanceof Matrix3d)) {
/* 531 */         String[] rows = ConfigCommand.formatMatrixRows((Matrix3d)this.elements.get(i));
/*     */
/* 533 */         System.out.println("\n ((" + rows[0] + ")");
/* 534 */         System.out.println("  (" + rows[1] + ")");
/* 535 */         System.out.print("  (" + rows[2] + "))");
/* 536 */         if (i != argc - 1) System.out.println();
/*     */       }
/* 538 */       else if ((this.elements.get(i) instanceof Matrix4d)) {
/* 539 */         String[] rows = ConfigCommand.formatMatrixRows((Matrix4d)this.elements.get(i));
/*     */
/* 541 */         System.out.println("\n ((" + rows[0] + ")");
/* 542 */         System.out.println("  (" + rows[1] + ")");
/* 543 */         System.out.println("  (" + rows[2] + ")");
/* 544 */         System.out.print("  (" + rows[3] + "))");
/* 545 */         if (i != argc - 1) System.out.println();
/*     */       }
/* 547 */       else if ((this.elements.get(i) instanceof ConfigSexpression)) {
/* 548 */         if (i > 0) System.out.print(" ");
/* 549 */         ((ConfigSexpression)this.elements.get(i)).print();
/* 550 */         if (i != argc - 1) System.out.println();
/*     */       }
/* 552 */       else if ((this.elements.get(i) instanceof ConfigCommand)) {
/* 553 */         if (i > 0) System.out.print(" ");
/* 554 */         System.out.print(this.elements.get(i).toString());
/* 555 */         if (i != argc - 1) System.out.println();
/*     */       }
/*     */       else
/*     */       {
/* 558 */         if (i > 0) System.out.print(" ");
/* 559 */         System.out.print(this.elements.get(i).toString());
/*     */       }
/*     */     }
/* 562 */     System.out.print(")");
/*     */   }
/*     */ }

/* Location:           Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name:     com.sun.j3d.utils.universe.ConfigSexpression
* JD-Core Version:    0.6.2
*/
TOP

Related Classes of com.sun.j3d.utils.universe.ConfigSexpression

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.