Advertisement
Guest User

asthmatic_thematic

a guest
Feb 16th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.30 KB | None | 0 0
  1. // some content:
  2. //        Written by: Paul E. Martz
  3. //        Copyright 1997 by Paul E. Martz, all right reserved
  4. //        Non-commercial use by individuals is permitted.
  5. // diamond square algorithm itself is public domain
  6.  
  7. // float random
  8. float rndmf(float high) {
  9.   return (high*rand()/(RAND_MAX+1.0));
  10. }
  11.  
  12. static float randnum (float min, float max) {
  13.   return ((float)(min + (max-min)*R_ONE));
  14. }
  15.  
  16. /*
  17.  * fractRand is a useful interface to randnum.
  18.  */
  19. #define fractRand(v) randnum (-v, v)
  20.  
  21.  
  22. /*
  23.  * avgEndpoints - Given the i location and a stride to the data
  24.  * values, return the average those data values. "i" can be thought of
  25.  * as the data value in the center of two line endpoints. We use
  26.  * "stride" to get the values of the endpoints. Averaging them yields
  27.  * the midpoint of the line.
  28.  *
  29.  * Called by fill1DFractArray.
  30.  */
  31. static float avgEndpoints (int i, int stride, float *fa)
  32. {
  33.     return ((float) (fa[i-stride] +
  34.              fa[i+stride]) * .5f);
  35. }
  36.  
  37. float distance (float i,float j,float k,float l)
  38. {
  39.   return sqrt((i-j)*(i-j)+(k-l)*(k-l));
  40. }
  41.  
  42. /*
  43.  * avgDiamondVals - Given the i,j location as the center of a diamond,
  44.  * average the data values at the four corners of the diamond and
  45.  * return it. "Stride" represents the distance from the diamond center
  46.  * to a diamond corner.
  47.  *
  48.  * Called by fill2DFractArray.
  49.  */
  50. static float avgDiamondVals (int i, int j, int stride,
  51.                  int size, int subSize, float *fa)
  52. {
  53.     /* In this diagram, our input stride is 1, the i,j location is
  54.        indicated by "X", and the four value we want to average are
  55.        "*"s:
  56.            .   *   .
  57.  
  58.            *   X   *
  59.  
  60.            .   *   .
  61.        */
  62.  
  63.     /* In order to support tiled surfaces which meet seamless at the
  64.        edges (that is, they "wrap"), We need to be careful how we
  65.        calculate averages when the i,j diamond center lies on an edge
  66.        of the array. The first four 'if' clauses handle these
  67.        cases. The final 'else' clause handles the general case (in
  68.        which i,j is not on an edge).
  69.      */
  70.     if (i == 0)
  71.     return ((float) (fa[(i*size) + j-stride] +
  72.              fa[(i*size) + j+stride] +
  73.              fa[((subSize-stride)*size) + j] +
  74.              fa[((i+stride)*size) + j]) * .25f);
  75.     else if (i == size-1)
  76.     return ((float) (fa[(i*size) + j-stride] +
  77.              fa[(i*size) + j+stride] +
  78.              fa[((i-stride)*size) + j] +
  79.              fa[((0+stride)*size) + j]) * .25f);
  80.     else if (j == 0)
  81.     return ((float) (fa[((i-stride)*size) + j] +
  82.              fa[((i+stride)*size) + j] +
  83.              fa[(i*size) + j+stride] +
  84.              fa[(i*size) + subSize-stride]) * .25f);
  85.     else if (j == size-1)
  86.     return ((float) (fa[((i-stride)*size) + j] +
  87.              fa[((i+stride)*size) + j] +
  88.              fa[(i*size) + j-stride] +
  89.              fa[(i*size) + 0+stride]) * .25f);
  90.     else
  91.     return ((float) (fa[((i-stride)*size) + j] +
  92.              fa[((i+stride)*size) + j] +
  93.              fa[(i*size) + j-stride] +
  94.              fa[(i*size) + j+stride]) * .25f);
  95. }
  96.  
  97.  
  98. /*
  99.  * avgSquareVals - Given the i,j location as the center of a square,
  100.  * average the data values at the four corners of the square and return
  101.  * it. "Stride" represents half the length of one side of the square.
  102.  *
  103.  * Called by fill2DFractArray.
  104.  */
  105. static float avgSquareVals (int i, int j, int stride, int size, float *fa)
  106. {
  107.     /* In this diagram, our input stride is 1, the i,j location is
  108.        indicated by "*", and the four value we want to average are
  109.        "X"s:
  110.            X   .   X
  111.  
  112.            .   *   .
  113.  
  114.            X   .   X
  115.        */
  116.     return ((float) (fa[((i-stride)*size) + j-stride] +
  117.              fa[((i-stride)*size) + j+stride] +
  118.              fa[((i+stride)*size) + j-stride] +
  119.              fa[((i+stride)*size) + j+stride]) * .25f);
  120. }
  121.  
  122.  
  123.   // ifdef DEBUG
  124. /*
  125.  * dump1DFractArray - Use for debugging.
  126.  */
  127. void dump1DFractArray (float *fa, int size)
  128. {
  129.     int i;
  130.  
  131.     for (i=0; i<size; i++)
  132.     printf ("(%.2f)   ", fa[i]);
  133.     printf ("\n");
  134. }
  135.  
  136. /*
  137.  * dump2DFractArray - Use for debugging.
  138.  */
  139. void dump2DFractArray (float *fa, int size)
  140. {
  141.     int i, j;
  142.  
  143.     for (i=0; i<size; i++) {
  144.     j=0;
  145.     // printf ("[%d,%d]: ", i, j);
  146.     for (; j<size; j++) {
  147.         printf ("%.2f   ",
  148.             fa[(i*size)+j]);
  149.     }
  150.     printf ("\n");
  151.     }
  152. }
  153.   // endif
  154.  
  155.  
  156. /*
  157.  * powerOf2 - Returns 1 if size is a power of 2. Returns 0 if size is
  158.  * not a power of 2, or is zero.
  159.  */
  160. static int powerOf2 (int size)
  161. {
  162.     int i, bitcount = 0;
  163.  
  164.     /* Note this code assumes that (sizeof(int)*8) will yield the
  165.        number of bits in an int. Should be portable to most
  166.        platforms. */
  167.     for (i=0; i<sizeof(int)*8; i++)
  168.     if (size & (1<<i))
  169.         bitcount++;
  170.     if (bitcount == 1)
  171.     /* One bit. Must be a power of 2. */
  172.     return (1);
  173.     else
  174.     /* either size==0, or size not a power of 2. Sorry, Charlie. */
  175.     return (0);
  176. }
  177.  
  178.  
  179. /*
  180.  * fill1DFractArray - Tessalate an array of values into an
  181.  * approximation of fractal Brownian motion.
  182.  */
  183. void fill1DFractArray (float *fa, int size,
  184.                int seedValue, float heightScale, float h)
  185. {
  186.     int i;
  187.     int stride;
  188.     int subSize;
  189.     float ratio, scale;
  190.  
  191.     if (!powerOf2(size) || (size==1)) {
  192.     /* We can't tesselate the array if it is not a power of 2. */
  193. #ifdef DEBUG
  194.     printf ("Error: fill1DFractArray: size %d is not a power of 2.\n");
  195. #endif /* DEBUG */
  196.     return;
  197.     }
  198.  
  199.     /* subSize is the dimension of the array in terms of connected line
  200.        segments, while size is the dimension in terms of number of
  201.        vertices. */
  202.     subSize = size;
  203.     size++;
  204.    
  205.     /* initialize random number generator */
  206.     srandom (seedValue);
  207.  
  208. #ifdef DEBUG
  209.     printf ("initialized\n");
  210.     dump1DFractArray (fa, size);
  211. #endif
  212.  
  213.     /* Set up our roughness constants.
  214.        Random numbers are always generated in the range 0.0 to 1.0.
  215.        'scale' is multiplied by the randum number.
  216.        'ratio' is multiplied by 'scale' after each iteration
  217.        to effectively reduce the randum number range.
  218.        */
  219.     ratio = (float) pow (2.,-h);
  220.     scale = heightScale * ratio;
  221.  
  222.     /* Seed the endpoints of the array. To enable seamless wrapping,
  223.        the endpoints need to be the same point. */
  224.     stride = subSize / 2;
  225.     fa[0] =
  226.       fa[subSize] = 0.f;
  227.  
  228. #ifdef DEBUG
  229.     printf ("seeded\n");
  230.     dump1DFractArray (fa, size);
  231. #endif
  232.  
  233.     while (stride) {
  234.         for (i=stride; i<subSize; i+=stride) {
  235.             fa[i] = scale * fractRand (.5f) +
  236.                 avgEndpoints (i, stride, fa);
  237.  
  238.             /* reduce random number range */
  239.             scale *= ratio;
  240.  
  241.             i+=stride;
  242.         }
  243.         stride >>= 1;
  244.     }
  245.  
  246. #ifdef DEBUG
  247.     printf ("complete\n");
  248.     dump1DFractArray (fa, size);
  249. #endif
  250. }
  251.  
  252.  
  253. /*
  254.  * fill2DFractArray - Use the diamond-square algorithm to tessalate a
  255.  * grid of float values into a fractal height map.
  256.  */
  257. void fill2DFractArray (float *fa, int size,
  258.                int seedValue, float heightScale, float h)
  259. {
  260.     int i, j;
  261.     int stride;
  262.     int oddline;
  263.     int subSize;
  264.     float ratio, scale;
  265.  
  266.     if (!powerOf2(size) || (size==1)) {
  267.     /* We can't tesselate the array if it is not a power of 2. */
  268. #ifdef DEBUG
  269.     printf ("Error: fill2DFractArray: size %d is not a power of 2.\n");
  270. #endif /* DEBUG */
  271.     return;
  272.     }
  273.  
  274.     /* subSize is the dimension of the array in terms of connected line
  275.        segments, while size is the dimension in terms of number of
  276.        vertices. */
  277.     subSize = size;
  278.     size++;
  279.    
  280.     /* initialize random number generator */
  281.     srandom (seedValue);
  282.    
  283. #ifdef DEBUG
  284.     printf ("initialized\n");
  285.     dump2DFractArray (fa, size);
  286. #endif
  287.  
  288.     /* Set up our roughness constants.
  289.        Random numbers are always generated in the range 0.0 to 1.0.
  290.        'scale' is multiplied by the randum number.
  291.        'ratio' is multiplied by 'scale' after each iteration
  292.        to effectively reduce the randum number range.
  293.        */
  294.     ratio = (float) pow (2.,-h);
  295.     scale = heightScale * ratio;
  296.  
  297.     /* Seed the first four values. For example, in a 4x4 array, we
  298.        would initialize the data points indicated by '*':
  299.  
  300.            *   .   .   .   *
  301.  
  302.            .   .   .   .   .
  303.  
  304.            .   .   .   .   .
  305.  
  306.            .   .   .   .   .
  307.  
  308.            *   .   .   .   *
  309.  
  310.        In terms of the "diamond-square" algorithm, this gives us
  311.        "squares".
  312.  
  313.        We want the four corners of the array to have the same
  314.        point. This will allow us to tile the arrays next to each other
  315.        such that they join seemlessly. */
  316.  
  317.     stride = subSize / 2;
  318.     fa[(0*size)+0] =
  319.       fa[(subSize*size)+0] =
  320.         fa[(subSize*size)+subSize] =
  321.           fa[(0*size)+subSize] = 0.f;
  322.    
  323. #ifdef DEBUG
  324.     printf ("seeded\n");
  325.     dump2DFractArray (fa, size);
  326. #endif
  327.  
  328.     /* Now we add ever-increasing detail based on the "diamond" seeded
  329.        values. We loop over stride, which gets cut in half at the
  330.        bottom of the loop. Since it's an int, eventually division by 2
  331.        will produce a zero result, terminating the loop. */
  332.     while (stride) {
  333.         /* Take the existing "square" data and produce "diamond"
  334.            data. On the first pass through with a 4x4 matrix, the
  335.            existing data is shown as "X"s, and we need to generate the
  336.            "*" now:
  337.  
  338.                X   .   .   .   X
  339.  
  340.                .   .   .   .   .
  341.  
  342.                .   .   *   .   .
  343.  
  344.                .   .   .   .   .
  345.  
  346.                X   .   .   .   X
  347.  
  348.           It doesn't look like diamonds. What it actually is, for the
  349.           first pass, is the corners of four diamonds meeting at the
  350.           center of the array. */
  351.         for (i=stride; i<subSize; i+=stride) {
  352.             for (j=stride; j<subSize; j+=stride) {
  353.                 fa[(i * size) + j] =
  354.                     scale * fractRand (.5f) +
  355.                     avgSquareVals (i, j, stride, size, fa);
  356.                 j += stride;
  357.             }
  358.             i += stride;
  359.         }
  360. #ifdef DEBUG
  361.         printf ("Diamonds:\n");
  362.         dump2DFractArray (fa, size);
  363. #endif
  364.  
  365.         /* Take the existing "diamond" data and make it into
  366.            "squares". Back to our 4X4 example: The first time we
  367.            encounter this code, the existing values are represented by
  368.            "X"s, and the values we want to generate here are "*"s:
  369.  
  370.                X   .   *   .   X
  371.  
  372.                .   .   .   .   .
  373.  
  374.                *   .   X   .   *
  375.  
  376.                .   .   .   .   .
  377.  
  378.                X   .   *   .   X
  379.  
  380.            i and j represent our (x,y) position in the array. The
  381.            first value we want to generate is at (i=2,j=0), and we use
  382.            "oddline" and "stride" to increment j to the desired value.
  383.            */
  384.         oddline = 0;
  385.         for (i=0; i<subSize; i+=stride) {
  386.             oddline = (oddline == 0);
  387.             for (j=0; j<subSize; j+=stride) {
  388.                 if ((oddline) && !j) j+=stride;
  389.  
  390.                 /* i and j are setup. Call avgDiamondVals with the
  391.                    current position. It will return the average of the
  392.                    surrounding diamond data points. */
  393.                 fa[(i * size) + j] =
  394.                     scale * fractRand (.5f) +
  395.                     avgDiamondVals (i, j, stride, size, subSize, fa);
  396.  
  397.                 /* To wrap edges seamlessly, copy edge values around
  398.                    to other side of array */
  399.                 if (i==0)
  400.                     fa[(subSize*size) + j] =
  401.                         fa[(i * size) + j];
  402.                 if (j==0)
  403.                     fa[(i*size) + subSize] =
  404.                         fa[(i * size) + j];
  405.  
  406.                 j+=stride;
  407.             }
  408.         }
  409. #ifdef DEBUG
  410.         printf ("Squares:\n");
  411.         dump2DFractArray (fa, size);
  412. #endif
  413.  
  414.         /* reduce random number range. */
  415.         scale *= ratio;
  416.         stride >>= 1;
  417.     }
  418.  
  419. #ifdef DEBUG
  420.     printf ("complete\n");
  421.     dump2DFractArray (fa, size);
  422. #endif
  423. }
  424.  
  425.  
  426. /*
  427.  * alloc1DFractArray - Allocate float-sized data points for a 1D strip
  428.  * containing size line segments.
  429.  */
  430. float *alloc1DFractArray (int size)
  431. {
  432.     /* Increment size (see comment in alloc2DFractArray, below, for an
  433.        explanation). */
  434.     size++;
  435.  
  436.     return ((float *) malloc (sizeof(float) * size));
  437. }
  438.  
  439. /*
  440.  * alloc2DFractArray - Allocate float-sized data points for a sizeXsize
  441.  * mesh.
  442.  */
  443. float *alloc2DFractArray (int size)
  444. {
  445.     /* For a sizeXsize array, we need (size+1)X(size+1) space. For
  446.        example, a 2x2 mesh needs 3x3=9 data points:
  447.  
  448.            *   *   *
  449.  
  450.            *   *   *
  451.  
  452.            *   *   *
  453.  
  454.        To account for this, increment 'size'. */
  455.     size++;
  456.  
  457.     return ((float *) malloc (sizeof(float) * size * size));
  458. }
  459.  
  460.  
  461.  
  462. /*
  463.  * freeFractArray - Takes a pointer to float and frees it. Can be used
  464.  * to free data that was allocated by either alloc1DFractArray or
  465.  * alloc2DFractArray.
  466.  */
  467. void freeFractArray (float *fa)
  468. {
  469.     free (fa);
  470. }
  471.  
  472.  
  473.  
  474. static void genNormal (float x1, float y1, float z1,
  475.                float x2, float y2, float z2,
  476.                float x3, float y3, float z3,
  477.                float *normal)
  478. {
  479.     float   len;
  480.     float   v1x, v1y, v1z;
  481.     float   v2x, v2y, v2z;
  482.  
  483.  
  484.     v1x = x2 - x1;
  485.     v1y = y2 - y1;
  486.     v1z = z2 - z1;
  487.     v2x = x3 - x1;
  488.     v2y = y3 - y1;
  489.     v2z = z3 - z1;
  490.  
  491.     normal[0] = v1y*v2z - v1z*v2y;
  492.     normal[1] = v1z*v2x - v1x*v2z;
  493.     normal[2] = v1x*v2y - v1y*v2x;
  494.  
  495.     len = (float) sqrt (normal[0]*normal[0] + normal[1]*normal[1] +
  496.             normal[2]*normal[2]);
  497.  
  498.     normal[0] /= len;
  499.     normal[1] /= len;
  500.     normal[2] /= len;
  501. }
  502.  
  503.  void segment(float * arr, float s0, float s1, int nsegs)
  504.   {
  505.     // fills in already allocated array of floats
  506.     int i;
  507.     float l[nsegs], ls=0;
  508.  
  509.     for (i=0; i<nsegs; i++) {
  510.       l[i] = rand()%1000;
  511.       ls += l[i];
  512.     }
  513.     arr[0] = s0;
  514.     for (i=1; i<nsegs; i++) arr[i] = arr[i-1] + l[i]*(s1-s0)/ls;
  515.  
  516.     // expects there to be one more point, to be set to s1
  517.     arr[nsegs] = s1;
  518.   }
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525. /*
  526. //  quickSort
  527. //
  528. //  This public-domain C implementation by Darel Rex Finley.
  529. //
  530. //  * Returns YES if sort was successful, or NO if the nested
  531. //    pivots went too deep, in which case your array will have
  532. //    been re-ordered, but probably not sorted correctly.
  533. //
  534. //  * This function assumes it is called with valid parameters.
  535. //
  536. //  * Example calls:
  537. //    quickSort(&myArray[0],5); // sorts elements 0, 1, 2, 3, and 4
  538. //    quickSort(&myArray[3],5); // sorts elements 3, 4, 5, 6, and 7
  539.  
  540. bool quickSort(int *arr, int elements) {
  541.  
  542.   #define  MAX_LEVELS  1000
  543.  
  544.   int  piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R ;
  545.  
  546.   beg[0]=0; end[0]=elements;
  547.   while (i>=0) {
  548.     L=beg[i]; R=end[i]-1;
  549.     if (L<R) {
  550.       piv=arr[L]; if (i==MAX_LEVELS-1) return NO;
  551.       while (L<R) {
  552.         while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R];
  553.         while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L]; }
  554.       arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L; }
  555.     else {
  556.       i--; }}
  557.   return YES; }
  558.  
  559. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement