home *** CD-ROM | disk | FTP | other *** search
- /* thread.c - Generate a threaded screw-like object
- * - Written by Glenn M. Lewis - 5/15/91
- *
- * This code is placed in the public domain without any warranty for
- * suitability for a particular purpose. Please just leave my name in
- * the source as the original author.
- */
-
- #include <stdio.h>
- #include <math.h>
-
- #define PI (3.14159265)
- char *malloc();
- double atof(), SPACING, HEIGHT, IN_RADIUS, OUT_RADIUS;
- double height, heightstep, theta, thetastep;
- double *outer_x, *outer_y, *outer_z;
- double *inner_x, *inner_y, *inner_z;
- int e, f, edge, face, count, NUMPTS;
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int i;
-
- if (argc != 6) {
- fprintf(stderr, "Usage: %s NUMPTS SPACING HEIGHT IN_RADIUS OUT_RADIUS\n",
- argv[0]);
- fprintf(stderr, "where NUMPTS is the number of points in one revolution\n");
- fprintf(stderr, " SPACING is the number of units between threads\n");
- fprintf(stderr, " HEIGHT is the total height of the bolt.\n");
- fprintf(stderr, " IN_RADIUS is the radius of the inner thread.\n");
- fprintf(stderr, " OUT_RADIUS is the radius of the outer thread.\n");
- fprintf(stderr, "A good starting point would be:\n");
- fprintf(stderr, "Thread 60 0.5 10 4 5 | WriteTDDD > Thread.tddd\n");
- exit(0);
- }
-
- printf(
- "/* thread.ttddd - Written by Glenn M. Lewis - 3/26/91\n\
- * Steve Worley had asked on the Imagine mailing list how to create\n\
- * a thread-like object for Imagine. Instead of saying 'how about\n\
- * using TTDDD to create it,' I thought... I'll see if I can create\n\
- * a thread using TTDDD. Here are the results.\n\
- */\n\
- \n\
- OBJ Begin\n\
- DESC Begin\n\
- NAME \"Thread\"\n\
- SHAP Shape 2\n\
- SHAP Lamp 0\n\
- \n");
-
- /* OK. Start calculating the position of the points, edges, and faces... */
- NUMPTS = atoi(argv[1]);
- SPACING = atof(argv[2]);
- HEIGHT = atof(argv[3]);
- IN_RADIUS = atof(argv[4]);
- OUT_RADIUS = atof(argv[5]);
-
- /*
- * Calculate the points needed...
- */
- height = 0.0;
- heightstep = SPACING/(double)NUMPTS;
- theta = 0.0;
- thetastep = PI*2.0/(double)NUMPTS;
-
- count = 2+HEIGHT/heightstep;
- if (!(outer_x = (double*)malloc((unsigned)count*sizeof(double)))) {
- NO_MEM:
- fprintf(stderr, "Out of memory.\n*** ABORT ***\n");
- exit(-1);
- }
- if (!(outer_y = (double*)malloc((unsigned)count*sizeof(double)))) goto NO_MEM;
- if (!(outer_z = (double*)malloc((unsigned)count*sizeof(double)))) goto NO_MEM;
- if (!(inner_x = (double*)malloc((unsigned)count*sizeof(double)))) goto NO_MEM;
- if (!(inner_y = (double*)malloc((unsigned)count*sizeof(double)))) goto NO_MEM;
- if (!(inner_z = (double*)malloc((unsigned)count*sizeof(double)))) goto NO_MEM;
-
- count = 0;
- while (height < HEIGHT) {
- outer_x[count] = sin(theta) * OUT_RADIUS;
- outer_y[count] = cos(theta) * OUT_RADIUS;
- outer_z[count] = height;
- inner_x[count] = sin(theta) * IN_RADIUS;
- inner_y[count] = cos(theta) * IN_RADIUS;
- inner_z[count] = height+SPACING/2.0;
- theta += thetastep;
- height += heightstep;
- count++;
- }
-
- /*
- * Start writing the TTDDD data...
- *
- * First, list the points...
- */
- printf(" PNTS Pcount %d\n", count*2);
- edge = 0;
- for (i=0; i<count; i++) {
- printf(" PNTS Point %d %g %g %g\n", i*2,
- outer_x[i], outer_y[i], outer_z[i]);
- printf(" PNTS Point %d %g %g %g\n", i*2+1,
- inner_x[i], inner_y[i], inner_z[i]);
- /* Count up the number of edges we need */
- if (i<count-1) {
- edge += 3; /* O[i]..O[i+1]..I[i]..O[i] */
- if (i+NUMPTS<count) { edge += 3; } /* I[i]..I[i+1]..O[i-N]..I[i] */
- if (i+1<count) { edge += 3; } /* O[i+1]..I[i+1]..I[i]..O[i+1] */
- if (i+NUMPTS+1<count) { edge += 3; } /* I[i+1]..O[i+1-N]..O[i-N]..I[i+1] */
- }
- }
- /* Free up malloc()'d memory... */
- free(outer_x);
- free(outer_y);
- free(outer_z);
- free(inner_x);
- free(inner_y);
- free(inner_z);
-
- /*
- * Second, list the edges...
- */
- printf("\n EDGE ECount %d\n", edge);
- e=0;
- face = 0;
- for (i=0; i<count-1; i++) {
- printf(" EDGE Edge %d %d %d\n", e++, i*2, (i+1)*2);
- printf(" EDGE Edge %d %d %d\n", e++, (i+1)*2, i*2+1);
- printf(" EDGE Edge %d %d %d\n", e++, i*2+1, i*2);
- face++;
- if (i+NUMPTS<count) {
- printf(" EDGE Edge %d %d %d\n", e++, i*2+1, (i+1)*2+1);
- printf(" EDGE Edge %d %d %d\n", e++, (i+1)*2+1, (i+NUMPTS)*2);
- printf(" EDGE Edge %d %d %d\n", e++, (i+NUMPTS)*2, i*2+1);
- face++;
- }
- if (i+1<count) {
- printf(" EDGE Edge %d %d %d\n", e++, (i+1)*2, (i+1)*2+1);
- printf(" EDGE Edge %d %d %d\n", e++, (i+1)*2+1, i*2+1);
- printf(" EDGE Edge %d %d %d\n", e++, i*2+1, (i+1)*2);
- face++;
- }
- if (i+NUMPTS+1<count) {
- printf(" EDGE Edge %d %d %d\n", e++, (i+1)*2+1, (i+1+NUMPTS)*2);
- printf(" EDGE Edge %d %d %d\n", e++,(i+1+NUMPTS)*2,(i+NUMPTS)*2);
- printf(" EDGE Edge %d %d %d\n", e++, (i+NUMPTS)*2, (i+1)*2+1);
- face++;
- }
- }
-
- /*
- * Third, list the faces...
- */
- printf("\n FACE Tcount %d\n", face);
- e=0;
- f=0;
- for (i=0; i<count-1; i++) {
- printf(" FACE Connect %d %d %d %d\n", f++, e++, e++, e++);
- if (i+NUMPTS<count) {
- printf(" FACE Connect %d %d %d %d\n", f++, e++, e++, e++);
- }
- if (i+1<count) {
- printf(" FACE Connect %d %d %d %d\n", f++, e++, e++, e++);
- }
- if (i+NUMPTS+1<count) {
- printf(" FACE Connect %d %d %d %d\n", f++, e++, e++, e++);
- }
- }
- /*
- * Now wrap up the object description...
- */
- printf("\nEnd DESC\nTOBJ\nEnd OBJ\n");
- exit(0);
- }
-