10 typedef float triplet
[3];
11 typedef int quadrlat
[4];
16 Tcolorin
, Tcolorout
, Tnameout
} ArgType
;
18 extern char *malloc();
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};
38 FILE *infile
, *outfile
= NULL
;
42 * mucky global variables
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
)
64 for( i
= 0; i
<argc
; i
++ ) {
65 if( *argv
[i
] == '-' ) {
68 for( j
= 0; ArgName
[j
]; j
++ ) {
69 if( !strncmp(ArgName
[j
], argv
[i
]+1, strlen(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
] );
84 if( strlen(argv
[i
]+1) > strlen(ArgName
[j
]) ) {
85 argpos
= argv
[i
] + strlen(ArgName
[j
]) +1;
90 fprintf(stderr
,"missing arg for -%s\n", argv
[i
-1] );
95 switch( (ArgType
)j
) {
99 element
= j
- (int)TminX
;
100 MinIn
[element
] = atof(argpos
);
101 MinSet
[element
] = true;
106 element
= j
- (int)TmaxX
;
107 MaxIn
[element
] = atof(argpos
);
108 MaxSet
[element
] = true;
110 case Tcolorin
: colorin
= atoi(argpos
); break;
111 case Tcolorout
: colorout
= atoi(argpos
); break;
112 case Tnameout
: nameout
= argpos
;
122 fprintf(stderr
,"No input file named\n");
126 nameout
= (char *)malloc(200); /* replace with max path size */
127 sprintf(nameout
, "%s.v3d", namein
);
132 * read an integer number into variable
139 if( !fscanf(infile
, "%d", IntVar
)) {
140 fprintf(stderr
, "could not read integer at line %d\n", linenum
);
144 * skip to end of line
146 while( c
= getc(infile
), c
!= EOF
&& c
!= '\n' ) ;
149 void readTriplet( tripvar
)
153 if(!fscanf(infile
, "%f %f %f",tripvar
, tripvar
+1, tripvar
+2)) {
154 fprintf(stderr
,"could not read coordinate triplet at line %d\n",
160 * read an entire ring
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
178 readInt( &NumRings
);
180 readInt( &RevRange
);
182 PtsInPerRing
= RevMesh
+ 1;
183 if( RevRange
== 360 ) {
185 PtsOutPerRing
= RevMesh
;
189 PtsOutPerRing
= PtsInPerRing
;
193 * this procedure reads in a list of data points to determine the
195 * total number of data points to write out
196 * maximun x,y,z spread
203 inring
= (triplet
*)malloc(sizeof(triplet
)*PtsInPerRing
);
204 indlist
= (char *)malloc(sizeof(char)*NumRings
);
206 for( i
= 0; i
< NumRings
; i
++ ) {
210 for( j
= 0; j
< PtsOutPerRing
; j
++ ) {
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] ) {
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
];
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
)
253 for( i
= 0; i
< 3; i
++ ) {
254 fprintf(outfile
,"%f%c", trip
[i
]*Scale
[i
]+Disp
[i
], i
==2?'\n':' ');
262 for( i
= 0; i
< PtsOutPerRing
; i
++ ) {
263 writeTriplet(inring
[i
]);
273 fprintf(outfile
, "%d\n", pnt_total
);
274 for( ringno
= 0; ringno
< NumRings
; ringno
++ ) {
276 if( indlist
[ringno
] )
279 writeTriplet( inring
[0] );
284 void writeQuad(quad
, numpts
, color
, order
)
286 int numpts
, color
, order
;
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
);
302 PolysPerRing
= PtsInPerRing
-1;
304 for( RingB
=1; PtCntA
= PtCntB
, RingB
< NumRings
; RingA
= RingB
++ ) {
306 PtCntB
+= indlist
[RingA
]? PtsOutPerRing
: 1;
307 for( RevA
=0; RevA
< PolysPerRing
; RevA
++ ) {
312 if( RevB
>= PtsOutPerRing
) RevB
= 0;
315 if(indlist
[RingA
] ) {
316 quad
[qcnt
++] = PtCntA
+ RevA
;
317 quad
[qcnt
++] = PtCntA
+ RevB
;
320 quad
[qcnt
++] = PtCntA
;
322 if(indlist
[RingB
] ) {
323 quad
[qcnt
++] = PtCntB
+ RevB
;
324 quad
[qcnt
++] = PtCntB
+ RevA
;
327 quad
[qcnt
++] = PtCntB
;
330 if( colorin
>= 0 ) writeQuad(quad
, qcnt
, colorin
, false);
331 if( colorout
>=0 ) writeQuad(quad
, qcnt
, colorout
, true);
341 getCliArgs(argc
, argv
);
342 infile
= fopen(namein
, "r");
344 fprintf(stderr
,"can not read \"%s\"\n", namein
);
352 outfile
= fopen(nameout
, "w");
354 fprintf(stderr
, "can not write to \"%s\"\n", nameout
);
358 infile
= fopen(namein
, "r");