/* Copyright 1994, 2002 by Steven Worley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. A detailed description and application examples can be found in the 1996 SIGGRAPH paper "A Cellular Texture Basis Function" and especially in the 2002 book "Texturing and Modeling, a Procedural Approach, 3rd edition." There is also extra information on the web site http://www.worley.com/cellular.html . If you do find interesting uses for this tool, and especially if you enhance it, please drop me an email at steve@worley.com. */ #include "AppHdr.h" #include #include #include #include #include "worley.h" /* Function prototype */ namespace worley { /* This macro is a *lot* faster than using (int32_t)floor() on an x86 CPU. It actually speeds up the entire _worley() call with almost 10%. Added by Stefan Gustavson, October 2003. */ #define LFLOOR(x) ((x)<0 ? ((int32_t)x-1) : ((int32_t)x) ) /* A hardwired lookup table to quickly determine how many feature points should be in each spatial cube. We use a table so we don't need to make multiple slower tests. A random number indexed into this array will give an approximate Poisson distribution of mean density 2.5. Read the book for the int32_twinded explanation. */ static int Poisson_count[256]= {4,3,1,1,1,2,4,2,2,2,5,1,0,2,1,2,2,0,4,3,2,1,2,1,3,2,2,4,2,2,5,1,2,3,2,2,2,2,2,3, 2,4,2,5,3,2,2,2,5,3,3,5,2,1,3,3,4,4,2,3,0,4,2,2,2,1,3,2,2,2,3,3,3,1,2,0,2,1,1,2, 2,2,2,5,3,2,3,2,3,2,2,1,0,2,1,1,2,1,2,2,1,3,4,2,2,2,5,4,2,4,2,2,5,4,3,2,2,5,4,3, 3,3,5,2,2,2,2,2,3,1,1,4,2,1,3,3,4,3,2,4,3,3,3,4,5,1,4,2,4,3,1,2,3,5,3,2,1,3,1,3, 3,3,2,3,1,5,5,4,2,2,4,1,3,4,1,5,3,3,5,3,4,3,2,2,1,1,1,1,1,2,4,5,4,5,4,2,1,5,1,1, 2,3,3,3,2,5,2,3,3,2,0,2,1,1,4,2,1,3,2,1,2,2,3,2,5,5,3,4,5,5,2,4,4,5,3,2,2,2,1,4, 2,3,3,4,2,5,4,2,4,2,2,2,4,5,3,2}; /* This constant is manipulated to make sure that the mean value of F[0] is 1.0. This makes an easy natural "scale" size of the cellular features. */ #define DENSITY_ADJUSTMENT 0.398150 /* the function to merge-sort a "cube" of samples into the current best-found list of values. */ static void AddSamples(int32_t xi, int32_t yi, int32_t zi, int32_t max_order, double at[3], double *F, double (*delta)[3], uint32_t *ID); /* The main function! */ static void _worley(double at[3], int32_t max_order, double *F, double (*delta)[3], uint32_t *ID) { double x2,y2,z2, mx2, my2, mz2; double new_at[3]; int32_t int_at[3], i; /* Initialize the F values to "huge" so they will be replaced by the first real sample tests. Note we'll be storing and comparing the SQUARED distance from the feature points to avoid lots of slow sqrt() calls. We'll use sqrt() only on the final answer. */ for (i=0; i>24)%256]; /* 256 element lookup table. Use MSB */ seed=1402024253*seed+586950981; /* churn the seed with good Knuth LCG */ for (j=0; j0 && d2 */ /* Bump down more distant information to make room for this new point. */ for (i=max_order-2; i>=index; i--) { F[i+1]=F[i]; ID[i+1]=ID[i]; delta[i+1][0]=delta[i][0]; delta[i+1][1]=delta[i][1]; delta[i+1][2]=delta[i][2]; } /* Insert the new point's information into the list. */ F[index]=d2; ID[index]=this_id; delta[index][0]=dx; delta[index][1]=dy; delta[index][2]=dz; } } return; } noise_datum noise(double x, double y, double z) { double point[3] = {x,y,z}; double F[2]; double delta[2][3]; uint32_t id[2]; _worley(point, 2, F, delta, id); noise_datum datum; datum.distance[0] = F[0]; datum.distance[1] = F[1]; datum.id[0] = id[0]; datum.id[1] = id[1]; for (int i = 0; i < 2; ++i) for (int j = 0; j < 3; ++j) datum.pos[i][j] = delta[i][j]; return datum; } }