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

Quick Search    Search Deep

Source code: com/trapezium/chisel/condensers/InterpolatorResolution.java


1   /*
2    * @(#)InterpolatorResolution.java
3    *
4    * Copyright (c) 1998 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.condensers;
12  
13  import com.trapezium.vrml.node.Node;
14  import com.trapezium.vrml.node.PROTOInstance;
15  import com.trapezium.vrml.fields.Field;
16  import com.trapezium.chisel.*;
17  
18  /**
19   *  This adjusts number of digits beyond the decimal point for any interpolator's
20   *  "key" and "keyValue" fields.
21   */
22  public class InterpolatorResolution extends ResolutionAdjuster {
23      int colorKeyResolution;
24      int colorKeyValueResolution;
25      int coordinateKeyResolution;
26      int coordinateKeyValueResolution;
27      int normalKeyResolution;
28      int normalKeyValueResolution;
29      int orientationKeyResolution;
30      int orientationKeyValueResolution;
31      int positionKeyResolution;
32      int positionKeyValueResolution;
33      int scalarKeyResolution;
34      int scalarKeyValueResolution;
35      
36      static final int COLOR_KEY_RESOLUTION = 0;
37      static final int COLOR_KEYVALUE_RESOLUTION = 1;
38      static final int COORDINATE_KEY_RESOLUTION = 2;
39      static final int COORDINATE_KEYVALUE_RESOLUTION = 3;
40      static final int NORMAL_KEY_RESOLUTION = 4;
41      static final int NORMAL_KEYVALUE_RESOLUTION = 5;
42      static final int ORIENTATION_KEY_RESOLUTION = 6;
43      static final int ORIENTATION_KEYVALUE_RESOLUTION = 7;
44      static final int POSITION_KEY_RESOLUTION = 8;
45      static final int POSITION_KEYVALUE_RESOLUTION = 9;
46      static final int SCALAR_KEY_RESOLUTION = 10;
47      static final int SCALAR_KEYVALUE_RESOLUTION = 11;
48  
49    public InterpolatorResolution() {
50      super( "Interpolator", "Adjusting interpolator numeric resolution..." );
51      colorKeyResolution = 3;
52      colorKeyValueResolution = 3;
53      coordinateKeyResolution = 3;
54      coordinateKeyValueResolution = 3;
55      normalKeyResolution = 3;
56      normalKeyValueResolution = 3;
57      orientationKeyResolution = 3;
58      orientationKeyValueResolution = 3;
59      positionKeyResolution = 3;
60      positionKeyValueResolution = 3;
61      scalarKeyResolution = 3;
62      scalarKeyValueResolution = 3;
63    }
64  
65      /** Attempt optimization for an Interpolator Node.  If this node is part of a
66       *  PROTOInstance, nothing is done, since it is assumed the optimization occurs
67       *  within the PROTO declaration.
68       */
69     public void attemptOptimization( Node n ) {
70         if ( n.getParent() instanceof PROTOInstance ) {
71             return;
72         }
73         int keyResolution = 3;
74         int keyValueResolution = 3;
75         String nodeName = n.getBaseName();
76         if ( nodeName.compareTo( "ColorInterpolator" ) == 0 ) {
77             keyResolution = colorKeyResolution;
78             keyValueResolution = colorKeyValueResolution;
79         } else if ( nodeName.compareTo( "CoordinateInterpolator" ) == 0 ) {
80             keyResolution = coordinateKeyResolution;
81             keyValueResolution = coordinateKeyValueResolution;
82         } else if ( nodeName.compareTo( "NormalInterpolator" ) == 0 ) {
83             keyResolution = normalKeyResolution;
84             keyValueResolution = normalKeyValueResolution;
85         } else if ( nodeName.compareTo( "OrientationInterpolator" ) == 0 ) {
86             keyResolution = orientationKeyResolution;
87             keyValueResolution = orientationKeyValueResolution;
88         } else if ( nodeName.compareTo( "PositionInterpolator" ) == 0 ) {
89             keyResolution = positionKeyResolution;
90             keyValueResolution = positionKeyValueResolution;
91         } else if ( nodeName.compareTo( "ScalarInterpolator" ) == 0 ) {
92             keyResolution = scalarKeyResolution;
93             keyValueResolution = scalarKeyValueResolution;
94         }
95         Field key = n.getField( "key" );
96         if ( key != null ) {
97             replaceRange( key.getFirstTokenOffset(), key.getLastTokenOffset(), new Integer( keyResolution ));
98         }
99         Field keyValue = n.getField( "keyValue" );
100        if ( keyValue != null ) {
101            replaceRange( keyValue.getFirstTokenOffset(), keyValue.getLastTokenOffset(), new Integer( keyValueResolution ));
102        }
103    }
104 
105 
106   /** Adjust resolution */
107   public void optimize( TokenPrinter tp, Object param, int startTokenOffset, int endTokenOffset ) {
108       if ( param instanceof Integer ) {
109           int resolution = ((Integer)param).intValue();
110           int resolutionFactor = 1;
111           for ( int i = 0; i < resolution; i++ ) {
112               resolutionFactor = resolutionFactor * 10;
113           }
114           int scanner = startTokenOffset;
115           dataSource.setState( scanner );
116           while ( true ) {
117               if ( dataSource.isNumber( scanner )) {
118                   float fval = dataSource.getFloat( scanner );
119                   fval = fval * resolutionFactor;
120                   int ival = (int)fval;
121                   fval = fval - (float)ival;
122                   if ( fval <= -.5f ) {
123                       ival--;
124                     } else if ( fval >= .5f ) {
125                         ival++;
126                     }
127                   tp.printAtResolution( ival, resolution );
128               } else {
129                   tp.print( dataSource, scanner );
130               }
131               if ( scanner >= endTokenOffset ) {
132                   break;
133               }
134               scanner = dataSource.getNextToken();
135           }
136         }
137   }
138 
139     /** control over 6 interpolators, key & keyValue resolution levels */
140     public int getNumberOptions() {
141         return( 12 );
142     }
143 
144     /** Get the class for an option */
145     public Class getOptionClass( int offset ) {
146         return( Integer.TYPE );
147     }
148 
149     public String getOptionLabel( int offset ) {
150         switch (offset) {
151             case COLOR_KEY_RESOLUTION:
152                 return( "Color key" );
153             case COLOR_KEYVALUE_RESOLUTION:
154                 return( "Color keyValue" );
155             case COORDINATE_KEY_RESOLUTION:
156                 return( "Coordinate key" );
157             case COORDINATE_KEYVALUE_RESOLUTION:
158                 return( "Coordinate keyValue" );
159             case NORMAL_KEY_RESOLUTION:
160                 return( "Normal key" );
161             case NORMAL_KEYVALUE_RESOLUTION:
162                 return( "Normal keyValue" );
163             case ORIENTATION_KEY_RESOLUTION:
164                 return( "Orientation key" );
165             case ORIENTATION_KEYVALUE_RESOLUTION:
166                 return( "Orientation keyValue" );
167             case POSITION_KEY_RESOLUTION:
168                 return( "Position key" );
169             case POSITION_KEYVALUE_RESOLUTION:
170                 return( "Position keyValue" );
171             case SCALAR_KEY_RESOLUTION:
172                 return( "Scalar key" );
173             case SCALAR_KEYVALUE_RESOLUTION:
174                 return( "Scalar keyValue" );
175             default:
176                 return( null );
177         }
178     }
179 
180     public Object getOptionValue( int offset ) {
181         switch (offset) {
182             case COLOR_KEY_RESOLUTION:
183                 return( intToOptionValue( colorKeyResolution ));
184             case COLOR_KEYVALUE_RESOLUTION:
185                 return( intToOptionValue( colorKeyValueResolution ));
186             case COORDINATE_KEY_RESOLUTION:
187                 return( intToOptionValue( coordinateKeyResolution ));
188             case COORDINATE_KEYVALUE_RESOLUTION:
189                 return( intToOptionValue( coordinateKeyValueResolution ));
190             case NORMAL_KEY_RESOLUTION:
191                 return( intToOptionValue( normalKeyResolution ));
192             case NORMAL_KEYVALUE_RESOLUTION:
193                 return( intToOptionValue( normalKeyValueResolution ));
194             case ORIENTATION_KEY_RESOLUTION:
195                 return( intToOptionValue( orientationKeyResolution ));
196             case ORIENTATION_KEYVALUE_RESOLUTION:
197                 return( intToOptionValue( orientationKeyValueResolution ));
198             case POSITION_KEY_RESOLUTION:
199                 return( intToOptionValue( positionKeyResolution ));
200             case POSITION_KEYVALUE_RESOLUTION:
201                 return( intToOptionValue( positionKeyValueResolution ));
202             case SCALAR_KEY_RESOLUTION:
203                 return( intToOptionValue( scalarKeyResolution ));
204             case SCALAR_KEYVALUE_RESOLUTION:
205                 return( intToOptionValue( scalarKeyValueResolution ));
206         }
207         return "";
208     }
209 
210     public void setOptionValue( int offset, Object value ) {
211         switch (offset) {
212             case COLOR_KEY_RESOLUTION:
213                 colorKeyResolution  = optionValueToInt( value );
214                 break;
215             case COLOR_KEYVALUE_RESOLUTION:
216                 colorKeyValueResolution = optionValueToInt( value );
217                 break;
218             case COORDINATE_KEY_RESOLUTION:
219                 coordinateKeyResolution = optionValueToInt( value );
220                 break;
221             case COORDINATE_KEYVALUE_RESOLUTION:
222                 coordinateKeyValueResolution = optionValueToInt( value );
223                 break;
224             case NORMAL_KEY_RESOLUTION:
225                 normalKeyResolution = optionValueToInt( value );
226                 break;
227             case NORMAL_KEYVALUE_RESOLUTION:
228                 normalKeyValueResolution = optionValueToInt( value );
229                 break;
230             case ORIENTATION_KEY_RESOLUTION:
231                 orientationKeyResolution = optionValueToInt( value );
232                 break;
233             case ORIENTATION_KEYVALUE_RESOLUTION:
234                 orientationKeyValueResolution = optionValueToInt( value );
235                 break;
236             case POSITION_KEY_RESOLUTION:
237                 positionKeyResolution = optionValueToInt( value );
238                 break;
239             case POSITION_KEYVALUE_RESOLUTION:
240                 positionKeyValueResolution = optionValueToInt( value );
241                 break;
242             case SCALAR_KEY_RESOLUTION:
243                 scalarKeyResolution = optionValueToInt( value );
244                 break;
245             case SCALAR_KEYVALUE_RESOLUTION:
246                 scalarKeyValueResolution = optionValueToInt( value );
247                 break;
248         }
249     }
250 
251     public Object getOptionConstraints( int offset ) {
252         return( new IntegerConstraints(1, 10, 1 ));
253     }
254 }