home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch4_4 / aspc.c
Encoding:
C/C++ Source or Header  |  1995-04-04  |  1.3 KB  |  45 lines

  1. /* aspc.c -- generic adaptive sampling of parametric curves */
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct point { double t,x,y,z; } Point;
  6.  
  7. #define T(p)    ((p)->t)
  8. #define X(p)    ((p)->x)
  9. #define Y(p)    ((p)->y)
  10. #define Z(p)    ((p)->z)
  11.  
  12. extern void gamma(Point* p);            /* user supplied */
  13. extern void line(Point* p, Point* q);   /* user supplied */
  14.  
  15. static int flat(Point* p, Point* q, Point* r);
  16.  
  17. static void sample(Point* p, Point* q)
  18. {
  19.  Point rr, *r=&rr;
  20.  double t = 0.45 + 0.1 * (rand()/(double) RAND_MAX);
  21.  T(r) = T(p) + t*(T(q)-T(p));
  22.  gamma(r);
  23.  if (flat(p,q,r)) line(p,q); else { sample(p,r); sample(r,q); }
  24. }
  25.  
  26. static int flat(Point* p, Point* q, Point* r)
  27. {
  28.  extern double tol;                     /* user supplied */
  29.  double xp = X(p)-X(r); double yp = Y(p)-Y(r); double zp = Z(p)-Z(r);
  30.  double xq = X(q)-X(r); double yq = Y(q)-Y(r); double zq = Z(q)-Z(r);
  31.  double x =  yp*zq-yq*zp;
  32.  double y =  xp*zq-xq*zp;
  33.  double z =  xp*yq-xq*yp;
  34.  return (x*x+y*y+z*z) < tol;            /* |pr x qr|^2 < tol */
  35. }
  36.  
  37. void aspc(double a, double b)           /* entry point */
  38. {
  39.  Point pp, *p = &pp;
  40.  Point qq, *q = &qq;
  41.  srand(time(0));                        /* randomize */
  42.  T(p)= a; gamma(p); T(q)=b; gamma(q);   /* set up */
  43.  sample(p,q);                           /* sample */
  44. }
  45.