home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 August / chip_08_2000.iso / aktualnosci / shareware / Rhinoceros / rh11eval_20000320.exe / %MAINDIR% / RIB / patterns.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  3.9 KB  |  134 lines

  1. /**************************************************************************
  2.  * patterns.h - various "helper" routines for making patterns, including
  3.  *              antialiased versions of some useful functions.
  4.  *
  5.  * Author: Larry Gritz (gritzl@acm.org), though most are obvious to any
  6.  *         experienced shader writer.
  7.  *
  8.  * $Revision: 1.4 $    $Date: 1998/09/18 02:31:32 $
  9.  *
  10.  **************************************************************************/
  11.  
  12.  
  13. #ifndef PATTERNS_H
  14. #define PATTERNS_H 1
  15.  
  16.  
  17. /*
  18.  * Define metrics for estimating filter widths, if none has already
  19.  * been defined.  This is crucial for antialiasing.
  20.  */
  21. #ifndef MINFILTWIDTH
  22. #  define MINFILTWIDTH 1.0e-6
  23. #endif
  24. #ifndef filterwidth
  25. #  define filterwidth(x)  max (abs(Du(x)*du) + abs(Dv(x)*dv), MINFILTWIDTH)
  26. #endif
  27. #ifndef filterwidthp
  28. #  define filterwidthp(p) max (sqrt(area(p)), MINFILTWIDTH)
  29. #endif
  30.  
  31.  
  32.  
  33. /* A 1-D pulse pattern:  return 1 if edge0 <= x <= edge1, otherwise 0 */
  34. float pulse (float edge0, edge1, x)
  35. {
  36.     return step(edge0,x) - step(edge1,x);
  37. }
  38.  
  39.  
  40. /* A filtered version of pulse:  this is just an analytic solution to
  41.  * the convolution of pulse, above, with a box filter of a particular
  42.  * width.  Derivation is left to the reader.
  43.  */
  44. float filteredpulse (float edge0, edge1, x, width)
  45. {
  46.     float x0 = x - width*0.5;
  47.     float x1 = x0 + width;
  48.     return max (0, (min(x1,edge1)-max(x0,edge0)) / width);
  49. }
  50.  
  51.  
  52.  
  53.  
  54. /* A pulse train: a signal that repeats with a given period, and is
  55.  * 0 when 0 <= mod(x,period) < edge, and 1 when mod(x,period) > edge.
  56.  */
  57. float pulsetrain (float period, duty, x)
  58. {
  59.     return pulse (duty*period, 1, mod(x,period));
  60. }
  61.  
  62.  
  63.  
  64. /* Filtered pulse train:  it's not as simple as just returning the mod
  65.  * of filteredpulse -- you have to take into account that the filter may
  66.  * cover multiple pulses in the train.
  67.  * Strategy: consider the function INTFPT, which is the integral of the
  68.  * train from 0 to x.  Just subtract!
  69.  */
  70. float filteredpulsetrain (float period, duty, x, width)
  71. {
  72.     /* First, normalize so period == 1 and our domain of interest is > 0 */
  73.     float w = width/period;
  74.     float x0 = x/period - w/2;
  75.     float x1 = x0+w;
  76.     /* Now we want to integrate the normalized pulsetrain over [x0,x1] where
  77.      * 0 <= x0 < 1 and x0 < x1. 
  78.      */
  79. #define INTFPT(x) ((1-duty)*floor(x) + max(0,x-floor(x)-duty))
  80.     return (INTFPT(x1) - INTFPT(x0)) / (x1-x0);
  81. #undef INTFPT    
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. /* basic tiling pattern --
  89.  *   inputs:
  90.  *      x, y                    positions on a 2-D surface
  91.  *      tilewidth, tileheight   dimensions of each tile
  92.  *      rowstagger              how much does each row stagger relative to
  93.  *                                   the previous row
  94.  *      rowstaggervary          how much should rowstagger randomly vary
  95.  *      jaggedfreq, jaggedamp   adds noise to the edge between the tiles
  96.  *   outputs:
  97.  *      row, column             index which tile the sample is in
  98.  *      xtile, ytile            position within this tile (0-1)
  99.  */
  100. void basictile (float x, y;
  101.         uniform float tilewidth, tileheight;
  102.         uniform float rowstagger, rowstaggervary;
  103.         uniform float jaggedfreq, jaggedamp;
  104.         output float column, row;
  105.         output float xtile, ytile;
  106.     )
  107. {
  108.     point PP;
  109.     float scoord = x, tcoord = y;
  110.  
  111.     if (jaggedamp != 0.0) {
  112.     /* Make the shapes of the bricks vary just a bit */
  113.     PP = point noise (x*jaggedfreq/tilewidth, y*jaggedfreq/tileheight);
  114.     scoord += jaggedamp * xcomp (PP);
  115.     tcoord += jaggedamp * ycomp (PP);
  116.     }
  117.  
  118.     xtile = scoord / tilewidth;
  119.     ytile = tcoord / tileheight;
  120.     row = floor (ytile);   /* which brick row? */
  121.  
  122.     /* Shift the columns randomly by row */
  123.     xtile += mod (rowstagger * row, 1);
  124.     xtile += rowstaggervary * (noise (row+0.5) - 0.5);
  125.  
  126.     column = floor (xtile);
  127.     xtile -= column;
  128.     ytile -= row;
  129. }
  130.  
  131.  
  132.  
  133. #endif /* defined(PATTERNS_H) */
  134.