Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/trapezium/chisel/mutators/EV_to_IFS.java


1   /*
2    * @(#)EV_to_IFS.java
3    *
4    * Copyright (c) 1998-1999 by Trapezium Development LLC.  All Rights Reserved.
5    *
6    * The information in this file is the property of Trapezium Development LLC
7    * and may be used only in accordance with the terms of the license granted
8    * by Trapezium.
9    *
10   */
11  package com.trapezium.chisel.mutators;
12  
13  import com.trapezium.chisel.*;
14  
15  import com.trapezium.parse.TokenTypes;
16  import com.trapezium.vrml.fields.Field;
17  import com.trapezium.vrml.fields.FieldValue;
18  import com.trapezium.vrml.fields.MFFieldValue;
19  import com.trapezium.vrml.node.Node;
20  import com.trapezium.vrml.VrmlElement;
21  import com.trapezium.vrml.fields.ExposedField;
22  import com.trapezium.vrml.fields.SFNodeValue;
23  
24  public class EV_to_IFS extends Optimizer {
25  
26      /** The main class for performing an ElevationGrid split operation */
27      class EVinfo {
28          Node elevationGridNode;
29          float[] heights;
30  
31          public EVinfo( Node n ) {
32              elevationGridNode = n;
33          }
34  
35          void convertToIFS( TokenPrinter tp ) {
36              tp.print( "IndexedFaceSet {" );
37          int xDimension = elevationGridNode.getIntValue( "xDimension" );
38          int zDimension = elevationGridNode.getIntValue( "zDimension" );
39          float xSpacing = elevationGridNode.getFloatValue( dataSource, "xSpacing" );
40          float zSpacing = elevationGridNode.getFloatValue( dataSource, "zSpacing" );
41          int numberPoints = xDimension*zDimension;
42          if ( numberPoints > 0 ) {
43              heights = elevationGridNode.getFloatArray( "height" );
44              if ( heights != null ) {
45                  // coordinates
46                  tp.flush();
47                  tp.print( "coord Coordinate { point [" );
48                  tp.flush();
49                  int heightScanner = 0;
50                  for ( int i = 0; i < zDimension; i++ ) {
51                      float zValue = zSpacing * i;
52                      for ( int j = 0; j < xDimension; j++ ) {
53                          float xValue = xSpacing * j;
54                          tp.print( xValue );
55                          tp.print( heights[ heightScanner ] );
56                          tp.print( zValue );
57                          heightScanner++;
58                      }
59                      tp.flush();
60                  }
61                  tp.print( "] }" );
62                  tp.flush();
63                  
64                  // coordIndex
65                  tp.print( "coordIndex [" );
66                  tp.flush();
67                  for ( int i = 0; i < ( zDimension - 1 ); i++ ) {
68                      for ( int j = 0; j < ( xDimension - 1 ); j++ ) {
69                          int base = i * xDimension + j;
70                          // each egrid square turns into two triangles
71                          tp.print( base );
72                          tp.print( base + xDimension );
73                          tp.print( base + xDimension + 1 );
74                          tp.print( -1 );
75                          tp.print( base );
76                          tp.print( base + xDimension + 1 );
77                          tp.print( base + 1 );
78                          tp.print( -1 );
79                      }
80                      tp.flush();
81                  }
82                  tp.print( "]" );
83                  tp.flush();
84                }
85            }
86            Field texCoord = elevationGridNode.getField( "texCoord" );
87            if ( texCoord != null ) {
88                tp.flush();
89                tp.printRange( texCoord.getFirstTokenOffset(), texCoord.getLastTokenOffset(), false );
90                tp.flush();
91            }
92              tp.print( "}" );
93          }
94      }
95  
96    public EV_to_IFS() {
97      super( "ElevationGrid", "Convert ElevationGrid to IFS..." );
98    }
99  
100   public void attemptOptimization( Node n ) {
101         replaceRange( n.getFirstTokenOffset(), n.getLastTokenOffset(), new EVinfo( n ));
102   }
103 
104 
105   // RangeReplacer calls this when it has a range of tokens to replace
106   public void optimize( TokenPrinter tp, Object param, int startTokenOffset, int endTokenOffset ) {
107       if ( param instanceof EVinfo ) {
108           EVinfo ei = (EVinfo)param;
109           ei.convertToIFS( tp );
110       }
111   }
112 }
113 
114