Back to index

radiance  4R0+20100331
genrev.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: genrev.c,v 2.10 2008/10/11 04:39:34 greg Exp $";
00003 #endif
00004 /*
00005  *  genrev.c - program to generate functions of rotation about z
00006  *
00007  *     The program takes as input the functions of t for z and r
00008  *     (the radius).  If z is increasing, the normal points
00009  *     outward, inward for decreasing z.  Negative radii are forbidden.
00010  *
00011  *     8/6/86
00012  */
00013 
00014 #include  <stdlib.h>
00015 #include  <stdio.h>
00016 #include  <string.h>
00017 #include  <math.h>
00018 
00019 #include  "rterror.h"
00020 #include  "resolu.h"
00021 #include  "calcomp.h"
00022 
00023 #define  ZNAME              "Z`SYS`"             /* z function name */
00024 #define  RNAME              "R`SYS`"             /* r function name */
00025 
00026 #define  PI          3.14159265358979323846
00027 
00028 #define  FTINY              1e-9
00029                                    /* orientation */
00030 #define  OUT         01
00031 #define  IN          02
00032 #define  UP          04
00033 #define  DOWN        010
00034 
00035 
00036 void
00037 computen(nzp, nrp, z0, r0, z1, r1)        /* compute normal */
00038 double  *nzp, *nrp, z0, r0, z1, r1;
00039 {
00040        double  dr, dz, len;
00041 
00042        dz = r0 - r1;                      /* right angle vector */
00043        dr = z1 - z0;
00044        len = sqrt(dr*dr + dz*dz);
00045        *nzp = dz/len;
00046        *nrp = dr/len;
00047 }
00048 
00049 
00050 double
00051 l_hermite(char *nm)
00052 {
00053        double  t;
00054        
00055        t = argument(5);
00056        return(       argument(1)*((2.0*t-3.0)*t*t+1.0) +
00057               argument(2)*(-2.0*t+3.0)*t*t +
00058               argument(3)*((t-2.0)*t+1.0)*t +
00059               argument(4)*(t-1.0)*t*t );
00060 }
00061 
00062 
00063 double
00064 l_bezier(char *nm)
00065 {
00066        double  t;
00067 
00068        t = argument(5);
00069        return(       argument(1) * (1.+t*(-3.+t*(3.-t))) +
00070               argument(2) * 3.*t*(1.+t*(-2.+t)) +
00071               argument(3) * 3.*t*t*(1.-t) +
00072               argument(4) * t*t*t );
00073 }
00074 
00075 
00076 double
00077 l_bspline(char *nm)
00078 {
00079        double  t;
00080 
00081        t = argument(5);
00082        return(       argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
00083               argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
00084               argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
00085               argument(4) * (1./6.*t*t*t) );
00086 }
00087 
00088 
00089 int
00090 main(argc, argv)
00091 int  argc;
00092 char  *argv[];
00093 {
00094        char  stmp[256];
00095        char  *modname;
00096        int  smooth = 0;
00097        double  t, lastz, z, nextz, lastr, r, nextr;
00098        double  lastnz, lastnr, nz, nr, nextnz, nextnr;
00099        int  i, nseg;
00100        int  orient;
00101 
00102        varset("PI", ':', PI);
00103        funset("hermite", 5, ':', l_hermite);
00104        funset("bezier", 5, ':', l_bezier);
00105        funset("bspline", 5, ':', l_bspline);
00106 
00107        if (argc < 6)
00108               goto userror;
00109 
00110        for (i = 6; i < argc; i++)
00111               if (!strcmp(argv[i], "-e"))
00112                      scompile(argv[++i], NULL, 0);
00113               else if (!strcmp(argv[i], "-f"))
00114                      fcompile(argv[++i]);
00115               else if (!strcmp(argv[i], "-s"))
00116                      smooth = 1;
00117               else
00118                      goto userror;
00119 
00120        sprintf(stmp, "%s(t)=%s;", ZNAME, argv[3]);
00121        scompile(stmp, NULL, 0);
00122        sprintf(stmp, "%s(t)=%s;", RNAME, argv[4]);
00123        scompile(stmp, NULL, 0);
00124        nseg = atoi(argv[5]);
00125        if (nseg <= 0)
00126               goto userror;
00127        modname = smooth ? "Phong" : argv[1];
00128 
00129        fputs("# ", stdout);
00130        printargs(argc, argv, stdout);
00131        eclock = 0;
00132 
00133        lastnz = lastnr = 0.0;
00134        t = 0.0;
00135        lastz = funvalue(ZNAME, 1, &t);
00136        lastr = funvalue(RNAME, 1, &t);
00137        t = 1.0/nseg;
00138        z = funvalue(ZNAME, 1, &t);
00139        r = funvalue(RNAME, 1, &t);
00140        computen(&nz, &nr, lastz, lastr, z, r);
00141        for (i = 1; i <= nseg; i++) {
00142               if (i < nseg) {
00143                      t = (double)(i+1)/nseg;
00144                      nextz = funvalue(ZNAME, 1, &t);
00145                      nextr = funvalue(RNAME, 1, &t);
00146                      computen(&nextnz, &nextnr, z, r, nextz, nextr);
00147               } else
00148                      nextnz = nextnr = 0.0;
00149               orient = 0;
00150               if (z < lastz-FTINY)
00151                      orient |= DOWN;
00152               else if (z > lastz+FTINY)
00153                      orient |= UP;
00154               if (r < lastr-FTINY)
00155                      orient |= IN;
00156               else if (r > lastr+FTINY)
00157                      orient |= OUT;
00158               if (!orient)
00159                      goto endfor;
00160               if (smooth) {
00161                      printf("\n%s texfunc Phong\n", argv[1]);
00162                      printf("4 rev_dx rev_dy rev_dz rev.cal\n");
00163                      printf("0\n4\n");
00164                      if (orient&(UP|DOWN)) {
00165                             t = (nextnz - lastnz)/(z - lastz);
00166                             printf("\t%18.12g\t%18.12g\n",
00167                                           t, lastnz - t*lastz);
00168                      } else
00169                             printf("\t0\t%d\n", orient&IN ? 1 : -1);
00170                      if (orient&(OUT|IN))  {
00171                             t = (nextnr - lastnr)/(r - lastr);
00172                             printf("\t%18.12g\t%18.12g\n",
00173                                           t, lastnr - t*lastr);
00174                      } else
00175                             printf("\t0\t%d\n", orient&UP ? 1 : -1);
00176               }
00177               if (!(orient&(IN|OUT))) {
00178                      printf("\n%s %s %s.%d\n", modname,
00179                                    orient&DOWN ? "tube" : "cylinder",
00180                                    argv[2], i);
00181                      printf("0\n0\n7\n");
00182                      printf("\t0\t0\t%18.12g\n", lastz);
00183                      printf("\t0\t0\t%18.12g\n", z);
00184                      printf("\t%18.12g\n", r);
00185               } else if (!(orient&(UP|DOWN))) {
00186                      printf("\n%s ring %s.%d\n", modname, argv[2], i);
00187                      printf("0\n0\n8\n");
00188                      printf("\t0\t0\t%18.12g\n", z);
00189                      printf("\t0\t0\t%18.12g\n", orient&IN ? 1.0 : -1.0);
00190                      printf("\t%18.12g\t%18.12g\n", lastr, r);
00191               } else {
00192                      printf("\n%s %s %s.%d\n", modname,
00193                                    orient&DOWN ? "cup" : "cone",
00194                                    argv[2], i);
00195                      printf("0\n0\n8\n");
00196                      printf("\t0\t0\t%18.12g\n", lastz);
00197                      printf("\t0\t0\t%18.12g\n", z);
00198                      printf("\t%18.12g\t%18.12g\n", lastr, r);
00199               }
00200        endfor:
00201               lastz = z; lastr = r;
00202               z = nextz; r = nextr;
00203               lastnz = nz; lastnr = nr;
00204               nz = nextnz; nr = nextnr;
00205        }
00206        return 0;
00207 
00208 userror:
00209        fprintf(stderr,
00210        "Usage: %s material name z(t) r(t) nseg [-e expr] [-f file] [-s]\n",
00211                      argv[0]);
00212        return 1;
00213 }
00214 
00215