1 package com.insanityengine.ghia.model;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 import com.insanityengine.ghia.m3.*;
8 import com.insanityengine.ghia.util.*;
9 import com.insanityengine.ghia.libograf.*;
10
11 /***
12 *
13 * <P>
14 * Loads aoff geometry files
15 * </P>
16 *
17 * @author BrianHammond
18 *
19 * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/model/GeomLoader.java,v 1.6 2005/03/26 23:42:41 brian Exp $
20 *
21 */
22
23 public class GeomLoader implements DrawingInterface {
24
25
26
27 /***
28 *
29 * Main method
30 *
31 * @param args from the cli
32 *
33 */
34 public final static void main( String args[] ) {
35 GeomLoader gloader = new GeomLoader( args[ 0 ] );
36 gloader.center();
37 gloader.toJava( System.out );
38 }
39
40
41
42 /***
43 *
44 * Constructor
45 *
46 */
47 public GeomLoader() {
48 }
49
50 /***
51 *
52 * Constructor
53 *
54 */
55 public GeomLoader( String href ) {
56 init( href );
57 }
58
59 /***
60 *
61 * Initiliaze this GeomLoader from an href
62 * (file:// is your friend)
63 *
64 * @param href to load from
65 *
66 * @return true if load succeeds, false on fail
67 *
68 */
69 public final boolean init( String href ) {
70 boolean success = false;
71 try {
72 URL tmp = new URL( href );
73
74 BufferedReader in= (
75 new BufferedReader(
76 new InputStreamReader(
77 tmp.openStream()
78 )
79 )
80 );
81
82 StringTokenizer st = new StringTokenizer( in.readLine() );
83
84 int i, j;
85
86 vno = Stdlib.atoi( st.nextToken() );
87 sno = Stdlib.atoi( st.nextToken() );
88
89 ptz = new Pt3[ vno ];
90 ptIdxz = new int[ sno ][];
91
92 float x, y, z;
93
94 for ( i = 0 ; i < vno ; ++i ) {
95 st = new StringTokenizer( in.readLine() );
96 x = Stdlib.atof( st.nextToken() );
97 y = Stdlib.atof( st.nextToken() );
98 z = Stdlib.atof( st.nextToken() );
99
100 if ( i == 0 ) {
101 xmin = xmax = x;
102 ymin = ymax = y;
103 zmin = zmax = z;
104 } else {
105 if ( x < xmin ) xmin = x;
106 if ( x > xmax ) xmax = x;
107 if ( y < ymin ) ymin = y;
108 if ( y > ymax ) ymax = y;
109 if ( z < zmin ) zmin = z;
110 if ( z > zmax ) zmax = z;
111 }
112
113 ptz[ i ] = new Pt3( x, y, z );
114 }
115
116 int vc;
117
118 for ( i = 0 ; i < sno ; ++i ) {
119 st = new StringTokenizer( in.readLine() );
120 vc = Stdlib.atoi( st.nextToken() );
121 ptIdxz[ i ] = new int[ vc ];
122
123 for ( j = 0 ; j < vc ; ++j ) {
124 ptIdxz[ i ][ j ] = Stdlib.atoi( st.nextToken() ) - 1;
125 }
126 }
127
128 name = href.substring( 1 + href.lastIndexOf( java.io.File.separator ) );
129 if ( -1 != name.indexOf( '.' ) ) name = name.substring( 0, name.indexOf( '.' ) );
130 name = name.substring( 0, 1 ).toUpperCase() + name.substring( 1 );
131
132 System.err.println( name + ": " + " sno = " + sno + " vno = " + vno );
133
134 success = true;
135
136 } catch ( Exception e ) {
137
138 }
139 return success;
140 }
141
142 /***
143 *
144 * Move the centroid of the model to the origin.
145 *
146 */
147 public void center() {
148 if ( null != ptz ) {
149 float xminus = ( xmax + xmin ) / 2;
150 float yminus = ( ymax + ymin ) / 2;
151 float zminus = ( zmax + zmin ) / 2;
152
153 Pt3 pt = new Pt3( xminus, yminus, zminus );
154
155 System.err.println( "xmax = " + xmax + " xmin = " + xmin + " xminus = " + xminus );
156 System.err.println( "ymax = " + ymax + " ymin = " + ymin + " yminus = " + yminus );
157 System.err.println( "zmax = " + zmax + " zmin = " + zmin + " zminus = " + zminus );
158 System.err.println( "Center by " + pt );
159
160 for ( int i = 0 ; i < ptz.length ; i++ ) {
161 ptz[ i ].subtract( pt );
162 }
163
164 xmin -= xminus;
165 xmax -= xminus;
166
167 ymin -= yminus;
168 ymax -= yminus;
169
170 zmin -= zminus;
171 zmax -= zminus;
172 }
173 }
174
175 /***
176 *
177 * Do some kinda drawing thang
178 *
179 * @param gl context to draw in
180 *
181 */
182 public void draw( LiboGrafInterface gl ) {
183 int i, j, vc;
184 Pt3 pt;
185
186 float xdiff = xmax - xmin + 1;
187 float ydiff = ymax - ymin + 1;
188
189 for ( i = 0 ; i < sno ; ++i ) {
190 gl.startPolygon();
191
192 vc = ptIdxz[ i ].length;
193 for ( j = 0 ; j < vc ; ++j ) {
194 pt = ptz[ ptIdxz[ i ][ j ] ];
195
196 gl.textCoord(
197 ( pt.x - xmin ) / xdiff,
198 ( pt.y - ymin ) / ydiff
199 );
200
201 gl.addPoint( pt );
202 }
203
204 gl.stopPolygon();
205 }
206 }
207
208 /***
209 *
210 * getVertexCount
211 *
212 * @return a int
213 *
214 */
215 public final int getVertexCount() {
216 return vno;
217 }
218
219 /***
220 *
221 * getSurfaceCount
222 *
223 * @return a int
224 *
225 */
226 public final int getSurfaceCount() {
227 return sno;
228 }
229
230 /***
231 *
232 * Write java code (crudely) representing the original object
233 *
234 * @param out to print to
235 *
236 */
237 public final void toJava( java.io.PrintStream out ) {
238 String eol = "\n";
239 String classname = "GeomLoaded" + name;
240 StringBuffer str = new StringBuffer();
241
242 Pt3 pt;
243 int i, j, vc, pidx;
244
245 str.append( "package com.insanityengine.ghia.GeomLoaded;" + eol );
246 str.append( "" + eol );
247 str.append( "import com.insanityengine.ghia.m3.*;" + eol );
248 str.append( "import com.insanityengine.ghia.libograf.*;" + eol );
249 str.append( "" + eol );
250 str.append( "/**" + eol );
251 str.append( " *" + eol );
252 str.append( " * Generated by com.insanityengine.ghia.m3.GeomLoader" + eol );
253 str.append( " *" + eol );
254 str.append( " */" + eol );
255 str.append( "" + eol );
256 str.append( "public class " + classname + " implements DrawingInterface {" + eol );
257 str.append( "" + eol );
258
259 str.append( "\t/**" + eol );
260 str.append( "\t *" + eol );
261 str.append( "\t * Constructor" + eol );
262 str.append( "\t *" + eol );
263 str.append( "\t */" + eol );
264 str.append( "\tpublic " + classname + "() {" + eol );
265 str.append( "\t}" + eol );
266 str.append( "" + eol );
267
268 str.append( "\t/**" + eol );
269 str.append( "\t *" + eol );
270 str.append( "\t * Do some kinda drawing thang" + eol );
271 str.append( "\t *" + eol );
272 str.append( "\t * @param gl context to draw in" + eol );
273 str.append( "\t *" + eol );
274 str.append( "\t */" + eol );
275 str.append( "\tpublic final void draw( LiboGrafInterface gl ) {" + eol );
276 str.append( "\t\t" + classname + ".staticDraw( gl );" + eol );
277 str.append( "\t}" + eol );
278 str.append( "" + eol );
279
280 int div = 1024;
281
282 str.append( "\t/**" + eol );
283 str.append( "\t *" + eol );
284 str.append( "\t * Do some kinda drawing thang" + eol );
285 str.append( "\t *" + eol );
286 str.append( "\t * @param gl context to draw in" + eol );
287 str.append( "\t *" + eol );
288 str.append( "\t */" + eol );
289 str.append( "\tpublic final static void staticDraw( LiboGrafInterface gl ) {" + eol );
290 str.append( "" + eol );
291 for ( i = j = 0 ; i < sno ; i += div, ++j ) {
292 str.append( "\t\tgl.drawPolygons( getPolygons" + j + "() );" + eol );
293 }
294 str.append( "\t}" + eol );
295 str.append( "" + eol );
296
297 str.append( "\t// P R I V A T E" + eol );
298 str.append( "" + eol );
299 str.append( "\tprivate final static int polygonCount = " + sno + ";" + eol );
300 str.append( "" + eol );
301
302 for ( i = 0 ; i < vno ; ++i ) {
303 str.append(
304 "\tprivate final static Pt3 pt" + i + " = new Pt3( " +
305 ptz[ i ].x + "f, " + ptz[ i ].y + "f, " + ptz[ i ].z +
306 "f );" + eol
307 );
308
309 }
310 str.append( "" + eol );
311
312 int a, b, v;
313 for ( i = j = 0 ; i < sno ; i += div, ++j ) {
314 str.append( "\tprivate final static Pt3 [][] getPolygons" + j + "() {" + eol );
315
316 str.append( "\t\tPt3 [][] polygon = {" + eol );
317 b = i + div;
318 if ( b > sno ) b = sno;
319 for ( a = i ; a < b ; a++ ) {
320 str.append( "\t\t\t{" );
321 vc = ptIdxz[ a ].length;
322 for ( v = 0 ; v < vc ; v++ ) {
323 pidx = ptIdxz[ a ][ v ];
324 if ( 0 != v ) str.append( "," );
325 str.append( " pt" + pidx );
326 }
327 str.append( " }, // " + a + eol );
328 }
329 str.append( "\t\t};" + eol );
330 str.append( "\t\treturn polygon;" + eol );
331 str.append( "" + eol );
332 str.append( "\t}" + eol );
333 str.append( "" + eol );
334 }
335
336 str.append( "}; // that's all GeomLoader.toJava wrote" + eol );
337
338 out.print( str );
339 }
340
341
342
343
344 int vno, sno;
345
346 float xmin, xmax;
347 float ymin, ymax;
348 float zmin, zmax;
349
350 Pt3[] ptz = null;
351 int [][] ptIdxz = null;
352
353 String name = "none";
354 };
355
356 /***
357 *
358 * $Log: GeomLoader.java,v $
359 * Revision 1.6 2005/03/26 23:42:41 brian
360 * fix links
361 *
362 * Revision 1.5 2005/03/21 05:52:12 brian
363 * add enuff stuff to be able to calc pps (polygons per second)
364 *
365 * Revision 1.4 2005/03/19 17:50:02 brian
366 * repackaging
367 *
368 * Revision 1.3 2004/09/16 10:59:07 brian
369 * updated code generator
370 *
371 * Revision 1.2 2004/09/11 19:32:29 brian
372 * effects of adding LiboGrafInterface
373 *
374 * Revision 1.1 2004/09/02 13:17:16 brian
375 * the big reorg
376 *
377 * Revision 1.10 2004/09/02 12:04:26 brian
378 * generate constructor and center before generation in main method
379 *
380 * Revision 1.9 2004/09/02 01:01:30 brian
381 * generate code compiles, but is it right?
382 *
383 * Revision 1.8 2004/09/01 20:23:20 brian
384 * this generated code also does not compile, but it is pretty
385 *
386 * Revision 1.7 2004/09/01 18:30:49 brian
387 * toJava method produces hilarious javac error 'GeomLoadedMech.java:20: code too large', boo-hoo :D
388 *
389 * Revision 1.6 2004/09/01 16:51:53 brian
390 * fixed the center method
391 *
392 * Revision 1.5 2004/09/01 14:53:26 brian
393 * added center method to reposition models centroid
394 *
395 * Revision 1.4 2004/09/01 01:10:42 brian
396 * fix class level javadoc placement
397 *
398 * Revision 1.3 2004/09/01 00:11:06 brian
399 * author, log and header stuff
400 *
401 */