# dont perform the libgen and libsocket test in configure for AROS.
[AROS-Contrib.git] / fish / surf / tov3d.c
blob539fb6967fc8c29ae5a2216937134aa8608fef75
1 #include <stdio.h>
2 #include <math.h>
4 #define true 1
5 #define false 0
7 /*
8 * types
9 */
10 typedef float triplet[3];
11 typedef int quadrlat[4];
13 typedef enum {
14 TminX, TminY, TminZ,
15 TmaxX, TmaxY, TmaxZ,
16 Tcolorin, Tcolorout, Tnameout } ArgType;
18 extern char *malloc();
19 extern double atof();
21 char *ArgName[] = {
22 "minx", "miny", "minz", "maxx", "maxy", "maxz", "ci", "co", "o", NULL
26 * command line arguments
28 triplet MinIn, MaxIn; /* define bounding box */
29 char MinSet[3] = {false}, MaxSet[3] = {false};
31 int colorin = -1,
32 colorout = -1;
33 char *namein = NULL,
34 *nameout = NULL;
36 * file arguments
38 FILE *infile, *outfile= NULL;
39 int linenum;
42 * mucky global variables
44 triplet *inring;
45 char *indlist;
46 int pnt_total; /* total number of points */
47 triplet Scale = { 1.0, 1.0, 1.0 };
48 triplet Disp = { 0.0, 0.0, 0.0 };
51 int NumRings, /* number of curved rings */
52 PtsInPerRing, /* points read in per ring */
53 PtsOutPerRing, /* points written per ring */
54 IsClosed; /* true if revolution is 360 degrees */
57 * parse the command line arguments
59 void getCliArgs(argc, argv)
60 int argc;
61 char *argv[];
63 int i;
64 for( i = 0; i <argc; i++ ) {
65 if( *argv[i] == '-' ) {
66 int j;
68 for( j = 0; ArgName[j]; j++ ) {
69 if( !strncmp(ArgName[j], argv[i]+1, strlen(ArgName[j])))
70 break;
73 if( !ArgName[j] ) {
74 fprintf(stderr,"%s unknown, try one of below:\n", argv[i]);
75 for( j = 0; ArgName[j]; j++ ) {
76 fprintf(stderr,"-%s val\n", ArgName[j] );
78 exit(-1);
80 else {
81 char *argpos;
82 register int element;
84 if( strlen(argv[i]+1) > strlen(ArgName[j]) ) {
85 argpos = argv[i] + strlen(ArgName[j]) +1;
87 else {
88 i++;
89 if( i >= argc ) {
90 fprintf(stderr,"missing arg for -%s\n", argv[i-1] );
91 exit(-1);
93 argpos = argv[i];
95 switch( (ArgType)j ) {
96 case TminX:
97 case TminY:
98 case TminZ:
99 element = j - (int)TminX;
100 MinIn[element] = atof(argpos);
101 MinSet[element] = true;
102 break;
103 case TmaxX:
104 case TmaxY:
105 case TmaxZ:
106 element = j - (int)TmaxX;
107 MaxIn[element] = atof(argpos);
108 MaxSet[element] = true;
109 break;
110 case Tcolorin: colorin = atoi(argpos); break;
111 case Tcolorout: colorout = atoi(argpos); break;
112 case Tnameout: nameout = argpos;
113 } /* end case */
116 else {
117 namein = argv[i];
119 } /* end for loop */
121 if (!namein ) {
122 fprintf(stderr,"No input file named\n");
123 exit(-1);
125 if(!nameout) {
126 nameout = (char *)malloc(200); /* replace with max path size */
127 sprintf(nameout, "%s.v3d", namein);
132 * read an integer number into variable
134 void readInt(IntVar)
135 int *IntVar;
137 char c;
138 linenum++;
139 if( !fscanf(infile, "%d", IntVar)) {
140 fprintf(stderr, "could not read integer at line %d\n", linenum);
141 exit(-1);
144 * skip to end of line
146 while( c = getc(infile), c != EOF && c != '\n' ) ;
149 void readTriplet( tripvar )
150 triplet tripvar;
152 linenum++;
153 if(!fscanf(infile, "%f %f %f",tripvar, tripvar+1, tripvar+2)) {
154 fprintf(stderr,"could not read coordinate triplet at line %d\n",
155 linenum);
156 exit(-1);
160 * read an entire ring
162 void readRing() {
163 int i;
164 for( i = 0; i< PtsInPerRing; i++ ) {
165 readTriplet(inring[i]);
170 * read the first few lines in the file that tell us how many
171 * rings, howmany points per ring, etc
173 void readHeader() {
174 int RevRange;
175 int RevMesh;
177 linenum = 0;
178 readInt( &NumRings );
179 readInt( &RevMesh );
180 readInt( &RevRange );
182 PtsInPerRing = RevMesh + 1;
183 if( RevRange == 360 ) {
184 IsClosed = true;
185 PtsOutPerRing = RevMesh;
187 else {
188 IsClosed = false;
189 PtsOutPerRing = PtsInPerRing;
193 * this procedure reads in a list of data points to determine the
194 * following:
195 * total number of data points to write out
196 * maximun x,y,z spread
198 void readPass1() {
199 triplet max3, min3;
200 int i;
201 pnt_total = 0;
203 inring = (triplet *)malloc(sizeof(triplet)*PtsInPerRing);
204 indlist = (char *)malloc(sizeof(char)*NumRings);
206 for( i = 0; i < NumRings; i++ ) {
207 int j;
208 readRing();
210 for( j = 0; j < PtsOutPerRing; j++ ) {
211 int k;
212 for( k = 0; k < 3; k++ ) {
213 if( max3[k] < inring[j][k] || !i && !j ) max3[k] = inring[j][k];
214 if( min3[k] > inring[j][k] || !i && !j ) min3[k] = inring[j][k];
217 if( inring[0][0] == inring[1][0] &&
218 inring[0][1] == inring[1][1] ) {
219 indlist[i] = 0;
220 pnt_total += 1;
222 else {
223 indlist[i] = 1;
224 pnt_total += PtsOutPerRing;
228 * compute scaling and displacement
230 for( i = 0; i < 3; i++ ) {
231 if( MaxSet[i] && !MinSet[i] )
232 MinIn[i] = -MaxIn[i];
233 else if( !MaxSet[i] && MinSet[i] )
234 MaxIn[i] = -MinIn[i];
236 Scale[i] = 1.0;
237 Disp[i] = 0.0;
238 if( MaxSet[i] || MinSet[i] ) {
239 float diffOut, diffIn;
241 diffOut = MaxIn[i] - MinIn[i];
242 diffIn = max3[i] - min3[i];
243 if( diffIn > 0 ) Scale[i] = diffOut/diffIn;
244 Disp[i] = MaxIn[i] - max3[i] * Scale[i];
249 void writeTriplet(trip)
250 triplet trip;
252 int i;
253 for( i = 0; i < 3; i++ ) {
254 fprintf(outfile,"%f%c", trip[i]*Scale[i]+Disp[i], i==2?'\n':' ');
259 void writeRing()
261 int i;
262 for( i = 0; i < PtsOutPerRing; i++ ) {
263 writeTriplet(inring[i]);
269 void writePoints()
271 int ringno;
273 fprintf(outfile, "%d\n", pnt_total);
274 for( ringno = 0; ringno < NumRings; ringno++ ) {
275 readRing();
276 if( indlist[ringno] )
277 writeRing();
278 else
279 writeTriplet( inring[0] );
284 void writeQuad(quad, numpts, color, order)
285 quadrlat quad;
286 int numpts, color, order;
288 int i;
289 fprintf(outfile, "%d ", numpts);
290 for(i=0; i <numpts; i++ ) {
291 fprintf(outfile,"%d ", quad[order?i:(numpts-i -1)]);
293 fprintf(outfile,"%d \n", color);
296 void writePolys() {
297 int RevA, RevB;
298 int PtCntA, PtCntB;
299 int RingA, RingB;
300 int PolysPerRing;
302 PolysPerRing = PtsInPerRing -1;
303 PtCntB = RingA = 0;
304 for( RingB =1; PtCntA = PtCntB, RingB < NumRings; RingA = RingB++ ) {
306 PtCntB += indlist[RingA]? PtsOutPerRing: 1;
307 for( RevA =0; RevA < PolysPerRing; RevA++ ) {
308 quadrlat quad;
309 int qcnt;
311 RevB = RevA +1;
312 if( RevB >= PtsOutPerRing ) RevB = 0;
314 qcnt = 0;
315 if(indlist[RingA] ) {
316 quad[qcnt++] = PtCntA + RevA;
317 quad[qcnt++] = PtCntA + RevB;
319 else
320 quad[qcnt++] = PtCntA;
322 if(indlist[RingB] ) {
323 quad[qcnt++] = PtCntB + RevB;
324 quad[qcnt++] = PtCntB + RevA;
326 else
327 quad[qcnt++] = PtCntB;
329 if( qcnt >= 3 ) {
330 if( colorin >= 0 ) writeQuad(quad, qcnt, colorin, false);
331 if( colorout >=0 ) writeQuad(quad, qcnt, colorout, true);
337 main(argc, argv)
338 int argc;
339 char *argv[];
341 getCliArgs(argc, argv);
342 infile = fopen(namein, "r");
343 if(!infile) {
344 fprintf(stderr,"can not read \"%s\"\n", namein);
345 exit(-1);
348 readHeader();
349 readPass1();
350 fclose(infile);
352 outfile = fopen(nameout, "w");
353 if( !outfile) {
354 fprintf(stderr, "can not write to \"%s\"\n", nameout);
355 exit(-1);
358 infile = fopen(namein, "r");
359 readHeader();
360 writePoints();
361 fclose(infile);
362 writePolys();
363 fclose(outfile);